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