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
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!):
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)
66 #define LZMA_VERSION_MAJOR ([0-9]+)\n\
68 #define LZMA_VERSION_MINOR ([0-9]+)\n\
70 #define LZMA_VERSION_PATCH ([0-9]+)\n\
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")
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")
110 # Definitions common to all targets:
111 add_compile_definitions(
113 PACKAGE_NAME="${PACKAGE_NAME}"
114 PACKAGE_BUGREPORT="xz@tukaani.org"
115 PACKAGE_URL="https://tukaani.org/xz/"
123 HAVE_DECODER_ARMTHUMB
134 HAVE_ENCODER_ARMTHUMB
150 # Standard headers and types are available:
156 # Disable assert() checks when no build type has been specified. Non-empty
157 # build types like "Release" and "Debug" handle this by default.
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.
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)
184 list(REMOVE_AT CMAKE_REQUIRED_LIBRARIES 0)
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(
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)
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)
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)
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)
232 #############################################################################
234 #############################################################################
236 option(BUILD_SHARED_LIBS "Build liblzma as a shared library instead of static")
239 src/common/mythread.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
392 src/liblzma/rangecoder
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.
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.")
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; }
434 HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
435 cmake_pop_check_state()
436 tuklib_add_definition_if(liblzma HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR)
439 check_include_file(cpuid.h HAVE_CPUID_H)
440 tuklib_add_definition_if(liblzma HAVE_CPUID_H)
443 check_include_file(immintrin.h HAVE_IMMINTRIN_H)
445 target_compile_definitions(liblzma PRIVATE HAVE_IMMINTRIN_H)
448 check_c_source_compiles("
449 #include <immintrin.h>
453 _mm_movemask_epi8(x);
457 HAVE__MM_MOVEMASK_EPI8)
458 tuklib_add_definition_if(liblzma HAVE__MM_MOVEMASK_EPI8)
461 check_c_source_compiles("
462 #include <immintrin.h>
463 #if defined(__e2k__) && __iset__ < 6
466 #if (defined(__GNUC__) || defined(__clang__)) && !defined(__EDG__)
467 __attribute__((__target__(\"ssse3,sse4.1,pclmul\")))
469 __m128i my_clmul(__m128i a)
471 const __m128i b = _mm_set_epi64x(1, 2);
472 return _mm_clmulepi64_si128(a, b, 0);
474 int main(void) { return 0; }
477 tuklib_add_definition_if(liblzma HAVE_USABLE_CLMUL)
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)
487 target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=0)
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"
499 # Export the public API symbols with __declspec(dllexport).
500 target_compile_definitions(liblzma PRIVATE DLL_EXPORT)
502 # Disable __declspec(dllimport) when linking against static liblzma.
503 target_compile_definitions(liblzma INTERFACE LZMA_API_STATIC)
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.
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"
519 set_target_properties(liblzma PROPERTIES
520 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map"
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"
528 set_target_properties(liblzma PROPERTIES
529 LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map"
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.
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
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 #############################################################################
616 #############################################################################
618 # The command line tools needs this.
619 check_symbol_exists(getopt_long getopt.h HAVE_GETOPT_LONG)
622 #############################################################################
624 #############################################################################
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
639 target_include_directories(xzdec PRIVATE
644 target_link_libraries(xzdec PRIVATE liblzma)
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"
654 tuklib_progname(xzdec)
656 install(TARGETS xzdec
657 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
661 install(FILES src/xzdec/xzdec.1
662 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
668 #############################################################################
670 #############################################################################
672 if(NOT MSVC AND HAVE_GETOPT_LONG)
674 src/common/mythread.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
716 target_include_directories(xz PRIVATE
721 target_link_libraries(xz PRIVATE liblzma)
723 target_compile_definitions(xz PRIVATE ASSUME_RAM=128)
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"
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)
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)
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)
762 # How to set file time:
763 check_symbol_exists(futimens "sys/types.h;sys/stat.h" HAVE_FUTIMENS)
765 tuklib_add_definitions(xz HAVE_FUTIMENS)
767 check_symbol_exists(futimes "sys/time.h" HAVE_FUTIMES)
769 tuklib_add_definitions(xz HAVE_FUTIMES)
771 check_symbol_exists(futimesat "sys/time.h" HAVE_FUTIMESAT)
773 tuklib_add_definitions(xz HAVE_FUTIMESAT)
775 check_symbol_exists(utimes "sys/time.h" HAVE_UTIMES)
777 tuklib_add_definitions(xz HAVE_UTIMES)
779 check_symbol_exists(_futime "sys/utime.h" HAVE__FUTIME)
781 tuklib_add_definitions(xz HAVE__FUTIME)
783 check_symbol_exists(utime "utime.h" HAVE_UTIME)
784 tuklib_add_definition_if(xz HAVE_UTIME)
792 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
796 install(FILES src/xz/xz.1
797 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
800 option(CREATE_XZ_SYMLINKS "Create unxz and xzcat symlinks" ON)
801 option(CREATE_LZMA_SYMLINKS "Create lzma, unlzma, and lzcat symlinks"
805 if(CREATE_XZ_SYMLINKS)
806 list(APPEND XZ_LINKS "unxz" "xzcat")
809 if(CREATE_LZMA_SYMLINKS)
810 list(APPEND XZ_LINKS "lzma" "unlzma" "lzcat")
813 # Create symlinks in the build directory and then install them.
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}"
824 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}"
825 DESTINATION "${CMAKE_INSTALL_BINDIR}"
827 add_custom_target("${LINK}.1" ALL
828 "${CMAKE_COMMAND}" -E create_symlink "xz.1" "${LINK}.1"
829 BYPRODUCTS "${LINK}.1"
831 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}.1"
832 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1"
839 #############################################################################
841 #############################################################################
860 foreach(TEST IN LISTS LIBLZMA_TESTS)
861 add_executable("${TEST}" "tests/${TEST}.c")
863 target_include_directories("${TEST}" PRIVATE
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"
878 add_test(NAME "${TEST}"
879 COMMAND "${CMAKE_CURRENT_BINARY_DIR}/tests_bin/${TEST}"
882 # Set srcdir environment variable so that the tests find their
883 # input files from the source tree.
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"