1# CMake support for LZ4 2# 3# To the extent possible under law, the author(s) have dedicated all 4# copyright and related and neighboring rights to this software to 5# the public domain worldwide. This software is distributed without 6# any warranty. 7# 8# For details, see <http://creativecommons.org/publicdomain/zero/1.0/>. 9# 10# LZ4's CMake support is maintained by Evan Nemerson; when filing 11# bugs please mention @nemequ to make sure I see it. 12 13cmake_minimum_required(VERSION 2.8.12) 14 15set(LZ4_TOP_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../..") 16 17# Parse version information 18file(STRINGS "${LZ4_TOP_SOURCE_DIR}/lib/lz4.h" LZ4_VERSION_MAJOR REGEX "^#define LZ4_VERSION_MAJOR +([0-9]+) +.*$") 19string(REGEX REPLACE "^#define LZ4_VERSION_MAJOR +([0-9]+) +.*$" "\\1" LZ4_VERSION_MAJOR "${LZ4_VERSION_MAJOR}") 20file(STRINGS "${LZ4_TOP_SOURCE_DIR}/lib/lz4.h" LZ4_VERSION_MINOR REGEX "^#define LZ4_VERSION_MINOR +([0-9]+) +.*$") 21string(REGEX REPLACE "^#define LZ4_VERSION_MINOR +([0-9]+) +.*$" "\\1" LZ4_VERSION_MINOR "${LZ4_VERSION_MINOR}") 22file(STRINGS "${LZ4_TOP_SOURCE_DIR}/lib/lz4.h" LZ4_VERSION_RELEASE REGEX "^#define LZ4_VERSION_RELEASE +([0-9]+) +.*$") 23string(REGEX REPLACE "^#define LZ4_VERSION_RELEASE +([0-9]+) +.*$" "\\1" LZ4_VERSION_RELEASE "${LZ4_VERSION_RELEASE}") 24set(LZ4_VERSION_STRING "${LZ4_VERSION_MAJOR}.${LZ4_VERSION_MINOR}.${LZ4_VERSION_RELEASE}") 25mark_as_advanced(LZ4_VERSION_STRING LZ4_VERSION_MAJOR LZ4_VERSION_MINOR LZ4_VERSION_RELEASE) 26 27if("${CMAKE_VERSION}" VERSION_LESS "3.0") 28 project(LZ4 C) 29else() 30 cmake_policy (SET CMP0048 NEW) 31 project(LZ4 32 VERSION ${LZ4_VERSION_STRING} 33 LANGUAGES C) 34endif() 35 36option(LZ4_BUILD_CLI "Build lz4 program" ON) 37option(LZ4_BUILD_LEGACY_LZ4C "Build lz4c program with legacy argument support" ON) 38 39# If LZ4 is being bundled in another project, we don't want to 40# install anything. However, we want to let people override this, so 41# we'll use the LZ4_BUNDLED_MODE variable to let them do that; just 42# set it to OFF in your project before you add_subdirectory(lz4/contrib/cmake_unofficial). 43get_directory_property(LZ4_PARENT_DIRECTORY PARENT_DIRECTORY) 44if("${LZ4_BUNDLED_MODE}" STREQUAL "") 45 # Bundled mode hasn't been set one way or the other, set the default 46 # depending on whether or not we are the top-level project. 47 if("${LZ4_PARENT_DIRECTORY}" STREQUAL "") 48 set(LZ4_BUNDLED_MODE OFF) 49 else() 50 set(LZ4_BUNDLED_MODE ON) 51 endif() 52endif() 53mark_as_advanced(LZ4_BUNDLED_MODE) 54 55# CPack 56if(NOT LZ4_BUNDLED_MODE AND NOT CPack_CMake_INCLUDED) 57 set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "LZ4 compression library") 58 set(CPACK_PACKAGE_DESCRIPTION_FILE "${LZ4_TOP_SOURCE_DIR}/README.md") 59 set(CPACK_RESOURCE_FILE_LICENSE "${LZ4_TOP_SOURCE_DIR}/LICENSE") 60 set(CPACK_PACKAGE_VERSION_MAJOR ${LZ4_VERSION_MAJOR}) 61 set(CPACK_PACKAGE_VERSION_MINOR ${LZ4_VERSION_MINOR}) 62 set(CPACK_PACKAGE_VERSION_PATCH ${LZ4_VERSION_RELEASE}) 63 include(CPack) 64endif(NOT LZ4_BUNDLED_MODE AND NOT CPack_CMake_INCLUDED) 65 66# Allow people to choose whether to build shared or static libraries 67# via the BUILD_SHARED_LIBS option unless we are in bundled mode, in 68# which case we always use static libraries. 69include(CMakeDependentOption) 70CMAKE_DEPENDENT_OPTION(BUILD_SHARED_LIBS "Build shared libraries" ON "NOT LZ4_BUNDLED_MODE" OFF) 71CMAKE_DEPENDENT_OPTION(BUILD_STATIC_LIBS "Build static libraries" OFF "BUILD_SHARED_LIBS" ON) 72 73if(NOT BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS) 74 message(FATAL_ERROR "Both BUILD_SHARED_LIBS and BUILD_STATIC_LIBS have been disabled") 75endif() 76 77set(LZ4_LIB_SOURCE_DIR "${LZ4_TOP_SOURCE_DIR}/lib") 78set(LZ4_PROG_SOURCE_DIR "${LZ4_TOP_SOURCE_DIR}/programs") 79 80include_directories("${LZ4_LIB_SOURCE_DIR}") 81 82# CLI sources 83set(LZ4_SOURCES 84 "${LZ4_LIB_SOURCE_DIR}/lz4.c" 85 "${LZ4_LIB_SOURCE_DIR}/lz4hc.c" 86 "${LZ4_LIB_SOURCE_DIR}/lz4.h" 87 "${LZ4_LIB_SOURCE_DIR}/lz4hc.h" 88 "${LZ4_LIB_SOURCE_DIR}/lz4frame.c" 89 "${LZ4_LIB_SOURCE_DIR}/lz4frame.h" 90 "${LZ4_LIB_SOURCE_DIR}/xxhash.c") 91set(LZ4_CLI_SOURCES 92 "${LZ4_PROG_SOURCE_DIR}/bench.c" 93 "${LZ4_PROG_SOURCE_DIR}/lz4cli.c" 94 "${LZ4_PROG_SOURCE_DIR}/lz4io.c" 95 "${LZ4_PROG_SOURCE_DIR}/datagen.c") 96 97# Whether to use position independent code for the static library. If 98# we're building a shared library this is ignored and PIC is always 99# used. 100option(LZ4_POSITION_INDEPENDENT_LIB "Use position independent code for static library (if applicable)" ON) 101 102# liblz4 103set(LZ4_LIBRARIES_BUILT) 104if(BUILD_SHARED_LIBS) 105 add_library(lz4_shared SHARED ${LZ4_SOURCES}) 106 target_include_directories(lz4_shared 107 PUBLIC $<BUILD_INTERFACE:${LZ4_LIB_SOURCE_DIR}> 108 INTERFACE $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>) 109 set_target_properties(lz4_shared PROPERTIES 110 OUTPUT_NAME lz4 111 SOVERSION "${LZ4_VERSION_MAJOR}" 112 VERSION "${LZ4_VERSION_STRING}") 113 if(MSVC) 114 target_compile_definitions(lz4_shared PRIVATE 115 LZ4_DLL_EXPORT=1) 116 endif() 117 list(APPEND LZ4_LIBRARIES_BUILT lz4_shared) 118endif() 119if(BUILD_STATIC_LIBS) 120 set(STATIC_LIB_NAME lz4) 121 if (MSVC AND BUILD_SHARED_LIBS) 122 set(STATIC_LIB_NAME lz4_static) 123 endif() 124 add_library(lz4_static STATIC ${LZ4_SOURCES}) 125 target_include_directories(lz4_static 126 PUBLIC $<BUILD_INTERFACE:${LZ4_LIB_SOURCE_DIR}> 127 INTERFACE $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>) 128 set_target_properties(lz4_static PROPERTIES 129 OUTPUT_NAME ${STATIC_LIB_NAME} 130 POSITION_INDEPENDENT_CODE ${LZ4_POSITION_INDEPENDENT_LIB}) 131 list(APPEND LZ4_LIBRARIES_BUILT lz4_static) 132endif() 133 134if(BUILD_STATIC_LIBS) 135 set(LZ4_LINK_LIBRARY lz4_static) 136else() 137 list(APPEND LZ4_CLI_SOURCES ${LZ4_SOURCES}) 138endif() 139 140# lz4 141if (LZ4_BUILD_CLI) 142 set(LZ4_PROGRAMS_BUILT lz4cli) 143 add_executable(lz4cli ${LZ4_CLI_SOURCES}) 144 set_target_properties(lz4cli PROPERTIES OUTPUT_NAME lz4) 145 if (BUILD_STATIC_LIBS) 146 target_link_libraries(lz4cli ${LZ4_LINK_LIBRARY}) 147 endif() 148endif() 149 150# lz4c 151if (LZ4_BUILD_LEGACY_LZ4C) 152 list(APPEND LZ4_PROGRAMS_BUILT lz4c) 153 add_executable(lz4c ${LZ4_CLI_SOURCES}) 154 set_target_properties(lz4c PROPERTIES COMPILE_DEFINITIONS "ENABLE_LZ4C_LEGACY_OPTIONS") 155 if (BUILD_STATIC_LIBS) 156 target_link_libraries(lz4c ${LZ4_LINK_LIBRARY}) 157 endif() 158endif() 159 160# Extra warning flags 161include (CheckCCompilerFlag) 162foreach (flag 163 # GCC-style 164 -Wall 165 -Wextra 166 -Wundef 167 -Wcast-qual 168 -Wcast-align 169 -Wshadow 170 -Wswitch-enum 171 -Wdeclaration-after-statement 172 -Wstrict-prototypes 173 -Wpointer-arith 174 175 # MSVC-style 176 /W4) 177 # Because https://gcc.gnu.org/wiki/FAQ#wnowarning 178 string(REGEX REPLACE "\\-Wno\\-(.+)" "-W\\1" flag_to_test "${flag}") 179 string(REGEX REPLACE "[^a-zA-Z0-9]+" "_" test_name "CFLAG_${flag_to_test}") 180 181 check_c_compiler_flag("${ADD_COMPILER_FLAGS_PREPEND} ${flag_to_test}" ${test_name}) 182 183 if(${test_name}) 184 set(CMAKE_C_FLAGS "${flag} ${CMAKE_C_FLAGS}") 185 endif() 186 187 unset(test_name) 188 unset(flag_to_test) 189endforeach (flag) 190 191if(NOT LZ4_BUNDLED_MODE) 192 include(GNUInstallDirs) 193 194 install(TARGETS ${LZ4_PROGRAMS_BUILT} 195 BUNDLE DESTINATION "${CMAKE_INSTALL_BINDIR}" 196 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") 197 install(TARGETS ${LZ4_LIBRARIES_BUILT} 198 EXPORT lz4Targets 199 LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" 200 ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" 201 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") 202 install(FILES 203 "${LZ4_LIB_SOURCE_DIR}/lz4.h" 204 "${LZ4_LIB_SOURCE_DIR}/lz4frame.h" 205 "${LZ4_LIB_SOURCE_DIR}/lz4hc.h" 206 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") 207 install(FILES "${LZ4_PROG_SOURCE_DIR}/lz4.1" 208 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1") 209 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblz4.pc" 210 DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") 211 212 include(CMakePackageConfigHelpers) 213 write_basic_package_version_file( 214 "${CMAKE_CURRENT_BINARY_DIR}/lz4ConfigVersion.cmake" 215 VERSION ${LZ4_VERSION_STRING} 216 COMPATIBILITY SameMajorVersion) 217 218 set(LZ4_PKG_INSTALLDIR "${CMAKE_INSTALL_LIBDIR}/cmake/lz4") 219 configure_package_config_file( 220 "${CMAKE_CURRENT_LIST_DIR}/lz4Config.cmake.in" 221 "${CMAKE_CURRENT_BINARY_DIR}/lz4Config.cmake" 222 INSTALL_DESTINATION ${LZ4_PKG_INSTALLDIR}) 223 export(EXPORT lz4Targets 224 FILE ${CMAKE_CURRENT_BINARY_DIR}/lz4Targets.cmake 225 NAMESPACE LZ4::) 226 227 install(EXPORT lz4Targets 228 FILE lz4Targets.cmake 229 NAMESPACE LZ4:: 230 DESTINATION ${LZ4_PKG_INSTALLDIR}) 231 install(FILES 232 ${CMAKE_CURRENT_BINARY_DIR}/lz4Config.cmake 233 ${CMAKE_CURRENT_BINARY_DIR}/lz4ConfigVersion.cmake 234 DESTINATION ${LZ4_PKG_INSTALLDIR}) 235 236 # install lz4cat and unlz4 symlinks on *nix 237 if(UNIX AND LZ4_BUILD_CLI) 238 install(CODE " 239 foreach(f lz4cat unlz4) 240 set(dest \"\$ENV{DESTDIR}${CMAKE_INSTALL_FULL_BINDIR}/\${f}\") 241 message(STATUS \"Symlinking: \${dest} -> lz4\") 242 execute_process( 243 COMMAND \"${CMAKE_COMMAND}\" -E create_symlink lz4 \"\${dest}\") 244 endforeach() 245 ") 246 247 # create manpage aliases 248 foreach(f lz4cat unlz4) 249 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${f}.1" ".so man1/lz4.1\n") 250 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${f}.1" 251 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1") 252 endforeach() 253 endif(UNIX AND LZ4_BUILD_CLI) 254endif(NOT LZ4_BUNDLED_MODE) 255 256# pkg-config 257set(PREFIX "${CMAKE_INSTALL_PREFIX}") 258 259if("${CMAKE_INSTALL_FULL_LIBDIR}" STREQUAL "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}") 260 set(LIBDIR "\${prefix}/${CMAKE_INSTALL_LIBDIR}") 261else() 262 set(LIBDIR "${CMAKE_INSTALL_FULL_LIBDIR}") 263endif() 264 265if("${CMAKE_INSTALL_FULL_INCLUDEDIR}" STREQUAL "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}") 266 set(INCLUDEDIR "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}") 267else() 268 set(INCLUDEDIR "${CMAKE_INSTALL_FULL_INCLUDEDIR}") 269endif() 270 271# for liblz4.pc substitution 272set(VERSION ${LZ4_VERSION_STRING}) 273configure_file(${LZ4_LIB_SOURCE_DIR}/liblz4.pc.in liblz4.pc @ONLY) 274