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