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.29 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 C17). Setting CMAKE_C_STANDARD here
156 # makes it the default for all targets. It doesn't affect the INTERFACE so
157 # liblzma::liblzma won't end up with INTERFACE_COMPILE_FEATURES "c_std_99"
158 # (the API headers are C89 and C++ compatible).
159 set(CMAKE_C_STANDARD 99)
160 set(CMAKE_C_STANDARD_REQUIRED ON)
162 # Support 32-bit x86 assembly files.
164 option(ENABLE_X86_ASM "Enable 32-bit x86 assembly code" OFF)
170 # On Apple OSes, don't build executables as bundles:
171 set(CMAKE_MACOSX_BUNDLE OFF)
173 # Set CMAKE_INSTALL_LIBDIR and friends. This needs to be done before
174 # the LOCALEDIR_DEFINITION workaround below.
175 include(GNUInstallDirs)
177 # windres from GNU binutils can be tricky with command line arguments
178 # that contain spaces or other funny characters. Unfortunately we need
179 # a space in PACKAGE_NAME. Using \x20 to encode the US-ASCII space seems
180 # to work in both cmd.exe and /bin/sh.
182 # However, even \x20 isn't enough in all situations, resulting in
183 # "syntax error" from windres. Using --use-temp-file prevents windres
184 # from using popen() and this seems to fix the problem.
186 # llvm-windres from Clang/LLVM 16.0.6 and older: The \x20 results
187 # in "XZx20Utils" in the compiled binary. The option --use-temp-file
188 # makes no difference.
190 # llvm-windres 17.0.0 and later: It emulates GNU windres more accurately, so
191 # the workarounds used with GNU windres must be used with llvm-windres too.
193 # CMake 3.27 doesn't have CMAKE_RC_COMPILER_ID so we rely on
194 # CMAKE_C_COMPILER_ID.
195 if((MINGW OR CYGWIN OR MSYS) AND (
196 NOT CMAKE_C_COMPILER_ID STREQUAL "Clang" OR
197 CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "17"))
198 # Use workarounds with GNU windres and llvm-windres >= 17.0.0. The \x20
199 # in PACKAGE_NAME_DEFINITION works with gcc and clang too so we don't need
200 # to worry how to pass different flags to windres and the C compiler.
201 # Keep the original PACKAGE_NAME intact for generation of liblzma.pc.
202 string(APPEND CMAKE_RC_FLAGS " --use-temp-file")
203 string(REPLACE " " "\\x20" PACKAGE_NAME_DEFINITION "${PACKAGE_NAME}")
205 # Use octal because "Program Files" would become \x20F.
206 string(REPLACE " " "\\040" LOCALEDIR_DEFINITION
207 "${CMAKE_INSTALL_FULL_LOCALEDIR}")
209 # Elsewhere a space is safe. This also keeps things compatible with
210 # EBCDIC in case CMake-based build is ever done on such a system.
211 set(PACKAGE_NAME_DEFINITION "${PACKAGE_NAME}")
212 set(LOCALEDIR_DEFINITION "${CMAKE_INSTALL_FULL_LOCALEDIR}")
215 # Definitions common to all targets:
216 add_compile_definitions(
218 PACKAGE_NAME="${PACKAGE_NAME_DEFINITION}"
219 PACKAGE_BUGREPORT="${PACKAGE_BUGREPORT}"
220 PACKAGE_URL="${PACKAGE_URL}"
222 # Standard headers and types are available:
228 # Always enable CRC32 since liblzma should never build without it.
231 # Disable assert() checks when no build type has been specified. Non-empty
232 # build types like "Release" and "Debug" handle this by default.
237 ######################
238 # System definitions #
239 ######################
241 # _GNU_SOURCE and such definitions. This specific macro is special since
242 # it also adds the definitions to CMAKE_REQUIRED_DEFINITIONS.
243 tuklib_use_system_extensions(ALL)
245 # Check for large file support. It's required on some 32-bit platforms and
246 # even on 64-bit MinGW-w64 to get 64-bit off_t. This can be forced off on
247 # the CMake command line if needed: -DLARGE_FILE_SUPPORT=OFF
248 tuklib_large_file_support(ALL)
250 # This is needed by liblzma and xz.
253 # This is used for liblzma.pc generation to add -lrt if needed.
256 # Check for clock_gettime(). Do this before checking for threading so
257 # that we know there if CLOCK_MONOTONIC is available.
258 check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME)
260 if(NOT HAVE_CLOCK_GETTIME)
261 # With glibc <= 2.17 or Solaris 10 this needs librt.
262 # Add librt for the next check for HAVE_CLOCK_GETTIME. If it is
263 # found after including the library, we know that librt is required.
264 list(INSERT CMAKE_REQUIRED_LIBRARIES 0 rt)
265 check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME_LIBRT)
267 # If it was found now, add librt to all targets and keep it in
268 # CMAKE_REQUIRED_LIBRARIES for further tests too.
269 if(HAVE_CLOCK_GETTIME_LIBRT)
271 set(LIBS "-lrt") # For liblzma.pc
273 list(REMOVE_AT CMAKE_REQUIRED_LIBRARIES 0)
277 if(HAVE_CLOCK_GETTIME OR HAVE_CLOCK_GETTIME_LIBRT)
278 add_compile_definitions(HAVE_CLOCK_GETTIME)
280 # Check if CLOCK_MONOTONIC is available for clock_gettime().
281 check_symbol_exists(CLOCK_MONOTONIC time.h HAVE_CLOCK_MONOTONIC)
282 tuklib_add_definition_if(ALL HAVE_CLOCK_MONOTONIC)
285 # Translation support requires CMake 3.20 because it added the Intl::Intl
286 # target so we don't need to play with the individual variables.
288 # The definition ENABLE_NLS is added only to those targets that use it, thus
289 # it's not done here. (xz has translations, xzdec doesn't.)
290 if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.20")
292 find_package(Gettext)
295 option(ENABLE_NLS "Native Language Support (translated messages)" ON)
297 # If translation support is enabled but neither gettext tools or
298 # pre-generated .gmo files exist, translation support cannot be
301 # The detection of pre-generated .gmo files is done by only
302 # checking for the existence of a single .gmo file; Ukrainian
303 # is one of many translations that gets regular updates.
304 if(ENABLE_NLS AND NOT GETTEXT_FOUND AND
305 NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/po/uk.gmo")
306 # This only sets the variable, not the cache variable!
309 # This message is shown only when new enough CMake is used and
310 # library support for translations was found. The assumptions is
311 # that in this situation the user might have interest in the
312 # translations. This also keeps this code simpler.
313 message(WARNING "Native language support (NLS) has been disabled. "
314 "NLS support requires either gettext tools or "
315 "pre-generated .gmo files. The latter are only "
316 "available in distribution tarballs. "
317 "To avoid this warning, NLS can be explicitly "
318 "disabled by passing -DENABLE_NLS=OFF to cmake.")
321 # Warn if NLS is enabled but translated man pages are missing.
322 if(UNIX AND ENABLE_NLS AND
323 NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/po4a/man")
324 message(WARNING "Native language support (NLS) has been enabled "
325 "but pre-generated translated man pages "
326 "were not found and thus they won't be installed. "
327 "Run 'po4a/update-po' to generate them.")
330 # The *installed* name of the translation files is "xz.mo".
331 set(TRANSLATION_DOMAIN "xz")
335 # Options for new enough GCC or Clang on any arch or operating system:
336 if(CMAKE_C_COMPILER_ID MATCHES GNU|Clang)
337 # configure.ac has a long list but it won't be copied here:
338 add_compile_options(-Wall -Wextra)
342 #############################################################################
344 #############################################################################
346 option(BUILD_SHARED_LIBS "Build liblzma as a shared library instead of static")
349 # Symbol versioning only affects ELF shared libraries. The option is
350 # ignored for static libraries.
352 # Determine the default value so that it's always set with
353 # shared libraries in mind which helps if the build dir is reconfigured
354 # from static to shared libs without resetting the cache variables.
355 set(SYMBOL_VERSIONING_DEFAULT OFF)
357 if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND
358 (CMAKE_SYSTEM_PROCESSOR MATCHES "[Mm]icro[Bb]laze" OR
359 CMAKE_C_COMPILER_ID STREQUAL "NVHPC"))
360 # As a special case, GNU/Linux on MicroBlaze gets the generic
361 # symbol versioning because GCC 12 doesn't support the __symver__
362 # attribute on MicroBlaze. On Linux, CMAKE_SYSTEM_PROCESSOR comes
363 # from "uname -m" for native builds (should be "microblaze") or from
364 # the CMake toolchain file (not perfectly standardized but it very
365 # likely has "microblaze" in lower case or mixed case somewhere in
368 # NVIDIA HPC Compiler doesn't support symbol versioning but
369 # it uses the linked from the system so the linker script
370 # can still be used to get the generic symbol versioning.
371 set(SYMBOL_VERSIONING_DEFAULT "generic")
373 elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
374 # GNU/Linux-specific symbol versioning for shared liblzma.
375 # This includes a few extra compatibility symbols for RHEL/CentOS 7
376 # which are pointless on non-glibc non-Linux systems.
378 # Avoid symvers on Linux with non-glibc like musl and uClibc.
379 # In Autoconf it's enough to check that $host_os equals linux-gnu
380 # instead of, for example, linux-musl. CMake doesn't provide such
383 # This check is here for now since it's not strictly required
385 check_c_source_compiles(
386 "#include <features.h>
387 #if defined(__GLIBC__) && !defined(__UCLIBC__)
388 int main(void) { return 0; }
395 if(IS_LINUX_WITH_GLIBC)
396 set(SYMBOL_VERSIONING_DEFAULT "linux")
399 elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
400 set(SYMBOL_VERSIONING_DEFAULT "generic")
403 set(SYMBOL_VERSIONING "${SYMBOL_VERSIONING_DEFAULT}" CACHE STRING
404 "Enable ELF shared library symbol versioning (OFF, generic, linux)")
406 # Show a dropdown menu in CMake GUI:
407 set_property(CACHE SYMBOL_VERSIONING PROPERTY STRINGS "OFF;generic;linux")
410 set(LIBLZMA_API_HEADERS
411 src/liblzma/api/lzma.h
412 src/liblzma/api/lzma/base.h
413 src/liblzma/api/lzma/bcj.h
414 src/liblzma/api/lzma/block.h
415 src/liblzma/api/lzma/check.h
416 src/liblzma/api/lzma/container.h
417 src/liblzma/api/lzma/delta.h
418 src/liblzma/api/lzma/filter.h
419 src/liblzma/api/lzma/hardware.h
420 src/liblzma/api/lzma/index.h
421 src/liblzma/api/lzma/index_hash.h
422 src/liblzma/api/lzma/lzma12.h
423 src/liblzma/api/lzma/stream_flags.h
424 src/liblzma/api/lzma/version.h
425 src/liblzma/api/lzma/vli.h
429 src/common/mythread.h
431 src/common/tuklib_common.h
432 src/common/tuklib_config.h
433 src/common/tuklib_integer.h
434 src/common/tuklib_physmem.c
435 src/common/tuklib_physmem.h
436 ${LIBLZMA_API_HEADERS}
437 src/liblzma/check/check.c
438 src/liblzma/check/check.h
439 src/liblzma/check/crc_common.h
440 src/liblzma/check/crc_x86_clmul.h
441 src/liblzma/check/crc32_arm64.h
442 src/liblzma/common/block_util.c
443 src/liblzma/common/common.c
444 src/liblzma/common/common.h
445 src/liblzma/common/easy_preset.c
446 src/liblzma/common/easy_preset.h
447 src/liblzma/common/filter_common.c
448 src/liblzma/common/filter_common.h
449 src/liblzma/common/hardware_physmem.c
450 src/liblzma/common/index.c
451 src/liblzma/common/index.h
452 src/liblzma/common/memcmplen.h
453 src/liblzma/common/stream_flags_common.c
454 src/liblzma/common/stream_flags_common.h
455 src/liblzma/common/string_conversion.c
456 src/liblzma/common/vli_size.c
459 target_include_directories(liblzma PRIVATE
464 src/liblzma/rangecoder
472 ######################
473 # Size optimizations #
474 ######################
476 option(ENABLE_SMALL "Reduce code size at expense of speed. \
477 This may be useful together with CMAKE_BUILD_TYPE=MinSizeRel.")
480 add_compile_definitions(HAVE_SMALL)
488 set(ADDITIONAL_SUPPORTED_CHECKS crc64 sha256)
490 set(ADDITIONAL_CHECK_TYPES "${ADDITIONAL_SUPPORTED_CHECKS}" CACHE STRING
491 "Additional check types to support (crc32 is always built)")
493 foreach(CHECK IN LISTS ADDITIONAL_CHECK_TYPES)
494 if(NOT CHECK IN_LIST ADDITIONAL_SUPPORTED_CHECKS)
495 message(FATAL_ERROR "'${CHECK}' is not a supported check type")
500 target_sources(liblzma PRIVATE src/liblzma/check/crc32_small.c)
502 target_sources(liblzma PRIVATE
503 src/liblzma/check/crc32_table.c
504 src/liblzma/check/crc32_table_be.h
505 src/liblzma/check/crc32_table_le.h
509 target_sources(liblzma PRIVATE src/liblzma/check/crc32_x86.S)
511 target_sources(liblzma PRIVATE src/liblzma/check/crc32_fast.c)
515 if("crc64" IN_LIST ADDITIONAL_CHECK_TYPES)
516 add_compile_definitions("HAVE_CHECK_CRC64")
519 target_sources(liblzma PRIVATE src/liblzma/check/crc64_small.c)
521 target_sources(liblzma PRIVATE
522 src/liblzma/check/crc64_table.c
523 src/liblzma/check/crc64_table_be.h
524 src/liblzma/check/crc64_table_le.h
528 target_sources(liblzma PRIVATE src/liblzma/check/crc64_x86.S)
530 target_sources(liblzma PRIVATE src/liblzma/check/crc64_fast.c)
535 if("sha256" IN_LIST ADDITIONAL_CHECK_TYPES)
536 add_compile_definitions("HAVE_CHECK_SHA256")
537 target_sources(liblzma PRIVATE src/liblzma/check/sha256.c)
545 set(SUPPORTED_MATCH_FINDERS hc3 hc4 bt2 bt3 bt4)
547 set(MATCH_FINDERS "${SUPPORTED_MATCH_FINDERS}" CACHE STRING
548 "Match finders to support (at least one is required for LZMA1 or LZMA2)")
550 foreach(MF IN LISTS MATCH_FINDERS)
551 if(MF IN_LIST SUPPORTED_MATCH_FINDERS)
552 string(TOUPPER "${MF}" MF_UPPER)
553 add_compile_definitions("HAVE_MF_${MF_UPPER}")
555 message(FATAL_ERROR "'${MF}' is not a supported match finder")
564 # Supported threading methods:
565 # ON - autodetect the best threading method. The autodetection will
566 # prefer Windows threading (win95 or vista) over posix if both are
567 # available. vista threads will be used over win95 unless it is a
569 # OFF - Disable threading.
570 # posix - Use posix threading (pthreads), or throw an error if not available.
571 # win95 - Use Windows win95 threading, or throw an error if not available.
572 # vista - Use Windows vista threading, or throw an error if not available.
573 set(SUPPORTED_THREADING_METHODS ON OFF posix win95 vista)
575 set(ENABLE_THREADS ON CACHE STRING
576 "Threading method: Set to 'ON' to autodetect, 'OFF' to disable threading.")
578 # Create dropdown in CMake GUI since only 1 threading method is possible
579 # to select in a build.
580 set_property(CACHE ENABLE_THREADS
581 PROPERTY STRINGS "${SUPPORTED_THREADING_METHODS}")
583 # This is a flag variable set when win95 threads are used. We must ensure
584 # the combination of enable_small and win95 threads is not used without a
585 # compiler supporting attribute __constructor__.
586 set(USE_WIN95_THREADS OFF)
588 # This is a flag variable set when posix threads (pthreads) are used.
589 # It's needed when creating liblzma-config.cmake where dependency on
590 # Threads::Threads is only needed with pthreads.
591 set(USE_POSIX_THREADS OFF)
593 if(NOT ENABLE_THREADS IN_LIST SUPPORTED_THREADING_METHODS)
594 message(FATAL_ERROR "'${ENABLE_THREADS}' is not a supported "
599 # Also set THREADS_PREFER_PTHREAD_FLAG since the flag has no effect
600 # for Windows threading.
601 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
602 find_package(Threads REQUIRED)
604 # If both Windows and posix threading are available, prefer Windows.
605 # Note that on Cygwin CMAKE_USE_WIN32_THREADS_INIT is false.
606 if(CMAKE_USE_WIN32_THREADS_INIT AND NOT ENABLE_THREADS STREQUAL "posix")
607 if(ENABLE_THREADS STREQUAL "win95"
608 OR (ENABLE_THREADS STREQUAL "ON"
609 AND CMAKE_SIZEOF_VOID_P EQUAL 4))
610 # Use Windows 95 (and thus XP) compatible threads.
611 # This avoids use of features that were added in
612 # Windows Vista. This is used for 32-bit x86 builds for
613 # compatibility reasons since it makes no measurable difference
614 # in performance compared to Vista threads.
615 set(USE_WIN95_THREADS ON)
616 add_compile_definitions(MYTHREAD_WIN95)
618 add_compile_definitions(MYTHREAD_VISTA)
620 elseif(CMAKE_USE_PTHREADS_INIT)
621 if(ENABLE_THREADS STREQUAL "posix" OR ENABLE_THREADS STREQUAL "ON")
622 # The threading library only needs to be explicitly linked
623 # for posix threads, so this is needed for creating
624 # liblzma-config.cmake later.
625 set(USE_POSIX_THREADS ON)
627 target_link_libraries(liblzma Threads::Threads)
628 add_compile_definitions(MYTHREAD_POSIX)
630 # Check if pthread_condattr_setclock() exists to
631 # use CLOCK_MONOTONIC.
632 if(HAVE_CLOCK_MONOTONIC)
633 list(INSERT CMAKE_REQUIRED_LIBRARIES 0
634 "${CMAKE_THREAD_LIBS_INIT}")
635 check_symbol_exists(pthread_condattr_setclock pthread.h
636 HAVE_PTHREAD_CONDATTR_SETCLOCK)
637 tuklib_add_definition_if(ALL HAVE_PTHREAD_CONDATTR_SETCLOCK)
641 "Windows threading method was requested but a compatible "
642 "library could not be found")
645 message(SEND_ERROR "No supported threading library found")
648 target_sources(liblzma PRIVATE
649 src/common/tuklib_cpucores.c
650 src/common/tuklib_cpucores.h
651 src/liblzma/common/hardware_cputhreads.c
652 src/liblzma/common/outqueue.c
653 src/liblzma/common/outqueue.h
673 # The SUPPORTED_FILTERS are shared between Encoders and Decoders
674 # since only lzip does not appear in both lists. lzip is a special
675 # case anyway, so it is handled separately in the Decoders section.
676 set(SUPPORTED_FILTERS
683 set(ENCODERS "${SUPPORTED_FILTERS}" CACHE STRING "Encoders to support")
685 # If LZMA2 is enabled, then LZMA1 must also be enabled.
686 if(NOT "lzma1" IN_LIST ENCODERS AND "lzma2" IN_LIST ENCODERS)
687 message(FATAL_ERROR "LZMA2 encoder requires that LZMA1 is also enabled")
690 # If LZMA1 is enabled, then at least one match finder must be enabled.
691 if(MATCH_FINDERS STREQUAL "" AND "lzma1" IN_LIST ENCODERS)
692 message(FATAL_ERROR "At least 1 match finder is required for an "
696 set(HAVE_DELTA_CODER OFF)
697 set(SIMPLE_ENCODERS OFF)
698 set(HAVE_ENCODERS OFF)
700 foreach(ENCODER IN LISTS ENCODERS)
701 if(ENCODER IN_LIST SUPPORTED_FILTERS)
702 set(HAVE_ENCODERS ON)
704 if(NOT SIMPLE_ENCODERS AND ENCODER IN_LIST SIMPLE_FILTERS)
705 set(SIMPLE_ENCODERS ON)
708 string(TOUPPER "${ENCODER}" ENCODER_UPPER)
709 add_compile_definitions("HAVE_ENCODER_${ENCODER_UPPER}")
711 message(FATAL_ERROR "'${ENCODER}' is not a supported encoder")
716 add_compile_definitions(HAVE_ENCODERS)
718 target_sources(liblzma PRIVATE
719 src/liblzma/common/alone_encoder.c
720 src/liblzma/common/block_buffer_encoder.c
721 src/liblzma/common/block_buffer_encoder.h
722 src/liblzma/common/block_encoder.c
723 src/liblzma/common/block_encoder.h
724 src/liblzma/common/block_header_encoder.c
725 src/liblzma/common/easy_buffer_encoder.c
726 src/liblzma/common/easy_encoder.c
727 src/liblzma/common/easy_encoder_memusage.c
728 src/liblzma/common/filter_buffer_encoder.c
729 src/liblzma/common/filter_encoder.c
730 src/liblzma/common/filter_encoder.h
731 src/liblzma/common/filter_flags_encoder.c
732 src/liblzma/common/index_encoder.c
733 src/liblzma/common/index_encoder.h
734 src/liblzma/common/stream_buffer_encoder.c
735 src/liblzma/common/stream_encoder.c
736 src/liblzma/common/stream_flags_encoder.c
737 src/liblzma/common/vli_encoder.c
741 target_sources(liblzma PRIVATE
742 src/liblzma/common/stream_encoder_mt.c
747 target_sources(liblzma PRIVATE
748 src/liblzma/simple/simple_encoder.c
749 src/liblzma/simple/simple_encoder.h
753 if("lzma1" IN_LIST ENCODERS)
754 target_sources(liblzma PRIVATE
755 src/liblzma/lzma/lzma_encoder.c
756 src/liblzma/lzma/lzma_encoder.h
757 src/liblzma/lzma/lzma_encoder_optimum_fast.c
758 src/liblzma/lzma/lzma_encoder_optimum_normal.c
759 src/liblzma/lzma/lzma_encoder_private.h
760 src/liblzma/lzma/fastpos.h
761 src/liblzma/lz/lz_encoder.c
762 src/liblzma/lz/lz_encoder.h
763 src/liblzma/lz/lz_encoder_hash.h
764 src/liblzma/lz/lz_encoder_hash_table.h
765 src/liblzma/lz/lz_encoder_mf.c
766 src/liblzma/rangecoder/price.h
767 src/liblzma/rangecoder/price_table.c
768 src/liblzma/rangecoder/range_encoder.h
772 target_sources(liblzma PRIVATE src/liblzma/lzma/fastpos_table.c)
776 if("lzma2" IN_LIST ENCODERS)
777 target_sources(liblzma PRIVATE
778 src/liblzma/lzma/lzma2_encoder.c
779 src/liblzma/lzma/lzma2_encoder.h
783 if("delta" IN_LIST ENCODERS)
784 set(HAVE_DELTA_CODER ON)
785 target_sources(liblzma PRIVATE
786 src/liblzma/delta/delta_encoder.c
787 src/liblzma/delta/delta_encoder.h
797 set(DECODERS "${SUPPORTED_FILTERS}" CACHE STRING "Decoders to support")
799 set(SIMPLE_DECODERS OFF)
800 set(HAVE_DECODERS OFF)
802 foreach(DECODER IN LISTS DECODERS)
803 if(DECODER IN_LIST SUPPORTED_FILTERS)
804 set(HAVE_DECODERS ON)
806 if(NOT SIMPLE_DECODERS AND DECODER IN_LIST SIMPLE_FILTERS)
807 set(SIMPLE_DECODERS ON)
810 string(TOUPPER "${DECODER}" DECODER_UPPER)
811 add_compile_definitions("HAVE_DECODER_${DECODER_UPPER}")
813 message(FATAL_ERROR "'${DECODER}' is not a supported decoder")
818 add_compile_definitions(HAVE_DECODERS)
820 target_sources(liblzma PRIVATE
821 src/liblzma/common/alone_decoder.c
822 src/liblzma/common/alone_decoder.h
823 src/liblzma/common/auto_decoder.c
824 src/liblzma/common/block_buffer_decoder.c
825 src/liblzma/common/block_decoder.c
826 src/liblzma/common/block_decoder.h
827 src/liblzma/common/block_header_decoder.c
828 src/liblzma/common/easy_decoder_memusage.c
829 src/liblzma/common/file_info.c
830 src/liblzma/common/filter_buffer_decoder.c
831 src/liblzma/common/filter_decoder.c
832 src/liblzma/common/filter_decoder.h
833 src/liblzma/common/filter_flags_decoder.c
834 src/liblzma/common/index_decoder.c
835 src/liblzma/common/index_decoder.h
836 src/liblzma/common/index_hash.c
837 src/liblzma/common/stream_buffer_decoder.c
838 src/liblzma/common/stream_decoder.c
839 src/liblzma/common/stream_flags_decoder.c
840 src/liblzma/common/stream_decoder.h
841 src/liblzma/common/vli_decoder.c
845 target_sources(liblzma PRIVATE
846 src/liblzma/common/stream_decoder_mt.c
851 target_sources(liblzma PRIVATE
852 src/liblzma/simple/simple_decoder.c
853 src/liblzma/simple/simple_decoder.h
857 if("lzma1" IN_LIST DECODERS)
858 target_sources(liblzma PRIVATE
859 src/liblzma/lzma/lzma_decoder.c
860 src/liblzma/lzma/lzma_decoder.h
861 src/liblzma/rangecoder/range_decoder.h
862 src/liblzma/lz/lz_decoder.c
863 src/liblzma/lz/lz_decoder.h
867 if("lzma2" IN_LIST DECODERS)
868 target_sources(liblzma PRIVATE
869 src/liblzma/lzma/lzma2_decoder.c
870 src/liblzma/lzma/lzma2_decoder.h
874 if("delta" IN_LIST DECODERS)
875 set(HAVE_DELTA_CODER ON)
876 target_sources(liblzma PRIVATE
877 src/liblzma/delta/delta_decoder.c
878 src/liblzma/delta/delta_decoder.h
883 # Some sources must appear if the filter is configured as either
884 # an encoder or decoder.
885 if("lzma1" IN_LIST ENCODERS OR "lzma1" IN_LIST DECODERS)
886 target_sources(liblzma PRIVATE
887 src/liblzma/rangecoder/range_common.h
888 src/liblzma/lzma/lzma_encoder_presets.c
889 src/liblzma/lzma/lzma_common.h
894 target_sources(liblzma PRIVATE
895 src/liblzma/delta/delta_common.c
896 src/liblzma/delta/delta_common.h
897 src/liblzma/delta/delta_private.h
901 if(SIMPLE_ENCODERS OR SIMPLE_DECODERS)
902 target_sources(liblzma PRIVATE
903 src/liblzma/simple/simple_coder.c
904 src/liblzma/simple/simple_coder.h
905 src/liblzma/simple/simple_private.h
909 foreach(SIMPLE_CODER IN LISTS SIMPLE_FILTERS)
910 if(SIMPLE_CODER IN_LIST ENCODERS OR SIMPLE_CODER IN_LIST DECODERS)
911 target_sources(liblzma PRIVATE "src/liblzma/simple/${SIMPLE_CODER}.c")
920 option(MICROLZMA_ENCODER
921 "MicroLZMA encoder (needed by specific applications only)" ON)
923 option(MICROLZMA_DECODER
924 "MicroLZMA decoder (needed by specific applications only)" ON)
926 if(MICROLZMA_ENCODER)
927 if(NOT "lzma1" IN_LIST ENCODERS)
928 message(FATAL_ERROR "The LZMA1 encoder is required to support the "
932 target_sources(liblzma PRIVATE src/liblzma/common/microlzma_encoder.c)
935 if(MICROLZMA_DECODER)
936 if(NOT "lzma1" IN_LIST DECODERS)
937 message(FATAL_ERROR "The LZMA1 decoder is required to support the "
941 target_sources(liblzma PRIVATE src/liblzma/common/microlzma_decoder.c)
945 #############################
946 # lzip (.lz) format support #
947 #############################
949 option(LZIP_DECODER "Support lzip decoder" ON)
952 # If lzip decoder support is requested, make sure LZMA1 decoder is enabled.
953 if(NOT "lzma1" IN_LIST DECODERS)
954 message(FATAL_ERROR "The LZMA1 decoder is required to support the "
958 add_compile_definitions(HAVE_LZIP_DECODER)
960 target_sources(liblzma PRIVATE
961 src/liblzma/common/lzip_decoder.c
962 src/liblzma/common/lzip_decoder.h
971 # ON Use sandboxing if a supported method is available in the OS.
972 # OFF Disable sandboxing.
973 # capsicum Require Capsicum (FreeBSD >= 10.2) and fail if not found.
974 # pledge Require pledge(2) (OpenBSD >= 5.9) and fail if not found.
975 # landlock Require Landlock (Linux >= 5.13) and fail if not found.
976 set(SUPPORTED_SANDBOX_METHODS ON OFF capsicum pledge landlock)
978 set(ENABLE_SANDBOX ON CACHE STRING
979 "Sandboxing method to use in 'xz', 'xzdec', and 'lzmadec'")
981 set_property(CACHE ENABLE_SANDBOX
982 PROPERTY STRINGS "${SUPPORTED_SANDBOX_METHODS}")
984 if(NOT ENABLE_SANDBOX IN_LIST SUPPORTED_SANDBOX_METHODS)
985 message(FATAL_ERROR "'${ENABLE_SANDBOX}' is not a supported "
989 # When autodetecting, the search order is fixed and we must not find
990 # more than one method.
991 if(ENABLE_SANDBOX STREQUAL "OFF")
992 set(SANDBOX_FOUND ON)
994 set(SANDBOX_FOUND OFF)
997 # Since xz and xzdec can both use sandboxing, the compile definition needed
998 # to use the sandbox must be added to both targets.
999 set(SANDBOX_COMPILE_DEFINITION OFF)
1001 # Sandboxing: Capsicum
1002 if(NOT SANDBOX_FOUND AND ENABLE_SANDBOX MATCHES "^ON$|^capsicum$")
1003 check_symbol_exists(cap_rights_limit sys/capsicum.h
1004 HAVE_CAP_RIGHTS_LIMIT)
1005 if(HAVE_CAP_RIGHTS_LIMIT)
1006 set(SANDBOX_COMPILE_DEFINITION "HAVE_CAP_RIGHTS_LIMIT")
1007 set(SANDBOX_FOUND ON)
1011 # Sandboxing: pledge(2)
1012 if(NOT SANDBOX_FOUND AND ENABLE_SANDBOX MATCHES "^ON$|^pledge$")
1013 check_symbol_exists(pledge unistd.h HAVE_PLEDGE)
1015 set(SANDBOX_COMPILE_DEFINITION "HAVE_PLEDGE")
1016 set(SANDBOX_FOUND ON)
1020 # Sandboxing: Landlock
1021 if(NOT SANDBOX_FOUND AND ENABLE_SANDBOX MATCHES "^ON$|^landlock$")
1022 # A compile check is done here because some systems have
1023 # linux/landlock.h, but do not have the syscalls defined
1024 # in order to actually use Linux Landlock.
1025 check_c_source_compiles("
1026 #include <linux/landlock.h>
1027 #include <sys/syscall.h>
1028 #include <sys/prctl.h>
1030 void my_sandbox(void)
1032 (void)prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
1033 (void)SYS_landlock_create_ruleset;
1034 (void)SYS_landlock_restrict_self;
1035 (void)LANDLOCK_CREATE_RULESET_VERSION;
1039 int main(void) { return 0; }
1041 HAVE_LINUX_LANDLOCK)
1043 if(HAVE_LINUX_LANDLOCK)
1044 set(SANDBOX_COMPILE_DEFINITION "HAVE_LINUX_LANDLOCK")
1045 set(SANDBOX_FOUND ON)
1047 # Of our three sandbox methods, only Landlock is incompatible
1048 # with -fsanitize. FreeBSD 13.2 with Capsicum was tested with
1049 # -fsanitize=address,undefined and had no issues. OpenBSD (as
1050 # of version 7.4) has minimal support for process instrumentation.
1051 # OpenBSD does not distribute the additional libraries needed
1052 # (libasan, libubsan, etc.) with GCC or Clang needed for runtime
1053 # sanitization support and instead only support
1054 # -fsanitize-minimal-runtime for minimal undefined behavior
1055 # sanitization. This minimal support is compatible with our use
1056 # of the Pledge sandbox. So only Landlock will result in a
1057 # build that cannot compress or decompress a single file to
1059 if(CMAKE_C_FLAGS MATCHES "-fsanitize=")
1061 "CMAKE_C_FLAGS or the environment variable CFLAGS "
1062 "contains '-fsanitize=' which is incompatible "
1063 "with Landlock sandboxing. Use -DENABLE_SANDBOX=OFF "
1064 "as an argument to 'cmake' when using '-fsanitize'.")
1069 if(NOT SANDBOX_FOUND AND NOT ENABLE_SANDBOX MATCHES "^ON$|^OFF$")
1070 message(SEND_ERROR "ENABLE_SANDBOX=${ENABLE_SANDBOX} was used but "
1071 "support for the sandboxing method wasn't found.")
1076 # Put the tuklib functions under the lzma_ namespace.
1077 target_compile_definitions(liblzma PRIVATE TUKLIB_SYMBOL_PREFIX=lzma_)
1078 tuklib_cpucores(liblzma)
1079 tuklib_physmem(liblzma)
1081 # While liblzma can be built without tuklib_cpucores or tuklib_physmem
1082 # modules, the liblzma API functions lzma_cputhreads() and lzma_physmem()
1083 # will then be useless (which isn't too bad but still unfortunate). Since
1084 # I expect the CMake-based builds to be only used on systems that are
1085 # supported by these tuklib modules, problems with these tuklib modules
1086 # are considered a hard error for now. This hopefully helps to catch bugs
1087 # in the CMake versions of the tuklib checks.
1088 if(NOT TUKLIB_CPUCORES_FOUND OR NOT TUKLIB_PHYSMEM_FOUND)
1089 # Use SEND_ERROR instead of FATAL_ERROR. If someone reports a bug,
1090 # seeing the results of the remaining checks can be useful too.
1092 "tuklib_cpucores() or tuklib_physmem() failed. "
1093 "Unless you really are building for a system where these "
1094 "modules are not supported (unlikely), this is a bug in the "
1095 "included cmake/tuklib_*.cmake files that should be fixed. "
1096 "To build anyway, edit this CMakeLists.txt to ignore this error.")
1099 # Check for __attribute__((__constructor__)) support.
1100 # This needs -Werror because some compilers just warn
1101 # about this being unsupported.
1102 cmake_push_check_state()
1103 set(CMAKE_REQUIRED_FLAGS "-Werror")
1104 check_c_source_compiles("
1105 __attribute__((__constructor__))
1106 static void my_constructor_func(void) { return; }
1107 int main(void) { return 0; }
1109 HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
1110 cmake_pop_check_state()
1111 tuklib_add_definition_if(liblzma HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
1113 # The Win95 threading lacks a thread-safe one-time initialization function.
1114 # The one-time initialization is needed for crc32_small.c and crc64_small.c
1115 # create the CRC tables. So if small mode is enabled, the threading mode is
1116 # win95, and the compiler does not support attribute constructor, then we
1117 # would end up with a multithreaded build that is thread-unsafe. As a
1118 # result this configuration is not allowed.
1119 if(USE_WIN95_THREADS AND ENABLE_SMALL AND NOT HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
1120 message(SEND_ERROR "Threading method win95 and ENABLE_SMALL "
1121 "cannot be used at the same time with a compiler "
1122 "that doesn't support "
1123 "__attribute__((__constructor__))")
1128 check_include_file(cpuid.h HAVE_CPUID_H)
1129 tuklib_add_definition_if(liblzma HAVE_CPUID_H)
1132 check_include_file(immintrin.h HAVE_IMMINTRIN_H)
1133 if(HAVE_IMMINTRIN_H)
1134 target_compile_definitions(liblzma PRIVATE HAVE_IMMINTRIN_H)
1137 check_c_source_compiles("
1138 #include <immintrin.h>
1142 _mm_movemask_epi8(x);
1146 HAVE__MM_MOVEMASK_EPI8)
1147 tuklib_add_definition_if(liblzma HAVE__MM_MOVEMASK_EPI8)
1150 option(ALLOW_CLMUL_CRC "Allow carryless multiplication for CRC \
1151 calculation if supported by the system" ON)
1154 check_c_source_compiles("
1155 #include <immintrin.h>
1156 #if defined(__e2k__) && __iset__ < 6
1159 #if (defined(__GNUC__) || defined(__clang__)) \
1160 && !defined(__EDG__)
1161 __attribute__((__target__(\"ssse3,sse4.1,pclmul\")))
1163 __m128i my_clmul(__m128i a)
1165 const __m128i b = _mm_set_epi64x(1, 2);
1166 return _mm_clmulepi64_si128(a, b, 0);
1168 int main(void) { return 0; }
1171 tuklib_add_definition_if(liblzma HAVE_USABLE_CLMUL)
1175 # ARM64 C Language Extensions define CRC32 functions in arm_acle.h.
1176 # These are supported by at least GCC and Clang which both need
1177 # __attribute__((__target__("+crc"))), unless the needed compiler flags
1178 # are used to support the CRC instruction.
1179 option(ALLOW_ARM64_CRC32 "Allow ARM64 CRC32 instruction if supported by \
1182 if(ALLOW_ARM64_CRC32)
1183 check_c_source_compiles("
1187 #include <arm_acle.h>
1190 #if (defined(__GNUC__) || defined(__clang__)) && !defined(__EDG__)
1191 __attribute__((__target__(\"+crc\")))
1193 uint32_t my_crc(uint32_t a, uint64_t b)
1195 return __crc32d(a, b);
1197 int main(void) { return 0; }
1201 if(HAVE_ARM64_CRC32)
1202 target_compile_definitions(liblzma PRIVATE HAVE_ARM64_CRC32)
1204 # Check for ARM64 CRC32 instruction runtime detection.
1205 # getauxval() is supported on Linux.
1206 check_symbol_exists(getauxval sys/auxv.h HAVE_GETAUXVAL)
1207 tuklib_add_definition_if(liblzma HAVE_GETAUXVAL)
1209 # elf_aux_info() is supported on FreeBSD.
1210 check_symbol_exists(elf_aux_info sys/auxv.h HAVE_ELF_AUX_INFO)
1211 tuklib_add_definition_if(liblzma HAVE_ELF_AUX_INFO)
1213 # sysctlbyname("hw.optional.armv8_crc32", ...) is supported on Darwin
1214 # (macOS, iOS, etc.). Note that sysctlbyname() is supported on FreeBSD,
1215 # NetBSD, and possibly others too but the string is specific to
1216 # Apple OSes. The C code is responsible for checking
1217 # defined(__APPLE__) before using
1218 # sysctlbyname("hw.optional.armv8_crc32", ...).
1219 check_symbol_exists(sysctlbyname sys/sysctl.h HAVE_SYSCTLBYNAME)
1220 tuklib_add_definition_if(liblzma HAVE_SYSCTLBYNAME)
1225 # Symbol visibility support:
1227 # The C_VISIBILITY_PRESET property takes care of adding the compiler
1228 # option -fvisibility=hidden (or equivalent) if and only if it is supported.
1230 # HAVE_VISIBILITY should always be defined to 0 or 1. It tells liblzma
1231 # if __attribute__((__visibility__("default")))
1232 # and __attribute__((__visibility__("hidden"))) are supported.
1233 # Those are useful only when the compiler supports -fvisibility=hidden
1234 # or such option so HAVE_VISIBILITY should be 1 only when both option and
1235 # the attribute support are present. HAVE_VISIBILITY is ignored on Windows
1236 # and Cygwin by the liblzma C code; __declspec(dllexport) is used instead.
1238 # CMake's GenerateExportHeader module is too fancy since liblzma already
1239 # has the necessary macros. Instead, check CMake's internal variable
1240 # CMAKE_C_COMPILE_OPTIONS_VISIBILITY (it's the C-specific variant of
1241 # CMAKE_<LANG>_COMPILE_OPTIONS_VISIBILITY) which contains the compiler
1242 # command line option for visibility support. It's empty or unset when
1243 # visibility isn't supported. (It was added to CMake 2.8.12 in the commit
1244 # 0e9f4bc00c6b26f254e74063e4026ac33b786513 in 2013.) This way we don't
1245 # set HAVE_VISIBILITY to 1 when visibility isn't actually supported.
1246 if(BUILD_SHARED_LIBS AND CMAKE_C_COMPILE_OPTIONS_VISIBILITY)
1247 set_target_properties(liblzma PROPERTIES C_VISIBILITY_PRESET hidden)
1248 target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=1)
1250 target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=0)
1254 if(BUILD_SHARED_LIBS)
1255 # Add the Windows resource file for liblzma.dll.
1256 target_sources(liblzma PRIVATE src/liblzma/liblzma_w32res.rc)
1258 set_target_properties(liblzma PROPERTIES
1259 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1262 # Export the public API symbols with __declspec(dllexport).
1263 target_compile_definitions(liblzma PRIVATE DLL_EXPORT)
1266 # Create a DEF file. The linker puts the ordinal numbers there
1267 # too so the output from the linker isn't our final file.
1268 target_link_options(liblzma PRIVATE
1269 "-Wl,--output-def,liblzma.def.in")
1271 # Remove the ordinal numbers from the DEF file so that
1272 # no one will create an import library that links by ordinal
1273 # instead of by name. We don't maintain a DEF file so the
1274 # ordinal numbers aren't stable.
1275 add_custom_command(TARGET liblzma POST_BUILD
1276 COMMAND "${CMAKE_COMMAND}"
1277 -DINPUT_FILE=liblzma.def.in
1278 -DOUTPUT_FILE=liblzma.def
1280 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/remove-ordinals.cmake"
1281 BYPRODUCTS "liblzma.def"
1285 # Disable __declspec(dllimport) when linking against static liblzma.
1286 target_compile_definitions(liblzma INTERFACE LZMA_API_STATIC)
1288 elseif(BUILD_SHARED_LIBS AND SYMBOL_VERSIONING STREQUAL "linux")
1289 # Note that adding link options doesn't affect static builds
1290 # but HAVE_SYMBOL_VERSIONS_LINUX must not be used with static builds
1291 # because it would put symbol versions into the static library which
1292 # can cause problems. It's clearer if all symver related things are
1293 # omitted when not building a shared library.
1295 # NOTE: Set it explicitly to 1 to make it clear that versioning is
1296 # done unconditionally in the C files.
1297 target_compile_definitions(liblzma PRIVATE HAVE_SYMBOL_VERSIONS_LINUX=1)
1298 target_link_options(liblzma PRIVATE
1299 "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
1301 set_target_properties(liblzma PROPERTIES
1302 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
1304 elseif(BUILD_SHARED_LIBS AND SYMBOL_VERSIONING STREQUAL "generic")
1305 target_link_options(liblzma PRIVATE
1306 "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
1308 set_target_properties(liblzma PROPERTIES
1309 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
1313 set_target_properties(liblzma PROPERTIES
1314 # At least for now the package versioning matches the rules used for
1315 # shared library versioning (excluding development releases) so it is
1316 # fine to use the package version here.
1317 SOVERSION "${xz_VERSION_MAJOR}"
1318 VERSION "${xz_VERSION}"
1320 # The name liblzma a mess because in many places "lib" is just a prefix
1321 # and not part of the actual name. (Don't name a new library this way!)
1322 # Cygwin uses "cyg", MSYS2 uses "msys-", and some platforms use no prefix.
1323 # However, we want to avoid lzma.dll on Windows as that would conflict
1324 # with LZMA SDK. liblzma has been liblzma.dll on Windows since the
1325 # beginning so try to stick with it.
1327 # Up to XZ Utils 5.6.2 we set PREFIX and IMPORT_PREFIX properties to ""
1328 # while keeping the default "liblzma" OUTPUT_NAME that was derived from
1329 # the target name. But this broke naming on Cygwin and MSYS2.
1331 # Setting OUTPUT_NAME without the "lib" prefix means that CMake will add
1332 # the platform-specific prefix as needed. So on most systems CMake will
1333 # add "lib" but on Cygwin and MSYS2 the naming will be correct too.
1335 # On Windows, CMake uses the "lib" prefix with MinGW-w64 but not with
1336 # other toolchains. Those those need to be handled specially to get
1337 # the DLL file named liblzma.dll instead of lzma.dll.
1341 if(WIN32 AND NOT MINGW)
1342 # Up to XZ Utils 5.6.2 and building with MSVC, we produced liblzma.dll
1343 # and liblzma.lib. The downside of liblzma.lib is that it's not
1344 # compatible with pkgconf usage. liblzma.pc contains "-llzma" which
1345 # "pkgconf --msvc-syntax --libs liblzma" converts to "lzma.lib".
1346 # So as a compromise, we can keep the liblzma.dll name but the import
1347 # library and static liblzma need to be named lzma.lib so that pkgconf
1348 # can be used with MSVC. (MinGW-w64 finds both names with "-llzma".)
1349 set_target_properties(liblzma PROPERTIES RUNTIME_OUTPUT_NAME "liblzma")
1352 # Create liblzma-config-version.cmake.
1354 # FIXME: SameMajorVersion is correct for stable releases but it is wrong
1355 # for development releases where each release may have incompatible changes.
1356 include(CMakePackageConfigHelpers)
1357 write_basic_package_version_file(
1358 "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
1359 VERSION "${xz_VERSION}"
1360 COMPATIBILITY SameMajorVersion)
1362 # Create liblzma-config.cmake. We use this spelling instead of
1363 # liblzmaConfig.cmake to make find_package work in case insensitive
1364 # manner even with case sensitive file systems. This gives more consistent
1365 # behavior between operating systems. This optionally includes a dependency
1366 # on a threading library, so the contents are created in two separate parts.
1367 # The "second half" is always needed, so create it first.
1368 set(LZMA_CONFIG_CONTENTS
1369 "include(\"\${CMAKE_CURRENT_LIST_DIR}/liblzma-targets.cmake\")
1371 if(NOT TARGET LibLZMA::LibLZMA)
1372 # Be compatible with the spelling used by the FindLibLZMA module. This
1373 # doesn't use ALIAS because it would make CMake resolve LibLZMA::LibLZMA
1374 # to liblzma::liblzma instead of keeping the original spelling. Keeping
1375 # the original spelling is important for good FindLibLZMA compatibility.
1376 add_library(LibLZMA::LibLZMA INTERFACE IMPORTED)
1377 set_target_properties(LibLZMA::LibLZMA PROPERTIES
1378 INTERFACE_LINK_LIBRARIES liblzma::liblzma)
1382 if(USE_POSIX_THREADS)
1383 set(LZMA_CONFIG_CONTENTS
1384 "include(CMakeFindDependencyMacro)
1385 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
1386 find_dependency(Threads)
1388 ${LZMA_CONFIG_CONTENTS}
1392 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
1393 "${LZMA_CONFIG_CONTENTS}")
1395 # Create liblzma.pc.
1396 set(prefix "${CMAKE_INSTALL_PREFIX}")
1397 set(exec_prefix "${CMAKE_INSTALL_PREFIX}")
1398 set(libdir "${CMAKE_INSTALL_FULL_LIBDIR}")
1399 set(includedir "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
1400 set(PTHREAD_CFLAGS "${CMAKE_THREAD_LIBS_INIT}")
1401 configure_file(src/liblzma/liblzma.pc.in liblzma.pc
1405 # Install the library binary. The INCLUDES specifies the include path that
1406 # is exported for other projects to use but it doesn't install any files.
1407 install(TARGETS liblzma EXPORT liblzmaTargets
1408 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1409 COMPONENT liblzma_Runtime
1410 LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
1411 COMPONENT liblzma_Runtime
1412 NAMELINK_COMPONENT liblzma_Development
1413 ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
1414 COMPONENT liblzma_Development
1415 INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
1417 # Install the liblzma API headers. These use a subdirectory so
1418 # this has to be done as a separate step.
1419 install(DIRECTORY src/liblzma/api/
1420 COMPONENT liblzma_Development
1421 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
1422 FILES_MATCHING PATTERN "*.h")
1424 # Install the CMake files that other packages can use to find liblzma.
1425 set(liblzma_INSTALL_CMAKEDIR
1426 "${CMAKE_INSTALL_LIBDIR}/cmake/liblzma"
1427 CACHE STRING "Path to liblzma's .cmake files")
1429 install(EXPORT liblzmaTargets
1431 FILE liblzma-targets.cmake
1432 DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
1433 COMPONENT liblzma_Development)
1435 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
1436 "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
1437 DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
1438 COMPONENT liblzma_Development)
1440 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblzma.pc"
1441 DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig"
1442 COMPONENT liblzma_Development)
1445 #############################################################################
1446 # Helper functions for installing files
1447 #############################################################################
1449 # For each non-empty element in the list LINK_NAMES, creates symbolic links
1450 # ${LINK_NAME}${LINK_SUFFIX} -> ${TARGET_NAME} in the directory ${DIR}.
1451 # The target file should exist because on Cygwin and MSYS2 symlink creation
1452 # can fail under certain conditions if the target doesn't exist.
1453 function(my_install_symlinks COMPONENT DIR TARGET_NAME LINK_SUFFIX LINK_NAMES)
1454 install(CODE "set(D \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${DIR}\")
1455 foreach(L ${LINK_NAMES})
1456 file(CREATE_LINK \"${TARGET_NAME}\"
1457 \"\${D}/\${L}${LINK_SUFFIX}\"
1460 COMPONENT "${COMPONENT}")
1463 # Installs a man page file of a given language ("" for the untranslated file)
1464 # and optionally its alternative names as symlinks. This is a helper function
1465 # for my_install_man() below.
1466 function(my_install_man_lang COMPONENT SRC_FILE MAN_LANG LINK_NAMES)
1467 # Get the man page section from the filename suffix.
1468 string(REGEX REPLACE "^.*\.([^/.]+)$" "\\1" MAN_SECTION "${SRC_FILE}")
1470 # A few man pages might be missing from translations.
1471 # Don't attempt to install them or create the related symlinks.
1472 if(NOT MAN_LANG STREQUAL "" AND NOT EXISTS "${SRC_FILE}")
1476 # Installing the file must be done before creating the symlinks
1477 # due to Cygwin and MSYS2.
1478 install(FILES "${SRC_FILE}"
1479 DESTINATION "${CMAKE_INSTALL_MANDIR}/${MAN_LANG}/man${MAN_SECTION}"
1480 COMPONENT "${COMPONENT}")
1482 # Get the basename of the file to be used as the symlink target.
1483 get_filename_component(BASENAME "${SRC_FILE}" NAME)
1485 # LINK_NAMES don't contain the man page filename suffix (like ".1")
1486 # so it needs to be told to my_install_symlinks.
1487 my_install_symlinks("${COMPONENT}"
1488 "${CMAKE_INSTALL_MANDIR}/${MAN_LANG}/man${MAN_SECTION}"
1489 "${BASENAME}" ".${MAN_SECTION}" "${LINK_NAMES}")
1492 # Installs a man page file and optionally its alternative names as symlinks.
1493 # Does the same for translations if ENABLE_NLS.
1494 function(my_install_man COMPONENT SRC_FILE LINK_NAMES)
1495 my_install_man_lang("${COMPONENT}" "${SRC_FILE}" "" "${LINK_NAMES}")
1498 # Find the translated versions of this man page.
1499 get_filename_component(BASENAME "${SRC_FILE}" NAME)
1500 file(GLOB MAN_FILES "po4a/man/*/${BASENAME}")
1502 foreach(F ${MAN_FILES})
1503 get_filename_component(MAN_LANG "${F}" DIRECTORY)
1504 get_filename_component(MAN_LANG "${MAN_LANG}" NAME)
1505 my_install_man_lang("${COMPONENT}" "${F}" "${MAN_LANG}"
1512 #############################################################################
1513 # libgnu (getopt_long)
1514 #############################################################################
1516 # This mirrors how the Autotools build system handles the getopt_long
1517 # replacement, calling the object library libgnu since the replacement
1518 # version comes from Gnulib.
1519 add_library(libgnu OBJECT)
1521 # CMake requires that even an object library must have at least once source
1522 # file. So we give it a header file that results in no output files.
1524 # NOTE: Using a file outside the lib directory makes it possible to
1525 # delete lib/*.h and lib/*.c and still keep the build working if
1526 # getopt_long replacement isn't needed. It's convenient if one wishes
1527 # to be certain that no GNU LGPL code gets included in the binaries.
1528 target_sources(libgnu PRIVATE src/common/sysdefs.h)
1530 # The Ninja Generator requires setting the linker language since it cannot
1531 # guess the programming language of just a header file. Setting this
1532 # property avoids needing an empty .c file or an non-empty unnecessary .c
1534 set_target_properties(libgnu PROPERTIES LINKER_LANGUAGE C)
1536 # Create /lib directory in the build directory and add it to the include path.
1537 file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib")
1538 target_include_directories(libgnu PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/lib")
1540 # Include /lib from the source directory. It does no harm even if none of
1541 # the Gnulib replacements are used.
1542 target_include_directories(libgnu PUBLIC lib)
1544 # The command line tools need getopt_long in order to parse arguments. If
1545 # the system does not have a getopt_long implementation we can use the one
1546 # from Gnulib instead.
1547 check_symbol_exists(getopt_long getopt.h HAVE_GETOPT_LONG)
1549 if(NOT HAVE_GETOPT_LONG)
1550 # Set the __GETOPT_PREFIX definition to "rpl_" (replacement) to avoid
1551 # name conflicts with libc symbols. The same prefix is set if using
1552 # the Autotools build (m4/getopt.m4).
1553 target_compile_definitions(libgnu PUBLIC "__GETOPT_PREFIX=rpl_")
1555 # Create a custom copy command to copy the getopt header to the build
1556 # directory and re-copy it if it is updated. (Gnulib does it this way
1557 # because it allows choosing which .in.h files to actually use in the
1558 # build. We need just getopt.h so this is a bit overcomplicated for
1559 # a single header file only.)
1560 add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/lib/getopt.h"
1561 COMMAND "${CMAKE_COMMAND}" -E copy
1562 "${CMAKE_CURRENT_SOURCE_DIR}/lib/getopt.in.h"
1563 "${CMAKE_CURRENT_BINARY_DIR}/lib/getopt.h"
1564 MAIN_DEPENDENCY "${CMAKE_CURRENT_SOURCE_DIR}/lib/getopt.in.h"
1567 target_sources(libgnu PRIVATE
1574 lib/getopt-pfx-core.h
1575 lib/getopt-pfx-ext.h
1576 "${CMAKE_CURRENT_BINARY_DIR}/lib/getopt.h"
1581 #############################################################################
1583 #############################################################################
1585 if(HAVE_DECODERS AND (NOT MSVC OR MSVC_VERSION GREATER_EQUAL 1900))
1586 foreach(XZDEC xzdec lzmadec)
1587 add_executable("${XZDEC}"
1588 src/common/sysdefs.h
1589 src/common/tuklib_common.h
1590 src/common/tuklib_config.h
1591 src/common/tuklib_exit.c
1592 src/common/tuklib_exit.h
1593 src/common/tuklib_gettext.h
1594 src/common/tuklib_progname.c
1595 src/common/tuklib_progname.h
1599 target_include_directories("${XZDEC}" PRIVATE
1604 target_link_libraries("${XZDEC}" PRIVATE liblzma libgnu)
1607 # Add the Windows resource file for xzdec.exe or lzmadec.exe.
1608 target_sources("${XZDEC}" PRIVATE src/xzdec/xzdec_w32res.rc)
1609 set_target_properties("${XZDEC}" PROPERTIES
1610 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1614 if(SANDBOX_COMPILE_DEFINITION)
1615 target_compile_definitions("${XZDEC}" PRIVATE
1616 "${SANDBOX_COMPILE_DEFINITION}")
1619 tuklib_progname("${XZDEC}")
1621 install(TARGETS "${XZDEC}"
1622 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1623 COMPONENT "${XZDEC}_Runtime")
1626 # This is the only build-time difference with lzmadec.
1627 target_compile_definitions(lzmadec PRIVATE "LZMADEC")
1630 # NOTE: This puts the lzmadec.1 symlinks into xzdec_Documentation.
1631 # This isn't great but doing them separately with translated
1632 # man pages would require extra code. So this has to suffice for now.
1633 my_install_man(xzdec_Documentation src/xzdec/xzdec.1 lzmadec)
1638 #############################################################################
1640 #############################################################################
1642 if(HAVE_DECODERS AND (NOT MSVC OR MSVC_VERSION GREATER_EQUAL 1900))
1643 add_executable(lzmainfo
1644 src/common/sysdefs.h
1645 src/common/tuklib_common.h
1646 src/common/tuklib_config.h
1647 src/common/tuklib_exit.c
1648 src/common/tuklib_exit.h
1649 src/common/tuklib_gettext.h
1650 src/common/tuklib_progname.c
1651 src/common/tuklib_progname.h
1652 src/lzmainfo/lzmainfo.c
1655 target_include_directories(lzmainfo PRIVATE
1660 target_link_libraries(lzmainfo PRIVATE liblzma libgnu)
1663 # Add the Windows resource file for lzmainfo.exe.
1664 target_sources(lzmainfo PRIVATE src/lzmainfo/lzmainfo_w32res.rc)
1665 set_target_properties(lzmainfo PROPERTIES
1666 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1670 tuklib_progname(lzmainfo)
1672 # NOTE: The translations are in the "xz" domain and the .mo files are
1673 # installed as part of the "xz" target.
1675 target_link_libraries(lzmainfo PRIVATE Intl::Intl)
1677 target_compile_definitions(lzmainfo PRIVATE
1679 PACKAGE="${TRANSLATION_DOMAIN}"
1680 LOCALEDIR="${LOCALEDIR_DEFINITION}"
1684 install(TARGETS lzmainfo
1685 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1686 COMPONENT lzmainfo_Runtime)
1689 my_install_man(lzmainfo_Documentation src/lzmainfo/lzmainfo.1 "")
1694 #############################################################################
1696 #############################################################################
1698 if(NOT MSVC OR MSVC_VERSION GREATER_EQUAL 1900)
1700 src/common/mythread.h
1701 src/common/sysdefs.h
1702 src/common/tuklib_common.h
1703 src/common/tuklib_config.h
1704 src/common/tuklib_exit.c
1705 src/common/tuklib_exit.h
1706 src/common/tuklib_gettext.h
1707 src/common/tuklib_integer.h
1708 src/common/tuklib_mbstr.h
1709 src/common/tuklib_mbstr_fw.c
1710 src/common/tuklib_mbstr_width.c
1711 src/common/tuklib_open_stdxxx.c
1712 src/common/tuklib_open_stdxxx.h
1713 src/common/tuklib_progname.c
1714 src/common/tuklib_progname.h
1742 target_include_directories(xz PRIVATE
1748 target_sources(xz PRIVATE
1754 target_link_libraries(xz PRIVATE liblzma libgnu)
1756 target_compile_definitions(xz PRIVATE ASSUME_RAM=128)
1759 # Add the Windows resource file for xz.exe.
1760 target_sources(xz PRIVATE src/xz/xz_w32res.rc)
1761 set_target_properties(xz PROPERTIES
1762 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1766 if(SANDBOX_COMPILE_DEFINITION)
1767 target_compile_definitions(xz PRIVATE "${SANDBOX_COMPILE_DEFINITION}")
1773 check_symbol_exists(optreset getopt.h HAVE_OPTRESET)
1774 tuklib_add_definition_if(xz HAVE_OPTRESET)
1776 check_symbol_exists(posix_fadvise fcntl.h HAVE_POSIX_FADVISE)
1777 tuklib_add_definition_if(xz HAVE_POSIX_FADVISE)
1779 # How to get file time:
1780 check_struct_has_member("struct stat" st_atim.tv_nsec
1781 "sys/types.h;sys/stat.h"
1782 HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1783 if(HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1784 tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1786 check_struct_has_member("struct stat" st_atimespec.tv_nsec
1787 "sys/types.h;sys/stat.h"
1788 HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1789 if(HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1790 tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1792 check_struct_has_member("struct stat" st_atimensec
1793 "sys/types.h;sys/stat.h"
1794 HAVE_STRUCT_STAT_ST_ATIMENSEC)
1795 tuklib_add_definition_if(xz HAVE_STRUCT_STAT_ST_ATIMENSEC)
1799 # How to set file time:
1800 check_symbol_exists(futimens "sys/types.h;sys/stat.h" HAVE_FUTIMENS)
1802 tuklib_add_definitions(xz HAVE_FUTIMENS)
1804 check_symbol_exists(futimes "sys/time.h" HAVE_FUTIMES)
1806 tuklib_add_definitions(xz HAVE_FUTIMES)
1808 check_symbol_exists(futimesat "sys/time.h" HAVE_FUTIMESAT)
1810 tuklib_add_definitions(xz HAVE_FUTIMESAT)
1812 check_symbol_exists(utimes "sys/time.h" HAVE_UTIMES)
1814 tuklib_add_definitions(xz HAVE_UTIMES)
1816 check_symbol_exists(_futime "sys/utime.h" HAVE__FUTIME)
1818 tuklib_add_definitions(xz HAVE__FUTIME)
1820 check_symbol_exists(utime "utime.h" HAVE_UTIME)
1821 tuklib_add_definition_if(xz HAVE_UTIME)
1829 target_link_libraries(xz PRIVATE Intl::Intl)
1831 target_compile_definitions(xz PRIVATE
1833 PACKAGE="${TRANSLATION_DOMAIN}"
1834 LOCALEDIR="${LOCALEDIR_DEFINITION}"
1837 file(STRINGS po/LINGUAS LINGUAS)
1839 # Where to find .gmo files. If msgfmt is available, the .po files
1840 # will be converted as part of the build. Otherwise we will use
1841 # the pre-generated .gmo files which are included in XZ Utils
1842 # tarballs by Autotools.
1843 set(GMO_DIR "${CMAKE_CURRENT_SOURCE_DIR}/po")
1846 # NOTE: gettext_process_po_files' INSTALL_DESTINATION is
1847 # incompatible with how Autotools requires the .po files to
1848 # be named. CMake would require each .po file to be named with
1849 # the translation domain and thus each .po file would need its
1850 # own language-specific directory (like "po/fi/xz.po"). On top
1851 # of this, INSTALL_DESTINATION doesn't allow specifying COMPONENT
1852 # and thus the .mo files go into "Unspecified" component. So we
1853 # can use gettext_process_po_files to convert the .po files but
1854 # installation needs to be done with our own code.
1856 # Also, the .gmo files will go to root of the build directory
1857 # instead of neatly into a subdirectory. This is hardcoded in
1858 # CMake's FindGettext.cmake.
1859 foreach(LANG IN LISTS LINGUAS)
1860 gettext_process_po_files("${LANG}" ALL
1861 PO_FILES "${CMAKE_CURRENT_SOURCE_DIR}/po/${LANG}.po")
1864 set(GMO_DIR "${CMAKE_CURRENT_BINARY_DIR}")
1867 foreach(LANG IN LISTS LINGUAS)
1869 FILES "${GMO_DIR}/${LANG}.gmo"
1870 DESTINATION "${CMAKE_INSTALL_LOCALEDIR}/${LANG}/LC_MESSAGES"
1871 RENAME "${TRANSLATION_DOMAIN}.mo"
1872 COMPONENT xz_Runtime)
1876 # This command must be before the symlink creation to keep things working
1877 # on Cygwin and MSYS2 in all cases.
1879 # - Cygwin can encode symlinks in multiple ways. This can be
1880 # controlled via the environment variable "CYGWIN". If it contains
1881 # "winsymlinks:nativestrict" then symlink creation will fail if
1882 # the link target doesn't exist. This mode isn't the default though.
1883 # See: https://cygwin.com/faq.html#faq.api.symlinks
1885 # - MSYS2 supports the same winsymlinks option in the environment
1886 # variable "MSYS" (not "MSYS2). The default in MSYS2 is to make
1887 # a copy of the file instead of any kind of symlink. Thus the link
1888 # target must exist or the creation of the "symlink" (copy) will fail.
1890 # Our installation order must be such that when a symbolic link is created
1891 # its target must already exists. There is no race condition for parallel
1892 # builds because the generated cmake_install.cmake executes serially.
1894 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1895 COMPONENT xz_Runtime)
1898 option(CREATE_XZ_SYMLINKS "Create unxz and xzcat symlinks" ON)
1899 option(CREATE_LZMA_SYMLINKS "Create lzma, unlzma, and lzcat symlinks"
1903 if(CREATE_XZ_SYMLINKS)
1904 list(APPEND XZ_LINKS "unxz" "xzcat")
1907 if(CREATE_LZMA_SYMLINKS)
1908 list(APPEND XZ_LINKS "lzma" "unlzma" "lzcat")
1911 # On Cygwin, don't add the .exe suffix to the symlinks.
1913 # FIXME? Does this make sense on MSYS & MSYS2 where "ln -s"
1914 # by default makes copies? Inside MSYS & MSYS2 it is possible
1915 # to execute files without the .exe suffix but not outside
1916 # (like in Command Prompt). Omitting the suffix matches
1917 # what configure.ac has done for many years though.
1918 my_install_symlinks(xz_Runtime "${CMAKE_INSTALL_BINDIR}"
1919 "xz${CMAKE_EXECUTABLE_SUFFIX}" "" "${XZ_LINKS}")
1921 # Install the man pages and (optionally) their symlinks
1923 my_install_man(xz_Documentation src/xz/xz.1 "${XZ_LINKS}")
1928 #############################################################################
1930 #############################################################################
1933 # NOTE: This isn't as sophisticated as in the Autotools build which
1934 # uses posix-shell.m4 but hopefully this doesn't need to be either.
1935 # CMake likely won't be used on as many (old) obscure systems as the
1936 # Autotools-based builds are.
1937 if(CMAKE_SYSTEM_NAME STREQUAL "SunOS" AND EXISTS "/usr/xpg4/bin/sh")
1938 set(POSIX_SHELL_DEFAULT "/usr/xpg4/bin/sh")
1940 set(POSIX_SHELL_DEFAULT "/bin/sh")
1943 set(POSIX_SHELL "${POSIX_SHELL_DEFAULT}" CACHE STRING
1944 "Shell to use for scripts (xzgrep and others)")
1946 # Guess the extra path to add from POSIX_SHELL. Autotools-based build
1947 # has a separate option --enable-path-for-scripts=PREFIX but this is
1948 # enough for Solaris.
1949 set(enable_path_for_scripts)
1950 get_filename_component(POSIX_SHELL_DIR "${POSIX_SHELL}" DIRECTORY)
1952 if(NOT POSIX_SHELL_DIR STREQUAL "/bin" AND
1953 NOT POSIX_SHELL_DIR STREQUAL "/usr/bin")
1954 set(enable_path_for_scripts "PATH=${POSIX_SHELL_DIR}:\$PATH")
1957 set(XZDIFF_LINKS xzcmp)
1958 set(XZGREP_LINKS xzegrep xzfgrep)
1962 if(CREATE_LZMA_SYMLINKS)
1963 list(APPEND XZDIFF_LINKS lzdiff lzcmp)
1964 list(APPEND XZGREP_LINKS lzgrep lzegrep lzfgrep)
1965 list(APPEND XZMORE_LINKS lzmore)
1966 list(APPEND XZLESS_LINKS lzless)
1971 foreach(S xzdiff xzgrep xzmore xzless)
1972 configure_file("src/scripts/${S}.in" "${S}"
1976 install(PROGRAMS "${CMAKE_CURRENT_BINARY_DIR}/${S}"
1977 DESTINATION "${CMAKE_INSTALL_BINDIR}"
1978 COMPONENT scripts_Runtime)
1981 # file(CHMOD ...) would need CMake 3.19 so use execute_process instead.
1982 # Using +x is fine even if umask was 077. If execute bit is set at all
1983 # then "make install" will set it for group and other access bits too.
1984 execute_process(COMMAND chmod +x xzdiff xzgrep xzmore xzless
1985 WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
1989 unset(enable_path_for_scripts)
1991 my_install_symlinks(scripts_Runtime "${CMAKE_INSTALL_BINDIR}" xzdiff ""
1994 my_install_symlinks(scripts_Runtime "${CMAKE_INSTALL_BINDIR}" xzgrep ""
1997 my_install_symlinks(scripts_Runtime "${CMAKE_INSTALL_BINDIR}" xzmore ""
2000 my_install_symlinks(scripts_Runtime "${CMAKE_INSTALL_BINDIR}" xzless ""
2003 my_install_man(scripts_Documentation src/scripts/xzdiff.1 "${XZDIFF_LINKS}")
2004 my_install_man(scripts_Documentation src/scripts/xzgrep.1 "${XZGREP_LINKS}")
2005 my_install_man(scripts_Documentation src/scripts/xzmore.1 "${XZMORE_LINKS}")
2006 my_install_man(scripts_Documentation src/scripts/xzless.1 "${XZLESS_LINKS}")
2010 #############################################################################
2012 #############################################################################
2015 option(ENABLE_DOXYGEN "Use Doxygen to generate liblzma API docs" OFF)
2018 file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/doc")
2022 COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/doxygen/update-doxygen"
2024 "${CMAKE_CURRENT_SOURCE_DIR}"
2025 "${CMAKE_CURRENT_BINARY_DIR}/doc"
2026 OUTPUT doc/api/index.html
2027 DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/doxygen/update-doxygen"
2028 "${CMAKE_CURRENT_SOURCE_DIR}/doxygen/Doxyfile"
2029 ${LIBLZMA_API_HEADERS}
2034 DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/doc/api/index.html"
2037 install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/doc/api"
2038 DESTINATION "${CMAKE_INSTALL_DOCDIR}"
2039 COMPONENT liblzma_Documentation)
2043 install(DIRECTORY doc/examples
2044 DESTINATION "${CMAKE_INSTALL_DOCDIR}"
2045 COMPONENT liblzma_Documentation)
2047 # GPLv2 applies to the scripts. If GNU getopt_long is used then
2048 # LGPLv2.1 applies to the command line tools but, using the
2049 # section 3 of LGPLv2.1, GNU getopt_long can be handled as GPLv2 too.
2050 # Thus GPLv2 should be enough here.
2051 install(FILES AUTHORS
2060 doc/lzma-file-format.txt
2061 doc/xz-file-format.txt
2062 DESTINATION "${CMAKE_INSTALL_DOCDIR}"
2063 COMPONENT Documentation)
2066 #############################################################################
2068 #############################################################################
2070 # Tests are in a separate file so that it's possible to delete the whole
2071 # "tests" directory and still have a working build, just without the tests.
2072 include(tests/tests.cmake OPTIONAL)