liblzma: Add support for LZMA_SYNC_FLUSH in the Block encoder.
[xz/debian.git] / CMakeLists.txt
blob62d349ae75fe8a4ecc8081d5be580c725d566530
1 #############################################################################
3 # Very limited CMake support for building some parts of XZ Utils
5 # Building static liblzma with MSVC should work. Building shared liblzma.dll
6 # with MSVC may or may not work (building liblzma_w32res.rc might be broken).
7 # 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 tests (no test failures either!)
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(CheckSymbolExists)
52 include(CheckStructHasMember)
53 include(cmake/tuklib_integer.cmake)
54 include(cmake/tuklib_cpucores.cmake)
55 include(cmake/tuklib_physmem.cmake)
56 include(cmake/tuklib_progname.cmake)
57 include(cmake/tuklib_mbstr.cmake)
59 # Get the package version from version.h into XZ_VERSION variable.
60 file(READ src/liblzma/api/lzma/version.h XZ_VERSION)
61 string(REGEX REPLACE
62 "^.*\n\
63 #define LZMA_VERSION_MAJOR ([0-9]+)\n\
64 #define LZMA_VERSION_MINOR ([0-9]+)\n\
65 #define LZMA_VERSION_PATCH ([0-9]+)\n\
66 .*$"
67        "\\1.\\2.\\3" XZ_VERSION "${XZ_VERSION}")
69 # Among other things, this gives us variables xz_VERSION and xz_VERSION_MAJOR.
70 project(xz VERSION "${XZ_VERSION}" LANGUAGES C)
72 # On Apple OSes, don't build executables as bundles:
73 set(CMAKE_MACOSX_BUNDLE OFF)
75 # Definitions common to all targets:
76 add_compile_definitions(
77     # Package info:
78     PACKAGE_NAME="XZ Utils"
79     PACKAGE_BUGREPORT="lasse.collin@tukaani.org"
80     PACKAGE_URL="https://tukaani.org/xz/"
82     # Features:
83     HAVE_CHECK_CRC32
84     HAVE_CHECK_CRC64
85     HAVE_CHECK_SHA256
86     HAVE_DECODERS
87     HAVE_DECODER_ARM
88     HAVE_DECODER_ARMTHUMB
89     HAVE_DECODER_DELTA
90     HAVE_DECODER_IA64
91     HAVE_DECODER_LZMA1
92     HAVE_DECODER_LZMA2
93     HAVE_DECODER_POWERPC
94     HAVE_DECODER_SPARC
95     HAVE_DECODER_X86
96     HAVE_ENCODERS
97     HAVE_ENCODER_ARM
98     HAVE_ENCODER_ARMTHUMB
99     HAVE_ENCODER_DELTA
100     HAVE_ENCODER_IA64
101     HAVE_ENCODER_LZMA1
102     HAVE_ENCODER_LZMA2
103     HAVE_ENCODER_POWERPC
104     HAVE_ENCODER_SPARC
105     HAVE_ENCODER_X86
106     HAVE_MF_BT2
107     HAVE_MF_BT3
108     HAVE_MF_BT4
109     HAVE_MF_HC3
110     HAVE_MF_HC4
112     # Standard headers and types are available:
113     HAVE_STDBOOL_H
114     HAVE__BOOL
115     HAVE_STDINT_H
116     HAVE_INTTYPES_H
118     # Disable assert() checks when no build type has been specified. Non-empty
119     # build types like "Release" and "Debug" handle this by default.
120     $<$<CONFIG:>:NDEBUG>
123 # _GNU_SOURCE and such definitions. This specific macro is special since
124 # it also adds the definitions to CMAKE_REQUIRED_DEFINITIONS.
125 tuklib_use_system_extensions(ALL)
127 # This is needed by liblzma and xz.
128 tuklib_integer(ALL)
130 # Check for clock_gettime(). Do this before checking for threading so
131 # that we know there if CLOCK_MONOTONIC is available.
132 if(NOT WIN32 AND NOT DEFINED HAVE_CLOCK_GETTIME)
133     check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME)
134     if(NOT HAVE_CLOCK_GETTIME)
135         # With glibc <= 2.17 or Solaris 10 this needs librt.
136         unset(HAVE_CLOCK_GETTIME CACHE)
138         list(INSERT CMAKE_REQUIRED_LIBRARIES 0 rt)
139         check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME)
141         # If it was found now, add it to all targets and keep it
142         # in CMAKE_REQUIRED_LIBRARIES for further tests too.
143         if(HAVE_CLOCK_GETTIME)
144             link_libraries(rt)
145         else()
146             list(REMOVE_AT CMAKE_REQUIRED_LIBRARIES 0)
147         endif()
148     endif()
149     if(HAVE_CLOCK_GETTIME)
150         # Check if CLOCK_MONOTONIC is available for clock_gettime().
151         check_symbol_exists(CLOCK_MONOTONIC time.h HAVE_DECL_CLOCK_MONOTONIC)
153         # HAVE_DECL_CLOCK_MONOTONIC should always be defined to 0 or 1
154         # when clock_gettime is available.
155         add_compile_definitions(
156             HAVE_CLOCK_GETTIME
157             HAVE_DECL_CLOCK_MONOTONIC=$<BOOL:"${HAVE_DECL_CLOCK_MONOTONIC}">
158         )
159     endif()
160 endif()
162 # Threading support:
163 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
164 find_package(Threads REQUIRED)
165 if(CMAKE_USE_WIN32_THREADS_INIT)
166     if(CMAKE_SIZEOF_VOID_P EQUAL 4)
167         # Define to 1 when using Windows 95 (and thus XP) compatible threads. This
168         # avoids use of features that were added in Windows Vista.
169         # This is used for 32-bit x86 builds for compatibility reasons since it
170         # makes no measurable difference in performance compared to Vista threads.
171         add_compile_definitions(MYTHREAD_WIN95)
172     else()
173         # Define to 1 when using Windows Vista compatible threads. This uses features
174         # that are not available on Windows XP.
175         add_compile_definitions(MYTHREAD_VISTA)
176     endif()
177 else()
178     add_compile_definitions(MYTHREAD_POSIX)
180     # Check if pthread_condattr_setclock() exists to use CLOCK_MONOTONIC.
181     if(HAVE_DECL_CLOCK_MONOTONIC)
182         list(INSERT CMAKE_REQUIRED_LIBRARIES 0 "${CMAKE_THREAD_LIBS_INIT}")
183         check_symbol_exists(pthread_condattr_setclock pthread.h
184                             HAVE_PTHREAD_CONDATTR_SETCLOCK)
185         tuklib_add_definition_if(ALL HAVE_PTHREAD_CONDATTR_SETCLOCK)
186     endif()
187 endif()
189 # Options for new enough GCC or Clang on any arch or operating system:
190 if(CMAKE_C_COMPILER_ID MATCHES GNU|Clang)
191     # configure.ac has a long list but it won't be copied here:
192     add_compile_options(-Wall -Wextra)
193 endif()
196 #############################################################################
197 # liblzma
198 #############################################################################
200 option(BUILD_SHARED_LIBS "Build liblzma as a shared library instead of static")
202 add_library(liblzma
203     src/common/mythread.h
204     src/common/sysdefs.h
205     src/common/tuklib_common.h
206     src/common/tuklib_config.h
207     src/common/tuklib_cpucores.c
208     src/common/tuklib_cpucores.h
209     src/common/tuklib_integer.h
210     src/common/tuklib_physmem.c
211     src/common/tuklib_physmem.h
212     src/liblzma/api/lzma.h
213     src/liblzma/api/lzma/base.h
214     src/liblzma/api/lzma/bcj.h
215     src/liblzma/api/lzma/block.h
216     src/liblzma/api/lzma/check.h
217     src/liblzma/api/lzma/container.h
218     src/liblzma/api/lzma/delta.h
219     src/liblzma/api/lzma/filter.h
220     src/liblzma/api/lzma/hardware.h
221     src/liblzma/api/lzma/index.h
222     src/liblzma/api/lzma/index_hash.h
223     src/liblzma/api/lzma/lzma12.h
224     src/liblzma/api/lzma/stream_flags.h
225     src/liblzma/api/lzma/version.h
226     src/liblzma/api/lzma/vli.h
227     src/liblzma/check/check.c
228     src/liblzma/check/check.h
229     src/liblzma/check/crc32_fast.c
230     src/liblzma/check/crc32_table.c
231     src/liblzma/check/crc32_table_be.h
232     src/liblzma/check/crc32_table_le.h
233     src/liblzma/check/crc64_fast.c
234     src/liblzma/check/crc64_table.c
235     src/liblzma/check/crc64_table_be.h
236     src/liblzma/check/crc64_table_le.h
237     src/liblzma/check/crc_macros.h
238     src/liblzma/check/sha256.c
239     src/liblzma/common/alone_decoder.c
240     src/liblzma/common/alone_decoder.h
241     src/liblzma/common/alone_encoder.c
242     src/liblzma/common/auto_decoder.c
243     src/liblzma/common/block_buffer_decoder.c
244     src/liblzma/common/block_buffer_encoder.c
245     src/liblzma/common/block_buffer_encoder.h
246     src/liblzma/common/block_decoder.c
247     src/liblzma/common/block_decoder.h
248     src/liblzma/common/block_encoder.c
249     src/liblzma/common/block_encoder.h
250     src/liblzma/common/block_header_decoder.c
251     src/liblzma/common/block_header_encoder.c
252     src/liblzma/common/block_util.c
253     src/liblzma/common/common.c
254     src/liblzma/common/common.h
255     src/liblzma/common/easy_buffer_encoder.c
256     src/liblzma/common/easy_decoder_memusage.c
257     src/liblzma/common/easy_encoder.c
258     src/liblzma/common/easy_encoder_memusage.c
259     src/liblzma/common/easy_preset.c
260     src/liblzma/common/easy_preset.h
261     src/liblzma/common/filter_buffer_decoder.c
262     src/liblzma/common/filter_buffer_encoder.c
263     src/liblzma/common/filter_common.c
264     src/liblzma/common/filter_common.h
265     src/liblzma/common/filter_decoder.c
266     src/liblzma/common/filter_decoder.h
267     src/liblzma/common/filter_encoder.c
268     src/liblzma/common/filter_encoder.h
269     src/liblzma/common/filter_flags_decoder.c
270     src/liblzma/common/filter_flags_encoder.c
271     src/liblzma/common/hardware_cputhreads.c
272     src/liblzma/common/hardware_physmem.c
273     src/liblzma/common/index.c
274     src/liblzma/common/index.h
275     src/liblzma/common/index_decoder.c
276     src/liblzma/common/index_encoder.c
277     src/liblzma/common/index_encoder.h
278     src/liblzma/common/index_hash.c
279     src/liblzma/common/memcmplen.h
280     src/liblzma/common/outqueue.c
281     src/liblzma/common/outqueue.h
282     src/liblzma/common/stream_buffer_decoder.c
283     src/liblzma/common/stream_buffer_encoder.c
284     src/liblzma/common/stream_decoder.c
285     src/liblzma/common/stream_decoder.h
286     src/liblzma/common/stream_encoder.c
287     src/liblzma/common/stream_encoder_mt.c
288     src/liblzma/common/stream_flags_common.c
289     src/liblzma/common/stream_flags_common.h
290     src/liblzma/common/stream_flags_decoder.c
291     src/liblzma/common/stream_flags_encoder.c
292     src/liblzma/common/vli_decoder.c
293     src/liblzma/common/vli_encoder.c
294     src/liblzma/common/vli_size.c
295     src/liblzma/delta/delta_common.c
296     src/liblzma/delta/delta_common.h
297     src/liblzma/delta/delta_decoder.c
298     src/liblzma/delta/delta_decoder.h
299     src/liblzma/delta/delta_encoder.c
300     src/liblzma/delta/delta_encoder.h
301     src/liblzma/delta/delta_private.h
302     src/liblzma/lz/lz_decoder.c
303     src/liblzma/lz/lz_decoder.h
304     src/liblzma/lz/lz_encoder.c
305     src/liblzma/lz/lz_encoder.h
306     src/liblzma/lz/lz_encoder_hash.h
307     src/liblzma/lz/lz_encoder_hash_table.h
308     src/liblzma/lz/lz_encoder_mf.c
309     src/liblzma/lzma/fastpos.h
310     src/liblzma/lzma/fastpos_table.c
311     src/liblzma/lzma/lzma2_decoder.c
312     src/liblzma/lzma/lzma2_decoder.h
313     src/liblzma/lzma/lzma2_encoder.c
314     src/liblzma/lzma/lzma2_encoder.h
315     src/liblzma/lzma/lzma_common.h
316     src/liblzma/lzma/lzma_decoder.c
317     src/liblzma/lzma/lzma_decoder.h
318     src/liblzma/lzma/lzma_encoder.c
319     src/liblzma/lzma/lzma_encoder.h
320     src/liblzma/lzma/lzma_encoder_optimum_fast.c
321     src/liblzma/lzma/lzma_encoder_optimum_normal.c
322     src/liblzma/lzma/lzma_encoder_presets.c
323     src/liblzma/lzma/lzma_encoder_private.h
324     src/liblzma/rangecoder/price.h
325     src/liblzma/rangecoder/price_table.c
326     src/liblzma/rangecoder/range_common.h
327     src/liblzma/rangecoder/range_decoder.h
328     src/liblzma/rangecoder/range_encoder.h
329     src/liblzma/simple/arm.c
330     src/liblzma/simple/armthumb.c
331     src/liblzma/simple/ia64.c
332     src/liblzma/simple/powerpc.c
333     src/liblzma/simple/simple_coder.c
334     src/liblzma/simple/simple_coder.h
335     src/liblzma/simple/simple_decoder.c
336     src/liblzma/simple/simple_decoder.h
337     src/liblzma/simple/simple_encoder.c
338     src/liblzma/simple/simple_encoder.h
339     src/liblzma/simple/simple_private.h
340     src/liblzma/simple/sparc.c
341     src/liblzma/simple/x86.c
344 target_include_directories(liblzma PRIVATE
345     src/liblzma/api
346     src/liblzma/common
347     src/liblzma/check
348     src/liblzma/lz
349     src/liblzma/rangecoder
350     src/liblzma/lzma
351     src/liblzma/delta
352     src/liblzma/simple
353     src/common
356 target_link_libraries(liblzma Threads::Threads)
358 # Put the tuklib functions under the lzma_ namespace.
359 target_compile_definitions(liblzma PRIVATE TUKLIB_SYMBOL_PREFIX=lzma_)
360 tuklib_cpucores(liblzma)
361 tuklib_physmem(liblzma)
363 # While liblzma can be built without tuklib_cpucores or tuklib_physmem
364 # modules, the liblzma API functions lzma_cputhreads() and lzma_physmem()
365 # will then be useless (which isn't too bad but still unfortunate). Since
366 # I expect the CMake-based builds to be only used on systems that are
367 # supported by these tuklib modules, problems with these tuklib modules
368 # are considered a hard error for now. This hopefully helps to catch bugs
369 # in the CMake versions of the tuklib checks.
370 if(NOT TUKLIB_CPUCORES_FOUND OR NOT TUKLIB_PHYSMEM_FOUND)
371     # Use SEND_ERROR instead of FATAL_ERROR. If someone reports a bug,
372     # seeing the results of the remaining checks can be useful too.
373     message(SEND_ERROR
374             "tuklib_cpucores() or tuklib_physmem() failed. "
375             "Unless you really are building for a system where these "
376             "modules are not supported (unlikely), this is a bug in the "
377             "included cmake/tuklib_*.cmake files that should be fixed. "
378             "To build anyway, edit this CMakeLists.txt to ignore this error.")
379 endif()
381 # immintrin.h:
382 include(CheckIncludeFile)
383 check_include_file(immintrin.h HAVE_IMMINTRIN_H)
384 if(HAVE_IMMINTRIN_H)
385     target_compile_definitions(liblzma PRIVATE HAVE_IMMINTRIN_H)
387     # SSE2 intrinsics:
388     include(CheckCSourceCompiles)
389     check_c_source_compiles("
390             #include <immintrin.h>
391             int main(void)
392             {
393                 __m128i x = { 0 };
394                 _mm_movemask_epi8(x);
395                 return 0;
396             }
397         "
398         HAVE__MM_MOVEMASK_EPI8)
399     tuklib_add_definition_if(liblzma HAVE__MM_MOVEMASK_EPI8)
400 endif()
402 # Support -fvisiblity=hidden when building shared liblzma.
403 # These lines do nothing on Windows (even under Cygwin).
404 # HAVE_VISIBILITY should always be defined to 0 or 1.
405 if(BUILD_SHARED_LIBS)
406     set_target_properties(liblzma PROPERTIES C_VISIBILITY_PRESET hidden)
407     target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=1)
408 else()
409     target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=0)
410 endif()
412 if(WIN32)
413     if(BUILD_SHARED_LIBS)
414         # Add the Windows resource file for liblzma.dll.
415         target_sources(liblzma PRIVATE src/liblzma/liblzma_w32res.rc)
417         set_target_properties(liblzma PROPERTIES
418             LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc"
419         )
421         # Export the public API symbols with __declspec(dllexport).
422         target_compile_definitions(liblzma PRIVATE DLL_EXPORT)
423     else()
424         # Disable __declspec(dllimport) when linking against static liblzma.
425         target_compile_definitions(liblzma INTERFACE LZMA_API_STATIC)
426     endif()
427 elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
428     # GNU/Linux-specific symbol versioning for shared liblzma.
429     # Note that adding link options doesn't affect static builds.
430     target_compile_definitions(liblzma PRIVATE HAVE_SYMBOL_VERSIONS_LINUX)
431     target_link_options(liblzma PRIVATE
432         "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
433     )
434     set_target_properties(liblzma PROPERTIES
435         LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
436     )
437 elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
438     # Symbol versioning for shared liblzma for non-GNU/Linux.
439     # FIXME? What about Solaris?
440     target_link_options(liblzma PRIVATE
441         "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
442     )
443     set_target_properties(liblzma PROPERTIES
444         LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
445     )
446 endif()
448 set_target_properties(liblzma PROPERTIES
449     # At least for now the package versioning matches the rules used for
450     # shared library versioning (excluding development releases) so it is
451     # fine to use the package version here.
452     SOVERSION "${xz_VERSION_MAJOR}"
453     VERSION "${xz_VERSION}"
455     # It's liblzma.so or liblzma.dll, not libliblzma.so or lzma.dll.
456     # Avoid the name lzma.dll because it would conflict with LZMA SDK.
457     PREFIX ""
460 # Create liblzma-config-version.cmake. We use this spelling instead of
461 # liblzmaConfig.cmake to make find_package work in case insensitive manner
462 # even with case sensitive file systems. This gives more consistent behavior
463 # between operating systems.
465 # FIXME: SameMajorVersion is correct for stable releases but it is wrong
466 # for development releases where each release may have incompatible changes.
467 include(CMakePackageConfigHelpers)
468 write_basic_package_version_file(
469     "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
470     VERSION "${liblzma_VERSION}"
471     COMPATIBILITY SameMajorVersion)
473 # Create liblzma-config.cmake.
474 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
475 "include(CMakeFindDependencyMacro)
476 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
477 find_dependency(Threads)
479 include(\"\${CMAKE_CURRENT_LIST_DIR}/liblzma-targets.cmake\")
481 # Be compatible with the spelling used by the FindLibLZMA module. This
482 # doesn't use ALIAS because it would make CMake resolve LibLZMA::LibLZMA
483 # to liblzma::liblzma instead of keeping the original spelling. Keeping
484 # the original spelling is important for good FindLibLZMA compatibility.
485 add_library(LibLZMA::LibLZMA INTERFACE IMPORTED)
486 set_target_properties(LibLZMA::LibLZMA PROPERTIES
487                       INTERFACE_LINK_LIBRARIES liblzma::liblzma)
490 # Set CMAKE_INSTALL_LIBDIR and friends.
491 include(GNUInstallDirs)
493 # Install the library binary. The INCLUDES specifies the include path that
494 # is exported for other projects to use but it doesn't install any files.
495 install(TARGETS liblzma EXPORT liblzmaTargets
496         RUNTIME  DESTINATION "${CMAKE_INSTALL_BINDIR}"
497                  COMPONENT liblzma_Runtime
498         LIBRARY  DESTINATION "${CMAKE_INSTALL_LIBDIR}"
499                  COMPONENT liblzma_Runtime
500                  NAMELINK_COMPONENT liblzma_Development
501         ARCHIVE  DESTINATION "${CMAKE_INSTALL_LIBDIR}"
502                  COMPONENT liblzma_Development
503         INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
505 # Install the liblzma API headers. These use a subdirectory so
506 # this has to be done as a separate step.
507 install(DIRECTORY src/liblzma/api/
508         COMPONENT liblzma_Development
509         DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
510         FILES_MATCHING PATTERN "*.h")
512 # Install the CMake files that other packages can use to find liblzma.
513 set(liblzma_INSTALL_CMAKEDIR
514     "${CMAKE_INSTALL_LIBDIR}/cmake/liblzma"
515     CACHE STRING "Path to liblzma's .cmake files")
517 install(EXPORT liblzmaTargets
518         NAMESPACE liblzma::
519         FILE liblzma-targets.cmake
520         DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
521         COMPONENT liblzma_Development)
523 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
524               "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
525         DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
526         COMPONENT liblzma_Development)
529 #############################################################################
530 # getopt_long
531 #############################################################################
533 # The command line tools needs this.
534 check_symbol_exists(getopt_long getopt.h HAVE_GETOPT_LONG)
537 #############################################################################
538 # xzdec
539 #############################################################################
541 if(HAVE_GETOPT_LONG)
542     add_executable(xzdec
543         src/common/sysdefs.h
544         src/common/tuklib_common.h
545         src/common/tuklib_config.h
546         src/common/tuklib_exit.c
547         src/common/tuklib_exit.h
548         src/common/tuklib_gettext.h
549         src/common/tuklib_progname.c
550         src/common/tuklib_progname.h
551         src/xzdec/xzdec.c
552     )
554     target_include_directories(xzdec PRIVATE
555         src/common
556         src/liblzma/api
557     )
559     target_link_libraries(xzdec PRIVATE liblzma)
561     tuklib_progname(xzdec)
563     install(TARGETS xzdec
564             RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
565                     COMPONENT xzdec)
567     if(UNIX)
568         install(FILES src/xzdec/xzdec.1
569                 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
570                 COMPONENT xzdec)
571     endif()
572 endif()
575 #############################################################################
576 # xz
577 #############################################################################
579 if(NOT MSVC AND HAVE_GETOPT_LONG)
580     add_executable(xz
581         src/common/mythread.h
582         src/common/sysdefs.h
583         src/common/tuklib_common.h
584         src/common/tuklib_config.h
585         src/common/tuklib_exit.c
586         src/common/tuklib_exit.h
587         src/common/tuklib_gettext.h
588         src/common/tuklib_integer.h
589         src/common/tuklib_mbstr.h
590         src/common/tuklib_mbstr_fw.c
591         src/common/tuklib_mbstr_width.c
592         src/common/tuklib_open_stdxxx.c
593         src/common/tuklib_open_stdxxx.h
594         src/common/tuklib_progname.c
595         src/common/tuklib_progname.h
596         src/xz/args.c
597         src/xz/args.h
598         src/xz/coder.c
599         src/xz/coder.h
600         src/xz/file_io.c
601         src/xz/file_io.h
602         src/xz/hardware.c
603         src/xz/hardware.h
604         src/xz/list.c
605         src/xz/list.h
606         src/xz/main.c
607         src/xz/main.h
608         src/xz/message.c
609         src/xz/message.h
610         src/xz/mytime.c
611         src/xz/mytime.h
612         src/xz/options.c
613         src/xz/options.h
614         src/xz/private.h
615         src/xz/signals.c
616         src/xz/signals.h
617         src/xz/suffix.c
618         src/xz/suffix.h
619         src/xz/util.c
620         src/xz/util.h
621     )
623     target_include_directories(xz PRIVATE
624         src/common
625         src/liblzma/api
626     )
628     target_link_libraries(xz PRIVATE liblzma)
630     target_compile_definitions(xz PRIVATE ASSUME_RAM=128)
632     tuklib_progname(xz)
633     tuklib_mbstr(xz)
635     check_symbol_exists(optreset getopt.h HAVE_OPTRESET)
636     tuklib_add_definition_if(xz HAVE_OPTRESET)
638     check_symbol_exists(posix_fadvise fcntl.h HAVE_POSIX_FADVISE)
639     tuklib_add_definition_if(xz HAVE_POSIX_FADVISE)
641     # How to get file time:
642     check_struct_has_member("struct stat" st_atim.tv_nsec
643                             "sys/types.h;sys/stat.h"
644                             HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
645     if(HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
646         tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
647     else()
648         check_struct_has_member("struct stat" st_atimespec.tv_nsec
649                                 "sys/types.h;sys/stat.h"
650                                 HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
651         if(HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
652             tuklib_add_definitions(xz HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
653         else()
654             check_struct_has_member("struct stat" st_atimensec
655                                     "sys/types.h;sys/stat.h"
656                                     HAVE_STRUCT_STAT_ST_ATIMENSEC)
657             tuklib_add_definition_if(xz HAVE_STRUCT_STAT_ST_ATIMENSEC)
658         endif()
659     endif()
661     # How to set file time:
662     check_symbol_exists(futimens "sys/types.h;sys/stat.h" HAVE_FUTIMENS)
663     if(HAVE_FUTIMENS)
664         tuklib_add_definitions(xz HAVE_FUTIMENS)
665     else()
666         check_symbol_exists(futimes "sys/time.h" HAVE_FUTIMES)
667         if(HAVE_FUTIMES)
668             tuklib_add_definitions(xz HAVE_FUTIMES)
669         else()
670             check_symbol_exists(futimesat "sys/time.h" HAVE_FUTIMESAT)
671             if(HAVE_FUTIMESAT)
672                 tuklib_add_definitions(xz HAVE_FUTIMESAT)
673             else()
674                 check_symbol_exists(utimes "sys/time.h" HAVE_UTIMES)
675                 if(HAVE_UTIMES)
676                     tuklib_add_definitions(xz HAVE_UTIMES)
677                 else()
678                     check_symbol_exists(_futime "sys/utime.h" HAVE__FUTIME)
679                     if(HAVE__FUTIME)
680                         tuklib_add_definitions(xz HAVE__FUTIME)
681                     else()
682                         check_symbol_exists(utime "utime.h" HAVE_UTIME)
683                         tuklib_add_definition_if(xz HAVE_UTIME)
684                     endif()
685                 endif()
686             endif()
687         endif()
688     endif()
690     install(TARGETS xz
691             RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
692                     COMPONENT xz)
694     if(UNIX)
695         install(FILES src/xz/xz.1
696                 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
697                 COMPONENT xz)
699         option(CREATE_XZ_SYMLINKS "Create unxz and xzcat symlinks" ON)
700         option(CREATE_LZMA_SYMLINKS "Create lzma, unlzma, and lzcat symlinks"
701                ON)
702         set(XZ_LINKS)
704         if(CREATE_XZ_SYMLINKS)
705             list(APPEND XZ_LINKS "unxz" "xzcat")
706         endif()
708         if(CREATE_LZMA_SYMLINKS)
709             list(APPEND XZ_LINKS "lzma" "unlzma" "lzcat")
710         endif()
712         # Create symlinks in the build directory and then install them.
713         #
714         # The symlinks do not likely need any special extension since
715         # even on Windows the symlink can still be executed without
716         # the .exe extension.
717         foreach(LINK IN LISTS XZ_LINKS)
718             add_custom_target("${LINK}" ALL
719                 "${CMAKE_COMMAND}" -E create_symlink
720                     "$<TARGET_FILE_NAME:xz>" "${LINK}"
721                 BYPRODUCTS "${LINK}"
722                 VERBATIM)
723             install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}"
724                     DESTINATION "${CMAKE_INSTALL_BINDIR}"
725                     COMPONENT xz)
726             add_custom_target("${LINK}.1" ALL
727                 "${CMAKE_COMMAND}" -E create_symlink "xz.1" "${LINK}.1"
728                 BYPRODUCTS "${LINK}.1"
729                 VERBATIM)
730             install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}.1"
731                     DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
732                     COMPONENT xz)
733         endforeach()
734     endif()
735 endif()