xz: Windows: Don't (de)compress to special files like "con" or "nul".
[xz.git] / CMakeLists.txt
blobac04ff6eb7f52663415d2de5de6b30e81f79631f
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         HAVE_FUNC_ATTRIBUTE_IFUNC)
795     cmake_pop_check_state()
796     tuklib_add_definition_if(liblzma HAVE_FUNC_ATTRIBUTE_IFUNC)
797 endif()
799 # cpuid.h
800 check_include_file(cpuid.h HAVE_CPUID_H)
801 tuklib_add_definition_if(liblzma HAVE_CPUID_H)
803 # immintrin.h:
804 check_include_file(immintrin.h HAVE_IMMINTRIN_H)
805 if(HAVE_IMMINTRIN_H)
806     target_compile_definitions(liblzma PRIVATE HAVE_IMMINTRIN_H)
808     # SSE2 intrinsics:
809     check_c_source_compiles("
810             #include <immintrin.h>
811             int main(void)
812             {
813                 __m128i x = { 0 };
814                 _mm_movemask_epi8(x);
815                 return 0;
816             }
817         "
818         HAVE__MM_MOVEMASK_EPI8)
819     tuklib_add_definition_if(liblzma HAVE__MM_MOVEMASK_EPI8)
821     # CLMUL intrinsic:
822     check_c_source_compiles("
823             #include <immintrin.h>
824             #if defined(__e2k__) && __iset__ < 6
825             #   error
826             #endif
827             #if (defined(__GNUC__) || defined(__clang__)) && !defined(__EDG__)
828             __attribute__((__target__(\"ssse3,sse4.1,pclmul\")))
829             #endif
830             __m128i my_clmul(__m128i a)
831             {
832                 const __m128i b = _mm_set_epi64x(1, 2);
833                 return _mm_clmulepi64_si128(a, b, 0);
834             }
835             int main(void) { return 0; }
836     "
837     HAVE_USABLE_CLMUL)
838     tuklib_add_definition_if(liblzma HAVE_USABLE_CLMUL)
839 endif()
841 # Support -fvisiblity=hidden when building shared liblzma.
842 # These lines do nothing on Windows (even under Cygwin).
843 # HAVE_VISIBILITY should always be defined to 0 or 1.
844 if(BUILD_SHARED_LIBS)
845     set_target_properties(liblzma PROPERTIES C_VISIBILITY_PRESET hidden)
846     target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=1)
847 else()
848     target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=0)
849 endif()
851 if(WIN32)
852     if(BUILD_SHARED_LIBS)
853         # Add the Windows resource file for liblzma.dll.
854         target_sources(liblzma PRIVATE src/liblzma/liblzma_w32res.rc)
856         set_target_properties(liblzma PROPERTIES
857             LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
858         )
860         # Export the public API symbols with __declspec(dllexport).
861         target_compile_definitions(liblzma PRIVATE DLL_EXPORT)
862     else()
863         # Disable __declspec(dllimport) when linking against static liblzma.
864         target_compile_definitions(liblzma INTERFACE LZMA_API_STATIC)
865     endif()
866 elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
867     # GNU/Linux-specific symbol versioning for shared liblzma.
868     # Note that adding link options doesn't affect static builds
869     # but HAVE_SYMBOL_VERSIONS_LINUX must not be used with static builds
870     # because it would put symbol versions into the static library which
871     # can cause problems. It's clearer if all symver related things are
872     # omitted when not building a shared library.
873     #
874     # NOTE: Set it explicitly to 1 to make it clear that versioning is
875     # done unconditionally in the C files.
876     target_compile_definitions(liblzma PRIVATE HAVE_SYMBOL_VERSIONS_LINUX=1)
877     target_link_options(liblzma PRIVATE
878         "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
879     )
880     set_target_properties(liblzma PROPERTIES
881         LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
882     )
883 elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
884     # Symbol versioning for shared liblzma for non-GNU/Linux.
885     # FIXME? What about Solaris?
886     target_link_options(liblzma PRIVATE
887         "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
888     )
889     set_target_properties(liblzma PROPERTIES
890         LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
891     )
892 endif()
894 set_target_properties(liblzma PROPERTIES
895     # At least for now the package versioning matches the rules used for
896     # shared library versioning (excluding development releases) so it is
897     # fine to use the package version here.
898     SOVERSION "${xz_VERSION_MAJOR}"
899     VERSION "${xz_VERSION}"
901     # It's liblzma.so or liblzma.dll, not libliblzma.so or lzma.dll.
902     # Avoid the name lzma.dll because it would conflict with LZMA SDK.
903     PREFIX ""
906 # Create liblzma-config-version.cmake.
908 # FIXME: SameMajorVersion is correct for stable releases but it is wrong
909 # for development releases where each release may have incompatible changes.
910 include(CMakePackageConfigHelpers)
911 write_basic_package_version_file(
912     "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
913     VERSION "${liblzma_VERSION}"
914     COMPATIBILITY SameMajorVersion)
916 # Create liblzma-config.cmake. We use this spelling instead of
917 # liblzmaConfig.cmake to make find_package work in case insensitive
918 # manner even with case sensitive file systems. This gives more consistent
919 # behavior between operating systems. This optionally includes a dependency
920 # on a threading library, so the contents are created in two separate parts.
921 # The "second half" is always needed, so create it first.
922 set(LZMA_CONFIG_CONTENTS
923 "include(\"\${CMAKE_CURRENT_LIST_DIR}/liblzma-targets.cmake\")
925 if(NOT TARGET LibLZMA::LibLZMA)
926     # Be compatible with the spelling used by the FindLibLZMA module. This
927     # doesn't use ALIAS because it would make CMake resolve LibLZMA::LibLZMA
928     # to liblzma::liblzma instead of keeping the original spelling. Keeping
929     # the original spelling is important for good FindLibLZMA compatibility.
930     add_library(LibLZMA::LibLZMA INTERFACE IMPORTED)
931     set_target_properties(LibLZMA::LibLZMA PROPERTIES
932                           INTERFACE_LINK_LIBRARIES liblzma::liblzma)
933 endif()
936 if(ENABLE_THREADS STREQUAL "posix")
937     set(LZMA_CONFIG_CONTENTS
938 "include(CMakeFindDependencyMacro)
939 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
940 find_dependency(Threads)
942 ${LZMA_CONFIG_CONTENTS}
944 endif()
946 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
947         "${LZMA_CONFIG_CONTENTS}")
949 # Set CMAKE_INSTALL_LIBDIR and friends.
950 include(GNUInstallDirs)
952 # Install the library binary. The INCLUDES specifies the include path that
953 # is exported for other projects to use but it doesn't install any files.
954 install(TARGETS liblzma EXPORT liblzmaTargets
955         RUNTIME  DESTINATION "${CMAKE_INSTALL_BINDIR}"
956                  COMPONENT liblzma_Runtime
957         LIBRARY  DESTINATION "${CMAKE_INSTALL_LIBDIR}"
958                  COMPONENT liblzma_Runtime
959                  NAMELINK_COMPONENT liblzma_Development
960         ARCHIVE  DESTINATION "${CMAKE_INSTALL_LIBDIR}"
961                  COMPONENT liblzma_Development
962         INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
964 # Install the liblzma API headers. These use a subdirectory so
965 # this has to be done as a separate step.
966 install(DIRECTORY src/liblzma/api/
967         COMPONENT liblzma_Development
968         DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
969         FILES_MATCHING PATTERN "*.h")
971 # Install the CMake files that other packages can use to find liblzma.
972 set(liblzma_INSTALL_CMAKEDIR
973     "${CMAKE_INSTALL_LIBDIR}/cmake/liblzma"
974     CACHE STRING "Path to liblzma's .cmake files")
976 install(EXPORT liblzmaTargets
977         NAMESPACE liblzma::
978         FILE liblzma-targets.cmake
979         DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
980         COMPONENT liblzma_Development)
982 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
983               "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
984         DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
985         COMPONENT liblzma_Development)
988 #############################################################################
989 # libgnu (getopt_long)
990 #############################################################################
992 # This mirrors how the Autotools build system handles the getopt_long
993 # replacement, calling the object library libgnu since the replacement
994 # version comes from Gnulib.
995 add_library(libgnu OBJECT)
997 # CMake requires that even an object library must have at least once source
998 # file. So we give it a header file that results in no output files.
999 target_sources(libgnu PRIVATE lib/getopt.in.h)
1001 # Create /lib directory in the build directory and add it to the include path.
1002 file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib")
1003 target_include_directories(libgnu PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/lib")
1005 # The command line tools need getopt_long in order to parse arguments. If
1006 # the system does not have a getopt_long implementation we can use the one
1007 # from Gnulib instead.
1008 check_symbol_exists(getopt_long getopt.h HAVE_GETOPT_LONG)
1010 if(NOT HAVE_GETOPT_LONG)
1011     # Set the __GETOPT_PREFIX definition to "rpl_" (replacement) to avoid
1012     # name conflicts with libc symbols. The same prefix is set if using
1013     # the Autotools build (m4/getopt.m4).
1014     target_compile_definitions(libgnu PUBLIC "__GETOPT_PREFIX=rpl_")
1016     # Create a custom copy command to copy the getopt header to the build
1017     # directory and re-copy it if it is updated. (Gnulib does it this way
1018     # because it allows choosing which .in.h files to actually use in the
1019     # build. We need just getopt.h so this is a bit overcomplicated for
1020     # a single header file only.)
1021     add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/lib/getopt.h"
1022         COMMAND "${CMAKE_COMMAND}" -E copy
1023             "${CMAKE_CURRENT_SOURCE_DIR}/lib/getopt.in.h"
1024             "${CMAKE_CURRENT_BINARY_DIR}/lib/getopt.h"
1025         MAIN_DEPENDENCY "${CMAKE_CURRENT_SOURCE_DIR}/lib/getopt.in.h"
1026         VERBATIM)
1028     target_sources(libgnu PRIVATE
1029         lib/getopt1.c
1030         lib/getopt.c
1031         lib/getopt_int.h
1032         "${CMAKE_CURRENT_BINARY_DIR}/lib/getopt.h"
1033     )
1034 endif()
1037 #############################################################################
1038 # xzdec
1039 #############################################################################
1041 if(HAVE_DECODERS AND (NOT MSVC OR MSVC_VERSION GREATER_EQUAL 1900))
1042     add_executable(xzdec
1043         src/common/sysdefs.h
1044         src/common/tuklib_common.h
1045         src/common/tuklib_config.h
1046         src/common/tuklib_exit.c
1047         src/common/tuklib_exit.h
1048         src/common/tuklib_gettext.h
1049         src/common/tuklib_progname.c
1050         src/common/tuklib_progname.h
1051         src/xzdec/xzdec.c
1052     )
1054     target_include_directories(xzdec PRIVATE
1055         src/common
1056         src/liblzma/api
1057     )
1059     target_link_libraries(xzdec PRIVATE liblzma libgnu)
1061     if(WIN32)
1062         # Add the Windows resource file for xzdec.exe.
1063         target_sources(xzdec PRIVATE src/xzdec/xzdec_w32res.rc)
1064         set_target_properties(xzdec PROPERTIES
1065             LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1066         )
1067     endif()
1069     tuklib_progname(xzdec)
1071     install(TARGETS xzdec
1072             RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1073                     COMPONENT xzdec)
1075     if(UNIX)
1076         install(FILES src/xzdec/xzdec.1
1077                 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1078                 COMPONENT xzdec)
1079     endif()
1080 endif()
1083 #############################################################################
1084 # xz
1085 #############################################################################
1087 if(NOT MSVC OR MSVC_VERSION GREATER_EQUAL 1900)
1088     add_executable(xz
1089         src/common/mythread.h
1090         src/common/sysdefs.h
1091         src/common/tuklib_common.h
1092         src/common/tuklib_config.h
1093         src/common/tuklib_exit.c
1094         src/common/tuklib_exit.h
1095         src/common/tuklib_gettext.h
1096         src/common/tuklib_integer.h
1097         src/common/tuklib_mbstr.h
1098         src/common/tuklib_mbstr_fw.c
1099         src/common/tuklib_mbstr_width.c
1100         src/common/tuklib_open_stdxxx.c
1101         src/common/tuklib_open_stdxxx.h
1102         src/common/tuklib_progname.c
1103         src/common/tuklib_progname.h
1104         src/xz/args.c
1105         src/xz/args.h
1106         src/xz/coder.c
1107         src/xz/coder.h
1108         src/xz/file_io.c
1109         src/xz/file_io.h
1110         src/xz/hardware.c
1111         src/xz/hardware.h
1112         src/xz/main.c
1113         src/xz/main.h
1114         src/xz/message.c
1115         src/xz/message.h
1116         src/xz/mytime.c
1117         src/xz/mytime.h
1118         src/xz/options.c
1119         src/xz/options.h
1120         src/xz/private.h
1121         src/xz/signals.c
1122         src/xz/signals.h
1123         src/xz/suffix.c
1124         src/xz/suffix.h
1125         src/xz/util.c
1126         src/xz/util.h
1127     )
1129     target_include_directories(xz PRIVATE
1130         src/common
1131         src/liblzma/api
1132     )
1134     if(HAVE_DECODERS)
1135         target_sources(xz PRIVATE
1136             src/xz/list.c
1137             src/xz/list.h
1138         )
1139     endif()
1141     target_link_libraries(xz PRIVATE liblzma libgnu)
1143     target_compile_definitions(xz PRIVATE ASSUME_RAM=128)
1145     if(WIN32)
1146         # Add the Windows resource file for xz.exe.
1147         target_sources(xz PRIVATE src/xz/xz_w32res.rc)
1148         set_target_properties(xz PROPERTIES
1149             LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1150         )
1151     endif()
1153     tuklib_progname(xz)
1154     tuklib_mbstr(xz)
1156     check_symbol_exists(optreset getopt.h HAVE_OPTRESET)
1157     tuklib_add_definition_if(xz HAVE_OPTRESET)
1159     check_symbol_exists(posix_fadvise fcntl.h HAVE_POSIX_FADVISE)
1160     tuklib_add_definition_if(xz HAVE_POSIX_FADVISE)
1162     # How to get file time:
1163     check_struct_has_member("struct stat" st_atim.tv_nsec
1164                             "sys/types.h;sys/stat.h"
1165                             HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1166     if(HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1167         tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1168     else()
1169         check_struct_has_member("struct stat" st_atimespec.tv_nsec
1170                                 "sys/types.h;sys/stat.h"
1171                                 HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1172         if(HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1173             tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1174         else()
1175             check_struct_has_member("struct stat" st_atimensec
1176                                     "sys/types.h;sys/stat.h"
1177                                     HAVE_STRUCT_STAT_ST_ATIMENSEC)
1178             tuklib_add_definition_if(xz HAVE_STRUCT_STAT_ST_ATIMENSEC)
1179         endif()
1180     endif()
1182     # How to set file time:
1183     check_symbol_exists(futimens "sys/types.h;sys/stat.h" HAVE_FUTIMENS)
1184     if(HAVE_FUTIMENS)
1185         tuklib_add_definitions(xz HAVE_FUTIMENS)
1186     else()
1187         check_symbol_exists(futimes "sys/time.h" HAVE_FUTIMES)
1188         if(HAVE_FUTIMES)
1189             tuklib_add_definitions(xz HAVE_FUTIMES)
1190         else()
1191             check_symbol_exists(futimesat "sys/time.h" HAVE_FUTIMESAT)
1192             if(HAVE_FUTIMESAT)
1193                 tuklib_add_definitions(xz HAVE_FUTIMESAT)
1194             else()
1195                 check_symbol_exists(utimes "sys/time.h" HAVE_UTIMES)
1196                 if(HAVE_UTIMES)
1197                     tuklib_add_definitions(xz HAVE_UTIMES)
1198                 else()
1199                     check_symbol_exists(_futime "sys/utime.h" HAVE__FUTIME)
1200                     if(HAVE__FUTIME)
1201                         tuklib_add_definitions(xz HAVE__FUTIME)
1202                     else()
1203                         check_symbol_exists(utime "utime.h" HAVE_UTIME)
1204                         tuklib_add_definition_if(xz HAVE_UTIME)
1205                     endif()
1206                 endif()
1207             endif()
1208         endif()
1209     endif()
1211     install(TARGETS xz
1212             RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1213                     COMPONENT xz)
1215     if(UNIX)
1216         install(FILES src/xz/xz.1
1217                 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1218                 COMPONENT xz)
1220         option(CREATE_XZ_SYMLINKS "Create unxz and xzcat symlinks" ON)
1221         option(CREATE_LZMA_SYMLINKS "Create lzma, unlzma, and lzcat symlinks"
1222                ON)
1223         set(XZ_LINKS)
1225         if(CREATE_XZ_SYMLINKS)
1226             list(APPEND XZ_LINKS "unxz" "xzcat")
1227         endif()
1229         if(CREATE_LZMA_SYMLINKS)
1230             list(APPEND XZ_LINKS "lzma" "unlzma" "lzcat")
1231         endif()
1233         # With Windows Cygwin and MSYS2 the symlinking is complicated. Both
1234         # of these environments set the UNIX variable so they will try to
1235         # make the symlinks. The ability for Cygwin and MSYS2 to make
1236         # broken symlinks is determined by the CYGWIN and MSYS2 environment
1237         # variables, respectively. Broken symlinks are needed for the man
1238         # page symlinks and for determining if the xz and lzma symlinks need
1239         # to depend on the xz target or not. If broken symlinks cannot be
1240         # made then the xz binary must be created before the symlinks.
1241         set(ALLOW_BROKEN_SYMLINKS ON)
1243         if(CMAKE_SYSTEM_NAME STREQUAL "CYGWIN")
1244             # The Cygwin env variable can be set to four possible values:
1245             #
1246             # 1. "lnk". Create symlinks as Windows shortcuts.
1247             #
1248             # 2. "native". Create symlinks as native Windows symlinks
1249             #    if supported by the system. Fallback to "lnk" if native
1250             #    symlinks are not supported.
1251             #
1252             # 3. "nativestrict". Create symlinks as native Windows symlinks
1253             #    if supported by the system. If the target of the symlink
1254             #    does not exist or the creation of the symlink fails for any
1255             #    reason, do not create the symlink.
1256             #
1257             # 4. "sys". Create symlinks as plain files with a special
1258             #    system attribute containing the path to the symlink target.
1259             #
1260             # So, the only case we care about for broken symlinks is
1261             # "nativestrict" since all other values mean that broken
1262             # symlinks are allowed. If the env variable is not set the
1263             # default is "native". If the env variable is set but not
1264             # assigned one of the four values, then the default is the same
1265             # as option 1 "lnk".
1266             string(FIND "$ENV{CYGWIN}" "winsymlinks:nativestrict" SYMLINK_POS)
1267             if(SYMLINK_POS GREATER -1)
1268                 set(ALLOW_BROKEN_SYMLINKS OFF)
1269             endif()
1270         elseif(CMAKE_SYSTEM_NAME STREQUAL "MSYS")
1271             # The MSYS env variable behaves similar to the CYGWIN but has a
1272             # different default behavior. If winsymlinks is set but not
1273             # assigned one of the four supported values, the default is to
1274             # *copy* the target to the symlink destination. This will fail
1275             # if the target does not exist so broken symlinks cannot be
1276             # allowed.
1277             string(FIND "$ENV{MSYS}" "winsymlinks" SYMLINK_POS)
1278             if(SYMLINK_POS GREATER -1)
1279                 string(FIND "$ENV{MSYS}" "winsymlinks:nativestrict"
1280                         SYMLINK_POS)
1281                 if(SYMLINK_POS GREATER -1)
1282                     set(ALLOW_BROKEN_SYMLINKS OFF)
1283                 endif()
1284             else()
1285                 set(ALLOW_BROKEN_SYMLINKS OFF)
1286             endif()
1287         endif()
1289         # Create symlinks in the build directory and then install them.
1290         #
1291         # The symlinks do not likely need any special extension since
1292         # even on Windows the symlink can still be executed without
1293         # the .exe extension.
1294         foreach(LINK IN LISTS XZ_LINKS)
1295             add_custom_target("${LINK}" ALL
1296                 "${CMAKE_COMMAND}" -E create_symlink
1297                     "$<TARGET_FILE_NAME:xz>" "${LINK}"
1298                 BYPRODUCTS "${LINK}"
1299                 VERBATIM)
1300             install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}"
1301                     DESTINATION "${CMAKE_INSTALL_BINDIR}"
1302                     COMPONENT xz)
1304             # Only create the man page symlinks if the symlinks can be
1305             # created broken. The symlinks will not be valid until install
1306             # so they cannot be created on these system environments.
1307             if(ALLOW_BROKEN_SYMLINKS)
1308                 add_custom_target("${LINK}.1" ALL
1309                     "${CMAKE_COMMAND}" -E create_symlink "xz.1" "${LINK}.1"
1310                     BYPRODUCTS "${LINK}.1"
1311                     VERBATIM)
1312                 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}.1"
1313                         DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1314                         COMPONENT xz)
1315             else()
1316                 # Add the xz target as dependency when broken symlinks
1317                 # cannot be made. This ensures parallel builds do not fail
1318                 # since it will enforce the order of creating xz first, then
1319                 # the symlinks.
1320                 add_dependencies("${LINK}" xz)
1321             endif()
1322         endforeach()
1323     endif()
1324 endif()
1327 #############################################################################
1328 # Tests
1329 #############################################################################
1331 include(CTest)
1333 if(BUILD_TESTING)
1334     set(LIBLZMA_TESTS
1335         test_bcj_exact_size
1336         test_block_header
1337         test_check
1338         test_filter_flags
1339         test_filter_str
1340         test_hardware
1341         test_index
1342         test_index_hash
1343         test_lzip_decoder
1344         test_memlimit
1345         test_stream_flags
1346         test_vli
1347     )
1349     foreach(TEST IN LISTS LIBLZMA_TESTS)
1350         add_executable("${TEST}" "tests/${TEST}.c")
1352         target_include_directories("${TEST}" PRIVATE
1353             src/common
1354             src/liblzma/api
1355             src/liblzma
1356             lib
1357         )
1359         target_link_libraries("${TEST}" PRIVATE liblzma)
1361         # Put the test programs into their own subdirectory so they don't
1362         # pollute the top-level dir which might contain xz and xzdec.
1363         set_target_properties("${TEST}" PROPERTIES
1364             RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tests_bin"
1365         )
1367         add_test(NAME "${TEST}"
1368                  COMMAND "${CMAKE_CURRENT_BINARY_DIR}/tests_bin/${TEST}"
1369         )
1371         # Set srcdir environment variable so that the tests find their
1372         # input files from the source tree.
1373         #
1374         # Set the return code for skipped tests to match Automake convention.
1375         set_tests_properties("${TEST}" PROPERTIES
1376             ENVIRONMENT "srcdir=${CMAKE_CURRENT_LIST_DIR}/tests"
1377             SKIP_RETURN_CODE 77
1378         )
1379     endforeach()
1380 endif()