CI: Add Clang64 MSYS2 environment to Windows CI.
[xz.git] / CMakeLists.txt
blob7aed9d9fc1c25c2584ab8114a369a60b1cb10461
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 if(NOT ENABLE_THREADS IN_LIST SUPPORTED_THREAD_METHODS)
352     message(SEND_ERROR "'${ENABLE_THREADS}' is not a supported thread type")
353 endif()
355 if(ENABLE_THREADS)
356     # Also set THREADS_PREFER_PTHREAD_FLAG since the flag has no effect
357     # for Windows threading.
358     set(THREADS_PREFER_PTHREAD_FLAG TRUE)
359     find_package(Threads REQUIRED)
361     # If both Windows and posix threading are available, prefer Windows.
362     if(CMAKE_USE_WIN32_THREADS_INIT AND NOT ENABLE_THREADS STREQUAL "posix")
363         if(ENABLE_THREADS STREQUAL "win95"
364                 OR (ENABLE_THREADS STREQUAL "ON"
365                 AND CMAKE_SIZEOF_VOID_P EQUAL 4))
366             # Use Windows 95 (and thus XP) compatible threads.
367             # This avoids use of features that were added in
368             # Windows Vista. This is used for 32-bit x86 builds for
369             # compatibility reasons since it makes no measurable difference
370             # in performance compared to Vista threads.
371             #
372             # The Win95 threading lacks thread-safe one-time initialization
373             # function.
374             if(ENABLE_SMALL)
375                 message(SEND_ERROR "Threading method win95 and ENABLE_SMALL "
376                                    "cannot be used at the same time")
377             endif()
379             add_compile_definitions(MYTHREAD_WIN95)
380         else()
381             add_compile_definitions(MYTHREAD_VISTA)
382         endif()
383     elseif(CMAKE_USE_PTHREADS_INIT)
384         if(ENABLE_THREADS STREQUAL "posix" OR ENABLE_THREADS STREQUAL "ON")
385             # Overwrite ENABLE_THREADS in case it was set to "ON".
386             # The threading library only needs to be explicitly linked
387             # for posix threads, so this is needed for creating
388             # liblzma-config.cmake later.
389             set(ENABLE_THREADS "posix")
391             target_link_libraries(liblzma Threads::Threads)
392             add_compile_definitions(MYTHREAD_POSIX)
394             # Check if pthread_condattr_setclock() exists to use CLOCK_MONOTONIC.
395             if(HAVE_CLOCK_MONOTONIC)
396                 list(INSERT CMAKE_REQUIRED_LIBRARIES 0 "${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 # Check for __attribute__((__ifunc__())) support.
767 option(ALLOW_ATTR_IFUNC "Allow use of __attribute__((__ifunc__())) if \
768 supported by the system" ON)
770 if(ALLOW_ATTR_IFUNC)
771     cmake_push_check_state()
772     set(CMAKE_REQUIRED_FLAGS "-Werror")
773     check_c_source_compiles("
774             static void func(void) { return; }
775             static void (*resolve_func(void)) (void) { return func; }
776             void func_ifunc(void)
777                     __attribute__((__ifunc__(\"resolve_func\")));
778             int main(void) { return 0; }
779         "
780         HAVE_FUNC_ATTRIBUTE_IFUNC)
781     cmake_pop_check_state()
782     tuklib_add_definition_if(liblzma HAVE_FUNC_ATTRIBUTE_IFUNC)
783 endif()
785 # cpuid.h
786 check_include_file(cpuid.h HAVE_CPUID_H)
787 tuklib_add_definition_if(liblzma HAVE_CPUID_H)
789 # immintrin.h:
790 check_include_file(immintrin.h HAVE_IMMINTRIN_H)
791 if(HAVE_IMMINTRIN_H)
792     target_compile_definitions(liblzma PRIVATE HAVE_IMMINTRIN_H)
794     # SSE2 intrinsics:
795     check_c_source_compiles("
796             #include <immintrin.h>
797             int main(void)
798             {
799                 __m128i x = { 0 };
800                 _mm_movemask_epi8(x);
801                 return 0;
802             }
803         "
804         HAVE__MM_MOVEMASK_EPI8)
805     tuklib_add_definition_if(liblzma HAVE__MM_MOVEMASK_EPI8)
807     # CLMUL intrinsic:
808     check_c_source_compiles("
809             #include <immintrin.h>
810             #if defined(__e2k__) && __iset__ < 6
811             #   error
812             #endif
813             #if (defined(__GNUC__) || defined(__clang__)) && !defined(__EDG__)
814             __attribute__((__target__(\"ssse3,sse4.1,pclmul\")))
815             #endif
816             __m128i my_clmul(__m128i a)
817             {
818                 const __m128i b = _mm_set_epi64x(1, 2);
819                 return _mm_clmulepi64_si128(a, b, 0);
820             }
821             int main(void) { return 0; }
822     "
823     HAVE_USABLE_CLMUL)
824     tuklib_add_definition_if(liblzma HAVE_USABLE_CLMUL)
825 endif()
827 # Support -fvisiblity=hidden when building shared liblzma.
828 # These lines do nothing on Windows (even under Cygwin).
829 # HAVE_VISIBILITY should always be defined to 0 or 1.
830 if(BUILD_SHARED_LIBS)
831     set_target_properties(liblzma PROPERTIES C_VISIBILITY_PRESET hidden)
832     target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=1)
833 else()
834     target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=0)
835 endif()
837 if(WIN32)
838     if(BUILD_SHARED_LIBS)
839         # Add the Windows resource file for liblzma.dll.
840         target_sources(liblzma PRIVATE src/liblzma/liblzma_w32res.rc)
842         set_target_properties(liblzma PROPERTIES
843             LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
844         )
846         # Export the public API symbols with __declspec(dllexport).
847         target_compile_definitions(liblzma PRIVATE DLL_EXPORT)
848     else()
849         # Disable __declspec(dllimport) when linking against static liblzma.
850         target_compile_definitions(liblzma INTERFACE LZMA_API_STATIC)
851     endif()
852 elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
853     # GNU/Linux-specific symbol versioning for shared liblzma.
854     # Note that adding link options doesn't affect static builds
855     # but HAVE_SYMBOL_VERSIONS_LINUX must not be used with static builds
856     # because it would put symbol versions into the static library which
857     # can cause problems. It's clearer if all symver related things are
858     # omitted when not building a shared library.
859     #
860     # NOTE: Set it explicitly to 1 to make it clear that versioning is
861     # done unconditionally in the C files.
862     target_compile_definitions(liblzma PRIVATE HAVE_SYMBOL_VERSIONS_LINUX=1)
863     target_link_options(liblzma PRIVATE
864         "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
865     )
866     set_target_properties(liblzma PROPERTIES
867         LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
868     )
869 elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
870     # Symbol versioning for shared liblzma for non-GNU/Linux.
871     # FIXME? What about Solaris?
872     target_link_options(liblzma PRIVATE
873         "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
874     )
875     set_target_properties(liblzma PROPERTIES
876         LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
877     )
878 endif()
880 set_target_properties(liblzma PROPERTIES
881     # At least for now the package versioning matches the rules used for
882     # shared library versioning (excluding development releases) so it is
883     # fine to use the package version here.
884     SOVERSION "${xz_VERSION_MAJOR}"
885     VERSION "${xz_VERSION}"
887     # It's liblzma.so or liblzma.dll, not libliblzma.so or lzma.dll.
888     # Avoid the name lzma.dll because it would conflict with LZMA SDK.
889     PREFIX ""
892 # Create liblzma-config-version.cmake.
894 # FIXME: SameMajorVersion is correct for stable releases but it is wrong
895 # for development releases where each release may have incompatible changes.
896 include(CMakePackageConfigHelpers)
897 write_basic_package_version_file(
898     "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
899     VERSION "${liblzma_VERSION}"
900     COMPATIBILITY SameMajorVersion)
902 # Create liblzma-config.cmake. We use this spelling instead of
903 # liblzmaConfig.cmake to make find_package work in case insensitive
904 # manner even with case sensitive file systems. This gives more consistent
905 # behavior between operating systems. This optionally includes a dependency
906 # on a threading library, so the contents are created in two separate parts.
907 # The "second half" is always needed, so create it first.
908 set(LZMA_CONFIG_CONTENTS
909 "include(\"\${CMAKE_CURRENT_LIST_DIR}/liblzma-targets.cmake\")
911 if(NOT TARGET LibLZMA::LibLZMA)
912     # Be compatible with the spelling used by the FindLibLZMA module. This
913     # doesn't use ALIAS because it would make CMake resolve LibLZMA::LibLZMA
914     # to liblzma::liblzma instead of keeping the original spelling. Keeping
915     # the original spelling is important for good FindLibLZMA compatibility.
916     add_library(LibLZMA::LibLZMA INTERFACE IMPORTED)
917     set_target_properties(LibLZMA::LibLZMA PROPERTIES
918                           INTERFACE_LINK_LIBRARIES liblzma::liblzma)
919 endif()
922 if(ENABLE_THREADS STREQUAL "posix")
923     set(LZMA_CONFIG_CONTENTS
924 "include(CMakeFindDependencyMacro)
925 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
926 find_dependency(Threads)
928 ${LZMA_CONFIG_CONTENTS}
930 endif()
932 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
933         "${LZMA_CONFIG_CONTENTS}")
935 # Set CMAKE_INSTALL_LIBDIR and friends.
936 include(GNUInstallDirs)
938 # Install the library binary. The INCLUDES specifies the include path that
939 # is exported for other projects to use but it doesn't install any files.
940 install(TARGETS liblzma EXPORT liblzmaTargets
941         RUNTIME  DESTINATION "${CMAKE_INSTALL_BINDIR}"
942                  COMPONENT liblzma_Runtime
943         LIBRARY  DESTINATION "${CMAKE_INSTALL_LIBDIR}"
944                  COMPONENT liblzma_Runtime
945                  NAMELINK_COMPONENT liblzma_Development
946         ARCHIVE  DESTINATION "${CMAKE_INSTALL_LIBDIR}"
947                  COMPONENT liblzma_Development
948         INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
950 # Install the liblzma API headers. These use a subdirectory so
951 # this has to be done as a separate step.
952 install(DIRECTORY src/liblzma/api/
953         COMPONENT liblzma_Development
954         DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
955         FILES_MATCHING PATTERN "*.h")
957 # Install the CMake files that other packages can use to find liblzma.
958 set(liblzma_INSTALL_CMAKEDIR
959     "${CMAKE_INSTALL_LIBDIR}/cmake/liblzma"
960     CACHE STRING "Path to liblzma's .cmake files")
962 install(EXPORT liblzmaTargets
963         NAMESPACE liblzma::
964         FILE liblzma-targets.cmake
965         DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
966         COMPONENT liblzma_Development)
968 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
969               "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
970         DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
971         COMPONENT liblzma_Development)
974 #############################################################################
975 # getopt_long
976 #############################################################################
978 # The command line tools needs this.
979 check_symbol_exists(getopt_long getopt.h HAVE_GETOPT_LONG)
982 #############################################################################
983 # xzdec
984 #############################################################################
986 if(HAVE_GETOPT_LONG AND HAVE_DECODERS)
987     add_executable(xzdec
988         src/common/sysdefs.h
989         src/common/tuklib_common.h
990         src/common/tuklib_config.h
991         src/common/tuklib_exit.c
992         src/common/tuklib_exit.h
993         src/common/tuklib_gettext.h
994         src/common/tuklib_progname.c
995         src/common/tuklib_progname.h
996         src/xzdec/xzdec.c
997     )
999     target_include_directories(xzdec PRIVATE
1000         src/common
1001         src/liblzma/api
1002     )
1004     target_link_libraries(xzdec PRIVATE liblzma)
1006     if(WIN32)
1007         # Add the Windows resource file for xzdec.exe.
1008         target_sources(xzdec PRIVATE src/xzdec/xzdec_w32res.rc)
1009         set_target_properties(xzdec PROPERTIES
1010             LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1011         )
1012     endif()
1014     tuklib_progname(xzdec)
1016     install(TARGETS xzdec
1017             RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1018                     COMPONENT xzdec)
1020     if(UNIX)
1021         install(FILES src/xzdec/xzdec.1
1022                 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1023                 COMPONENT xzdec)
1024     endif()
1025 endif()
1028 #############################################################################
1029 # xz
1030 #############################################################################
1032 if(NOT MSVC AND HAVE_GETOPT_LONG)
1033     add_executable(xz
1034         src/common/mythread.h
1035         src/common/sysdefs.h
1036         src/common/tuklib_common.h
1037         src/common/tuklib_config.h
1038         src/common/tuklib_exit.c
1039         src/common/tuklib_exit.h
1040         src/common/tuklib_gettext.h
1041         src/common/tuklib_integer.h
1042         src/common/tuklib_mbstr.h
1043         src/common/tuklib_mbstr_fw.c
1044         src/common/tuklib_mbstr_width.c
1045         src/common/tuklib_open_stdxxx.c
1046         src/common/tuklib_open_stdxxx.h
1047         src/common/tuklib_progname.c
1048         src/common/tuklib_progname.h
1049         src/xz/args.c
1050         src/xz/args.h
1051         src/xz/coder.c
1052         src/xz/coder.h
1053         src/xz/file_io.c
1054         src/xz/file_io.h
1055         src/xz/hardware.c
1056         src/xz/hardware.h
1057         src/xz/main.c
1058         src/xz/main.h
1059         src/xz/message.c
1060         src/xz/message.h
1061         src/xz/mytime.c
1062         src/xz/mytime.h
1063         src/xz/options.c
1064         src/xz/options.h
1065         src/xz/private.h
1066         src/xz/signals.c
1067         src/xz/signals.h
1068         src/xz/suffix.c
1069         src/xz/suffix.h
1070         src/xz/util.c
1071         src/xz/util.h
1072     )
1074     target_include_directories(xz PRIVATE
1075         src/common
1076         src/liblzma/api
1077     )
1079     if(HAVE_DECODERS)
1080         target_sources(xz PRIVATE
1081             src/xz/list.c
1082             src/xz/list.h
1083         )
1084     endif()
1086     target_link_libraries(xz PRIVATE liblzma)
1088     target_compile_definitions(xz PRIVATE ASSUME_RAM=128)
1090     if(WIN32)
1091         # Add the Windows resource file for xz.exe.
1092         target_sources(xz PRIVATE src/xz/xz_w32res.rc)
1093         set_target_properties(xz PROPERTIES
1094             LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
1095         )
1096     endif()
1098     tuklib_progname(xz)
1099     tuklib_mbstr(xz)
1101     check_symbol_exists(optreset getopt.h HAVE_OPTRESET)
1102     tuklib_add_definition_if(xz HAVE_OPTRESET)
1104     check_symbol_exists(posix_fadvise fcntl.h HAVE_POSIX_FADVISE)
1105     tuklib_add_definition_if(xz HAVE_POSIX_FADVISE)
1107     # How to get file time:
1108     check_struct_has_member("struct stat" st_atim.tv_nsec
1109                             "sys/types.h;sys/stat.h"
1110                             HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1111     if(HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1112         tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
1113     else()
1114         check_struct_has_member("struct stat" st_atimespec.tv_nsec
1115                                 "sys/types.h;sys/stat.h"
1116                                 HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1117         if(HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1118             tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
1119         else()
1120             check_struct_has_member("struct stat" st_atimensec
1121                                     "sys/types.h;sys/stat.h"
1122                                     HAVE_STRUCT_STAT_ST_ATIMENSEC)
1123             tuklib_add_definition_if(xz HAVE_STRUCT_STAT_ST_ATIMENSEC)
1124         endif()
1125     endif()
1127     # How to set file time:
1128     check_symbol_exists(futimens "sys/types.h;sys/stat.h" HAVE_FUTIMENS)
1129     if(HAVE_FUTIMENS)
1130         tuklib_add_definitions(xz HAVE_FUTIMENS)
1131     else()
1132         check_symbol_exists(futimes "sys/time.h" HAVE_FUTIMES)
1133         if(HAVE_FUTIMES)
1134             tuklib_add_definitions(xz HAVE_FUTIMES)
1135         else()
1136             check_symbol_exists(futimesat "sys/time.h" HAVE_FUTIMESAT)
1137             if(HAVE_FUTIMESAT)
1138                 tuklib_add_definitions(xz HAVE_FUTIMESAT)
1139             else()
1140                 check_symbol_exists(utimes "sys/time.h" HAVE_UTIMES)
1141                 if(HAVE_UTIMES)
1142                     tuklib_add_definitions(xz HAVE_UTIMES)
1143                 else()
1144                     check_symbol_exists(_futime "sys/utime.h" HAVE__FUTIME)
1145                     if(HAVE__FUTIME)
1146                         tuklib_add_definitions(xz HAVE__FUTIME)
1147                     else()
1148                         check_symbol_exists(utime "utime.h" HAVE_UTIME)
1149                         tuklib_add_definition_if(xz HAVE_UTIME)
1150                     endif()
1151                 endif()
1152             endif()
1153         endif()
1154     endif()
1156     install(TARGETS xz
1157             RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
1158                     COMPONENT xz)
1160     if(UNIX)
1161         install(FILES src/xz/xz.1
1162                 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1163                 COMPONENT xz)
1165         option(CREATE_XZ_SYMLINKS "Create unxz and xzcat symlinks" ON)
1166         option(CREATE_LZMA_SYMLINKS "Create lzma, unlzma, and lzcat symlinks"
1167                ON)
1168         set(XZ_LINKS)
1170         if(CREATE_XZ_SYMLINKS)
1171             list(APPEND XZ_LINKS "unxz" "xzcat")
1172         endif()
1174         if(CREATE_LZMA_SYMLINKS)
1175             list(APPEND XZ_LINKS "lzma" "unlzma" "lzcat")
1176         endif()
1178         # Create symlinks in the build directory and then install them.
1179         #
1180         # The symlinks do not likely need any special extension since
1181         # even on Windows the symlink can still be executed without
1182         # the .exe extension.
1183         foreach(LINK IN LISTS XZ_LINKS)
1184             add_custom_target("${LINK}" ALL
1185                 "${CMAKE_COMMAND}" -E create_symlink
1186                     "$<TARGET_FILE_NAME:xz>" "${LINK}"
1187                 BYPRODUCTS "${LINK}"
1188                 VERBATIM)
1189             install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}"
1190                     DESTINATION "${CMAKE_INSTALL_BINDIR}"
1191                     COMPONENT xz)
1192             add_custom_target("${LINK}.1" ALL
1193                 "${CMAKE_COMMAND}" -E create_symlink "xz.1" "${LINK}.1"
1194                 BYPRODUCTS "${LINK}.1"
1195                 VERBATIM)
1196             install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}.1"
1197                     DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
1198                     COMPONENT xz)
1199         endforeach()
1200     endif()
1201 endif()
1204 #############################################################################
1205 # Tests
1206 #############################################################################
1208 include(CTest)
1210 if(BUILD_TESTING)
1211     set(LIBLZMA_TESTS
1212         test_bcj_exact_size
1213         test_block_header
1214         test_check
1215         test_filter_flags
1216         test_filter_str
1217         test_hardware
1218         test_index
1219         test_index_hash
1220         test_lzip_decoder
1221         test_memlimit
1222         test_stream_flags
1223         test_vli
1224     )
1226     foreach(TEST IN LISTS LIBLZMA_TESTS)
1227         add_executable("${TEST}" "tests/${TEST}.c")
1229         target_include_directories("${TEST}" PRIVATE
1230             src/common
1231             src/liblzma/api
1232             src/liblzma
1233             lib
1234         )
1236         target_link_libraries("${TEST}" PRIVATE liblzma)
1238         # Put the test programs into their own subdirectory so they don't
1239         # pollute the top-level dir which might contain xz and xzdec.
1240         set_target_properties("${TEST}" PROPERTIES
1241             RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tests_bin"
1242         )
1244         add_test(NAME "${TEST}"
1245                  COMMAND "${CMAKE_CURRENT_BINARY_DIR}/tests_bin/${TEST}"
1246         )
1248         # Set srcdir environment variable so that the tests find their
1249         # input files from the source tree.
1250         #
1251         # Set the return code for skipped tests to match Automake convention.
1252         set_tests_properties("${TEST}" PROPERTIES
1253             ENVIRONMENT "srcdir=${CMAKE_CURRENT_LIST_DIR}/tests"
1254             SKIP_RETURN_CODE 77
1255         )
1256     endforeach()
1257 endif()