Tests: Fix type-limits warning in test_filter_flags.
[xz.git] / CMakeLists.txt
blobe5b4e5b5e244b466947c330ebf7335a277fbcd4d
1 #############################################################################
3 # Very limited CMake support for building some parts of XZ Utils
5 # For now, this is indented 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.25 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 #define LZMA_VERSION_MINOR ([0-9]+)\n\
68 #define LZMA_VERSION_PATCH ([0-9]+)\n\
69 .*$"
70        "\\1.\\2.\\3" XZ_VERSION "${XZ_VERSION}")
72 # Among other things, this gives us variables xz_VERSION and xz_VERSION_MAJOR.
73 project(xz VERSION "${XZ_VERSION}" LANGUAGES C)
75 # On Apple OSes, don't build executables as bundles:
76 set(CMAKE_MACOSX_BUNDLE OFF)
78 # windres from GNU binutils can be tricky with command line arguments
79 # that contain spaces or other funny characters. Unfortunately we need
80 # a space in PACKAGE_NAME. Using \x20 to encode the US-ASCII space seems
81 # to work in both cmd.exe and /bin/sh.
83 # However, even \x20 isn't enough in all situations, resulting in
84 # "syntax error" from windres. Using --use-temp-file prevents windres
85 # from using popen() and this seems to fix the problem.
87 # llvm-windres claims to be compatible with GNU windres but with that
88 # the \x20 results in "XZx20Utils" in the compiled binary. (At the
89 # same time it works correctly with clang (the C compiler).) The option
90 # --use-temp-file makes no difference.
92 # CMake 3.25 doesn't have CMAKE_RC_COMPILER_ID so we rely on
93 # CMAKE_C_COMPILER_ID. If Clang is used together with GNU windres
94 # then it will fail, but this way the risk of a bad string in
95 # the binary should be fairly low.
96 if(WIN32 AND CMAKE_C_COMPILER_ID STREQUAL "GNU")
97     # Use workarounds with GNU windres. The \x20 in PACKAGE_NAME works
98     # with gcc too so we don't need to worry how to pass different flags
99     # to windres and gcc.
100     string(APPEND CMAKE_RC_FLAGS " --use-temp-file")
101     set(PACKAGE_NAME "XZ\\x20Utils")
102 else()
103     # Elsewhere a space is safe. This also keeps things compatible with
104     # EBCDIC in case CMake-based build is ever done on such a system.
105     set(PACKAGE_NAME "XZ Utils")
106 endif()
108 # Definitions common to all targets:
109 add_compile_definitions(
110     # Package info:
111     PACKAGE_NAME="${PACKAGE_NAME}"
112     PACKAGE_BUGREPORT="xz@tukaani.org"
113     PACKAGE_URL="https://tukaani.org/xz/"
115     # Features:
116     HAVE_CHECK_CRC32
117     HAVE_CHECK_CRC64
118     HAVE_CHECK_SHA256
119     HAVE_DECODERS
120     HAVE_DECODER_ARM
121     HAVE_DECODER_ARMTHUMB
122     HAVE_DECODER_ARM64
123     HAVE_DECODER_DELTA
124     HAVE_DECODER_IA64
125     HAVE_DECODER_LZMA1
126     HAVE_DECODER_LZMA2
127     HAVE_DECODER_POWERPC
128     HAVE_DECODER_SPARC
129     HAVE_DECODER_X86
130     HAVE_ENCODERS
131     HAVE_ENCODER_ARM
132     HAVE_ENCODER_ARMTHUMB
133     HAVE_ENCODER_ARM64
134     HAVE_ENCODER_DELTA
135     HAVE_ENCODER_IA64
136     HAVE_ENCODER_LZMA1
137     HAVE_ENCODER_LZMA2
138     HAVE_ENCODER_POWERPC
139     HAVE_ENCODER_SPARC
140     HAVE_ENCODER_X86
141     HAVE_MF_BT2
142     HAVE_MF_BT3
143     HAVE_MF_BT4
144     HAVE_MF_HC3
145     HAVE_MF_HC4
146     HAVE_LZIP_DECODER
148     # Standard headers and types are available:
149     HAVE_STDBOOL_H
150     HAVE__BOOL
151     HAVE_STDINT_H
152     HAVE_INTTYPES_H
154     # Disable assert() checks when no build type has been specified. Non-empty
155     # build types like "Release" and "Debug" handle this by default.
156     $<$<CONFIG:>:NDEBUG>
159 # _GNU_SOURCE and such definitions. This specific macro is special since
160 # it also adds the definitions to CMAKE_REQUIRED_DEFINITIONS.
161 tuklib_use_system_extensions(ALL)
163 # This is needed by liblzma and xz.
164 tuklib_integer(ALL)
166 # Check for clock_gettime(). Do this before checking for threading so
167 # that we know there if CLOCK_MONOTONIC is available.
168 if(NOT WIN32 AND NOT DEFINED HAVE_CLOCK_GETTIME)
169     check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME)
170     if(NOT HAVE_CLOCK_GETTIME)
171         # With glibc <= 2.17 or Solaris 10 this needs librt.
172         unset(HAVE_CLOCK_GETTIME CACHE)
174         list(INSERT CMAKE_REQUIRED_LIBRARIES 0 rt)
175         check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME)
177         # If it was found now, add it to all targets and keep it
178         # in CMAKE_REQUIRED_LIBRARIES for further tests too.
179         if(HAVE_CLOCK_GETTIME)
180             link_libraries(rt)
181         else()
182             list(REMOVE_AT CMAKE_REQUIRED_LIBRARIES 0)
183         endif()
184     endif()
185     if(HAVE_CLOCK_GETTIME)
186         # Check if CLOCK_MONOTONIC is available for clock_gettime().
187         check_symbol_exists(CLOCK_MONOTONIC time.h HAVE_CLOCK_MONOTONIC)
189         add_compile_definitions(
190             HAVE_CLOCK_GETTIME
191             HAVE_CLOCK_MONOTONIC
192         )
193     endif()
194 endif()
196 # Threading support:
197 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
198 find_package(Threads REQUIRED)
199 if(CMAKE_USE_WIN32_THREADS_INIT)
200     if(CMAKE_SIZEOF_VOID_P EQUAL 4)
201         # Define to 1 when using Windows 95 (and thus XP) compatible threads. This
202         # avoids use of features that were added in Windows Vista.
203         # This is used for 32-bit x86 builds for compatibility reasons since it
204         # makes no measurable difference in performance compared to Vista threads.
205         add_compile_definitions(MYTHREAD_WIN95)
206     else()
207         # Define to 1 when using Windows Vista compatible threads. This uses features
208         # that are not available on Windows XP.
209         add_compile_definitions(MYTHREAD_VISTA)
210     endif()
211 else()
212     add_compile_definitions(MYTHREAD_POSIX)
214     # Check if pthread_condattr_setclock() exists to use CLOCK_MONOTONIC.
215     if(HAVE_CLOCK_MONOTONIC)
216         list(INSERT CMAKE_REQUIRED_LIBRARIES 0 "${CMAKE_THREAD_LIBS_INIT}")
217         check_symbol_exists(pthread_condattr_setclock pthread.h
218                             HAVE_PTHREAD_CONDATTR_SETCLOCK)
219         tuklib_add_definition_if(ALL HAVE_PTHREAD_CONDATTR_SETCLOCK)
220     endif()
221 endif()
223 # Options for new enough GCC or Clang on any arch or operating system:
224 if(CMAKE_C_COMPILER_ID MATCHES GNU|Clang)
225     # configure.ac has a long list but it won't be copied here:
226     add_compile_options(-Wall -Wextra)
227 endif()
230 #############################################################################
231 # liblzma
232 #############################################################################
234 option(BUILD_SHARED_LIBS "Build liblzma as a shared library instead of static")
236 add_library(liblzma
237     src/common/mythread.h
238     src/common/sysdefs.h
239     src/common/tuklib_common.h
240     src/common/tuklib_config.h
241     src/common/tuklib_cpucores.c
242     src/common/tuklib_cpucores.h
243     src/common/tuklib_integer.h
244     src/common/tuklib_physmem.c
245     src/common/tuklib_physmem.h
246     src/liblzma/api/lzma.h
247     src/liblzma/api/lzma/base.h
248     src/liblzma/api/lzma/bcj.h
249     src/liblzma/api/lzma/block.h
250     src/liblzma/api/lzma/check.h
251     src/liblzma/api/lzma/container.h
252     src/liblzma/api/lzma/delta.h
253     src/liblzma/api/lzma/filter.h
254     src/liblzma/api/lzma/hardware.h
255     src/liblzma/api/lzma/index.h
256     src/liblzma/api/lzma/index_hash.h
257     src/liblzma/api/lzma/lzma12.h
258     src/liblzma/api/lzma/stream_flags.h
259     src/liblzma/api/lzma/version.h
260     src/liblzma/api/lzma/vli.h
261     src/liblzma/check/check.c
262     src/liblzma/check/check.h
263     src/liblzma/check/crc32_fast.c
264     src/liblzma/check/crc32_table.c
265     src/liblzma/check/crc32_table_be.h
266     src/liblzma/check/crc32_table_le.h
267     src/liblzma/check/crc64_fast.c
268     src/liblzma/check/crc64_table.c
269     src/liblzma/check/crc64_table_be.h
270     src/liblzma/check/crc64_table_le.h
271     src/liblzma/check/crc_macros.h
272     src/liblzma/check/sha256.c
273     src/liblzma/common/alone_decoder.c
274     src/liblzma/common/alone_decoder.h
275     src/liblzma/common/alone_encoder.c
276     src/liblzma/common/auto_decoder.c
277     src/liblzma/common/block_buffer_decoder.c
278     src/liblzma/common/block_buffer_encoder.c
279     src/liblzma/common/block_buffer_encoder.h
280     src/liblzma/common/block_decoder.c
281     src/liblzma/common/block_decoder.h
282     src/liblzma/common/block_encoder.c
283     src/liblzma/common/block_encoder.h
284     src/liblzma/common/block_header_decoder.c
285     src/liblzma/common/block_header_encoder.c
286     src/liblzma/common/block_util.c
287     src/liblzma/common/common.c
288     src/liblzma/common/common.h
289     src/liblzma/common/easy_buffer_encoder.c
290     src/liblzma/common/easy_decoder_memusage.c
291     src/liblzma/common/easy_encoder.c
292     src/liblzma/common/easy_encoder_memusage.c
293     src/liblzma/common/easy_preset.c
294     src/liblzma/common/easy_preset.h
295     src/liblzma/common/file_info.c
296     src/liblzma/common/filter_buffer_decoder.c
297     src/liblzma/common/filter_buffer_encoder.c
298     src/liblzma/common/filter_common.c
299     src/liblzma/common/filter_common.h
300     src/liblzma/common/filter_decoder.c
301     src/liblzma/common/filter_decoder.h
302     src/liblzma/common/filter_encoder.c
303     src/liblzma/common/filter_encoder.h
304     src/liblzma/common/filter_flags_decoder.c
305     src/liblzma/common/filter_flags_encoder.c
306     src/liblzma/common/hardware_cputhreads.c
307     src/liblzma/common/hardware_physmem.c
308     src/liblzma/common/index.c
309     src/liblzma/common/index.h
310     src/liblzma/common/index_decoder.c
311     src/liblzma/common/index_decoder.h
312     src/liblzma/common/index_encoder.c
313     src/liblzma/common/index_encoder.h
314     src/liblzma/common/index_hash.c
315     src/liblzma/common/lzip_decoder.c
316     src/liblzma/common/lzip_decoder.h
317     src/liblzma/common/memcmplen.h
318     src/liblzma/common/outqueue.c
319     src/liblzma/common/outqueue.h
320     src/liblzma/common/stream_buffer_decoder.c
321     src/liblzma/common/stream_buffer_encoder.c
322     src/liblzma/common/stream_decoder.c
323     src/liblzma/common/stream_decoder_mt.c
324     src/liblzma/common/stream_decoder.h
325     src/liblzma/common/stream_encoder.c
326     src/liblzma/common/stream_encoder_mt.c
327     src/liblzma/common/stream_flags_common.c
328     src/liblzma/common/stream_flags_common.h
329     src/liblzma/common/stream_flags_decoder.c
330     src/liblzma/common/stream_flags_encoder.c
331     src/liblzma/common/string_conversion.c
332     src/liblzma/common/vli_decoder.c
333     src/liblzma/common/vli_encoder.c
334     src/liblzma/common/vli_size.c
335     src/liblzma/delta/delta_common.c
336     src/liblzma/delta/delta_common.h
337     src/liblzma/delta/delta_decoder.c
338     src/liblzma/delta/delta_decoder.h
339     src/liblzma/delta/delta_encoder.c
340     src/liblzma/delta/delta_encoder.h
341     src/liblzma/delta/delta_private.h
342     src/liblzma/lz/lz_decoder.c
343     src/liblzma/lz/lz_decoder.h
344     src/liblzma/lz/lz_encoder.c
345     src/liblzma/lz/lz_encoder.h
346     src/liblzma/lz/lz_encoder_hash.h
347     src/liblzma/lz/lz_encoder_hash_table.h
348     src/liblzma/lz/lz_encoder_mf.c
349     src/liblzma/lzma/fastpos.h
350     src/liblzma/lzma/fastpos_table.c
351     src/liblzma/lzma/lzma2_decoder.c
352     src/liblzma/lzma/lzma2_decoder.h
353     src/liblzma/lzma/lzma2_encoder.c
354     src/liblzma/lzma/lzma2_encoder.h
355     src/liblzma/lzma/lzma_common.h
356     src/liblzma/lzma/lzma_decoder.c
357     src/liblzma/lzma/lzma_decoder.h
358     src/liblzma/lzma/lzma_encoder.c
359     src/liblzma/lzma/lzma_encoder.h
360     src/liblzma/lzma/lzma_encoder_optimum_fast.c
361     src/liblzma/lzma/lzma_encoder_optimum_normal.c
362     src/liblzma/lzma/lzma_encoder_presets.c
363     src/liblzma/lzma/lzma_encoder_private.h
364     src/liblzma/rangecoder/price.h
365     src/liblzma/rangecoder/price_table.c
366     src/liblzma/rangecoder/range_common.h
367     src/liblzma/rangecoder/range_decoder.h
368     src/liblzma/rangecoder/range_encoder.h
369     src/liblzma/simple/arm.c
370     src/liblzma/simple/armthumb.c
371     src/liblzma/simple/arm64.c
372     src/liblzma/simple/ia64.c
373     src/liblzma/simple/powerpc.c
374     src/liblzma/simple/simple_coder.c
375     src/liblzma/simple/simple_coder.h
376     src/liblzma/simple/simple_decoder.c
377     src/liblzma/simple/simple_decoder.h
378     src/liblzma/simple/simple_encoder.c
379     src/liblzma/simple/simple_encoder.h
380     src/liblzma/simple/simple_private.h
381     src/liblzma/simple/sparc.c
382     src/liblzma/simple/x86.c
385 target_include_directories(liblzma PRIVATE
386     src/liblzma/api
387     src/liblzma/common
388     src/liblzma/check
389     src/liblzma/lz
390     src/liblzma/rangecoder
391     src/liblzma/lzma
392     src/liblzma/delta
393     src/liblzma/simple
394     src/common
397 target_link_libraries(liblzma Threads::Threads)
399 # Put the tuklib functions under the lzma_ namespace.
400 target_compile_definitions(liblzma PRIVATE TUKLIB_SYMBOL_PREFIX=lzma_)
401 tuklib_cpucores(liblzma)
402 tuklib_physmem(liblzma)
404 # While liblzma can be built without tuklib_cpucores or tuklib_physmem
405 # modules, the liblzma API functions lzma_cputhreads() and lzma_physmem()
406 # will then be useless (which isn't too bad but still unfortunate). Since
407 # I expect the CMake-based builds to be only used on systems that are
408 # supported by these tuklib modules, problems with these tuklib modules
409 # are considered a hard error for now. This hopefully helps to catch bugs
410 # in the CMake versions of the tuklib checks.
411 if(NOT TUKLIB_CPUCORES_FOUND OR NOT TUKLIB_PHYSMEM_FOUND)
412     # Use SEND_ERROR instead of FATAL_ERROR. If someone reports a bug,
413     # seeing the results of the remaining checks can be useful too.
414     message(SEND_ERROR
415             "tuklib_cpucores() or tuklib_physmem() failed. "
416             "Unless you really are building for a system where these "
417             "modules are not supported (unlikely), this is a bug in the "
418             "included cmake/tuklib_*.cmake files that should be fixed. "
419             "To build anyway, edit this CMakeLists.txt to ignore this error.")
420 endif()
422 # Check for __attribute__((__constructor__)) support.
423 # This needs -Werror because some compilers just warn
424 # about this being unsupported.
425 cmake_push_check_state()
426 set(CMAKE_REQUIRED_FLAGS "-Werror")
427 check_c_source_compiles("
428         __attribute__((__constructor__))
429         static void my_constructor_func(void) { return; }
430         int main(void) { return 0; }
431     "
432     HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
433 cmake_pop_check_state()
434 tuklib_add_definition_if(liblzma HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
436 # cpuid.h
437 check_include_file(cpuid.h HAVE_CPUID_H)
438 tuklib_add_definition_if(liblzma HAVE_CPUID_H)
440 # immintrin.h:
441 check_include_file(immintrin.h HAVE_IMMINTRIN_H)
442 if(HAVE_IMMINTRIN_H)
443     target_compile_definitions(liblzma PRIVATE HAVE_IMMINTRIN_H)
445     # SSE2 intrinsics:
446     check_c_source_compiles("
447             #include <immintrin.h>
448             int main(void)
449             {
450                 __m128i x = { 0 };
451                 _mm_movemask_epi8(x);
452                 return 0;
453             }
454         "
455         HAVE__MM_MOVEMASK_EPI8)
456     tuklib_add_definition_if(liblzma HAVE__MM_MOVEMASK_EPI8)
458     # CLMUL intrinsic:
459     check_c_source_compiles("
460             #include <immintrin.h>
461             #if defined(__e2k__) && __iset__ < 6
462             #   error
463             #endif
464             #if (defined(__GNUC__) || defined(__clang__)) && !defined(__EDG__)
465             __attribute__((__target__(\"ssse3,sse4.1,pclmul\")))
466             #endif
467             __m128i my_clmul(__m128i a)
468             {
469                 const __m128i b = _mm_set_epi64x(1, 2);
470                 return _mm_clmulepi64_si128(a, b, 0);
471             }
472             int main(void) { return 0; }
473     "
474     HAVE_USABLE_CLMUL)
475     tuklib_add_definition_if(liblzma HAVE_USABLE_CLMUL)
476 endif()
478 # Support -fvisiblity=hidden when building shared liblzma.
479 # These lines do nothing on Windows (even under Cygwin).
480 # HAVE_VISIBILITY should always be defined to 0 or 1.
481 if(BUILD_SHARED_LIBS)
482     set_target_properties(liblzma PROPERTIES C_VISIBILITY_PRESET hidden)
483     target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=1)
484 else()
485     target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=0)
486 endif()
488 if(WIN32)
489     if(BUILD_SHARED_LIBS)
490         # Add the Windows resource file for liblzma.dll.
491         target_sources(liblzma PRIVATE src/liblzma/liblzma_w32res.rc)
493         set_target_properties(liblzma PROPERTIES
494             LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
495         )
497         # Export the public API symbols with __declspec(dllexport).
498         target_compile_definitions(liblzma PRIVATE DLL_EXPORT)
499     else()
500         # Disable __declspec(dllimport) when linking against static liblzma.
501         target_compile_definitions(liblzma INTERFACE LZMA_API_STATIC)
502     endif()
503 elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
504     # GNU/Linux-specific symbol versioning for shared liblzma.
505     # Note that adding link options doesn't affect static builds
506     # but HAVE_SYMBOL_VERSIONS_LINUX must not be used with static builds
507     # because it would put symbol versions into the static library which
508     # can cause problems. It's clearer if all symver related things are
509     # omitted when not building a shared library.
510     #
511     # NOTE: Set it explicitly to 1 to make it clear that versioning is
512     # done unconditionally in the C files.
513     target_compile_definitions(liblzma PRIVATE HAVE_SYMBOL_VERSIONS_LINUX=1)
514     target_link_options(liblzma PRIVATE
515         "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
516     )
517     set_target_properties(liblzma PROPERTIES
518         LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
519     )
520 elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
521     # Symbol versioning for shared liblzma for non-GNU/Linux.
522     # FIXME? What about Solaris?
523     target_link_options(liblzma PRIVATE
524         "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
525     )
526     set_target_properties(liblzma PROPERTIES
527         LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
528     )
529 endif()
531 set_target_properties(liblzma PROPERTIES
532     # At least for now the package versioning matches the rules used for
533     # shared library versioning (excluding development releases) so it is
534     # fine to use the package version here.
535     SOVERSION "${xz_VERSION_MAJOR}"
536     VERSION "${xz_VERSION}"
538     # It's liblzma.so or liblzma.dll, not libliblzma.so or lzma.dll.
539     # Avoid the name lzma.dll because it would conflict with LZMA SDK.
540     PREFIX ""
543 # Create liblzma-config-version.cmake. We use this spelling instead of
544 # liblzmaConfig.cmake to make find_package work in case insensitive manner
545 # even with case sensitive file systems. This gives more consistent behavior
546 # between operating systems.
548 # FIXME: SameMajorVersion is correct for stable releases but it is wrong
549 # for development releases where each release may have incompatible changes.
550 include(CMakePackageConfigHelpers)
551 write_basic_package_version_file(
552     "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
553     VERSION "${liblzma_VERSION}"
554     COMPATIBILITY SameMajorVersion)
556 # Create liblzma-config.cmake.
557 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
558 "include(CMakeFindDependencyMacro)
559 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
560 find_dependency(Threads)
562 include(\"\${CMAKE_CURRENT_LIST_DIR}/liblzma-targets.cmake\")
564 # Be compatible with the spelling used by the FindLibLZMA module. This
565 # doesn't use ALIAS because it would make CMake resolve LibLZMA::LibLZMA
566 # to liblzma::liblzma instead of keeping the original spelling. Keeping
567 # the original spelling is important for good FindLibLZMA compatibility.
568 add_library(LibLZMA::LibLZMA INTERFACE IMPORTED)
569 set_target_properties(LibLZMA::LibLZMA PROPERTIES
570                       INTERFACE_LINK_LIBRARIES liblzma::liblzma)
573 # Set CMAKE_INSTALL_LIBDIR and friends.
574 include(GNUInstallDirs)
576 # Install the library binary. The INCLUDES specifies the include path that
577 # is exported for other projects to use but it doesn't install any files.
578 install(TARGETS liblzma EXPORT liblzmaTargets
579         RUNTIME  DESTINATION "${CMAKE_INSTALL_BINDIR}"
580                  COMPONENT liblzma_Runtime
581         LIBRARY  DESTINATION "${CMAKE_INSTALL_LIBDIR}"
582                  COMPONENT liblzma_Runtime
583                  NAMELINK_COMPONENT liblzma_Development
584         ARCHIVE  DESTINATION "${CMAKE_INSTALL_LIBDIR}"
585                  COMPONENT liblzma_Development
586         INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
588 # Install the liblzma API headers. These use a subdirectory so
589 # this has to be done as a separate step.
590 install(DIRECTORY src/liblzma/api/
591         COMPONENT liblzma_Development
592         DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
593         FILES_MATCHING PATTERN "*.h")
595 # Install the CMake files that other packages can use to find liblzma.
596 set(liblzma_INSTALL_CMAKEDIR
597     "${CMAKE_INSTALL_LIBDIR}/cmake/liblzma"
598     CACHE STRING "Path to liblzma's .cmake files")
600 install(EXPORT liblzmaTargets
601         NAMESPACE liblzma::
602         FILE liblzma-targets.cmake
603         DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
604         COMPONENT liblzma_Development)
606 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
607               "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
608         DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
609         COMPONENT liblzma_Development)
612 #############################################################################
613 # getopt_long
614 #############################################################################
616 # The command line tools needs this.
617 check_symbol_exists(getopt_long getopt.h HAVE_GETOPT_LONG)
620 #############################################################################
621 # xzdec
622 #############################################################################
624 if(HAVE_GETOPT_LONG)
625     add_executable(xzdec
626         src/common/sysdefs.h
627         src/common/tuklib_common.h
628         src/common/tuklib_config.h
629         src/common/tuklib_exit.c
630         src/common/tuklib_exit.h
631         src/common/tuklib_gettext.h
632         src/common/tuklib_progname.c
633         src/common/tuklib_progname.h
634         src/xzdec/xzdec.c
635     )
637     target_include_directories(xzdec PRIVATE
638         src/common
639         src/liblzma/api
640     )
642     target_link_libraries(xzdec PRIVATE liblzma)
644     if(WIN32)
645         # Add the Windows resource file for xzdec.exe.
646         target_sources(xzdec PRIVATE src/xzdec/xzdec_w32res.rc)
647         set_target_properties(xzdec PROPERTIES
648             LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
649         )
650     endif()
652     tuklib_progname(xzdec)
654     install(TARGETS xzdec
655             RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
656                     COMPONENT xzdec)
658     if(UNIX)
659         install(FILES src/xzdec/xzdec.1
660                 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
661                 COMPONENT xzdec)
662     endif()
663 endif()
666 #############################################################################
667 # xz
668 #############################################################################
670 if(NOT MSVC AND HAVE_GETOPT_LONG)
671     add_executable(xz
672         src/common/mythread.h
673         src/common/sysdefs.h
674         src/common/tuklib_common.h
675         src/common/tuklib_config.h
676         src/common/tuklib_exit.c
677         src/common/tuklib_exit.h
678         src/common/tuklib_gettext.h
679         src/common/tuklib_integer.h
680         src/common/tuklib_mbstr.h
681         src/common/tuklib_mbstr_fw.c
682         src/common/tuklib_mbstr_width.c
683         src/common/tuklib_open_stdxxx.c
684         src/common/tuklib_open_stdxxx.h
685         src/common/tuklib_progname.c
686         src/common/tuklib_progname.h
687         src/xz/args.c
688         src/xz/args.h
689         src/xz/coder.c
690         src/xz/coder.h
691         src/xz/file_io.c
692         src/xz/file_io.h
693         src/xz/hardware.c
694         src/xz/hardware.h
695         src/xz/list.c
696         src/xz/list.h
697         src/xz/main.c
698         src/xz/main.h
699         src/xz/message.c
700         src/xz/message.h
701         src/xz/mytime.c
702         src/xz/mytime.h
703         src/xz/options.c
704         src/xz/options.h
705         src/xz/private.h
706         src/xz/signals.c
707         src/xz/signals.h
708         src/xz/suffix.c
709         src/xz/suffix.h
710         src/xz/util.c
711         src/xz/util.h
712     )
714     target_include_directories(xz PRIVATE
715         src/common
716         src/liblzma/api
717     )
719     target_link_libraries(xz PRIVATE liblzma)
721     target_compile_definitions(xz PRIVATE ASSUME_RAM=128)
723     if(WIN32)
724         # Add the Windows resource file for xz.exe.
725         target_sources(xz PRIVATE src/xz/xz_w32res.rc)
726         set_target_properties(xz PROPERTIES
727             LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
728         )
729     endif()
731     tuklib_progname(xz)
732     tuklib_mbstr(xz)
734     check_symbol_exists(optreset getopt.h HAVE_OPTRESET)
735     tuklib_add_definition_if(xz HAVE_OPTRESET)
737     check_symbol_exists(posix_fadvise fcntl.h HAVE_POSIX_FADVISE)
738     tuklib_add_definition_if(xz HAVE_POSIX_FADVISE)
740     # How to get file time:
741     check_struct_has_member("struct stat" st_atim.tv_nsec
742                             "sys/types.h;sys/stat.h"
743                             HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
744     if(HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
745         tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
746     else()
747         check_struct_has_member("struct stat" st_atimespec.tv_nsec
748                                 "sys/types.h;sys/stat.h"
749                                 HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
750         if(HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
751             tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
752         else()
753             check_struct_has_member("struct stat" st_atimensec
754                                     "sys/types.h;sys/stat.h"
755                                     HAVE_STRUCT_STAT_ST_ATIMENSEC)
756             tuklib_add_definition_if(xz HAVE_STRUCT_STAT_ST_ATIMENSEC)
757         endif()
758     endif()
760     # How to set file time:
761     check_symbol_exists(futimens "sys/types.h;sys/stat.h" HAVE_FUTIMENS)
762     if(HAVE_FUTIMENS)
763         tuklib_add_definitions(xz HAVE_FUTIMENS)
764     else()
765         check_symbol_exists(futimes "sys/time.h" HAVE_FUTIMES)
766         if(HAVE_FUTIMES)
767             tuklib_add_definitions(xz HAVE_FUTIMES)
768         else()
769             check_symbol_exists(futimesat "sys/time.h" HAVE_FUTIMESAT)
770             if(HAVE_FUTIMESAT)
771                 tuklib_add_definitions(xz HAVE_FUTIMESAT)
772             else()
773                 check_symbol_exists(utimes "sys/time.h" HAVE_UTIMES)
774                 if(HAVE_UTIMES)
775                     tuklib_add_definitions(xz HAVE_UTIMES)
776                 else()
777                     check_symbol_exists(_futime "sys/utime.h" HAVE__FUTIME)
778                     if(HAVE__FUTIME)
779                         tuklib_add_definitions(xz HAVE__FUTIME)
780                     else()
781                         check_symbol_exists(utime "utime.h" HAVE_UTIME)
782                         tuklib_add_definition_if(xz HAVE_UTIME)
783                     endif()
784                 endif()
785             endif()
786         endif()
787     endif()
789     install(TARGETS xz
790             RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
791                     COMPONENT xz)
793     if(UNIX)
794         install(FILES src/xz/xz.1
795                 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
796                 COMPONENT xz)
798         option(CREATE_XZ_SYMLINKS "Create unxz and xzcat symlinks" ON)
799         option(CREATE_LZMA_SYMLINKS "Create lzma, unlzma, and lzcat symlinks"
800                ON)
801         set(XZ_LINKS)
803         if(CREATE_XZ_SYMLINKS)
804             list(APPEND XZ_LINKS "unxz" "xzcat")
805         endif()
807         if(CREATE_LZMA_SYMLINKS)
808             list(APPEND XZ_LINKS "lzma" "unlzma" "lzcat")
809         endif()
811         # Create symlinks in the build directory and then install them.
812         #
813         # The symlinks do not likely need any special extension since
814         # even on Windows the symlink can still be executed without
815         # the .exe extension.
816         foreach(LINK IN LISTS XZ_LINKS)
817             add_custom_target("${LINK}" ALL
818                 "${CMAKE_COMMAND}" -E create_symlink
819                     "$<TARGET_FILE_NAME:xz>" "${LINK}"
820                 BYPRODUCTS "${LINK}"
821                 VERBATIM)
822             install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}"
823                     DESTINATION "${CMAKE_INSTALL_BINDIR}"
824                     COMPONENT xz)
825             add_custom_target("${LINK}.1" ALL
826                 "${CMAKE_COMMAND}" -E create_symlink "xz.1" "${LINK}.1"
827                 BYPRODUCTS "${LINK}.1"
828                 VERBATIM)
829             install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}.1"
830                     DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
831                     COMPONENT xz)
832         endforeach()
833     endif()
834 endif()
837 #############################################################################
838 # Tests
839 #############################################################################
841 include(CTest)
843 if(BUILD_TESTING)
844     set(LIBLZMA_TESTS
845         test_bcj_exact_size
846         test_block_header
847         test_check
848         test_filter_flags
849         test_hardware
850         test_index
851         test_index_hash
852         test_memlimit
853         test_stream_flags
854         test_vli
855     )
857     foreach(TEST IN LISTS LIBLZMA_TESTS)
858         add_executable("${TEST}" "tests/${TEST}.c")
860         target_include_directories("${TEST}" PRIVATE
861             src/common
862             src/liblzma/api
863             src/liblzma
864             lib
865         )
867         target_link_libraries("${TEST}" PRIVATE liblzma)
869         # Put the test programs into their own subdirectory so they don't
870         # pollute the top-level dir which might contain xz and xzdec.
871         set_target_properties("${TEST}" PROPERTIES
872             RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tests_bin"
873         )
875         add_test(NAME "${TEST}"
876                  COMMAND "${CMAKE_CURRENT_BINARY_DIR}/tests_bin/${TEST}"
877         )
879         # Set srcdir environment variable so that the tests find their
880         # input files from the source tree.
881         #
882         # Set the return code for skipped tests to match Automake convention.
883         set_tests_properties("${TEST}" PROPERTIES
884             ENVIRONMENT "srcdir=${CMAKE_CURRENT_LIST_DIR}/tests"
885             SKIP_RETURN_CODE 77
886         )
887     endforeach()
888 endif()