liblzma: Add include guards to crc_common.h.
[xz.git] / CMakeLists.txt
blob6e3cd6f88ed3d1bfcfa2327172fdf097eea34278
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 from Clang/LLVM 16.0.6 and older: The \x20 results
97 # in "XZx20Utils" in the compiled binary. The option --use-temp-file
98 # makes no difference.
100 # llvm-windres 17.0.0 and later: It emulates GNU windres more accurately, so
101 # the workarounds used with GNU windres must be used with llvm-windres too.
103 # CMake 3.27 doesn't have CMAKE_RC_COMPILER_ID so we rely on
104 # CMAKE_C_COMPILER_ID.
105 if((MINGW OR CYGWIN OR MSYS) AND (
106         NOT CMAKE_C_COMPILER_ID STREQUAL "Clang" OR
107         CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "17"))
108     # Use workarounds with GNU windres and llvm-windres >= 17.0.0. The \x20
109     # in PACKAGE_NAME works with gcc and clang too so we don't need to worry
110     # how to pass different flags to windres and the C compiler.
111     string(APPEND CMAKE_RC_FLAGS " --use-temp-file")
112     set(PACKAGE_NAME "XZ\\x20Utils")
113 else()
114     # Elsewhere a space is safe. This also keeps things compatible with
115     # EBCDIC in case CMake-based build is ever done on such a system.
116     set(PACKAGE_NAME "XZ Utils")
117 endif()
119 # Definitions common to all targets:
120 add_compile_definitions(
121     # Package info:
122     PACKAGE_NAME="${PACKAGE_NAME}"
123     PACKAGE_BUGREPORT="xz@tukaani.org"
124     PACKAGE_URL="https://tukaani.org/xz/"
126     # Standard headers and types are available:
127     HAVE_STDBOOL_H
128     HAVE__BOOL
129     HAVE_STDINT_H
130     HAVE_INTTYPES_H
132     # Always enable CRC32 since liblzma should never build without it.
133     HAVE_CHECK_CRC32
135     # Disable assert() checks when no build type has been specified. Non-empty
136     # build types like "Release" and "Debug" handle this by default.
137     $<$<CONFIG:>:NDEBUG>
141 ######################
142 # System definitions #
143 ######################
145 # _GNU_SOURCE and such definitions. This specific macro is special since
146 # it also adds the definitions to CMAKE_REQUIRED_DEFINITIONS.
147 tuklib_use_system_extensions(ALL)
149 # This is needed by liblzma and xz.
150 tuklib_integer(ALL)
152 # Check for clock_gettime(). Do this before checking for threading so
153 # that we know there if CLOCK_MONOTONIC is available.
154 if(NOT WIN32)
155     check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME)
157     if(NOT HAVE_CLOCK_GETTIME)
158         # With glibc <= 2.17 or Solaris 10 this needs librt.
159         # Add librt for the next check for HAVE_CLOCK_GETTIME. If it is
160         # found after including the library, we know that librt is required.
161         list(INSERT CMAKE_REQUIRED_LIBRARIES 0 rt)
162         check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME_LIBRT)
164         # If it was found now, add librt to all targets and keep it in
165         # CMAKE_REQUIRED_LIBRARIES for further tests too.
166         if(HAVE_CLOCK_GETTIME_LIBRT)
167             link_libraries(rt)
168         else()
169             list(REMOVE_AT CMAKE_REQUIRED_LIBRARIES 0)
170         endif()
171     endif()
173     if(HAVE_CLOCK_GETTIME OR HAVE_CLOCK_GETTIME_LIBRT)
174         add_compile_definitions(HAVE_CLOCK_GETTIME)
176         # Check if CLOCK_MONOTONIC is available for clock_gettime().
177         check_symbol_exists(CLOCK_MONOTONIC time.h HAVE_CLOCK_MONOTONIC)
178         tuklib_add_definition_if(ALL HAVE_CLOCK_MONOTONIC)
179     endif()
180 endif()
182 # Options for new enough GCC or Clang on any arch or operating system:
183 if(CMAKE_C_COMPILER_ID MATCHES GNU|Clang)
184     # configure.ac has a long list but it won't be copied here:
185     add_compile_options(-Wall -Wextra)
186 endif()
189 #############################################################################
190 # liblzma
191 #############################################################################
193 option(BUILD_SHARED_LIBS "Build liblzma as a shared library instead of static")
195 add_library(liblzma
196     src/common/mythread.h
197     src/common/sysdefs.h
198     src/common/tuklib_common.h
199     src/common/tuklib_config.h
200     src/common/tuklib_integer.h
201     src/common/tuklib_physmem.c
202     src/common/tuklib_physmem.h
203     src/liblzma/api/lzma.h
204     src/liblzma/api/lzma/base.h
205     src/liblzma/api/lzma/bcj.h
206     src/liblzma/api/lzma/block.h
207     src/liblzma/api/lzma/check.h
208     src/liblzma/api/lzma/container.h
209     src/liblzma/api/lzma/delta.h
210     src/liblzma/api/lzma/filter.h
211     src/liblzma/api/lzma/hardware.h
212     src/liblzma/api/lzma/index.h
213     src/liblzma/api/lzma/index_hash.h
214     src/liblzma/api/lzma/lzma12.h
215     src/liblzma/api/lzma/stream_flags.h
216     src/liblzma/api/lzma/version.h
217     src/liblzma/api/lzma/vli.h
218     src/liblzma/check/check.c
219     src/liblzma/check/check.h
220     src/liblzma/check/crc_common.h
221     src/liblzma/common/block_util.c
222     src/liblzma/common/common.c
223     src/liblzma/common/common.h
224     src/liblzma/common/easy_preset.c
225     src/liblzma/common/easy_preset.h
226     src/liblzma/common/filter_common.c
227     src/liblzma/common/filter_common.h
228     src/liblzma/common/hardware_physmem.c
229     src/liblzma/common/index.c
230     src/liblzma/common/index.h
231     src/liblzma/common/memcmplen.h
232     src/liblzma/common/stream_flags_common.c
233     src/liblzma/common/stream_flags_common.h
234     src/liblzma/common/string_conversion.c
235     src/liblzma/common/vli_size.c
238 target_include_directories(liblzma PRIVATE
239     src/liblzma/api
240     src/liblzma/common
241     src/liblzma/check
242     src/liblzma/lz
243     src/liblzma/rangecoder
244     src/liblzma/lzma
245     src/liblzma/delta
246     src/liblzma/simple
247     src/common
251 ######################
252 # Size optimizations #
253 ######################
255 option(ENABLE_SMALL "Reduce code size at expense of speed. \
256 This may be useful together with CMAKE_BUILD_TYPE=MinSizeRel.")
258 if(ENABLE_SMALL)
259     add_compile_definitions(HAVE_SMALL)
260 endif()
263 ##########
264 # Checks #
265 ##########
267 set(ADDITIONAL_SUPPORTED_CHECKS crc64 sha256)
269 set(ADDITIONAL_CHECK_TYPES "${ADDITIONAL_SUPPORTED_CHECKS}" CACHE STRING
270     "Additional check types to support (crc32 is always built)")
272 foreach(CHECK IN LISTS ADDITIONAL_CHECK_TYPES)
273     if(NOT CHECK IN_LIST ADDITIONAL_SUPPORTED_CHECKS)
274         message(SEND_ERROR "'${CHECK}' is not a supported check type")
275     endif()
276 endforeach()
278 if(ENABLE_SMALL)
279     target_sources(liblzma PRIVATE src/liblzma/check/crc32_small.c)
280 else()
281     target_sources(liblzma PRIVATE
282         src/liblzma/check/crc32_fast.c
283         src/liblzma/check/crc32_table.c
284         src/liblzma/check/crc32_table_be.h
285         src/liblzma/check/crc32_table_le.h
286     )
287 endif()
289 if("crc64" IN_LIST ADDITIONAL_CHECK_TYPES)
290     add_compile_definitions("HAVE_CHECK_CRC64")
292     if(ENABLE_SMALL)
293         target_sources(liblzma PRIVATE src/liblzma/check/crc64_small.c)
294     else()
295         target_sources(liblzma PRIVATE
296             src/liblzma/check/crc64_fast.c
297             src/liblzma/check/crc64_table.c
298             src/liblzma/check/crc64_table_be.h
299             src/liblzma/check/crc64_table_le.h
300         )
301     endif()
302 endif()
304 if("sha256" IN_LIST ADDITIONAL_CHECK_TYPES)
305     add_compile_definitions("HAVE_CHECK_SHA256")
306     target_sources(liblzma PRIVATE src/liblzma/check/sha256.c)
307 endif()
310 #################
311 # Match finders #
312 #################
314 set(SUPPORTED_MATCH_FINDERS hc3 hc4 bt2 bt3 bt4)
316 set(MATCH_FINDERS "${SUPPORTED_MATCH_FINDERS}" CACHE STRING
317     "Match finders to support (at least one is required for LZMA1 or LZMA2)")
319 foreach(MF IN LISTS MATCH_FINDERS)
320     if(MF IN_LIST SUPPORTED_MATCH_FINDERS)
321         string(TOUPPER "${MF}" MF_UPPER)
322         add_compile_definitions("HAVE_MF_${MF_UPPER}")
323     else()
324         message(SEND_ERROR "'${MF}' is not a supported match finder")
325     endif()
326 endforeach()
329 #############
330 # Threading #
331 #############
333 # Supported thread methods:
334 # ON    - autodetect the best threading method. The autodetection will
335 #         prefer Windows threading (win95 or vista) over posix if both are
336 #         available. vista threads will be used over win95 unless it is a
337 #         32-bit build.
338 # OFF   - Disable threading.
339 # posix - Use posix threading, or throw an error if not available.
340 # win95 - Use Windows win95 threading, or throw an error if not available.
341 # vista - Use Windows vista threading, or throw an error if not available.
342 set(SUPPORTED_THREAD_METHODS ON OFF posix win95 vista)
344 set(ENABLE_THREADS ON CACHE STRING
345         "Threading method type to support. Set to 'OFF' to disable threading")
347 # Create dropdown in CMake GUI since only 1 threading method is possible
348 # to select in a build.
349 set_property(CACHE ENABLE_THREADS
350         PROPERTY STRINGS "${SUPPORTED_THREAD_METHODS}")
352 # This is a flag variable set when win95 threads are used. We must ensure
353 # the combination of enable_small and win95 threads is not used without a
354 # compiler supporting attribute __constructor__.
355 set(USE_WIN95_THREADS OFF)
357 if(NOT ENABLE_THREADS IN_LIST SUPPORTED_THREAD_METHODS)
358     message(SEND_ERROR "'${ENABLE_THREADS}' is not a supported thread type")
359 endif()
361 if(ENABLE_THREADS)
362     # Also set THREADS_PREFER_PTHREAD_FLAG since the flag has no effect
363     # for Windows threading.
364     set(THREADS_PREFER_PTHREAD_FLAG TRUE)
365     find_package(Threads REQUIRED)
367     # If both Windows and posix threading are available, prefer Windows.
368     # Note that on Cygwin CMAKE_USE_WIN32_THREADS_INIT is false.
369     if(CMAKE_USE_WIN32_THREADS_INIT AND NOT ENABLE_THREADS STREQUAL "posix")
370         if(ENABLE_THREADS STREQUAL "win95"
371                 OR (ENABLE_THREADS STREQUAL "ON"
372                 AND CMAKE_SIZEOF_VOID_P EQUAL 4))
373             # Use Windows 95 (and thus XP) compatible threads.
374             # This avoids use of features that were added in
375             # Windows Vista. This is used for 32-bit x86 builds for
376             # compatibility reasons since it makes no measurable difference
377             # in performance compared to Vista threads.
378             set(USE_WIN95_THREADS ON)
379             add_compile_definitions(MYTHREAD_WIN95)
380         else()
381             add_compile_definitions(MYTHREAD_VISTA)
382         endif()
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
395             # use CLOCK_MONOTONIC.
396             if(HAVE_CLOCK_MONOTONIC)
397                 list(INSERT CMAKE_REQUIRED_LIBRARIES 0
398                      "${CMAKE_THREAD_LIBS_INIT}")
399                 check_symbol_exists(pthread_condattr_setclock pthread.h
400                                     HAVE_PTHREAD_CONDATTR_SETCLOCK)
401                 tuklib_add_definition_if(ALL HAVE_PTHREAD_CONDATTR_SETCLOCK)
402             endif()
403         else()
404             message(SEND_ERROR
405                     "Windows thread method requested, but a compatible "
406                     "library could not be found")
407         endif()
408     else()
409         message(SEND_ERROR "No supported threading library found")
410     endif()
412     target_sources(liblzma PRIVATE
413         src/common/tuklib_cpucores.c
414         src/common/tuklib_cpucores.h
415         src/liblzma/common/hardware_cputhreads.c
416         src/liblzma/common/outqueue.c
417         src/liblzma/common/outqueue.h
418     )
419 endif()
422 ############
423 # Encoders #
424 ############
426 set(SIMPLE_FILTERS
427     x86
428     arm
429     armthumb
430     arm64
431     powerpc
432     ia64
433     sparc
436 # The SUPPORTED_FILTERS are shared between Encoders and Decoders
437 # since only lzip does not appear in both lists. lzip is a special
438 # case anyway, so it is handled separately in the Decoders section.
439 set(SUPPORTED_FILTERS
440     lzma1
441     lzma2
442     delta
443     "${SIMPLE_FILTERS}"
446 set(ENCODERS "${SUPPORTED_FILTERS}" CACHE STRING "Encoders to support")
448 # If LZMA2 is enabled, then LZMA1 must also be enabled.
449 if(NOT "lzma1" IN_LIST ENCODERS AND "lzma2" IN_LIST ENCODERS)
450     message(SEND_ERROR "LZMA2 encoder requires that LZMA1 is also enabled")
451 endif()
453 # If LZMA1 is enabled, then at least one match finder must be enabled.
454 if(MATCH_FINDERS STREQUAL "" AND "lzma1" IN_LIST ENCODERS)
455     message(SEND_ERROR "At least 1 match finder is required for an "
456                        "LZ-based encoder")
457 endif()
459 set(HAVE_DELTA_CODER OFF)
460 set(SIMPLE_ENCODERS OFF)
461 set(HAVE_ENCODERS OFF)
463 foreach(ENCODER IN LISTS ENCODERS)
464     if(ENCODER IN_LIST SUPPORTED_FILTERS)
465         set(HAVE_ENCODERS ON)
467         if(NOT SIMPLE_ENCODERS AND ENCODER IN_LIST SIMPLE_FILTERS)
468             set(SIMPLE_ENCODERS ON)
469         endif()
471         string(TOUPPER "${ENCODER}" ENCODER_UPPER)
472         add_compile_definitions("HAVE_ENCODER_${ENCODER_UPPER}")
473     else()
474         message(SEND_ERROR "'${ENCODER}' is not a supported encoder")
475     endif()
476 endforeach()
478 if(HAVE_ENCODERS)
479     add_compile_definitions(HAVE_ENCODERS)
481     target_sources(liblzma PRIVATE
482         src/liblzma/common/alone_encoder.c
483         src/liblzma/common/block_buffer_encoder.c
484         src/liblzma/common/block_buffer_encoder.h
485         src/liblzma/common/block_encoder.c
486         src/liblzma/common/block_encoder.h
487         src/liblzma/common/block_header_encoder.c
488         src/liblzma/common/easy_buffer_encoder.c
489         src/liblzma/common/easy_encoder.c
490         src/liblzma/common/easy_encoder_memusage.c
491         src/liblzma/common/filter_buffer_encoder.c
492         src/liblzma/common/filter_encoder.c
493         src/liblzma/common/filter_encoder.h
494         src/liblzma/common/filter_flags_encoder.c
495         src/liblzma/common/index_encoder.c
496         src/liblzma/common/index_encoder.h
497         src/liblzma/common/stream_buffer_encoder.c
498         src/liblzma/common/stream_encoder.c
499         src/liblzma/common/stream_flags_encoder.c
500         src/liblzma/common/vli_encoder.c
501     )
503     if(ENABLE_THREADS)
504         target_sources(liblzma PRIVATE
505             src/liblzma/common/stream_encoder_mt.c
506         )
507     endif()
509     if(SIMPLE_ENCODERS)
510         target_sources(liblzma PRIVATE
511             src/liblzma/simple/simple_encoder.c
512             src/liblzma/simple/simple_encoder.h
513         )
514     endif()
516     if("lzma1" IN_LIST ENCODERS)
517         target_sources(liblzma PRIVATE
518             src/liblzma/lzma/lzma_encoder.c
519             src/liblzma/lzma/lzma_encoder.h
520             src/liblzma/lzma/lzma_encoder_optimum_fast.c
521             src/liblzma/lzma/lzma_encoder_optimum_normal.c
522             src/liblzma/lzma/lzma_encoder_private.h
523             src/liblzma/lzma/fastpos.h
524             src/liblzma/lz/lz_encoder.c
525             src/liblzma/lz/lz_encoder.h
526             src/liblzma/lz/lz_encoder_hash.h
527             src/liblzma/lz/lz_encoder_hash_table.h
528             src/liblzma/lz/lz_encoder_mf.c
529             src/liblzma/rangecoder/price.h
530             src/liblzma/rangecoder/price_table.c
531             src/liblzma/rangecoder/range_encoder.h
532         )
534         if(NOT ENABLE_SMALL)
535             target_sources(liblzma PRIVATE src/liblzma/lzma/fastpos_table.c)
536         endif()
537     endif()
539     if("lzma2" IN_LIST ENCODERS)
540         target_sources(liblzma PRIVATE
541             src/liblzma/lzma/lzma2_encoder.c
542             src/liblzma/lzma/lzma2_encoder.h
543         )
544     endif()
546     if("delta" IN_LIST ENCODERS)
547         set(HAVE_DELTA_CODER ON)
548         target_sources(liblzma PRIVATE
549             src/liblzma/delta/delta_encoder.c
550             src/liblzma/delta/delta_encoder.h
551         )
552     endif()
553 endif()
556 ############
557 # Decoders #
558 ############
560 set(DECODERS "${SUPPORTED_FILTERS}" CACHE STRING "Decoders to support")
562 set(SIMPLE_DECODERS OFF)
563 set(HAVE_DECODERS OFF)
565 foreach(DECODER IN LISTS DECODERS)
566     if(DECODER IN_LIST SUPPORTED_FILTERS)
567         set(HAVE_DECODERS ON)
569         if(NOT SIMPLE_DECODERS AND DECODER IN_LIST SIMPLE_FILTERS)
570             set(SIMPLE_DECODERS ON)
571         endif()
573         string(TOUPPER "${DECODER}" DECODER_UPPER)
574         add_compile_definitions("HAVE_DECODER_${DECODER_UPPER}")
575     else()
576         message(SEND_ERROR "'${DECODER}' is not a supported decoder")
577     endif()
578 endforeach()
580 if(HAVE_DECODERS)
581     add_compile_definitions(HAVE_DECODERS)
583     target_sources(liblzma PRIVATE
584         src/liblzma/common/alone_decoder.c
585         src/liblzma/common/alone_decoder.h
586         src/liblzma/common/auto_decoder.c
587         src/liblzma/common/block_buffer_decoder.c
588         src/liblzma/common/block_decoder.c
589         src/liblzma/common/block_decoder.h
590         src/liblzma/common/block_header_decoder.c
591         src/liblzma/common/easy_decoder_memusage.c
592         src/liblzma/common/file_info.c
593         src/liblzma/common/filter_buffer_decoder.c
594         src/liblzma/common/filter_decoder.c
595         src/liblzma/common/filter_decoder.h
596         src/liblzma/common/filter_flags_decoder.c
597         src/liblzma/common/index_decoder.c
598         src/liblzma/common/index_decoder.h
599         src/liblzma/common/index_hash.c
600         src/liblzma/common/stream_buffer_decoder.c
601         src/liblzma/common/stream_decoder.c
602         src/liblzma/common/stream_flags_decoder.c
603         src/liblzma/common/stream_decoder.h
604         src/liblzma/common/vli_decoder.c
605     )
607     if(ENABLE_THREADS)
608         target_sources(liblzma PRIVATE
609             src/liblzma/common/stream_decoder_mt.c
610         )
611     endif()
613     if(SIMPLE_DECODERS)
614         target_sources(liblzma PRIVATE
615             src/liblzma/simple/simple_decoder.c
616             src/liblzma/simple/simple_decoder.h
617         )
618     endif()
620     if("lzma1" IN_LIST DECODERS)
621         target_sources(liblzma PRIVATE
622             src/liblzma/lzma/lzma_decoder.c
623             src/liblzma/lzma/lzma_decoder.h
624             src/liblzma/rangecoder/range_decoder.h
625             src/liblzma/lz/lz_decoder.c
626             src/liblzma/lz/lz_decoder.h
627         )
628     endif()
630     if("lzma2" IN_LIST DECODERS)
631         target_sources(liblzma PRIVATE
632             src/liblzma/lzma/lzma2_decoder.c
633             src/liblzma/lzma/lzma2_decoder.h
634         )
635     endif()
637     if("delta" IN_LIST DECODERS)
638         set(HAVE_DELTA_CODER ON)
639         target_sources(liblzma PRIVATE
640             src/liblzma/delta/delta_decoder.c
641             src/liblzma/delta/delta_decoder.h
642         )
643     endif()
644 endif()
646 # Some sources must appear if the filter is configured as either
647 # an encoder or decoder.
648 if("lzma1" IN_LIST ENCODERS OR "lzma1" IN_LIST DECODERS)
649     target_sources(liblzma PRIVATE
650         src/liblzma/rangecoder/range_common.h
651         src/liblzma/lzma/lzma_encoder_presets.c
652         src/liblzma/lzma/lzma_common.h
653     )
654 endif()
656 if(HAVE_DELTA_CODER)
657     target_sources(liblzma PRIVATE
658         src/liblzma/delta/delta_common.c
659         src/liblzma/delta/delta_common.h
660         src/liblzma/delta/delta_private.h
661     )
662 endif()
664 if(SIMPLE_ENCODERS OR SIMPLE_DECODERS)
665     target_sources(liblzma PRIVATE
666         src/liblzma/simple/simple_coder.c
667         src/liblzma/simple/simple_coder.h
668         src/liblzma/simple/simple_private.h
669     )
670 endif()
672 foreach(SIMPLE_CODER IN LISTS SIMPLE_FILTERS)
673     if(SIMPLE_CODER IN_LIST ENCODERS OR SIMPLE_CODER IN_LIST DECODERS)
674         target_sources(liblzma PRIVATE "src/liblzma/simple/${SIMPLE_CODER}.c")
675     endif()
676 endforeach()
679 #############
680 # MicroLZMA #
681 #############
683 option(MICROLZMA_ENCODER
684        "MicroLZMA encoder (needed by specific applications only)" ON)
686 option(MICROLZMA_DECODER
687        "MicroLZMA decoder (needed by specific applications only)" ON)
689 if(MICROLZMA_ENCODER)
690     if(NOT "lzma1" IN_LIST ENCODERS)
691         message(SEND_ERROR "The LZMA1 encoder is required to support the "
692                            "MicroLZMA encoder")
693     endif()
695     target_sources(liblzma PRIVATE src/liblzma/common/microlzma_encoder.c)
696 endif()
698 if(MICROLZMA_DECODER)
699     if(NOT "lzma1" IN_LIST DECODERS)
700         message(SEND_ERROR "The LZMA1 decoder is required to support the "
701                            "MicroLZMA decoder")
702     endif()
704     target_sources(liblzma PRIVATE src/liblzma/common/microlzma_decoder.c)
705 endif()
708 #############################
709 # lzip (.lz) format support #
710 #############################
712 option(LZIP_DECODER "Support lzip decoder" ON)
714 if(LZIP_DECODER)
715     # If lzip decoder support is requested, make sure LZMA1 decoder is enabled.
716     if(NOT "lzma1" IN_LIST DECODERS)
717         message(SEND_ERROR "The LZMA1 decoder is required to support the "
718                            "lzip decoder")
719     endif()
721     add_compile_definitions(HAVE_LZIP_DECODER)
723     target_sources(liblzma PRIVATE
724         src/liblzma/common/lzip_decoder.c
725         src/liblzma/common/lzip_decoder.h
726     )
727 endif()
731 # Put the tuklib functions under the lzma_ namespace.
732 target_compile_definitions(liblzma PRIVATE TUKLIB_SYMBOL_PREFIX=lzma_)
733 tuklib_cpucores(liblzma)
734 tuklib_physmem(liblzma)
736 # While liblzma can be built without tuklib_cpucores or tuklib_physmem
737 # modules, the liblzma API functions lzma_cputhreads() and lzma_physmem()
738 # will then be useless (which isn't too bad but still unfortunate). Since
739 # I expect the CMake-based builds to be only used on systems that are
740 # supported by these tuklib modules, problems with these tuklib modules
741 # are considered a hard error for now. This hopefully helps to catch bugs
742 # in the CMake versions of the tuklib checks.
743 if(NOT TUKLIB_CPUCORES_FOUND OR NOT TUKLIB_PHYSMEM_FOUND)
744     # Use SEND_ERROR instead of FATAL_ERROR. If someone reports a bug,
745     # seeing the results of the remaining checks can be useful too.
746     message(SEND_ERROR
747             "tuklib_cpucores() or tuklib_physmem() failed. "
748             "Unless you really are building for a system where these "
749             "modules are not supported (unlikely), this is a bug in the "
750             "included cmake/tuklib_*.cmake files that should be fixed. "
751             "To build anyway, edit this CMakeLists.txt to ignore this error.")
752 endif()
754 # Check for __attribute__((__constructor__)) support.
755 # This needs -Werror because some compilers just warn
756 # about this being unsupported.
757 cmake_push_check_state()
758 set(CMAKE_REQUIRED_FLAGS "-Werror")
759 check_c_source_compiles("
760         __attribute__((__constructor__))
761         static void my_constructor_func(void) { return; }
762         int main(void) { return 0; }
763     "
764     HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
765 cmake_pop_check_state()
766 tuklib_add_definition_if(liblzma HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
768 # The Win95 threading lacks a thread-safe one-time initialization function.
769 # The one-time initialization is needed for crc32_small.c and crc64_small.c
770 # create the CRC tables. So if small mode is enabled, the threading mode is
771 # win95, and the compiler does not support attribute constructor, then we
772 # would end up with a multithreaded build that is thread-unsafe. As a
773 # result this configuration is not allowed.
774 if(USE_WIN95_THREADS AND ENABLE_SMALL AND NOT HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
775     message(SEND_ERROR "Threading method win95 and ENABLE_SMALL "
776                         "cannot be used at the same time with a compiler "
777                         "that doesn't support "
778                         "__attribute__((__constructor__))")
779 endif()
782 # Check for __attribute__((__ifunc__())) support.
783 option(ALLOW_ATTR_IFUNC "Allow use of __attribute__((__ifunc__())) if \
784 supported by the system" ON)
786 if(ALLOW_ATTR_IFUNC)
787     cmake_push_check_state()
788     set(CMAKE_REQUIRED_FLAGS "-Werror")
789     check_c_source_compiles("
790             static void func(void) { return; }
791             static void (*resolve_func(void)) (void) { return func; }
792             void func_ifunc(void)
793                     __attribute__((__ifunc__(\"resolve_func\")));
794             int main(void) { return 0; }
795             /*
796              * 'clang -Wall' incorrectly warns that resolve_func is
797              * unused (-Wunused-function). Correct assembly output is
798              * still produced. This problem exists at least in Clang
799              * versions 4 to 17. The following silences the bogus warning:
800              */
801             void make_clang_quiet(void);
802             void make_clang_quiet(void) { resolve_func()(); }
803         "
804         HAVE_FUNC_ATTRIBUTE_IFUNC)
805     cmake_pop_check_state()
806     tuklib_add_definition_if(liblzma HAVE_FUNC_ATTRIBUTE_IFUNC)
807 endif()
809 # cpuid.h
810 check_include_file(cpuid.h HAVE_CPUID_H)
811 tuklib_add_definition_if(liblzma HAVE_CPUID_H)
813 # immintrin.h:
814 check_include_file(immintrin.h HAVE_IMMINTRIN_H)
815 if(HAVE_IMMINTRIN_H)
816     target_compile_definitions(liblzma PRIVATE HAVE_IMMINTRIN_H)
818     # SSE2 intrinsics:
819     check_c_source_compiles("
820             #include <immintrin.h>
821             int main(void)
822             {
823                 __m128i x = { 0 };
824                 _mm_movemask_epi8(x);
825                 return 0;
826             }
827         "
828         HAVE__MM_MOVEMASK_EPI8)
829     tuklib_add_definition_if(liblzma HAVE__MM_MOVEMASK_EPI8)
831     # CLMUL intrinsic:
832     check_c_source_compiles("
833             #include <immintrin.h>
834             #if defined(__e2k__) && __iset__ < 6
835             #   error
836             #endif
837             #if (defined(__GNUC__) || defined(__clang__)) && !defined(__EDG__)
838             __attribute__((__target__(\"ssse3,sse4.1,pclmul\")))
839             #endif
840             __m128i my_clmul(__m128i a)
841             {
842                 const __m128i b = _mm_set_epi64x(1, 2);
843                 return _mm_clmulepi64_si128(a, b, 0);
844             }
845             int main(void) { return 0; }
846     "
847     HAVE_USABLE_CLMUL)
849     if(HAVE_USABLE_CLMUL)
850         target_sources(liblzma PRIVATE src/liblzma/check/crc_clmul.c)
851         target_compile_definitions(liblzma PRIVATE HAVE_USABLE_CLMUL)
852     endif()
853 endif()
855 # Support -fvisiblity=hidden when building shared liblzma.
856 # These lines do nothing on Windows (even under Cygwin).
857 # HAVE_VISIBILITY should always be defined to 0 or 1.
858 if(BUILD_SHARED_LIBS)
859     set_target_properties(liblzma PROPERTIES C_VISIBILITY_PRESET hidden)
860     target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=1)
861 else()
862     target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=0)
863 endif()
865 if(WIN32)
866     if(BUILD_SHARED_LIBS)
867         # Add the Windows resource file for liblzma.dll.
868         target_sources(liblzma PRIVATE src/liblzma/liblzma_w32res.rc)
870         set_target_properties(liblzma PROPERTIES
871             LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
872         )
874         # Export the public API symbols with __declspec(dllexport).
875         target_compile_definitions(liblzma PRIVATE DLL_EXPORT)
876     else()
877         # Disable __declspec(dllimport) when linking against static liblzma.
878         target_compile_definitions(liblzma INTERFACE LZMA_API_STATIC)
879     endif()
880 elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
881     # GNU/Linux-specific symbol versioning for shared liblzma.
882     # Note that adding link options doesn't affect static builds
883     # but HAVE_SYMBOL_VERSIONS_LINUX must not be used with static builds
884     # because it would put symbol versions into the static library which
885     # can cause problems. It's clearer if all symver related things are
886     # omitted when not building a shared library.
887     #
888     # NOTE: Set it explicitly to 1 to make it clear that versioning is
889     # done unconditionally in the C files.
890     target_compile_definitions(liblzma PRIVATE HAVE_SYMBOL_VERSIONS_LINUX=1)
891     target_link_options(liblzma PRIVATE
892         "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
893     )
894     set_target_properties(liblzma PROPERTIES
895         LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
896     )
897 elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
898     # Symbol versioning for shared liblzma for non-GNU/Linux.
899     # FIXME? What about Solaris?
900     target_link_options(liblzma PRIVATE
901         "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
902     )
903     set_target_properties(liblzma PROPERTIES
904         LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
905     )
906 endif()
908 set_target_properties(liblzma PROPERTIES
909     # At least for now the package versioning matches the rules used for
910     # shared library versioning (excluding development releases) so it is
911     # fine to use the package version here.
912     SOVERSION "${xz_VERSION_MAJOR}"
913     VERSION "${xz_VERSION}"
915     # It's liblzma.so or liblzma.dll, not libliblzma.so or lzma.dll.
916     # Avoid the name lzma.dll because it would conflict with LZMA SDK.
917     PREFIX ""
920 # Create liblzma-config-version.cmake.
922 # FIXME: SameMajorVersion is correct for stable releases but it is wrong
923 # for development releases where each release may have incompatible changes.
924 include(CMakePackageConfigHelpers)
925 write_basic_package_version_file(
926     "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
927     VERSION "${liblzma_VERSION}"
928     COMPATIBILITY SameMajorVersion)
930 # Create liblzma-config.cmake. We use this spelling instead of
931 # liblzmaConfig.cmake to make find_package work in case insensitive
932 # manner even with case sensitive file systems. This gives more consistent
933 # behavior between operating systems. This optionally includes a dependency
934 # on a threading library, so the contents are created in two separate parts.
935 # The "second half" is always needed, so create it first.
936 set(LZMA_CONFIG_CONTENTS
937 "include(\"\${CMAKE_CURRENT_LIST_DIR}/liblzma-targets.cmake\")
939 if(NOT TARGET LibLZMA::LibLZMA)
940     # Be compatible with the spelling used by the FindLibLZMA module. This
941     # doesn't use ALIAS because it would make CMake resolve LibLZMA::LibLZMA
942     # to liblzma::liblzma instead of keeping the original spelling. Keeping
943     # the original spelling is important for good FindLibLZMA compatibility.
944     add_library(LibLZMA::LibLZMA INTERFACE IMPORTED)
945     set_target_properties(LibLZMA::LibLZMA PROPERTIES
946                           INTERFACE_LINK_LIBRARIES liblzma::liblzma)
947 endif()
950 if(ENABLE_THREADS STREQUAL "posix")
951     set(LZMA_CONFIG_CONTENTS
952 "include(CMakeFindDependencyMacro)
953 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
954 find_dependency(Threads)
956 ${LZMA_CONFIG_CONTENTS}
958 endif()
960 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
961         "${LZMA_CONFIG_CONTENTS}")
963 # Set CMAKE_INSTALL_LIBDIR and friends.
964 include(GNUInstallDirs)
966 # Install the library binary. The INCLUDES specifies the include path that
967 # is exported for other projects to use but it doesn't install any files.
968 install(TARGETS liblzma EXPORT liblzmaTargets
969         RUNTIME  DESTINATION "${CMAKE_INSTALL_BINDIR}"
970                  COMPONENT liblzma_Runtime
971         LIBRARY  DESTINATION "${CMAKE_INSTALL_LIBDIR}"
972                  COMPONENT liblzma_Runtime
973                  NAMELINK_COMPONENT liblzma_Development
974         ARCHIVE  DESTINATION "${CMAKE_INSTALL_LIBDIR}"
975                  COMPONENT liblzma_Development
976         INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
978 # Install the liblzma API headers. These use a subdirectory so
979 # this has to be done as a separate step.
980 install(DIRECTORY src/liblzma/api/
981         COMPONENT liblzma_Development
982         DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
983         FILES_MATCHING PATTERN "*.h")
985 # Install the CMake files that other packages can use to find liblzma.
986 set(liblzma_INSTALL_CMAKEDIR
987     "${CMAKE_INSTALL_LIBDIR}/cmake/liblzma"
988     CACHE STRING "Path to liblzma's .cmake files")
990 install(EXPORT liblzmaTargets
991         NAMESPACE liblzma::
992         FILE liblzma-targets.cmake
993         DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
994         COMPONENT liblzma_Development)
996 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
997               "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
998         DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
999         COMPONENT liblzma_Development)
1002 #############################################################################
1003 # libgnu (getopt_long)
1004 #############################################################################
1006 # This mirrors how the Autotools build system handles the getopt_long
1007 # replacement, calling the object library libgnu since the replacement
1008 # version comes from Gnulib.
1009 add_library(libgnu OBJECT)
1011 # CMake requires that even an object library must have at least once source
1012 # file. So we give it a header file that results in no output files.
1013 target_sources(libgnu PRIVATE lib/getopt.in.h)
1015 # The Ninja Generator requires setting the linker language since it cannot
1016 # guess the programming language of just a header file. Setting this
1017 # property avoids needing an empty .c file or an non-empty unnecessary .c
1018 # file.
1019 set_target_properties(libgnu PROPERTIES LINKER_LANGUAGE C)
1021 # Create /lib directory in the build directory and add it to the include path.
1022 file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib")
1023 target_include_directories(libgnu PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/lib")
1025 # Include /lib from the source directory. It does no harm even if none of
1026 # the Gnulib replacements are used.
1027 target_include_directories(libgnu PUBLIC lib)
1029 # The command line tools need getopt_long in order to parse arguments. If
1030 # the system does not have a getopt_long implementation we can use the one
1031 # from Gnulib instead.
1032 check_symbol_exists(getopt_long getopt.h HAVE_GETOPT_LONG)
1034 if(NOT HAVE_GETOPT_LONG)
1035     # Set the __GETOPT_PREFIX definition to "rpl_" (replacement) to avoid
1036     # name conflicts with libc symbols. The same prefix is set if using
1037     # the Autotools build (m4/getopt.m4).
1038     target_compile_definitions(libgnu PUBLIC "__GETOPT_PREFIX=rpl_")
1040     # Create a custom copy command to copy the getopt header to the build
1041     # directory and re-copy it if it is updated. (Gnulib does it this way
1042     # because it allows choosing which .in.h files to actually use in the
1043     # build. We need just getopt.h so this is a bit overcomplicated for
1044     # a single header file only.)
1045     add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/lib/getopt.h"
1046         COMMAND "${CMAKE_COMMAND}" -E copy
1047             "${CMAKE_CURRENT_SOURCE_DIR}/lib/getopt.in.h"
1048             "${CMAKE_CURRENT_BINARY_DIR}/lib/getopt.h"
1049         MAIN_DEPENDENCY "${CMAKE_CURRENT_SOURCE_DIR}/lib/getopt.in.h"
1050         VERBATIM)
1052     target_sources(libgnu PRIVATE
1053         lib/getopt1.c
1054         lib/getopt.c
1055         lib/getopt_int.h
1056         lib/getopt-cdefs.h
1057         lib/getopt-core.h
1058         lib/getopt-ext.h
1059         lib/getopt-pfx-core.h
1060         lib/getopt-pfx-ext.h
1061         "${CMAKE_CURRENT_BINARY_DIR}/lib/getopt.h"
1062     )
1063 endif()
1066 #############################################################################
1067 # xzdec
1068 #############################################################################
1070 if(HAVE_DECODERS AND (NOT MSVC OR MSVC_VERSION GREATER_EQUAL 1900))
1071     add_executable(xzdec
1072         src/common/sysdefs.h
1073         src/common/tuklib_common.h
1074         src/common/tuklib_config.h
1075         src/common/tuklib_exit.c
1076         src/common/tuklib_exit.h
1077         src/common/tuklib_gettext.h
1078         src/common/tuklib_progname.c
1079         src/common/tuklib_progname.h
1080         src/xzdec/xzdec.c
1081     )
1083     target_include_directories(xzdec PRIVATE
1084         src/common
1085         src/liblzma/api
1086     )
1088     target_link_libraries(xzdec PRIVATE liblzma libgnu)
1090     if(WIN32)
1091         # Add the Windows resource file for xzdec.exe.
1092         target_sources(xzdec PRIVATE src/xzdec/xzdec_w32res.rc)
1093         set_target_properties(xzdec PROPERTIES
1094             LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1095         )
1096     endif()
1098     tuklib_progname(xzdec)
1100     install(TARGETS xzdec
1101             RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1102                     COMPONENT xzdec)
1104     if(UNIX)
1105         install(FILES src/xzdec/xzdec.1
1106                 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1107                 COMPONENT xzdec)
1108     endif()
1109 endif()
1112 #############################################################################
1113 # xz
1114 #############################################################################
1116 if(NOT MSVC OR MSVC_VERSION GREATER_EQUAL 1900)
1117     add_executable(xz
1118         src/common/mythread.h
1119         src/common/sysdefs.h
1120         src/common/tuklib_common.h
1121         src/common/tuklib_config.h
1122         src/common/tuklib_exit.c
1123         src/common/tuklib_exit.h
1124         src/common/tuklib_gettext.h
1125         src/common/tuklib_integer.h
1126         src/common/tuklib_mbstr.h
1127         src/common/tuklib_mbstr_fw.c
1128         src/common/tuklib_mbstr_width.c
1129         src/common/tuklib_open_stdxxx.c
1130         src/common/tuklib_open_stdxxx.h
1131         src/common/tuklib_progname.c
1132         src/common/tuklib_progname.h
1133         src/xz/args.c
1134         src/xz/args.h
1135         src/xz/coder.c
1136         src/xz/coder.h
1137         src/xz/file_io.c
1138         src/xz/file_io.h
1139         src/xz/hardware.c
1140         src/xz/hardware.h
1141         src/xz/main.c
1142         src/xz/main.h
1143         src/xz/message.c
1144         src/xz/message.h
1145         src/xz/mytime.c
1146         src/xz/mytime.h
1147         src/xz/options.c
1148         src/xz/options.h
1149         src/xz/private.h
1150         src/xz/signals.c
1151         src/xz/signals.h
1152         src/xz/suffix.c
1153         src/xz/suffix.h
1154         src/xz/util.c
1155         src/xz/util.h
1156     )
1158     target_include_directories(xz PRIVATE
1159         src/common
1160         src/liblzma/api
1161     )
1163     if(HAVE_DECODERS)
1164         target_sources(xz PRIVATE
1165             src/xz/list.c
1166             src/xz/list.h
1167         )
1168     endif()
1170     target_link_libraries(xz PRIVATE liblzma libgnu)
1172     target_compile_definitions(xz PRIVATE ASSUME_RAM=128)
1174     if(WIN32)
1175         # Add the Windows resource file for xz.exe.
1176         target_sources(xz PRIVATE src/xz/xz_w32res.rc)
1177         set_target_properties(xz PROPERTIES
1178             LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1179         )
1180     endif()
1182     tuklib_progname(xz)
1183     tuklib_mbstr(xz)
1185     check_symbol_exists(optreset getopt.h HAVE_OPTRESET)
1186     tuklib_add_definition_if(xz HAVE_OPTRESET)
1188     check_symbol_exists(posix_fadvise fcntl.h HAVE_POSIX_FADVISE)
1189     tuklib_add_definition_if(xz HAVE_POSIX_FADVISE)
1191     # How to get file time:
1192     check_struct_has_member("struct stat" st_atim.tv_nsec
1193                             "sys/types.h;sys/stat.h"
1194                             HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1195     if(HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1196         tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1197     else()
1198         check_struct_has_member("struct stat" st_atimespec.tv_nsec
1199                                 "sys/types.h;sys/stat.h"
1200                                 HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1201         if(HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1202             tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1203         else()
1204             check_struct_has_member("struct stat" st_atimensec
1205                                     "sys/types.h;sys/stat.h"
1206                                     HAVE_STRUCT_STAT_ST_ATIMENSEC)
1207             tuklib_add_definition_if(xz HAVE_STRUCT_STAT_ST_ATIMENSEC)
1208         endif()
1209     endif()
1211     # How to set file time:
1212     check_symbol_exists(futimens "sys/types.h;sys/stat.h" HAVE_FUTIMENS)
1213     if(HAVE_FUTIMENS)
1214         tuklib_add_definitions(xz HAVE_FUTIMENS)
1215     else()
1216         check_symbol_exists(futimes "sys/time.h" HAVE_FUTIMES)
1217         if(HAVE_FUTIMES)
1218             tuklib_add_definitions(xz HAVE_FUTIMES)
1219         else()
1220             check_symbol_exists(futimesat "sys/time.h" HAVE_FUTIMESAT)
1221             if(HAVE_FUTIMESAT)
1222                 tuklib_add_definitions(xz HAVE_FUTIMESAT)
1223             else()
1224                 check_symbol_exists(utimes "sys/time.h" HAVE_UTIMES)
1225                 if(HAVE_UTIMES)
1226                     tuklib_add_definitions(xz HAVE_UTIMES)
1227                 else()
1228                     check_symbol_exists(_futime "sys/utime.h" HAVE__FUTIME)
1229                     if(HAVE__FUTIME)
1230                         tuklib_add_definitions(xz HAVE__FUTIME)
1231                     else()
1232                         check_symbol_exists(utime "utime.h" HAVE_UTIME)
1233                         tuklib_add_definition_if(xz HAVE_UTIME)
1234                     endif()
1235                 endif()
1236             endif()
1237         endif()
1238     endif()
1240     install(TARGETS xz
1241             RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1242                     COMPONENT xz)
1244     if(UNIX)
1245         install(FILES src/xz/xz.1
1246                 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1247                 COMPONENT xz)
1249         option(CREATE_XZ_SYMLINKS "Create unxz and xzcat symlinks" ON)
1250         option(CREATE_LZMA_SYMLINKS "Create lzma, unlzma, and lzcat symlinks"
1251                ON)
1252         set(XZ_LINKS)
1254         if(CREATE_XZ_SYMLINKS)
1255             list(APPEND XZ_LINKS "unxz" "xzcat")
1256         endif()
1258         if(CREATE_LZMA_SYMLINKS)
1259             list(APPEND XZ_LINKS "lzma" "unlzma" "lzcat")
1260         endif()
1262         # With Windows Cygwin and MSYS2 the symlinking is complicated. Both
1263         # of these environments set the UNIX variable so they will try to
1264         # make the symlinks. The ability for Cygwin and MSYS2 to make
1265         # broken symlinks is determined by the CYGWIN and MSYS2 environment
1266         # variables, respectively. Broken symlinks are needed for the man
1267         # page symlinks and for determining if the xz and lzma symlinks need
1268         # to depend on the xz target or not. If broken symlinks cannot be
1269         # made then the xz binary must be created before the symlinks.
1270         set(ALLOW_BROKEN_SYMLINKS ON)
1272         if(CMAKE_SYSTEM_NAME STREQUAL "CYGWIN")
1273             # The Cygwin env variable can be set to four possible values:
1274             #
1275             # 1. "lnk". Create symlinks as Windows shortcuts.
1276             #
1277             # 2. "native". Create symlinks as native Windows symlinks
1278             #    if supported by the system. Fallback to "lnk" if native
1279             #    symlinks are not supported.
1280             #
1281             # 3. "nativestrict". Create symlinks as native Windows symlinks
1282             #    if supported by the system. If the target of the symlink
1283             #    does not exist or the creation of the symlink fails for any
1284             #    reason, do not create the symlink.
1285             #
1286             # 4. "sys". Create symlinks as plain files with a special
1287             #    system attribute containing the path to the symlink target.
1288             #
1289             # So, the only case we care about for broken symlinks is
1290             # "nativestrict" since all other values mean that broken
1291             # symlinks are allowed. If the env variable is not set the
1292             # default is "native". If the env variable is set but not
1293             # assigned one of the four values, then the default is the same
1294             # as option 1 "lnk".
1295             string(FIND "$ENV{CYGWIN}" "winsymlinks:nativestrict" SYMLINK_POS)
1296             if(SYMLINK_POS GREATER -1)
1297                 set(ALLOW_BROKEN_SYMLINKS OFF)
1298             endif()
1299         elseif(CMAKE_SYSTEM_NAME STREQUAL "MSYS")
1300             # The MSYS env variable behaves similar to the CYGWIN but has a
1301             # different default behavior. If winsymlinks is set but not
1302             # assigned one of the four supported values, the default is to
1303             # *copy* the target to the symlink destination. This will fail
1304             # if the target does not exist so broken symlinks cannot be
1305             # allowed.
1306             string(FIND "$ENV{MSYS}" "winsymlinks" SYMLINK_POS)
1307             if(SYMLINK_POS GREATER -1)
1308                 string(FIND "$ENV{MSYS}" "winsymlinks:nativestrict"
1309                         SYMLINK_POS)
1310                 if(SYMLINK_POS GREATER -1)
1311                     set(ALLOW_BROKEN_SYMLINKS OFF)
1312                 endif()
1313             else()
1314                 set(ALLOW_BROKEN_SYMLINKS OFF)
1315             endif()
1316         endif()
1318         # Create symlinks in the build directory and then install them.
1319         #
1320         # The symlinks do not likely need any special extension since
1321         # even on Windows the symlink can still be executed without
1322         # the .exe extension.
1323         foreach(LINK IN LISTS XZ_LINKS)
1324             add_custom_target("create_${LINK}" ALL
1325                 "${CMAKE_COMMAND}" -E create_symlink
1326                     "$<TARGET_FILE_NAME:xz>" "${LINK}"
1327                 BYPRODUCTS "${LINK}"
1328                 VERBATIM)
1329             install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}"
1330                     DESTINATION "${CMAKE_INSTALL_BINDIR}"
1331                     COMPONENT xz)
1333             # Only create the man page symlinks if the symlinks can be
1334             # created broken. The symlinks will not be valid until install
1335             # so they cannot be created on these system environments.
1336             if(ALLOW_BROKEN_SYMLINKS)
1337                 add_custom_target("create_${LINK}.1" ALL
1338                     "${CMAKE_COMMAND}" -E create_symlink "xz.1" "${LINK}.1"
1339                     BYPRODUCTS "${LINK}.1"
1340                     VERBATIM)
1341                 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}.1"
1342                         DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1343                         COMPONENT xz)
1344             else()
1345                 # Add the xz target as dependency when broken symlinks
1346                 # cannot be made. This ensures parallel builds do not fail
1347                 # since it will enforce the order of creating xz first, then
1348                 # the symlinks.
1349                 add_dependencies("create_${LINK}" xz)
1350             endif()
1351         endforeach()
1352     endif()
1353 endif()
1356 #############################################################################
1357 # Tests
1358 #############################################################################
1360 include(CTest)
1362 if(BUILD_TESTING)
1363     set(LIBLZMA_TESTS
1364         test_bcj_exact_size
1365         test_block_header
1366         test_check
1367         test_filter_flags
1368         test_filter_str
1369         test_hardware
1370         test_index
1371         test_index_hash
1372         test_lzip_decoder
1373         test_memlimit
1374         test_stream_flags
1375         test_vli
1376     )
1378     foreach(TEST IN LISTS LIBLZMA_TESTS)
1379         add_executable("${TEST}" "tests/${TEST}.c")
1381         target_include_directories("${TEST}" PRIVATE
1382             src/common
1383             src/liblzma/api
1384             src/liblzma
1385         )
1387         target_link_libraries("${TEST}" PRIVATE liblzma)
1389         # Put the test programs into their own subdirectory so they don't
1390         # pollute the top-level dir which might contain xz and xzdec.
1391         set_target_properties("${TEST}" PROPERTIES
1392             RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tests_bin"
1393         )
1395         add_test(NAME "${TEST}"
1396                  COMMAND "${CMAKE_CURRENT_BINARY_DIR}/tests_bin/${TEST}"
1397         )
1399         # Set srcdir environment variable so that the tests find their
1400         # input files from the source tree.
1401         #
1402         # Set the return code for skipped tests to match Automake convention.
1403         set_tests_properties("${TEST}" PROPERTIES
1404             ENVIRONMENT "srcdir=${CMAKE_CURRENT_LIST_DIR}/tests"
1405             SKIP_RETURN_CODE 77
1406         )
1407     endforeach()
1408 endif()