1 # SPDX-License-Identifier: 0BSD
3 #############################################################################
5 # CMake support for building XZ Utils
7 # The complete CMake-based build hasn't been tested much yet and
8 # thus it's still slightly experimental. Testing this especially
9 # outside GNU/Linux and Windows would be great now.
11 # A few things are still missing compared to the Autotools-based build:
13 # - 32-bit x86 assembly code for CRC32 and CRC64 isn't used by default.
14 # Use the option -DENABLE_X86_ASM=ON on the CMake command line to
15 # enable the assembly files. They are compatible with Linux, *BSDs,
16 # Cygwin, MinGW-w64, and Darwin. They are NOT compatible with MSVC.
18 # NOTE: The C code includes a generic version compatible with all
19 # processors and CLMUL version that requires a new enough processor
20 # with the PCLMULQDQ instruction. If the 32-bit x86 assembly files
21 # are used, the CLMUL version in the C code is NOT built. On modern
22 # processors with CLMUL support, the C code should be faster than
23 # the assembly code while on old processors the assembly code wins.
25 # - External SHA-256 code isn't supported but it's disabled by
26 # default in the Autotools build too (--enable-external-sha256).
28 # - Extra compiler warning flags aren't added by default.
30 # About CMAKE_BUILD_TYPE:
32 # - CMake's standard choices are fine to use for production builds,
33 # including "Release" and "RelWithDebInfo".
35 # NOTE: While "Release" uses -O3 by default with some compilers,
36 # this file overrides -O3 to -O2 for "Release" builds if
37 # CMAKE_C_FLAGS_RELEASE is not defined by the user. At least
38 # with GCC and Clang/LLVM, -O3 doesn't seem useful for this
39 # package as it can result in bigger binaries without any
40 # improvement in speed compared to -O2.
42 # - Empty value (the default) is handled slightly specially: It
43 # adds -DNDEBUG to disable debugging code (assert() and a few
44 # other things). No optimization flags are added so an empty
45 # CMAKE_BUILD_TYPE is an easy way to build with whatever
46 # optimization flags one wants, and so this method is also
47 # suitable for production builds.
49 # If debugging is wanted when using empty CMAKE_BUILD_TYPE,
50 # include -UNDEBUG in the CFLAGS environment variable or
51 # in the CMAKE_C_FLAGS CMake variable to override -DNDEBUG.
52 # With empty CMAKE_BUILD_TYPE, the -UNDEBUG option will go
53 # after the -DNDEBUG option on the compiler command line and
54 # thus NDEBUG will be undefined.
56 # - Non-standard build types like "None" aren't treated specially
57 # and thus won't have -DNEBUG. Such non-standard build types
58 # SHOULD BE AVOIDED FOR PRODUCTION BUILDS. Or at least one
59 # should remember to add -DNDEBUG.
61 # If building from xz.git instead of a release tarball, consider
62 # the following *before* running cmake:
64 # - To get translated messages, install GNU gettext tools (the
65 # command msgfmt is needed). Alternatively disable translations
66 # by setting ENABLE_NLS=OFF.
68 # - To get translated man pages, run po4a/update-po which requires
69 # the po4a tool. The build works without this step too.
71 # This file provides the following installation components (if you only
72 # need liblzma, install only its components!):
73 # - liblzma_Runtime (shared library only)
74 # - liblzma_Development
75 # - liblzma_Documentation (examples and Doxygen-generated API docs as HTML)
76 # - xz_Runtime (xz, the symlinks, and possibly translation files)
77 # - xz_Documentation (xz man pages and the symlinks)
79 # - xzdec_Documentation (xzdec *and* lzmadec man pages)
82 # - lzmainfo_Documentation (lzmainfo man pages)
83 # - scripts_Runtime (xzdiff, xzgrep, xzless, xzmore)
84 # - scripts_Documentation (their man pages)
85 # - Documentation (generic docs like README and licenses)
87 # To find the target liblzma::liblzma from other packages, use the CONFIG
88 # option with find_package() to avoid a conflict with the FindLibLZMA module
89 # with case-insensitive file systems. For example, to require liblzma 5.2.5
90 # or a newer compatible version:
92 # find_package(liblzma 5.2.5 REQUIRED CONFIG)
93 # target_link_libraries(my_application liblzma::liblzma)
95 #############################################################################
97 # Author: Lasse Collin
99 #############################################################################
101 # NOTE: Translation support is disabled with CMake older than 3.20.
102 cmake_minimum_required(VERSION 3.14...3.30 FATAL_ERROR)
104 include(CMakePushCheckState)
105 include(CheckIncludeFile)
106 include(CheckSymbolExists)
107 include(CheckStructHasMember)
108 include(CheckCSourceCompiles)
109 include(cmake/tuklib_large_file_support.cmake)
110 include(cmake/tuklib_integer.cmake)
111 include(cmake/tuklib_cpucores.cmake)
112 include(cmake/tuklib_physmem.cmake)
113 include(cmake/tuklib_progname.cmake)
114 include(cmake/tuklib_mbstr.cmake)
116 set(PACKAGE_NAME "XZ Utils")
117 set(PACKAGE_BUGREPORT "xz@tukaani.org")
118 set(PACKAGE_URL "https://tukaani.org/xz/")
120 # Get the package version from version.h into PACKAGE_VERSION variable.
121 file(READ src/liblzma/api/lzma/version.h PACKAGE_VERSION)
124 #define LZMA_VERSION_MAJOR ([0-9]+)\n\
126 #define LZMA_VERSION_MINOR ([0-9]+)\n\
128 #define LZMA_VERSION_PATCH ([0-9]+)\n\
130 "\\1.\\2.\\3" PACKAGE_VERSION "${PACKAGE_VERSION}")
132 # With several compilers, CMAKE_BUILD_TYPE=Release uses -O3 optimization
133 # which results in bigger code without a clear difference in speed. If
134 # no user-defined CMAKE_C_FLAGS_RELEASE is present, override -O3 to -O2
135 # to make it possible to recommend CMAKE_BUILD_TYPE=Release.
136 if(NOT DEFINED CMAKE_C_FLAGS_RELEASE)
137 set(OVERRIDE_O3_IN_C_FLAGS_RELEASE ON)
140 # Among other things, this gives us variables xz_VERSION and xz_VERSION_MAJOR.
141 project(xz VERSION "${PACKAGE_VERSION}" LANGUAGES C)
143 if(OVERRIDE_O3_IN_C_FLAGS_RELEASE)
144 # Looking at CMake's source, there aren't any _FLAGS_RELEASE_INIT
145 # entries where "-O3" would appear as part of some other option,
146 # thus a simple search and replace should be fine.
147 string(REPLACE -O3 -O2 CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
149 # Update the cache value while keeping its docstring unchanged.
150 set_property(CACHE CMAKE_C_FLAGS_RELEASE
151 PROPERTY VALUE "${CMAKE_C_FLAGS_RELEASE}")
154 # We need a compiler that supports enough C99 or newer (variable-length arrays
155 # aren't needed, those are optional in C11/C17). C11 is preferred since C11
156 # features may be optionally used if they are available.
158 # Setting CMAKE_C_STANDARD here makes it the default for all targets.
159 # It doesn't affect the INTERFACE so liblzma::liblzma won't end up with
160 # INTERFACE_COMPILE_FEATURES "c_std_99" or such (the API headers are C89
161 # and C++ compatible).
163 # Avoid set(CMAKE_C_STANDARD_REQUIRED ON) because it's fine to decay
164 # to C99 if C11 isn't supported.
165 set(CMAKE_C_STANDARD 11)
167 # Support 32-bit x86 assembly files.
169 option(ENABLE_X86_ASM "Enable 32-bit x86 assembly code" OFF)
175 # On Apple OSes, don't build executables as bundles:
176 set(CMAKE_MACOSX_BUNDLE OFF)
178 # Set CMAKE_INSTALL_LIBDIR and friends. This needs to be done before
179 # the LOCALEDIR_DEFINITION workaround below.
180 include(GNUInstallDirs)
182 # windres from GNU binutils can be tricky with command line arguments
183 # that contain spaces or other funny characters. Unfortunately we need
184 # a space in PACKAGE_NAME. Using \x20 to encode the US-ASCII space seems
185 # to work in both cmd.exe and /bin/sh.
187 # However, even \x20 isn't enough in all situations, resulting in
188 # "syntax error" from windres. Using --use-temp-file prevents windres
189 # from using popen() and this seems to fix the problem.
191 # llvm-windres from Clang/LLVM 16.0.6 and older: The \x20 results
192 # in "XZx20Utils" in the compiled binary. The option --use-temp-file
193 # makes no difference.
195 # llvm-windres 17.0.0 and later: It emulates GNU windres more accurately, so
196 # the workarounds used with GNU windres must be used with llvm-windres too.
198 # CMake 3.27 doesn't have CMAKE_RC_COMPILER_ID so we rely on
199 # CMAKE_C_COMPILER_ID.
200 if((MINGW OR CYGWIN OR MSYS) AND (
201 NOT CMAKE_C_COMPILER_ID STREQUAL "Clang" OR
202 CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "17"))
203 # Use workarounds with GNU windres and llvm-windres >= 17.0.0. The \x20
204 # in PACKAGE_NAME_DEFINITION works with gcc and clang too so we don't need
205 # to worry how to pass different flags to windres and the C compiler.
206 # Keep the original PACKAGE_NAME intact for generation of liblzma.pc.
207 string(APPEND CMAKE_RC_FLAGS " --use-temp-file")
208 string(REPLACE " " "\\x20" PACKAGE_NAME_DEFINITION "${PACKAGE_NAME}")
210 # Use octal because "Program Files" would become \x20F.
211 string(REPLACE " " "\\040" LOCALEDIR_DEFINITION
212 "${CMAKE_INSTALL_FULL_LOCALEDIR}")
214 # Elsewhere a space is safe. This also keeps things compatible with
215 # EBCDIC in case CMake-based build is ever done on such a system.
216 set(PACKAGE_NAME_DEFINITION "${PACKAGE_NAME}")
217 set(LOCALEDIR_DEFINITION "${CMAKE_INSTALL_FULL_LOCALEDIR}")
220 # Definitions common to all targets:
221 add_compile_definitions(
223 PACKAGE_NAME="${PACKAGE_NAME_DEFINITION}"
224 PACKAGE_BUGREPORT="${PACKAGE_BUGREPORT}"
225 PACKAGE_URL="${PACKAGE_URL}"
227 # Standard headers and types are available:
233 # Always enable CRC32 since liblzma should never build without it.
236 # Disable assert() checks when no build type has been specified. Non-empty
237 # build types like "Release" and "Debug" handle this by default.
242 ######################
243 # System definitions #
244 ######################
246 # _GNU_SOURCE and such definitions. This specific macro is special since
247 # it also adds the definitions to CMAKE_REQUIRED_DEFINITIONS.
248 tuklib_use_system_extensions(ALL)
250 # Check for large file support. It's required on some 32-bit platforms and
251 # even on 64-bit MinGW-w64 to get 64-bit off_t. This can be forced off on
252 # the CMake command line if needed: -DLARGE_FILE_SUPPORT=OFF
253 tuklib_large_file_support(ALL)
255 # This is needed by liblzma and xz.
258 # This is used for liblzma.pc generation to add -lrt if needed.
260 # The variable name LIBS comes from Autoconf where AC_SEARCH_LIBS adds the
261 # libraries it finds into the shell variable LIBS. These libraries need to
262 # be put into liblzma.pc too, thus liblzma.pc.in has @LIBS@ because that
263 # matches the Autoconf's variable. When CMake support was added, using
264 # the same variable with configure_file() was the simplest method.
267 # Check for clock_gettime(). Do this before checking for threading so
268 # that we know there if CLOCK_MONOTONIC is available.
269 check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME)
271 if(NOT HAVE_CLOCK_GETTIME)
272 # With glibc <= 2.17 or Solaris 10 this needs librt.
273 # Add librt for the next check for HAVE_CLOCK_GETTIME. If it is
274 # found after including the library, we know that librt is required.
275 list(INSERT CMAKE_REQUIRED_LIBRARIES 0 rt)
276 check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME_LIBRT)
278 # If it was found now, add librt to all targets and keep it in
279 # CMAKE_REQUIRED_LIBRARIES for further tests too.
280 if(HAVE_CLOCK_GETTIME_LIBRT)
282 set(LIBS "-lrt ${LIBS}") # For liblzma.pc
284 list(REMOVE_AT CMAKE_REQUIRED_LIBRARIES 0)
288 if(HAVE_CLOCK_GETTIME OR HAVE_CLOCK_GETTIME_LIBRT)
289 add_compile_definitions(HAVE_CLOCK_GETTIME)
291 # Check if CLOCK_MONOTONIC is available for clock_gettime().
292 check_symbol_exists(CLOCK_MONOTONIC time.h HAVE_CLOCK_MONOTONIC)
293 tuklib_add_definition_if(ALL HAVE_CLOCK_MONOTONIC)
296 # Translation support requires CMake 3.20 because it added the Intl::Intl
297 # target so we don't need to play with the individual variables.
299 # The definition ENABLE_NLS is added only to those targets that use it, thus
300 # it's not done here. (xz has translations, xzdec doesn't.)
301 if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.20")
303 find_package(Gettext)
306 option(ENABLE_NLS "Native Language Support (translated messages)" ON)
308 # If translation support is enabled but neither gettext tools or
309 # pre-generated .gmo files exist, translation support cannot be
312 # The detection of pre-generated .gmo files is done by only
313 # checking for the existence of a single .gmo file; Ukrainian
314 # is one of many translations that gets regular updates.
315 if(ENABLE_NLS AND NOT GETTEXT_FOUND AND
316 NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/po/uk.gmo")
317 # This only sets the variable, not the cache variable!
320 # This message is shown only when new enough CMake is used and
321 # library support for translations was found. The assumptions is
322 # that in this situation the user might have interest in the
323 # translations. This also keeps this code simpler.
324 message(WARNING "Native language support (NLS) has been disabled. "
325 "NLS support requires either gettext tools or "
326 "pre-generated .gmo files. The latter are only "
327 "available in distribution tarballs. "
328 "To avoid this warning, NLS can be explicitly "
329 "disabled by passing -DENABLE_NLS=OFF to cmake.")
332 # Warn if NLS is enabled but translated man pages are missing.
333 if(UNIX AND ENABLE_NLS AND
334 NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/po4a/man")
335 message(WARNING "Native language support (NLS) has been enabled "
336 "but pre-generated translated man pages "
337 "were not found and thus they won't be installed. "
338 "Run 'po4a/update-po' to generate them.")
341 # The *installed* name of the translation files is "xz.mo".
342 set(TRANSLATION_DOMAIN "xz")
346 # Options for new enough GCC or Clang on any arch or operating system:
347 if(CMAKE_C_COMPILER_ID MATCHES GNU|Clang)
348 # configure.ac has a long list but it won't be copied here:
349 add_compile_options(-Wall -Wextra)
353 #############################################################################
355 #############################################################################
357 option(BUILD_SHARED_LIBS "Build liblzma as a shared library instead of static")
360 # Symbol versioning only affects ELF shared libraries. The option is
361 # ignored for static libraries.
363 # Determine the default value so that it's always set with
364 # shared libraries in mind which helps if the build dir is reconfigured
365 # from static to shared libs without resetting the cache variables.
366 set(SYMBOL_VERSIONING_DEFAULT OFF)
368 if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND
369 (CMAKE_SYSTEM_PROCESSOR MATCHES "[Mm]icro[Bb]laze" OR
370 CMAKE_C_COMPILER_ID STREQUAL "NVHPC"))
371 # As a special case, GNU/Linux on MicroBlaze gets the generic
372 # symbol versioning because GCC 12 doesn't support the __symver__
373 # attribute on MicroBlaze. On Linux, CMAKE_SYSTEM_PROCESSOR comes
374 # from "uname -m" for native builds (should be "microblaze") or from
375 # the CMake toolchain file (not perfectly standardized but it very
376 # likely has "microblaze" in lower case or mixed case somewhere in
379 # NVIDIA HPC Compiler doesn't support symbol versioning but
380 # it uses the linked from the system so the linker script
381 # can still be used to get the generic symbol versioning.
382 set(SYMBOL_VERSIONING_DEFAULT "generic")
384 elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
385 # GNU/Linux-specific symbol versioning for shared liblzma.
386 # This includes a few extra compatibility symbols for RHEL/CentOS 7
387 # which are pointless on non-glibc non-Linux systems.
389 # Avoid symvers on Linux with non-glibc like musl and uClibc.
390 # In Autoconf it's enough to check that $host_os equals linux-gnu
391 # instead of, for example, linux-musl. CMake doesn't provide such
394 # This check is here for now since it's not strictly required
396 check_c_source_compiles(
397 "#include <features.h>
398 #if defined(__GLIBC__) && !defined(__UCLIBC__)
399 int main(void) { return 0; }
406 if(IS_LINUX_WITH_GLIBC)
407 set(SYMBOL_VERSIONING_DEFAULT "linux")
410 elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
411 set(SYMBOL_VERSIONING_DEFAULT "generic")
414 set(SYMBOL_VERSIONING "${SYMBOL_VERSIONING_DEFAULT}" CACHE STRING
415 "Enable ELF shared library symbol versioning (OFF, generic, linux)")
417 # Show a dropdown menu in CMake GUI:
418 set_property(CACHE SYMBOL_VERSIONING PROPERTY STRINGS "OFF;generic;linux")
421 set(LIBLZMA_API_HEADERS
422 src/liblzma/api/lzma.h
423 src/liblzma/api/lzma/base.h
424 src/liblzma/api/lzma/bcj.h
425 src/liblzma/api/lzma/block.h
426 src/liblzma/api/lzma/check.h
427 src/liblzma/api/lzma/container.h
428 src/liblzma/api/lzma/delta.h
429 src/liblzma/api/lzma/filter.h
430 src/liblzma/api/lzma/hardware.h
431 src/liblzma/api/lzma/index.h
432 src/liblzma/api/lzma/index_hash.h
433 src/liblzma/api/lzma/lzma12.h
434 src/liblzma/api/lzma/stream_flags.h
435 src/liblzma/api/lzma/version.h
436 src/liblzma/api/lzma/vli.h
440 src/common/mythread.h
442 src/common/tuklib_common.h
443 src/common/tuklib_config.h
444 src/common/tuklib_integer.h
445 src/common/tuklib_physmem.c
446 src/common/tuklib_physmem.h
447 ${LIBLZMA_API_HEADERS}
448 src/liblzma/check/check.c
449 src/liblzma/check/check.h
450 src/liblzma/check/crc_common.h
451 src/liblzma/check/crc_x86_clmul.h
452 src/liblzma/check/crc32_arm64.h
453 src/liblzma/common/block_util.c
454 src/liblzma/common/common.c
455 src/liblzma/common/common.h
456 src/liblzma/common/easy_preset.c
457 src/liblzma/common/easy_preset.h
458 src/liblzma/common/filter_common.c
459 src/liblzma/common/filter_common.h
460 src/liblzma/common/hardware_physmem.c
461 src/liblzma/common/index.c
462 src/liblzma/common/index.h
463 src/liblzma/common/memcmplen.h
464 src/liblzma/common/stream_flags_common.c
465 src/liblzma/common/stream_flags_common.h
466 src/liblzma/common/string_conversion.c
467 src/liblzma/common/vli_size.c
470 target_include_directories(liblzma PRIVATE
475 src/liblzma/rangecoder
483 ######################
484 # Size optimizations #
485 ######################
487 option(ENABLE_SMALL "Reduce code size at expense of speed. \
488 This may be useful together with CMAKE_BUILD_TYPE=MinSizeRel.")
491 add_compile_definitions(HAVE_SMALL)
499 set(ADDITIONAL_SUPPORTED_CHECKS crc64 sha256)
501 set(ADDITIONAL_CHECK_TYPES "${ADDITIONAL_SUPPORTED_CHECKS}" CACHE STRING
502 "Additional check types to support (crc32 is always built)")
504 foreach(CHECK IN LISTS ADDITIONAL_CHECK_TYPES)
505 if(NOT CHECK IN_LIST ADDITIONAL_SUPPORTED_CHECKS)
506 message(FATAL_ERROR "'${CHECK}' is not a supported check type")
511 target_sources(liblzma PRIVATE src/liblzma/check/crc32_small.c)
513 target_sources(liblzma PRIVATE
514 src/liblzma/check/crc32_table.c
515 src/liblzma/check/crc32_table_be.h
516 src/liblzma/check/crc32_table_le.h
520 target_sources(liblzma PRIVATE src/liblzma/check/crc32_x86.S)
522 target_sources(liblzma PRIVATE src/liblzma/check/crc32_fast.c)
526 if("crc64" IN_LIST ADDITIONAL_CHECK_TYPES)
527 add_compile_definitions("HAVE_CHECK_CRC64")
530 target_sources(liblzma PRIVATE src/liblzma/check/crc64_small.c)
532 target_sources(liblzma PRIVATE
533 src/liblzma/check/crc64_table.c
534 src/liblzma/check/crc64_table_be.h
535 src/liblzma/check/crc64_table_le.h
539 target_sources(liblzma PRIVATE src/liblzma/check/crc64_x86.S)
541 target_sources(liblzma PRIVATE src/liblzma/check/crc64_fast.c)
546 if("sha256" IN_LIST ADDITIONAL_CHECK_TYPES)
547 add_compile_definitions("HAVE_CHECK_SHA256")
548 target_sources(liblzma PRIVATE src/liblzma/check/sha256.c)
556 set(SUPPORTED_MATCH_FINDERS hc3 hc4 bt2 bt3 bt4)
558 set(MATCH_FINDERS "${SUPPORTED_MATCH_FINDERS}" CACHE STRING
559 "Match finders to support (at least one is required for LZMA1 or LZMA2)")
561 foreach(MF IN LISTS MATCH_FINDERS)
562 if(MF IN_LIST SUPPORTED_MATCH_FINDERS)
563 string(TOUPPER "${MF}" MF_UPPER)
564 add_compile_definitions("HAVE_MF_${MF_UPPER}")
566 message(FATAL_ERROR "'${MF}' is not a supported match finder")
575 # Supported threading methods:
576 # ON - autodetect the best threading method. The autodetection will
577 # prefer Windows threading (win95 or vista) over posix if both are
578 # available. vista threads will be used over win95 unless it is a
580 # OFF - Disable threading.
581 # posix - Use posix threading (pthreads), or throw an error if not available.
582 # win95 - Use Windows win95 threading, or throw an error if not available.
583 # vista - Use Windows vista threading, or throw an error if not available.
584 set(SUPPORTED_THREADING_METHODS ON OFF posix win95 vista)
586 set(ENABLE_THREADS ON CACHE STRING
587 "Threading method: Set to 'ON' to autodetect, 'OFF' to disable threading.")
589 # Create dropdown in CMake GUI since only 1 threading method is possible
590 # to select in a build.
591 set_property(CACHE ENABLE_THREADS
592 PROPERTY STRINGS "${SUPPORTED_THREADING_METHODS}")
594 # This is a flag variable set when win95 threads are used. We must ensure
595 # the combination of enable_small and win95 threads is not used without a
596 # compiler supporting attribute __constructor__.
597 set(USE_WIN95_THREADS OFF)
599 # This is a flag variable set when posix threads (pthreads) are used.
600 # It's needed when creating liblzma-config.cmake where dependency on
601 # Threads::Threads is only needed with pthreads.
602 set(USE_POSIX_THREADS OFF)
604 if(NOT ENABLE_THREADS IN_LIST SUPPORTED_THREADING_METHODS)
605 message(FATAL_ERROR "'${ENABLE_THREADS}' is not a supported "
610 # Also set THREADS_PREFER_PTHREAD_FLAG since the flag has no effect
611 # for Windows threading.
612 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
613 find_package(Threads REQUIRED)
615 # If both Windows and posix threading are available, prefer Windows.
616 # Note that on Cygwin CMAKE_USE_WIN32_THREADS_INIT is false.
617 if(CMAKE_USE_WIN32_THREADS_INIT AND NOT ENABLE_THREADS STREQUAL "posix")
618 if(ENABLE_THREADS STREQUAL "win95"
619 OR (ENABLE_THREADS STREQUAL "ON"
620 AND CMAKE_SIZEOF_VOID_P EQUAL 4))
621 # Use Windows 95 (and thus XP) compatible threads.
622 # This avoids use of features that were added in
623 # Windows Vista. This is used for 32-bit x86 builds for
624 # compatibility reasons since it makes no measurable difference
625 # in performance compared to Vista threads.
626 set(USE_WIN95_THREADS ON)
627 add_compile_definitions(MYTHREAD_WIN95)
629 add_compile_definitions(MYTHREAD_VISTA)
631 elseif(CMAKE_USE_PTHREADS_INIT)
632 if(ENABLE_THREADS STREQUAL "posix" OR ENABLE_THREADS STREQUAL "ON")
633 # The threading library only needs to be explicitly linked
634 # for posix threads, so this is needed for creating
635 # liblzma-config.cmake later.
636 set(USE_POSIX_THREADS ON)
638 target_link_libraries(liblzma PRIVATE Threads::Threads)
639 add_compile_definitions(MYTHREAD_POSIX)
641 # Make the thread libs available in later checks. In practice
642 # only pthread_condattr_setclock check should need this.
643 list(INSERT CMAKE_REQUIRED_LIBRARIES 0 "${CMAKE_THREAD_LIBS_INIT}")
645 # Check if pthread_condattr_setclock() exists to
646 # use CLOCK_MONOTONIC.
647 if(HAVE_CLOCK_MONOTONIC)
648 check_symbol_exists(pthread_condattr_setclock pthread.h
649 HAVE_PTHREAD_CONDATTR_SETCLOCK)
650 tuklib_add_definition_if(ALL HAVE_PTHREAD_CONDATTR_SETCLOCK)
654 "Windows threading method was requested but a compatible "
655 "library could not be found")
658 message(SEND_ERROR "No supported threading library found")
661 target_sources(liblzma PRIVATE
662 src/common/tuklib_cpucores.c
663 src/common/tuklib_cpucores.h
664 src/liblzma/common/hardware_cputhreads.c
665 src/liblzma/common/outqueue.c
666 src/liblzma/common/outqueue.h
686 # The SUPPORTED_FILTERS are shared between Encoders and Decoders
687 # since only lzip does not appear in both lists. lzip is a special
688 # case anyway, so it is handled separately in the Decoders section.
689 set(SUPPORTED_FILTERS
696 set(ENCODERS "${SUPPORTED_FILTERS}" CACHE STRING "Encoders to support")
698 # If LZMA2 is enabled, then LZMA1 must also be enabled.
699 if(NOT "lzma1" IN_LIST ENCODERS AND "lzma2" IN_LIST ENCODERS)
700 message(FATAL_ERROR "LZMA2 encoder requires that LZMA1 is also enabled")
703 # If LZMA1 is enabled, then at least one match finder must be enabled.
704 if(MATCH_FINDERS STREQUAL "" AND "lzma1" IN_LIST ENCODERS)
705 message(FATAL_ERROR "At least 1 match finder is required for an "
709 set(HAVE_DELTA_CODER OFF)
710 set(SIMPLE_ENCODERS OFF)
711 set(HAVE_ENCODERS OFF)
713 foreach(ENCODER IN LISTS ENCODERS)
714 if(ENCODER IN_LIST SUPPORTED_FILTERS)
715 set(HAVE_ENCODERS ON)
717 if(NOT SIMPLE_ENCODERS AND ENCODER IN_LIST SIMPLE_FILTERS)
718 set(SIMPLE_ENCODERS ON)
721 string(TOUPPER "${ENCODER}" ENCODER_UPPER)
722 add_compile_definitions("HAVE_ENCODER_${ENCODER_UPPER}")
724 message(FATAL_ERROR "'${ENCODER}' is not a supported encoder")
729 add_compile_definitions(HAVE_ENCODERS)
731 target_sources(liblzma PRIVATE
732 src/liblzma/common/alone_encoder.c
733 src/liblzma/common/block_buffer_encoder.c
734 src/liblzma/common/block_buffer_encoder.h
735 src/liblzma/common/block_encoder.c
736 src/liblzma/common/block_encoder.h
737 src/liblzma/common/block_header_encoder.c
738 src/liblzma/common/easy_buffer_encoder.c
739 src/liblzma/common/easy_encoder.c
740 src/liblzma/common/easy_encoder_memusage.c
741 src/liblzma/common/filter_buffer_encoder.c
742 src/liblzma/common/filter_encoder.c
743 src/liblzma/common/filter_encoder.h
744 src/liblzma/common/filter_flags_encoder.c
745 src/liblzma/common/index_encoder.c
746 src/liblzma/common/index_encoder.h
747 src/liblzma/common/stream_buffer_encoder.c
748 src/liblzma/common/stream_encoder.c
749 src/liblzma/common/stream_flags_encoder.c
750 src/liblzma/common/vli_encoder.c
754 target_sources(liblzma PRIVATE
755 src/liblzma/common/stream_encoder_mt.c
760 target_sources(liblzma PRIVATE
761 src/liblzma/simple/simple_encoder.c
762 src/liblzma/simple/simple_encoder.h
766 if("lzma1" IN_LIST ENCODERS)
767 target_sources(liblzma PRIVATE
768 src/liblzma/lzma/lzma_encoder.c
769 src/liblzma/lzma/lzma_encoder.h
770 src/liblzma/lzma/lzma_encoder_optimum_fast.c
771 src/liblzma/lzma/lzma_encoder_optimum_normal.c
772 src/liblzma/lzma/lzma_encoder_private.h
773 src/liblzma/lzma/fastpos.h
774 src/liblzma/lz/lz_encoder.c
775 src/liblzma/lz/lz_encoder.h
776 src/liblzma/lz/lz_encoder_hash.h
777 src/liblzma/lz/lz_encoder_hash_table.h
778 src/liblzma/lz/lz_encoder_mf.c
779 src/liblzma/rangecoder/price.h
780 src/liblzma/rangecoder/price_table.c
781 src/liblzma/rangecoder/range_encoder.h
785 target_sources(liblzma PRIVATE src/liblzma/lzma/fastpos_table.c)
789 if("lzma2" IN_LIST ENCODERS)
790 target_sources(liblzma PRIVATE
791 src/liblzma/lzma/lzma2_encoder.c
792 src/liblzma/lzma/lzma2_encoder.h
796 if("delta" IN_LIST ENCODERS)
797 set(HAVE_DELTA_CODER ON)
798 target_sources(liblzma PRIVATE
799 src/liblzma/delta/delta_encoder.c
800 src/liblzma/delta/delta_encoder.h
810 set(DECODERS "${SUPPORTED_FILTERS}" CACHE STRING "Decoders to support")
812 set(SIMPLE_DECODERS OFF)
813 set(HAVE_DECODERS OFF)
815 foreach(DECODER IN LISTS DECODERS)
816 if(DECODER IN_LIST SUPPORTED_FILTERS)
817 set(HAVE_DECODERS ON)
819 if(NOT SIMPLE_DECODERS AND DECODER IN_LIST SIMPLE_FILTERS)
820 set(SIMPLE_DECODERS ON)
823 string(TOUPPER "${DECODER}" DECODER_UPPER)
824 add_compile_definitions("HAVE_DECODER_${DECODER_UPPER}")
826 message(FATAL_ERROR "'${DECODER}' is not a supported decoder")
831 add_compile_definitions(HAVE_DECODERS)
833 target_sources(liblzma PRIVATE
834 src/liblzma/common/alone_decoder.c
835 src/liblzma/common/alone_decoder.h
836 src/liblzma/common/auto_decoder.c
837 src/liblzma/common/block_buffer_decoder.c
838 src/liblzma/common/block_decoder.c
839 src/liblzma/common/block_decoder.h
840 src/liblzma/common/block_header_decoder.c
841 src/liblzma/common/easy_decoder_memusage.c
842 src/liblzma/common/file_info.c
843 src/liblzma/common/filter_buffer_decoder.c
844 src/liblzma/common/filter_decoder.c
845 src/liblzma/common/filter_decoder.h
846 src/liblzma/common/filter_flags_decoder.c
847 src/liblzma/common/index_decoder.c
848 src/liblzma/common/index_decoder.h
849 src/liblzma/common/index_hash.c
850 src/liblzma/common/stream_buffer_decoder.c
851 src/liblzma/common/stream_decoder.c
852 src/liblzma/common/stream_flags_decoder.c
853 src/liblzma/common/stream_decoder.h
854 src/liblzma/common/vli_decoder.c
858 target_sources(liblzma PRIVATE
859 src/liblzma/common/stream_decoder_mt.c
864 target_sources(liblzma PRIVATE
865 src/liblzma/simple/simple_decoder.c
866 src/liblzma/simple/simple_decoder.h
870 if("lzma1" IN_LIST DECODERS)
871 target_sources(liblzma PRIVATE
872 src/liblzma/lzma/lzma_decoder.c
873 src/liblzma/lzma/lzma_decoder.h
874 src/liblzma/rangecoder/range_decoder.h
875 src/liblzma/lz/lz_decoder.c
876 src/liblzma/lz/lz_decoder.h
880 if("lzma2" IN_LIST DECODERS)
881 target_sources(liblzma PRIVATE
882 src/liblzma/lzma/lzma2_decoder.c
883 src/liblzma/lzma/lzma2_decoder.h
887 if("delta" IN_LIST DECODERS)
888 set(HAVE_DELTA_CODER ON)
889 target_sources(liblzma PRIVATE
890 src/liblzma/delta/delta_decoder.c
891 src/liblzma/delta/delta_decoder.h
896 # Some sources must appear if the filter is configured as either
897 # an encoder or decoder.
898 if("lzma1" IN_LIST ENCODERS OR "lzma1" IN_LIST DECODERS)
899 target_sources(liblzma PRIVATE
900 src/liblzma/rangecoder/range_common.h
901 src/liblzma/lzma/lzma_encoder_presets.c
902 src/liblzma/lzma/lzma_common.h
907 target_sources(liblzma PRIVATE
908 src/liblzma/delta/delta_common.c
909 src/liblzma/delta/delta_common.h
910 src/liblzma/delta/delta_private.h
914 if(SIMPLE_ENCODERS OR SIMPLE_DECODERS)
915 target_sources(liblzma PRIVATE
916 src/liblzma/simple/simple_coder.c
917 src/liblzma/simple/simple_coder.h
918 src/liblzma/simple/simple_private.h
922 foreach(SIMPLE_CODER IN LISTS SIMPLE_FILTERS)
923 if(SIMPLE_CODER IN_LIST ENCODERS OR SIMPLE_CODER IN_LIST DECODERS)
924 target_sources(liblzma PRIVATE "src/liblzma/simple/${SIMPLE_CODER}.c")
933 option(MICROLZMA_ENCODER
934 "MicroLZMA encoder (needed by specific applications only)" ON)
936 option(MICROLZMA_DECODER
937 "MicroLZMA decoder (needed by specific applications only)" ON)
939 if(MICROLZMA_ENCODER)
940 if(NOT "lzma1" IN_LIST ENCODERS)
941 message(FATAL_ERROR "The LZMA1 encoder is required to support the "
945 target_sources(liblzma PRIVATE src/liblzma/common/microlzma_encoder.c)
948 if(MICROLZMA_DECODER)
949 if(NOT "lzma1" IN_LIST DECODERS)
950 message(FATAL_ERROR "The LZMA1 decoder is required to support the "
954 target_sources(liblzma PRIVATE src/liblzma/common/microlzma_decoder.c)
958 #############################
959 # lzip (.lz) format support #
960 #############################
962 option(LZIP_DECODER "Support lzip decoder" ON)
965 # If lzip decoder support is requested, make sure LZMA1 decoder is enabled.
966 if(NOT "lzma1" IN_LIST DECODERS)
967 message(FATAL_ERROR "The LZMA1 decoder is required to support the "
971 add_compile_definitions(HAVE_LZIP_DECODER)
973 target_sources(liblzma PRIVATE
974 src/liblzma/common/lzip_decoder.c
975 src/liblzma/common/lzip_decoder.h
984 # ON Use sandboxing if a supported method is available in the OS.
985 # OFF Disable sandboxing.
986 # capsicum Require Capsicum (FreeBSD >= 10.2) and fail if not found.
987 # pledge Require pledge(2) (OpenBSD >= 5.9) and fail if not found.
988 # landlock Require Landlock (Linux >= 5.13) and fail if not found.
989 set(SUPPORTED_SANDBOX_METHODS ON OFF capsicum pledge landlock)
991 set(ENABLE_SANDBOX ON CACHE STRING
992 "Sandboxing method to use in 'xz', 'xzdec', and 'lzmadec'")
994 set_property(CACHE ENABLE_SANDBOX
995 PROPERTY STRINGS "${SUPPORTED_SANDBOX_METHODS}")
997 if(NOT ENABLE_SANDBOX IN_LIST SUPPORTED_SANDBOX_METHODS)
998 message(FATAL_ERROR "'${ENABLE_SANDBOX}' is not a supported "
1002 # When autodetecting, the search order is fixed and we must not find
1003 # more than one method.
1004 if(ENABLE_SANDBOX STREQUAL "OFF")
1005 set(SANDBOX_FOUND ON)
1007 set(SANDBOX_FOUND OFF)
1010 # Since xz and xzdec can both use sandboxing, the compile definition needed
1011 # to use the sandbox must be added to both targets.
1012 set(SANDBOX_COMPILE_DEFINITION OFF)
1014 # Sandboxing: Capsicum
1015 if(NOT SANDBOX_FOUND AND ENABLE_SANDBOX MATCHES "^ON$|^capsicum$")
1016 check_symbol_exists(cap_rights_limit sys/capsicum.h
1017 HAVE_CAP_RIGHTS_LIMIT)
1018 if(HAVE_CAP_RIGHTS_LIMIT)
1019 set(SANDBOX_COMPILE_DEFINITION "HAVE_CAP_RIGHTS_LIMIT")
1020 set(SANDBOX_FOUND ON)
1024 # Sandboxing: pledge(2)
1025 if(NOT SANDBOX_FOUND AND ENABLE_SANDBOX MATCHES "^ON$|^pledge$")
1026 check_symbol_exists(pledge unistd.h HAVE_PLEDGE)
1028 set(SANDBOX_COMPILE_DEFINITION "HAVE_PLEDGE")
1029 set(SANDBOX_FOUND ON)
1033 # Sandboxing: Landlock
1034 if(NOT SANDBOX_FOUND AND ENABLE_SANDBOX MATCHES "^ON$|^landlock$")
1035 # A compile check is done here because some systems have
1036 # linux/landlock.h, but do not have the syscalls defined
1037 # in order to actually use Linux Landlock.
1038 check_c_source_compiles("
1039 #include <linux/landlock.h>
1040 #include <sys/syscall.h>
1041 #include <sys/prctl.h>
1045 (void)prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1046 (void)SYS_landlock_create_ruleset;
1047 (void)SYS_landlock_restrict_self;
1048 (void)LANDLOCK_CREATE_RULESET_VERSION;
1052 HAVE_LINUX_LANDLOCK)
1054 if(HAVE_LINUX_LANDLOCK)
1055 set(SANDBOX_COMPILE_DEFINITION "HAVE_LINUX_LANDLOCK")
1056 set(SANDBOX_FOUND ON)
1058 # Of our three sandbox methods, only Landlock is incompatible
1059 # with -fsanitize. FreeBSD 13.2 with Capsicum was tested with
1060 # -fsanitize=address,undefined and had no issues. OpenBSD (as
1061 # of version 7.4) has minimal support for process instrumentation.
1062 # OpenBSD does not distribute the additional libraries needed
1063 # (libasan, libubsan, etc.) with GCC or Clang needed for runtime
1064 # sanitization support and instead only support
1065 # -fsanitize-minimal-runtime for minimal undefined behavior
1066 # sanitization. This minimal support is compatible with our use
1067 # of the Pledge sandbox. So only Landlock will result in a
1068 # build that cannot compress or decompress a single file to
1070 if(CMAKE_C_FLAGS MATCHES "-fsanitize=")
1072 "CMAKE_C_FLAGS or the environment variable CFLAGS "
1073 "contains '-fsanitize=' which is incompatible "
1074 "with Landlock sandboxing. Use -DENABLE_SANDBOX=OFF "
1075 "as an argument to 'cmake' when using '-fsanitize'.")
1080 if(NOT SANDBOX_FOUND AND NOT ENABLE_SANDBOX MATCHES "^ON$|^OFF$")
1081 message(SEND_ERROR "ENABLE_SANDBOX=${ENABLE_SANDBOX} was used but "
1082 "support for the sandboxing method wasn't found.")
1087 # Put the tuklib functions under the lzma_ namespace.
1088 target_compile_definitions(liblzma PRIVATE TUKLIB_SYMBOL_PREFIX=lzma_)
1089 tuklib_cpucores(liblzma)
1090 tuklib_physmem(liblzma)
1092 # While liblzma can be built without tuklib_cpucores or tuklib_physmem
1093 # modules, the liblzma API functions lzma_cputhreads() and lzma_physmem()
1094 # will then be useless (which isn't too bad but still unfortunate). Since
1095 # I expect the CMake-based builds to be only used on systems that are
1096 # supported by these tuklib modules, problems with these tuklib modules
1097 # are considered a hard error for now. This hopefully helps to catch bugs
1098 # in the CMake versions of the tuklib checks.
1099 if(NOT TUKLIB_CPUCORES_FOUND OR NOT TUKLIB_PHYSMEM_FOUND)
1100 # Use SEND_ERROR instead of FATAL_ERROR. If someone reports a bug,
1101 # seeing the results of the remaining checks can be useful too.
1103 "tuklib_cpucores() or tuklib_physmem() failed. "
1104 "Unless you really are building for a system where these "
1105 "modules are not supported (unlikely), this is a bug in the "
1106 "included cmake/tuklib_*.cmake files that should be fixed. "
1107 "To build anyway, edit this CMakeLists.txt to ignore this error.")
1110 # Check for __attribute__((__constructor__)) support.
1111 # This needs -Werror because some compilers just warn
1112 # about this being unsupported.
1113 cmake_push_check_state()
1114 set(CMAKE_REQUIRED_FLAGS "-Werror")
1115 check_c_source_compiles("
1116 __attribute__((__constructor__))
1117 static void my_constructor_func(void) { return; }
1118 int main(void) { return 0; }
1120 HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
1121 cmake_pop_check_state()
1122 tuklib_add_definition_if(liblzma HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
1124 # The Win95 threading lacks a thread-safe one-time initialization function.
1125 # The one-time initialization is needed for crc32_small.c and crc64_small.c
1126 # create the CRC tables. So if small mode is enabled, the threading mode is
1127 # win95, and the compiler does not support attribute constructor, then we
1128 # would end up with a multithreaded build that is thread-unsafe. As a
1129 # result this configuration is not allowed.
1130 if(USE_WIN95_THREADS AND ENABLE_SMALL AND NOT HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
1131 message(SEND_ERROR "Threading method win95 and ENABLE_SMALL "
1132 "cannot be used at the same time with a compiler "
1133 "that doesn't support "
1134 "__attribute__((__constructor__))")
1139 check_include_file(cpuid.h HAVE_CPUID_H)
1140 tuklib_add_definition_if(liblzma HAVE_CPUID_H)
1143 check_include_file(immintrin.h HAVE_IMMINTRIN_H)
1144 if(HAVE_IMMINTRIN_H)
1145 target_compile_definitions(liblzma PRIVATE HAVE_IMMINTRIN_H)
1148 check_c_source_compiles("
1149 #include <immintrin.h>
1153 _mm_movemask_epi8(x);
1157 HAVE__MM_MOVEMASK_EPI8)
1158 tuklib_add_definition_if(liblzma HAVE__MM_MOVEMASK_EPI8)
1161 option(ALLOW_CLMUL_CRC "Allow carryless multiplication for CRC \
1162 calculation if supported by the system" ON)
1165 check_c_source_compiles("
1166 #include <immintrin.h>
1167 #if defined(__e2k__) && __iset__ < 6
1170 #if (defined(__GNUC__) || defined(__clang__)) \
1171 && !defined(__EDG__)
1172 __attribute__((__target__(\"ssse3,sse4.1,pclmul\")))
1176 __m128i a = _mm_set_epi64x(1, 2);
1177 a = _mm_clmulepi64_si128(a, a, 0);
1182 tuklib_add_definition_if(liblzma HAVE_USABLE_CLMUL)
1186 # ARM64 C Language Extensions define CRC32 functions in arm_acle.h.
1187 # These are supported by at least GCC and Clang which both need
1188 # __attribute__((__target__("+crc"))), unless the needed compiler flags
1189 # are used to support the CRC instruction.
1190 option(ALLOW_ARM64_CRC32 "Allow ARM64 CRC32 instruction if supported by \
1193 if(ALLOW_ARM64_CRC32)
1194 check_c_source_compiles("
1198 #include <arm_acle.h>
1201 #if (defined(__GNUC__) || defined(__clang__)) && !defined(__EDG__)
1202 __attribute__((__target__(\"+crc\")))
1206 return __crc32d(1, 2) != 0;
1211 if(HAVE_ARM64_CRC32)
1212 target_compile_definitions(liblzma PRIVATE HAVE_ARM64_CRC32)
1214 # Check for ARM64 CRC32 instruction runtime detection.
1215 # getauxval() is supported on Linux.
1216 check_symbol_exists(getauxval sys/auxv.h HAVE_GETAUXVAL)
1217 tuklib_add_definition_if(liblzma HAVE_GETAUXVAL)
1219 # elf_aux_info() is supported on FreeBSD and OpenBSD >= 7.6.
1220 check_symbol_exists(elf_aux_info sys/auxv.h HAVE_ELF_AUX_INFO)
1221 tuklib_add_definition_if(liblzma HAVE_ELF_AUX_INFO)
1223 # sysctlbyname("hw.optional.armv8_crc32", ...) is supported on Darwin
1224 # (macOS, iOS, etc.). Note that sysctlbyname() is supported on FreeBSD,
1225 # NetBSD, and possibly others too but the string is specific to
1226 # Apple OSes. The C code is responsible for checking
1227 # defined(__APPLE__) before using
1228 # sysctlbyname("hw.optional.armv8_crc32", ...).
1229 check_symbol_exists(sysctlbyname sys/sysctl.h HAVE_SYSCTLBYNAME)
1230 tuklib_add_definition_if(liblzma HAVE_SYSCTLBYNAME)
1235 # Symbol visibility support:
1237 # The C_VISIBILITY_PRESET property takes care of adding the compiler
1238 # option -fvisibility=hidden (or equivalent) if and only if it is supported.
1240 # HAVE_VISIBILITY should always be defined to 0 or 1. It tells liblzma
1241 # if __attribute__((__visibility__("default")))
1242 # and __attribute__((__visibility__("hidden"))) are supported.
1243 # Those are useful only when the compiler supports -fvisibility=hidden
1244 # or such option so HAVE_VISIBILITY should be 1 only when both option and
1245 # the attribute support are present. HAVE_VISIBILITY is ignored on Windows
1246 # and Cygwin by the liblzma C code; __declspec(dllexport) is used instead.
1248 # CMake's GenerateExportHeader module is too fancy since liblzma already
1249 # has the necessary macros. Instead, check CMake's internal variable
1250 # CMAKE_C_COMPILE_OPTIONS_VISIBILITY (it's the C-specific variant of
1251 # CMAKE_<LANG>_COMPILE_OPTIONS_VISIBILITY) which contains the compiler
1252 # command line option for visibility support. It's empty or unset when
1253 # visibility isn't supported. (It was added to CMake 2.8.12 in the commit
1254 # 0e9f4bc00c6b26f254e74063e4026ac33b786513 in 2013.) This way we don't
1255 # set HAVE_VISIBILITY to 1 when visibility isn't actually supported.
1256 if(BUILD_SHARED_LIBS AND CMAKE_C_COMPILE_OPTIONS_VISIBILITY)
1257 set_target_properties(liblzma PROPERTIES C_VISIBILITY_PRESET hidden)
1258 target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=1)
1260 target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=0)
1264 if(BUILD_SHARED_LIBS)
1265 # Add the Windows resource file for liblzma.dll.
1266 target_sources(liblzma PRIVATE src/liblzma/liblzma_w32res.rc)
1268 set_target_properties(liblzma PROPERTIES
1269 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1272 # Export the public API symbols with __declspec(dllexport).
1273 target_compile_definitions(liblzma PRIVATE DLL_EXPORT)
1276 # Create a DEF file. The linker puts the ordinal numbers there
1277 # too so the output from the linker isn't our final file.
1278 target_link_options(liblzma PRIVATE
1279 "-Wl,--output-def,liblzma.def.in")
1281 # Remove the ordinal numbers from the DEF file so that
1282 # no one will create an import library that links by ordinal
1283 # instead of by name. We don't maintain a DEF file so the
1284 # ordinal numbers aren't stable.
1285 add_custom_command(TARGET liblzma POST_BUILD
1286 COMMAND "${CMAKE_COMMAND}"
1287 -DINPUT_FILE=liblzma.def.in
1288 -DOUTPUT_FILE=liblzma.def
1290 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/remove-ordinals.cmake"
1291 BYPRODUCTS "liblzma.def"
1295 # Disable __declspec(dllimport) when linking against static liblzma.
1296 target_compile_definitions(liblzma INTERFACE LZMA_API_STATIC)
1298 elseif(BUILD_SHARED_LIBS AND SYMBOL_VERSIONING STREQUAL "linux")
1299 # Note that adding link options doesn't affect static builds
1300 # but HAVE_SYMBOL_VERSIONS_LINUX must not be used with static builds
1301 # because it would put symbol versions into the static library which
1302 # can cause problems. It's clearer if all symver related things are
1303 # omitted when not building a shared library.
1305 # NOTE: Set it explicitly to 1 to make it clear that versioning is
1306 # done unconditionally in the C files.
1307 target_compile_definitions(liblzma PRIVATE HAVE_SYMBOL_VERSIONS_LINUX=1)
1308 target_link_options(liblzma PRIVATE
1309 "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
1311 set_target_properties(liblzma PROPERTIES
1312 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
1314 elseif(BUILD_SHARED_LIBS AND SYMBOL_VERSIONING STREQUAL "generic")
1315 target_link_options(liblzma PRIVATE
1316 "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
1318 set_target_properties(liblzma PROPERTIES
1319 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
1323 set_target_properties(liblzma PROPERTIES
1324 # At least for now the package versioning matches the rules used for
1325 # shared library versioning (excluding development releases) so it is
1326 # fine to use the package version here.
1327 SOVERSION "${xz_VERSION_MAJOR}"
1328 VERSION "${xz_VERSION}"
1330 # The name liblzma a mess because in many places "lib" is just a prefix
1331 # and not part of the actual name. (Don't name a new library this way!)
1332 # Cygwin uses "cyg", MSYS2 uses "msys-", and some platforms use no prefix.
1333 # However, we want to avoid lzma.dll on Windows as that would conflict
1334 # with LZMA SDK. liblzma has been liblzma.dll on Windows since the
1335 # beginning so try to stick with it.
1337 # Up to XZ Utils 5.6.2 we set PREFIX and IMPORT_PREFIX properties to ""
1338 # while keeping the default "liblzma" OUTPUT_NAME that was derived from
1339 # the target name. But this broke naming on Cygwin and MSYS2.
1341 # Setting OUTPUT_NAME without the "lib" prefix means that CMake will add
1342 # the platform-specific prefix as needed. So on most systems CMake will
1343 # add "lib" but on Cygwin and MSYS2 the naming will be correct too.
1345 # On Windows, CMake uses the "lib" prefix with MinGW-w64 but not with
1346 # other toolchains. Those those need to be handled specially to get
1347 # the DLL file named liblzma.dll instead of lzma.dll.
1351 if(WIN32 AND NOT MINGW)
1352 # Up to XZ Utils 5.6.2 and building with MSVC, we produced liblzma.dll
1353 # and liblzma.lib. The downside of liblzma.lib is that it's not
1354 # compatible with pkgconf usage. liblzma.pc contains "-llzma" which
1355 # "pkgconf --msvc-syntax --libs liblzma" converts to "lzma.lib".
1356 # So as a compromise, we can keep the liblzma.dll name but the import
1357 # library and static liblzma need to be named lzma.lib so that pkgconf
1358 # can be used with MSVC. (MinGW-w64 finds both names with "-llzma".)
1359 set_target_properties(liblzma PROPERTIES RUNTIME_OUTPUT_NAME "liblzma")
1362 # Create liblzma-config-version.cmake.
1364 # FIXME: SameMajorVersion is correct for stable releases but it is wrong
1365 # for development releases where each release may have incompatible changes.
1366 include(CMakePackageConfigHelpers)
1367 write_basic_package_version_file(
1368 "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
1369 VERSION "${xz_VERSION}"
1370 COMPATIBILITY SameMajorVersion)
1372 # Create liblzma-config.cmake. We use this spelling instead of
1373 # liblzmaConfig.cmake to make find_package work in case insensitive
1374 # manner even with case sensitive file systems. This gives more consistent
1375 # behavior between operating systems. This optionally includes a dependency
1376 # on a threading library, so the contents are created in two separate parts.
1377 # The "second half" is always needed, so create it first.
1378 set(LZMA_CONFIG_CONTENTS
1379 "include(\"\${CMAKE_CURRENT_LIST_DIR}/liblzma-targets.cmake\")
1381 if(NOT TARGET LibLZMA::LibLZMA)
1382 # Be compatible with the spelling used by the FindLibLZMA module. This
1383 # doesn't use ALIAS because it would make CMake resolve LibLZMA::LibLZMA
1384 # to liblzma::liblzma instead of keeping the original spelling. Keeping
1385 # the original spelling is important for good FindLibLZMA compatibility.
1386 add_library(LibLZMA::LibLZMA INTERFACE IMPORTED)
1387 set_target_properties(LibLZMA::LibLZMA PROPERTIES
1388 INTERFACE_LINK_LIBRARIES liblzma::liblzma)
1392 if(USE_POSIX_THREADS)
1393 set(LZMA_CONFIG_CONTENTS
1394 "include(CMakeFindDependencyMacro)
1395 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
1396 find_dependency(Threads)
1398 ${LZMA_CONFIG_CONTENTS}
1402 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
1403 "${LZMA_CONFIG_CONTENTS}")
1406 # Create liblzma.pc. If using CMake >= 3.20 and CMAKE_INSTALL_<dir> paths
1407 # are relative to CMAKE_INSTALL_PREFIX, the .pc file will be relocatable
1408 # (that is, all paths will be relative to ${prefix}). Otherwise absolute
1409 # paths will be used.
1410 set(prefix "${CMAKE_INSTALL_PREFIX}")
1411 set(exec_prefix "\${prefix}")
1413 if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.20")
1414 cmake_path(APPEND libdir "\${exec_prefix}" "${CMAKE_INSTALL_LIBDIR}")
1415 cmake_path(APPEND includedir "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}")
1417 set(libdir "${CMAKE_INSTALL_FULL_LIBDIR}")
1418 set(includedir "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
1421 # Threads::Threads is linked in only when using POSIX threads.
1422 # Use an empty value if using Windows threads or if threading is disabled.
1424 if(USE_POSIX_THREADS)
1425 set(PTHREAD_CFLAGS "${CMAKE_THREAD_LIBS_INIT}")
1428 configure_file(src/liblzma/liblzma.pc.in liblzma.pc @ONLY)
1431 # Install the library binary. The INCLUDES specifies the include path that
1432 # is exported for other projects to use but it doesn't install any files.
1433 install(TARGETS liblzma EXPORT liblzmaTargets
1434 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1435 COMPONENT liblzma_Runtime
1436 LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
1437 COMPONENT liblzma_Runtime
1438 NAMELINK_COMPONENT liblzma_Development
1439 ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
1440 COMPONENT liblzma_Development
1441 INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
1443 # Install the liblzma API headers. These use a subdirectory so
1444 # this has to be done as a separate step.
1445 install(DIRECTORY src/liblzma/api/
1446 COMPONENT liblzma_Development
1447 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
1448 FILES_MATCHING PATTERN "*.h")
1450 # Install the CMake files that other packages can use to find liblzma.
1451 set(liblzma_INSTALL_CMAKEDIR
1452 "${CMAKE_INSTALL_LIBDIR}/cmake/liblzma"
1453 CACHE STRING "Path to liblzma's .cmake files")
1455 install(EXPORT liblzmaTargets
1457 FILE liblzma-targets.cmake
1458 DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
1459 COMPONENT liblzma_Development)
1461 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
1462 "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
1463 DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
1464 COMPONENT liblzma_Development)
1466 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblzma.pc"
1467 DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig"
1468 COMPONENT liblzma_Development)
1471 #############################################################################
1472 # Helper functions for installing files
1473 #############################################################################
1475 # For each non-empty element in the list LINK_NAMES, creates symbolic links
1476 # ${LINK_NAME}${LINK_SUFFIX} -> ${TARGET_NAME} in the directory ${DIR}.
1477 # The target file should exist because on Cygwin and MSYS2 symlink creation
1478 # can fail under certain conditions if the target doesn't exist.
1479 function(my_install_symlinks COMPONENT DIR TARGET_NAME LINK_SUFFIX LINK_NAMES)
1480 install(CODE "set(D \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${DIR}\")
1481 foreach(L ${LINK_NAMES})
1482 file(CREATE_LINK \"${TARGET_NAME}\"
1483 \"\${D}/\${L}${LINK_SUFFIX}\"
1486 COMPONENT "${COMPONENT}")
1489 # Installs a man page file of a given language ("" for the untranslated file)
1490 # and optionally its alternative names as symlinks. This is a helper function
1491 # for my_install_man() below.
1492 function(my_install_man_lang COMPONENT SRC_FILE MAN_LANG LINK_NAMES)
1493 # Get the man page section from the filename suffix.
1494 string(REGEX REPLACE "^.*\.([^/.]+)$" "\\1" MAN_SECTION "${SRC_FILE}")
1496 # A few man pages might be missing from translations.
1497 # Don't attempt to install them or create the related symlinks.
1498 if(NOT MAN_LANG STREQUAL "" AND NOT EXISTS "${SRC_FILE}")
1502 # Installing the file must be done before creating the symlinks
1503 # due to Cygwin and MSYS2.
1504 install(FILES "${SRC_FILE}"
1505 DESTINATION "${CMAKE_INSTALL_MANDIR}/${MAN_LANG}/man${MAN_SECTION}"
1506 COMPONENT "${COMPONENT}")
1508 # Get the basename of the file to be used as the symlink target.
1509 get_filename_component(BASENAME "${SRC_FILE}" NAME)
1511 # LINK_NAMES don't contain the man page filename suffix (like ".1")
1512 # so it needs to be told to my_install_symlinks.
1513 my_install_symlinks("${COMPONENT}"
1514 "${CMAKE_INSTALL_MANDIR}/${MAN_LANG}/man${MAN_SECTION}"
1515 "${BASENAME}" ".${MAN_SECTION}" "${LINK_NAMES}")
1518 # Installs a man page file and optionally its alternative names as symlinks.
1519 # Does the same for translations if ENABLE_NLS.
1520 function(my_install_man COMPONENT SRC_FILE LINK_NAMES)
1521 my_install_man_lang("${COMPONENT}" "${SRC_FILE}" "" "${LINK_NAMES}")
1524 # Find the translated versions of this man page.
1525 get_filename_component(BASENAME "${SRC_FILE}" NAME)
1526 file(GLOB MAN_FILES "po4a/man/*/${BASENAME}")
1528 foreach(F ${MAN_FILES})
1529 get_filename_component(MAN_LANG "${F}" DIRECTORY)
1530 get_filename_component(MAN_LANG "${MAN_LANG}" NAME)
1531 my_install_man_lang("${COMPONENT}" "${F}" "${MAN_LANG}"
1538 #############################################################################
1539 # libgnu (getopt_long)
1540 #############################################################################
1542 # This mirrors how the Autotools build system handles the getopt_long
1543 # replacement, calling the object library libgnu since the replacement
1544 # version comes from Gnulib.
1545 add_library(libgnu OBJECT)
1547 # CMake requires that even an object library must have at least once source
1548 # file. So we give it a header file that results in no output files.
1550 # NOTE: Using a file outside the lib directory makes it possible to
1551 # delete lib/*.h and lib/*.c and still keep the build working if
1552 # getopt_long replacement isn't needed. It's convenient if one wishes
1553 # to be certain that no GNU LGPL code gets included in the binaries.
1554 target_sources(libgnu PRIVATE src/common/sysdefs.h)
1556 # The Ninja Generator requires setting the linker language since it cannot
1557 # guess the programming language of just a header file. Setting this
1558 # property avoids needing an empty .c file or an non-empty unnecessary .c
1560 set_target_properties(libgnu PROPERTIES LINKER_LANGUAGE C)
1562 # Create /lib directory in the build directory and add it to the include path.
1563 file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib")
1564 target_include_directories(libgnu PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/lib")
1566 # Include /lib from the source directory. It does no harm even if none of
1567 # the Gnulib replacements are used.
1568 target_include_directories(libgnu PUBLIC lib)
1570 # The command line tools need getopt_long in order to parse arguments. If
1571 # the system does not have a getopt_long implementation we can use the one
1572 # from Gnulib instead.
1573 check_symbol_exists(getopt_long getopt.h HAVE_GETOPT_LONG)
1575 if(NOT HAVE_GETOPT_LONG)
1576 # Set the __GETOPT_PREFIX definition to "rpl_" (replacement) to avoid
1577 # name conflicts with libc symbols. The same prefix is set if using
1578 # the Autotools build (m4/getopt.m4).
1579 target_compile_definitions(libgnu PUBLIC "__GETOPT_PREFIX=rpl_")
1581 # Copy the getopt header to the build directory and re-copy it
1582 # if it is updated. (Gnulib does it this way because it allows
1583 # choosing which .in.h files to actually use in the build. We
1584 # need just getopt.h so this is a bit overcomplicated for
1585 # a single header file only.)
1586 configure_file("${CMAKE_CURRENT_SOURCE_DIR}/lib/getopt.in.h"
1587 "${CMAKE_CURRENT_BINARY_DIR}/lib/getopt.h"
1590 target_sources(libgnu PRIVATE
1597 lib/getopt-pfx-core.h
1598 lib/getopt-pfx-ext.h
1599 "${CMAKE_CURRENT_BINARY_DIR}/lib/getopt.h"
1604 #############################################################################
1606 #############################################################################
1608 if(HAVE_DECODERS AND (NOT MSVC OR MSVC_VERSION GREATER_EQUAL 1900))
1609 foreach(XZDEC xzdec lzmadec)
1610 add_executable("${XZDEC}"
1611 src/common/sysdefs.h
1612 src/common/tuklib_common.h
1613 src/common/tuklib_config.h
1614 src/common/tuklib_exit.c
1615 src/common/tuklib_exit.h
1616 src/common/tuklib_gettext.h
1617 src/common/tuklib_progname.c
1618 src/common/tuklib_progname.h
1622 target_include_directories("${XZDEC}" PRIVATE
1627 target_link_libraries("${XZDEC}" PRIVATE liblzma libgnu)
1630 # Add the Windows resource file for xzdec.exe or lzmadec.exe.
1631 target_sources("${XZDEC}" PRIVATE src/xzdec/xzdec_w32res.rc)
1632 set_target_properties("${XZDEC}" PROPERTIES
1633 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1637 if(SANDBOX_COMPILE_DEFINITION)
1638 target_compile_definitions("${XZDEC}" PRIVATE
1639 "${SANDBOX_COMPILE_DEFINITION}")
1642 tuklib_progname("${XZDEC}")
1644 install(TARGETS "${XZDEC}"
1645 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1646 COMPONENT "${XZDEC}_Runtime")
1649 # This is the only build-time difference with lzmadec.
1650 target_compile_definitions(lzmadec PRIVATE "LZMADEC")
1653 # NOTE: This puts the lzmadec.1 symlinks into xzdec_Documentation.
1654 # This isn't great but doing them separately with translated
1655 # man pages would require extra code. So this has to suffice for now.
1656 my_install_man(xzdec_Documentation src/xzdec/xzdec.1 lzmadec)
1661 #############################################################################
1663 #############################################################################
1665 if(HAVE_DECODERS AND (NOT MSVC OR MSVC_VERSION GREATER_EQUAL 1900))
1666 add_executable(lzmainfo
1667 src/common/sysdefs.h
1668 src/common/tuklib_common.h
1669 src/common/tuklib_config.h
1670 src/common/tuklib_exit.c
1671 src/common/tuklib_exit.h
1672 src/common/tuklib_gettext.h
1673 src/common/tuklib_progname.c
1674 src/common/tuklib_progname.h
1675 src/lzmainfo/lzmainfo.c
1678 target_include_directories(lzmainfo PRIVATE
1683 target_link_libraries(lzmainfo PRIVATE liblzma libgnu)
1686 # Add the Windows resource file for lzmainfo.exe.
1687 target_sources(lzmainfo PRIVATE src/lzmainfo/lzmainfo_w32res.rc)
1688 set_target_properties(lzmainfo PROPERTIES
1689 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1693 tuklib_progname(lzmainfo)
1695 # NOTE: The translations are in the "xz" domain and the .mo files are
1696 # installed as part of the "xz" target.
1698 target_link_libraries(lzmainfo PRIVATE Intl::Intl)
1700 target_compile_definitions(lzmainfo PRIVATE
1702 PACKAGE="${TRANSLATION_DOMAIN}"
1703 LOCALEDIR="${LOCALEDIR_DEFINITION}"
1707 install(TARGETS lzmainfo
1708 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1709 COMPONENT lzmainfo_Runtime)
1712 my_install_man(lzmainfo_Documentation src/lzmainfo/lzmainfo.1 "")
1717 #############################################################################
1719 #############################################################################
1721 if(NOT MSVC OR MSVC_VERSION GREATER_EQUAL 1900)
1723 src/common/mythread.h
1724 src/common/sysdefs.h
1725 src/common/tuklib_common.h
1726 src/common/tuklib_config.h
1727 src/common/tuklib_exit.c
1728 src/common/tuklib_exit.h
1729 src/common/tuklib_gettext.h
1730 src/common/tuklib_integer.h
1731 src/common/tuklib_mbstr.h
1732 src/common/tuklib_mbstr_fw.c
1733 src/common/tuklib_mbstr_width.c
1734 src/common/tuklib_open_stdxxx.c
1735 src/common/tuklib_open_stdxxx.h
1736 src/common/tuklib_progname.c
1737 src/common/tuklib_progname.h
1765 target_include_directories(xz PRIVATE
1771 target_sources(xz PRIVATE
1777 target_link_libraries(xz PRIVATE liblzma libgnu)
1779 if(USE_POSIX_THREADS)
1780 # src/xz/signals.c uses mythread_sigmask() which with POSIX
1781 # threads calls pthread_sigmask(). Thus, we need the threading
1782 # library as a dependency for xz. The liblzma target links against
1783 # Threads::Threads PRIVATEly, thus that won't provide the pthreads
1786 # NOTE: The build may work without this if the symbol is in libc
1787 # but it is mandatory to have this here to keep it working with
1788 # all pthread implementations.
1789 target_link_libraries(xz PRIVATE Threads::Threads)
1792 target_compile_definitions(xz PRIVATE ASSUME_RAM=128)
1795 # Add the Windows resource file for xz.exe.
1796 target_sources(xz PRIVATE src/xz/xz_w32res.rc)
1797 set_target_properties(xz PROPERTIES
1798 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1802 if(SANDBOX_COMPILE_DEFINITION)
1803 target_compile_definitions(xz PRIVATE "${SANDBOX_COMPILE_DEFINITION}")
1809 check_symbol_exists(optreset getopt.h HAVE_OPTRESET)
1810 tuklib_add_definition_if(xz HAVE_OPTRESET)
1812 check_symbol_exists(posix_fadvise fcntl.h HAVE_POSIX_FADVISE)
1813 tuklib_add_definition_if(xz HAVE_POSIX_FADVISE)
1815 # How to get file time:
1816 check_struct_has_member("struct stat" st_atim.tv_nsec
1817 "sys/types.h;sys/stat.h"
1818 HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1819 if(HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1820 tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1822 check_struct_has_member("struct stat" st_atimespec.tv_nsec
1823 "sys/types.h;sys/stat.h"
1824 HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1825 if(HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1826 tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1828 check_struct_has_member("struct stat" st_atimensec
1829 "sys/types.h;sys/stat.h"
1830 HAVE_STRUCT_STAT_ST_ATIMENSEC)
1831 tuklib_add_definition_if(xz HAVE_STRUCT_STAT_ST_ATIMENSEC)
1835 # How to set file time:
1836 check_symbol_exists(futimens "sys/types.h;sys/stat.h" HAVE_FUTIMENS)
1838 tuklib_add_definitions(xz HAVE_FUTIMENS)
1840 check_symbol_exists(futimes "sys/time.h" HAVE_FUTIMES)
1842 tuklib_add_definitions(xz HAVE_FUTIMES)
1844 check_symbol_exists(futimesat "sys/time.h" HAVE_FUTIMESAT)
1846 tuklib_add_definitions(xz HAVE_FUTIMESAT)
1848 check_symbol_exists(utimes "sys/time.h" HAVE_UTIMES)
1850 tuklib_add_definitions(xz HAVE_UTIMES)
1852 check_symbol_exists(_futime "sys/utime.h" HAVE__FUTIME)
1854 tuklib_add_definitions(xz HAVE__FUTIME)
1856 check_symbol_exists(utime "utime.h" HAVE_UTIME)
1857 tuklib_add_definition_if(xz HAVE_UTIME)
1865 target_link_libraries(xz PRIVATE Intl::Intl)
1867 target_compile_definitions(xz PRIVATE
1869 PACKAGE="${TRANSLATION_DOMAIN}"
1870 LOCALEDIR="${LOCALEDIR_DEFINITION}"
1873 file(STRINGS po/LINGUAS LINGUAS)
1875 # Where to find .gmo files. If msgfmt is available, the .po files
1876 # will be converted as part of the build. Otherwise we will use
1877 # the pre-generated .gmo files which are included in XZ Utils
1878 # tarballs by Autotools.
1879 set(GMO_DIR "${CMAKE_CURRENT_SOURCE_DIR}/po")
1882 # NOTE: gettext_process_po_files' INSTALL_DESTINATION is
1883 # incompatible with how Autotools requires the .po files to
1884 # be named. CMake would require each .po file to be named with
1885 # the translation domain and thus each .po file would need its
1886 # own language-specific directory (like "po/fi/xz.po"). On top
1887 # of this, INSTALL_DESTINATION doesn't allow specifying COMPONENT
1888 # and thus the .mo files go into "Unspecified" component. So we
1889 # can use gettext_process_po_files to convert the .po files but
1890 # installation needs to be done with our own code.
1892 # Also, the .gmo files will go to root of the build directory
1893 # instead of neatly into a subdirectory. This is hardcoded in
1894 # CMake's FindGettext.cmake.
1895 foreach(LANG IN LISTS LINGUAS)
1896 gettext_process_po_files("${LANG}" ALL
1897 PO_FILES "${CMAKE_CURRENT_SOURCE_DIR}/po/${LANG}.po")
1900 set(GMO_DIR "${CMAKE_CURRENT_BINARY_DIR}")
1903 foreach(LANG IN LISTS LINGUAS)
1905 FILES "${GMO_DIR}/${LANG}.gmo"
1906 DESTINATION "${CMAKE_INSTALL_LOCALEDIR}/${LANG}/LC_MESSAGES"
1907 RENAME "${TRANSLATION_DOMAIN}.mo"
1908 COMPONENT xz_Runtime)
1912 # This command must be before the symlink creation to keep things working
1913 # on Cygwin and MSYS2 in all cases.
1915 # - Cygwin can encode symlinks in multiple ways. This can be
1916 # controlled via the environment variable "CYGWIN". If it contains
1917 # "winsymlinks:nativestrict" then symlink creation will fail if
1918 # the link target doesn't exist. This mode isn't the default though.
1919 # See: https://cygwin.com/faq.html#faq.api.symlinks
1921 # - MSYS2 supports the same winsymlinks option in the environment
1922 # variable "MSYS" (not "MSYS2). The default in MSYS2 is to make
1923 # a copy of the file instead of any kind of symlink. Thus the link
1924 # target must exist or the creation of the "symlink" (copy) will fail.
1926 # Our installation order must be such that when a symbolic link is created
1927 # its target must already exists. There is no race condition for parallel
1928 # builds because the generated cmake_install.cmake executes serially.
1930 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1931 COMPONENT xz_Runtime)
1934 option(CREATE_XZ_SYMLINKS "Create unxz and xzcat symlinks" ON)
1935 option(CREATE_LZMA_SYMLINKS "Create lzma, unlzma, and lzcat symlinks"
1939 if(CREATE_XZ_SYMLINKS)
1940 list(APPEND XZ_LINKS "unxz" "xzcat")
1943 if(CREATE_LZMA_SYMLINKS)
1944 list(APPEND XZ_LINKS "lzma" "unlzma" "lzcat")
1947 # On Cygwin, don't add the .exe suffix to the symlinks.
1949 # FIXME? Does this make sense on MSYS & MSYS2 where "ln -s"
1950 # by default makes copies? Inside MSYS & MSYS2 it is possible
1951 # to execute files without the .exe suffix but not outside
1952 # (like in Command Prompt). Omitting the suffix matches
1953 # what configure.ac has done for many years though.
1954 my_install_symlinks(xz_Runtime "${CMAKE_INSTALL_BINDIR}"
1955 "xz${CMAKE_EXECUTABLE_SUFFIX}" "" "${XZ_LINKS}")
1957 # Install the man pages and (optionally) their symlinks
1959 my_install_man(xz_Documentation src/xz/xz.1 "${XZ_LINKS}")
1964 #############################################################################
1966 #############################################################################
1969 # NOTE: This isn't as sophisticated as in the Autotools build which
1970 # uses posix-shell.m4 but hopefully this doesn't need to be either.
1971 # CMake likely won't be used on as many (old) obscure systems as the
1972 # Autotools-based builds are.
1973 if(CMAKE_SYSTEM_NAME STREQUAL "SunOS" AND EXISTS "/usr/xpg4/bin/sh")
1974 set(POSIX_SHELL_DEFAULT "/usr/xpg4/bin/sh")
1976 set(POSIX_SHELL_DEFAULT "/bin/sh")
1979 set(POSIX_SHELL "${POSIX_SHELL_DEFAULT}" CACHE STRING
1980 "Shell to use for scripts (xzgrep and others)")
1982 # Guess the extra path to add from POSIX_SHELL. Autotools-based build
1983 # has a separate option --enable-path-for-scripts=PREFIX but this is
1984 # enough for Solaris.
1985 set(enable_path_for_scripts)
1986 get_filename_component(POSIX_SHELL_DIR "${POSIX_SHELL}" DIRECTORY)
1988 if(NOT POSIX_SHELL_DIR STREQUAL "/bin" AND
1989 NOT POSIX_SHELL_DIR STREQUAL "/usr/bin")
1990 set(enable_path_for_scripts "PATH=${POSIX_SHELL_DIR}:\$PATH")
1993 set(XZDIFF_LINKS xzcmp)
1994 set(XZGREP_LINKS xzegrep xzfgrep)
1998 if(CREATE_LZMA_SYMLINKS)
1999 list(APPEND XZDIFF_LINKS lzdiff lzcmp)
2000 list(APPEND XZGREP_LINKS lzgrep lzegrep lzfgrep)
2001 list(APPEND XZMORE_LINKS lzmore)
2002 list(APPEND XZLESS_LINKS lzless)
2007 foreach(S xzdiff xzgrep xzmore xzless)
2008 configure_file("src/scripts/${S}.in" "${S}"
2012 install(PROGRAMS "${CMAKE_CURRENT_BINARY_DIR}/${S}"
2013 DESTINATION "${CMAKE_INSTALL_BINDIR}"
2014 COMPONENT scripts_Runtime)
2017 # file(CHMOD ...) would need CMake 3.19 so use execute_process instead.
2018 # Using +x is fine even if umask was 077. If execute bit is set at all
2019 # then "make install" will set it for group and other access bits too.
2020 execute_process(COMMAND chmod +x xzdiff xzgrep xzmore xzless
2021 WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
2025 unset(enable_path_for_scripts)
2027 my_install_symlinks(scripts_Runtime "${CMAKE_INSTALL_BINDIR}" xzdiff ""
2030 my_install_symlinks(scripts_Runtime "${CMAKE_INSTALL_BINDIR}" xzgrep ""
2033 my_install_symlinks(scripts_Runtime "${CMAKE_INSTALL_BINDIR}" xzmore ""
2036 my_install_symlinks(scripts_Runtime "${CMAKE_INSTALL_BINDIR}" xzless ""
2039 my_install_man(scripts_Documentation src/scripts/xzdiff.1 "${XZDIFF_LINKS}")
2040 my_install_man(scripts_Documentation src/scripts/xzgrep.1 "${XZGREP_LINKS}")
2041 my_install_man(scripts_Documentation src/scripts/xzmore.1 "${XZMORE_LINKS}")
2042 my_install_man(scripts_Documentation src/scripts/xzless.1 "${XZLESS_LINKS}")
2046 #############################################################################
2048 #############################################################################
2051 option(ENABLE_DOXYGEN "Use Doxygen to generate liblzma API docs" OFF)
2054 file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/doc")
2058 COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/doxygen/update-doxygen"
2060 "${CMAKE_CURRENT_SOURCE_DIR}"
2061 "${CMAKE_CURRENT_BINARY_DIR}/doc"
2062 OUTPUT doc/api/index.html
2063 DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/doxygen/update-doxygen"
2064 "${CMAKE_CURRENT_SOURCE_DIR}/doxygen/Doxyfile"
2065 ${LIBLZMA_API_HEADERS}
2070 DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/doc/api/index.html"
2073 install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/doc/api"
2074 DESTINATION "${CMAKE_INSTALL_DOCDIR}"
2075 COMPONENT liblzma_Documentation)
2079 install(DIRECTORY doc/examples
2080 DESTINATION "${CMAKE_INSTALL_DOCDIR}"
2081 COMPONENT liblzma_Documentation)
2083 # GPLv2 applies to the scripts. If GNU getopt_long is used then
2084 # LGPLv2.1 applies to the command line tools but, using the
2085 # section 3 of LGPLv2.1, GNU getopt_long can be handled as GPLv2 too.
2086 # Thus GPLv2 should be enough here.
2087 install(FILES AUTHORS
2096 doc/lzma-file-format.txt
2097 doc/xz-file-format.txt
2098 DESTINATION "${CMAKE_INSTALL_DOCDIR}"
2099 COMPONENT Documentation)
2102 #############################################################################
2104 #############################################################################
2106 # Tests are in a separate file so that it's possible to delete the whole
2107 # "tests" directory and still have a working build, just without the tests.
2108 include(tests/tests.cmake OPTIONAL)