Update THANKS.
[xz.git] / CMakeLists.txt
blobc69b135edad186848da1207846cdbe4e18699414
1 #############################################################################
3 # Very limited CMake support for building some parts of XZ Utils
5 # For now, this is intended to be useful to build static or shared liblzma
6 # on Windows with MSVC (to avoid the need to maintain Visual Studio project
7 # files). Building liblzma on a few other platforms should work too but it
8 # is somewhat experimental and not as portable as using ./configure.
10 # On some platforms this builds also xz and xzdec, but these are
11 # highly experimental and meant for testing only:
12 #   - No large file support on those 32-bit platforms that need it
13 #   - No replacement getopt_long(), libc must have it
14 #   - No sandboxing support
15 #   - No translations
17 # Other missing things:
18 #   - No xzgrep or other scripts or their symlinks
19 #   - No xz tests (liblzma tests only)
21 # NOTE: Even if the code compiles without warnings, the end result may be
22 # different than via ./configure. Specifically, the list of #defines
23 # may be different (if so, probably this CMakeLists.txt got them wrong).
25 # This file provides the following installation components (if you only
26 # need liblzma, install only its components!):
27 #   - liblzma_Runtime
28 #   - liblzma_Development
29 #   - xz (on some platforms only)
30 #   - xzdec (on some platforms only)
32 # To find the target liblzma::liblzma from other packages, use the CONFIG
33 # option with find_package() to avoid a conflict with the FindLibLZMA module
34 # with case-insensitive file systems. For example, to require liblzma 5.2.5
35 # or a newer compatible version:
37 #     find_package(liblzma 5.2.5 REQUIRED CONFIG)
38 #     target_link_libraries(my_application liblzma::liblzma)
40 #############################################################################
42 # Author: Lasse Collin
44 # This file has been put into the public domain.
45 # You can do whatever you want with this file.
47 #############################################################################
49 cmake_minimum_required(VERSION 3.13...3.26 FATAL_ERROR)
51 include(CMakePushCheckState)
52 include(CheckIncludeFile)
53 include(CheckSymbolExists)
54 include(CheckStructHasMember)
55 include(CheckCSourceCompiles)
56 include(cmake/tuklib_integer.cmake)
57 include(cmake/tuklib_cpucores.cmake)
58 include(cmake/tuklib_physmem.cmake)
59 include(cmake/tuklib_progname.cmake)
60 include(cmake/tuklib_mbstr.cmake)
62 # Get the package version from version.h into XZ_VERSION variable.
63 file(READ src/liblzma/api/lzma/version.h XZ_VERSION)
64 string(REGEX REPLACE
65 "^.*\n\
66 #define LZMA_VERSION_MAJOR ([0-9]+)\n\
67 .*\
68 #define LZMA_VERSION_MINOR ([0-9]+)\n\
69 .*\
70 #define LZMA_VERSION_PATCH ([0-9]+)\n\
71 .*$"
72        "\\1.\\2.\\3" XZ_VERSION "${XZ_VERSION}")
74 # Among other things, this gives us variables xz_VERSION and xz_VERSION_MAJOR.
75 project(xz VERSION "${XZ_VERSION}" LANGUAGES C)
77 # We need a compiler that supports enough C99 or newer (variable-length arrays
78 # aren't needed, those are optional in C17). Setting CMAKE_C_STANDARD here
79 # makes it the default for all targets. It doesn't affect the INTERFACE so
80 # liblzma::liblzma won't end up with INTERFACE_COMPILE_FEATURES "c_std_99"
81 # (the API headers are C89 and C++ compatible).
82 set(CMAKE_C_STANDARD 99)
83 set(CMAKE_C_STANDARD_REQUIRED ON)
85 # On Apple OSes, don't build executables as bundles:
86 set(CMAKE_MACOSX_BUNDLE OFF)
88 # windres from GNU binutils can be tricky with command line arguments
89 # that contain spaces or other funny characters. Unfortunately we need
90 # a space in PACKAGE_NAME. Using \x20 to encode the US-ASCII space seems
91 # to work in both cmd.exe and /bin/sh.
93 # However, even \x20 isn't enough in all situations, resulting in
94 # "syntax error" from windres. Using --use-temp-file prevents windres
95 # from using popen() and this seems to fix the problem.
97 # llvm-windres claims to be compatible with GNU windres but with that
98 # the \x20 results in "XZx20Utils" in the compiled binary. (At the
99 # same time it works correctly with clang (the C compiler).) The option
100 # --use-temp-file makes no difference.
102 # CMake 3.25 doesn't have CMAKE_RC_COMPILER_ID so we rely on
103 # CMAKE_C_COMPILER_ID. If Clang is used together with GNU windres
104 # then it will fail, but this way the risk of a bad string in
105 # the binary should be fairly low.
106 if(WIN32 AND CMAKE_C_COMPILER_ID STREQUAL "GNU")
107     # Use workarounds with GNU windres. The \x20 in PACKAGE_NAME works
108     # with gcc too so we don't need to worry how to pass different flags
109     # to windres and gcc.
110     string(APPEND CMAKE_RC_FLAGS " --use-temp-file")
111     set(PACKAGE_NAME "XZ\\x20Utils")
112 else()
113     # Elsewhere a space is safe. This also keeps things compatible with
114     # EBCDIC in case CMake-based build is ever done on such a system.
115     set(PACKAGE_NAME "XZ Utils")
116 endif()
118 # Definitions common to all targets:
119 add_compile_definitions(
120     # Package info:
121     PACKAGE_NAME="${PACKAGE_NAME}"
122     PACKAGE_BUGREPORT="xz@tukaani.org"
123     PACKAGE_URL="https://tukaani.org/xz/"
125     # Standard headers and types are available:
126     HAVE_STDBOOL_H
127     HAVE__BOOL
128     HAVE_STDINT_H
129     HAVE_INTTYPES_H
131     # Always enable CRC32 since liblzma should never build without it.
132     HAVE_CHECK_CRC32
134     # Disable assert() checks when no build type has been specified. Non-empty
135     # build types like "Release" and "Debug" handle this by default.
136     $<$<CONFIG:>:NDEBUG>
140 ######################
141 # System definitions #
142 ######################
144 # _GNU_SOURCE and such definitions. This specific macro is special since
145 # it also adds the definitions to CMAKE_REQUIRED_DEFINITIONS.
146 tuklib_use_system_extensions(ALL)
148 # This is needed by liblzma and xz.
149 tuklib_integer(ALL)
151 # Check for clock_gettime(). Do this before checking for threading so
152 # that we know there if CLOCK_MONOTONIC is available.
153 if(NOT WIN32 AND NOT DEFINED HAVE_CLOCK_GETTIME)
154     check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME)
155     if(NOT HAVE_CLOCK_GETTIME)
156         # With glibc <= 2.17 or Solaris 10 this needs librt.
157         unset(HAVE_CLOCK_GETTIME CACHE)
159         list(INSERT CMAKE_REQUIRED_LIBRARIES 0 rt)
160         check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME)
162         # If it was found now, add it to all targets and keep it
163         # in CMAKE_REQUIRED_LIBRARIES for further tests too.
164         if(HAVE_CLOCK_GETTIME)
165             link_libraries(rt)
166         else()
167             list(REMOVE_AT CMAKE_REQUIRED_LIBRARIES 0)
168         endif()
169     endif()
170     if(HAVE_CLOCK_GETTIME)
171         # Check if CLOCK_MONOTONIC is available for clock_gettime().
172         check_symbol_exists(CLOCK_MONOTONIC time.h HAVE_CLOCK_MONOTONIC)
174         add_compile_definitions(
175             HAVE_CLOCK_GETTIME
176             HAVE_CLOCK_MONOTONIC
177         )
178     endif()
179 endif()
181 # Options for new enough GCC or Clang on any arch or operating system:
182 if(CMAKE_C_COMPILER_ID MATCHES GNU|Clang)
183     # configure.ac has a long list but it won't be copied here:
184     add_compile_options(-Wall -Wextra)
185 endif()
188 #############################################################################
189 # liblzma
190 #############################################################################
192 option(BUILD_SHARED_LIBS "Build liblzma as a shared library instead of static")
194 add_library(liblzma
195     src/common/mythread.h
196     src/common/sysdefs.h
197     src/common/tuklib_common.h
198     src/common/tuklib_config.h
199     src/common/tuklib_integer.h
200     src/common/tuklib_physmem.c
201     src/common/tuklib_physmem.h
202     src/liblzma/api/lzma.h
203     src/liblzma/api/lzma/base.h
204     src/liblzma/api/lzma/bcj.h
205     src/liblzma/api/lzma/block.h
206     src/liblzma/api/lzma/check.h
207     src/liblzma/api/lzma/container.h
208     src/liblzma/api/lzma/delta.h
209     src/liblzma/api/lzma/filter.h
210     src/liblzma/api/lzma/hardware.h
211     src/liblzma/api/lzma/index.h
212     src/liblzma/api/lzma/index_hash.h
213     src/liblzma/api/lzma/lzma12.h
214     src/liblzma/api/lzma/stream_flags.h
215     src/liblzma/api/lzma/version.h
216     src/liblzma/api/lzma/vli.h
217     src/liblzma/check/check.c
218     src/liblzma/check/check.h
219     src/liblzma/check/crc_macros.h
220     src/liblzma/common/block_util.c
221     src/liblzma/common/common.c
222     src/liblzma/common/common.h
223     src/liblzma/common/easy_preset.c
224     src/liblzma/common/easy_preset.h
225     src/liblzma/common/filter_common.c
226     src/liblzma/common/filter_common.h
227     src/liblzma/common/hardware_physmem.c
228     src/liblzma/common/index.c
229     src/liblzma/common/index.h
230     src/liblzma/common/memcmplen.h
231     src/liblzma/common/stream_flags_common.c
232     src/liblzma/common/stream_flags_common.h
233     src/liblzma/common/string_conversion.c
234     src/liblzma/common/vli_size.c
237 target_include_directories(liblzma PRIVATE
238     src/liblzma/api
239     src/liblzma/common
240     src/liblzma/check
241     src/liblzma/lz
242     src/liblzma/rangecoder
243     src/liblzma/lzma
244     src/liblzma/delta
245     src/liblzma/simple
246     src/common
250 ######################
251 # Size optimizations #
252 ######################
254 option(ENABLE_SMALL "Reduce code size at expense of speed. \
255 This may be useful together with CMAKE_BUILD_TYPE=MinSizeRel.")
257 if(ENABLE_SMALL)
258     add_compile_definitions(HAVE_SMALL)
259 endif()
262 ##########
263 # Checks #
264 ##########
266 set(ADDITIONAL_SUPPORTED_CHECKS crc64 sha256)
268 set(ADDITIONAL_CHECK_TYPES "${ADDITIONAL_SUPPORTED_CHECKS}" CACHE STRING
269     "Additional check types to support (crc32 is always built)")
271 foreach(CHECK IN LISTS ADDITIONAL_CHECK_TYPES)
272     if(NOT CHECK IN_LIST ADDITIONAL_SUPPORTED_CHECKS)
273         message(SEND_ERROR "'${CHECK}' is not a supported check type")
274     endif()
275 endforeach()
277 if(ENABLE_SMALL)
278     target_sources(liblzma PRIVATE src/liblzma/check/crc32_small.c)
279 else()
280     target_sources(liblzma PRIVATE
281         src/liblzma/check/crc32_fast.c
282         src/liblzma/check/crc32_table.c
283         src/liblzma/check/crc32_table_be.h
284         src/liblzma/check/crc32_table_le.h
285     )
286 endif()
288 if("crc64" IN_LIST ADDITIONAL_CHECK_TYPES)
289     add_compile_definitions("HAVE_CHECK_CRC64")
291     if(ENABLE_SMALL)
292         target_sources(liblzma PRIVATE src/liblzma/check/crc64_small.c)
293     else()
294         target_sources(liblzma PRIVATE
295             src/liblzma/check/crc64_fast.c
296             src/liblzma/check/crc64_table.c
297             src/liblzma/check/crc64_table_be.h
298             src/liblzma/check/crc64_table_le.h
299         )
300     endif()
301 endif()
303 if("sha256" IN_LIST ADDITIONAL_CHECK_TYPES)
304     add_compile_definitions("HAVE_CHECK_SHA256")
305     target_sources(liblzma PRIVATE src/liblzma/check/sha256.c)
306 endif()
309 #################
310 # Match finders #
311 #################
313 set(SUPPORTED_MATCH_FINDERS hc3 hc4 bt2 bt3 bt4)
315 set(MATCH_FINDERS "${SUPPORTED_MATCH_FINDERS}" CACHE STRING
316     "Match finders to support (at least one is required for LZMA1 or LZMA2)")
318 foreach(MF IN LISTS MATCH_FINDERS)
319     if(MF IN_LIST SUPPORTED_MATCH_FINDERS)
320         string(TOUPPER "${MF}" MF_UPPER)
321         add_compile_definitions("HAVE_MF_${MF_UPPER}")
322     else()
323         message(SEND_ERROR "'${MF}' is not a supported match finder")
324     endif()
325 endforeach()
328 #############
329 # Threading #
330 #############
332 # Supported thread methods:
333 # ON    - autodetect the best threading method. The autodetection will
334 #         prefer Windows threading (win95 or vista) over posix if both are
335 #         available. vista threads will be used over win95 unless it is a
336 #         32-bit build.
337 # OFF   - Disable threading.
338 # posix - Use posix threading, or throw an error if not available.
339 # win95 - Use Windows win95 threading, or throw an error if not available.
340 # vista - Use Windows vista threading, or throw an error if not available.
341 set(SUPPORTED_THREAD_METHODS ON OFF posix win95 vista)
343 set(ENABLE_THREADS ON CACHE STRING
344         "Threading method type to support. Set to 'OFF' to disable threading")
346 # Create dropdown in CMake GUI since only 1 threading method is possible
347 # to select in a build.
348 set_property(CACHE ENABLE_THREADS
349         PROPERTY STRINGS "${SUPPORTED_THREAD_METHODS}")
351 # This is a flag variable set when win95 threads are used. We must ensure
352 # the combination of enable_small and win95 threads is not used without a
353 # compiler supporting attribute __constructor__.
354 set(USE_WIN95_THREADS OFF)
356 if(NOT ENABLE_THREADS IN_LIST SUPPORTED_THREAD_METHODS)
357     message(SEND_ERROR "'${ENABLE_THREADS}' is not a supported thread type")
358 endif()
360 if(ENABLE_THREADS)
361     # Also set THREADS_PREFER_PTHREAD_FLAG since the flag has no effect
362     # for Windows threading.
363     set(THREADS_PREFER_PTHREAD_FLAG TRUE)
364     find_package(Threads REQUIRED)
366     # If both Windows and posix threading are available, prefer Windows.
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 use CLOCK_MONOTONIC.
393             if(HAVE_CLOCK_MONOTONIC)
394                 list(INSERT CMAKE_REQUIRED_LIBRARIES 0 "${CMAKE_THREAD_LIBS_INIT}")
395                 check_symbol_exists(pthread_condattr_setclock pthread.h
396                                     HAVE_PTHREAD_CONDATTR_SETCLOCK)
397                 tuklib_add_definition_if(ALL HAVE_PTHREAD_CONDATTR_SETCLOCK)
398             endif()
399         else()
400             message(SEND_ERROR
401                     "Windows thread method requested, but a compatible "
402                     "library could not be found")
403         endif()
404     else()
405         message(SEND_ERROR "No supported threading library found")
406     endif()
408     target_sources(liblzma PRIVATE
409         src/common/tuklib_cpucores.c
410         src/common/tuklib_cpucores.h
411         src/liblzma/common/hardware_cputhreads.c
412         src/liblzma/common/outqueue.c
413         src/liblzma/common/outqueue.h
414     )
415 endif()
418 ############
419 # Encoders #
420 ############
422 set(SIMPLE_FILTERS
423     x86
424     arm
425     armthumb
426     arm64
427     powerpc
428     ia64
429     sparc
432 # The SUPPORTED_FILTERS are shared between Encoders and Decoders
433 # since only lzip does not appear in both lists. lzip is a special
434 # case anyway, so it is handled separately in the Decoders section.
435 set(SUPPORTED_FILTERS
436     lzma1
437     lzma2
438     delta
439     "${SIMPLE_FILTERS}"
442 set(ENCODERS "${SUPPORTED_FILTERS}" CACHE STRING "Encoders to support")
444 # If LZMA2 is enabled, then LZMA1 must also be enabled.
445 if(NOT "lzma1" IN_LIST ENCODERS AND "lzma2" IN_LIST ENCODERS)
446     message(SEND_ERROR "LZMA2 encoder requires that LZMA1 is also enabled")
447 endif()
449 # If LZMA1 is enabled, then at least one match finder must be enabled.
450 if(MATCH_FINDERS STREQUAL "" AND "lzma1" IN_LIST ENCODERS)
451     message(SEND_ERROR "At least 1 match finder is required for an "
452                        "LZ-based encoder")
453 endif()
455 set(HAVE_DELTA_CODER OFF)
456 set(SIMPLE_ENCODERS OFF)
457 set(HAVE_ENCODERS OFF)
459 foreach(ENCODER IN LISTS ENCODERS)
460     if(ENCODER IN_LIST SUPPORTED_FILTERS)
461         set(HAVE_ENCODERS ON)
463         if(NOT SIMPLE_ENCODERS AND ENCODER IN_LIST SIMPLE_FILTERS)
464             set(SIMPLE_ENCODERS ON)
465         endif()
467         string(TOUPPER "${ENCODER}" ENCODER_UPPER)
468         add_compile_definitions("HAVE_ENCODER_${ENCODER_UPPER}")
469     else()
470         message(SEND_ERROR "'${ENCODER}' is not a supported encoder")
471     endif()
472 endforeach()
474 if(HAVE_ENCODERS)
475     add_compile_definitions(HAVE_ENCODERS)
477     target_sources(liblzma PRIVATE
478         src/liblzma/common/alone_encoder.c
479         src/liblzma/common/block_buffer_encoder.c
480         src/liblzma/common/block_buffer_encoder.h
481         src/liblzma/common/block_encoder.c
482         src/liblzma/common/block_encoder.h
483         src/liblzma/common/block_header_encoder.c
484         src/liblzma/common/easy_buffer_encoder.c
485         src/liblzma/common/easy_encoder.c
486         src/liblzma/common/easy_encoder_memusage.c
487         src/liblzma/common/filter_buffer_encoder.c
488         src/liblzma/common/filter_encoder.c
489         src/liblzma/common/filter_encoder.h
490         src/liblzma/common/filter_flags_encoder.c
491         src/liblzma/common/index_encoder.c
492         src/liblzma/common/index_encoder.h
493         src/liblzma/common/stream_buffer_encoder.c
494         src/liblzma/common/stream_encoder.c
495         src/liblzma/common/stream_flags_encoder.c
496         src/liblzma/common/vli_encoder.c
497     )
499     if(ENABLE_THREADS)
500         target_sources(liblzma PRIVATE
501             src/liblzma/common/stream_encoder_mt.c
502         )
503     endif()
505     if(SIMPLE_ENCODERS)
506         target_sources(liblzma PRIVATE
507             src/liblzma/simple/simple_encoder.c
508             src/liblzma/simple/simple_encoder.h
509         )
510     endif()
512     if("lzma1" IN_LIST ENCODERS)
513         target_sources(liblzma PRIVATE
514             src/liblzma/lzma/lzma_encoder.c
515             src/liblzma/lzma/lzma_encoder.h
516             src/liblzma/lzma/lzma_encoder_optimum_fast.c
517             src/liblzma/lzma/lzma_encoder_optimum_normal.c
518             src/liblzma/lzma/lzma_encoder_private.h
519             src/liblzma/lzma/fastpos.h
520             src/liblzma/lz/lz_encoder.c
521             src/liblzma/lz/lz_encoder.h
522             src/liblzma/lz/lz_encoder_hash.h
523             src/liblzma/lz/lz_encoder_hash_table.h
524             src/liblzma/lz/lz_encoder_mf.c
525             src/liblzma/rangecoder/price.h
526             src/liblzma/rangecoder/price_table.c
527             src/liblzma/rangecoder/range_encoder.h
528         )
530         if(NOT ENABLE_SMALL)
531             target_sources(liblzma PRIVATE src/liblzma/lzma/fastpos_table.c)
532         endif()
533     endif()
535     if("lzma2" IN_LIST ENCODERS)
536         target_sources(liblzma PRIVATE
537             src/liblzma/lzma/lzma2_encoder.c
538             src/liblzma/lzma/lzma2_encoder.h
539         )
540     endif()
542     if("delta" IN_LIST ENCODERS)
543         set(HAVE_DELTA_CODER ON)
544         target_sources(liblzma PRIVATE
545             src/liblzma/delta/delta_encoder.c
546             src/liblzma/delta/delta_encoder.h
547         )
548     endif()
549 endif()
552 ############
553 # Decoders #
554 ############
556 set(DECODERS "${SUPPORTED_FILTERS}" CACHE STRING "Decoders to support")
558 set(SIMPLE_DECODERS OFF)
559 set(HAVE_DECODERS OFF)
561 foreach(DECODER IN LISTS DECODERS)
562     if(DECODER IN_LIST SUPPORTED_FILTERS)
563         set(HAVE_DECODERS ON)
565         if(NOT SIMPLE_DECODERS AND DECODER IN_LIST SIMPLE_FILTERS)
566             set(SIMPLE_DECODERS ON)
567         endif()
569         string(TOUPPER "${DECODER}" DECODER_UPPER)
570         add_compile_definitions("HAVE_DECODER_${DECODER_UPPER}")
571     else()
572         message(SEND_ERROR "'${DECODER}' is not a supported decoder")
573     endif()
574 endforeach()
576 if(HAVE_DECODERS)
577     add_compile_definitions(HAVE_DECODERS)
579     target_sources(liblzma PRIVATE
580         src/liblzma/common/alone_decoder.c
581         src/liblzma/common/alone_decoder.h
582         src/liblzma/common/auto_decoder.c
583         src/liblzma/common/block_buffer_decoder.c
584         src/liblzma/common/block_decoder.c
585         src/liblzma/common/block_decoder.h
586         src/liblzma/common/block_header_decoder.c
587         src/liblzma/common/easy_decoder_memusage.c
588         src/liblzma/common/file_info.c
589         src/liblzma/common/filter_buffer_decoder.c
590         src/liblzma/common/filter_decoder.c
591         src/liblzma/common/filter_decoder.h
592         src/liblzma/common/filter_flags_decoder.c
593         src/liblzma/common/index_decoder.c
594         src/liblzma/common/index_decoder.h
595         src/liblzma/common/index_hash.c
596         src/liblzma/common/stream_buffer_decoder.c
597         src/liblzma/common/stream_decoder.c
598         src/liblzma/common/stream_flags_decoder.c
599         src/liblzma/common/stream_decoder.h
600         src/liblzma/common/vli_decoder.c
601     )
603     if(ENABLE_THREADS)
604         target_sources(liblzma PRIVATE
605             src/liblzma/common/stream_decoder_mt.c
606         )
607     endif()
609     if(SIMPLE_DECODERS)
610         target_sources(liblzma PRIVATE
611             src/liblzma/simple/simple_decoder.c
612             src/liblzma/simple/simple_decoder.h
613         )
614     endif()
616     if("lzma1" IN_LIST DECODERS)
617         target_sources(liblzma PRIVATE
618             src/liblzma/lzma/lzma_decoder.c
619             src/liblzma/lzma/lzma_decoder.h
620             src/liblzma/rangecoder/range_decoder.h
621             src/liblzma/lz/lz_decoder.c
622             src/liblzma/lz/lz_decoder.h
623         )
624     endif()
626     if("lzma2" IN_LIST DECODERS)
627         target_sources(liblzma PRIVATE
628             src/liblzma/lzma/lzma2_decoder.c
629             src/liblzma/lzma/lzma2_decoder.h
630         )
631     endif()
633     if("delta" IN_LIST DECODERS)
634         set(HAVE_DELTA_CODER ON)
635         target_sources(liblzma PRIVATE
636             src/liblzma/delta/delta_decoder.c
637             src/liblzma/delta/delta_decoder.h
638         )
639     endif()
640 endif()
642 # Some sources must appear if the filter is configured as either
643 # an encoder or decoder.
644 if("lzma1" IN_LIST ENCODERS OR "lzma1" IN_LIST DECODERS)
645     target_sources(liblzma PRIVATE
646         src/liblzma/rangecoder/range_common.h
647         src/liblzma/lzma/lzma_encoder_presets.c
648         src/liblzma/lzma/lzma_common.h
649     )
650 endif()
652 if(HAVE_DELTA_CODER)
653     target_sources(liblzma PRIVATE
654         src/liblzma/delta/delta_common.c
655         src/liblzma/delta/delta_common.h
656         src/liblzma/delta/delta_private.h
657     )
658 endif()
660 if(SIMPLE_ENCODERS OR SIMPLE_DECODERS)
661     target_sources(liblzma PRIVATE
662         src/liblzma/simple/simple_coder.c
663         src/liblzma/simple/simple_coder.h
664         src/liblzma/simple/simple_private.h
665     )
666 endif()
668 foreach(SIMPLE_CODER IN LISTS SIMPLE_FILTERS)
669     if(SIMPLE_CODER IN_LIST ENCODERS OR SIMPLE_CODER IN_LIST DECODERS)
670         target_sources(liblzma PRIVATE "src/liblzma/simple/${SIMPLE_CODER}.c")
671     endif()
672 endforeach()
675 #############
676 # MicroLZMA #
677 #############
679 option(MICROLZMA_ENCODER
680        "MicroLZMA encoder (needed by specific applications only)" ON)
682 option(MICROLZMA_DECODER
683        "MicroLZMA decoder (needed by specific applications only)" ON)
685 if(MICROLZMA_ENCODER)
686     if(NOT "lzma1" IN_LIST ENCODERS)
687         message(SEND_ERROR "The LZMA1 encoder is required to support the "
688                            "MicroLZMA encoder")
689     endif()
691     target_sources(liblzma PRIVATE src/liblzma/common/microlzma_encoder.c)
692 endif()
694 if(MICROLZMA_DECODER)
695     if(NOT "lzma1" IN_LIST DECODERS)
696         message(SEND_ERROR "The LZMA1 decoder is required to support the "
697                            "MicroLZMA decoder")
698     endif()
700     target_sources(liblzma PRIVATE src/liblzma/common/microlzma_decoder.c)
701 endif()
704 #############################
705 # lzip (.lz) format support #
706 #############################
708 option(LZIP_DECODER "Support lzip decoder" ON)
710 if(LZIP_DECODER)
711     # If lzip decoder support is requested, make sure LZMA1 decoder is enabled.
712     if(NOT "lzma1" IN_LIST DECODERS)
713         message(SEND_ERROR "The LZMA1 decoder is required to support the "
714                            "lzip decoder")
715     endif()
717     add_compile_definitions(HAVE_LZIP_DECODER)
719     target_sources(liblzma PRIVATE
720         src/liblzma/common/lzip_decoder.c
721         src/liblzma/common/lzip_decoder.h
722     )
723 endif()
727 # Put the tuklib functions under the lzma_ namespace.
728 target_compile_definitions(liblzma PRIVATE TUKLIB_SYMBOL_PREFIX=lzma_)
729 tuklib_cpucores(liblzma)
730 tuklib_physmem(liblzma)
732 # While liblzma can be built without tuklib_cpucores or tuklib_physmem
733 # modules, the liblzma API functions lzma_cputhreads() and lzma_physmem()
734 # will then be useless (which isn't too bad but still unfortunate). Since
735 # I expect the CMake-based builds to be only used on systems that are
736 # supported by these tuklib modules, problems with these tuklib modules
737 # are considered a hard error for now. This hopefully helps to catch bugs
738 # in the CMake versions of the tuklib checks.
739 if(NOT TUKLIB_CPUCORES_FOUND OR NOT TUKLIB_PHYSMEM_FOUND)
740     # Use SEND_ERROR instead of FATAL_ERROR. If someone reports a bug,
741     # seeing the results of the remaining checks can be useful too.
742     message(SEND_ERROR
743             "tuklib_cpucores() or tuklib_physmem() failed. "
744             "Unless you really are building for a system where these "
745             "modules are not supported (unlikely), this is a bug in the "
746             "included cmake/tuklib_*.cmake files that should be fixed. "
747             "To build anyway, edit this CMakeLists.txt to ignore this error.")
748 endif()
750 # Check for __attribute__((__constructor__)) support.
751 # This needs -Werror because some compilers just warn
752 # about this being unsupported.
753 cmake_push_check_state()
754 set(CMAKE_REQUIRED_FLAGS "-Werror")
755 check_c_source_compiles("
756         __attribute__((__constructor__))
757         static void my_constructor_func(void) { return; }
758         int main(void) { return 0; }
759     "
760     HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
761 cmake_pop_check_state()
762 tuklib_add_definition_if(liblzma HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
764 # The Win95 threading lacks a thread-safe one-time initialization function.
765 # The one-time initialization is needed for crc32_small.c and crc64_small.c
766 # create the CRC tables. So if small mode is enabled, the threading mode is
767 # win95, and the compiler does not support attribute constructor, then we
768 # would end up with a multithreaded build that is thread-unsafe. As a
769 # result this configuration is not allowed.
770 if(USE_WIN95_THREADS AND ENABLE_SMALL AND NOT HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
771     message(SEND_ERROR "Threading method win95 and ENABLE_SMALL "
772                         "cannot be used at the same time with a compiler "
773                         "that doesn't support "
774                         "__attribute__((__constructor__))")
775 endif()
778 # Check for __attribute__((__ifunc__())) support.
779 option(ALLOW_ATTR_IFUNC "Allow use of __attribute__((__ifunc__())) if \
780 supported by the system" ON)
782 if(ALLOW_ATTR_IFUNC)
783     cmake_push_check_state()
784     set(CMAKE_REQUIRED_FLAGS "-Werror")
785     check_c_source_compiles("
786             static void func(void) { return; }
787             static void (*resolve_func(void)) (void) { return func; }
788             void func_ifunc(void)
789                     __attribute__((__ifunc__(\"resolve_func\")));
790             int main(void) { return 0; }
791         "
792         HAVE_FUNC_ATTRIBUTE_IFUNC)
793     cmake_pop_check_state()
794     tuklib_add_definition_if(liblzma HAVE_FUNC_ATTRIBUTE_IFUNC)
795 endif()
797 # cpuid.h
798 check_include_file(cpuid.h HAVE_CPUID_H)
799 tuklib_add_definition_if(liblzma HAVE_CPUID_H)
801 # immintrin.h:
802 check_include_file(immintrin.h HAVE_IMMINTRIN_H)
803 if(HAVE_IMMINTRIN_H)
804     target_compile_definitions(liblzma PRIVATE HAVE_IMMINTRIN_H)
806     # SSE2 intrinsics:
807     check_c_source_compiles("
808             #include <immintrin.h>
809             int main(void)
810             {
811                 __m128i x = { 0 };
812                 _mm_movemask_epi8(x);
813                 return 0;
814             }
815         "
816         HAVE__MM_MOVEMASK_EPI8)
817     tuklib_add_definition_if(liblzma HAVE__MM_MOVEMASK_EPI8)
819     # CLMUL intrinsic:
820     check_c_source_compiles("
821             #include <immintrin.h>
822             #if defined(__e2k__) && __iset__ < 6
823             #   error
824             #endif
825             #if (defined(__GNUC__) || defined(__clang__)) && !defined(__EDG__)
826             __attribute__((__target__(\"ssse3,sse4.1,pclmul\")))
827             #endif
828             __m128i my_clmul(__m128i a)
829             {
830                 const __m128i b = _mm_set_epi64x(1, 2);
831                 return _mm_clmulepi64_si128(a, b, 0);
832             }
833             int main(void) { return 0; }
834     "
835     HAVE_USABLE_CLMUL)
836     tuklib_add_definition_if(liblzma HAVE_USABLE_CLMUL)
837 endif()
839 # Support -fvisiblity=hidden when building shared liblzma.
840 # These lines do nothing on Windows (even under Cygwin).
841 # HAVE_VISIBILITY should always be defined to 0 or 1.
842 if(BUILD_SHARED_LIBS)
843     set_target_properties(liblzma PROPERTIES C_VISIBILITY_PRESET hidden)
844     target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=1)
845 else()
846     target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=0)
847 endif()
849 if(WIN32)
850     if(BUILD_SHARED_LIBS)
851         # Add the Windows resource file for liblzma.dll.
852         target_sources(liblzma PRIVATE src/liblzma/liblzma_w32res.rc)
854         set_target_properties(liblzma PROPERTIES
855             LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
856         )
858         # Export the public API symbols with __declspec(dllexport).
859         target_compile_definitions(liblzma PRIVATE DLL_EXPORT)
860     else()
861         # Disable __declspec(dllimport) when linking against static liblzma.
862         target_compile_definitions(liblzma INTERFACE LZMA_API_STATIC)
863     endif()
864 elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
865     # GNU/Linux-specific symbol versioning for shared liblzma.
866     # Note that adding link options doesn't affect static builds
867     # but HAVE_SYMBOL_VERSIONS_LINUX must not be used with static builds
868     # because it would put symbol versions into the static library which
869     # can cause problems. It's clearer if all symver related things are
870     # omitted when not building a shared library.
871     #
872     # NOTE: Set it explicitly to 1 to make it clear that versioning is
873     # done unconditionally in the C files.
874     target_compile_definitions(liblzma PRIVATE HAVE_SYMBOL_VERSIONS_LINUX=1)
875     target_link_options(liblzma PRIVATE
876         "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
877     )
878     set_target_properties(liblzma PROPERTIES
879         LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
880     )
881 elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
882     # Symbol versioning for shared liblzma for non-GNU/Linux.
883     # FIXME? What about Solaris?
884     target_link_options(liblzma PRIVATE
885         "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
886     )
887     set_target_properties(liblzma PROPERTIES
888         LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
889     )
890 endif()
892 set_target_properties(liblzma PROPERTIES
893     # At least for now the package versioning matches the rules used for
894     # shared library versioning (excluding development releases) so it is
895     # fine to use the package version here.
896     SOVERSION "${xz_VERSION_MAJOR}"
897     VERSION "${xz_VERSION}"
899     # It's liblzma.so or liblzma.dll, not libliblzma.so or lzma.dll.
900     # Avoid the name lzma.dll because it would conflict with LZMA SDK.
901     PREFIX ""
904 # Create liblzma-config-version.cmake.
906 # FIXME: SameMajorVersion is correct for stable releases but it is wrong
907 # for development releases where each release may have incompatible changes.
908 include(CMakePackageConfigHelpers)
909 write_basic_package_version_file(
910     "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
911     VERSION "${liblzma_VERSION}"
912     COMPATIBILITY SameMajorVersion)
914 # Create liblzma-config.cmake. We use this spelling instead of
915 # liblzmaConfig.cmake to make find_package work in case insensitive
916 # manner even with case sensitive file systems. This gives more consistent
917 # behavior between operating systems. This optionally includes a dependency
918 # on a threading library, so the contents are created in two separate parts.
919 # The "second half" is always needed, so create it first.
920 set(LZMA_CONFIG_CONTENTS
921 "include(\"\${CMAKE_CURRENT_LIST_DIR}/liblzma-targets.cmake\")
923 if(NOT TARGET LibLZMA::LibLZMA)
924     # Be compatible with the spelling used by the FindLibLZMA module. This
925     # doesn't use ALIAS because it would make CMake resolve LibLZMA::LibLZMA
926     # to liblzma::liblzma instead of keeping the original spelling. Keeping
927     # the original spelling is important for good FindLibLZMA compatibility.
928     add_library(LibLZMA::LibLZMA INTERFACE IMPORTED)
929     set_target_properties(LibLZMA::LibLZMA PROPERTIES
930                           INTERFACE_LINK_LIBRARIES liblzma::liblzma)
931 endif()
934 if(ENABLE_THREADS STREQUAL "posix")
935     set(LZMA_CONFIG_CONTENTS
936 "include(CMakeFindDependencyMacro)
937 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
938 find_dependency(Threads)
940 ${LZMA_CONFIG_CONTENTS}
942 endif()
944 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
945         "${LZMA_CONFIG_CONTENTS}")
947 # Set CMAKE_INSTALL_LIBDIR and friends.
948 include(GNUInstallDirs)
950 # Install the library binary. The INCLUDES specifies the include path that
951 # is exported for other projects to use but it doesn't install any files.
952 install(TARGETS liblzma EXPORT liblzmaTargets
953         RUNTIME  DESTINATION "${CMAKE_INSTALL_BINDIR}"
954                  COMPONENT liblzma_Runtime
955         LIBRARY  DESTINATION "${CMAKE_INSTALL_LIBDIR}"
956                  COMPONENT liblzma_Runtime
957                  NAMELINK_COMPONENT liblzma_Development
958         ARCHIVE  DESTINATION "${CMAKE_INSTALL_LIBDIR}"
959                  COMPONENT liblzma_Development
960         INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
962 # Install the liblzma API headers. These use a subdirectory so
963 # this has to be done as a separate step.
964 install(DIRECTORY src/liblzma/api/
965         COMPONENT liblzma_Development
966         DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
967         FILES_MATCHING PATTERN "*.h")
969 # Install the CMake files that other packages can use to find liblzma.
970 set(liblzma_INSTALL_CMAKEDIR
971     "${CMAKE_INSTALL_LIBDIR}/cmake/liblzma"
972     CACHE STRING "Path to liblzma's .cmake files")
974 install(EXPORT liblzmaTargets
975         NAMESPACE liblzma::
976         FILE liblzma-targets.cmake
977         DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
978         COMPONENT liblzma_Development)
980 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
981               "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
982         DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
983         COMPONENT liblzma_Development)
986 #############################################################################
987 # getopt_long
988 #############################################################################
990 # The command line tools needs this.
991 check_symbol_exists(getopt_long getopt.h HAVE_GETOPT_LONG)
994 #############################################################################
995 # xzdec
996 #############################################################################
998 if(HAVE_GETOPT_LONG AND HAVE_DECODERS)
999     add_executable(xzdec
1000         src/common/sysdefs.h
1001         src/common/tuklib_common.h
1002         src/common/tuklib_config.h
1003         src/common/tuklib_exit.c
1004         src/common/tuklib_exit.h
1005         src/common/tuklib_gettext.h
1006         src/common/tuklib_progname.c
1007         src/common/tuklib_progname.h
1008         src/xzdec/xzdec.c
1009     )
1011     target_include_directories(xzdec PRIVATE
1012         src/common
1013         src/liblzma/api
1014     )
1016     target_link_libraries(xzdec PRIVATE liblzma)
1018     if(WIN32)
1019         # Add the Windows resource file for xzdec.exe.
1020         target_sources(xzdec PRIVATE src/xzdec/xzdec_w32res.rc)
1021         set_target_properties(xzdec PROPERTIES
1022             LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1023         )
1024     endif()
1026     tuklib_progname(xzdec)
1028     install(TARGETS xzdec
1029             RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1030                     COMPONENT xzdec)
1032     if(UNIX)
1033         install(FILES src/xzdec/xzdec.1
1034                 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1035                 COMPONENT xzdec)
1036     endif()
1037 endif()
1040 #############################################################################
1041 # xz
1042 #############################################################################
1044 if(NOT MSVC AND HAVE_GETOPT_LONG)
1045     add_executable(xz
1046         src/common/mythread.h
1047         src/common/sysdefs.h
1048         src/common/tuklib_common.h
1049         src/common/tuklib_config.h
1050         src/common/tuklib_exit.c
1051         src/common/tuklib_exit.h
1052         src/common/tuklib_gettext.h
1053         src/common/tuklib_integer.h
1054         src/common/tuklib_mbstr.h
1055         src/common/tuklib_mbstr_fw.c
1056         src/common/tuklib_mbstr_width.c
1057         src/common/tuklib_open_stdxxx.c
1058         src/common/tuklib_open_stdxxx.h
1059         src/common/tuklib_progname.c
1060         src/common/tuklib_progname.h
1061         src/xz/args.c
1062         src/xz/args.h
1063         src/xz/coder.c
1064         src/xz/coder.h
1065         src/xz/file_io.c
1066         src/xz/file_io.h
1067         src/xz/hardware.c
1068         src/xz/hardware.h
1069         src/xz/main.c
1070         src/xz/main.h
1071         src/xz/message.c
1072         src/xz/message.h
1073         src/xz/mytime.c
1074         src/xz/mytime.h
1075         src/xz/options.c
1076         src/xz/options.h
1077         src/xz/private.h
1078         src/xz/signals.c
1079         src/xz/signals.h
1080         src/xz/suffix.c
1081         src/xz/suffix.h
1082         src/xz/util.c
1083         src/xz/util.h
1084     )
1086     target_include_directories(xz PRIVATE
1087         src/common
1088         src/liblzma/api
1089     )
1091     if(HAVE_DECODERS)
1092         target_sources(xz PRIVATE
1093             src/xz/list.c
1094             src/xz/list.h
1095         )
1096     endif()
1098     target_link_libraries(xz PRIVATE liblzma)
1100     target_compile_definitions(xz PRIVATE ASSUME_RAM=128)
1102     if(WIN32)
1103         # Add the Windows resource file for xz.exe.
1104         target_sources(xz PRIVATE src/xz/xz_w32res.rc)
1105         set_target_properties(xz PROPERTIES
1106             LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1107         )
1108     endif()
1110     tuklib_progname(xz)
1111     tuklib_mbstr(xz)
1113     check_symbol_exists(optreset getopt.h HAVE_OPTRESET)
1114     tuklib_add_definition_if(xz HAVE_OPTRESET)
1116     check_symbol_exists(posix_fadvise fcntl.h HAVE_POSIX_FADVISE)
1117     tuklib_add_definition_if(xz HAVE_POSIX_FADVISE)
1119     # How to get file time:
1120     check_struct_has_member("struct stat" st_atim.tv_nsec
1121                             "sys/types.h;sys/stat.h"
1122                             HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1123     if(HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1124         tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1125     else()
1126         check_struct_has_member("struct stat" st_atimespec.tv_nsec
1127                                 "sys/types.h;sys/stat.h"
1128                                 HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1129         if(HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1130             tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1131         else()
1132             check_struct_has_member("struct stat" st_atimensec
1133                                     "sys/types.h;sys/stat.h"
1134                                     HAVE_STRUCT_STAT_ST_ATIMENSEC)
1135             tuklib_add_definition_if(xz HAVE_STRUCT_STAT_ST_ATIMENSEC)
1136         endif()
1137     endif()
1139     # How to set file time:
1140     check_symbol_exists(futimens "sys/types.h;sys/stat.h" HAVE_FUTIMENS)
1141     if(HAVE_FUTIMENS)
1142         tuklib_add_definitions(xz HAVE_FUTIMENS)
1143     else()
1144         check_symbol_exists(futimes "sys/time.h" HAVE_FUTIMES)
1145         if(HAVE_FUTIMES)
1146             tuklib_add_definitions(xz HAVE_FUTIMES)
1147         else()
1148             check_symbol_exists(futimesat "sys/time.h" HAVE_FUTIMESAT)
1149             if(HAVE_FUTIMESAT)
1150                 tuklib_add_definitions(xz HAVE_FUTIMESAT)
1151             else()
1152                 check_symbol_exists(utimes "sys/time.h" HAVE_UTIMES)
1153                 if(HAVE_UTIMES)
1154                     tuklib_add_definitions(xz HAVE_UTIMES)
1155                 else()
1156                     check_symbol_exists(_futime "sys/utime.h" HAVE__FUTIME)
1157                     if(HAVE__FUTIME)
1158                         tuklib_add_definitions(xz HAVE__FUTIME)
1159                     else()
1160                         check_symbol_exists(utime "utime.h" HAVE_UTIME)
1161                         tuklib_add_definition_if(xz HAVE_UTIME)
1162                     endif()
1163                 endif()
1164             endif()
1165         endif()
1166     endif()
1168     install(TARGETS xz
1169             RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1170                     COMPONENT xz)
1172     if(UNIX)
1173         install(FILES src/xz/xz.1
1174                 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1175                 COMPONENT xz)
1177         option(CREATE_XZ_SYMLINKS "Create unxz and xzcat symlinks" ON)
1178         option(CREATE_LZMA_SYMLINKS "Create lzma, unlzma, and lzcat symlinks"
1179                ON)
1180         set(XZ_LINKS)
1182         if(CREATE_XZ_SYMLINKS)
1183             list(APPEND XZ_LINKS "unxz" "xzcat")
1184         endif()
1186         if(CREATE_LZMA_SYMLINKS)
1187             list(APPEND XZ_LINKS "lzma" "unlzma" "lzcat")
1188         endif()
1190         # With Windows Cygwin and MSYS2 the symlinking is complicated. Both
1191         # of these environments set the UNIX variable so they will try to
1192         # make the symlinks. The ability for Cygwin and MSYS2 to make
1193         # broken symlinks is determined by the CYGWIN and MSYS2 environment
1194         # variables, respectively. Broken symlinks are needed for the man
1195         # page symlinks and for determining if the xz and lzma symlinks need
1196         # to depend on the xz target or not. If broken symlinks cannot be
1197         # made then the xz binary must be created before the symlinks.
1198         set(ALLOW_BROKEN_SYMLINKS ON)
1200         if(CMAKE_SYSTEM_NAME STREQUAL "CYGWIN")
1201             # The Cygwin env variable can be set to four possible values:
1202             #
1203             # 1. "lnk". Create symlinks as Windows shortcuts.
1204             #
1205             # 2. "native". Create symlinks as native Windows symlinks
1206             #    if supported by the system. Fallback to "lnk" if native
1207             #    symlinks are not supported.
1208             #
1209             # 3. "nativestrict". Create symlinks as native Windows symlinks
1210             #    if supported by the system. If the target of the symlink
1211             #    does not exist or the creation of the symlink fails for any
1212             #    reason, do not create the symlink.
1213             #
1214             # 4. "sys". Create symlinks as plain files with a special
1215             #    system attribute containing the path to the symlink target.
1216             #
1217             # So, the only case we care about for broken symlinks is
1218             # "nativestrict" since all other values mean that broken
1219             # symlinks are allowed. If the env variable is not set the
1220             # default is "native". If the env variable is set but not
1221             # assigned one of the four values, then the default is the same
1222             # as option 1 "lnk".
1223             string(FIND "$ENV{CYGWIN}" "winsymlinks:nativestrict" SYMLINK_POS)
1224             if(SYMLINK_POS GREATER -1)
1225                 set(ALLOW_BROKEN_SYMLINKS OFF)
1226             endif()
1227         elseif(CMAKE_SYSTEM_NAME STREQUAL "MSYS")
1228             # The MSYS env variable behaves similar to the CYGWIN but has a
1229             # different default behavior. If winsymlinks is set but not
1230             # assigned one of the four supported values, the default is to
1231             # *copy* the target to the symlink destination. This will fail
1232             # if the target does not exist so broken symlinks cannot be
1233             # allowed.
1234             string(FIND "$ENV{MSYS}" "winsymlinks" SYMLINK_POS)
1235             if(SYMLINK_POS GREATER -1)
1236                 string(FIND "$ENV{MSYS}" "winsymlinks:nativestrict"
1237                         SYMLINK_POS)
1238                 if(SYMLINK_POS GREATER -1)
1239                     set(ALLOW_BROKEN_SYMLINKS OFF)
1240                 endif()
1241             else()
1242                 set(ALLOW_BROKEN_SYMLINKS OFF)
1243             endif()
1244         endif()
1246         # Create symlinks in the build directory and then install them.
1247         #
1248         # The symlinks do not likely need any special extension since
1249         # even on Windows the symlink can still be executed without
1250         # the .exe extension.
1251         foreach(LINK IN LISTS XZ_LINKS)
1252             add_custom_target("${LINK}" ALL
1253                 "${CMAKE_COMMAND}" -E create_symlink
1254                     "$<TARGET_FILE_NAME:xz>" "${LINK}"
1255                 BYPRODUCTS "${LINK}"
1256                 VERBATIM)
1257             install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}"
1258                     DESTINATION "${CMAKE_INSTALL_BINDIR}"
1259                     COMPONENT xz)
1261             # Only create the man page symlinks if the symlinks can be
1262             # created broken. The symlinks will not be valid until install
1263             # so they cannot be created on these system environments.
1264             if(ALLOW_BROKEN_SYMLINKS)
1265                 add_custom_target("${LINK}.1" ALL
1266                     "${CMAKE_COMMAND}" -E create_symlink "xz.1" "${LINK}.1"
1267                     BYPRODUCTS "${LINK}.1"
1268                     VERBATIM)
1269                 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}.1"
1270                         DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1271                         COMPONENT xz)
1272             else()
1273                 # Add the xz target as dependency when broken symlinks
1274                 # cannot be made. This ensures parallel builds do not fail
1275                 # since it will enforce the order of creating xz first, then
1276                 # the symlinks.
1277                 add_dependencies("${LINK}" xz)
1278             endif()
1279         endforeach()
1280     endif()
1281 endif()
1284 #############################################################################
1285 # Tests
1286 #############################################################################
1288 include(CTest)
1290 if(BUILD_TESTING)
1291     set(LIBLZMA_TESTS
1292         test_bcj_exact_size
1293         test_block_header
1294         test_check
1295         test_filter_flags
1296         test_filter_str
1297         test_hardware
1298         test_index
1299         test_index_hash
1300         test_lzip_decoder
1301         test_memlimit
1302         test_stream_flags
1303         test_vli
1304     )
1306     foreach(TEST IN LISTS LIBLZMA_TESTS)
1307         add_executable("${TEST}" "tests/${TEST}.c")
1309         target_include_directories("${TEST}" PRIVATE
1310             src/common
1311             src/liblzma/api
1312             src/liblzma
1313             lib
1314         )
1316         target_link_libraries("${TEST}" PRIVATE liblzma)
1318         # Put the test programs into their own subdirectory so they don't
1319         # pollute the top-level dir which might contain xz and xzdec.
1320         set_target_properties("${TEST}" PROPERTIES
1321             RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tests_bin"
1322         )
1324         add_test(NAME "${TEST}"
1325                  COMMAND "${CMAKE_CURRENT_BINARY_DIR}/tests_bin/${TEST}"
1326         )
1328         # Set srcdir environment variable so that the tests find their
1329         # input files from the source tree.
1330         #
1331         # Set the return code for skipped tests to match Automake convention.
1332         set_tests_properties("${TEST}" PROPERTIES
1333             ENVIRONMENT "srcdir=${CMAKE_CURRENT_LIST_DIR}/tests"
1334             SKIP_RETURN_CODE 77
1335         )
1336     endforeach()
1337 endif()