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