liblzma: Avoid compiler warning without creating extra symbol.
[xz.git] / CMakeLists.txt
blobc8d52f2db4d8be20ab37e2fdff2cad999aa83c7a
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 sandboxing support
14 #   - No translations
16 # Other missing things:
17 #   - No xzgrep or other scripts or their symlinks
18 #   - No xz tests (liblzma tests only)
20 # NOTE: Even if the code compiles without warnings, the end result may be
21 # different than via ./configure. Specifically, the list of #defines
22 # may be different (if so, probably this CMakeLists.txt got them wrong).
24 # This file provides the following installation components (if you only
25 # need liblzma, install only its components!):
26 #   - liblzma_Runtime
27 #   - liblzma_Development
28 #   - xz (on some platforms only)
29 #   - xzdec (on some platforms only)
31 # To find the target liblzma::liblzma from other packages, use the CONFIG
32 # option with find_package() to avoid a conflict with the FindLibLZMA module
33 # with case-insensitive file systems. For example, to require liblzma 5.2.5
34 # or a newer compatible version:
36 #     find_package(liblzma 5.2.5 REQUIRED CONFIG)
37 #     target_link_libraries(my_application liblzma::liblzma)
39 #############################################################################
41 # Author: Lasse Collin
43 # This file has been put into the public domain.
44 # You can do whatever you want with this file.
46 #############################################################################
48 cmake_minimum_required(VERSION 3.13...3.27 FATAL_ERROR)
50 include(CMakePushCheckState)
51 include(CheckIncludeFile)
52 include(CheckSymbolExists)
53 include(CheckStructHasMember)
54 include(CheckCSourceCompiles)
55 include(cmake/tuklib_integer.cmake)
56 include(cmake/tuklib_cpucores.cmake)
57 include(cmake/tuklib_physmem.cmake)
58 include(cmake/tuklib_progname.cmake)
59 include(cmake/tuklib_mbstr.cmake)
61 # Get the package version from version.h into XZ_VERSION variable.
62 file(READ src/liblzma/api/lzma/version.h XZ_VERSION)
63 string(REGEX REPLACE
64 "^.*\n\
65 #define LZMA_VERSION_MAJOR ([0-9]+)\n\
66 .*\
67 #define LZMA_VERSION_MINOR ([0-9]+)\n\
68 .*\
69 #define LZMA_VERSION_PATCH ([0-9]+)\n\
70 .*$"
71        "\\1.\\2.\\3" XZ_VERSION "${XZ_VERSION}")
73 # Among other things, this gives us variables xz_VERSION and xz_VERSION_MAJOR.
74 project(xz VERSION "${XZ_VERSION}" LANGUAGES C)
76 # We need a compiler that supports enough C99 or newer (variable-length arrays
77 # aren't needed, those are optional in C17). Setting CMAKE_C_STANDARD here
78 # makes it the default for all targets. It doesn't affect the INTERFACE so
79 # liblzma::liblzma won't end up with INTERFACE_COMPILE_FEATURES "c_std_99"
80 # (the API headers are C89 and C++ compatible).
81 set(CMAKE_C_STANDARD 99)
82 set(CMAKE_C_STANDARD_REQUIRED ON)
84 # On Apple OSes, don't build executables as bundles:
85 set(CMAKE_MACOSX_BUNDLE OFF)
87 # windres from GNU binutils can be tricky with command line arguments
88 # that contain spaces or other funny characters. Unfortunately we need
89 # a space in PACKAGE_NAME. Using \x20 to encode the US-ASCII space seems
90 # to work in both cmd.exe and /bin/sh.
92 # However, even \x20 isn't enough in all situations, resulting in
93 # "syntax error" from windres. Using --use-temp-file prevents windres
94 # from using popen() and this seems to fix the problem.
96 # llvm-windres claims to be compatible with GNU windres but with that
97 # the \x20 results in "XZx20Utils" in the compiled binary. (At the
98 # same time it works correctly with clang (the C compiler).) The option
99 # --use-temp-file makes no difference.
101 # CMake 3.25 doesn't have CMAKE_RC_COMPILER_ID so we rely on
102 # CMAKE_C_COMPILER_ID. If Clang is used together with GNU windres
103 # then it will fail, but this way the risk of a bad string in
104 # the binary should be fairly low.
105 if(WIN32 AND CMAKE_C_COMPILER_ID STREQUAL "GNU")
106     # Use workarounds with GNU windres. The \x20 in PACKAGE_NAME works
107     # with gcc too so we don't need to worry how to pass different flags
108     # to windres and gcc.
109     string(APPEND CMAKE_RC_FLAGS " --use-temp-file")
110     set(PACKAGE_NAME "XZ\\x20Utils")
111 else()
112     # Elsewhere a space is safe. This also keeps things compatible with
113     # EBCDIC in case CMake-based build is ever done on such a system.
114     set(PACKAGE_NAME "XZ Utils")
115 endif()
117 # Definitions common to all targets:
118 add_compile_definitions(
119     # Package info:
120     PACKAGE_NAME="${PACKAGE_NAME}"
121     PACKAGE_BUGREPORT="xz@tukaani.org"
122     PACKAGE_URL="https://tukaani.org/xz/"
124     # Standard headers and types are available:
125     HAVE_STDBOOL_H
126     HAVE__BOOL
127     HAVE_STDINT_H
128     HAVE_INTTYPES_H
130     # Always enable CRC32 since liblzma should never build without it.
131     HAVE_CHECK_CRC32
133     # Disable assert() checks when no build type has been specified. Non-empty
134     # build types like "Release" and "Debug" handle this by default.
135     $<$<CONFIG:>:NDEBUG>
139 ######################
140 # System definitions #
141 ######################
143 # _GNU_SOURCE and such definitions. This specific macro is special since
144 # it also adds the definitions to CMAKE_REQUIRED_DEFINITIONS.
145 tuklib_use_system_extensions(ALL)
147 # This is needed by liblzma and xz.
148 tuklib_integer(ALL)
150 # Check for clock_gettime(). Do this before checking for threading so
151 # that we know there if CLOCK_MONOTONIC is available.
152 if(NOT WIN32)
153     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         # Add librt for the next check for HAVE_CLOCK_GETTIME. If it is
158         # found after including the library, we know that librt is required.
159         list(INSERT CMAKE_REQUIRED_LIBRARIES 0 rt)
160         check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME_LIBRT)
162         # If it was found now, add librt to all targets and keep it in
163         # CMAKE_REQUIRED_LIBRARIES for further tests too.
164         if(HAVE_CLOCK_GETTIME_LIBRT)
165             link_libraries(rt)
166         else()
167             list(REMOVE_AT CMAKE_REQUIRED_LIBRARIES 0)
168         endif()
169     endif()
171     if(HAVE_CLOCK_GETTIME OR HAVE_CLOCK_GETTIME_LIBRT)
172         add_compile_definitions(HAVE_CLOCK_GETTIME)
174         # Check if CLOCK_MONOTONIC is available for clock_gettime().
175         check_symbol_exists(CLOCK_MONOTONIC time.h HAVE_CLOCK_MONOTONIC)
176         tuklib_add_definition_if(ALL HAVE_CLOCK_MONOTONIC)
177     endif()
178 endif()
180 # Options for new enough GCC or Clang on any arch or operating system:
181 if(CMAKE_C_COMPILER_ID MATCHES GNU|Clang)
182     # configure.ac has a long list but it won't be copied here:
183     add_compile_options(-Wall -Wextra)
184 endif()
187 #############################################################################
188 # liblzma
189 #############################################################################
191 option(BUILD_SHARED_LIBS "Build liblzma as a shared library instead of static")
193 add_library(liblzma
194     src/common/mythread.h
195     src/common/sysdefs.h
196     src/common/tuklib_common.h
197     src/common/tuklib_config.h
198     src/common/tuklib_integer.h
199     src/common/tuklib_physmem.c
200     src/common/tuklib_physmem.h
201     src/liblzma/api/lzma.h
202     src/liblzma/api/lzma/base.h
203     src/liblzma/api/lzma/bcj.h
204     src/liblzma/api/lzma/block.h
205     src/liblzma/api/lzma/check.h
206     src/liblzma/api/lzma/container.h
207     src/liblzma/api/lzma/delta.h
208     src/liblzma/api/lzma/filter.h
209     src/liblzma/api/lzma/hardware.h
210     src/liblzma/api/lzma/index.h
211     src/liblzma/api/lzma/index_hash.h
212     src/liblzma/api/lzma/lzma12.h
213     src/liblzma/api/lzma/stream_flags.h
214     src/liblzma/api/lzma/version.h
215     src/liblzma/api/lzma/vli.h
216     src/liblzma/check/check.c
217     src/liblzma/check/check.h
218     src/liblzma/check/crc_macros.h
219     src/liblzma/common/block_util.c
220     src/liblzma/common/common.c
221     src/liblzma/common/common.h
222     src/liblzma/common/easy_preset.c
223     src/liblzma/common/easy_preset.h
224     src/liblzma/common/filter_common.c
225     src/liblzma/common/filter_common.h
226     src/liblzma/common/hardware_physmem.c
227     src/liblzma/common/index.c
228     src/liblzma/common/index.h
229     src/liblzma/common/memcmplen.h
230     src/liblzma/common/stream_flags_common.c
231     src/liblzma/common/stream_flags_common.h
232     src/liblzma/common/string_conversion.c
233     src/liblzma/common/vli_size.c
236 target_include_directories(liblzma PRIVATE
237     src/liblzma/api
238     src/liblzma/common
239     src/liblzma/check
240     src/liblzma/lz
241     src/liblzma/rangecoder
242     src/liblzma/lzma
243     src/liblzma/delta
244     src/liblzma/simple
245     src/common
249 ######################
250 # Size optimizations #
251 ######################
253 option(ENABLE_SMALL "Reduce code size at expense of speed. \
254 This may be useful together with CMAKE_BUILD_TYPE=MinSizeRel.")
256 if(ENABLE_SMALL)
257     add_compile_definitions(HAVE_SMALL)
258 endif()
261 ##########
262 # Checks #
263 ##########
265 set(ADDITIONAL_SUPPORTED_CHECKS crc64 sha256)
267 set(ADDITIONAL_CHECK_TYPES "${ADDITIONAL_SUPPORTED_CHECKS}" CACHE STRING
268     "Additional check types to support (crc32 is always built)")
270 foreach(CHECK IN LISTS ADDITIONAL_CHECK_TYPES)
271     if(NOT CHECK IN_LIST ADDITIONAL_SUPPORTED_CHECKS)
272         message(SEND_ERROR "'${CHECK}' is not a supported check type")
273     endif()
274 endforeach()
276 if(ENABLE_SMALL)
277     target_sources(liblzma PRIVATE src/liblzma/check/crc32_small.c)
278 else()
279     target_sources(liblzma PRIVATE
280         src/liblzma/check/crc32_fast.c
281         src/liblzma/check/crc32_table.c
282         src/liblzma/check/crc32_table_be.h
283         src/liblzma/check/crc32_table_le.h
284     )
285 endif()
287 if("crc64" IN_LIST ADDITIONAL_CHECK_TYPES)
288     add_compile_definitions("HAVE_CHECK_CRC64")
290     if(ENABLE_SMALL)
291         target_sources(liblzma PRIVATE src/liblzma/check/crc64_small.c)
292     else()
293         target_sources(liblzma PRIVATE
294             src/liblzma/check/crc64_fast.c
295             src/liblzma/check/crc64_table.c
296             src/liblzma/check/crc64_table_be.h
297             src/liblzma/check/crc64_table_le.h
298         )
299     endif()
300 endif()
302 if("sha256" IN_LIST ADDITIONAL_CHECK_TYPES)
303     add_compile_definitions("HAVE_CHECK_SHA256")
304     target_sources(liblzma PRIVATE src/liblzma/check/sha256.c)
305 endif()
308 #################
309 # Match finders #
310 #################
312 set(SUPPORTED_MATCH_FINDERS hc3 hc4 bt2 bt3 bt4)
314 set(MATCH_FINDERS "${SUPPORTED_MATCH_FINDERS}" CACHE STRING
315     "Match finders to support (at least one is required for LZMA1 or LZMA2)")
317 foreach(MF IN LISTS MATCH_FINDERS)
318     if(MF IN_LIST SUPPORTED_MATCH_FINDERS)
319         string(TOUPPER "${MF}" MF_UPPER)
320         add_compile_definitions("HAVE_MF_${MF_UPPER}")
321     else()
322         message(SEND_ERROR "'${MF}' is not a supported match finder")
323     endif()
324 endforeach()
327 #############
328 # Threading #
329 #############
331 # Supported thread methods:
332 # ON    - autodetect the best threading method. The autodetection will
333 #         prefer Windows threading (win95 or vista) over posix if both are
334 #         available. vista threads will be used over win95 unless it is a
335 #         32-bit build.
336 # OFF   - Disable threading.
337 # posix - Use posix threading, or throw an error if not available.
338 # win95 - Use Windows win95 threading, or throw an error if not available.
339 # vista - Use Windows vista threading, or throw an error if not available.
340 set(SUPPORTED_THREAD_METHODS ON OFF posix win95 vista)
342 set(ENABLE_THREADS ON CACHE STRING
343         "Threading method type to support. Set to 'OFF' to disable threading")
345 # Create dropdown in CMake GUI since only 1 threading method is possible
346 # to select in a build.
347 set_property(CACHE ENABLE_THREADS
348         PROPERTY STRINGS "${SUPPORTED_THREAD_METHODS}")
350 # This is a flag variable set when win95 threads are used. We must ensure
351 # the combination of enable_small and win95 threads is not used without a
352 # compiler supporting attribute __constructor__.
353 set(USE_WIN95_THREADS OFF)
355 if(NOT ENABLE_THREADS IN_LIST SUPPORTED_THREAD_METHODS)
356     message(SEND_ERROR "'${ENABLE_THREADS}' is not a supported thread type")
357 endif()
359 if(ENABLE_THREADS)
360     # Also set THREADS_PREFER_PTHREAD_FLAG since the flag has no effect
361     # for Windows threading.
362     set(THREADS_PREFER_PTHREAD_FLAG TRUE)
363     find_package(Threads REQUIRED)
365     # If both Windows and posix threading are available, prefer Windows.
366     # Note that on Cygwin CMAKE_USE_WIN32_THREADS_INIT is false.
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)
378         else()
379             add_compile_definitions(MYTHREAD_VISTA)
380         endif()
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
393             # use CLOCK_MONOTONIC.
394             if(HAVE_CLOCK_MONOTONIC)
395                 list(INSERT CMAKE_REQUIRED_LIBRARIES 0
396                      "${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)
400             endif()
401         else()
402             message(SEND_ERROR
403                     "Windows thread method requested, but a compatible "
404                     "library could not be found")
405         endif()
406     else()
407         message(SEND_ERROR "No supported threading library found")
408     endif()
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
416     )
417 endif()
420 ############
421 # Encoders #
422 ############
424 set(SIMPLE_FILTERS
425     x86
426     arm
427     armthumb
428     arm64
429     powerpc
430     ia64
431     sparc
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
438     lzma1
439     lzma2
440     delta
441     "${SIMPLE_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")
449 endif()
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 "
454                        "LZ-based encoder")
455 endif()
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)
467         endif()
469         string(TOUPPER "${ENCODER}" ENCODER_UPPER)
470         add_compile_definitions("HAVE_ENCODER_${ENCODER_UPPER}")
471     else()
472         message(SEND_ERROR "'${ENCODER}' is not a supported encoder")
473     endif()
474 endforeach()
476 if(HAVE_ENCODERS)
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
499     )
501     if(ENABLE_THREADS)
502         target_sources(liblzma PRIVATE
503             src/liblzma/common/stream_encoder_mt.c
504         )
505     endif()
507     if(SIMPLE_ENCODERS)
508         target_sources(liblzma PRIVATE
509             src/liblzma/simple/simple_encoder.c
510             src/liblzma/simple/simple_encoder.h
511         )
512     endif()
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
530         )
532         if(NOT ENABLE_SMALL)
533             target_sources(liblzma PRIVATE src/liblzma/lzma/fastpos_table.c)
534         endif()
535     endif()
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
541         )
542     endif()
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
549         )
550     endif()
551 endif()
554 ############
555 # Decoders #
556 ############
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)
569         endif()
571         string(TOUPPER "${DECODER}" DECODER_UPPER)
572         add_compile_definitions("HAVE_DECODER_${DECODER_UPPER}")
573     else()
574         message(SEND_ERROR "'${DECODER}' is not a supported decoder")
575     endif()
576 endforeach()
578 if(HAVE_DECODERS)
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
603     )
605     if(ENABLE_THREADS)
606         target_sources(liblzma PRIVATE
607             src/liblzma/common/stream_decoder_mt.c
608         )
609     endif()
611     if(SIMPLE_DECODERS)
612         target_sources(liblzma PRIVATE
613             src/liblzma/simple/simple_decoder.c
614             src/liblzma/simple/simple_decoder.h
615         )
616     endif()
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
625         )
626     endif()
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
632         )
633     endif()
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
640         )
641     endif()
642 endif()
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
651     )
652 endif()
654 if(HAVE_DELTA_CODER)
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
659     )
660 endif()
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
667     )
668 endif()
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")
673     endif()
674 endforeach()
677 #############
678 # MicroLZMA #
679 #############
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 "
690                            "MicroLZMA encoder")
691     endif()
693     target_sources(liblzma PRIVATE src/liblzma/common/microlzma_encoder.c)
694 endif()
696 if(MICROLZMA_DECODER)
697     if(NOT "lzma1" IN_LIST DECODERS)
698         message(SEND_ERROR "The LZMA1 decoder is required to support the "
699                            "MicroLZMA decoder")
700     endif()
702     target_sources(liblzma PRIVATE src/liblzma/common/microlzma_decoder.c)
703 endif()
706 #############################
707 # lzip (.lz) format support #
708 #############################
710 option(LZIP_DECODER "Support lzip decoder" ON)
712 if(LZIP_DECODER)
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 "
716                            "lzip decoder")
717     endif()
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
724     )
725 endif()
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.
744     message(SEND_ERROR
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.")
750 endif()
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; }
761     "
762     HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
763 cmake_pop_check_state()
764 tuklib_add_definition_if(liblzma HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
766 # The Win95 threading lacks a thread-safe one-time initialization function.
767 # The one-time initialization is needed for crc32_small.c and crc64_small.c
768 # create the CRC tables. So if small mode is enabled, the threading mode is
769 # win95, and the compiler does not support attribute constructor, then we
770 # would end up with a multithreaded build that is thread-unsafe. As a
771 # result this configuration is not allowed.
772 if(USE_WIN95_THREADS AND ENABLE_SMALL AND NOT HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
773     message(SEND_ERROR "Threading method win95 and ENABLE_SMALL "
774                         "cannot be used at the same time with a compiler "
775                         "that doesn't support "
776                         "__attribute__((__constructor__))")
777 endif()
780 # Check for __attribute__((__ifunc__())) support.
781 option(ALLOW_ATTR_IFUNC "Allow use of __attribute__((__ifunc__())) if \
782 supported by the system" ON)
784 if(ALLOW_ATTR_IFUNC)
785     cmake_push_check_state()
786     set(CMAKE_REQUIRED_FLAGS "-Werror")
787     check_c_source_compiles("
788             static void func(void) { return; }
789             static void (*resolve_func(void)) (void) { return func; }
790             void func_ifunc(void)
791                     __attribute__((__ifunc__(\"resolve_func\")));
792             int main(void) { return 0; }
793             /*
794              * 'clang -Wall' incorrectly warns that resolve_func is
795              * unused (-Wunused-function). Correct assembly output is
796              * still produced. This problem exists at least in Clang
797              * versions 4 to 17. The following silences the bogus warning:
798              */
799             void make_clang_quiet(void);
800             void make_clang_quiet(void) { resolve_func()(); }
801         "
802         HAVE_FUNC_ATTRIBUTE_IFUNC)
803     cmake_pop_check_state()
804     tuklib_add_definition_if(liblzma HAVE_FUNC_ATTRIBUTE_IFUNC)
805 endif()
807 # cpuid.h
808 check_include_file(cpuid.h HAVE_CPUID_H)
809 tuklib_add_definition_if(liblzma HAVE_CPUID_H)
811 # immintrin.h:
812 check_include_file(immintrin.h HAVE_IMMINTRIN_H)
813 if(HAVE_IMMINTRIN_H)
814     target_compile_definitions(liblzma PRIVATE HAVE_IMMINTRIN_H)
816     # SSE2 intrinsics:
817     check_c_source_compiles("
818             #include <immintrin.h>
819             int main(void)
820             {
821                 __m128i x = { 0 };
822                 _mm_movemask_epi8(x);
823                 return 0;
824             }
825         "
826         HAVE__MM_MOVEMASK_EPI8)
827     tuklib_add_definition_if(liblzma HAVE__MM_MOVEMASK_EPI8)
829     # CLMUL intrinsic:
830     check_c_source_compiles("
831             #include <immintrin.h>
832             #if defined(__e2k__) && __iset__ < 6
833             #   error
834             #endif
835             #if (defined(__GNUC__) || defined(__clang__)) && !defined(__EDG__)
836             __attribute__((__target__(\"ssse3,sse4.1,pclmul\")))
837             #endif
838             __m128i my_clmul(__m128i a)
839             {
840                 const __m128i b = _mm_set_epi64x(1, 2);
841                 return _mm_clmulepi64_si128(a, b, 0);
842             }
843             int main(void) { return 0; }
844     "
845     HAVE_USABLE_CLMUL)
846     tuklib_add_definition_if(liblzma HAVE_USABLE_CLMUL)
847 endif()
849 # Support -fvisiblity=hidden when building shared liblzma.
850 # These lines do nothing on Windows (even under Cygwin).
851 # HAVE_VISIBILITY should always be defined to 0 or 1.
852 if(BUILD_SHARED_LIBS)
853     set_target_properties(liblzma PROPERTIES C_VISIBILITY_PRESET hidden)
854     target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=1)
855 else()
856     target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=0)
857 endif()
859 if(WIN32)
860     if(BUILD_SHARED_LIBS)
861         # Add the Windows resource file for liblzma.dll.
862         target_sources(liblzma PRIVATE src/liblzma/liblzma_w32res.rc)
864         set_target_properties(liblzma PROPERTIES
865             LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
866         )
868         # Export the public API symbols with __declspec(dllexport).
869         target_compile_definitions(liblzma PRIVATE DLL_EXPORT)
870     else()
871         # Disable __declspec(dllimport) when linking against static liblzma.
872         target_compile_definitions(liblzma INTERFACE LZMA_API_STATIC)
873     endif()
874 elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
875     # GNU/Linux-specific symbol versioning for shared liblzma.
876     # Note that adding link options doesn't affect static builds
877     # but HAVE_SYMBOL_VERSIONS_LINUX must not be used with static builds
878     # because it would put symbol versions into the static library which
879     # can cause problems. It's clearer if all symver related things are
880     # omitted when not building a shared library.
881     #
882     # NOTE: Set it explicitly to 1 to make it clear that versioning is
883     # done unconditionally in the C files.
884     target_compile_definitions(liblzma PRIVATE HAVE_SYMBOL_VERSIONS_LINUX=1)
885     target_link_options(liblzma PRIVATE
886         "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
887     )
888     set_target_properties(liblzma PROPERTIES
889         LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
890     )
891 elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
892     # Symbol versioning for shared liblzma for non-GNU/Linux.
893     # FIXME? What about Solaris?
894     target_link_options(liblzma PRIVATE
895         "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
896     )
897     set_target_properties(liblzma PROPERTIES
898         LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
899     )
900 endif()
902 set_target_properties(liblzma PROPERTIES
903     # At least for now the package versioning matches the rules used for
904     # shared library versioning (excluding development releases) so it is
905     # fine to use the package version here.
906     SOVERSION "${xz_VERSION_MAJOR}"
907     VERSION "${xz_VERSION}"
909     # It's liblzma.so or liblzma.dll, not libliblzma.so or lzma.dll.
910     # Avoid the name lzma.dll because it would conflict with LZMA SDK.
911     PREFIX ""
914 # Create liblzma-config-version.cmake.
916 # FIXME: SameMajorVersion is correct for stable releases but it is wrong
917 # for development releases where each release may have incompatible changes.
918 include(CMakePackageConfigHelpers)
919 write_basic_package_version_file(
920     "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
921     VERSION "${liblzma_VERSION}"
922     COMPATIBILITY SameMajorVersion)
924 # Create liblzma-config.cmake. We use this spelling instead of
925 # liblzmaConfig.cmake to make find_package work in case insensitive
926 # manner even with case sensitive file systems. This gives more consistent
927 # behavior between operating systems. This optionally includes a dependency
928 # on a threading library, so the contents are created in two separate parts.
929 # The "second half" is always needed, so create it first.
930 set(LZMA_CONFIG_CONTENTS
931 "include(\"\${CMAKE_CURRENT_LIST_DIR}/liblzma-targets.cmake\")
933 if(NOT TARGET LibLZMA::LibLZMA)
934     # Be compatible with the spelling used by the FindLibLZMA module. This
935     # doesn't use ALIAS because it would make CMake resolve LibLZMA::LibLZMA
936     # to liblzma::liblzma instead of keeping the original spelling. Keeping
937     # the original spelling is important for good FindLibLZMA compatibility.
938     add_library(LibLZMA::LibLZMA INTERFACE IMPORTED)
939     set_target_properties(LibLZMA::LibLZMA PROPERTIES
940                           INTERFACE_LINK_LIBRARIES liblzma::liblzma)
941 endif()
944 if(ENABLE_THREADS STREQUAL "posix")
945     set(LZMA_CONFIG_CONTENTS
946 "include(CMakeFindDependencyMacro)
947 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
948 find_dependency(Threads)
950 ${LZMA_CONFIG_CONTENTS}
952 endif()
954 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
955         "${LZMA_CONFIG_CONTENTS}")
957 # Set CMAKE_INSTALL_LIBDIR and friends.
958 include(GNUInstallDirs)
960 # Install the library binary. The INCLUDES specifies the include path that
961 # is exported for other projects to use but it doesn't install any files.
962 install(TARGETS liblzma EXPORT liblzmaTargets
963         RUNTIME  DESTINATION "${CMAKE_INSTALL_BINDIR}"
964                  COMPONENT liblzma_Runtime
965         LIBRARY  DESTINATION "${CMAKE_INSTALL_LIBDIR}"
966                  COMPONENT liblzma_Runtime
967                  NAMELINK_COMPONENT liblzma_Development
968         ARCHIVE  DESTINATION "${CMAKE_INSTALL_LIBDIR}"
969                  COMPONENT liblzma_Development
970         INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
972 # Install the liblzma API headers. These use a subdirectory so
973 # this has to be done as a separate step.
974 install(DIRECTORY src/liblzma/api/
975         COMPONENT liblzma_Development
976         DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
977         FILES_MATCHING PATTERN "*.h")
979 # Install the CMake files that other packages can use to find liblzma.
980 set(liblzma_INSTALL_CMAKEDIR
981     "${CMAKE_INSTALL_LIBDIR}/cmake/liblzma"
982     CACHE STRING "Path to liblzma's .cmake files")
984 install(EXPORT liblzmaTargets
985         NAMESPACE liblzma::
986         FILE liblzma-targets.cmake
987         DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
988         COMPONENT liblzma_Development)
990 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
991               "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
992         DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
993         COMPONENT liblzma_Development)
996 #############################################################################
997 # libgnu (getopt_long)
998 #############################################################################
1000 # This mirrors how the Autotools build system handles the getopt_long
1001 # replacement, calling the object library libgnu since the replacement
1002 # version comes from Gnulib.
1003 add_library(libgnu OBJECT)
1005 # CMake requires that even an object library must have at least once source
1006 # file. So we give it a header file that results in no output files.
1007 target_sources(libgnu PRIVATE lib/getopt.in.h)
1009 # Create /lib directory in the build directory and add it to the include path.
1010 file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib")
1011 target_include_directories(libgnu PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/lib")
1013 # Include /lib from the source directory. It does no harm even if none of
1014 # the Gnulib replacements are used.
1015 target_include_directories(libgnu PUBLIC lib)
1017 # The command line tools need getopt_long in order to parse arguments. If
1018 # the system does not have a getopt_long implementation we can use the one
1019 # from Gnulib instead.
1020 check_symbol_exists(getopt_long getopt.h HAVE_GETOPT_LONG)
1022 if(NOT HAVE_GETOPT_LONG)
1023     # Set the __GETOPT_PREFIX definition to "rpl_" (replacement) to avoid
1024     # name conflicts with libc symbols. The same prefix is set if using
1025     # the Autotools build (m4/getopt.m4).
1026     target_compile_definitions(libgnu PUBLIC "__GETOPT_PREFIX=rpl_")
1028     # Create a custom copy command to copy the getopt header to the build
1029     # directory and re-copy it if it is updated. (Gnulib does it this way
1030     # because it allows choosing which .in.h files to actually use in the
1031     # build. We need just getopt.h so this is a bit overcomplicated for
1032     # a single header file only.)
1033     add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/lib/getopt.h"
1034         COMMAND "${CMAKE_COMMAND}" -E copy
1035             "${CMAKE_CURRENT_SOURCE_DIR}/lib/getopt.in.h"
1036             "${CMAKE_CURRENT_BINARY_DIR}/lib/getopt.h"
1037         MAIN_DEPENDENCY "${CMAKE_CURRENT_SOURCE_DIR}/lib/getopt.in.h"
1038         VERBATIM)
1040     target_sources(libgnu PRIVATE
1041         lib/getopt1.c
1042         lib/getopt.c
1043         lib/getopt_int.h
1044         lib/getopt-cdefs.h
1045         lib/getopt-core.h
1046         lib/getopt-ext.h
1047         lib/getopt-pfx-core.h
1048         lib/getopt-pfx-ext.h
1049         "${CMAKE_CURRENT_BINARY_DIR}/lib/getopt.h"
1050     )
1051 endif()
1054 #############################################################################
1055 # xzdec
1056 #############################################################################
1058 if(HAVE_DECODERS AND (NOT MSVC OR MSVC_VERSION GREATER_EQUAL 1900))
1059     add_executable(xzdec
1060         src/common/sysdefs.h
1061         src/common/tuklib_common.h
1062         src/common/tuklib_config.h
1063         src/common/tuklib_exit.c
1064         src/common/tuklib_exit.h
1065         src/common/tuklib_gettext.h
1066         src/common/tuklib_progname.c
1067         src/common/tuklib_progname.h
1068         src/xzdec/xzdec.c
1069     )
1071     target_include_directories(xzdec PRIVATE
1072         src/common
1073         src/liblzma/api
1074     )
1076     target_link_libraries(xzdec PRIVATE liblzma libgnu)
1078     if(WIN32)
1079         # Add the Windows resource file for xzdec.exe.
1080         target_sources(xzdec PRIVATE src/xzdec/xzdec_w32res.rc)
1081         set_target_properties(xzdec PROPERTIES
1082             LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1083         )
1084     endif()
1086     tuklib_progname(xzdec)
1088     install(TARGETS xzdec
1089             RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1090                     COMPONENT xzdec)
1092     if(UNIX)
1093         install(FILES src/xzdec/xzdec.1
1094                 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1095                 COMPONENT xzdec)
1096     endif()
1097 endif()
1100 #############################################################################
1101 # xz
1102 #############################################################################
1104 if(NOT MSVC OR MSVC_VERSION GREATER_EQUAL 1900)
1105     add_executable(xz
1106         src/common/mythread.h
1107         src/common/sysdefs.h
1108         src/common/tuklib_common.h
1109         src/common/tuklib_config.h
1110         src/common/tuklib_exit.c
1111         src/common/tuklib_exit.h
1112         src/common/tuklib_gettext.h
1113         src/common/tuklib_integer.h
1114         src/common/tuklib_mbstr.h
1115         src/common/tuklib_mbstr_fw.c
1116         src/common/tuklib_mbstr_width.c
1117         src/common/tuklib_open_stdxxx.c
1118         src/common/tuklib_open_stdxxx.h
1119         src/common/tuklib_progname.c
1120         src/common/tuklib_progname.h
1121         src/xz/args.c
1122         src/xz/args.h
1123         src/xz/coder.c
1124         src/xz/coder.h
1125         src/xz/file_io.c
1126         src/xz/file_io.h
1127         src/xz/hardware.c
1128         src/xz/hardware.h
1129         src/xz/main.c
1130         src/xz/main.h
1131         src/xz/message.c
1132         src/xz/message.h
1133         src/xz/mytime.c
1134         src/xz/mytime.h
1135         src/xz/options.c
1136         src/xz/options.h
1137         src/xz/private.h
1138         src/xz/signals.c
1139         src/xz/signals.h
1140         src/xz/suffix.c
1141         src/xz/suffix.h
1142         src/xz/util.c
1143         src/xz/util.h
1144     )
1146     target_include_directories(xz PRIVATE
1147         src/common
1148         src/liblzma/api
1149     )
1151     if(HAVE_DECODERS)
1152         target_sources(xz PRIVATE
1153             src/xz/list.c
1154             src/xz/list.h
1155         )
1156     endif()
1158     target_link_libraries(xz PRIVATE liblzma libgnu)
1160     target_compile_definitions(xz PRIVATE ASSUME_RAM=128)
1162     if(WIN32)
1163         # Add the Windows resource file for xz.exe.
1164         target_sources(xz PRIVATE src/xz/xz_w32res.rc)
1165         set_target_properties(xz PROPERTIES
1166             LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1167         )
1168     endif()
1170     tuklib_progname(xz)
1171     tuklib_mbstr(xz)
1173     check_symbol_exists(optreset getopt.h HAVE_OPTRESET)
1174     tuklib_add_definition_if(xz HAVE_OPTRESET)
1176     check_symbol_exists(posix_fadvise fcntl.h HAVE_POSIX_FADVISE)
1177     tuklib_add_definition_if(xz HAVE_POSIX_FADVISE)
1179     # How to get file time:
1180     check_struct_has_member("struct stat" st_atim.tv_nsec
1181                             "sys/types.h;sys/stat.h"
1182                             HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1183     if(HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1184         tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1185     else()
1186         check_struct_has_member("struct stat" st_atimespec.tv_nsec
1187                                 "sys/types.h;sys/stat.h"
1188                                 HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1189         if(HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1190             tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1191         else()
1192             check_struct_has_member("struct stat" st_atimensec
1193                                     "sys/types.h;sys/stat.h"
1194                                     HAVE_STRUCT_STAT_ST_ATIMENSEC)
1195             tuklib_add_definition_if(xz HAVE_STRUCT_STAT_ST_ATIMENSEC)
1196         endif()
1197     endif()
1199     # How to set file time:
1200     check_symbol_exists(futimens "sys/types.h;sys/stat.h" HAVE_FUTIMENS)
1201     if(HAVE_FUTIMENS)
1202         tuklib_add_definitions(xz HAVE_FUTIMENS)
1203     else()
1204         check_symbol_exists(futimes "sys/time.h" HAVE_FUTIMES)
1205         if(HAVE_FUTIMES)
1206             tuklib_add_definitions(xz HAVE_FUTIMES)
1207         else()
1208             check_symbol_exists(futimesat "sys/time.h" HAVE_FUTIMESAT)
1209             if(HAVE_FUTIMESAT)
1210                 tuklib_add_definitions(xz HAVE_FUTIMESAT)
1211             else()
1212                 check_symbol_exists(utimes "sys/time.h" HAVE_UTIMES)
1213                 if(HAVE_UTIMES)
1214                     tuklib_add_definitions(xz HAVE_UTIMES)
1215                 else()
1216                     check_symbol_exists(_futime "sys/utime.h" HAVE__FUTIME)
1217                     if(HAVE__FUTIME)
1218                         tuklib_add_definitions(xz HAVE__FUTIME)
1219                     else()
1220                         check_symbol_exists(utime "utime.h" HAVE_UTIME)
1221                         tuklib_add_definition_if(xz HAVE_UTIME)
1222                     endif()
1223                 endif()
1224             endif()
1225         endif()
1226     endif()
1228     install(TARGETS xz
1229             RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1230                     COMPONENT xz)
1232     if(UNIX)
1233         install(FILES src/xz/xz.1
1234                 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1235                 COMPONENT xz)
1237         option(CREATE_XZ_SYMLINKS "Create unxz and xzcat symlinks" ON)
1238         option(CREATE_LZMA_SYMLINKS "Create lzma, unlzma, and lzcat symlinks"
1239                ON)
1240         set(XZ_LINKS)
1242         if(CREATE_XZ_SYMLINKS)
1243             list(APPEND XZ_LINKS "unxz" "xzcat")
1244         endif()
1246         if(CREATE_LZMA_SYMLINKS)
1247             list(APPEND XZ_LINKS "lzma" "unlzma" "lzcat")
1248         endif()
1250         # With Windows Cygwin and MSYS2 the symlinking is complicated. Both
1251         # of these environments set the UNIX variable so they will try to
1252         # make the symlinks. The ability for Cygwin and MSYS2 to make
1253         # broken symlinks is determined by the CYGWIN and MSYS2 environment
1254         # variables, respectively. Broken symlinks are needed for the man
1255         # page symlinks and for determining if the xz and lzma symlinks need
1256         # to depend on the xz target or not. If broken symlinks cannot be
1257         # made then the xz binary must be created before the symlinks.
1258         set(ALLOW_BROKEN_SYMLINKS ON)
1260         if(CMAKE_SYSTEM_NAME STREQUAL "CYGWIN")
1261             # The Cygwin env variable can be set to four possible values:
1262             #
1263             # 1. "lnk". Create symlinks as Windows shortcuts.
1264             #
1265             # 2. "native". Create symlinks as native Windows symlinks
1266             #    if supported by the system. Fallback to "lnk" if native
1267             #    symlinks are not supported.
1268             #
1269             # 3. "nativestrict". Create symlinks as native Windows symlinks
1270             #    if supported by the system. If the target of the symlink
1271             #    does not exist or the creation of the symlink fails for any
1272             #    reason, do not create the symlink.
1273             #
1274             # 4. "sys". Create symlinks as plain files with a special
1275             #    system attribute containing the path to the symlink target.
1276             #
1277             # So, the only case we care about for broken symlinks is
1278             # "nativestrict" since all other values mean that broken
1279             # symlinks are allowed. If the env variable is not set the
1280             # default is "native". If the env variable is set but not
1281             # assigned one of the four values, then the default is the same
1282             # as option 1 "lnk".
1283             string(FIND "$ENV{CYGWIN}" "winsymlinks:nativestrict" SYMLINK_POS)
1284             if(SYMLINK_POS GREATER -1)
1285                 set(ALLOW_BROKEN_SYMLINKS OFF)
1286             endif()
1287         elseif(CMAKE_SYSTEM_NAME STREQUAL "MSYS")
1288             # The MSYS env variable behaves similar to the CYGWIN but has a
1289             # different default behavior. If winsymlinks is set but not
1290             # assigned one of the four supported values, the default is to
1291             # *copy* the target to the symlink destination. This will fail
1292             # if the target does not exist so broken symlinks cannot be
1293             # allowed.
1294             string(FIND "$ENV{MSYS}" "winsymlinks" SYMLINK_POS)
1295             if(SYMLINK_POS GREATER -1)
1296                 string(FIND "$ENV{MSYS}" "winsymlinks:nativestrict"
1297                         SYMLINK_POS)
1298                 if(SYMLINK_POS GREATER -1)
1299                     set(ALLOW_BROKEN_SYMLINKS OFF)
1300                 endif()
1301             else()
1302                 set(ALLOW_BROKEN_SYMLINKS OFF)
1303             endif()
1304         endif()
1306         # Create symlinks in the build directory and then install them.
1307         #
1308         # The symlinks do not likely need any special extension since
1309         # even on Windows the symlink can still be executed without
1310         # the .exe extension.
1311         foreach(LINK IN LISTS XZ_LINKS)
1312             add_custom_target("${LINK}" ALL
1313                 "${CMAKE_COMMAND}" -E create_symlink
1314                     "$<TARGET_FILE_NAME:xz>" "${LINK}"
1315                 BYPRODUCTS "${LINK}"
1316                 VERBATIM)
1317             install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}"
1318                     DESTINATION "${CMAKE_INSTALL_BINDIR}"
1319                     COMPONENT xz)
1321             # Only create the man page symlinks if the symlinks can be
1322             # created broken. The symlinks will not be valid until install
1323             # so they cannot be created on these system environments.
1324             if(ALLOW_BROKEN_SYMLINKS)
1325                 add_custom_target("${LINK}.1" ALL
1326                     "${CMAKE_COMMAND}" -E create_symlink "xz.1" "${LINK}.1"
1327                     BYPRODUCTS "${LINK}.1"
1328                     VERBATIM)
1329                 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}.1"
1330                         DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1331                         COMPONENT xz)
1332             else()
1333                 # Add the xz target as dependency when broken symlinks
1334                 # cannot be made. This ensures parallel builds do not fail
1335                 # since it will enforce the order of creating xz first, then
1336                 # the symlinks.
1337                 add_dependencies("${LINK}" xz)
1338             endif()
1339         endforeach()
1340     endif()
1341 endif()
1344 #############################################################################
1345 # Tests
1346 #############################################################################
1348 include(CTest)
1350 if(BUILD_TESTING)
1351     set(LIBLZMA_TESTS
1352         test_bcj_exact_size
1353         test_block_header
1354         test_check
1355         test_filter_flags
1356         test_filter_str
1357         test_hardware
1358         test_index
1359         test_index_hash
1360         test_lzip_decoder
1361         test_memlimit
1362         test_stream_flags
1363         test_vli
1364     )
1366     foreach(TEST IN LISTS LIBLZMA_TESTS)
1367         add_executable("${TEST}" "tests/${TEST}.c")
1369         target_include_directories("${TEST}" PRIVATE
1370             src/common
1371             src/liblzma/api
1372             src/liblzma
1373         )
1375         target_link_libraries("${TEST}" PRIVATE liblzma)
1377         # Put the test programs into their own subdirectory so they don't
1378         # pollute the top-level dir which might contain xz and xzdec.
1379         set_target_properties("${TEST}" PROPERTIES
1380             RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tests_bin"
1381         )
1383         add_test(NAME "${TEST}"
1384                  COMMAND "${CMAKE_CURRENT_BINARY_DIR}/tests_bin/${TEST}"
1385         )
1387         # Set srcdir environment variable so that the tests find their
1388         # input files from the source tree.
1389         #
1390         # Set the return code for skipped tests to match Automake convention.
1391         set_tests_properties("${TEST}" PROPERTIES
1392             ENVIRONMENT "srcdir=${CMAKE_CURRENT_LIST_DIR}/tests"
1393             SKIP_RETURN_CODE 77
1394         )
1395     endforeach()
1396 endif()