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.
153 if(NOT WIN32 AND NOT DEFINED HAVE_CLOCK_GETTIME)
154 check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME)
155 if(NOT HAVE_CLOCK_GETTIME)
156 # With glibc <= 2.17 or Solaris 10 this needs librt.
157 unset(HAVE_CLOCK_GETTIME CACHE)
159 list(INSERT CMAKE_REQUIRED_LIBRARIES 0 rt)
160 check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME)
162 # If it was found now, add it to all targets and keep it
163 # in CMAKE_REQUIRED_LIBRARIES for further tests too.
164 if(HAVE_CLOCK_GETTIME)
167 list(REMOVE_AT CMAKE_REQUIRED_LIBRARIES 0)
170 if(HAVE_CLOCK_GETTIME)
171 # Check if CLOCK_MONOTONIC is available for clock_gettime().
172 check_symbol_exists(CLOCK_MONOTONIC time.h HAVE_CLOCK_MONOTONIC)
174 add_compile_definitions(
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 if(NOT ENABLE_THREADS IN_LIST SUPPORTED_THREAD_METHODS)
352 message(SEND_ERROR "'${ENABLE_THREADS}' is not a supported thread type")
356 # Also set THREADS_PREFER_PTHREAD_FLAG since the flag has no effect
357 # for Windows threading.
358 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
359 find_package(Threads REQUIRED)
361 # If both Windows and posix threading are available, prefer Windows.
362 if(CMAKE_USE_WIN32_THREADS_INIT AND NOT ENABLE_THREADS STREQUAL "posix")
363 if(ENABLE_THREADS STREQUAL "win95"
364 OR (ENABLE_THREADS STREQUAL "ON"
365 AND CMAKE_SIZEOF_VOID_P EQUAL 4))
366 # Use Windows 95 (and thus XP) compatible threads.
367 # This avoids use of features that were added in
368 # Windows Vista. This is used for 32-bit x86 builds for
369 # compatibility reasons since it makes no measurable difference
370 # in performance compared to Vista threads.
372 # The Win95 threading lacks thread-safe one-time initialization
375 message(SEND_ERROR "Threading method win95 and ENABLE_SMALL "
376 "cannot be used at the same time")
379 add_compile_definitions(MYTHREAD_WIN95)
381 add_compile_definitions(MYTHREAD_VISTA)
383 elseif(CMAKE_USE_PTHREADS_INIT)
384 if(ENABLE_THREADS STREQUAL "posix" OR ENABLE_THREADS STREQUAL "ON")
385 # Overwrite ENABLE_THREADS in case it was set to "ON".
386 # The threading library only needs to be explicitly linked
387 # for posix threads, so this is needed for creating
388 # liblzma-config.cmake later.
389 set(ENABLE_THREADS "posix")
391 target_link_libraries(liblzma Threads::Threads)
392 add_compile_definitions(MYTHREAD_POSIX)
394 # Check if pthread_condattr_setclock() exists to use CLOCK_MONOTONIC.
395 if(HAVE_CLOCK_MONOTONIC)
396 list(INSERT CMAKE_REQUIRED_LIBRARIES 0 "${CMAKE_THREAD_LIBS_INIT}")
397 check_symbol_exists(pthread_condattr_setclock pthread.h
398 HAVE_PTHREAD_CONDATTR_SETCLOCK)
399 tuklib_add_definition_if(ALL HAVE_PTHREAD_CONDATTR_SETCLOCK)
403 "Windows thread method requested, but a compatible "
404 "library could not be found")
407 message(SEND_ERROR "No supported threading library found")
410 target_sources(liblzma PRIVATE
411 src/common/tuklib_cpucores.c
412 src/common/tuklib_cpucores.h
413 src/liblzma/common/hardware_cputhreads.c
414 src/liblzma/common/outqueue.c
415 src/liblzma/common/outqueue.h
434 # The SUPPORTED_FILTERS are shared between Encoders and Decoders
435 # since only lzip does not appear in both lists. lzip is a special
436 # case anyway, so it is handled separately in the Decoders section.
437 set(SUPPORTED_FILTERS
444 set(ENCODERS "${SUPPORTED_FILTERS}" CACHE STRING "Encoders to support")
446 # If LZMA2 is enabled, then LZMA1 must also be enabled.
447 if(NOT "lzma1" IN_LIST ENCODERS AND "lzma2" IN_LIST ENCODERS)
448 message(SEND_ERROR "LZMA2 encoder requires that LZMA1 is also enabled")
451 # If LZMA1 is enabled, then at least one match finder must be enabled.
452 if(MATCH_FINDERS STREQUAL "" AND "lzma1" IN_LIST ENCODERS)
453 message(SEND_ERROR "At least 1 match finder is required for an "
457 set(HAVE_DELTA_CODER OFF)
458 set(SIMPLE_ENCODERS OFF)
459 set(HAVE_ENCODERS OFF)
461 foreach(ENCODER IN LISTS ENCODERS)
462 if(ENCODER IN_LIST SUPPORTED_FILTERS)
463 set(HAVE_ENCODERS ON)
465 if(NOT SIMPLE_ENCODERS AND ENCODER IN_LIST SIMPLE_FILTERS)
466 set(SIMPLE_ENCODERS ON)
469 string(TOUPPER "${ENCODER}" ENCODER_UPPER)
470 add_compile_definitions("HAVE_ENCODER_${ENCODER_UPPER}")
472 message(SEND_ERROR "'${ENCODER}' is not a supported encoder")
477 add_compile_definitions(HAVE_ENCODERS)
479 target_sources(liblzma PRIVATE
480 src/liblzma/common/alone_encoder.c
481 src/liblzma/common/block_buffer_encoder.c
482 src/liblzma/common/block_buffer_encoder.h
483 src/liblzma/common/block_encoder.c
484 src/liblzma/common/block_encoder.h
485 src/liblzma/common/block_header_encoder.c
486 src/liblzma/common/easy_buffer_encoder.c
487 src/liblzma/common/easy_encoder.c
488 src/liblzma/common/easy_encoder_memusage.c
489 src/liblzma/common/filter_buffer_encoder.c
490 src/liblzma/common/filter_encoder.c
491 src/liblzma/common/filter_encoder.h
492 src/liblzma/common/filter_flags_encoder.c
493 src/liblzma/common/index_encoder.c
494 src/liblzma/common/index_encoder.h
495 src/liblzma/common/stream_buffer_encoder.c
496 src/liblzma/common/stream_encoder.c
497 src/liblzma/common/stream_flags_encoder.c
498 src/liblzma/common/vli_encoder.c
502 target_sources(liblzma PRIVATE
503 src/liblzma/common/stream_encoder_mt.c
508 target_sources(liblzma PRIVATE
509 src/liblzma/simple/simple_encoder.c
510 src/liblzma/simple/simple_encoder.h
514 if("lzma1" IN_LIST ENCODERS)
515 target_sources(liblzma PRIVATE
516 src/liblzma/lzma/lzma_encoder.c
517 src/liblzma/lzma/lzma_encoder.h
518 src/liblzma/lzma/lzma_encoder_optimum_fast.c
519 src/liblzma/lzma/lzma_encoder_optimum_normal.c
520 src/liblzma/lzma/lzma_encoder_private.h
521 src/liblzma/lzma/fastpos.h
522 src/liblzma/lz/lz_encoder.c
523 src/liblzma/lz/lz_encoder.h
524 src/liblzma/lz/lz_encoder_hash.h
525 src/liblzma/lz/lz_encoder_hash_table.h
526 src/liblzma/lz/lz_encoder_mf.c
527 src/liblzma/rangecoder/price.h
528 src/liblzma/rangecoder/price_table.c
529 src/liblzma/rangecoder/range_encoder.h
533 target_sources(liblzma PRIVATE src/liblzma/lzma/fastpos_table.c)
537 if("lzma2" IN_LIST ENCODERS)
538 target_sources(liblzma PRIVATE
539 src/liblzma/lzma/lzma2_encoder.c
540 src/liblzma/lzma/lzma2_encoder.h
544 if("delta" IN_LIST ENCODERS)
545 set(HAVE_DELTA_CODER ON)
546 target_sources(liblzma PRIVATE
547 src/liblzma/delta/delta_encoder.c
548 src/liblzma/delta/delta_encoder.h
558 set(DECODERS "${SUPPORTED_FILTERS}" CACHE STRING "Decoders to support")
560 set(SIMPLE_DECODERS OFF)
561 set(HAVE_DECODERS OFF)
563 foreach(DECODER IN LISTS DECODERS)
564 if(DECODER IN_LIST SUPPORTED_FILTERS)
565 set(HAVE_DECODERS ON)
567 if(NOT SIMPLE_DECODERS AND DECODER IN_LIST SIMPLE_FILTERS)
568 set(SIMPLE_DECODERS ON)
571 string(TOUPPER "${DECODER}" DECODER_UPPER)
572 add_compile_definitions("HAVE_DECODER_${DECODER_UPPER}")
574 message(SEND_ERROR "'${DECODER}' is not a supported decoder")
579 add_compile_definitions(HAVE_DECODERS)
581 target_sources(liblzma PRIVATE
582 src/liblzma/common/alone_decoder.c
583 src/liblzma/common/alone_decoder.h
584 src/liblzma/common/auto_decoder.c
585 src/liblzma/common/block_buffer_decoder.c
586 src/liblzma/common/block_decoder.c
587 src/liblzma/common/block_decoder.h
588 src/liblzma/common/block_header_decoder.c
589 src/liblzma/common/easy_decoder_memusage.c
590 src/liblzma/common/file_info.c
591 src/liblzma/common/filter_buffer_decoder.c
592 src/liblzma/common/filter_decoder.c
593 src/liblzma/common/filter_decoder.h
594 src/liblzma/common/filter_flags_decoder.c
595 src/liblzma/common/index_decoder.c
596 src/liblzma/common/index_decoder.h
597 src/liblzma/common/index_hash.c
598 src/liblzma/common/stream_buffer_decoder.c
599 src/liblzma/common/stream_decoder.c
600 src/liblzma/common/stream_flags_decoder.c
601 src/liblzma/common/stream_decoder.h
602 src/liblzma/common/vli_decoder.c
606 target_sources(liblzma PRIVATE
607 src/liblzma/common/stream_decoder_mt.c
612 target_sources(liblzma PRIVATE
613 src/liblzma/simple/simple_decoder.c
614 src/liblzma/simple/simple_decoder.h
618 if("lzma1" IN_LIST DECODERS)
619 target_sources(liblzma PRIVATE
620 src/liblzma/lzma/lzma_decoder.c
621 src/liblzma/lzma/lzma_decoder.h
622 src/liblzma/rangecoder/range_decoder.h
623 src/liblzma/lz/lz_decoder.c
624 src/liblzma/lz/lz_decoder.h
628 if("lzma2" IN_LIST DECODERS)
629 target_sources(liblzma PRIVATE
630 src/liblzma/lzma/lzma2_decoder.c
631 src/liblzma/lzma/lzma2_decoder.h
635 if("delta" IN_LIST DECODERS)
636 set(HAVE_DELTA_CODER ON)
637 target_sources(liblzma PRIVATE
638 src/liblzma/delta/delta_decoder.c
639 src/liblzma/delta/delta_decoder.h
644 # Some sources must appear if the filter is configured as either
645 # an encoder or decoder.
646 if("lzma1" IN_LIST ENCODERS OR "lzma1" IN_LIST DECODERS)
647 target_sources(liblzma PRIVATE
648 src/liblzma/rangecoder/range_common.h
649 src/liblzma/lzma/lzma_encoder_presets.c
650 src/liblzma/lzma/lzma_common.h
655 target_sources(liblzma PRIVATE
656 src/liblzma/delta/delta_common.c
657 src/liblzma/delta/delta_common.h
658 src/liblzma/delta/delta_private.h
662 if(SIMPLE_ENCODERS OR SIMPLE_DECODERS)
663 target_sources(liblzma PRIVATE
664 src/liblzma/simple/simple_coder.c
665 src/liblzma/simple/simple_coder.h
666 src/liblzma/simple/simple_private.h
670 foreach(SIMPLE_CODER IN LISTS SIMPLE_FILTERS)
671 if(SIMPLE_CODER IN_LIST ENCODERS OR SIMPLE_CODER IN_LIST DECODERS)
672 target_sources(liblzma PRIVATE "src/liblzma/simple/${SIMPLE_CODER}.c")
681 option(MICROLZMA_ENCODER
682 "MicroLZMA encoder (needed by specific applications only)" ON)
684 option(MICROLZMA_DECODER
685 "MicroLZMA decoder (needed by specific applications only)" ON)
687 if(MICROLZMA_ENCODER)
688 if(NOT "lzma1" IN_LIST ENCODERS)
689 message(SEND_ERROR "The LZMA1 encoder is required to support the "
693 target_sources(liblzma PRIVATE src/liblzma/common/microlzma_encoder.c)
696 if(MICROLZMA_DECODER)
697 if(NOT "lzma1" IN_LIST DECODERS)
698 message(SEND_ERROR "The LZMA1 decoder is required to support the "
702 target_sources(liblzma PRIVATE src/liblzma/common/microlzma_decoder.c)
706 #############################
707 # lzip (.lz) format support #
708 #############################
710 option(LZIP_DECODER "Support lzip decoder" ON)
713 # If lzip decoder support is requested, make sure LZMA1 decoder is enabled.
714 if(NOT "lzma1" IN_LIST DECODERS)
715 message(SEND_ERROR "The LZMA1 decoder is required to support the "
719 add_compile_definitions(HAVE_LZIP_DECODER)
721 target_sources(liblzma PRIVATE
722 src/liblzma/common/lzip_decoder.c
723 src/liblzma/common/lzip_decoder.h
729 # Put the tuklib functions under the lzma_ namespace.
730 target_compile_definitions(liblzma PRIVATE TUKLIB_SYMBOL_PREFIX=lzma_)
731 tuklib_cpucores(liblzma)
732 tuklib_physmem(liblzma)
734 # While liblzma can be built without tuklib_cpucores or tuklib_physmem
735 # modules, the liblzma API functions lzma_cputhreads() and lzma_physmem()
736 # will then be useless (which isn't too bad but still unfortunate). Since
737 # I expect the CMake-based builds to be only used on systems that are
738 # supported by these tuklib modules, problems with these tuklib modules
739 # are considered a hard error for now. This hopefully helps to catch bugs
740 # in the CMake versions of the tuklib checks.
741 if(NOT TUKLIB_CPUCORES_FOUND OR NOT TUKLIB_PHYSMEM_FOUND)
742 # Use SEND_ERROR instead of FATAL_ERROR. If someone reports a bug,
743 # seeing the results of the remaining checks can be useful too.
745 "tuklib_cpucores() or tuklib_physmem() failed. "
746 "Unless you really are building for a system where these "
747 "modules are not supported (unlikely), this is a bug in the "
748 "included cmake/tuklib_*.cmake files that should be fixed. "
749 "To build anyway, edit this CMakeLists.txt to ignore this error.")
752 # Check for __attribute__((__constructor__)) support.
753 # This needs -Werror because some compilers just warn
754 # about this being unsupported.
755 cmake_push_check_state()
756 set(CMAKE_REQUIRED_FLAGS "-Werror")
757 check_c_source_compiles("
758 __attribute__((__constructor__))
759 static void my_constructor_func(void) { return; }
760 int main(void) { return 0; }
762 HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
763 cmake_pop_check_state()
764 tuklib_add_definition_if(liblzma HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
767 check_include_file(cpuid.h HAVE_CPUID_H)
768 tuklib_add_definition_if(liblzma HAVE_CPUID_H)
771 check_include_file(immintrin.h HAVE_IMMINTRIN_H)
773 target_compile_definitions(liblzma PRIVATE HAVE_IMMINTRIN_H)
776 check_c_source_compiles("
777 #include <immintrin.h>
781 _mm_movemask_epi8(x);
785 HAVE__MM_MOVEMASK_EPI8)
786 tuklib_add_definition_if(liblzma HAVE__MM_MOVEMASK_EPI8)
789 check_c_source_compiles("
790 #include <immintrin.h>
791 #if defined(__e2k__) && __iset__ < 6
794 #if (defined(__GNUC__) || defined(__clang__)) && !defined(__EDG__)
795 __attribute__((__target__(\"ssse3,sse4.1,pclmul\")))
797 __m128i my_clmul(__m128i a)
799 const __m128i b = _mm_set_epi64x(1, 2);
800 return _mm_clmulepi64_si128(a, b, 0);
802 int main(void) { return 0; }
805 tuklib_add_definition_if(liblzma HAVE_USABLE_CLMUL)
808 # Support -fvisiblity=hidden when building shared liblzma.
809 # These lines do nothing on Windows (even under Cygwin).
810 # HAVE_VISIBILITY should always be defined to 0 or 1.
811 if(BUILD_SHARED_LIBS)
812 set_target_properties(liblzma PROPERTIES C_VISIBILITY_PRESET hidden)
813 target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=1)
815 target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=0)
819 if(BUILD_SHARED_LIBS)
820 # Add the Windows resource file for liblzma.dll.
821 target_sources(liblzma PRIVATE src/liblzma/liblzma_w32res.rc)
823 set_target_properties(liblzma PROPERTIES
824 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
827 # Export the public API symbols with __declspec(dllexport).
828 target_compile_definitions(liblzma PRIVATE DLL_EXPORT)
830 # Disable __declspec(dllimport) when linking against static liblzma.
831 target_compile_definitions(liblzma INTERFACE LZMA_API_STATIC)
833 elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
834 # GNU/Linux-specific symbol versioning for shared liblzma.
835 # Note that adding link options doesn't affect static builds
836 # but HAVE_SYMBOL_VERSIONS_LINUX must not be used with static builds
837 # because it would put symbol versions into the static library which
838 # can cause problems. It's clearer if all symver related things are
839 # omitted when not building a shared library.
841 # NOTE: Set it explicitly to 1 to make it clear that versioning is
842 # done unconditionally in the C files.
843 target_compile_definitions(liblzma PRIVATE HAVE_SYMBOL_VERSIONS_LINUX=1)
844 target_link_options(liblzma PRIVATE
845 "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
847 set_target_properties(liblzma PROPERTIES
848 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
850 elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
851 # Symbol versioning for shared liblzma for non-GNU/Linux.
852 # FIXME? What about Solaris?
853 target_link_options(liblzma PRIVATE
854 "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
856 set_target_properties(liblzma PROPERTIES
857 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
861 set_target_properties(liblzma PROPERTIES
862 # At least for now the package versioning matches the rules used for
863 # shared library versioning (excluding development releases) so it is
864 # fine to use the package version here.
865 SOVERSION "${xz_VERSION_MAJOR}"
866 VERSION "${xz_VERSION}"
868 # It's liblzma.so or liblzma.dll, not libliblzma.so or lzma.dll.
869 # Avoid the name lzma.dll because it would conflict with LZMA SDK.
873 # Create liblzma-config-version.cmake.
875 # FIXME: SameMajorVersion is correct for stable releases but it is wrong
876 # for development releases where each release may have incompatible changes.
877 include(CMakePackageConfigHelpers)
878 write_basic_package_version_file(
879 "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
880 VERSION "${liblzma_VERSION}"
881 COMPATIBILITY SameMajorVersion)
883 # Create liblzma-config.cmake. We use this spelling instead of
884 # liblzmaConfig.cmake to make find_package work in case insensitive
885 # manner even with case sensitive file systems. This gives more consistent
886 # behavior between operating systems. This optionally includes a dependency
887 # on a threading library, so the contents are created in two separate parts.
888 # The "second half" is always needed, so create it first.
889 set(LZMA_CONFIG_CONTENTS
890 "include(\"\${CMAKE_CURRENT_LIST_DIR}/liblzma-targets.cmake\")
892 # Be compatible with the spelling used by the FindLibLZMA module. This
893 # doesn't use ALIAS because it would make CMake resolve LibLZMA::LibLZMA
894 # to liblzma::liblzma instead of keeping the original spelling. Keeping
895 # the original spelling is important for good FindLibLZMA compatibility.
896 add_library(LibLZMA::LibLZMA INTERFACE IMPORTED)
897 set_target_properties(LibLZMA::LibLZMA PROPERTIES
898 INTERFACE_LINK_LIBRARIES liblzma::liblzma)
901 if(ENABLE_THREADS STREQUAL "posix")
902 set(LZMA_CONFIG_CONTENTS
903 "include(CMakeFindDependencyMacro)
904 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
905 find_dependency(Threads)
907 ${LZMA_CONFIG_CONTENTS}
911 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
912 "${LZMA_CONFIG_CONTENTS}")
914 # Set CMAKE_INSTALL_LIBDIR and friends.
915 include(GNUInstallDirs)
917 # Install the library binary. The INCLUDES specifies the include path that
918 # is exported for other projects to use but it doesn't install any files.
919 install(TARGETS liblzma EXPORT liblzmaTargets
920 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
921 COMPONENT liblzma_Runtime
922 LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
923 COMPONENT liblzma_Runtime
924 NAMELINK_COMPONENT liblzma_Development
925 ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
926 COMPONENT liblzma_Development
927 INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
929 # Install the liblzma API headers. These use a subdirectory so
930 # this has to be done as a separate step.
931 install(DIRECTORY src/liblzma/api/
932 COMPONENT liblzma_Development
933 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
934 FILES_MATCHING PATTERN "*.h")
936 # Install the CMake files that other packages can use to find liblzma.
937 set(liblzma_INSTALL_CMAKEDIR
938 "${CMAKE_INSTALL_LIBDIR}/cmake/liblzma"
939 CACHE STRING "Path to liblzma's .cmake files")
941 install(EXPORT liblzmaTargets
943 FILE liblzma-targets.cmake
944 DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
945 COMPONENT liblzma_Development)
947 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
948 "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
949 DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
950 COMPONENT liblzma_Development)
953 #############################################################################
955 #############################################################################
957 # The command line tools needs this.
958 check_symbol_exists(getopt_long getopt.h HAVE_GETOPT_LONG)
961 #############################################################################
963 #############################################################################
965 if(HAVE_GETOPT_LONG AND HAVE_DECODERS)
968 src/common/tuklib_common.h
969 src/common/tuklib_config.h
970 src/common/tuklib_exit.c
971 src/common/tuklib_exit.h
972 src/common/tuklib_gettext.h
973 src/common/tuklib_progname.c
974 src/common/tuklib_progname.h
978 target_include_directories(xzdec PRIVATE
983 target_link_libraries(xzdec PRIVATE liblzma)
986 # Add the Windows resource file for xzdec.exe.
987 target_sources(xzdec PRIVATE src/xzdec/xzdec_w32res.rc)
988 set_target_properties(xzdec PROPERTIES
989 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
993 tuklib_progname(xzdec)
995 install(TARGETS xzdec
996 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1000 install(FILES src/xzdec/xzdec.1
1001 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1007 #############################################################################
1009 #############################################################################
1011 if(NOT MSVC AND HAVE_GETOPT_LONG)
1013 src/common/mythread.h
1014 src/common/sysdefs.h
1015 src/common/tuklib_common.h
1016 src/common/tuklib_config.h
1017 src/common/tuklib_exit.c
1018 src/common/tuklib_exit.h
1019 src/common/tuklib_gettext.h
1020 src/common/tuklib_integer.h
1021 src/common/tuklib_mbstr.h
1022 src/common/tuklib_mbstr_fw.c
1023 src/common/tuklib_mbstr_width.c
1024 src/common/tuklib_open_stdxxx.c
1025 src/common/tuklib_open_stdxxx.h
1026 src/common/tuklib_progname.c
1027 src/common/tuklib_progname.h
1053 target_include_directories(xz PRIVATE
1059 target_sources(xz PRIVATE
1065 target_link_libraries(xz PRIVATE liblzma)
1067 target_compile_definitions(xz PRIVATE ASSUME_RAM=128)
1070 # Add the Windows resource file for xz.exe.
1071 target_sources(xz PRIVATE src/xz/xz_w32res.rc)
1072 set_target_properties(xz PROPERTIES
1073 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1080 check_symbol_exists(optreset getopt.h HAVE_OPTRESET)
1081 tuklib_add_definition_if(xz HAVE_OPTRESET)
1083 check_symbol_exists(posix_fadvise fcntl.h HAVE_POSIX_FADVISE)
1084 tuklib_add_definition_if(xz HAVE_POSIX_FADVISE)
1086 # How to get file time:
1087 check_struct_has_member("struct stat" st_atim.tv_nsec
1088 "sys/types.h;sys/stat.h"
1089 HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1090 if(HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1091 tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1093 check_struct_has_member("struct stat" st_atimespec.tv_nsec
1094 "sys/types.h;sys/stat.h"
1095 HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1096 if(HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1097 tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1099 check_struct_has_member("struct stat" st_atimensec
1100 "sys/types.h;sys/stat.h"
1101 HAVE_STRUCT_STAT_ST_ATIMENSEC)
1102 tuklib_add_definition_if(xz HAVE_STRUCT_STAT_ST_ATIMENSEC)
1106 # How to set file time:
1107 check_symbol_exists(futimens "sys/types.h;sys/stat.h" HAVE_FUTIMENS)
1109 tuklib_add_definitions(xz HAVE_FUTIMENS)
1111 check_symbol_exists(futimes "sys/time.h" HAVE_FUTIMES)
1113 tuklib_add_definitions(xz HAVE_FUTIMES)
1115 check_symbol_exists(futimesat "sys/time.h" HAVE_FUTIMESAT)
1117 tuklib_add_definitions(xz HAVE_FUTIMESAT)
1119 check_symbol_exists(utimes "sys/time.h" HAVE_UTIMES)
1121 tuklib_add_definitions(xz HAVE_UTIMES)
1123 check_symbol_exists(_futime "sys/utime.h" HAVE__FUTIME)
1125 tuklib_add_definitions(xz HAVE__FUTIME)
1127 check_symbol_exists(utime "utime.h" HAVE_UTIME)
1128 tuklib_add_definition_if(xz HAVE_UTIME)
1136 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1140 install(FILES src/xz/xz.1
1141 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1144 option(CREATE_XZ_SYMLINKS "Create unxz and xzcat symlinks" ON)
1145 option(CREATE_LZMA_SYMLINKS "Create lzma, unlzma, and lzcat symlinks"
1149 if(CREATE_XZ_SYMLINKS)
1150 list(APPEND XZ_LINKS "unxz" "xzcat")
1153 if(CREATE_LZMA_SYMLINKS)
1154 list(APPEND XZ_LINKS "lzma" "unlzma" "lzcat")
1157 # Create symlinks in the build directory and then install them.
1159 # The symlinks do not likely need any special extension since
1160 # even on Windows the symlink can still be executed without
1161 # the .exe extension.
1162 foreach(LINK IN LISTS XZ_LINKS)
1163 add_custom_target("${LINK}" ALL
1164 "${CMAKE_COMMAND}" -E create_symlink
1165 "$<TARGET_FILE_NAME:xz>" "${LINK}"
1166 BYPRODUCTS "${LINK}"
1168 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}"
1169 DESTINATION "${CMAKE_INSTALL_BINDIR}"
1171 add_custom_target("${LINK}.1" ALL
1172 "${CMAKE_COMMAND}" -E create_symlink "xz.1" "${LINK}.1"
1173 BYPRODUCTS "${LINK}.1"
1175 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}.1"
1176 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1183 #############################################################################
1185 #############################################################################
1205 foreach(TEST IN LISTS LIBLZMA_TESTS)
1206 add_executable("${TEST}" "tests/${TEST}.c")
1208 target_include_directories("${TEST}" PRIVATE
1215 target_link_libraries("${TEST}" PRIVATE liblzma)
1217 # Put the test programs into their own subdirectory so they don't
1218 # pollute the top-level dir which might contain xz and xzdec.
1219 set_target_properties("${TEST}" PROPERTIES
1220 RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tests_bin"
1223 add_test(NAME "${TEST}"
1224 COMMAND "${CMAKE_CURRENT_BINARY_DIR}/tests_bin/${TEST}"
1227 # Set srcdir environment variable so that the tests find their
1228 # input files from the source tree.
1230 # Set the return code for skipped tests to match Automake convention.
1231 set_tests_properties("${TEST}" PROPERTIES
1232 ENVIRONMENT "srcdir=${CMAKE_CURRENT_LIST_DIR}/tests"