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(
186 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
187 find_package(Threads REQUIRED)
188 if(CMAKE_USE_WIN32_THREADS_INIT)
189 if(CMAKE_SIZEOF_VOID_P EQUAL 4)
190 # Define to 1 when using Windows 95 (and thus XP) compatible threads. This
191 # avoids use of features that were added in Windows Vista.
192 # This is used for 32-bit x86 builds for compatibility reasons since it
193 # makes no measurable difference in performance compared to Vista threads.
194 add_compile_definitions(MYTHREAD_WIN95)
196 # Define to 1 when using Windows Vista compatible threads. This uses features
197 # that are not available on Windows XP.
198 add_compile_definitions(MYTHREAD_VISTA)
201 add_compile_definitions(MYTHREAD_POSIX)
203 # Check if pthread_condattr_setclock() exists to use CLOCK_MONOTONIC.
204 if(HAVE_CLOCK_MONOTONIC)
205 list(INSERT CMAKE_REQUIRED_LIBRARIES 0 "${CMAKE_THREAD_LIBS_INIT}")
206 check_symbol_exists(pthread_condattr_setclock pthread.h
207 HAVE_PTHREAD_CONDATTR_SETCLOCK)
208 tuklib_add_definition_if(ALL HAVE_PTHREAD_CONDATTR_SETCLOCK)
212 # Options for new enough GCC or Clang on any arch or operating system:
213 if(CMAKE_C_COMPILER_ID MATCHES GNU|Clang)
214 # configure.ac has a long list but it won't be copied here:
215 add_compile_options(-Wall -Wextra)
219 #############################################################################
221 #############################################################################
223 option(BUILD_SHARED_LIBS "Build liblzma as a shared library instead of static")
226 src/common/mythread.h
228 src/common/tuklib_common.h
229 src/common/tuklib_config.h
230 src/common/tuklib_cpucores.c
231 src/common/tuklib_cpucores.h
232 src/common/tuklib_integer.h
233 src/common/tuklib_physmem.c
234 src/common/tuklib_physmem.h
235 src/liblzma/api/lzma.h
236 src/liblzma/api/lzma/base.h
237 src/liblzma/api/lzma/bcj.h
238 src/liblzma/api/lzma/block.h
239 src/liblzma/api/lzma/check.h
240 src/liblzma/api/lzma/container.h
241 src/liblzma/api/lzma/delta.h
242 src/liblzma/api/lzma/filter.h
243 src/liblzma/api/lzma/hardware.h
244 src/liblzma/api/lzma/index.h
245 src/liblzma/api/lzma/index_hash.h
246 src/liblzma/api/lzma/lzma12.h
247 src/liblzma/api/lzma/stream_flags.h
248 src/liblzma/api/lzma/version.h
249 src/liblzma/api/lzma/vli.h
250 src/liblzma/check/check.c
251 src/liblzma/check/check.h
252 src/liblzma/check/crc_macros.h
253 src/liblzma/common/block_util.c
254 src/liblzma/common/common.c
255 src/liblzma/common/common.h
256 src/liblzma/common/easy_preset.c
257 src/liblzma/common/easy_preset.h
258 src/liblzma/common/filter_common.c
259 src/liblzma/common/filter_common.h
260 src/liblzma/common/hardware_cputhreads.c
261 src/liblzma/common/hardware_physmem.c
262 src/liblzma/common/index.c
263 src/liblzma/common/index.h
264 src/liblzma/common/memcmplen.h
265 src/liblzma/common/outqueue.c
266 src/liblzma/common/outqueue.h
267 src/liblzma/common/stream_flags_common.c
268 src/liblzma/common/stream_flags_common.h
269 src/liblzma/common/string_conversion.c
270 src/liblzma/common/vli_size.c
273 target_include_directories(liblzma PRIVATE
278 src/liblzma/rangecoder
286 ######################
287 # Size optimizations #
288 ######################
290 option(ENABLE_SMALL "Reduce code size at expense of speed. \
291 This may be useful together with CMAKE_BUILD_TYPE=MinSizeRel.")
294 add_compile_definitions(HAVE_SMALL)
302 set(ADDITIONAL_SUPPORTED_CHECKS crc64 sha256)
304 set(ADDITIONAL_CHECK_TYPES "${ADDITIONAL_SUPPORTED_CHECKS}" CACHE STRING
305 "Additional check types to support (crc32 is always built)")
307 foreach(CHECK IN LISTS ADDITIONAL_CHECK_TYPES)
308 if(NOT CHECK IN_LIST ADDITIONAL_SUPPORTED_CHECKS)
309 message(SEND_ERROR "'${CHECK}' is not a supported check type")
314 target_sources(liblzma PRIVATE src/liblzma/check/crc32_small.c)
316 target_sources(liblzma PRIVATE
317 src/liblzma/check/crc32_fast.c
318 src/liblzma/check/crc32_table.c
319 src/liblzma/check/crc32_table_be.h
320 src/liblzma/check/crc32_table_le.h
324 if("crc64" IN_LIST ADDITIONAL_CHECK_TYPES)
325 add_compile_definitions("HAVE_CHECK_CRC64")
328 target_sources(liblzma PRIVATE src/liblzma/check/crc64_small.c)
330 target_sources(liblzma PRIVATE
331 src/liblzma/check/crc64_fast.c
332 src/liblzma/check/crc64_table.c
333 src/liblzma/check/crc64_table_be.h
334 src/liblzma/check/crc64_table_le.h
339 if("sha256" IN_LIST ADDITIONAL_CHECK_TYPES)
340 add_compile_definitions("HAVE_CHECK_SHA256")
341 target_sources(liblzma PRIVATE src/liblzma/check/sha256.c)
349 set(SUPPORTED_MATCH_FINDERS hc3 hc4 bt2 bt3 bt4)
351 set(MATCH_FINDERS "${SUPPORTED_MATCH_FINDERS}" CACHE STRING
352 "Match finders to support (at least one is required for LZMA1 or LZMA2)")
354 foreach(MF IN LISTS MATCH_FINDERS)
355 if(MF IN_LIST SUPPORTED_MATCH_FINDERS)
356 string(TOUPPER "${MF}" MF_UPPER)
357 add_compile_definitions("HAVE_MF_${MF_UPPER}")
359 message(SEND_ERROR "'${MF}' is not a supported match finder")
378 # The SUPPORTED_FILTERS are shared between Encoders and Decoders
379 # since only lzip does not appear in both lists. lzip is a special
380 # case anyway, so it is handled separately in the Decoders section.
381 set(SUPPORTED_FILTERS
388 set(ENCODERS "${SUPPORTED_FILTERS}" CACHE STRING "Encoders to support")
390 # If LZMA2 is enabled, then LZMA1 must also be enabled.
391 if(NOT "lzma1" IN_LIST ENCODERS AND "lzma2" IN_LIST ENCODERS)
392 message(SEND_ERROR "LZMA2 encoder requires that LZMA1 is also enabled")
395 # If LZMA1 is enabled, then at least one match finder must be enabled.
396 if(MATCH_FINDERS STREQUAL "" AND "lzma1" IN_LIST ENCODERS)
397 message(SEND_ERROR "At least 1 match finder is required for an "
401 set(HAVE_DELTA_CODER OFF)
402 set(SIMPLE_ENCODERS OFF)
403 set(HAVE_ENCODERS OFF)
405 foreach(ENCODER IN LISTS ENCODERS)
406 if(ENCODER IN_LIST SUPPORTED_FILTERS)
407 set(HAVE_ENCODERS ON)
409 if(NOT SIMPLE_ENCODERS AND ENCODER IN_LIST SIMPLE_FILTERS)
410 set(SIMPLE_ENCODERS ON)
413 string(TOUPPER "${ENCODER}" ENCODER_UPPER)
414 add_compile_definitions("HAVE_ENCODER_${ENCODER_UPPER}")
416 message(SEND_ERROR "'${ENCODER}' is not a supported encoder")
421 add_compile_definitions(HAVE_ENCODERS)
423 target_sources(liblzma PRIVATE
424 src/liblzma/common/alone_encoder.c
425 src/liblzma/common/block_buffer_encoder.c
426 src/liblzma/common/block_buffer_encoder.h
427 src/liblzma/common/block_encoder.c
428 src/liblzma/common/block_encoder.h
429 src/liblzma/common/block_header_encoder.c
430 src/liblzma/common/easy_buffer_encoder.c
431 src/liblzma/common/easy_encoder.c
432 src/liblzma/common/easy_encoder_memusage.c
433 src/liblzma/common/filter_buffer_encoder.c
434 src/liblzma/common/filter_encoder.c
435 src/liblzma/common/filter_encoder.h
436 src/liblzma/common/filter_flags_encoder.c
437 src/liblzma/common/index_encoder.c
438 src/liblzma/common/index_encoder.h
439 src/liblzma/common/stream_buffer_encoder.c
440 src/liblzma/common/stream_encoder.c
441 src/liblzma/common/stream_encoder_mt.c
442 src/liblzma/common/stream_flags_encoder.c
443 src/liblzma/common/vli_encoder.c
447 target_sources(liblzma PRIVATE
448 src/liblzma/simple/simple_encoder.c
449 src/liblzma/simple/simple_encoder.h
453 if("lzma1" IN_LIST ENCODERS)
454 target_sources(liblzma PRIVATE
455 src/liblzma/lzma/lzma_encoder.c
456 src/liblzma/lzma/lzma_encoder.h
457 src/liblzma/lzma/lzma_encoder_optimum_fast.c
458 src/liblzma/lzma/lzma_encoder_optimum_normal.c
459 src/liblzma/lzma/lzma_encoder_private.h
460 src/liblzma/lzma/fastpos.h
461 src/liblzma/lz/lz_encoder.c
462 src/liblzma/lz/lz_encoder.h
463 src/liblzma/lz/lz_encoder_hash.h
464 src/liblzma/lz/lz_encoder_hash_table.h
465 src/liblzma/lz/lz_encoder_mf.c
466 src/liblzma/rangecoder/price.h
467 src/liblzma/rangecoder/price_table.c
468 src/liblzma/rangecoder/range_encoder.h
472 target_sources(liblzma PRIVATE src/liblzma/lzma/fastpos_table.c)
476 if("lzma2" IN_LIST ENCODERS)
477 target_sources(liblzma PRIVATE
478 src/liblzma/lzma/lzma2_encoder.c
479 src/liblzma/lzma/lzma2_encoder.h
483 if("delta" IN_LIST ENCODERS)
484 set(HAVE_DELTA_CODER ON)
485 target_sources(liblzma PRIVATE
486 src/liblzma/delta/delta_encoder.c
487 src/liblzma/delta/delta_encoder.h
497 set(DECODERS "${SUPPORTED_FILTERS}" CACHE STRING "Decoders to support")
499 set(SIMPLE_DECODERS OFF)
500 set(HAVE_DECODERS OFF)
502 foreach(DECODER IN LISTS DECODERS)
503 if(DECODER IN_LIST SUPPORTED_FILTERS)
504 set(HAVE_DECODERS ON)
506 if(NOT SIMPLE_DECODERS AND DECODER IN_LIST SIMPLE_FILTERS)
507 set(SIMPLE_DECODERS ON)
510 string(TOUPPER "${DECODER}" DECODER_UPPER)
511 add_compile_definitions("HAVE_DECODER_${DECODER_UPPER}")
513 message(SEND_ERROR "'${DECODER}' is not a supported decoder")
518 add_compile_definitions(HAVE_DECODERS)
520 target_sources(liblzma PRIVATE
521 src/liblzma/common/alone_decoder.c
522 src/liblzma/common/alone_decoder.h
523 src/liblzma/common/auto_decoder.c
524 src/liblzma/common/block_buffer_decoder.c
525 src/liblzma/common/block_decoder.c
526 src/liblzma/common/block_decoder.h
527 src/liblzma/common/block_header_decoder.c
528 src/liblzma/common/easy_decoder_memusage.c
529 src/liblzma/common/file_info.c
530 src/liblzma/common/filter_buffer_decoder.c
531 src/liblzma/common/filter_decoder.c
532 src/liblzma/common/filter_decoder.h
533 src/liblzma/common/filter_flags_decoder.c
534 src/liblzma/common/index_decoder.c
535 src/liblzma/common/index_decoder.h
536 src/liblzma/common/index_hash.c
537 src/liblzma/common/stream_buffer_decoder.c
538 src/liblzma/common/stream_decoder.c
539 src/liblzma/common/stream_flags_decoder.c
540 src/liblzma/common/stream_decoder_mt.c
541 src/liblzma/common/stream_decoder.h
542 src/liblzma/common/vli_decoder.c
546 target_sources(liblzma PRIVATE
547 src/liblzma/simple/simple_decoder.c
548 src/liblzma/simple/simple_decoder.h
552 if("lzma1" IN_LIST DECODERS)
553 target_sources(liblzma PRIVATE
554 src/liblzma/lzma/lzma_decoder.c
555 src/liblzma/lzma/lzma_decoder.h
556 src/liblzma/rangecoder/range_decoder.h
557 src/liblzma/lz/lz_decoder.c
558 src/liblzma/lz/lz_decoder.h
562 if("lzma2" IN_LIST DECODERS)
563 target_sources(liblzma PRIVATE
564 src/liblzma/lzma/lzma2_decoder.c
565 src/liblzma/lzma/lzma2_decoder.h
569 if("delta" IN_LIST DECODERS)
570 set(HAVE_DELTA_CODER ON)
571 target_sources(liblzma PRIVATE
572 src/liblzma/delta/delta_decoder.c
573 src/liblzma/delta/delta_decoder.h
578 # Some sources must appear if the filter is configured as either
579 # an encoder or decoder.
580 if("lzma1" IN_LIST ENCODERS OR "lzma1" IN_LIST DECODERS)
581 target_sources(liblzma PRIVATE
582 src/liblzma/rangecoder/range_common.h
583 src/liblzma/lzma/lzma_encoder_presets.c
584 src/liblzma/lzma/lzma_common.h
589 target_sources(liblzma PRIVATE
590 src/liblzma/delta/delta_common.c
591 src/liblzma/delta/delta_common.h
592 src/liblzma/delta/delta_private.h
596 if(SIMPLE_ENCODERS OR SIMPLE_DECODERS)
597 target_sources(liblzma PRIVATE
598 src/liblzma/simple/simple_coder.c
599 src/liblzma/simple/simple_coder.h
600 src/liblzma/simple/simple_private.h
604 foreach(SIMPLE_CODER IN LISTS SIMPLE_FILTERS)
605 if(SIMPLE_CODER IN_LIST ENCODERS OR SIMPLE_CODER IN_LIST DECODERS)
606 target_sources(liblzma PRIVATE "src/liblzma/simple/${SIMPLE_CODER}.c")
615 option(MICROLZMA_ENCODER
616 "MicroLZMA encoder (needed by specific applications only)" ON)
618 option(MICROLZMA_DECODER
619 "MicroLZMA decoder (needed by specific applications only)" ON)
621 if(MICROLZMA_ENCODER)
622 if(NOT "lzma1" IN_LIST ENCODERS)
623 message(SEND_ERROR "The LZMA1 encoder is required to support the "
627 target_sources(liblzma PRIVATE src/liblzma/common/microlzma_encoder.c)
630 if(MICROLZMA_DECODER)
631 if(NOT "lzma1" IN_LIST DECODERS)
632 message(SEND_ERROR "The LZMA1 decoder is required to support the "
636 target_sources(liblzma PRIVATE src/liblzma/common/microlzma_decoder.c)
640 #############################
641 # lzip (.lz) format support #
642 #############################
644 option(LZIP_DECODER "Support lzip decoder" ON)
647 # If lzip decoder support is requested, make sure LZMA1 decoder is enabled.
648 if(NOT "lzma1" IN_LIST DECODERS)
649 message(SEND_ERROR "The LZMA1 decoder is required to support the "
653 add_compile_definitions(HAVE_LZIP_DECODER)
655 target_sources(liblzma PRIVATE
656 src/liblzma/common/lzip_decoder.c
657 src/liblzma/common/lzip_decoder.h
663 target_link_libraries(liblzma Threads::Threads)
665 # Put the tuklib functions under the lzma_ namespace.
666 target_compile_definitions(liblzma PRIVATE TUKLIB_SYMBOL_PREFIX=lzma_)
667 tuklib_cpucores(liblzma)
668 tuklib_physmem(liblzma)
670 # While liblzma can be built without tuklib_cpucores or tuklib_physmem
671 # modules, the liblzma API functions lzma_cputhreads() and lzma_physmem()
672 # will then be useless (which isn't too bad but still unfortunate). Since
673 # I expect the CMake-based builds to be only used on systems that are
674 # supported by these tuklib modules, problems with these tuklib modules
675 # are considered a hard error for now. This hopefully helps to catch bugs
676 # in the CMake versions of the tuklib checks.
677 if(NOT TUKLIB_CPUCORES_FOUND OR NOT TUKLIB_PHYSMEM_FOUND)
678 # Use SEND_ERROR instead of FATAL_ERROR. If someone reports a bug,
679 # seeing the results of the remaining checks can be useful too.
681 "tuklib_cpucores() or tuklib_physmem() failed. "
682 "Unless you really are building for a system where these "
683 "modules are not supported (unlikely), this is a bug in the "
684 "included cmake/tuklib_*.cmake files that should be fixed. "
685 "To build anyway, edit this CMakeLists.txt to ignore this error.")
688 # Check for __attribute__((__constructor__)) support.
689 # This needs -Werror because some compilers just warn
690 # about this being unsupported.
691 cmake_push_check_state()
692 set(CMAKE_REQUIRED_FLAGS "-Werror")
693 check_c_source_compiles("
694 __attribute__((__constructor__))
695 static void my_constructor_func(void) { return; }
696 int main(void) { return 0; }
698 HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
699 cmake_pop_check_state()
700 tuklib_add_definition_if(liblzma HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
703 check_include_file(cpuid.h HAVE_CPUID_H)
704 tuklib_add_definition_if(liblzma HAVE_CPUID_H)
707 check_include_file(immintrin.h HAVE_IMMINTRIN_H)
709 target_compile_definitions(liblzma PRIVATE HAVE_IMMINTRIN_H)
712 check_c_source_compiles("
713 #include <immintrin.h>
717 _mm_movemask_epi8(x);
721 HAVE__MM_MOVEMASK_EPI8)
722 tuklib_add_definition_if(liblzma HAVE__MM_MOVEMASK_EPI8)
725 check_c_source_compiles("
726 #include <immintrin.h>
727 #if defined(__e2k__) && __iset__ < 6
730 #if (defined(__GNUC__) || defined(__clang__)) && !defined(__EDG__)
731 __attribute__((__target__(\"ssse3,sse4.1,pclmul\")))
733 __m128i my_clmul(__m128i a)
735 const __m128i b = _mm_set_epi64x(1, 2);
736 return _mm_clmulepi64_si128(a, b, 0);
738 int main(void) { return 0; }
741 tuklib_add_definition_if(liblzma HAVE_USABLE_CLMUL)
744 # Support -fvisiblity=hidden when building shared liblzma.
745 # These lines do nothing on Windows (even under Cygwin).
746 # HAVE_VISIBILITY should always be defined to 0 or 1.
747 if(BUILD_SHARED_LIBS)
748 set_target_properties(liblzma PROPERTIES C_VISIBILITY_PRESET hidden)
749 target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=1)
751 target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=0)
755 if(BUILD_SHARED_LIBS)
756 # Add the Windows resource file for liblzma.dll.
757 target_sources(liblzma PRIVATE src/liblzma/liblzma_w32res.rc)
759 set_target_properties(liblzma PROPERTIES
760 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
763 # Export the public API symbols with __declspec(dllexport).
764 target_compile_definitions(liblzma PRIVATE DLL_EXPORT)
766 # Disable __declspec(dllimport) when linking against static liblzma.
767 target_compile_definitions(liblzma INTERFACE LZMA_API_STATIC)
769 elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
770 # GNU/Linux-specific symbol versioning for shared liblzma.
771 # Note that adding link options doesn't affect static builds
772 # but HAVE_SYMBOL_VERSIONS_LINUX must not be used with static builds
773 # because it would put symbol versions into the static library which
774 # can cause problems. It's clearer if all symver related things are
775 # omitted when not building a shared library.
777 # NOTE: Set it explicitly to 1 to make it clear that versioning is
778 # done unconditionally in the C files.
779 target_compile_definitions(liblzma PRIVATE HAVE_SYMBOL_VERSIONS_LINUX=1)
780 target_link_options(liblzma PRIVATE
781 "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
783 set_target_properties(liblzma PROPERTIES
784 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
786 elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
787 # Symbol versioning for shared liblzma for non-GNU/Linux.
788 # FIXME? What about Solaris?
789 target_link_options(liblzma PRIVATE
790 "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
792 set_target_properties(liblzma PROPERTIES
793 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
797 set_target_properties(liblzma PROPERTIES
798 # At least for now the package versioning matches the rules used for
799 # shared library versioning (excluding development releases) so it is
800 # fine to use the package version here.
801 SOVERSION "${xz_VERSION_MAJOR}"
802 VERSION "${xz_VERSION}"
804 # It's liblzma.so or liblzma.dll, not libliblzma.so or lzma.dll.
805 # Avoid the name lzma.dll because it would conflict with LZMA SDK.
809 # Create liblzma-config-version.cmake. We use this spelling instead of
810 # liblzmaConfig.cmake to make find_package work in case insensitive manner
811 # even with case sensitive file systems. This gives more consistent behavior
812 # between operating systems.
814 # FIXME: SameMajorVersion is correct for stable releases but it is wrong
815 # for development releases where each release may have incompatible changes.
816 include(CMakePackageConfigHelpers)
817 write_basic_package_version_file(
818 "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
819 VERSION "${liblzma_VERSION}"
820 COMPATIBILITY SameMajorVersion)
822 # Create liblzma-config.cmake.
823 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
824 "include(CMakeFindDependencyMacro)
825 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
826 find_dependency(Threads)
828 include(\"\${CMAKE_CURRENT_LIST_DIR}/liblzma-targets.cmake\")
830 # Be compatible with the spelling used by the FindLibLZMA module. This
831 # doesn't use ALIAS because it would make CMake resolve LibLZMA::LibLZMA
832 # to liblzma::liblzma instead of keeping the original spelling. Keeping
833 # the original spelling is important for good FindLibLZMA compatibility.
834 add_library(LibLZMA::LibLZMA INTERFACE IMPORTED)
835 set_target_properties(LibLZMA::LibLZMA PROPERTIES
836 INTERFACE_LINK_LIBRARIES liblzma::liblzma)
839 # Set CMAKE_INSTALL_LIBDIR and friends.
840 include(GNUInstallDirs)
842 # Install the library binary. The INCLUDES specifies the include path that
843 # is exported for other projects to use but it doesn't install any files.
844 install(TARGETS liblzma EXPORT liblzmaTargets
845 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
846 COMPONENT liblzma_Runtime
847 LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
848 COMPONENT liblzma_Runtime
849 NAMELINK_COMPONENT liblzma_Development
850 ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
851 COMPONENT liblzma_Development
852 INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
854 # Install the liblzma API headers. These use a subdirectory so
855 # this has to be done as a separate step.
856 install(DIRECTORY src/liblzma/api/
857 COMPONENT liblzma_Development
858 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
859 FILES_MATCHING PATTERN "*.h")
861 # Install the CMake files that other packages can use to find liblzma.
862 set(liblzma_INSTALL_CMAKEDIR
863 "${CMAKE_INSTALL_LIBDIR}/cmake/liblzma"
864 CACHE STRING "Path to liblzma's .cmake files")
866 install(EXPORT liblzmaTargets
868 FILE liblzma-targets.cmake
869 DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
870 COMPONENT liblzma_Development)
872 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
873 "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
874 DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
875 COMPONENT liblzma_Development)
878 #############################################################################
880 #############################################################################
882 # The command line tools needs this.
883 check_symbol_exists(getopt_long getopt.h HAVE_GETOPT_LONG)
886 #############################################################################
888 #############################################################################
893 src/common/tuklib_common.h
894 src/common/tuklib_config.h
895 src/common/tuklib_exit.c
896 src/common/tuklib_exit.h
897 src/common/tuklib_gettext.h
898 src/common/tuklib_progname.c
899 src/common/tuklib_progname.h
903 target_include_directories(xzdec PRIVATE
908 target_link_libraries(xzdec PRIVATE liblzma)
911 # Add the Windows resource file for xzdec.exe.
912 target_sources(xzdec PRIVATE src/xzdec/xzdec_w32res.rc)
913 set_target_properties(xzdec PROPERTIES
914 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
918 tuklib_progname(xzdec)
920 install(TARGETS xzdec
921 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
925 install(FILES src/xzdec/xzdec.1
926 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
932 #############################################################################
934 #############################################################################
936 if(NOT MSVC AND HAVE_GETOPT_LONG)
938 src/common/mythread.h
940 src/common/tuklib_common.h
941 src/common/tuklib_config.h
942 src/common/tuklib_exit.c
943 src/common/tuklib_exit.h
944 src/common/tuklib_gettext.h
945 src/common/tuklib_integer.h
946 src/common/tuklib_mbstr.h
947 src/common/tuklib_mbstr_fw.c
948 src/common/tuklib_mbstr_width.c
949 src/common/tuklib_open_stdxxx.c
950 src/common/tuklib_open_stdxxx.h
951 src/common/tuklib_progname.c
952 src/common/tuklib_progname.h
978 target_include_directories(xz PRIVATE
984 target_sources(xz PRIVATE
990 target_link_libraries(xz PRIVATE liblzma)
992 target_compile_definitions(xz PRIVATE ASSUME_RAM=128)
995 # Add the Windows resource file for xz.exe.
996 target_sources(xz PRIVATE src/xz/xz_w32res.rc)
997 set_target_properties(xz PROPERTIES
998 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1005 check_symbol_exists(optreset getopt.h HAVE_OPTRESET)
1006 tuklib_add_definition_if(xz HAVE_OPTRESET)
1008 check_symbol_exists(posix_fadvise fcntl.h HAVE_POSIX_FADVISE)
1009 tuklib_add_definition_if(xz HAVE_POSIX_FADVISE)
1011 # How to get file time:
1012 check_struct_has_member("struct stat" st_atim.tv_nsec
1013 "sys/types.h;sys/stat.h"
1014 HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1015 if(HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1016 tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1018 check_struct_has_member("struct stat" st_atimespec.tv_nsec
1019 "sys/types.h;sys/stat.h"
1020 HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1021 if(HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1022 tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1024 check_struct_has_member("struct stat" st_atimensec
1025 "sys/types.h;sys/stat.h"
1026 HAVE_STRUCT_STAT_ST_ATIMENSEC)
1027 tuklib_add_definition_if(xz HAVE_STRUCT_STAT_ST_ATIMENSEC)
1031 # How to set file time:
1032 check_symbol_exists(futimens "sys/types.h;sys/stat.h" HAVE_FUTIMENS)
1034 tuklib_add_definitions(xz HAVE_FUTIMENS)
1036 check_symbol_exists(futimes "sys/time.h" HAVE_FUTIMES)
1038 tuklib_add_definitions(xz HAVE_FUTIMES)
1040 check_symbol_exists(futimesat "sys/time.h" HAVE_FUTIMESAT)
1042 tuklib_add_definitions(xz HAVE_FUTIMESAT)
1044 check_symbol_exists(utimes "sys/time.h" HAVE_UTIMES)
1046 tuklib_add_definitions(xz HAVE_UTIMES)
1048 check_symbol_exists(_futime "sys/utime.h" HAVE__FUTIME)
1050 tuklib_add_definitions(xz HAVE__FUTIME)
1052 check_symbol_exists(utime "utime.h" HAVE_UTIME)
1053 tuklib_add_definition_if(xz HAVE_UTIME)
1061 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1065 install(FILES src/xz/xz.1
1066 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1069 option(CREATE_XZ_SYMLINKS "Create unxz and xzcat symlinks" ON)
1070 option(CREATE_LZMA_SYMLINKS "Create lzma, unlzma, and lzcat symlinks"
1074 if(CREATE_XZ_SYMLINKS)
1075 list(APPEND XZ_LINKS "unxz" "xzcat")
1078 if(CREATE_LZMA_SYMLINKS)
1079 list(APPEND XZ_LINKS "lzma" "unlzma" "lzcat")
1082 # Create symlinks in the build directory and then install them.
1084 # The symlinks do not likely need any special extension since
1085 # even on Windows the symlink can still be executed without
1086 # the .exe extension.
1087 foreach(LINK IN LISTS XZ_LINKS)
1088 add_custom_target("${LINK}" ALL
1089 "${CMAKE_COMMAND}" -E create_symlink
1090 "$<TARGET_FILE_NAME:xz>" "${LINK}"
1091 BYPRODUCTS "${LINK}"
1093 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}"
1094 DESTINATION "${CMAKE_INSTALL_BINDIR}"
1096 add_custom_target("${LINK}.1" ALL
1097 "${CMAKE_COMMAND}" -E create_symlink "xz.1" "${LINK}.1"
1098 BYPRODUCTS "${LINK}.1"
1100 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}.1"
1101 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1108 #############################################################################
1110 #############################################################################
1130 foreach(TEST IN LISTS LIBLZMA_TESTS)
1131 add_executable("${TEST}" "tests/${TEST}.c")
1133 target_include_directories("${TEST}" PRIVATE
1140 target_link_libraries("${TEST}" PRIVATE liblzma)
1142 # Put the test programs into their own subdirectory so they don't
1143 # pollute the top-level dir which might contain xz and xzdec.
1144 set_target_properties("${TEST}" PROPERTIES
1145 RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tests_bin"
1148 add_test(NAME "${TEST}"
1149 COMMAND "${CMAKE_CURRENT_BINARY_DIR}/tests_bin/${TEST}"
1152 # Set srcdir environment variable so that the tests find their
1153 # input files from the source tree.
1155 # Set the return code for skipped tests to match Automake convention.
1156 set_tests_properties("${TEST}" PROPERTIES
1157 ENVIRONMENT "srcdir=${CMAKE_CURRENT_LIST_DIR}/tests"