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:
12 # - No large file support on those 32-bit platforms that need it
13 # - No replacement getopt_long(), libc must have it
14 # - No sandboxing support
17 # Other missing things:
18 # - No xzgrep or other scripts or their symlinks
19 # - No xz tests (liblzma tests only)
21 # NOTE: Even if the code compiles without warnings, the end result may be
22 # different than via ./configure. Specifically, the list of #defines
23 # may be different (if so, probably this CMakeLists.txt got them wrong).
25 # This file provides the following installation components (if you only
26 # need liblzma, install only its components!):
28 # - liblzma_Development
29 # - xz (on some platforms only)
30 # - xzdec (on some platforms only)
32 # To find the target liblzma::liblzma from other packages, use the CONFIG
33 # option with find_package() to avoid a conflict with the FindLibLZMA module
34 # with case-insensitive file systems. For example, to require liblzma 5.2.5
35 # or a newer compatible version:
37 # find_package(liblzma 5.2.5 REQUIRED CONFIG)
38 # target_link_libraries(my_application liblzma::liblzma)
40 #############################################################################
42 # Author: Lasse Collin
44 # This file has been put into the public domain.
45 # You can do whatever you want with this file.
47 #############################################################################
49 cmake_minimum_required(VERSION 3.13...3.26 FATAL_ERROR)
51 include(CMakePushCheckState)
52 include(CheckIncludeFile)
53 include(CheckSymbolExists)
54 include(CheckStructHasMember)
55 include(CheckCSourceCompiles)
56 include(cmake/tuklib_integer.cmake)
57 include(cmake/tuklib_cpucores.cmake)
58 include(cmake/tuklib_physmem.cmake)
59 include(cmake/tuklib_progname.cmake)
60 include(cmake/tuklib_mbstr.cmake)
62 # Get the package version from version.h into XZ_VERSION variable.
63 file(READ src/liblzma/api/lzma/version.h XZ_VERSION)
66 #define LZMA_VERSION_MAJOR ([0-9]+)\n\
68 #define LZMA_VERSION_MINOR ([0-9]+)\n\
70 #define LZMA_VERSION_PATCH ([0-9]+)\n\
72 "\\1.\\2.\\3" XZ_VERSION "${XZ_VERSION}")
74 # Among other things, this gives us variables xz_VERSION and xz_VERSION_MAJOR.
75 project(xz VERSION "${XZ_VERSION}" LANGUAGES C)
77 # We need a compiler that supports enough C99 or newer (variable-length arrays
78 # aren't needed, those are optional in C17). Setting CMAKE_C_STANDARD here
79 # makes it the default for all targets. It doesn't affect the INTERFACE so
80 # liblzma::liblzma won't end up with INTERFACE_COMPILE_FEATURES "c_std_99"
81 # (the API headers are C89 and C++ compatible).
82 set(CMAKE_C_STANDARD 99)
83 set(CMAKE_C_STANDARD_REQUIRED ON)
85 # On Apple OSes, don't build executables as bundles:
86 set(CMAKE_MACOSX_BUNDLE OFF)
88 # windres from GNU binutils can be tricky with command line arguments
89 # that contain spaces or other funny characters. Unfortunately we need
90 # a space in PACKAGE_NAME. Using \x20 to encode the US-ASCII space seems
91 # to work in both cmd.exe and /bin/sh.
93 # However, even \x20 isn't enough in all situations, resulting in
94 # "syntax error" from windres. Using --use-temp-file prevents windres
95 # from using popen() and this seems to fix the problem.
97 # llvm-windres claims to be compatible with GNU windres but with that
98 # the \x20 results in "XZx20Utils" in the compiled binary. (At the
99 # same time it works correctly with clang (the C compiler).) The option
100 # --use-temp-file makes no difference.
102 # CMake 3.25 doesn't have CMAKE_RC_COMPILER_ID so we rely on
103 # CMAKE_C_COMPILER_ID. If Clang is used together with GNU windres
104 # then it will fail, but this way the risk of a bad string in
105 # the binary should be fairly low.
106 if(WIN32 AND CMAKE_C_COMPILER_ID STREQUAL "GNU")
107 # Use workarounds with GNU windres. The \x20 in PACKAGE_NAME works
108 # with gcc too so we don't need to worry how to pass different flags
109 # to windres and gcc.
110 string(APPEND CMAKE_RC_FLAGS " --use-temp-file")
111 set(PACKAGE_NAME "XZ\\x20Utils")
113 # Elsewhere a space is safe. This also keeps things compatible with
114 # EBCDIC in case CMake-based build is ever done on such a system.
115 set(PACKAGE_NAME "XZ Utils")
118 # Definitions common to all targets:
119 add_compile_definitions(
121 PACKAGE_NAME="${PACKAGE_NAME}"
122 PACKAGE_BUGREPORT="xz@tukaani.org"
123 PACKAGE_URL="https://tukaani.org/xz/"
125 # Standard headers and types are available:
131 # Always enable CRC32 since liblzma should never build without it.
134 # Disable assert() checks when no build type has been specified. Non-empty
135 # build types like "Release" and "Debug" handle this by default.
140 ######################
141 # System definitions #
142 ######################
144 # _GNU_SOURCE and such definitions. This specific macro is special since
145 # it also adds the definitions to CMAKE_REQUIRED_DEFINITIONS.
146 tuklib_use_system_extensions(ALL)
148 # This is needed by liblzma and xz.
151 # Check for clock_gettime(). Do this before checking for threading so
152 # that we know there if CLOCK_MONOTONIC is available.
154 check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME)
156 if(NOT HAVE_CLOCK_GETTIME)
157 # With glibc <= 2.17 or Solaris 10 this needs librt.
158 # Add librt for the next check for HAVE_CLOCK_GETTIME. If it is
159 # found after including the library, we know that librt is required.
160 list(INSERT CMAKE_REQUIRED_LIBRARIES 0 rt)
161 check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME_LIBRT)
163 # If it was found now, add librt to all targets and keep it in
164 # CMAKE_REQUIRED_LIBRARIES for further tests too.
165 if(HAVE_CLOCK_GETTIME_LIBRT)
168 list(REMOVE_AT CMAKE_REQUIRED_LIBRARIES 0)
172 if(HAVE_CLOCK_GETTIME OR HAVE_CLOCK_GETTIME_LIBRT)
173 add_compile_definitions(HAVE_CLOCK_GETTIME)
175 # Check if CLOCK_MONOTONIC is available for clock_gettime().
176 check_symbol_exists(CLOCK_MONOTONIC time.h HAVE_CLOCK_MONOTONIC)
177 tuklib_add_definition_if(ALL HAVE_CLOCK_MONOTONIC)
181 # Options for new enough GCC or Clang on any arch or operating system:
182 if(CMAKE_C_COMPILER_ID MATCHES GNU|Clang)
183 # configure.ac has a long list but it won't be copied here:
184 add_compile_options(-Wall -Wextra)
188 #############################################################################
190 #############################################################################
192 option(BUILD_SHARED_LIBS "Build liblzma as a shared library instead of static")
195 src/common/mythread.h
197 src/common/tuklib_common.h
198 src/common/tuklib_config.h
199 src/common/tuklib_integer.h
200 src/common/tuklib_physmem.c
201 src/common/tuklib_physmem.h
202 src/liblzma/api/lzma.h
203 src/liblzma/api/lzma/base.h
204 src/liblzma/api/lzma/bcj.h
205 src/liblzma/api/lzma/block.h
206 src/liblzma/api/lzma/check.h
207 src/liblzma/api/lzma/container.h
208 src/liblzma/api/lzma/delta.h
209 src/liblzma/api/lzma/filter.h
210 src/liblzma/api/lzma/hardware.h
211 src/liblzma/api/lzma/index.h
212 src/liblzma/api/lzma/index_hash.h
213 src/liblzma/api/lzma/lzma12.h
214 src/liblzma/api/lzma/stream_flags.h
215 src/liblzma/api/lzma/version.h
216 src/liblzma/api/lzma/vli.h
217 src/liblzma/check/check.c
218 src/liblzma/check/check.h
219 src/liblzma/check/crc_macros.h
220 src/liblzma/common/block_util.c
221 src/liblzma/common/common.c
222 src/liblzma/common/common.h
223 src/liblzma/common/easy_preset.c
224 src/liblzma/common/easy_preset.h
225 src/liblzma/common/filter_common.c
226 src/liblzma/common/filter_common.h
227 src/liblzma/common/hardware_physmem.c
228 src/liblzma/common/index.c
229 src/liblzma/common/index.h
230 src/liblzma/common/memcmplen.h
231 src/liblzma/common/stream_flags_common.c
232 src/liblzma/common/stream_flags_common.h
233 src/liblzma/common/string_conversion.c
234 src/liblzma/common/vli_size.c
237 target_include_directories(liblzma PRIVATE
242 src/liblzma/rangecoder
250 ######################
251 # Size optimizations #
252 ######################
254 option(ENABLE_SMALL "Reduce code size at expense of speed. \
255 This may be useful together with CMAKE_BUILD_TYPE=MinSizeRel.")
258 add_compile_definitions(HAVE_SMALL)
266 set(ADDITIONAL_SUPPORTED_CHECKS crc64 sha256)
268 set(ADDITIONAL_CHECK_TYPES "${ADDITIONAL_SUPPORTED_CHECKS}" CACHE STRING
269 "Additional check types to support (crc32 is always built)")
271 foreach(CHECK IN LISTS ADDITIONAL_CHECK_TYPES)
272 if(NOT CHECK IN_LIST ADDITIONAL_SUPPORTED_CHECKS)
273 message(SEND_ERROR "'${CHECK}' is not a supported check type")
278 target_sources(liblzma PRIVATE src/liblzma/check/crc32_small.c)
280 target_sources(liblzma PRIVATE
281 src/liblzma/check/crc32_fast.c
282 src/liblzma/check/crc32_table.c
283 src/liblzma/check/crc32_table_be.h
284 src/liblzma/check/crc32_table_le.h
288 if("crc64" IN_LIST ADDITIONAL_CHECK_TYPES)
289 add_compile_definitions("HAVE_CHECK_CRC64")
292 target_sources(liblzma PRIVATE src/liblzma/check/crc64_small.c)
294 target_sources(liblzma PRIVATE
295 src/liblzma/check/crc64_fast.c
296 src/liblzma/check/crc64_table.c
297 src/liblzma/check/crc64_table_be.h
298 src/liblzma/check/crc64_table_le.h
303 if("sha256" IN_LIST ADDITIONAL_CHECK_TYPES)
304 add_compile_definitions("HAVE_CHECK_SHA256")
305 target_sources(liblzma PRIVATE src/liblzma/check/sha256.c)
313 set(SUPPORTED_MATCH_FINDERS hc3 hc4 bt2 bt3 bt4)
315 set(MATCH_FINDERS "${SUPPORTED_MATCH_FINDERS}" CACHE STRING
316 "Match finders to support (at least one is required for LZMA1 or LZMA2)")
318 foreach(MF IN LISTS MATCH_FINDERS)
319 if(MF IN_LIST SUPPORTED_MATCH_FINDERS)
320 string(TOUPPER "${MF}" MF_UPPER)
321 add_compile_definitions("HAVE_MF_${MF_UPPER}")
323 message(SEND_ERROR "'${MF}' is not a supported match finder")
332 # Supported thread methods:
333 # ON - autodetect the best threading method. The autodetection will
334 # prefer Windows threading (win95 or vista) over posix if both are
335 # available. vista threads will be used over win95 unless it is a
337 # OFF - Disable threading.
338 # posix - Use posix threading, or throw an error if not available.
339 # win95 - Use Windows win95 threading, or throw an error if not available.
340 # vista - Use Windows vista threading, or throw an error if not available.
341 set(SUPPORTED_THREAD_METHODS ON OFF posix win95 vista)
343 set(ENABLE_THREADS ON CACHE STRING
344 "Threading method type to support. Set to 'OFF' to disable threading")
346 # Create dropdown in CMake GUI since only 1 threading method is possible
347 # to select in a build.
348 set_property(CACHE ENABLE_THREADS
349 PROPERTY STRINGS "${SUPPORTED_THREAD_METHODS}")
351 # This is a flag variable set when win95 threads are used. We must ensure
352 # the combination of enable_small and win95 threads is not used without a
353 # compiler supporting attribute __constructor__.
354 set(USE_WIN95_THREADS OFF)
356 if(NOT ENABLE_THREADS IN_LIST SUPPORTED_THREAD_METHODS)
357 message(SEND_ERROR "'${ENABLE_THREADS}' is not a supported thread type")
361 # Also set THREADS_PREFER_PTHREAD_FLAG since the flag has no effect
362 # for Windows threading.
363 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
364 find_package(Threads REQUIRED)
366 # If both Windows and posix threading are available, prefer Windows.
367 if(CMAKE_USE_WIN32_THREADS_INIT AND NOT ENABLE_THREADS STREQUAL "posix")
368 if(ENABLE_THREADS STREQUAL "win95"
369 OR (ENABLE_THREADS STREQUAL "ON"
370 AND CMAKE_SIZEOF_VOID_P EQUAL 4))
371 # Use Windows 95 (and thus XP) compatible threads.
372 # This avoids use of features that were added in
373 # Windows Vista. This is used for 32-bit x86 builds for
374 # compatibility reasons since it makes no measurable difference
375 # in performance compared to Vista threads.
376 set(USE_WIN95_THREADS ON)
377 add_compile_definitions(MYTHREAD_WIN95)
379 add_compile_definitions(MYTHREAD_VISTA)
381 elseif(CMAKE_USE_PTHREADS_INIT)
382 if(ENABLE_THREADS STREQUAL "posix" OR ENABLE_THREADS STREQUAL "ON")
383 # Overwrite ENABLE_THREADS in case it was set to "ON".
384 # The threading library only needs to be explicitly linked
385 # for posix threads, so this is needed for creating
386 # liblzma-config.cmake later.
387 set(ENABLE_THREADS "posix")
389 target_link_libraries(liblzma Threads::Threads)
390 add_compile_definitions(MYTHREAD_POSIX)
392 # Check if pthread_condattr_setclock() exists to use CLOCK_MONOTONIC.
393 if(HAVE_CLOCK_MONOTONIC)
394 list(INSERT CMAKE_REQUIRED_LIBRARIES 0 "${CMAKE_THREAD_LIBS_INIT}")
395 check_symbol_exists(pthread_condattr_setclock pthread.h
396 HAVE_PTHREAD_CONDATTR_SETCLOCK)
397 tuklib_add_definition_if(ALL HAVE_PTHREAD_CONDATTR_SETCLOCK)
401 "Windows thread method requested, but a compatible "
402 "library could not be found")
405 message(SEND_ERROR "No supported threading library found")
408 target_sources(liblzma PRIVATE
409 src/common/tuklib_cpucores.c
410 src/common/tuklib_cpucores.h
411 src/liblzma/common/hardware_cputhreads.c
412 src/liblzma/common/outqueue.c
413 src/liblzma/common/outqueue.h
432 # The SUPPORTED_FILTERS are shared between Encoders and Decoders
433 # since only lzip does not appear in both lists. lzip is a special
434 # case anyway, so it is handled separately in the Decoders section.
435 set(SUPPORTED_FILTERS
442 set(ENCODERS "${SUPPORTED_FILTERS}" CACHE STRING "Encoders to support")
444 # If LZMA2 is enabled, then LZMA1 must also be enabled.
445 if(NOT "lzma1" IN_LIST ENCODERS AND "lzma2" IN_LIST ENCODERS)
446 message(SEND_ERROR "LZMA2 encoder requires that LZMA1 is also enabled")
449 # If LZMA1 is enabled, then at least one match finder must be enabled.
450 if(MATCH_FINDERS STREQUAL "" AND "lzma1" IN_LIST ENCODERS)
451 message(SEND_ERROR "At least 1 match finder is required for an "
455 set(HAVE_DELTA_CODER OFF)
456 set(SIMPLE_ENCODERS OFF)
457 set(HAVE_ENCODERS OFF)
459 foreach(ENCODER IN LISTS ENCODERS)
460 if(ENCODER IN_LIST SUPPORTED_FILTERS)
461 set(HAVE_ENCODERS ON)
463 if(NOT SIMPLE_ENCODERS AND ENCODER IN_LIST SIMPLE_FILTERS)
464 set(SIMPLE_ENCODERS ON)
467 string(TOUPPER "${ENCODER}" ENCODER_UPPER)
468 add_compile_definitions("HAVE_ENCODER_${ENCODER_UPPER}")
470 message(SEND_ERROR "'${ENCODER}' is not a supported encoder")
475 add_compile_definitions(HAVE_ENCODERS)
477 target_sources(liblzma PRIVATE
478 src/liblzma/common/alone_encoder.c
479 src/liblzma/common/block_buffer_encoder.c
480 src/liblzma/common/block_buffer_encoder.h
481 src/liblzma/common/block_encoder.c
482 src/liblzma/common/block_encoder.h
483 src/liblzma/common/block_header_encoder.c
484 src/liblzma/common/easy_buffer_encoder.c
485 src/liblzma/common/easy_encoder.c
486 src/liblzma/common/easy_encoder_memusage.c
487 src/liblzma/common/filter_buffer_encoder.c
488 src/liblzma/common/filter_encoder.c
489 src/liblzma/common/filter_encoder.h
490 src/liblzma/common/filter_flags_encoder.c
491 src/liblzma/common/index_encoder.c
492 src/liblzma/common/index_encoder.h
493 src/liblzma/common/stream_buffer_encoder.c
494 src/liblzma/common/stream_encoder.c
495 src/liblzma/common/stream_flags_encoder.c
496 src/liblzma/common/vli_encoder.c
500 target_sources(liblzma PRIVATE
501 src/liblzma/common/stream_encoder_mt.c
506 target_sources(liblzma PRIVATE
507 src/liblzma/simple/simple_encoder.c
508 src/liblzma/simple/simple_encoder.h
512 if("lzma1" IN_LIST ENCODERS)
513 target_sources(liblzma PRIVATE
514 src/liblzma/lzma/lzma_encoder.c
515 src/liblzma/lzma/lzma_encoder.h
516 src/liblzma/lzma/lzma_encoder_optimum_fast.c
517 src/liblzma/lzma/lzma_encoder_optimum_normal.c
518 src/liblzma/lzma/lzma_encoder_private.h
519 src/liblzma/lzma/fastpos.h
520 src/liblzma/lz/lz_encoder.c
521 src/liblzma/lz/lz_encoder.h
522 src/liblzma/lz/lz_encoder_hash.h
523 src/liblzma/lz/lz_encoder_hash_table.h
524 src/liblzma/lz/lz_encoder_mf.c
525 src/liblzma/rangecoder/price.h
526 src/liblzma/rangecoder/price_table.c
527 src/liblzma/rangecoder/range_encoder.h
531 target_sources(liblzma PRIVATE src/liblzma/lzma/fastpos_table.c)
535 if("lzma2" IN_LIST ENCODERS)
536 target_sources(liblzma PRIVATE
537 src/liblzma/lzma/lzma2_encoder.c
538 src/liblzma/lzma/lzma2_encoder.h
542 if("delta" IN_LIST ENCODERS)
543 set(HAVE_DELTA_CODER ON)
544 target_sources(liblzma PRIVATE
545 src/liblzma/delta/delta_encoder.c
546 src/liblzma/delta/delta_encoder.h
556 set(DECODERS "${SUPPORTED_FILTERS}" CACHE STRING "Decoders to support")
558 set(SIMPLE_DECODERS OFF)
559 set(HAVE_DECODERS OFF)
561 foreach(DECODER IN LISTS DECODERS)
562 if(DECODER IN_LIST SUPPORTED_FILTERS)
563 set(HAVE_DECODERS ON)
565 if(NOT SIMPLE_DECODERS AND DECODER IN_LIST SIMPLE_FILTERS)
566 set(SIMPLE_DECODERS ON)
569 string(TOUPPER "${DECODER}" DECODER_UPPER)
570 add_compile_definitions("HAVE_DECODER_${DECODER_UPPER}")
572 message(SEND_ERROR "'${DECODER}' is not a supported decoder")
577 add_compile_definitions(HAVE_DECODERS)
579 target_sources(liblzma PRIVATE
580 src/liblzma/common/alone_decoder.c
581 src/liblzma/common/alone_decoder.h
582 src/liblzma/common/auto_decoder.c
583 src/liblzma/common/block_buffer_decoder.c
584 src/liblzma/common/block_decoder.c
585 src/liblzma/common/block_decoder.h
586 src/liblzma/common/block_header_decoder.c
587 src/liblzma/common/easy_decoder_memusage.c
588 src/liblzma/common/file_info.c
589 src/liblzma/common/filter_buffer_decoder.c
590 src/liblzma/common/filter_decoder.c
591 src/liblzma/common/filter_decoder.h
592 src/liblzma/common/filter_flags_decoder.c
593 src/liblzma/common/index_decoder.c
594 src/liblzma/common/index_decoder.h
595 src/liblzma/common/index_hash.c
596 src/liblzma/common/stream_buffer_decoder.c
597 src/liblzma/common/stream_decoder.c
598 src/liblzma/common/stream_flags_decoder.c
599 src/liblzma/common/stream_decoder.h
600 src/liblzma/common/vli_decoder.c
604 target_sources(liblzma PRIVATE
605 src/liblzma/common/stream_decoder_mt.c
610 target_sources(liblzma PRIVATE
611 src/liblzma/simple/simple_decoder.c
612 src/liblzma/simple/simple_decoder.h
616 if("lzma1" IN_LIST DECODERS)
617 target_sources(liblzma PRIVATE
618 src/liblzma/lzma/lzma_decoder.c
619 src/liblzma/lzma/lzma_decoder.h
620 src/liblzma/rangecoder/range_decoder.h
621 src/liblzma/lz/lz_decoder.c
622 src/liblzma/lz/lz_decoder.h
626 if("lzma2" IN_LIST DECODERS)
627 target_sources(liblzma PRIVATE
628 src/liblzma/lzma/lzma2_decoder.c
629 src/liblzma/lzma/lzma2_decoder.h
633 if("delta" IN_LIST DECODERS)
634 set(HAVE_DELTA_CODER ON)
635 target_sources(liblzma PRIVATE
636 src/liblzma/delta/delta_decoder.c
637 src/liblzma/delta/delta_decoder.h
642 # Some sources must appear if the filter is configured as either
643 # an encoder or decoder.
644 if("lzma1" IN_LIST ENCODERS OR "lzma1" IN_LIST DECODERS)
645 target_sources(liblzma PRIVATE
646 src/liblzma/rangecoder/range_common.h
647 src/liblzma/lzma/lzma_encoder_presets.c
648 src/liblzma/lzma/lzma_common.h
653 target_sources(liblzma PRIVATE
654 src/liblzma/delta/delta_common.c
655 src/liblzma/delta/delta_common.h
656 src/liblzma/delta/delta_private.h
660 if(SIMPLE_ENCODERS OR SIMPLE_DECODERS)
661 target_sources(liblzma PRIVATE
662 src/liblzma/simple/simple_coder.c
663 src/liblzma/simple/simple_coder.h
664 src/liblzma/simple/simple_private.h
668 foreach(SIMPLE_CODER IN LISTS SIMPLE_FILTERS)
669 if(SIMPLE_CODER IN_LIST ENCODERS OR SIMPLE_CODER IN_LIST DECODERS)
670 target_sources(liblzma PRIVATE "src/liblzma/simple/${SIMPLE_CODER}.c")
679 option(MICROLZMA_ENCODER
680 "MicroLZMA encoder (needed by specific applications only)" ON)
682 option(MICROLZMA_DECODER
683 "MicroLZMA decoder (needed by specific applications only)" ON)
685 if(MICROLZMA_ENCODER)
686 if(NOT "lzma1" IN_LIST ENCODERS)
687 message(SEND_ERROR "The LZMA1 encoder is required to support the "
691 target_sources(liblzma PRIVATE src/liblzma/common/microlzma_encoder.c)
694 if(MICROLZMA_DECODER)
695 if(NOT "lzma1" IN_LIST DECODERS)
696 message(SEND_ERROR "The LZMA1 decoder is required to support the "
700 target_sources(liblzma PRIVATE src/liblzma/common/microlzma_decoder.c)
704 #############################
705 # lzip (.lz) format support #
706 #############################
708 option(LZIP_DECODER "Support lzip decoder" ON)
711 # If lzip decoder support is requested, make sure LZMA1 decoder is enabled.
712 if(NOT "lzma1" IN_LIST DECODERS)
713 message(SEND_ERROR "The LZMA1 decoder is required to support the "
717 add_compile_definitions(HAVE_LZIP_DECODER)
719 target_sources(liblzma PRIVATE
720 src/liblzma/common/lzip_decoder.c
721 src/liblzma/common/lzip_decoder.h
727 # Put the tuklib functions under the lzma_ namespace.
728 target_compile_definitions(liblzma PRIVATE TUKLIB_SYMBOL_PREFIX=lzma_)
729 tuklib_cpucores(liblzma)
730 tuklib_physmem(liblzma)
732 # While liblzma can be built without tuklib_cpucores or tuklib_physmem
733 # modules, the liblzma API functions lzma_cputhreads() and lzma_physmem()
734 # will then be useless (which isn't too bad but still unfortunate). Since
735 # I expect the CMake-based builds to be only used on systems that are
736 # supported by these tuklib modules, problems with these tuklib modules
737 # are considered a hard error for now. This hopefully helps to catch bugs
738 # in the CMake versions of the tuklib checks.
739 if(NOT TUKLIB_CPUCORES_FOUND OR NOT TUKLIB_PHYSMEM_FOUND)
740 # Use SEND_ERROR instead of FATAL_ERROR. If someone reports a bug,
741 # seeing the results of the remaining checks can be useful too.
743 "tuklib_cpucores() or tuklib_physmem() failed. "
744 "Unless you really are building for a system where these "
745 "modules are not supported (unlikely), this is a bug in the "
746 "included cmake/tuklib_*.cmake files that should be fixed. "
747 "To build anyway, edit this CMakeLists.txt to ignore this error.")
750 # Check for __attribute__((__constructor__)) support.
751 # This needs -Werror because some compilers just warn
752 # about this being unsupported.
753 cmake_push_check_state()
754 set(CMAKE_REQUIRED_FLAGS "-Werror")
755 check_c_source_compiles("
756 __attribute__((__constructor__))
757 static void my_constructor_func(void) { return; }
758 int main(void) { return 0; }
760 HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
761 cmake_pop_check_state()
762 tuklib_add_definition_if(liblzma HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
764 # The Win95 threading lacks a thread-safe one-time initialization function.
765 # The one-time initialization is needed for crc32_small.c and crc64_small.c
766 # create the CRC tables. So if small mode is enabled, the threading mode is
767 # win95, and the compiler does not support attribute constructor, then we
768 # would end up with a multithreaded build that is thread-unsafe. As a
769 # result this configuration is not allowed.
770 if(USE_WIN95_THREADS AND ENABLE_SMALL AND NOT HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
771 message(SEND_ERROR "Threading method win95 and ENABLE_SMALL "
772 "cannot be used at the same time with a compiler "
773 "that doesn't support "
774 "__attribute__((__constructor__))")
778 # Check for __attribute__((__ifunc__())) support.
779 option(ALLOW_ATTR_IFUNC "Allow use of __attribute__((__ifunc__())) if \
780 supported by the system" ON)
783 cmake_push_check_state()
784 set(CMAKE_REQUIRED_FLAGS "-Werror")
785 check_c_source_compiles("
786 static void func(void) { return; }
787 static void (*resolve_func(void)) (void) { return func; }
788 void func_ifunc(void)
789 __attribute__((__ifunc__(\"resolve_func\")));
790 int main(void) { return 0; }
792 HAVE_FUNC_ATTRIBUTE_IFUNC)
793 cmake_pop_check_state()
794 tuklib_add_definition_if(liblzma HAVE_FUNC_ATTRIBUTE_IFUNC)
798 check_include_file(cpuid.h HAVE_CPUID_H)
799 tuklib_add_definition_if(liblzma HAVE_CPUID_H)
802 check_include_file(immintrin.h HAVE_IMMINTRIN_H)
804 target_compile_definitions(liblzma PRIVATE HAVE_IMMINTRIN_H)
807 check_c_source_compiles("
808 #include <immintrin.h>
812 _mm_movemask_epi8(x);
816 HAVE__MM_MOVEMASK_EPI8)
817 tuklib_add_definition_if(liblzma HAVE__MM_MOVEMASK_EPI8)
820 check_c_source_compiles("
821 #include <immintrin.h>
822 #if defined(__e2k__) && __iset__ < 6
825 #if (defined(__GNUC__) || defined(__clang__)) && !defined(__EDG__)
826 __attribute__((__target__(\"ssse3,sse4.1,pclmul\")))
828 __m128i my_clmul(__m128i a)
830 const __m128i b = _mm_set_epi64x(1, 2);
831 return _mm_clmulepi64_si128(a, b, 0);
833 int main(void) { return 0; }
836 tuklib_add_definition_if(liblzma HAVE_USABLE_CLMUL)
839 # Support -fvisiblity=hidden when building shared liblzma.
840 # These lines do nothing on Windows (even under Cygwin).
841 # HAVE_VISIBILITY should always be defined to 0 or 1.
842 if(BUILD_SHARED_LIBS)
843 set_target_properties(liblzma PROPERTIES C_VISIBILITY_PRESET hidden)
844 target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=1)
846 target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=0)
850 if(BUILD_SHARED_LIBS)
851 # Add the Windows resource file for liblzma.dll.
852 target_sources(liblzma PRIVATE src/liblzma/liblzma_w32res.rc)
854 set_target_properties(liblzma PROPERTIES
855 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
858 # Export the public API symbols with __declspec(dllexport).
859 target_compile_definitions(liblzma PRIVATE DLL_EXPORT)
861 # Disable __declspec(dllimport) when linking against static liblzma.
862 target_compile_definitions(liblzma INTERFACE LZMA_API_STATIC)
864 elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
865 # GNU/Linux-specific symbol versioning for shared liblzma.
866 # Note that adding link options doesn't affect static builds
867 # but HAVE_SYMBOL_VERSIONS_LINUX must not be used with static builds
868 # because it would put symbol versions into the static library which
869 # can cause problems. It's clearer if all symver related things are
870 # omitted when not building a shared library.
872 # NOTE: Set it explicitly to 1 to make it clear that versioning is
873 # done unconditionally in the C files.
874 target_compile_definitions(liblzma PRIVATE HAVE_SYMBOL_VERSIONS_LINUX=1)
875 target_link_options(liblzma PRIVATE
876 "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
878 set_target_properties(liblzma PROPERTIES
879 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
881 elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
882 # Symbol versioning for shared liblzma for non-GNU/Linux.
883 # FIXME? What about Solaris?
884 target_link_options(liblzma PRIVATE
885 "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
887 set_target_properties(liblzma PROPERTIES
888 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
892 set_target_properties(liblzma PROPERTIES
893 # At least for now the package versioning matches the rules used for
894 # shared library versioning (excluding development releases) so it is
895 # fine to use the package version here.
896 SOVERSION "${xz_VERSION_MAJOR}"
897 VERSION "${xz_VERSION}"
899 # It's liblzma.so or liblzma.dll, not libliblzma.so or lzma.dll.
900 # Avoid the name lzma.dll because it would conflict with LZMA SDK.
904 # Create liblzma-config-version.cmake.
906 # FIXME: SameMajorVersion is correct for stable releases but it is wrong
907 # for development releases where each release may have incompatible changes.
908 include(CMakePackageConfigHelpers)
909 write_basic_package_version_file(
910 "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
911 VERSION "${liblzma_VERSION}"
912 COMPATIBILITY SameMajorVersion)
914 # Create liblzma-config.cmake. We use this spelling instead of
915 # liblzmaConfig.cmake to make find_package work in case insensitive
916 # manner even with case sensitive file systems. This gives more consistent
917 # behavior between operating systems. This optionally includes a dependency
918 # on a threading library, so the contents are created in two separate parts.
919 # The "second half" is always needed, so create it first.
920 set(LZMA_CONFIG_CONTENTS
921 "include(\"\${CMAKE_CURRENT_LIST_DIR}/liblzma-targets.cmake\")
923 if(NOT TARGET LibLZMA::LibLZMA)
924 # Be compatible with the spelling used by the FindLibLZMA module. This
925 # doesn't use ALIAS because it would make CMake resolve LibLZMA::LibLZMA
926 # to liblzma::liblzma instead of keeping the original spelling. Keeping
927 # the original spelling is important for good FindLibLZMA compatibility.
928 add_library(LibLZMA::LibLZMA INTERFACE IMPORTED)
929 set_target_properties(LibLZMA::LibLZMA PROPERTIES
930 INTERFACE_LINK_LIBRARIES liblzma::liblzma)
934 if(ENABLE_THREADS STREQUAL "posix")
935 set(LZMA_CONFIG_CONTENTS
936 "include(CMakeFindDependencyMacro)
937 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
938 find_dependency(Threads)
940 ${LZMA_CONFIG_CONTENTS}
944 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
945 "${LZMA_CONFIG_CONTENTS}")
947 # Set CMAKE_INSTALL_LIBDIR and friends.
948 include(GNUInstallDirs)
950 # Install the library binary. The INCLUDES specifies the include path that
951 # is exported for other projects to use but it doesn't install any files.
952 install(TARGETS liblzma EXPORT liblzmaTargets
953 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
954 COMPONENT liblzma_Runtime
955 LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
956 COMPONENT liblzma_Runtime
957 NAMELINK_COMPONENT liblzma_Development
958 ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
959 COMPONENT liblzma_Development
960 INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
962 # Install the liblzma API headers. These use a subdirectory so
963 # this has to be done as a separate step.
964 install(DIRECTORY src/liblzma/api/
965 COMPONENT liblzma_Development
966 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
967 FILES_MATCHING PATTERN "*.h")
969 # Install the CMake files that other packages can use to find liblzma.
970 set(liblzma_INSTALL_CMAKEDIR
971 "${CMAKE_INSTALL_LIBDIR}/cmake/liblzma"
972 CACHE STRING "Path to liblzma's .cmake files")
974 install(EXPORT liblzmaTargets
976 FILE liblzma-targets.cmake
977 DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
978 COMPONENT liblzma_Development)
980 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
981 "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
982 DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
983 COMPONENT liblzma_Development)
986 #############################################################################
988 #############################################################################
990 # The command line tools needs this.
991 check_symbol_exists(getopt_long getopt.h HAVE_GETOPT_LONG)
994 #############################################################################
996 #############################################################################
998 if(HAVE_GETOPT_LONG AND HAVE_DECODERS)
1000 src/common/sysdefs.h
1001 src/common/tuklib_common.h
1002 src/common/tuklib_config.h
1003 src/common/tuklib_exit.c
1004 src/common/tuklib_exit.h
1005 src/common/tuklib_gettext.h
1006 src/common/tuklib_progname.c
1007 src/common/tuklib_progname.h
1011 target_include_directories(xzdec PRIVATE
1016 target_link_libraries(xzdec PRIVATE liblzma)
1019 # Add the Windows resource file for xzdec.exe.
1020 target_sources(xzdec PRIVATE src/xzdec/xzdec_w32res.rc)
1021 set_target_properties(xzdec PROPERTIES
1022 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1026 tuklib_progname(xzdec)
1028 install(TARGETS xzdec
1029 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1033 install(FILES src/xzdec/xzdec.1
1034 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1040 #############################################################################
1042 #############################################################################
1044 if(NOT MSVC AND HAVE_GETOPT_LONG)
1046 src/common/mythread.h
1047 src/common/sysdefs.h
1048 src/common/tuklib_common.h
1049 src/common/tuklib_config.h
1050 src/common/tuklib_exit.c
1051 src/common/tuklib_exit.h
1052 src/common/tuklib_gettext.h
1053 src/common/tuklib_integer.h
1054 src/common/tuklib_mbstr.h
1055 src/common/tuklib_mbstr_fw.c
1056 src/common/tuklib_mbstr_width.c
1057 src/common/tuklib_open_stdxxx.c
1058 src/common/tuklib_open_stdxxx.h
1059 src/common/tuklib_progname.c
1060 src/common/tuklib_progname.h
1086 target_include_directories(xz PRIVATE
1092 target_sources(xz PRIVATE
1098 target_link_libraries(xz PRIVATE liblzma)
1100 target_compile_definitions(xz PRIVATE ASSUME_RAM=128)
1103 # Add the Windows resource file for xz.exe.
1104 target_sources(xz PRIVATE src/xz/xz_w32res.rc)
1105 set_target_properties(xz PROPERTIES
1106 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1113 check_symbol_exists(optreset getopt.h HAVE_OPTRESET)
1114 tuklib_add_definition_if(xz HAVE_OPTRESET)
1116 check_symbol_exists(posix_fadvise fcntl.h HAVE_POSIX_FADVISE)
1117 tuklib_add_definition_if(xz HAVE_POSIX_FADVISE)
1119 # How to get file time:
1120 check_struct_has_member("struct stat" st_atim.tv_nsec
1121 "sys/types.h;sys/stat.h"
1122 HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1123 if(HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1124 tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1126 check_struct_has_member("struct stat" st_atimespec.tv_nsec
1127 "sys/types.h;sys/stat.h"
1128 HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1129 if(HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1130 tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1132 check_struct_has_member("struct stat" st_atimensec
1133 "sys/types.h;sys/stat.h"
1134 HAVE_STRUCT_STAT_ST_ATIMENSEC)
1135 tuklib_add_definition_if(xz HAVE_STRUCT_STAT_ST_ATIMENSEC)
1139 # How to set file time:
1140 check_symbol_exists(futimens "sys/types.h;sys/stat.h" HAVE_FUTIMENS)
1142 tuklib_add_definitions(xz HAVE_FUTIMENS)
1144 check_symbol_exists(futimes "sys/time.h" HAVE_FUTIMES)
1146 tuklib_add_definitions(xz HAVE_FUTIMES)
1148 check_symbol_exists(futimesat "sys/time.h" HAVE_FUTIMESAT)
1150 tuklib_add_definitions(xz HAVE_FUTIMESAT)
1152 check_symbol_exists(utimes "sys/time.h" HAVE_UTIMES)
1154 tuklib_add_definitions(xz HAVE_UTIMES)
1156 check_symbol_exists(_futime "sys/utime.h" HAVE__FUTIME)
1158 tuklib_add_definitions(xz HAVE__FUTIME)
1160 check_symbol_exists(utime "utime.h" HAVE_UTIME)
1161 tuklib_add_definition_if(xz HAVE_UTIME)
1169 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1173 install(FILES src/xz/xz.1
1174 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1177 option(CREATE_XZ_SYMLINKS "Create unxz and xzcat symlinks" ON)
1178 option(CREATE_LZMA_SYMLINKS "Create lzma, unlzma, and lzcat symlinks"
1182 if(CREATE_XZ_SYMLINKS)
1183 list(APPEND XZ_LINKS "unxz" "xzcat")
1186 if(CREATE_LZMA_SYMLINKS)
1187 list(APPEND XZ_LINKS "lzma" "unlzma" "lzcat")
1190 # With Windows Cygwin and MSYS2 the symlinking is complicated. Both
1191 # of these environments set the UNIX variable so they will try to
1192 # make the symlinks. The ability for Cygwin and MSYS2 to make
1193 # broken symlinks is determined by the CYGWIN and MSYS2 environment
1194 # variables, respectively. Broken symlinks are needed for the man
1195 # page symlinks and for determining if the xz and lzma symlinks need
1196 # to depend on the xz target or not. If broken symlinks cannot be
1197 # made then the xz binary must be created before the symlinks.
1198 set(ALLOW_BROKEN_SYMLINKS ON)
1200 if(CMAKE_SYSTEM_NAME STREQUAL "CYGWIN")
1201 # The Cygwin env variable can be set to four possible values:
1203 # 1. "lnk". Create symlinks as Windows shortcuts.
1205 # 2. "native". Create symlinks as native Windows symlinks
1206 # if supported by the system. Fallback to "lnk" if native
1207 # symlinks are not supported.
1209 # 3. "nativestrict". Create symlinks as native Windows symlinks
1210 # if supported by the system. If the target of the symlink
1211 # does not exist or the creation of the symlink fails for any
1212 # reason, do not create the symlink.
1214 # 4. "sys". Create symlinks as plain files with a special
1215 # system attribute containing the path to the symlink target.
1217 # So, the only case we care about for broken symlinks is
1218 # "nativestrict" since all other values mean that broken
1219 # symlinks are allowed. If the env variable is not set the
1220 # default is "native". If the env variable is set but not
1221 # assigned one of the four values, then the default is the same
1222 # as option 1 "lnk".
1223 string(FIND "$ENV{CYGWIN}" "winsymlinks:nativestrict" SYMLINK_POS)
1224 if(SYMLINK_POS GREATER -1)
1225 set(ALLOW_BROKEN_SYMLINKS OFF)
1227 elseif(CMAKE_SYSTEM_NAME STREQUAL "MSYS")
1228 # The MSYS env variable behaves similar to the CYGWIN but has a
1229 # different default behavior. If winsymlinks is set but not
1230 # assigned one of the four supported values, the default is to
1231 # *copy* the target to the symlink destination. This will fail
1232 # if the target does not exist so broken symlinks cannot be
1234 string(FIND "$ENV{MSYS}" "winsymlinks" SYMLINK_POS)
1235 if(SYMLINK_POS GREATER -1)
1236 string(FIND "$ENV{MSYS}" "winsymlinks:nativestrict"
1238 if(SYMLINK_POS GREATER -1)
1239 set(ALLOW_BROKEN_SYMLINKS OFF)
1242 set(ALLOW_BROKEN_SYMLINKS OFF)
1246 # Create symlinks in the build directory and then install them.
1248 # The symlinks do not likely need any special extension since
1249 # even on Windows the symlink can still be executed without
1250 # the .exe extension.
1251 foreach(LINK IN LISTS XZ_LINKS)
1252 add_custom_target("${LINK}" ALL
1253 "${CMAKE_COMMAND}" -E create_symlink
1254 "$<TARGET_FILE_NAME:xz>" "${LINK}"
1255 BYPRODUCTS "${LINK}"
1257 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}"
1258 DESTINATION "${CMAKE_INSTALL_BINDIR}"
1261 # Only create the man page symlinks if the symlinks can be
1262 # created broken. The symlinks will not be valid until install
1263 # so they cannot be created on these system environments.
1264 if(ALLOW_BROKEN_SYMLINKS)
1265 add_custom_target("${LINK}.1" ALL
1266 "${CMAKE_COMMAND}" -E create_symlink "xz.1" "${LINK}.1"
1267 BYPRODUCTS "${LINK}.1"
1269 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}.1"
1270 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1273 # Add the xz target as dependency when broken symlinks
1274 # cannot be made. This ensures parallel builds do not fail
1275 # since it will enforce the order of creating xz first, then
1277 add_dependencies("${LINK}" xz)
1284 #############################################################################
1286 #############################################################################
1306 foreach(TEST IN LISTS LIBLZMA_TESTS)
1307 add_executable("${TEST}" "tests/${TEST}.c")
1309 target_include_directories("${TEST}" PRIVATE
1316 target_link_libraries("${TEST}" PRIVATE liblzma)
1318 # Put the test programs into their own subdirectory so they don't
1319 # pollute the top-level dir which might contain xz and xzdec.
1320 set_target_properties("${TEST}" PROPERTIES
1321 RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tests_bin"
1324 add_test(NAME "${TEST}"
1325 COMMAND "${CMAKE_CURRENT_BINARY_DIR}/tests_bin/${TEST}"
1328 # Set srcdir environment variable so that the tests find their
1329 # input files from the source tree.
1331 # Set the return code for skipped tests to match Automake convention.
1332 set_tests_properties("${TEST}" PROPERTIES
1333 ENVIRONMENT "srcdir=${CMAKE_CURRENT_LIST_DIR}/tests"