Files
Aaru.Compression.Native/3rdparty/zstd.cmake

303 lines
11 KiB
CMake
Raw Normal View History

2021-10-25 02:39:06 +01:00
# ################################################################
2023-09-24 03:57:46 +01:00
# Copyright (c) Meta Platforms, Inc. and affiliates.
2021-10-25 02:39:06 +01:00
# All rights reserved.
#
# This source code is licensed under both the BSD-style license (found in the
# LICENSE file in the root directory of this source tree) and the GPLv2 (found
# in the COPYING file in the root directory of this source tree).
# ################################################################
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
2021-10-25 02:39:06 +01:00
# As of 2018-12-26 ZSTD has been validated to build with cmake version 3.13.2 new policies.
# Set and use the newest cmake policies that are validated to work
set(ZSTD_MAX_VALIDATED_CMAKE_MAJOR_VERSION "3")
set(ZSTD_MAX_VALIDATED_CMAKE_MINOR_VERSION "13") #Policies never changed at PATCH level
2023-09-24 17:20:53 +01:00
if("${CMAKE_MAJOR_VERSION}" LESS 3)
2024-04-30 15:37:29 +01:00
set(ZSTD_CMAKE_POLICY_VERSION "${CMAKE_VERSION}")
2023-09-24 17:20:53 +01:00
elseif("${ZSTD_MAX_VALIDATED_CMAKE_MAJOR_VERSION}" EQUAL "${CMAKE_MAJOR_VERSION}" AND
"${ZSTD_MAX_VALIDATED_CMAKE_MINOR_VERSION}" GREATER "${CMAKE_MINOR_VERSION}")
2024-04-30 15:37:29 +01:00
set(ZSTD_CMAKE_POLICY_VERSION "${CMAKE_VERSION}")
2023-09-24 17:20:53 +01:00
else()
2024-04-30 15:37:29 +01:00
set(ZSTD_CMAKE_POLICY_VERSION "${ZSTD_MAX_VALIDATED_CMAKE_MAJOR_VERSION}.${ZSTD_MAX_VALIDATED_CMAKE_MINOR_VERSION}.0")
2023-09-24 17:20:53 +01:00
endif()
2021-10-25 02:39:06 +01:00
cmake_policy(VERSION ${ZSTD_CMAKE_POLICY_VERSION})
2023-09-24 02:41:32 +01:00
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/zstd/build/cmake/CMakeModules")
set(ZSTD_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/zstd")
2021-10-25 02:39:06 +01:00
set(LIBRARY_DIR ${ZSTD_SOURCE_DIR}/lib)
# Parse version
include(GetZstdLibraryVersion)
2023-09-24 17:20:53 +01:00
getzstdlibraryversion(${LIBRARY_DIR}/zstd.h zstd_VERSION_MAJOR zstd_VERSION_MINOR zstd_VERSION_PATCH)
2021-10-25 02:39:06 +01:00
2023-09-24 17:20:53 +01:00
if(CMAKE_MAJOR_VERSION LESS 3)
2024-04-30 15:37:29 +01:00
## Provide cmake 3+ behavior for older versions of cmake
project(zstd)
set(PROJECT_VERSION_MAJOR ${zstd_VERSION_MAJOR})
set(PROJECT_VERSION_MINOR ${zstd_VERSION_MINOR})
set(PROJECT_VERSION_PATCH ${zstd_VERSION_PATCH})
set(PROJECT_VERSION "${zstd_VERSION_MAJOR}.${zstd_VERSION_MINOR}.${zstd_VERSION_PATCH}")
enable_language(C) # Main library is in C
enable_language(ASM) # And ASM
enable_language(CXX) # Testing contributed code also utilizes CXX
2023-09-24 17:20:53 +01:00
else()
2024-04-30 15:37:29 +01:00
project(zstd
VERSION "${zstd_VERSION_MAJOR}.${zstd_VERSION_MINOR}.${zstd_VERSION_PATCH}"
LANGUAGES C # Main library is in C
ASM # And ASM
CXX # Testing contributed code also utilizes CXX
)
2023-09-24 17:20:53 +01:00
endif()
2021-10-25 02:39:06 +01:00
message(STATUS "ZSTD VERSION: ${zstd_VERSION}")
2023-09-24 03:57:46 +01:00
set(zstd_HOMEPAGE_URL "https://facebook.github.io/zstd/")
set(zstd_DESCRIPTION "Zstandard is a real-time compression algorithm, providing high compression ratios.")
2021-10-25 02:39:06 +01:00
# Set a default build type if none was specified
2023-09-24 17:20:53 +01:00
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
2024-04-30 15:37:29 +01:00
message(STATUS "Setting build type to 'Release' as none was specified.")
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
2023-09-24 17:20:53 +01:00
endif()
2021-10-25 02:39:06 +01:00
#-----------------------------------------------------------------------------
# Add extra compilation flags
#-----------------------------------------------------------------------------
#include(AddZstdCompilationFlags)
include(CheckCXXCompilerFlag)
include(CheckCCompilerFlag)
2023-09-24 17:20:53 +01:00
function(enablecompilerflag _flag _C _CXX)
2024-04-30 15:37:29 +01:00
string(REGEX REPLACE "\\+" "PLUS" varname "${_flag}")
string(REGEX REPLACE "[^A-Za-z0-9]+" "_" varname "${varname}")
string(REGEX REPLACE "^_+" "" varname "${varname}")
string(TOUPPER "${varname}" varname)
if(_C)
check_c_compiler_flag(${_flag} C_FLAG_${varname})
if(C_FLAG_${varname})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_flag}" PARENT_SCOPE)
2023-09-24 17:20:53 +01:00
endif()
2024-04-30 15:37:29 +01:00
endif()
if(_CXX)
check_cxx_compiler_flag(${_flag} CXX_FLAG_${varname})
if(CXX_FLAG_${varname})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_flag}" PARENT_SCOPE)
2023-09-24 17:20:53 +01:00
endif()
2024-04-30 15:37:29 +01:00
endif()
2021-10-25 02:39:06 +01:00
endfunction()
2023-09-24 17:20:53 +01:00
macro(add_zstd_compilation_flags)
2024-04-30 15:37:29 +01:00
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang" OR MINGW) #Not only UNIX but also WIN32 for MinGW
#Set c++11 by default
enablecompilerflag("-std=c++11" false true)
#Set c99 by default
enablecompilerflag("-std=c99" true false)
# if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND MSVC)
# # clang-cl normally maps -Wall to -Weverything.
# EnableCompilerFlag("/clang:-Wall" true true)
# else ()
# EnableCompilerFlag("-Wall" true true)
# endif ()
# EnableCompilerFlag("-Wextra" true true)
# EnableCompilerFlag("-Wundef" true true)
# EnableCompilerFlag("-Wshadow" true true)
# EnableCompilerFlag("-Wcast-align" true true)
# EnableCompilerFlag("-Wcast-qual" true true)
# EnableCompilerFlag("-Wstrict-prototypes" true false)
# Enable asserts in Debug mode
if(CMAKE_BUILD_TYPE MATCHES "Debug")
enablecompilerflag("-DDEBUGLEVEL=1" true true)
2023-09-24 17:20:53 +01:00
endif()
2024-04-30 15:37:29 +01:00
elseif(MSVC) # Add specific compilation flags for Windows Visual
2021-10-25 02:39:06 +01:00
2024-04-30 15:37:29 +01:00
set(ACTIVATE_MULTITHREADED_COMPILATION "ON" CACHE BOOL "activate multi-threaded compilation (/MP flag)")
if(CMAKE_GENERATOR MATCHES "Visual Studio" AND ACTIVATE_MULTITHREADED_COMPILATION)
enablecompilerflag("/MP" true true)
endif()
# UNICODE SUPPORT
enablecompilerflag("/D_UNICODE" true true)
enablecompilerflag("/DUNICODE" true true)
# Enable asserts in Debug mode
if(CMAKE_BUILD_TYPE MATCHES "Debug")
enablecompilerflag("/DDEBUGLEVEL=1" true true)
endif()
endif()
# Remove duplicates compilation flags
foreach(flag_var CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
if(${flag_var})
separate_arguments(${flag_var})
string(REPLACE ";" " " ${flag_var} "${${flag_var}}")
endif()
endforeach()
if(MSVC AND ZSTD_USE_STATIC_RUNTIME)
2023-09-24 17:20:53 +01:00
foreach(flag_var CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
2023-09-24 03:57:46 +01:00
CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
2024-04-30 15:37:29 +01:00
if(${flag_var})
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
endif()
2023-09-24 17:20:53 +01:00
endforeach()
2024-04-30 15:37:29 +01:00
endif()
2021-10-25 02:39:06 +01:00
endmacro()
2023-09-24 17:20:53 +01:00
add_zstd_compilation_flags()
2021-10-25 02:39:06 +01:00
# Always hide XXHash symbols
add_definitions(-DXXH_NAMESPACE=ZSTD_)
#-----------------------------------------------------------------------------
# Installation variables
#-----------------------------------------------------------------------------
#message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")
#message(STATUS "CMAKE_INSTALL_LIBDIR: ${CMAKE_INSTALL_LIBDIR}")
#-----------------------------------------------------------------------------
# Options
#-----------------------------------------------------------------------------
# Legacy support
2021-10-31 03:06:28 +00:00
option(ZSTD_LEGACY_SUPPORT "LEGACY SUPPORT" OFF)
2021-10-25 02:39:06 +01:00
2023-09-24 17:20:53 +01:00
if(ZSTD_LEGACY_SUPPORT)
2024-04-30 15:37:29 +01:00
message(STATUS "ZSTD_LEGACY_SUPPORT defined!")
add_definitions(-DZSTD_LEGACY_SUPPORT=5)
2023-09-24 17:20:53 +01:00
else()
2024-04-30 15:37:29 +01:00
message(STATUS "ZSTD_LEGACY_SUPPORT not defined!")
add_definitions(-DZSTD_LEGACY_SUPPORT=0)
2023-09-24 17:20:53 +01:00
endif()
2021-10-25 02:39:06 +01:00
# Multi-threading support
2023-09-24 17:20:53 +01:00
if(ANDROID)
2024-04-30 15:37:29 +01:00
option(ZSTD_MULTITHREAD_SUPPORT "MULTITHREADING SUPPORT" OFF)
2023-09-24 17:20:53 +01:00
else()
2024-04-30 15:37:29 +01:00
option(ZSTD_MULTITHREAD_SUPPORT "MULTITHREADING SUPPORT" ON)
2023-09-24 17:20:53 +01:00
endif()
2021-10-25 02:39:06 +01:00
#-----------------------------------------------------------------------------
# External dependencies
#-----------------------------------------------------------------------------
2023-09-24 17:20:53 +01:00
if(ZSTD_MULTITHREAD_SUPPORT AND UNIX)
2024-04-30 15:37:29 +01:00
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
if(CMAKE_USE_PTHREADS_INIT)
set(THREADS_LIBS "${CMAKE_THREAD_LIBS_INIT}")
else()
message(SEND_ERROR "ZSTD currently does not support thread libraries other than pthreads")
endif()
2023-09-24 17:20:53 +01:00
endif()
2021-10-25 02:39:06 +01:00
project(libzstd C)
set(CMAKE_INCLUDE_CURRENT_DIR TRUE)
# Define library directory, where sources and header files are located
include_directories(${LIBRARY_DIR} ${LIBRARY_DIR}/common)
file(GLOB CommonSources ${LIBRARY_DIR}/common/*.c)
file(GLOB CompressSources ${LIBRARY_DIR}/compress/*.c)
file(GLOB DecompressSources ${LIBRARY_DIR}/decompress/*.c)
2023-09-24 03:57:46 +01:00
file(GLOB DecompressAsmSources ${LIBRARY_DIR}/decompress/*.S)
2021-10-25 02:39:06 +01:00
file(GLOB DictBuilderSources ${LIBRARY_DIR}/dictBuilder/*.c)
set(Sources
2023-09-24 17:20:53 +01:00
${CommonSources}
${CompressSources}
${DecompressSources}
${DecompressAsmSources}
${DictBuilderSources})
2021-10-25 02:39:06 +01:00
file(GLOB CommonHeaders ${LIBRARY_DIR}/common/*.h)
file(GLOB CompressHeaders ${LIBRARY_DIR}/compress/*.h)
file(GLOB DecompressHeaders ${LIBRARY_DIR}/decompress/*.h)
file(GLOB DictBuilderHeaders ${LIBRARY_DIR}/dictBuilder/*.h)
set(Headers
2023-09-24 17:20:53 +01:00
${LIBRARY_DIR}/zstd.h
${CommonHeaders}
${CompressHeaders}
${DecompressHeaders}
${DictBuilderHeaders})
2021-10-25 02:39:06 +01:00
2023-09-24 17:20:53 +01:00
if(ZSTD_LEGACY_SUPPORT)
2024-04-30 15:37:29 +01:00
set(LIBRARY_LEGACY_DIR ${LIBRARY_DIR}/legacy)
include_directories(${LIBRARY_LEGACY_DIR})
set(Sources ${Sources}
${LIBRARY_LEGACY_DIR}/zstd_v01.c
${LIBRARY_LEGACY_DIR}/zstd_v02.c
${LIBRARY_LEGACY_DIR}/zstd_v03.c
${LIBRARY_LEGACY_DIR}/zstd_v04.c
${LIBRARY_LEGACY_DIR}/zstd_v05.c
${LIBRARY_LEGACY_DIR}/zstd_v06.c
${LIBRARY_LEGACY_DIR}/zstd_v07.c)
set(Headers ${Headers}
${LIBRARY_LEGACY_DIR}/zstd_legacy.h
${LIBRARY_LEGACY_DIR}/zstd_v01.h
${LIBRARY_LEGACY_DIR}/zstd_v02.h
${LIBRARY_LEGACY_DIR}/zstd_v03.h
${LIBRARY_LEGACY_DIR}/zstd_v04.h
${LIBRARY_LEGACY_DIR}/zstd_v05.h
${LIBRARY_LEGACY_DIR}/zstd_v06.h
${LIBRARY_LEGACY_DIR}/zstd_v07.h)
2023-09-24 17:20:53 +01:00
endif()
if(MSVC)
2024-04-30 15:37:29 +01:00
set(MSVC_RESOURCE_DIR ${ZSTD_SOURCE_DIR}/build/VS2010/libzstd-dll)
set(PlatformDependResources ${MSVC_RESOURCE_DIR}/libzstd-dll.rc)
2023-09-24 17:20:53 +01:00
endif()
2021-10-25 02:39:06 +01:00
2023-09-24 03:57:46 +01:00
# Explicitly set the language to C for all files, including ASM files.
# Our assembly expects to be compiled by a C compiler, and is only enabled for
# __GNUC__ compatible compilers. Otherwise all the ASM code is disabled by
# macros.
set_source_files_properties(${Sources} PROPERTIES LANGUAGE C)
2021-10-25 02:39:06 +01:00
# Split project to static and shared libraries build
set(library_targets)
2021-10-25 05:46:56 +01:00
#if (ZSTD_BUILD_STATIC)
2023-09-24 03:57:46 +01:00
add_library(libzstd_static STATIC ${Sources} ${Headers})
list(APPEND library_targets libzstd_static)
2023-09-24 17:20:53 +01:00
if(ZSTD_MULTITHREAD_SUPPORT)
2024-04-30 15:37:29 +01:00
set_property(TARGET libzstd_static APPEND PROPERTY COMPILE_DEFINITIONS "ZSTD_MULTITHREAD")
if(UNIX)
target_link_libraries(libzstd_static ${THREADS_LIBS})
endif()
2023-09-24 17:20:53 +01:00
endif()
2021-10-25 05:46:56 +01:00
#endif ()
2021-10-25 02:39:06 +01:00
# Add specific compile definitions for MSVC project
2023-09-24 17:20:53 +01:00
if(MSVC)
2024-04-30 15:37:29 +01:00
set_property(TARGET libzstd_static APPEND PROPERTY COMPILE_DEFINITIONS "ZSTD_HEAPMODE=0;_CRT_SECURE_NO_WARNINGS")
2023-09-24 17:20:53 +01:00
endif()
2021-10-25 02:39:06 +01:00
# With MSVC static library needs to be renamed to avoid conflict with import library
2023-09-24 17:20:53 +01:00
if(MSVC)
2024-04-30 15:37:29 +01:00
set(STATIC_LIBRARY_BASE_NAME zstd_static)
2023-09-24 17:20:53 +01:00
else()
2024-04-30 15:37:29 +01:00
set(STATIC_LIBRARY_BASE_NAME zstd)
2023-09-24 17:20:53 +01:00
endif()
2021-10-25 02:39:06 +01:00
# Define static and shared library names
#if (ZSTD_BUILD_STATIC)
2023-09-24 03:57:46 +01:00
set_target_properties(
libzstd_static
PROPERTIES
OUTPUT_NAME ${STATIC_LIBRARY_BASE_NAME})
2021-10-25 05:46:56 +01:00
#endif ()
2023-09-24 17:20:53 +01:00
if(NOT "${CMAKE_C_PLATFORM_ID}" MATCHES "MinGW" OR (NOT ${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" AND NOT ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64"))
2024-04-30 15:37:29 +01:00
set_property(TARGET libzstd_static PROPERTY POSITION_INDEPENDENT_CODE ON)
2023-09-24 17:20:53 +01:00
endif()