Home | History | Annotate | Download | only in cmake_unofficial
      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 
     13 set(LZ4_TOP_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../..")
     14 
     15 option(LZ4_BUILD_LEGACY_LZ4C "Build lz4c progam with legacy argument support" ON)
     16 
     17 # Parse version information
     18 file(STRINGS "${LZ4_TOP_SOURCE_DIR}/lib/lz4.h" LZ4_VERSION_MAJOR REGEX "^#define LZ4_VERSION_MAJOR +([0-9]+) +.*$")
     19 string(REGEX REPLACE "^#define LZ4_VERSION_MAJOR +([0-9]+) +.*$" "\\1" LZ4_VERSION_MAJOR "${LZ4_VERSION_MAJOR}")
     20 file(STRINGS "${LZ4_TOP_SOURCE_DIR}/lib/lz4.h" LZ4_VERSION_MINOR REGEX "^#define LZ4_VERSION_MINOR +([0-9]+) +.*$")
     21 string(REGEX REPLACE "^#define LZ4_VERSION_MINOR +([0-9]+) +.*$" "\\1" LZ4_VERSION_MINOR "${LZ4_VERSION_MINOR}")
     22 file(STRINGS "${LZ4_TOP_SOURCE_DIR}/lib/lz4.h" LZ4_VERSION_RELEASE REGEX "^#define LZ4_VERSION_RELEASE +([0-9]+) +.*$")
     23 string(REGEX REPLACE "^#define LZ4_VERSION_RELEASE +([0-9]+) +.*$" "\\1" LZ4_VERSION_RELEASE "${LZ4_VERSION_RELEASE}")
     24 set(LZ4_VERSION_STRING "${LZ4_VERSION_MAJOR}.${LZ4_VERSION_MINOR}.${LZ4_VERSION_RELEASE}")
     25 mark_as_advanced(LZ4_VERSION_STRING LZ4_VERSION_MAJOR LZ4_VERSION_MINOR LZ4_VERSION_RELEASE)
     26 
     27 if("${CMAKE_VERSION}" VERSION_LESS "3.0")
     28   project(LZ4 C)
     29 else()
     30   cmake_policy (SET CMP0048 NEW)
     31   project(LZ4
     32     VERSION ${LZ4_VERSION_STRING}
     33     LANGUAGES C)
     34 endif()
     35 
     36 cmake_minimum_required (VERSION 2.8.6)
     37 
     38 # If LZ4 is being bundled in another project, we don't want to
     39 # install anything.  However, we want to let people override this, so
     40 # we'll use the LZ4_BUNDLED_MODE variable to let them do that; just
     41 # set it to OFF in your project before you add_subdirectory(lz4/contrib/cmake_unofficial).
     42 get_directory_property(LZ4_PARENT_DIRECTORY PARENT_DIRECTORY)
     43 if("${LZ4_BUNDLED_MODE}" STREQUAL "")
     44   # Bundled mode hasn't been set one way or the other, set the default
     45   # depending on whether or not we are the top-level project.
     46   if("${LZ4_PARENT_DIRECTORY}" STREQUAL "")
     47     set(LZ4_BUNDLED_MODE OFF)
     48   else()
     49     set(LZ4_BUNDLED_MODE ON)
     50   endif()
     51 endif()
     52 mark_as_advanced(LZ4_BUNDLED_MODE)
     53 
     54 # CPack
     55 if(NOT LZ4_BUNDLED_MODE AND NOT CPack_CMake_INCLUDED)
     56   set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "LZ4 compression library")
     57   set(CPACK_PACKAGE_DESCRIPTION_FILE "${LZ4_TOP_SOURCE_DIR}/README.md")
     58   set(CPACK_RESOURCE_FILE_LICENSE "${LZ4_TOP_SOURCE_DIR}/LICENSE")
     59   set(CPACK_PACKAGE_VERSION_MAJOR ${LZ4_VERSION_MAJOR})
     60   set(CPACK_PACKAGE_VERSION_MINOR ${LZ4_VERSION_MINOR})
     61   set(CPACK_PACKAGE_VERSION_PATCH ${LZ4_VERSION_RELEASE})
     62   include(CPack)
     63 endif(NOT LZ4_BUNDLED_MODE AND NOT CPack_CMake_INCLUDED)
     64 
     65 # Allow people to choose whether to build shared or static libraries
     66 # via the BUILD_SHARED_LIBS option unless we are in bundled mode, in
     67 # which case we always use static libraries.
     68 include(CMakeDependentOption)
     69 CMAKE_DEPENDENT_OPTION(BUILD_SHARED_LIBS "Build shared libraries" ON "NOT LZ4_BUNDLED_MODE" OFF)
     70 CMAKE_DEPENDENT_OPTION(BUILD_STATIC_LIBS "Build static libraries" OFF "BUILD_SHARED_LIBS" ON)
     71 
     72 if(NOT BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS)
     73   message(FATAL_ERROR "Both BUILD_SHARED_LIBS and BUILD_STATIC_LIBS have been disabled")
     74 endif()
     75 
     76 set(LZ4_LIB_SOURCE_DIR "${LZ4_TOP_SOURCE_DIR}/lib")
     77 set(LZ4_PROG_SOURCE_DIR "${LZ4_TOP_SOURCE_DIR}/programs")
     78 
     79 include_directories("${LZ4_LIB_SOURCE_DIR}")
     80 
     81 # CLI sources
     82 set(LZ4_SOURCES
     83   "${LZ4_LIB_SOURCE_DIR}/lz4.c"
     84   "${LZ4_LIB_SOURCE_DIR}/lz4hc.c"
     85   "${LZ4_LIB_SOURCE_DIR}/lz4.h"
     86   "${LZ4_LIB_SOURCE_DIR}/lz4hc.h"
     87   "${LZ4_LIB_SOURCE_DIR}/lz4frame.c"
     88   "${LZ4_LIB_SOURCE_DIR}/lz4frame.h"
     89   "${LZ4_LIB_SOURCE_DIR}/xxhash.c")
     90 set(LZ4_CLI_SOURCES
     91   "${LZ4_PROG_SOURCE_DIR}/bench.c"
     92   "${LZ4_PROG_SOURCE_DIR}/lz4cli.c"
     93   "${LZ4_PROG_SOURCE_DIR}/lz4io.c"
     94   "${LZ4_PROG_SOURCE_DIR}/datagen.c")
     95 
     96 # Whether to use position independent code for the static library.  If
     97 # we're building a shared library this is ignored and PIC is always
     98 # used.
     99 option(LZ4_POSITION_INDEPENDENT_LIB "Use position independent code for static library (if applicable)" ON)
    100 
    101 # liblz4
    102 set(LZ4_LIBRARIES_BUILT)
    103 if(BUILD_SHARED_LIBS)
    104   add_library(lz4_shared SHARED ${LZ4_SOURCES})
    105   set_target_properties(lz4_shared PROPERTIES
    106     OUTPUT_NAME lz4
    107     SOVERSION "${LZ4_VERSION_MAJOR}"
    108     VERSION "${LZ4_VERSION_STRING}")
    109   list(APPEND LZ4_LIBRARIES_BUILT lz4_shared)
    110 endif()
    111 if(BUILD_STATIC_LIBS)
    112   add_library(lz4_static STATIC ${LZ4_SOURCES})
    113   set_target_properties(lz4_static PROPERTIES
    114     OUTPUT_NAME lz4
    115     POSITION_INDEPENDENT_CODE ${LZ4_POSITION_INDEPENDENT_LIB})
    116   list(APPEND LZ4_LIBRARIES_BUILT lz4_static)
    117 endif()
    118 
    119 # link to shared whenever possible, to static otherwise
    120 if(BUILD_SHARED_LIBS)
    121   set(LZ4_LINK_LIBRARY lz4_shared)
    122 else()
    123   set(LZ4_LINK_LIBRARY lz4_static)
    124 endif()
    125 
    126 # lz4
    127 set(LZ4_PROGRAMS_BUILT lz4cli)
    128 add_executable(lz4cli ${LZ4_CLI_SOURCES})
    129 set_target_properties(lz4cli PROPERTIES OUTPUT_NAME lz4)
    130 target_link_libraries(lz4cli ${LZ4_LINK_LIBRARY})
    131 
    132 # lz4c
    133 if (LZ4_BUILD_LEGACY_LZ4C)
    134   list(APPEND LZ4_PROGRAMS_BUILT lz4c)
    135   add_executable(lz4c ${LZ4_CLI_SOURCES})
    136   set_target_properties(lz4c PROPERTIES COMPILE_DEFINITIONS "ENABLE_LZ4C_LEGACY_OPTIONS")
    137   target_link_libraries(lz4c ${LZ4_LINK_LIBRARY})
    138 endif()
    139 
    140 # Extra warning flags
    141 include (CheckCCompilerFlag)
    142 foreach (flag
    143     # GCC-style
    144     -Wall
    145     -Wextra
    146     -Wundef
    147     -Wcast-qual
    148     -Wcast-align
    149     -Wshadow
    150     -Wswitch-enum
    151     -Wdeclaration-after-statement
    152     -Wstrict-prototypes
    153     -Wpointer-arith
    154 
    155     # MSVC-style
    156     /W4)
    157   # Because https://gcc.gnu.org/wiki/FAQ#wnowarning
    158   string(REGEX REPLACE "\\-Wno\\-(.+)" "-W\\1" flag_to_test "${flag}")
    159   string(REGEX REPLACE "[^a-zA-Z0-9]+" "_" test_name "CFLAG_${flag_to_test}")
    160 
    161   check_c_compiler_flag("${ADD_COMPILER_FLAGS_PREPEND} ${flag_to_test}" ${test_name})
    162 
    163   if(${test_name})
    164     set(CMAKE_C_FLAGS "${flag} ${CMAKE_C_FLAGS}")
    165   endif()
    166 
    167   unset(test_name)
    168   unset(flag_to_test)
    169 endforeach (flag)
    170 
    171 if(NOT LZ4_BUNDLED_MODE)
    172   include(GNUInstallDirs)
    173 
    174   install(TARGETS ${LZ4_PROGRAMS_BUILT}
    175     RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
    176   install(TARGETS ${LZ4_LIBRARIES_BUILT}
    177     LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
    178     ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
    179     RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
    180   install(FILES
    181     "${LZ4_LIB_SOURCE_DIR}/lz4.h"
    182     "${LZ4_LIB_SOURCE_DIR}/lz4frame.h"
    183     "${LZ4_LIB_SOURCE_DIR}/lz4hc.h"
    184     DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
    185   install(FILES "${LZ4_PROG_SOURCE_DIR}/lz4.1"
    186     DESTINATION "${CMAKE_INSTALL_MANDIR}/man1")
    187   install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblz4.pc"
    188     DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
    189 
    190   # install lz4cat and unlz4 symlinks on *nix
    191   if(UNIX)
    192     install(CODE "
    193       foreach(f lz4cat unlz4)
    194         set(dest \"\$ENV{DESTDIR}${CMAKE_INSTALL_FULL_BINDIR}/\${f}\")
    195         message(STATUS \"Symlinking: \${dest} -> lz4\")
    196         execute_process(
    197           COMMAND \"${CMAKE_COMMAND}\" -E create_symlink lz4 \"\${dest}\")
    198       endforeach()
    199     ")
    200 
    201     # create manpage aliases
    202     foreach(f lz4cat unlz4)
    203       file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${f}.1" ".so man1/lz4.1\n")
    204       install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${f}.1"
    205         DESTINATION "${CMAKE_INSTALL_MANDIR}/man1")
    206     endforeach()
    207   endif(UNIX)
    208 endif(NOT LZ4_BUNDLED_MODE)
    209 
    210 # pkg-config
    211 set(PREFIX "${CMAKE_INSTALL_PREFIX}")
    212 
    213 if("${CMAKE_INSTALL_FULL_LIBDIR}" STREQUAL "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
    214   set(LIBDIR "\${prefix}/${CMAKE_INSTALL_LIBDIR}")
    215 else()
    216   set(LIBDIR "${CMAKE_INSTALL_FULL_LIBDIR}")
    217 endif()
    218 
    219 if("${CMAKE_INSTALL_FULL_INCLUDEDIR}" STREQUAL "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}")
    220   set(INCLUDEDIR "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
    221 else()
    222   set(INCLUDEDIR "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
    223 endif()
    224 
    225 # for liblz4.pc substitution
    226 set(VERSION ${LZ4_VERSION_STRING})
    227 configure_file(${LZ4_LIB_SOURCE_DIR}/liblz4.pc.in liblz4.pc @ONLY)
    228