diff options
Diffstat (limited to '')
-rw-r--r-- | CMakeLists.txt | 1115 |
1 files changed, 890 insertions, 225 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index b214d71..59ecf01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,32 +1,88 @@ +# SPDX-License-Identifier: 0BSD + ############################################################################# # -# Very limited CMake support for building some parts of XZ Utils +# CMake support for building XZ Utils +# +# The complete CMake-based build hasn't been tested much yet and +# thus it's still slightly experimental. Testing this especially +# outside GNU/Linux and Windows would be great now. +# +# A few things are still missing compared to the Autotools-based build: +# +# - 32-bit x86 assembly code for CRC32 and CRC64 isn't used by default. +# Use the option -DENABLE_X86_ASM=ON on the CMake command line to +# enable the assembly files. They are compatible with Linux, *BSDs, +# Cygwin, MinGW-w64, and Darwin. They are NOT compatible with MSVC. +# +# NOTE: The C code includes a generic version compatible with all +# processors and CLMUL version that requires a new enough processor +# with the PCLMULQDQ instruction. If the 32-bit x86 assembly files +# are used, the CLMUL version in the C code is NOT built. On modern +# processors with CLMUL support, the C code should be faster than +# the assembly code while on old processors the assembly code wins. +# +# - External SHA-256 code isn't supported but it's disabled by +# default in the Autotools build too (--enable-external-sha256). +# +# - Extra compiler warning flags aren't added by default. +# +# About CMAKE_BUILD_TYPE: # -# For now, this is intended to be useful to build static or shared liblzma -# on Windows with MSVC (to avoid the need to maintain Visual Studio project -# files). Building liblzma on a few other platforms should work too but it -# is somewhat experimental and not as portable as using ./configure. +# - CMake's standard choices are fine to use for production builds, +# including "Release" and "RelWithDebInfo". # -# On some platforms this builds also xz and xzdec, but these are -# highly experimental and meant for testing only: -# - No replacement getopt_long(), libc must have it -# - No sandboxing support -# - No translations +# NOTE: While "Release" uses -O3 by default with some compilers, +# this file overrides -O3 to -O2 for "Release" builds if +# CMAKE_C_FLAGS_RELEASE is not defined by the user. At least +# with GCC and Clang/LLVM, -O3 doesn't seem useful for this +# package as it can result in bigger binaries without any +# improvement in speed compared to -O2. # -# Other missing things: -# - No xzgrep or other scripts or their symlinks -# - No xz tests (liblzma tests only) +# - Empty value (the default) is handled slightly specially: It +# adds -DNDEBUG to disable debugging code (assert() and a few +# other things). No optimization flags are added so an empty +# CMAKE_BUILD_TYPE is an easy way to build with whatever +# optimization flags one wants, and so this method is also +# suitable for production builds. # -# NOTE: Even if the code compiles without warnings, the end result may be -# different than via ./configure. Specifically, the list of #defines -# may be different (if so, probably this CMakeLists.txt got them wrong). +# If debugging is wanted when using empty CMAKE_BUILD_TYPE, +# include -UNDEBUG in the CFLAGS environment variable or +# in the CMAKE_C_FLAGS CMake variable to override -DNDEBUG. +# With empty CMAKE_BUILD_TYPE, the -UNDEBUG option will go +# after the -DNDEBUG option on the compiler command line and +# thus NDEBUG will be undefined. +# +# - Non-standard build types like "None" aren't treated specially +# and thus won't have -DNEBUG. Such non-standard build types +# SHOULD BE AVOIDED FOR PRODUCTION BUILDS. Or at least one +# should remember to add -DNDEBUG. +# +# If building from xz.git instead of a release tarball, consider +# the following *before* running cmake: +# +# - To get translated messages, install GNU gettext tools (the +# command msgfmt is needed). Alternatively disable translations +# by setting ENABLE_NLS=OFF. +# +# - To get translated man pages, run po4a/update-po which requires +# the po4a tool. The build works without this step too. # # This file provides the following installation components (if you only # need liblzma, install only its components!): -# - liblzma_Runtime +# - liblzma_Runtime (shared library only) # - liblzma_Development -# - xz (on some platforms only) -# - xzdec (on some platforms only) +# - liblzma_Documentation (examples and Doxygen-generated API docs as HTML) +# - xz_Runtime (xz, the symlinks, and possibly translation files) +# - xz_Documentation (xz man pages and the symlinks) +# - xzdec_Runtime +# - xzdec_Documentation (xzdec *and* lzmadec man pages) +# - lzmadec_Runtime +# - lzmainfo_Runtime +# - lzmainfo_Documentation (lzmainfo man pages) +# - scripts_Runtime (xzdiff, xzgrep, xzless, xzmore) +# - scripts_Documentation (their man pages) +# - Documentation (generic docs like README and licenses) # # To find the target liblzma::liblzma from other packages, use the CONFIG # option with find_package() to avoid a conflict with the FindLibLZMA module @@ -40,12 +96,10 @@ # # Author: Lasse Collin # -# This file has been put into the public domain. -# You can do whatever you want with this file. -# ############################################################################# -cmake_minimum_required(VERSION 3.13...3.27 FATAL_ERROR) +# NOTE: Translation support is disabled with CMake older than 3.20. +cmake_minimum_required(VERSION 3.14...3.29 FATAL_ERROR) include(CMakePushCheckState) include(CheckIncludeFile) @@ -75,9 +129,28 @@ string(REGEX REPLACE .*$" "\\1.\\2.\\3" PACKAGE_VERSION "${PACKAGE_VERSION}") +# With several compilers, CMAKE_BUILD_TYPE=Release uses -O3 optimization +# which results in bigger code without a clear difference in speed. If +# no user-defined CMAKE_C_FLAGS_RELEASE is present, override -O3 to -O2 +# to make it possible to recommend CMAKE_BUILD_TYPE=Release. +if(NOT DEFINED CMAKE_C_FLAGS_RELEASE) + set(OVERRIDE_O3_IN_C_FLAGS_RELEASE ON) +endif() + # Among other things, this gives us variables xz_VERSION and xz_VERSION_MAJOR. project(xz VERSION "${PACKAGE_VERSION}" LANGUAGES C) +if(OVERRIDE_O3_IN_C_FLAGS_RELEASE) + # Looking at CMake's source, there aren't any _FLAGS_RELEASE_INIT + # entries where "-O3" would appear as part of some other option, + # thus a simple search and replace should be fine. + string(REPLACE -O3 -O2 CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}") + + # Update the cache value while keeping its docstring unchanged. + set_property(CACHE CMAKE_C_FLAGS_RELEASE + PROPERTY VALUE "${CMAKE_C_FLAGS_RELEASE}") +endif() + # We need a compiler that supports enough C99 or newer (variable-length arrays # aren't needed, those are optional in C17). Setting CMAKE_C_STANDARD here # makes it the default for all targets. It doesn't affect the INTERFACE so @@ -86,9 +159,21 @@ project(xz VERSION "${PACKAGE_VERSION}" LANGUAGES C) set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED ON) +# Support 32-bit x86 assembly files. +if(NOT MSVC) + option(ENABLE_X86_ASM "Enable 32-bit x86 assembly code" OFF) + if(ENABLE_X86_ASM) + enable_language(ASM) + endif() +endif() + # On Apple OSes, don't build executables as bundles: set(CMAKE_MACOSX_BUNDLE OFF) +# Set CMAKE_INSTALL_LIBDIR and friends. This needs to be done before +# the LOCALEDIR_DEFINITION workaround below. +include(GNUInstallDirs) + # windres from GNU binutils can be tricky with command line arguments # that contain spaces or other funny characters. Unfortunately we need # a space in PACKAGE_NAME. Using \x20 to encode the US-ASCII space seems @@ -116,10 +201,15 @@ if((MINGW OR CYGWIN OR MSYS) AND ( # Keep the original PACKAGE_NAME intact for generation of liblzma.pc. string(APPEND CMAKE_RC_FLAGS " --use-temp-file") string(REPLACE " " "\\x20" PACKAGE_NAME_DEFINITION "${PACKAGE_NAME}") + + # Use octal because "Program Files" would become \x20F. + string(REPLACE " " "\\040" LOCALEDIR_DEFINITION + "${CMAKE_INSTALL_FULL_LOCALEDIR}") else() # Elsewhere a space is safe. This also keeps things compatible with # EBCDIC in case CMake-based build is ever done on such a system. set(PACKAGE_NAME_DEFINITION "${PACKAGE_NAME}") + set(LOCALEDIR_DEFINITION "${CMAKE_INSTALL_FULL_LOCALEDIR}") endif() # Definitions common to all targets: @@ -165,32 +255,80 @@ set(LIBS) # Check for clock_gettime(). Do this before checking for threading so # that we know there if CLOCK_MONOTONIC is available. -if(NOT WIN32) - check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME) - - if(NOT HAVE_CLOCK_GETTIME) - # With glibc <= 2.17 or Solaris 10 this needs librt. - # Add librt for the next check for HAVE_CLOCK_GETTIME. If it is - # found after including the library, we know that librt is required. - list(INSERT CMAKE_REQUIRED_LIBRARIES 0 rt) - check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME_LIBRT) - - # If it was found now, add librt to all targets and keep it in - # CMAKE_REQUIRED_LIBRARIES for further tests too. - if(HAVE_CLOCK_GETTIME_LIBRT) - link_libraries(rt) - set(LIBS "-lrt") # For liblzma.pc - else() - list(REMOVE_AT CMAKE_REQUIRED_LIBRARIES 0) - endif() +check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME) + +if(NOT HAVE_CLOCK_GETTIME) + # With glibc <= 2.17 or Solaris 10 this needs librt. + # Add librt for the next check for HAVE_CLOCK_GETTIME. If it is + # found after including the library, we know that librt is required. + list(INSERT CMAKE_REQUIRED_LIBRARIES 0 rt) + check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME_LIBRT) + + # If it was found now, add librt to all targets and keep it in + # CMAKE_REQUIRED_LIBRARIES for further tests too. + if(HAVE_CLOCK_GETTIME_LIBRT) + link_libraries(rt) + set(LIBS "-lrt") # For liblzma.pc + else() + list(REMOVE_AT CMAKE_REQUIRED_LIBRARIES 0) endif() +endif() - if(HAVE_CLOCK_GETTIME OR HAVE_CLOCK_GETTIME_LIBRT) - add_compile_definitions(HAVE_CLOCK_GETTIME) +if(HAVE_CLOCK_GETTIME OR HAVE_CLOCK_GETTIME_LIBRT) + add_compile_definitions(HAVE_CLOCK_GETTIME) - # Check if CLOCK_MONOTONIC is available for clock_gettime(). - check_symbol_exists(CLOCK_MONOTONIC time.h HAVE_CLOCK_MONOTONIC) - tuklib_add_definition_if(ALL HAVE_CLOCK_MONOTONIC) + # Check if CLOCK_MONOTONIC is available for clock_gettime(). + check_symbol_exists(CLOCK_MONOTONIC time.h HAVE_CLOCK_MONOTONIC) + tuklib_add_definition_if(ALL HAVE_CLOCK_MONOTONIC) +endif() + +# Translation support requires CMake 3.20 because it added the Intl::Intl +# target so we don't need to play with the individual variables. +# +# The definition ENABLE_NLS is added only to those targets that use it, thus +# it's not done here. (xz has translations, xzdec doesn't.) +if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.20") + find_package(Intl) + find_package(Gettext) + + if(Intl_FOUND) + option(ENABLE_NLS "Native Language Support (translated messages)" ON) + + # If translation support is enabled but neither gettext tools or + # pre-generated .gmo files exist, translation support cannot be + # enabled. + # + # The detection of pre-generated .gmo files is done by only + # checking for the existence of a single .gmo file; Ukrainian + # is one of many translations that gets regular updates. + if(ENABLE_NLS AND NOT GETTEXT_FOUND AND + NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/po/uk.gmo") + # This only sets the variable, not the cache variable! + set(ENABLE_NLS OFF) + + # This message is shown only when new enough CMake is used and + # library support for translations was found. The assumptions is + # that in this situation the user might have interest in the + # translations. This also keeps this code simpler. + message(WARNING "Native language support (NLS) has been disabled. " + "NLS support requires either gettext tools or " + "pre-generated .gmo files. The latter are only " + "available in distribution tarballs. " + "To avoid this warning, NLS can be explicitly " + "disabled by passing -DENABLE_NLS=OFF to cmake.") + endif() + + # Warn if NLS is enabled but translated man pages are missing. + if(UNIX AND ENABLE_NLS AND + NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/po4a/man") + message(WARNING "Native language support (NLS) has been enabled " + "but pre-generated translated man pages " + "were not found and thus they won't be installed. " + "Run 'po4a/update-po' to generate them.") + endif() + + # The *installed* name of the translation files is "xz.mo". + set(TRANSLATION_DOMAIN "xz") endif() endif() @@ -207,14 +345,69 @@ endif() option(BUILD_SHARED_LIBS "Build liblzma as a shared library instead of static") -add_library(liblzma - src/common/mythread.h - src/common/sysdefs.h - src/common/tuklib_common.h - src/common/tuklib_config.h - src/common/tuklib_integer.h - src/common/tuklib_physmem.c - src/common/tuklib_physmem.h +if(NOT WIN32) + # Symbol versioning only affects ELF shared libraries. The option is + # ignored for static libraries. + # + # Determine the default value so that it's always set with + # shared libraries in mind which helps if the build dir is reconfigured + # from static to shared libs without resetting the cache variables. + set(SYMBOL_VERSIONING_DEFAULT OFF) + + if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND + (CMAKE_SYSTEM_PROCESSOR MATCHES "[Mm]icro[Bb]laze" OR + CMAKE_C_COMPILER_ID STREQUAL "NVHPC")) + # As a special case, GNU/Linux on MicroBlaze gets the generic + # symbol versioning because GCC 12 doesn't support the __symver__ + # attribute on MicroBlaze. On Linux, CMAKE_SYSTEM_PROCESSOR comes + # from "uname -m" for native builds (should be "microblaze") or from + # the CMake toolchain file (not perfectly standardized but it very + # likely has "microblaze" in lower case or mixed case somewhere in + # the string). + # + # NVIDIA HPC Compiler doesn't support symbol versioning but + # it uses the linked from the system so the linker script + # can still be used to get the generic symbol versioning. + set(SYMBOL_VERSIONING_DEFAULT "generic") + + elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux") + # GNU/Linux-specific symbol versioning for shared liblzma. + # This includes a few extra compatibility symbols for RHEL/CentOS 7 + # which are pointless on non-glibc non-Linux systems. + # + # Avoid symvers on Linux with non-glibc like musl and uClibc. + # In Autoconf it's enough to check that $host_os equals linux-gnu + # instead of, for example, linux-musl. CMake doesn't provide such + # a method. + # + # This check is here for now since it's not strictly required + # by anything else. + check_c_source_compiles( + "#include <features.h> + #if defined(__GLIBC__) && !defined(__UCLIBC__) + int main(void) { return 0; } + #else + compile error + #endif + " + IS_LINUX_WITH_GLIBC) + + if(IS_LINUX_WITH_GLIBC) + set(SYMBOL_VERSIONING_DEFAULT "linux") + endif() + + elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD") + set(SYMBOL_VERSIONING_DEFAULT "generic") + endif() + + set(SYMBOL_VERSIONING "${SYMBOL_VERSIONING_DEFAULT}" CACHE STRING + "Enable ELF shared library symbol versioning (OFF, generic, linux)") + + # Show a dropdown menu in CMake GUI: + set_property(CACHE SYMBOL_VERSIONING PROPERTY STRINGS "OFF;generic;linux") +endif() + +set(LIBLZMA_API_HEADERS src/liblzma/api/lzma.h src/liblzma/api/lzma/base.h src/liblzma/api/lzma/bcj.h @@ -230,9 +423,22 @@ add_library(liblzma src/liblzma/api/lzma/stream_flags.h src/liblzma/api/lzma/version.h src/liblzma/api/lzma/vli.h +) + +add_library(liblzma + src/common/mythread.h + src/common/sysdefs.h + src/common/tuklib_common.h + src/common/tuklib_config.h + src/common/tuklib_integer.h + src/common/tuklib_physmem.c + src/common/tuklib_physmem.h + ${LIBLZMA_API_HEADERS} src/liblzma/check/check.c src/liblzma/check/check.h - src/liblzma/check/crc_macros.h + src/liblzma/check/crc_common.h + src/liblzma/check/crc_x86_clmul.h + src/liblzma/check/crc32_arm64.h src/liblzma/common/block_util.c src/liblzma/common/common.c src/liblzma/common/common.h @@ -294,11 +500,16 @@ if(ENABLE_SMALL) target_sources(liblzma PRIVATE src/liblzma/check/crc32_small.c) else() target_sources(liblzma PRIVATE - src/liblzma/check/crc32_fast.c src/liblzma/check/crc32_table.c src/liblzma/check/crc32_table_be.h src/liblzma/check/crc32_table_le.h ) + + if(ENABLE_X86_ASM) + target_sources(liblzma PRIVATE src/liblzma/check/crc32_x86.S) + else() + target_sources(liblzma PRIVATE src/liblzma/check/crc32_fast.c) + endif() endif() if("crc64" IN_LIST ADDITIONAL_CHECK_TYPES) @@ -308,11 +519,16 @@ if("crc64" IN_LIST ADDITIONAL_CHECK_TYPES) target_sources(liblzma PRIVATE src/liblzma/check/crc64_small.c) else() target_sources(liblzma PRIVATE - src/liblzma/check/crc64_fast.c src/liblzma/check/crc64_table.c src/liblzma/check/crc64_table_be.h src/liblzma/check/crc64_table_le.h ) + + if(ENABLE_X86_ASM) + target_sources(liblzma PRIVATE src/liblzma/check/crc64_x86.S) + else() + target_sources(liblzma PRIVATE src/liblzma/check/crc64_fast.c) + endif() endif() endif() @@ -451,6 +667,7 @@ set(SIMPLE_FILTERS powerpc ia64 sparc + riscv ) # The SUPPORTED_FILTERS are shared between Encoders and Decoders @@ -746,6 +963,114 @@ if(LZIP_DECODER) ) endif() + +############## +# Sandboxing # +############## + +# ON Use sandboxing if a supported method is available in the OS. +# OFF Disable sandboxing. +# capsicum Require Capsicum (FreeBSD >= 10.2) and fail if not found. +# pledge Require pledge(2) (OpenBSD >= 5.9) and fail if not found. +# landlock Require Landlock (Linux >= 5.13) and fail if not found. +set(SUPPORTED_SANDBOX_METHODS ON OFF capsicum pledge landlock) + +set(ENABLE_SANDBOX ON CACHE STRING + "Sandboxing method to use in 'xz', 'xzdec', and 'lzmadec'") + +set_property(CACHE ENABLE_SANDBOX + PROPERTY STRINGS "${SUPPORTED_SANDBOX_METHODS}") + +if(NOT ENABLE_SANDBOX IN_LIST SUPPORTED_SANDBOX_METHODS) + message(FATAL_ERROR "'${ENABLE_SANDBOX}' is not a supported " + "sandboxing method") +endif() + +# When autodetecting, the search order is fixed and we must not find +# more than one method. +if(ENABLE_SANDBOX STREQUAL "OFF") + set(SANDBOX_FOUND ON) +else() + set(SANDBOX_FOUND OFF) +endif() + +# Since xz and xzdec can both use sandboxing, the compile definition needed +# to use the sandbox must be added to both targets. +set(SANDBOX_COMPILE_DEFINITION OFF) + +# Sandboxing: Capsicum +if(NOT SANDBOX_FOUND AND ENABLE_SANDBOX MATCHES "^ON$|^capsicum$") + check_symbol_exists(cap_rights_limit sys/capsicum.h + HAVE_CAP_RIGHTS_LIMIT) + if(HAVE_CAP_RIGHTS_LIMIT) + set(SANDBOX_COMPILE_DEFINITION "HAVE_CAP_RIGHTS_LIMIT") + set(SANDBOX_FOUND ON) + endif() +endif() + +# Sandboxing: pledge(2) +if(NOT SANDBOX_FOUND AND ENABLE_SANDBOX MATCHES "^ON$|^pledge$") + check_symbol_exists(pledge unistd.h HAVE_PLEDGE) + if(HAVE_PLEDGE) + set(SANDBOX_COMPILE_DEFINITION "HAVE_PLEDGE") + set(SANDBOX_FOUND ON) + endif() +endif() + +# Sandboxing: Landlock +if(NOT SANDBOX_FOUND AND ENABLE_SANDBOX MATCHES "^ON$|^landlock$") + # A compile check is done here because some systems have + # linux/landlock.h, but do not have the syscalls defined + # in order to actually use Linux Landlock. + check_c_source_compiles(" + #include <linux/landlock.h> + #include <sys/syscall.h> + #include <sys/prctl.h> + + void my_sandbox(void) + { + (void)prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); + (void)SYS_landlock_create_ruleset; + (void)SYS_landlock_restrict_self; + (void)LANDLOCK_CREATE_RULESET_VERSION; + return; + } + + int main(void) { return 0; } + " + HAVE_LINUX_LANDLOCK) + + if(HAVE_LINUX_LANDLOCK) + set(SANDBOX_COMPILE_DEFINITION "HAVE_LINUX_LANDLOCK") + set(SANDBOX_FOUND ON) + + # Of our three sandbox methods, only Landlock is incompatible + # with -fsanitize. FreeBSD 13.2 with Capsicum was tested with + # -fsanitize=address,undefined and had no issues. OpenBSD (as + # of version 7.4) has minimal support for process instrumentation. + # OpenBSD does not distribute the additional libraries needed + # (libasan, libubsan, etc.) with GCC or Clang needed for runtime + # sanitization support and instead only support + # -fsanitize-minimal-runtime for minimal undefined behavior + # sanitization. This minimal support is compatible with our use + # of the Pledge sandbox. So only Landlock will result in a + # build that cannot compress or decompress a single file to + # standard out. + if(CMAKE_C_FLAGS MATCHES "-fsanitize=") + message(SEND_ERROR + "CMAKE_C_FLAGS or the environment variable CFLAGS " + "contains '-fsanitize=' which is incompatible " + "with Landlock sandboxing. Use -DENABLE_SANDBOX=OFF " + "as an argument to 'cmake' when using '-fsanitize'.") + endif() + endif() +endif() + +if(NOT SANDBOX_FOUND AND NOT ENABLE_SANDBOX MATCHES "^ON$|^OFF$") + message(SEND_ERROR "ENABLE_SANDBOX=${ENABLE_SANDBOX} was used but " + "support for the sandboxing method wasn't found.") +endif() + ### # Put the tuklib functions under the lzma_ namespace. @@ -798,6 +1123,7 @@ if(USE_WIN95_THREADS AND ENABLE_SMALL AND NOT HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR) "__attribute__((__constructor__))") endif() + # cpuid.h check_include_file(cpuid.h HAVE_CPUID_H) tuklib_add_definition_if(liblzma HAVE_CPUID_H) @@ -840,16 +1166,84 @@ calculation if supported by the system" ON) return _mm_clmulepi64_si128(a, b, 0); } int main(void) { return 0; } - " - HAVE_USABLE_CLMUL) + " + HAVE_USABLE_CLMUL) tuklib_add_definition_if(liblzma HAVE_USABLE_CLMUL) endif() endif() -# Support -fvisiblity=hidden when building shared liblzma. -# These lines do nothing on Windows (even under Cygwin). -# HAVE_VISIBILITY should always be defined to 0 or 1. -if(BUILD_SHARED_LIBS) +# ARM64 C Language Extensions define CRC32 functions in arm_acle.h. +# These are supported by at least GCC and Clang which both need +# __attribute__((__target__("+crc"))), unless the needed compiler flags +# are used to support the CRC instruction. +option(ALLOW_ARM64_CRC32 "Allow ARM64 CRC32 instruction if supported by \ +the system" ON) + +if(ALLOW_ARM64_CRC32) + check_c_source_compiles(" + #include <stdint.h> + + #ifndef _MSC_VER + #include <arm_acle.h> + #endif + + #if (defined(__GNUC__) || defined(__clang__)) && !defined(__EDG__) + __attribute__((__target__(\"+crc\"))) + #endif + uint32_t my_crc(uint32_t a, uint64_t b) + { + return __crc32d(a, b); + } + int main(void) { return 0; } + " + HAVE_ARM64_CRC32) + + if(HAVE_ARM64_CRC32) + target_compile_definitions(liblzma PRIVATE HAVE_ARM64_CRC32) + + # Check for ARM64 CRC32 instruction runtime detection. + # getauxval() is supported on Linux. + check_symbol_exists(getauxval sys/auxv.h HAVE_GETAUXVAL) + tuklib_add_definition_if(liblzma HAVE_GETAUXVAL) + + # elf_aux_info() is supported on FreeBSD. + check_symbol_exists(elf_aux_info sys/auxv.h HAVE_ELF_AUX_INFO) + tuklib_add_definition_if(liblzma HAVE_ELF_AUX_INFO) + + # sysctlbyname("hw.optional.armv8_crc32", ...) is supported on Darwin + # (macOS, iOS, etc.). Note that sysctlbyname() is supported on FreeBSD, + # NetBSD, and possibly others too but the string is specific to + # Apple OSes. The C code is responsible for checking + # defined(__APPLE__) before using + # sysctlbyname("hw.optional.armv8_crc32", ...). + check_symbol_exists(sysctlbyname sys/sysctl.h HAVE_SYSCTLBYNAME) + tuklib_add_definition_if(liblzma HAVE_SYSCTLBYNAME) + endif() +endif() + + +# Symbol visibility support: +# +# The C_VISIBILITY_PRESET property takes care of adding the compiler +# option -fvisibility=hidden (or equivalent) if and only if it is supported. +# +# HAVE_VISIBILITY should always be defined to 0 or 1. It tells liblzma +# if __attribute__((__visibility__("default"))) +# and __attribute__((__visibility__("hidden"))) are supported. +# Those are useful only when the compiler supports -fvisibility=hidden +# or such option so HAVE_VISIBILITY should be 1 only when both option and +# the attribute support are present. HAVE_VISIBILITY is ignored on Windows +# and Cygwin by the liblzma C code; __declspec(dllexport) is used instead. +# +# CMake's GenerateExportHeader module is too fancy since liblzma already +# has the necessary macros. Instead, check CMake's internal variable +# CMAKE_C_COMPILE_OPTIONS_VISIBILITY (it's the C-specific variant of +# CMAKE_<LANG>_COMPILE_OPTIONS_VISIBILITY) which contains the compiler +# command line option for visibility support. It's empty or unset when +# visibility isn't supported. (It was added to CMake 2.8.12 in the commit +# 0e9f4bc00c6b26f254e74063e4026ac33b786513 in 2013.) This way we don't +# set HAVE_VISIBILITY to 1 when visibility isn't actually supported. +if(BUILD_SHARED_LIBS AND CMAKE_C_COMPILE_OPTIONS_VISIBILITY) set_target_properties(liblzma PROPERTIES C_VISIBILITY_PRESET hidden) target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=1) else() @@ -891,8 +1285,7 @@ if(WIN32) # Disable __declspec(dllimport) when linking against static liblzma. target_compile_definitions(liblzma INTERFACE LZMA_API_STATIC) endif() -elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "Linux") - # GNU/Linux-specific symbol versioning for shared liblzma. +elseif(BUILD_SHARED_LIBS AND SYMBOL_VERSIONING STREQUAL "linux") # Note that adding link options doesn't affect static builds # but HAVE_SYMBOL_VERSIONS_LINUX must not be used with static builds # because it would put symbol versions into the static library which @@ -908,9 +1301,7 @@ elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "Linux") set_target_properties(liblzma PROPERTIES LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_linux.map" ) -elseif(BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL "FreeBSD") - # Symbol versioning for shared liblzma for non-GNU/Linux. - # FIXME? What about Solaris? +elseif(BUILD_SHARED_LIBS AND SYMBOL_VERSIONING STREQUAL "generic") target_link_options(liblzma PRIVATE "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/liblzma/liblzma_generic.map" ) @@ -975,9 +1366,6 @@ endif() file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake" "${LZMA_CONFIG_CONTENTS}") -# Set CMAKE_INSTALL_LIBDIR and friends. -include(GNUInstallDirs) - # Create liblzma.pc. set(prefix "${CMAKE_INSTALL_PREFIX}") set(exec_prefix "${CMAKE_INSTALL_PREFIX}") @@ -1031,19 +1419,204 @@ endif() ############################################################################# -# getopt_long +# Helper functions for installing files ############################################################################# -# The command line tools needs this. +# For each non-empty element in the list LINK_NAMES, creates symbolic links +# ${LINK_NAME}${LINK_SUFFIX} -> ${TARGET_NAME} in the directory ${DIR}. +# The target file should exist because on Cygwin and MSYS2 symlink creation +# can fail under certain conditions if the target doesn't exist. +function(my_install_symlinks COMPONENT DIR TARGET_NAME LINK_SUFFIX LINK_NAMES) + install(CODE "set(D \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${DIR}\") + foreach(L ${LINK_NAMES}) + file(CREATE_LINK \"${TARGET_NAME}\" + \"\${D}/\${L}${LINK_SUFFIX}\" + SYMBOLIC) + endforeach()" + COMPONENT "${COMPONENT}") +endfunction() + +# Installs a man page file of a given language ("" for the untranslated file) +# and optionally its alternative names as symlinks. This is a helper function +# for my_install_man() below. +function(my_install_man_lang COMPONENT SRC_FILE MAN_LANG LINK_NAMES) + # Get the man page section from the filename suffix. + string(REGEX REPLACE "^.*\.([^/.]+)$" "\\1" MAN_SECTION "${SRC_FILE}") + + # A few man pages might be missing from translations. + # Don't attempt to install them or create the related symlinks. + if(NOT MAN_LANG STREQUAL "" AND NOT EXISTS "${SRC_FILE}") + return() + endif() + + # Installing the file must be done before creating the symlinks + # due to Cygwin and MSYS2. + install(FILES "${SRC_FILE}" + DESTINATION "${CMAKE_INSTALL_MANDIR}/${MAN_LANG}/man${MAN_SECTION}" + COMPONENT "${COMPONENT}") + + # Get the basename of the file to be used as the symlink target. + get_filename_component(BASENAME "${SRC_FILE}" NAME) + + # LINK_NAMES don't contain the man page filename suffix (like ".1") + # so it needs to be told to my_install_symlinks. + my_install_symlinks("${COMPONENT}" + "${CMAKE_INSTALL_MANDIR}/${MAN_LANG}/man${MAN_SECTION}" + "${BASENAME}" ".${MAN_SECTION}" "${LINK_NAMES}") +endfunction() + +# Installs a man page file and optionally its alternative names as symlinks. +# Does the same for translations if ENABLE_NLS. +function(my_install_man COMPONENT SRC_FILE LINK_NAMES) + my_install_man_lang("${COMPONENT}" "${SRC_FILE}" "" "${LINK_NAMES}") + + if(ENABLE_NLS) + # Find the translated versions of this man page. + get_filename_component(BASENAME "${SRC_FILE}" NAME) + file(GLOB MAN_FILES "po4a/man/*/${BASENAME}") + + foreach(F ${MAN_FILES}) + get_filename_component(MAN_LANG "${F}" DIRECTORY) + get_filename_component(MAN_LANG "${MAN_LANG}" NAME) + my_install_man_lang("${COMPONENT}" "${F}" "${MAN_LANG}" + "${LINK_NAMES}") + endforeach() + endif() +endfunction() + + +############################################################################# +# libgnu (getopt_long) +############################################################################# + +# This mirrors how the Autotools build system handles the getopt_long +# replacement, calling the object library libgnu since the replacement +# version comes from Gnulib. +add_library(libgnu OBJECT) + +# CMake requires that even an object library must have at least once source +# file. So we give it a header file that results in no output files. +# +# NOTE: Using a file outside the lib directory makes it possible to +# delete lib/*.h and lib/*.c and still keep the build working if +# getopt_long replacement isn't needed. It's convenient if one wishes +# to be certain that no GNU LGPL code gets included in the binaries. +target_sources(libgnu PRIVATE src/common/sysdefs.h) + +# The Ninja Generator requires setting the linker language since it cannot +# guess the programming language of just a header file. Setting this +# property avoids needing an empty .c file or an non-empty unnecessary .c +# file. +set_target_properties(libgnu PROPERTIES LINKER_LANGUAGE C) + +# Create /lib directory in the build directory and add it to the include path. +file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib") +target_include_directories(libgnu PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/lib") + +# Include /lib from the source directory. It does no harm even if none of +# the Gnulib replacements are used. +target_include_directories(libgnu PUBLIC lib) + +# The command line tools need getopt_long in order to parse arguments. If +# the system does not have a getopt_long implementation we can use the one +# from Gnulib instead. check_symbol_exists(getopt_long getopt.h HAVE_GETOPT_LONG) +if(NOT HAVE_GETOPT_LONG) + # Set the __GETOPT_PREFIX definition to "rpl_" (replacement) to avoid + # name conflicts with libc symbols. The same prefix is set if using + # the Autotools build (m4/getopt.m4). + target_compile_definitions(libgnu PUBLIC "__GETOPT_PREFIX=rpl_") + + # Create a custom copy command to copy the getopt header to the build + # directory and re-copy it if it is updated. (Gnulib does it this way + # because it allows choosing which .in.h files to actually use in the + # build. We need just getopt.h so this is a bit overcomplicated for + # a single header file only.) + add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/lib/getopt.h" + COMMAND "${CMAKE_COMMAND}" -E copy + "${CMAKE_CURRENT_SOURCE_DIR}/lib/getopt.in.h" + "${CMAKE_CURRENT_BINARY_DIR}/lib/getopt.h" + MAIN_DEPENDENCY "${CMAKE_CURRENT_SOURCE_DIR}/lib/getopt.in.h" + VERBATIM) + + target_sources(libgnu PRIVATE + lib/getopt1.c + lib/getopt.c + lib/getopt_int.h + lib/getopt-cdefs.h + lib/getopt-core.h + lib/getopt-ext.h + lib/getopt-pfx-core.h + lib/getopt-pfx-ext.h + "${CMAKE_CURRENT_BINARY_DIR}/lib/getopt.h" + ) +endif() + + +############################################################################# +# xzdec and lzmadec +############################################################################# + +if(HAVE_DECODERS AND (NOT MSVC OR MSVC_VERSION GREATER_EQUAL 1900)) + foreach(XZDEC xzdec lzmadec) + add_executable("${XZDEC}" + src/common/sysdefs.h + src/common/tuklib_common.h + src/common/tuklib_config.h + src/common/tuklib_exit.c + src/common/tuklib_exit.h + src/common/tuklib_gettext.h + src/common/tuklib_progname.c + src/common/tuklib_progname.h + src/xzdec/xzdec.c + ) + + target_include_directories("${XZDEC}" PRIVATE + src/common + src/liblzma/api + ) + + target_link_libraries("${XZDEC}" PRIVATE liblzma libgnu) + + if(WIN32) + # Add the Windows resource file for xzdec.exe or lzmadec.exe. + target_sources("${XZDEC}" PRIVATE src/xzdec/xzdec_w32res.rc) + set_target_properties("${XZDEC}" PROPERTIES + LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc" + ) + endif() + + if(SANDBOX_COMPILE_DEFINITION) + target_compile_definitions("${XZDEC}" PRIVATE + "${SANDBOX_COMPILE_DEFINITION}") + endif() + + tuklib_progname("${XZDEC}") + + install(TARGETS "${XZDEC}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + COMPONENT "${XZDEC}_Runtime") + endforeach() + + # This is the only build-time difference with lzmadec. + target_compile_definitions(lzmadec PRIVATE "LZMADEC") + + if(UNIX) + # NOTE: This puts the lzmadec.1 symlinks into xzdec_Documentation. + # This isn't great but doing them separately with translated + # man pages would require extra code. So this has to suffice for now. + my_install_man(xzdec_Documentation src/xzdec/xzdec.1 lzmadec) + endif() +endif() + ############################################################################# -# xzdec +# lzmainfo ############################################################################# -if(HAVE_GETOPT_LONG AND HAVE_DECODERS) - add_executable(xzdec +if(HAVE_DECODERS AND (NOT MSVC OR MSVC_VERSION GREATER_EQUAL 1900)) + add_executable(lzmainfo src/common/sysdefs.h src/common/tuklib_common.h src/common/tuklib_config.h @@ -1052,34 +1625,44 @@ if(HAVE_GETOPT_LONG AND HAVE_DECODERS) src/common/tuklib_gettext.h src/common/tuklib_progname.c src/common/tuklib_progname.h - src/xzdec/xzdec.c + src/lzmainfo/lzmainfo.c ) - target_include_directories(xzdec PRIVATE + target_include_directories(lzmainfo PRIVATE src/common src/liblzma/api ) - target_link_libraries(xzdec PRIVATE liblzma) + target_link_libraries(lzmainfo PRIVATE liblzma libgnu) if(WIN32) - # Add the Windows resource file for xzdec.exe. - target_sources(xzdec PRIVATE src/xzdec/xzdec_w32res.rc) - set_target_properties(xzdec PROPERTIES + # Add the Windows resource file for lzmainfo.exe. + target_sources(lzmainfo PRIVATE src/lzmainfo/lzmainfo_w32res.rc) + set_target_properties(lzmainfo PROPERTIES LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/common/common_w32res.rc" ) endif() - tuklib_progname(xzdec) + tuklib_progname(lzmainfo) + + # NOTE: The translations are in the "xz" domain and the .mo files are + # installed as part of the "xz" target. + if(ENABLE_NLS) + target_link_libraries(lzmainfo PRIVATE Intl::Intl) - install(TARGETS xzdec + target_compile_definitions(lzmainfo PRIVATE + ENABLE_NLS + PACKAGE="${TRANSLATION_DOMAIN}" + LOCALEDIR="${LOCALEDIR_DEFINITION}" + ) + endif() + + install(TARGETS lzmainfo RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" - COMPONENT xzdec) + COMPONENT lzmainfo_Runtime) if(UNIX) - install(FILES src/xzdec/xzdec.1 - DESTINATION "${CMAKE_INSTALL_MANDIR}/man1" - COMPONENT xzdec) + my_install_man(lzmainfo_Documentation src/lzmainfo/lzmainfo.1 "") endif() endif() @@ -1088,7 +1671,7 @@ endif() # xz ############################################################################# -if(NOT MSVC AND HAVE_GETOPT_LONG) +if(NOT MSVC OR MSVC_VERSION GREATER_EQUAL 1900) add_executable(xz src/common/mythread.h src/common/sysdefs.h @@ -1122,6 +1705,8 @@ if(NOT MSVC AND HAVE_GETOPT_LONG) src/xz/options.c src/xz/options.h src/xz/private.h + src/xz/sandbox.c + src/xz/sandbox.h src/xz/signals.c src/xz/signals.h src/xz/suffix.c @@ -1142,7 +1727,7 @@ if(NOT MSVC AND HAVE_GETOPT_LONG) ) endif() - target_link_libraries(xz PRIVATE liblzma) + target_link_libraries(xz PRIVATE liblzma libgnu) target_compile_definitions(xz PRIVATE ASSUME_RAM=128) @@ -1154,6 +1739,10 @@ if(NOT MSVC AND HAVE_GETOPT_LONG) ) endif() + if(SANDBOX_COMPILE_DEFINITION) + target_compile_definitions(xz PRIVATE "${SANDBOX_COMPILE_DEFINITION}") + endif() + tuklib_progname(xz) tuklib_mbstr(xz) @@ -1212,15 +1801,76 @@ if(NOT MSVC AND HAVE_GETOPT_LONG) endif() endif() + if(ENABLE_NLS) + target_link_libraries(xz PRIVATE Intl::Intl) + + target_compile_definitions(xz PRIVATE + ENABLE_NLS + PACKAGE="${TRANSLATION_DOMAIN}" + LOCALEDIR="${LOCALEDIR_DEFINITION}" + ) + + file(STRINGS po/LINGUAS LINGUAS) + + # Where to find .gmo files. If msgfmt is available, the .po files + # will be converted as part of the build. Otherwise we will use + # the pre-generated .gmo files which are included in XZ Utils + # tarballs by Autotools. + set(GMO_DIR "${CMAKE_CURRENT_SOURCE_DIR}/po") + + if(GETTEXT_FOUND) + # NOTE: gettext_process_po_files' INSTALL_DESTINATION is + # incompatible with how Autotools requires the .po files to + # be named. CMake would require each .po file to be named with + # the translation domain and thus each .po file would need its + # own language-specific directory (like "po/fi/xz.po"). On top + # of this, INSTALL_DESTINATION doesn't allow specifying COMPONENT + # and thus the .mo files go into "Unspecified" component. So we + # can use gettext_process_po_files to convert the .po files but + # installation needs to be done with our own code. + # + # Also, the .gmo files will go to root of the build directory + # instead of neatly into a subdirectory. This is hardcoded in + # CMake's FindGettext.cmake. + foreach(LANG IN LISTS LINGUAS) + gettext_process_po_files("${LANG}" ALL + PO_FILES "${CMAKE_CURRENT_SOURCE_DIR}/po/${LANG}.po") + endforeach() + + set(GMO_DIR "${CMAKE_CURRENT_BINARY_DIR}") + endif() + + foreach(LANG IN LISTS LINGUAS) + install( + FILES "${GMO_DIR}/${LANG}.gmo" + DESTINATION "${CMAKE_INSTALL_LOCALEDIR}/${LANG}/LC_MESSAGES" + RENAME "${TRANSLATION_DOMAIN}.mo" + COMPONENT xz_Runtime) + endforeach() + endif() + + # This command must be before the symlink creation to keep things working + # on Cygwin and MSYS2 in all cases. + # + # - Cygwin can encode symlinks in multiple ways. This can be + # controlled via the environment variable "CYGWIN". If it contains + # "winsymlinks:nativestrict" then symlink creation will fail if + # the link target doesn't exist. This mode isn't the default though. + # See: https://cygwin.com/faq.html#faq.api.symlinks + # + # - MSYS2 supports the same winsymlinks option in the environment + # variable "MSYS" (not "MSYS2). The default in MSYS2 is to make + # a copy of the file instead of any kind of symlink. Thus the link + # target must exist or the creation of the "symlink" (copy) will fail. + # + # Our installation order must be such that when a symbolic link is created + # its target must already exists. There is no race condition for parallel + # builds because the generated cmake_install.cmake executes serially. install(TARGETS xz RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" - COMPONENT xz) + COMPONENT xz_Runtime) if(UNIX) - install(FILES src/xz/xz.1 - DESTINATION "${CMAKE_INSTALL_MANDIR}/man1" - COMPONENT xz) - option(CREATE_XZ_SYMLINKS "Create unxz and xzcat symlinks" ON) option(CREATE_LZMA_SYMLINKS "Create lzma, unlzma, and lzcat symlinks" ON) @@ -1234,150 +1884,165 @@ if(NOT MSVC AND HAVE_GETOPT_LONG) list(APPEND XZ_LINKS "lzma" "unlzma" "lzcat") endif() - # With Windows Cygwin and MSYS2 the symlinking is complicated. Both - # of these environments set the UNIX variable so they will try to - # make the symlinks. The ability for Cygwin and MSYS2 to make - # broken symlinks is determined by the CYGWIN and MSYS2 environment - # variables, respectively. Broken symlinks are needed for the man - # page symlinks and for determining if the xz and lzma symlinks need - # to depend on the xz target or not. If broken symlinks cannot be - # made then the xz binary must be created before the symlinks. - set(ALLOW_BROKEN_SYMLINKS ON) - - if(CMAKE_SYSTEM_NAME STREQUAL "CYGWIN") - # The Cygwin env variable can be set to four possible values: - # - # 1. "lnk". Create symlinks as Windows shortcuts. - # - # 2. "native". Create symlinks as native Windows symlinks - # if supported by the system. Fallback to "lnk" if native - # symlinks are not supported. - # - # 3. "nativestrict". Create symlinks as native Windows symlinks - # if supported by the system. If the target of the symlink - # does not exist or the creation of the symlink fails for any - # reason, do not create the symlink. - # - # 4. "sys". Create symlinks as plain files with a special - # system attribute containing the path to the symlink target. - # - # So, the only case we care about for broken symlinks is - # "nativestrict" since all other values mean that broken - # symlinks are allowed. If the env variable is not set the - # default is "native". If the env variable is set but not - # assigned one of the four values, then the default is the same - # as option 1 "lnk". - string(FIND "$ENV{CYGWIN}" "winsymlinks:nativestrict" SYMLINK_POS) - if(SYMLINK_POS GREATER -1) - set(ALLOW_BROKEN_SYMLINKS OFF) - endif() - elseif(CMAKE_SYSTEM_NAME STREQUAL "MSYS") - # The MSYS env variable behaves similar to the CYGWIN but has a - # different default behavior. If winsymlinks is set but not - # assigned one of the four supported values, the default is to - # *copy* the target to the symlink destination. This will fail - # if the target does not exist so broken symlinks cannot be - # allowed. - string(FIND "$ENV{MSYS}" "winsymlinks" SYMLINK_POS) - if(SYMLINK_POS GREATER -1) - string(FIND "$ENV{MSYS}" "winsymlinks:nativestrict" - SYMLINK_POS) - if(SYMLINK_POS GREATER -1) - set(ALLOW_BROKEN_SYMLINKS OFF) - endif() - else() - set(ALLOW_BROKEN_SYMLINKS OFF) - endif() - endif() - - # Create symlinks in the build directory and then install them. + # On Cygwin, don't add the .exe suffix to the symlinks. # - # The symlinks do not likely need any special extension since - # even on Windows the symlink can still be executed without - # the .exe extension. - foreach(LINK IN LISTS XZ_LINKS) - add_custom_target("create_${LINK}" ALL - "${CMAKE_COMMAND}" -E create_symlink - "$<TARGET_FILE_NAME:xz>" "${LINK}" - BYPRODUCTS "${LINK}" - VERBATIM) - install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}" - DESTINATION "${CMAKE_INSTALL_BINDIR}" - COMPONENT xz) - - # Only create the man page symlinks if the symlinks can be - # created broken. The symlinks will not be valid until install - # so they cannot be created on these system environments. - if(ALLOW_BROKEN_SYMLINKS) - add_custom_target("create_${LINK}.1" ALL - "${CMAKE_COMMAND}" -E create_symlink "xz.1" "${LINK}.1" - BYPRODUCTS "${LINK}.1" - VERBATIM) - install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINK}.1" - DESTINATION "${CMAKE_INSTALL_MANDIR}/man1" - COMPONENT xz) - else() - # Add the xz target as dependency when broken symlinks - # cannot be made. This ensures parallel builds do not fail - # since it will enforce the order of creating xz first, then - # the symlinks. - add_dependencies("create_${LINK}" xz) - endif() - endforeach() + # FIXME? Does this make sense on MSYS & MSYS2 where "ln -s" + # by default makes copies? Inside MSYS & MSYS2 it is possible + # to execute files without the .exe suffix but not outside + # (like in Command Prompt). Omitting the suffix matches + # what configure.ac has done for many years though. + my_install_symlinks(xz_Runtime "${CMAKE_INSTALL_BINDIR}" + "xz${CMAKE_EXECUTABLE_SUFFIX}" "" "${XZ_LINKS}") + + # Install the man pages and (optionally) their symlinks + # and translations. + my_install_man(xz_Documentation src/xz/xz.1 "${XZ_LINKS}") endif() endif() ############################################################################# -# Tests +# Scripts ############################################################################# -include(CTest) - -if(BUILD_TESTING) - set(LIBLZMA_TESTS - test_bcj_exact_size - test_block_header - test_check - test_filter_flags - test_filter_str - test_hardware - test_index - test_index_hash - test_lzip_decoder - test_memlimit - test_stream_flags - test_vli - ) +if(UNIX) + # NOTE: This isn't as sophisticated as in the Autotools build which + # uses posix-shell.m4 but hopefully this doesn't need to be either. + # CMake likely won't be used on as many (old) obscure systems as the + # Autotools-based builds are. + if(CMAKE_SYSTEM_NAME STREQUAL "SunOS" AND EXISTS "/usr/xpg4/bin/sh") + set(POSIX_SHELL_DEFAULT "/usr/xpg4/bin/sh") + else() + set(POSIX_SHELL_DEFAULT "/bin/sh") + endif() - foreach(TEST IN LISTS LIBLZMA_TESTS) - add_executable("${TEST}" "tests/${TEST}.c") + set(POSIX_SHELL "${POSIX_SHELL_DEFAULT}" CACHE STRING + "Shell to use for scripts (xzgrep and others)") - target_include_directories("${TEST}" PRIVATE - src/common - src/liblzma/api - src/liblzma - ) + # Guess the extra path to add from POSIX_SHELL. Autotools-based build + # has a separate option --enable-path-for-scripts=PREFIX but this is + # enough for Solaris. + set(enable_path_for_scripts) + get_filename_component(POSIX_SHELL_DIR "${POSIX_SHELL}" DIRECTORY) - target_link_libraries("${TEST}" PRIVATE liblzma) + if(NOT POSIX_SHELL_DIR STREQUAL "/bin" AND + NOT POSIX_SHELL_DIR STREQUAL "/usr/bin") + set(enable_path_for_scripts "PATH=${POSIX_SHELL_DIR}:\$PATH") + endif() - # Put the test programs into their own subdirectory so they don't - # pollute the top-level dir which might contain xz and xzdec. - set_target_properties("${TEST}" PROPERTIES - RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tests_bin" - ) + set(XZDIFF_LINKS xzcmp) + set(XZGREP_LINKS xzegrep xzfgrep) + set(XZMORE_LINKS) + set(XZLESS_LINKS) + + if(CREATE_LZMA_SYMLINKS) + list(APPEND XZDIFF_LINKS lzdiff lzcmp) + list(APPEND XZGREP_LINKS lzgrep lzegrep lzfgrep) + list(APPEND XZMORE_LINKS lzmore) + list(APPEND XZLESS_LINKS lzless) + endif() - add_test(NAME "${TEST}" - COMMAND "${CMAKE_CURRENT_BINARY_DIR}/tests_bin/${TEST}" + set(xz "xz") + + foreach(S xzdiff xzgrep xzmore xzless) + configure_file("src/scripts/${S}.in" "${S}" + @ONLY + NEWLINE_STYLE LF) + + install(PROGRAMS "${CMAKE_CURRENT_BINARY_DIR}/${S}" + DESTINATION "${CMAKE_INSTALL_BINDIR}" + COMPONENT scripts_Runtime) + endforeach() + + # file(CHMOD ...) would need CMake 3.19 so use execute_process instead. + # Using +x is fine even if umask was 077. If execute bit is set at all + # then "make install" will set it for group and other access bits too. + execute_process(COMMAND chmod +x xzdiff xzgrep xzmore xzless + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") + + unset(xz) + unset(POSIX_SHELL) + unset(enable_path_for_scripts) + + my_install_symlinks(scripts_Runtime "${CMAKE_INSTALL_BINDIR}" xzdiff "" + "${XZDIFF_LINKS}") + + my_install_symlinks(scripts_Runtime "${CMAKE_INSTALL_BINDIR}" xzgrep "" + "${XZGREP_LINKS}") + + my_install_symlinks(scripts_Runtime "${CMAKE_INSTALL_BINDIR}" xzmore "" + "${XZMORE_LINKS}") + + my_install_symlinks(scripts_Runtime "${CMAKE_INSTALL_BINDIR}" xzless "" + "${XZLESS_LINKS}") + + my_install_man(scripts_Documentation src/scripts/xzdiff.1 "${XZDIFF_LINKS}") + my_install_man(scripts_Documentation src/scripts/xzgrep.1 "${XZGREP_LINKS}") + my_install_man(scripts_Documentation src/scripts/xzmore.1 "${XZMORE_LINKS}") + my_install_man(scripts_Documentation src/scripts/xzless.1 "${XZLESS_LINKS}") +endif() + + +############################################################################# +# Documentation +############################################################################# + +if(UNIX) + option(ENABLE_DOXYGEN "Use Doxygen to generate liblzma API docs" OFF) + + if (ENABLE_DOXYGEN) + file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/doc") + + add_custom_command( + VERBATIM + COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/doxygen/update-doxygen" + ARGS "api" + "${CMAKE_CURRENT_SOURCE_DIR}" + "${CMAKE_CURRENT_BINARY_DIR}/doc" + OUTPUT doc/api/index.html + DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/doxygen/update-doxygen" + "${CMAKE_CURRENT_SOURCE_DIR}/doxygen/Doxyfile" + ${LIBLZMA_API_HEADERS} ) - # Set srcdir environment variable so that the tests find their - # input files from the source tree. - # - # Set the return code for skipped tests to match Automake convention. - set_tests_properties("${TEST}" PROPERTIES - ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests" - SKIP_RETURN_CODE 77 + add_custom_target( + liblzma-doc-api ALL + DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/doc/api/index.html" ) - endforeach() + + install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/doc/api" + DESTINATION "${CMAKE_INSTALL_DOCDIR}" + COMPONENT liblzma_Documentation) + endif() endif() + +install(DIRECTORY doc/examples + DESTINATION "${CMAKE_INSTALL_DOCDIR}" + COMPONENT liblzma_Documentation) + +# GPLv2 applies to the scripts. If GNU getopt_long is used then +# LGPLv2.1 applies to the command line tools but, using the +# section 3 of LGPLv2.1, GNU getopt_long can be handled as GPLv2 too. +# Thus GPLv2 should be enough here. +install(FILES AUTHORS + COPYING + COPYING.0BSD + COPYING.GPLv2 + NEWS + README + THANKS + doc/faq.txt + doc/history.txt + doc/lzma-file-format.txt + doc/xz-file-format.txt + DESTINATION "${CMAKE_INSTALL_DOCDIR}" + COMPONENT Documentation) + + +############################################################################# +# Tests +############################################################################# + +# Tests are in a separate file so that it's possible to delete the whole +# "tests" directory and still have a working build, just without the tests. +include(tests/tests.cmake OPTIONAL) |