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