1 #############################################################################
3 # Very limited CMake support for building some parts of XZ Utils
5 # For now, this is intended to be useful to build static or shared liblzma
6 # on Windows with MSVC (to avoid the need to maintain Visual Studio project
7 # files). Building liblzma on a few other platforms should work too but it
8 # is somewhat experimental and not as portable as using ./configure.
10 # On some platforms this builds also xz and xzdec, but these are
11 # highly experimental and meant for testing only:
14 # Other missing things:
15 # - No xzgrep or other scripts or their symlinks
16 # - No xz tests (liblzma tests only)
18 # NOTE: Even if the code compiles without warnings, the end result may be
19 # different than via ./configure. Specifically, the list of #defines
20 # may be different (if so, probably this CMakeLists.txt got them wrong).
22 # This file provides the following installation components (if you only
23 # need liblzma, install only its components!):
25 # - liblzma_Development
26 # - xz (on some platforms only)
27 # - xzdec (on some platforms only)
29 # To find the target liblzma::liblzma from other packages, use the CONFIG
30 # option with find_package() to avoid a conflict with the FindLibLZMA module
31 # with case-insensitive file systems. For example, to require liblzma 5.2.5
32 # or a newer compatible version:
34 # find_package(liblzma 5.2.5 REQUIRED CONFIG)
35 # target_link_libraries(my_application liblzma::liblzma)
37 #############################################################################
39 # Author: Lasse Collin
41 # This file has been put into the public domain.
42 # You can do whatever you want with this file.
44 #############################################################################
46 cmake_minimum_required(VERSION 3.13...3.27 FATAL_ERROR)
48 include(CMakePushCheckState)
49 include(CheckIncludeFile)
50 include(CheckSymbolExists)
51 include(CheckStructHasMember)
52 include(CheckCSourceCompiles)
53 include(cmake/tuklib_large_file_support.cmake)
54 include(cmake/tuklib_integer.cmake)
55 include(cmake/tuklib_cpucores.cmake)
56 include(cmake/tuklib_physmem.cmake)
57 include(cmake/tuklib_progname.cmake)
58 include(cmake/tuklib_mbstr.cmake)
60 set(PACKAGE_NAME "XZ Utils")
61 set(PACKAGE_BUGREPORT "xz@tukaani.org")
62 set(PACKAGE_URL "https://xz.tukaani.org/xz-utils/")
64 # Get the package version from version.h into PACKAGE_VERSION variable.
65 file(READ src/liblzma/api/lzma/version.h PACKAGE_VERSION)
68 #define LZMA_VERSION_MAJOR ([0-9]+)\n\
70 #define LZMA_VERSION_MINOR ([0-9]+)\n\
72 #define LZMA_VERSION_PATCH ([0-9]+)\n\
74 "\\1.\\2.\\3" PACKAGE_VERSION "${PACKAGE_VERSION}")
76 # Among other things, this gives us variables xz_VERSION and xz_VERSION_MAJOR.
77 project(xz VERSION "${PACKAGE_VERSION}" LANGUAGES C)
79 # We need a compiler that supports enough C99 or newer (variable-length arrays
80 # aren't needed, those are optional in C17). Setting CMAKE_C_STANDARD here
81 # makes it the default for all targets. It doesn't affect the INTERFACE so
82 # liblzma::liblzma won't end up with INTERFACE_COMPILE_FEATURES "c_std_99"
83 # (the API headers are C89 and C++ compatible).
84 set(CMAKE_C_STANDARD 99)
85 set(CMAKE_C_STANDARD_REQUIRED ON)
87 # On Apple OSes, don't build executables as bundles:
88 set(CMAKE_MACOSX_BUNDLE OFF)
90 # windres from GNU binutils can be tricky with command line arguments
91 # that contain spaces or other funny characters. Unfortunately we need
92 # a space in PACKAGE_NAME. Using \x20 to encode the US-ASCII space seems
93 # to work in both cmd.exe and /bin/sh.
95 # However, even \x20 isn't enough in all situations, resulting in
96 # "syntax error" from windres. Using --use-temp-file prevents windres
97 # from using popen() and this seems to fix the problem.
99 # llvm-windres from Clang/LLVM 16.0.6 and older: The \x20 results
100 # in "XZx20Utils" in the compiled binary. The option --use-temp-file
101 # makes no difference.
103 # llvm-windres 17.0.0 and later: It emulates GNU windres more accurately, so
104 # the workarounds used with GNU windres must be used with llvm-windres too.
106 # CMake 3.27 doesn't have CMAKE_RC_COMPILER_ID so we rely on
107 # CMAKE_C_COMPILER_ID.
108 if((MINGW OR CYGWIN OR MSYS) AND (
109 NOT CMAKE_C_COMPILER_ID STREQUAL "Clang" OR
110 CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "17"))
111 # Use workarounds with GNU windres and llvm-windres >= 17.0.0. The \x20
112 # in PACKAGE_NAME_DEFINITION works with gcc and clang too so we don't need
113 # to worry how to pass different flags to windres and the C compiler.
114 # Keep the original PACKAGE_NAME intact for generation of liblzma.pc.
115 string(APPEND CMAKE_RC_FLAGS " --use-temp-file")
116 string(REPLACE " " "\\x20" PACKAGE_NAME_DEFINITION "${PACKAGE_NAME}")
118 # Elsewhere a space is safe. This also keeps things compatible with
119 # EBCDIC in case CMake-based build is ever done on such a system.
120 set(PACKAGE_NAME_DEFINITION "${PACKAGE_NAME}")
123 # Definitions common to all targets:
124 add_compile_definitions(
126 PACKAGE_NAME="${PACKAGE_NAME_DEFINITION}"
127 PACKAGE_BUGREPORT="${PACKAGE_BUGREPORT}"
128 PACKAGE_URL="${PACKAGE_URL}"
130 # Standard headers and types are available:
136 # Always enable CRC32 since liblzma should never build without it.
139 # Disable assert() checks when no build type has been specified. Non-empty
140 # build types like "Release" and "Debug" handle this by default.
145 ######################
146 # System definitions #
147 ######################
149 # _GNU_SOURCE and such definitions. This specific macro is special since
150 # it also adds the definitions to CMAKE_REQUIRED_DEFINITIONS.
151 tuklib_use_system_extensions(ALL)
153 # Check for large file support. It's required on some 32-bit platforms and
154 # even on 64-bit MinGW-w64 to get 64-bit off_t. This can be forced off on
155 # the CMake command line if needed: -DLARGE_FILE_SUPPORT=OFF
156 tuklib_large_file_support(ALL)
158 # This is needed by liblzma and xz.
161 # This is used for liblzma.pc generation to add -lrt if needed.
164 # Check for clock_gettime(). Do this before checking for threading so
165 # that we know there if CLOCK_MONOTONIC is available.
166 check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME)
168 if(NOT HAVE_CLOCK_GETTIME)
169 # With glibc <= 2.17 or Solaris 10 this needs librt.
170 # Add librt for the next check for HAVE_CLOCK_GETTIME. If it is
171 # found after including the library, we know that librt is required.
172 list(INSERT CMAKE_REQUIRED_LIBRARIES 0 rt)
173 check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME_LIBRT)
175 # If it was found now, add librt to all targets and keep it in
176 # CMAKE_REQUIRED_LIBRARIES for further tests too.
177 if(HAVE_CLOCK_GETTIME_LIBRT)
179 set(LIBS "-lrt") # For liblzma.pc
181 list(REMOVE_AT CMAKE_REQUIRED_LIBRARIES 0)
185 if(HAVE_CLOCK_GETTIME OR HAVE_CLOCK_GETTIME_LIBRT)
186 add_compile_definitions(HAVE_CLOCK_GETTIME)
188 # Check if CLOCK_MONOTONIC is available for clock_gettime().
189 check_symbol_exists(CLOCK_MONOTONIC time.h HAVE_CLOCK_MONOTONIC)
190 tuklib_add_definition_if(ALL HAVE_CLOCK_MONOTONIC)
193 # Options for new enough GCC or Clang on any arch or operating system:
194 if(CMAKE_C_COMPILER_ID MATCHES GNU|Clang)
195 # configure.ac has a long list but it won't be copied here:
196 add_compile_options(-Wall -Wextra)
200 #############################################################################
202 #############################################################################
204 option(BUILD_SHARED_LIBS "Build liblzma as a shared library instead of static")
207 src/common/mythread.h
209 src/common/tuklib_common.h
210 src/common/tuklib_config.h
211 src/common/tuklib_integer.h
212 src/common/tuklib_physmem.c
213 src/common/tuklib_physmem.h
214 src/liblzma/api/lzma.h
215 src/liblzma/api/lzma/base.h
216 src/liblzma/api/lzma/bcj.h
217 src/liblzma/api/lzma/block.h
218 src/liblzma/api/lzma/check.h
219 src/liblzma/api/lzma/container.h
220 src/liblzma/api/lzma/delta.h
221 src/liblzma/api/lzma/filter.h
222 src/liblzma/api/lzma/hardware.h
223 src/liblzma/api/lzma/index.h
224 src/liblzma/api/lzma/index_hash.h
225 src/liblzma/api/lzma/lzma12.h
226 src/liblzma/api/lzma/stream_flags.h
227 src/liblzma/api/lzma/version.h
228 src/liblzma/api/lzma/vli.h
229 src/liblzma/check/check.c
230 src/liblzma/check/check.h
231 src/liblzma/check/crc_common.h
232 src/liblzma/check/crc_x86_clmul.h
233 src/liblzma/check/crc32_arm64.h
234 src/liblzma/common/block_util.c
235 src/liblzma/common/common.c
236 src/liblzma/common/common.h
237 src/liblzma/common/easy_preset.c
238 src/liblzma/common/easy_preset.h
239 src/liblzma/common/filter_common.c
240 src/liblzma/common/filter_common.h
241 src/liblzma/common/hardware_physmem.c
242 src/liblzma/common/index.c
243 src/liblzma/common/index.h
244 src/liblzma/common/memcmplen.h
245 src/liblzma/common/stream_flags_common.c
246 src/liblzma/common/stream_flags_common.h
247 src/liblzma/common/string_conversion.c
248 src/liblzma/common/vli_size.c
251 target_include_directories(liblzma PRIVATE
256 src/liblzma/rangecoder
264 ######################
265 # Size optimizations #
266 ######################
268 option(ENABLE_SMALL "Reduce code size at expense of speed. \
269 This may be useful together with CMAKE_BUILD_TYPE=MinSizeRel.")
272 add_compile_definitions(HAVE_SMALL)
280 set(ADDITIONAL_SUPPORTED_CHECKS crc64 sha256)
282 set(ADDITIONAL_CHECK_TYPES "${ADDITIONAL_SUPPORTED_CHECKS}" CACHE STRING
283 "Additional check types to support (crc32 is always built)")
285 foreach(CHECK IN LISTS ADDITIONAL_CHECK_TYPES)
286 if(NOT CHECK IN_LIST ADDITIONAL_SUPPORTED_CHECKS)
287 message(FATAL_ERROR "'${CHECK}' is not a supported check type")
292 target_sources(liblzma PRIVATE src/liblzma/check/crc32_small.c)
294 target_sources(liblzma PRIVATE
295 src/liblzma/check/crc32_fast.c
296 src/liblzma/check/crc32_table.c
297 src/liblzma/check/crc32_table_be.h
298 src/liblzma/check/crc32_table_le.h
302 if("crc64" IN_LIST ADDITIONAL_CHECK_TYPES)
303 add_compile_definitions("HAVE_CHECK_CRC64")
306 target_sources(liblzma PRIVATE src/liblzma/check/crc64_small.c)
308 target_sources(liblzma PRIVATE
309 src/liblzma/check/crc64_fast.c
310 src/liblzma/check/crc64_table.c
311 src/liblzma/check/crc64_table_be.h
312 src/liblzma/check/crc64_table_le.h
317 if("sha256" IN_LIST ADDITIONAL_CHECK_TYPES)
318 add_compile_definitions("HAVE_CHECK_SHA256")
319 target_sources(liblzma PRIVATE src/liblzma/check/sha256.c)
327 set(SUPPORTED_MATCH_FINDERS hc3 hc4 bt2 bt3 bt4)
329 set(MATCH_FINDERS "${SUPPORTED_MATCH_FINDERS}" CACHE STRING
330 "Match finders to support (at least one is required for LZMA1 or LZMA2)")
332 foreach(MF IN LISTS MATCH_FINDERS)
333 if(MF IN_LIST SUPPORTED_MATCH_FINDERS)
334 string(TOUPPER "${MF}" MF_UPPER)
335 add_compile_definitions("HAVE_MF_${MF_UPPER}")
337 message(FATAL_ERROR "'${MF}' is not a supported match finder")
346 # Supported threading methods:
347 # ON - autodetect the best threading method. The autodetection will
348 # prefer Windows threading (win95 or vista) over posix if both are
349 # available. vista threads will be used over win95 unless it is a
351 # OFF - Disable threading.
352 # posix - Use posix threading (pthreads), or throw an error if not available.
353 # win95 - Use Windows win95 threading, or throw an error if not available.
354 # vista - Use Windows vista threading, or throw an error if not available.
355 set(SUPPORTED_THREADING_METHODS ON OFF posix win95 vista)
357 set(ENABLE_THREADS ON CACHE STRING
358 "Threading method: Set to 'ON' to autodetect, 'OFF' to disable threading.")
360 # Create dropdown in CMake GUI since only 1 threading method is possible
361 # to select in a build.
362 set_property(CACHE ENABLE_THREADS
363 PROPERTY STRINGS "${SUPPORTED_THREADING_METHODS}")
365 # This is a flag variable set when win95 threads are used. We must ensure
366 # the combination of enable_small and win95 threads is not used without a
367 # compiler supporting attribute __constructor__.
368 set(USE_WIN95_THREADS OFF)
370 # This is a flag variable set when posix threads (pthreads) are used.
371 # It's needed when creating liblzma-config.cmake where dependency on
372 # Threads::Threads is only needed with pthreads.
373 set(USE_POSIX_THREADS OFF)
375 if(NOT ENABLE_THREADS IN_LIST SUPPORTED_THREADING_METHODS)
376 message(FATAL_ERROR "'${ENABLE_THREADS}' is not a supported "
381 # Also set THREADS_PREFER_PTHREAD_FLAG since the flag has no effect
382 # for Windows threading.
383 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
384 find_package(Threads REQUIRED)
386 # If both Windows and posix threading are available, prefer Windows.
387 # Note that on Cygwin CMAKE_USE_WIN32_THREADS_INIT is false.
388 if(CMAKE_USE_WIN32_THREADS_INIT AND NOT ENABLE_THREADS STREQUAL "posix")
389 if(ENABLE_THREADS STREQUAL "win95"
390 OR (ENABLE_THREADS STREQUAL "ON"
391 AND CMAKE_SIZEOF_VOID_P EQUAL 4))
392 # Use Windows 95 (and thus XP) compatible threads.
393 # This avoids use of features that were added in
394 # Windows Vista. This is used for 32-bit x86 builds for
395 # compatibility reasons since it makes no measurable difference
396 # in performance compared to Vista threads.
397 set(USE_WIN95_THREADS ON)
398 add_compile_definitions(MYTHREAD_WIN95)
400 add_compile_definitions(MYTHREAD_VISTA)
402 elseif(CMAKE_USE_PTHREADS_INIT)
403 if(ENABLE_THREADS STREQUAL "posix" OR ENABLE_THREADS STREQUAL "ON")
404 # The threading library only needs to be explicitly linked
405 # for posix threads, so this is needed for creating
406 # liblzma-config.cmake later.
407 set(USE_POSIX_THREADS ON)
409 target_link_libraries(liblzma Threads::Threads)
410 add_compile_definitions(MYTHREAD_POSIX)
412 # Check if pthread_condattr_setclock() exists to
413 # use CLOCK_MONOTONIC.
414 if(HAVE_CLOCK_MONOTONIC)
415 list(INSERT CMAKE_REQUIRED_LIBRARIES 0
416 "${CMAKE_THREAD_LIBS_INIT}")
417 check_symbol_exists(pthread_condattr_setclock pthread.h
418 HAVE_PTHREAD_CONDATTR_SETCLOCK)
419 tuklib_add_definition_if(ALL HAVE_PTHREAD_CONDATTR_SETCLOCK)
423 "Windows threading method was requested but a compatible "
424 "library could not be found")
427 message(SEND_ERROR "No supported threading library found")
430 target_sources(liblzma PRIVATE
431 src/common/tuklib_cpucores.c
432 src/common/tuklib_cpucores.h
433 src/liblzma/common/hardware_cputhreads.c
434 src/liblzma/common/outqueue.c
435 src/liblzma/common/outqueue.h
455 # The SUPPORTED_FILTERS are shared between Encoders and Decoders
456 # since only lzip does not appear in both lists. lzip is a special
457 # case anyway, so it is handled separately in the Decoders section.
458 set(SUPPORTED_FILTERS
465 set(ENCODERS "${SUPPORTED_FILTERS}" CACHE STRING "Encoders to support")
467 # If LZMA2 is enabled, then LZMA1 must also be enabled.
468 if(NOT "lzma1" IN_LIST ENCODERS AND "lzma2" IN_LIST ENCODERS)
469 message(FATAL_ERROR "LZMA2 encoder requires that LZMA1 is also enabled")
472 # If LZMA1 is enabled, then at least one match finder must be enabled.
473 if(MATCH_FINDERS STREQUAL "" AND "lzma1" IN_LIST ENCODERS)
474 message(FATAL_ERROR "At least 1 match finder is required for an "
478 set(HAVE_DELTA_CODER OFF)
479 set(SIMPLE_ENCODERS OFF)
480 set(HAVE_ENCODERS OFF)
482 foreach(ENCODER IN LISTS ENCODERS)
483 if(ENCODER IN_LIST SUPPORTED_FILTERS)
484 set(HAVE_ENCODERS ON)
486 if(NOT SIMPLE_ENCODERS AND ENCODER IN_LIST SIMPLE_FILTERS)
487 set(SIMPLE_ENCODERS ON)
490 string(TOUPPER "${ENCODER}" ENCODER_UPPER)
491 add_compile_definitions("HAVE_ENCODER_${ENCODER_UPPER}")
493 message(FATAL_ERROR "'${ENCODER}' is not a supported encoder")
498 add_compile_definitions(HAVE_ENCODERS)
500 target_sources(liblzma PRIVATE
501 src/liblzma/common/alone_encoder.c
502 src/liblzma/common/block_buffer_encoder.c
503 src/liblzma/common/block_buffer_encoder.h
504 src/liblzma/common/block_encoder.c
505 src/liblzma/common/block_encoder.h
506 src/liblzma/common/block_header_encoder.c
507 src/liblzma/common/easy_buffer_encoder.c
508 src/liblzma/common/easy_encoder.c
509 src/liblzma/common/easy_encoder_memusage.c
510 src/liblzma/common/filter_buffer_encoder.c
511 src/liblzma/common/filter_encoder.c
512 src/liblzma/common/filter_encoder.h
513 src/liblzma/common/filter_flags_encoder.c
514 src/liblzma/common/index_encoder.c
515 src/liblzma/common/index_encoder.h
516 src/liblzma/common/stream_buffer_encoder.c
517 src/liblzma/common/stream_encoder.c
518 src/liblzma/common/stream_flags_encoder.c
519 src/liblzma/common/vli_encoder.c
523 target_sources(liblzma PRIVATE
524 src/liblzma/common/stream_encoder_mt.c
529 target_sources(liblzma PRIVATE
530 src/liblzma/simple/simple_encoder.c
531 src/liblzma/simple/simple_encoder.h
535 if("lzma1" IN_LIST ENCODERS)
536 target_sources(liblzma PRIVATE
537 src/liblzma/lzma/lzma_encoder.c
538 src/liblzma/lzma/lzma_encoder.h
539 src/liblzma/lzma/lzma_encoder_optimum_fast.c
540 src/liblzma/lzma/lzma_encoder_optimum_normal.c
541 src/liblzma/lzma/lzma_encoder_private.h
542 src/liblzma/lzma/fastpos.h
543 src/liblzma/lz/lz_encoder.c
544 src/liblzma/lz/lz_encoder.h
545 src/liblzma/lz/lz_encoder_hash.h
546 src/liblzma/lz/lz_encoder_hash_table.h
547 src/liblzma/lz/lz_encoder_mf.c
548 src/liblzma/rangecoder/price.h
549 src/liblzma/rangecoder/price_table.c
550 src/liblzma/rangecoder/range_encoder.h
554 target_sources(liblzma PRIVATE src/liblzma/lzma/fastpos_table.c)
558 if("lzma2" IN_LIST ENCODERS)
559 target_sources(liblzma PRIVATE
560 src/liblzma/lzma/lzma2_encoder.c
561 src/liblzma/lzma/lzma2_encoder.h
565 if("delta" IN_LIST ENCODERS)
566 set(HAVE_DELTA_CODER ON)
567 target_sources(liblzma PRIVATE
568 src/liblzma/delta/delta_encoder.c
569 src/liblzma/delta/delta_encoder.h
579 set(DECODERS "${SUPPORTED_FILTERS}" CACHE STRING "Decoders to support")
581 set(SIMPLE_DECODERS OFF)
582 set(HAVE_DECODERS OFF)
584 foreach(DECODER IN LISTS DECODERS)
585 if(DECODER IN_LIST SUPPORTED_FILTERS)
586 set(HAVE_DECODERS ON)
588 if(NOT SIMPLE_DECODERS AND DECODER IN_LIST SIMPLE_FILTERS)
589 set(SIMPLE_DECODERS ON)
592 string(TOUPPER "${DECODER}" DECODER_UPPER)
593 add_compile_definitions("HAVE_DECODER_${DECODER_UPPER}")
595 message(FATAL_ERROR "'${DECODER}' is not a supported decoder")
600 add_compile_definitions(HAVE_DECODERS)
602 target_sources(liblzma PRIVATE
603 src/liblzma/common/alone_decoder.c
604 src/liblzma/common/alone_decoder.h
605 src/liblzma/common/auto_decoder.c
606 src/liblzma/common/block_buffer_decoder.c
607 src/liblzma/common/block_decoder.c
608 src/liblzma/common/block_decoder.h
609 src/liblzma/common/block_header_decoder.c
610 src/liblzma/common/easy_decoder_memusage.c
611 src/liblzma/common/file_info.c
612 src/liblzma/common/filter_buffer_decoder.c
613 src/liblzma/common/filter_decoder.c
614 src/liblzma/common/filter_decoder.h
615 src/liblzma/common/filter_flags_decoder.c
616 src/liblzma/common/index_decoder.c
617 src/liblzma/common/index_decoder.h
618 src/liblzma/common/index_hash.c
619 src/liblzma/common/stream_buffer_decoder.c
620 src/liblzma/common/stream_decoder.c
621 src/liblzma/common/stream_flags_decoder.c
622 src/liblzma/common/stream_decoder.h
623 src/liblzma/common/vli_decoder.c
627 target_sources(liblzma PRIVATE
628 src/liblzma/common/stream_decoder_mt.c
633 target_sources(liblzma PRIVATE
634 src/liblzma/simple/simple_decoder.c
635 src/liblzma/simple/simple_decoder.h
639 if("lzma1" IN_LIST DECODERS)
640 target_sources(liblzma PRIVATE
641 src/liblzma/lzma/lzma_decoder.c
642 src/liblzma/lzma/lzma_decoder.h
643 src/liblzma/rangecoder/range_decoder.h
644 src/liblzma/lz/lz_decoder.c
645 src/liblzma/lz/lz_decoder.h
649 if("lzma2" IN_LIST DECODERS)
650 target_sources(liblzma PRIVATE
651 src/liblzma/lzma/lzma2_decoder.c
652 src/liblzma/lzma/lzma2_decoder.h
656 if("delta" IN_LIST DECODERS)
657 set(HAVE_DELTA_CODER ON)
658 target_sources(liblzma PRIVATE
659 src/liblzma/delta/delta_decoder.c
660 src/liblzma/delta/delta_decoder.h
665 # Some sources must appear if the filter is configured as either
666 # an encoder or decoder.
667 if("lzma1" IN_LIST ENCODERS OR "lzma1" IN_LIST DECODERS)
668 target_sources(liblzma PRIVATE
669 src/liblzma/rangecoder/range_common.h
670 src/liblzma/lzma/lzma_encoder_presets.c
671 src/liblzma/lzma/lzma_common.h
676 target_sources(liblzma PRIVATE
677 src/liblzma/delta/delta_common.c
678 src/liblzma/delta/delta_common.h
679 src/liblzma/delta/delta_private.h
683 if(SIMPLE_ENCODERS OR SIMPLE_DECODERS)
684 target_sources(liblzma PRIVATE
685 src/liblzma/simple/simple_coder.c
686 src/liblzma/simple/simple_coder.h
687 src/liblzma/simple/simple_private.h
691 foreach(SIMPLE_CODER IN LISTS SIMPLE_FILTERS)
692 if(SIMPLE_CODER IN_LIST ENCODERS OR SIMPLE_CODER IN_LIST DECODERS)
693 target_sources(liblzma PRIVATE "src/liblzma/simple/${SIMPLE_CODER}.c")
702 option(MICROLZMA_ENCODER
703 "MicroLZMA encoder (needed by specific applications only)" ON)
705 option(MICROLZMA_DECODER
706 "MicroLZMA decoder (needed by specific applications only)" ON)
708 if(MICROLZMA_ENCODER)
709 if(NOT "lzma1" IN_LIST ENCODERS)
710 message(FATAL_ERROR "The LZMA1 encoder is required to support the "
714 target_sources(liblzma PRIVATE src/liblzma/common/microlzma_encoder.c)
717 if(MICROLZMA_DECODER)
718 if(NOT "lzma1" IN_LIST DECODERS)
719 message(FATAL_ERROR "The LZMA1 decoder is required to support the "
723 target_sources(liblzma PRIVATE src/liblzma/common/microlzma_decoder.c)
727 #############################
728 # lzip (.lz) format support #
729 #############################
731 option(LZIP_DECODER "Support lzip decoder" ON)
734 # If lzip decoder support is requested, make sure LZMA1 decoder is enabled.
735 if(NOT "lzma1" IN_LIST DECODERS)
736 message(FATAL_ERROR "The LZMA1 decoder is required to support the "
740 add_compile_definitions(HAVE_LZIP_DECODER)
742 target_sources(liblzma PRIVATE
743 src/liblzma/common/lzip_decoder.c
744 src/liblzma/common/lzip_decoder.h
753 # ON Use sandboxing if a supported method is available in the OS.
754 # OFF Disable sandboxing.
755 # capsicum Require Capsicum (FreeBSD >= 10.2) and fail if not found.
756 # pledge Require pledge(2) (OpenBSD >= 5.9) and fail if not found.
757 # landlock Require Landlock (Linux >= 5.13) and fail if not found.
758 set(SUPPORTED_SANDBOX_METHODS ON OFF capsicum pledge landlock)
760 set(ENABLE_SANDBOX ON CACHE STRING
761 "Sandboxing method to use in 'xz' and 'xzdec'")
763 set_property(CACHE ENABLE_SANDBOX
764 PROPERTY STRINGS "${SUPPORTED_SANDBOX_METHODS}")
766 if(NOT ENABLE_SANDBOX IN_LIST SUPPORTED_SANDBOX_METHODS)
767 message(FATAL_ERROR "'${ENABLE_SANDBOX}' is not a supported "
771 # When autodetecting, the search order is fixed and we must not find
772 # more than one method.
773 if(ENABLE_SANDBOX STREQUAL "OFF")
774 set(SANDBOX_FOUND ON)
776 set(SANDBOX_FOUND OFF)
779 # Since xz and xzdec can both use sandboxing, the compile definition needed
780 # to use the sandbox must be added to both targets.
781 set(SANDBOX_COMPILE_DEFINITION OFF)
783 # Sandboxing: Capsicum
784 if(NOT SANDBOX_FOUND AND ENABLE_SANDBOX MATCHES "^ON$|^capsicum$")
785 check_symbol_exists(cap_rights_limit sys/capsicum.h
786 HAVE_CAP_RIGHTS_LIMIT)
787 if(HAVE_CAP_RIGHTS_LIMIT)
788 set(SANDBOX_COMPILE_DEFINITION "HAVE_CAP_RIGHTS_LIMIT")
789 set(SANDBOX_FOUND ON)
793 # Sandboxing: pledge(2)
794 if(NOT SANDBOX_FOUND AND ENABLE_SANDBOX MATCHES "^ON$|^pledge$")
795 check_symbol_exists(pledge unistd.h HAVE_PLEDGE)
797 set(SANDBOX_COMPILE_DEFINITION "HAVE_PLEDGE")
798 set(SANDBOX_FOUND ON)
802 # Sandboxing: Landlock
803 if(NOT SANDBOX_FOUND AND ENABLE_SANDBOX MATCHES "^ON$|^landlock$")
804 check_include_file(linux/landlock.h HAVE_LINUX_LANDLOCK_H)
806 if(HAVE_LINUX_LANDLOCK_H)
807 set(SANDBOX_COMPILE_DEFINITION "HAVE_LINUX_LANDLOCK_H")
808 set(SANDBOX_FOUND ON)
810 # Of our three sandbox methods, only Landlock is incompatible
811 # with -fsanitize. FreeBSD 13.2 with Capsicum was tested with
812 # -fsanitize=address,undefined and had no issues. OpenBSD (as
813 # of version 7.4) has minimal support for process instrumentation.
814 # OpenBSD does not distribute the additional libraries needed
815 # (libasan, libubsan, etc.) with GCC or Clang needed for runtime
816 # sanitization support and instead only support
817 # -fsanitize-minimal-runtime for minimal undefined behavior
818 # sanitization. This minimal support is compatible with our use
819 # of the Pledge sandbox. So only Landlock will result in a
820 # build that cannot compress or decompress a single file to
822 if(CMAKE_C_FLAGS MATCHES "-fsanitize=")
824 "CMAKE_C_FLAGS or the environment variable CFLAGS "
825 "contains '-fsanitize=' which is incompatible "
826 "with Landlock sandboxing. Use -DENABLE_SANDBOX=OFF "
827 "as an argument to 'cmake' when using '-fsanitize'.")
832 if(NOT SANDBOX_FOUND AND NOT ENABLE_SANDBOX MATCHES "^ON$|^OFF$")
833 message(SEND_ERROR "ENABLE_SANDBOX=${ENABLE_SANDBOX} was used but "
834 "support for the sandboxing method wasn't found.")
839 # Put the tuklib functions under the lzma_ namespace.
840 target_compile_definitions(liblzma PRIVATE TUKLIB_SYMBOL_PREFIX=lzma_)
841 tuklib_cpucores(liblzma)
842 tuklib_physmem(liblzma)
844 # While liblzma can be built without tuklib_cpucores or tuklib_physmem
845 # modules, the liblzma API functions lzma_cputhreads() and lzma_physmem()
846 # will then be useless (which isn't too bad but still unfortunate). Since
847 # I expect the CMake-based builds to be only used on systems that are
848 # supported by these tuklib modules, problems with these tuklib modules
849 # are considered a hard error for now. This hopefully helps to catch bugs
850 # in the CMake versions of the tuklib checks.
851 if(NOT TUKLIB_CPUCORES_FOUND OR NOT TUKLIB_PHYSMEM_FOUND)
852 # Use SEND_ERROR instead of FATAL_ERROR. If someone reports a bug,
853 # seeing the results of the remaining checks can be useful too.
855 "tuklib_cpucores() or tuklib_physmem() failed. "
856 "Unless you really are building for a system where these "
857 "modules are not supported (unlikely), this is a bug in the "
858 "included cmake/tuklib_*.cmake files that should be fixed. "
859 "To build anyway, edit this CMakeLists.txt to ignore this error.")
862 # Check for __attribute__((__constructor__)) support.
863 # This needs -Werror because some compilers just warn
864 # about this being unsupported.
865 cmake_push_check_state()
866 set(CMAKE_REQUIRED_FLAGS "-Werror")
867 check_c_source_compiles("
868 __attribute__((__constructor__))
869 static void my_constructor_func(void) { return; }
870 int main(void) { return 0; }
872 HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
873 cmake_pop_check_state()
874 tuklib_add_definition_if(liblzma HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
876 # The Win95 threading lacks a thread-safe one-time initialization function.
877 # The one-time initialization is needed for crc32_small.c and crc64_small.c
878 # create the CRC tables. So if small mode is enabled, the threading mode is
879 # win95, and the compiler does not support attribute constructor, then we
880 # would end up with a multithreaded build that is thread-unsafe. As a
881 # result this configuration is not allowed.
882 if(USE_WIN95_THREADS AND ENABLE_SMALL AND NOT HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
883 message(SEND_ERROR "Threading method win95 and ENABLE_SMALL "
884 "cannot be used at the same time with a compiler "
885 "that doesn't support "
886 "__attribute__((__constructor__))")
890 # Check for __attribute__((__ifunc__())) support.
891 # Supported values for USE_ATTR_IFUNC:
893 # auto (default) - Detect ifunc support with a compile test.
894 # ON - Always enable ifunc.
895 # OFF - Disable ifunc usage.
896 set(USE_ATTR_IFUNC "auto" CACHE STRING "Use __attribute__((__ifunc__())).")
898 set(SUPPORTED_USE_ATTR_IFUNC auto ON OFF)
900 if(NOT USE_ATTR_IFUNC IN_LIST SUPPORTED_USE_ATTR_IFUNC)
901 message(FATAL_ERROR "'${USE_ATTR_IFUNC}' is not a supported value for"
905 # When USE_ATTR_IFUNC is 'auto', allow the use of __attribute__((__ifunc__()))
906 # if compiler support is detected and we are building for GNU/Linux (glibc)
907 # or FreeBSD. uClibc and musl don't support ifunc in their dynamic linkers
908 # but some compilers still accept the attribute when compiling for these
909 # C libraries, which results in broken binaries. That's why we need to
910 # check which libc is being used.
911 if(USE_ATTR_IFUNC STREQUAL "auto")
912 cmake_push_check_state()
913 set(CMAKE_REQUIRED_FLAGS "-Werror")
915 check_c_source_compiles("
917 * Force a compilation error when not using glibc on Linux
918 * or if we are not using FreeBSD. uClibc will define
919 * __GLIBC__ but does not support ifunc, so we must have
920 * an extra check to disable with uClibc.
922 #if defined(__linux__)
923 # include <features.h>
924 # if !defined(__GLIBC__) || defined(__UCLIBC__)
927 #elif !defined(__FreeBSD__)
931 static void func(void) { return; }
932 static void (*resolve_func(void)) (void) { return func; }
933 void func_ifunc(void)
934 __attribute__((__ifunc__(\"resolve_func\")));
935 int main(void) { return 0; }
937 * 'clang -Wall' incorrectly warns that resolve_func is
938 * unused (-Wunused-function). Correct assembly output is
939 * still produced. This problem exists at least in Clang
940 * versions 4 to 17. The following silences the bogus warning:
942 void make_clang_quiet(void);
943 void make_clang_quiet(void) { resolve_func()(); }
945 SYSTEM_SUPPORTS_IFUNC)
947 cmake_pop_check_state()
950 if(USE_ATTR_IFUNC STREQUAL "ON" OR SYSTEM_SUPPORTS_IFUNC)
951 tuklib_add_definitions(liblzma HAVE_FUNC_ATTRIBUTE_IFUNC)
953 if(CMAKE_C_FLAGS MATCHES "-fsanitize=")
955 "CMAKE_C_FLAGS or the environment variable CFLAGS "
956 "contains '-fsanitize=' which is incompatible "
957 "with ifunc. Use -DUSE_ATTR_IFUNC=OFF "
958 "as an argument to 'cmake' when using '-fsanitize'.")
963 check_include_file(cpuid.h HAVE_CPUID_H)
964 tuklib_add_definition_if(liblzma HAVE_CPUID_H)
967 check_include_file(immintrin.h HAVE_IMMINTRIN_H)
969 target_compile_definitions(liblzma PRIVATE HAVE_IMMINTRIN_H)
972 check_c_source_compiles("
973 #include <immintrin.h>
977 _mm_movemask_epi8(x);
981 HAVE__MM_MOVEMASK_EPI8)
982 tuklib_add_definition_if(liblzma HAVE__MM_MOVEMASK_EPI8)
985 option(ALLOW_CLMUL_CRC "Allow carryless multiplication for CRC \
986 calculation if supported by the system" ON)
989 check_c_source_compiles("
990 #include <immintrin.h>
991 #if defined(__e2k__) && __iset__ < 6
994 #if (defined(__GNUC__) || defined(__clang__)) \
996 __attribute__((__target__(\"ssse3,sse4.1,pclmul\")))
998 __m128i my_clmul(__m128i a)
1000 const __m128i b = _mm_set_epi64x(1, 2);
1001 return _mm_clmulepi64_si128(a, b, 0);
1003 int main(void) { return 0; }
1006 tuklib_add_definition_if(liblzma HAVE_USABLE_CLMUL)
1010 # ARM64 C Language Extensions define CRC32 functions in arm_acle.h.
1011 # These are supported by at least GCC and Clang which both need
1012 # __attribute__((__target__("+crc"))), unless the needed compiler flags
1013 # are used to support the CRC instruction.
1014 option(ALLOW_ARM64_CRC32 "Allow ARM64 CRC32 instruction if supported by \
1017 if(ALLOW_ARM64_CRC32)
1018 check_c_source_compiles("
1022 #include <arm_acle.h>
1025 #if (defined(__GNUC__) || defined(__clang__)) && !defined(__EDG__)
1026 __attribute__((__target__(\"+crc\")))
1028 uint32_t my_crc(uint32_t a, uint64_t b)
1030 return __crc32d(a, b);
1032 int main(void) { return 0; }
1036 if(HAVE_ARM64_CRC32)
1037 target_compile_definitions(liblzma PRIVATE HAVE_ARM64_CRC32)
1039 # Check for ARM64 CRC32 instruction runtime detection.
1040 # getauxval() is supported on Linux.
1041 check_symbol_exists(getauxval sys/auxv.h HAVE_GETAUXVAL)
1042 tuklib_add_definition_if(liblzma HAVE_GETAUXVAL)
1044 # elf_aux_info() is supported on FreeBSD.
1045 check_symbol_exists(elf_aux_info sys/auxv.h HAVE_ELF_AUX_INFO)
1046 tuklib_add_definition_if(liblzma HAVE_ELF_AUX_INFO)
1048 # sysctlbyname("hw.optional.armv8_crc32", ...) is supported on Darwin
1049 # (macOS, iOS, etc.). Note that sysctlbyname() is supported on FreeBSD,
1050 # NetBSD, and possibly others too but the string is specific to
1051 # Apple OSes. The C code is responsible for checking
1052 # defined(__APPLE__) before using
1053 # sysctlbyname("hw.optional.armv8_crc32", ...).
1054 check_symbol_exists(sysctlbyname sys/sysctl.h HAVE_SYSCTLBYNAME)
1055 tuklib_add_definition_if(liblzma HAVE_SYSCTLBYNAME)
1060 # Support -fvisiblity=hidden when building shared liblzma.
1061 # These lines do nothing on Windows (even under Cygwin).
1062 # HAVE_VISIBILITY should always be defined to 0 or 1.
1063 if(BUILD_SHARED_LIBS)
1064 set_target_properties(liblzma PROPERTIES C_VISIBILITY_PRESET hidden)
1065 target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=1)
1067 target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=0)
1071 if(BUILD_SHARED_LIBS)
1072 # Add the Windows resource file for liblzma.dll.
1073 target_sources(liblzma PRIVATE src/liblzma/liblzma_w32res.rc)
1075 set_target_properties(liblzma PROPERTIES
1076 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1079 # Export the public API symbols with __declspec(dllexport).
1080 target_compile_definitions(liblzma PRIVATE DLL_EXPORT)
1083 # Create a DEF file. The linker puts the ordinal numbers there
1084 # too so the output from the linker isn't our final file.
1085 target_link_options(liblzma PRIVATE
1086 "-Wl,--output-def,liblzma.def.in")
1088 # Remove the ordinal numbers from the DEF file so that
1089 # no one will create an import library that links by ordinal
1090 # instead of by name. We don't maintain a DEF file so the
1091 # ordinal numbers aren't stable.
1092 add_custom_command(TARGET liblzma POST_BUILD
1093 COMMAND "${CMAKE_COMMAND}"
1094 -DINPUT_FILE=liblzma.def.in
1095 -DOUTPUT_FILE=liblzma.def
1097 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/remove-ordinals.cmake"
1098 BYPRODUCTS "liblzma.def"
1102 # Disable __declspec(dllimport) when linking against static liblzma.
1103 target_compile_definitions(liblzma INTERFACE LZMA_API_STATIC)
1105 elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
1106 # GNU/Linux-specific symbol versioning for shared liblzma.
1107 # Note that adding link options doesn't affect static builds
1108 # but HAVE_SYMBOL_VERSIONS_LINUX must not be used with static builds
1109 # because it would put symbol versions into the static library which
1110 # can cause problems. It's clearer if all symver related things are
1111 # omitted when not building a shared library.
1113 # NOTE: Set it explicitly to 1 to make it clear that versioning is
1114 # done unconditionally in the C files.
1115 target_compile_definitions(liblzma PRIVATE HAVE_SYMBOL_VERSIONS_LINUX=1)
1116 target_link_options(liblzma PRIVATE
1117 "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
1119 set_target_properties(liblzma PROPERTIES
1120 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
1122 elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
1123 # Symbol versioning for shared liblzma for non-GNU/Linux.
1124 # FIXME? What about Solaris?
1125 target_link_options(liblzma PRIVATE
1126 "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
1128 set_target_properties(liblzma PROPERTIES
1129 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
1133 set_target_properties(liblzma PROPERTIES
1134 # At least for now the package versioning matches the rules used for
1135 # shared library versioning (excluding development releases) so it is
1136 # fine to use the package version here.
1137 SOVERSION "${xz_VERSION_MAJOR}"
1138 VERSION "${xz_VERSION}"
1140 # It's liblzma.so or liblzma.dll, not libliblzma.so or lzma.dll.
1141 # Avoid the name lzma.dll because it would conflict with LZMA SDK.
1146 # Create liblzma-config-version.cmake.
1148 # FIXME: SameMajorVersion is correct for stable releases but it is wrong
1149 # for development releases where each release may have incompatible changes.
1150 include(CMakePackageConfigHelpers)
1151 write_basic_package_version_file(
1152 "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
1153 VERSION "${liblzma_VERSION}"
1154 COMPATIBILITY SameMajorVersion)
1156 # Create liblzma-config.cmake. We use this spelling instead of
1157 # liblzmaConfig.cmake to make find_package work in case insensitive
1158 # manner even with case sensitive file systems. This gives more consistent
1159 # behavior between operating systems. This optionally includes a dependency
1160 # on a threading library, so the contents are created in two separate parts.
1161 # The "second half" is always needed, so create it first.
1162 set(LZMA_CONFIG_CONTENTS
1163 "include(\"\${CMAKE_CURRENT_LIST_DIR}/liblzma-targets.cmake\")
1165 if(NOT TARGET LibLZMA::LibLZMA)
1166 # Be compatible with the spelling used by the FindLibLZMA module. This
1167 # doesn't use ALIAS because it would make CMake resolve LibLZMA::LibLZMA
1168 # to liblzma::liblzma instead of keeping the original spelling. Keeping
1169 # the original spelling is important for good FindLibLZMA compatibility.
1170 add_library(LibLZMA::LibLZMA INTERFACE IMPORTED)
1171 set_target_properties(LibLZMA::LibLZMA PROPERTIES
1172 INTERFACE_LINK_LIBRARIES liblzma::liblzma)
1176 if(USE_POSIX_THREADS)
1177 set(LZMA_CONFIG_CONTENTS
1178 "include(CMakeFindDependencyMacro)
1179 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
1180 find_dependency(Threads)
1182 ${LZMA_CONFIG_CONTENTS}
1186 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
1187 "${LZMA_CONFIG_CONTENTS}")
1189 # Set CMAKE_INSTALL_LIBDIR and friends.
1190 include(GNUInstallDirs)
1192 # Create liblzma.pc.
1193 set(prefix "${CMAKE_INSTALL_PREFIX}")
1194 set(exec_prefix "${CMAKE_INSTALL_PREFIX}")
1195 set(libdir "${CMAKE_INSTALL_FULL_LIBDIR}")
1196 set(includedir "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
1197 set(PTHREAD_CFLAGS "${CMAKE_THREAD_LIBS_INIT}")
1198 configure_file(src/liblzma/liblzma.pc.in liblzma.pc
1202 # Install the library binary. The INCLUDES specifies the include path that
1203 # is exported for other projects to use but it doesn't install any files.
1204 install(TARGETS liblzma EXPORT liblzmaTargets
1205 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1206 COMPONENT liblzma_Runtime
1207 LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
1208 COMPONENT liblzma_Runtime
1209 NAMELINK_COMPONENT liblzma_Development
1210 ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
1211 COMPONENT liblzma_Development
1212 INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
1214 # Install the liblzma API headers. These use a subdirectory so
1215 # this has to be done as a separate step.
1216 install(DIRECTORY src/liblzma/api/
1217 COMPONENT liblzma_Development
1218 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
1219 FILES_MATCHING PATTERN "*.h")
1221 # Install the CMake files that other packages can use to find liblzma.
1222 set(liblzma_INSTALL_CMAKEDIR
1223 "${CMAKE_INSTALL_LIBDIR}/cmake/liblzma"
1224 CACHE STRING "Path to liblzma's .cmake files")
1226 install(EXPORT liblzmaTargets
1228 FILE liblzma-targets.cmake
1229 DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
1230 COMPONENT liblzma_Development)
1232 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
1233 "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
1234 DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
1235 COMPONENT liblzma_Development)
1238 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblzma.pc"
1239 DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig"
1240 COMPONENT liblzma_Development)
1244 #############################################################################
1245 # libgnu (getopt_long)
1246 #############################################################################
1248 # This mirrors how the Autotools build system handles the getopt_long
1249 # replacement, calling the object library libgnu since the replacement
1250 # version comes from Gnulib.
1251 add_library(libgnu OBJECT)
1253 # CMake requires that even an object library must have at least once source
1254 # file. So we give it a header file that results in no output files.
1255 target_sources(libgnu PRIVATE lib/getopt.in.h)
1257 # The Ninja Generator requires setting the linker language since it cannot
1258 # guess the programming language of just a header file. Setting this
1259 # property avoids needing an empty .c file or an non-empty unnecessary .c
1261 set_target_properties(libgnu PROPERTIES LINKER_LANGUAGE C)
1263 # Create /lib directory in the build directory and add it to the include path.
1264 file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib")
1265 target_include_directories(libgnu PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/lib")
1267 # Include /lib from the source directory. It does no harm even if none of
1268 # the Gnulib replacements are used.
1269 target_include_directories(libgnu PUBLIC lib)
1271 # The command line tools need getopt_long in order to parse arguments. If
1272 # the system does not have a getopt_long implementation we can use the one
1273 # from Gnulib instead.
1274 check_symbol_exists(getopt_long getopt.h HAVE_GETOPT_LONG)
1276 if(NOT HAVE_GETOPT_LONG)
1277 # Set the __GETOPT_PREFIX definition to "rpl_" (replacement) to avoid
1278 # name conflicts with libc symbols. The same prefix is set if using
1279 # the Autotools build (m4/getopt.m4).
1280 target_compile_definitions(libgnu PUBLIC "__GETOPT_PREFIX=rpl_")
1282 # Create a custom copy command to copy the getopt header to the build
1283 # directory and re-copy it if it is updated. (Gnulib does it this way
1284 # because it allows choosing which .in.h files to actually use in the
1285 # build. We need just getopt.h so this is a bit overcomplicated for
1286 # a single header file only.)
1287 add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/lib/getopt.h"
1288 COMMAND "${CMAKE_COMMAND}" -E copy
1289 "${CMAKE_CURRENT_SOURCE_DIR}/lib/getopt.in.h"
1290 "${CMAKE_CURRENT_BINARY_DIR}/lib/getopt.h"
1291 MAIN_DEPENDENCY "${CMAKE_CURRENT_SOURCE_DIR}/lib/getopt.in.h"
1294 target_sources(libgnu PRIVATE
1301 lib/getopt-pfx-core.h
1302 lib/getopt-pfx-ext.h
1303 "${CMAKE_CURRENT_BINARY_DIR}/lib/getopt.h"
1308 #############################################################################
1310 #############################################################################
1312 if(HAVE_DECODERS AND (NOT MSVC OR MSVC_VERSION GREATER_EQUAL 1900))
1313 add_executable(xzdec
1314 src/common/sysdefs.h
1315 src/common/tuklib_common.h
1316 src/common/tuklib_config.h
1317 src/common/tuklib_exit.c
1318 src/common/tuklib_exit.h
1319 src/common/tuklib_gettext.h
1320 src/common/tuklib_progname.c
1321 src/common/tuklib_progname.h
1325 target_include_directories(xzdec PRIVATE
1330 target_link_libraries(xzdec PRIVATE liblzma libgnu)
1333 # Add the Windows resource file for xzdec.exe.
1334 target_sources(xzdec PRIVATE src/xzdec/xzdec_w32res.rc)
1335 set_target_properties(xzdec PROPERTIES
1336 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1340 if(SANDBOX_COMPILE_DEFINITION)
1341 target_compile_definitions(xzdec PRIVATE
1342 "${SANDBOX_COMPILE_DEFINITION}")
1345 tuklib_progname(xzdec)
1347 install(TARGETS xzdec
1348 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1352 install(FILES src/xzdec/xzdec.1
1353 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1359 #############################################################################
1361 #############################################################################
1363 if(NOT MSVC OR MSVC_VERSION GREATER_EQUAL 1900)
1365 src/common/mythread.h
1366 src/common/sysdefs.h
1367 src/common/tuklib_common.h
1368 src/common/tuklib_config.h
1369 src/common/tuklib_exit.c
1370 src/common/tuklib_exit.h
1371 src/common/tuklib_gettext.h
1372 src/common/tuklib_integer.h
1373 src/common/tuklib_mbstr.h
1374 src/common/tuklib_mbstr_fw.c
1375 src/common/tuklib_mbstr_width.c
1376 src/common/tuklib_open_stdxxx.c
1377 src/common/tuklib_open_stdxxx.h
1378 src/common/tuklib_progname.c
1379 src/common/tuklib_progname.h
1405 target_include_directories(xz PRIVATE
1411 target_sources(xz PRIVATE
1417 target_link_libraries(xz PRIVATE liblzma libgnu)
1419 target_compile_definitions(xz PRIVATE ASSUME_RAM=128)
1422 # Add the Windows resource file for xz.exe.
1423 target_sources(xz PRIVATE src/xz/xz_w32res.rc)
1424 set_target_properties(xz PROPERTIES
1425 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1429 if(SANDBOX_COMPILE_DEFINITION)
1430 target_compile_definitions(xz PRIVATE "${SANDBOX_COMPILE_DEFINITION}")
1436 check_symbol_exists(optreset getopt.h HAVE_OPTRESET)
1437 tuklib_add_definition_if(xz HAVE_OPTRESET)
1439 check_symbol_exists(posix_fadvise fcntl.h HAVE_POSIX_FADVISE)
1440 tuklib_add_definition_if(xz HAVE_POSIX_FADVISE)
1442 # How to get file time:
1443 check_struct_has_member("struct stat" st_atim.tv_nsec
1444 "sys/types.h;sys/stat.h"
1445 HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1446 if(HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1447 tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1449 check_struct_has_member("struct stat" st_atimespec.tv_nsec
1450 "sys/types.h;sys/stat.h"
1451 HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1452 if(HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1453 tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1455 check_struct_has_member("struct stat" st_atimensec
1456 "sys/types.h;sys/stat.h"
1457 HAVE_STRUCT_STAT_ST_ATIMENSEC)
1458 tuklib_add_definition_if(xz HAVE_STRUCT_STAT_ST_ATIMENSEC)
1462 # How to set file time:
1463 check_symbol_exists(futimens "sys/types.h;sys/stat.h" HAVE_FUTIMENS)
1465 tuklib_add_definitions(xz HAVE_FUTIMENS)
1467 check_symbol_exists(futimes "sys/time.h" HAVE_FUTIMES)
1469 tuklib_add_definitions(xz HAVE_FUTIMES)
1471 check_symbol_exists(futimesat "sys/time.h" HAVE_FUTIMESAT)
1473 tuklib_add_definitions(xz HAVE_FUTIMESAT)
1475 check_symbol_exists(utimes "sys/time.h" HAVE_UTIMES)
1477 tuklib_add_definitions(xz HAVE_UTIMES)
1479 check_symbol_exists(_futime "sys/utime.h" HAVE__FUTIME)
1481 tuklib_add_definitions(xz HAVE__FUTIME)
1483 check_symbol_exists(utime "utime.h" HAVE_UTIME)
1484 tuklib_add_definition_if(xz HAVE_UTIME)
1492 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1496 install(FILES src/xz/xz.1
1497 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1500 option(CREATE_XZ_SYMLINKS "Create unxz and xzcat symlinks" ON)
1501 option(CREATE_LZMA_SYMLINKS "Create lzma, unlzma, and lzcat symlinks"
1505 if(CREATE_XZ_SYMLINKS)
1506 list(APPEND XZ_LINKS "unxz" "xzcat")
1509 if(CREATE_LZMA_SYMLINKS)
1510 list(APPEND XZ_LINKS "lzma" "unlzma" "lzcat")
1513 # With Windows Cygwin and MSYS2 the symlinking is complicated. Both
1514 # of these environments set the UNIX variable so they will try to
1515 # make the symlinks. The ability for Cygwin and MSYS2 to make
1516 # broken symlinks is determined by the CYGWIN and MSYS2 environment
1517 # variables, respectively. Broken symlinks are needed for the man
1518 # page symlinks and for determining if the xz and lzma symlinks need
1519 # to depend on the xz target or not. If broken symlinks cannot be
1520 # made then the xz binary must be created before the symlinks.
1521 set(ALLOW_BROKEN_SYMLINKS ON)
1523 if(CMAKE_SYSTEM_NAME STREQUAL "CYGWIN")
1524 # The Cygwin env variable can be set to four possible values:
1526 # 1. "lnk". Create symlinks as Windows shortcuts.
1528 # 2. "native". Create symlinks as native Windows symlinks
1529 # if supported by the system. Fallback to "lnk" if native
1530 # symlinks are not supported.
1532 # 3. "nativestrict". Create symlinks as native Windows symlinks
1533 # if supported by the system. If the target of the symlink
1534 # does not exist or the creation of the symlink fails for any
1535 # reason, do not create the symlink.
1537 # 4. "sys". Create symlinks as plain files with a special
1538 # system attribute containing the path to the symlink target.
1540 # So, the only case we care about for broken symlinks is
1541 # "nativestrict" since all other values mean that broken
1542 # symlinks are allowed. If the env variable is not set the
1543 # default is "native". If the env variable is set but not
1544 # assigned one of the four values, then the default is the same
1545 # as option 1 "lnk".
1546 string(FIND "$ENV{CYGWIN}" "winsymlinks:nativestrict" SYMLINK_POS)
1547 if(SYMLINK_POS GREATER -1)
1548 set(ALLOW_BROKEN_SYMLINKS OFF)
1550 elseif(CMAKE_SYSTEM_NAME STREQUAL "MSYS")
1551 # The MSYS env variable behaves similar to the CYGWIN but has a
1552 # different default behavior. If winsymlinks is set but not
1553 # assigned one of the four supported values, the default is to
1554 # *copy* the target to the symlink destination. This will fail
1555 # if the target does not exist so broken symlinks cannot be
1557 string(FIND "$ENV{MSYS}" "winsymlinks" SYMLINK_POS)
1558 if(SYMLINK_POS GREATER -1)
1559 string(FIND "$ENV{MSYS}" "winsymlinks:nativestrict"
1561 if(SYMLINK_POS GREATER -1)
1562 set(ALLOW_BROKEN_SYMLINKS OFF)
1565 set(ALLOW_BROKEN_SYMLINKS OFF)
1569 # Create symlinks in the build directory and then install them.
1571 # The symlinks do not likely need any special extension since
1572 # even on Windows the symlink can still be executed without
1573 # the .exe extension.
1574 foreach(LINK IN LISTS XZ_LINKS)
1575 add_custom_target("create_${LINK}" ALL
1576 "${CMAKE_COMMAND}" -E create_symlink
1577 "$<TARGET_FILE_NAME:xz>" "${LINK}"
1578 BYPRODUCTS "${LINK}"
1580 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}"
1581 DESTINATION "${CMAKE_INSTALL_BINDIR}"
1584 # Only create the man page symlinks if the symlinks can be
1585 # created broken. The symlinks will not be valid until install
1586 # so they cannot be created on these system environments.
1587 if(ALLOW_BROKEN_SYMLINKS)
1588 add_custom_target("create_${LINK}.1" ALL
1589 "${CMAKE_COMMAND}" -E create_symlink "xz.1" "${LINK}.1"
1590 BYPRODUCTS "${LINK}.1"
1592 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}.1"
1593 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1596 # Add the xz target as dependency when broken symlinks
1597 # cannot be made. This ensures parallel builds do not fail
1598 # since it will enforce the order of creating xz first, then
1600 add_dependencies("create_${LINK}" xz)
1607 #############################################################################
1609 #############################################################################
1629 foreach(TEST IN LISTS LIBLZMA_TESTS)
1630 add_executable("${TEST}" "tests/${TEST}.c")
1632 target_include_directories("${TEST}" PRIVATE
1638 target_link_libraries("${TEST}" PRIVATE liblzma)
1640 # Put the test programs into their own subdirectory so they don't
1641 # pollute the top-level dir which might contain xz and xzdec.
1642 set_target_properties("${TEST}" PROPERTIES
1643 RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tests_bin"
1646 add_test(NAME "${TEST}"
1647 COMMAND "${CMAKE_CURRENT_BINARY_DIR}/tests_bin/${TEST}"
1650 # Set srcdir environment variable so that the tests find their
1651 # input files from the source tree.
1653 # Set the return code for skipped tests to match Automake convention.
1654 set_tests_properties("${TEST}" PROPERTIES
1655 ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"