• 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
15option(LZ4_BUILD_CLI "Build lz4 program" ON)
16option(LZ4_BUILD_LEGACY_LZ4C "Build lz4c progam with legacy argument support" ON)
17
18# Parse version information
19file(STRINGS "${LZ4_TOP_SOURCE_DIR}/lib/lz4.h" LZ4_VERSION_MAJOR REGEX "^#define LZ4_VERSION_MAJOR +([0-9]+) +.*$")
20string(REGEX REPLACE "^#define LZ4_VERSION_MAJOR +([0-9]+) +.*$" "\\1" LZ4_VERSION_MAJOR "${LZ4_VERSION_MAJOR}")
21file(STRINGS "${LZ4_TOP_SOURCE_DIR}/lib/lz4.h" LZ4_VERSION_MINOR REGEX "^#define LZ4_VERSION_MINOR +([0-9]+) +.*$")
22string(REGEX REPLACE "^#define LZ4_VERSION_MINOR +([0-9]+) +.*$" "\\1" LZ4_VERSION_MINOR "${LZ4_VERSION_MINOR}")
23file(STRINGS "${LZ4_TOP_SOURCE_DIR}/lib/lz4.h" LZ4_VERSION_RELEASE REGEX "^#define LZ4_VERSION_RELEASE +([0-9]+) +.*$")
24string(REGEX REPLACE "^#define LZ4_VERSION_RELEASE +([0-9]+) +.*$" "\\1" LZ4_VERSION_RELEASE "${LZ4_VERSION_RELEASE}")
25set(LZ4_VERSION_STRING "${LZ4_VERSION_MAJOR}.${LZ4_VERSION_MINOR}.${LZ4_VERSION_RELEASE}")
26mark_as_advanced(LZ4_VERSION_STRING LZ4_VERSION_MAJOR LZ4_VERSION_MINOR LZ4_VERSION_RELEASE)
27
28if("${CMAKE_VERSION}" VERSION_LESS "3.0")
29  project(LZ4 C)
30else()
31  cmake_policy (SET CMP0048 NEW)
32  project(LZ4
33    VERSION ${LZ4_VERSION_STRING}
34    LANGUAGES C)
35endif()
36
37cmake_minimum_required (VERSION 2.8.6)
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  set_target_properties(lz4_shared PROPERTIES
107    OUTPUT_NAME lz4
108    SOVERSION "${LZ4_VERSION_MAJOR}"
109    VERSION "${LZ4_VERSION_STRING}")
110  if(MSVC)
111    target_compile_definitions(lz4_shared PRIVATE
112      LZ4_DLL_EXPORT=1)
113  endif()
114  list(APPEND LZ4_LIBRARIES_BUILT lz4_shared)
115endif()
116if(BUILD_STATIC_LIBS)
117  add_library(lz4_static STATIC ${LZ4_SOURCES})
118  set_target_properties(lz4_static PROPERTIES
119    OUTPUT_NAME lz4
120    POSITION_INDEPENDENT_CODE ${LZ4_POSITION_INDEPENDENT_LIB})
121  list(APPEND LZ4_LIBRARIES_BUILT lz4_static)
122endif()
123
124# link to shared whenever possible, to static otherwise
125if(BUILD_SHARED_LIBS)
126  set(LZ4_LINK_LIBRARY lz4_shared)
127else()
128  set(LZ4_LINK_LIBRARY lz4_static)
129endif()
130
131# lz4
132if (LZ4_BUILD_CLI)
133  set(LZ4_PROGRAMS_BUILT lz4cli)
134  add_executable(lz4cli ${LZ4_CLI_SOURCES})
135  set_target_properties(lz4cli PROPERTIES OUTPUT_NAME lz4)
136  target_link_libraries(lz4cli ${LZ4_LINK_LIBRARY})
137endif()
138
139# lz4c
140if (LZ4_BUILD_LEGACY_LZ4C)
141  list(APPEND LZ4_PROGRAMS_BUILT lz4c)
142  add_executable(lz4c ${LZ4_CLI_SOURCES})
143  set_target_properties(lz4c PROPERTIES COMPILE_DEFINITIONS "ENABLE_LZ4C_LEGACY_OPTIONS")
144  target_link_libraries(lz4c ${LZ4_LINK_LIBRARY})
145endif()
146
147# Extra warning flags
148include (CheckCCompilerFlag)
149foreach (flag
150    # GCC-style
151    -Wall
152    -Wextra
153    -Wundef
154    -Wcast-qual
155    -Wcast-align
156    -Wshadow
157    -Wswitch-enum
158    -Wdeclaration-after-statement
159    -Wstrict-prototypes
160    -Wpointer-arith
161
162    # MSVC-style
163    /W4)
164  # Because https://gcc.gnu.org/wiki/FAQ#wnowarning
165  string(REGEX REPLACE "\\-Wno\\-(.+)" "-W\\1" flag_to_test "${flag}")
166  string(REGEX REPLACE "[^a-zA-Z0-9]+" "_" test_name "CFLAG_${flag_to_test}")
167
168  check_c_compiler_flag("${ADD_COMPILER_FLAGS_PREPEND} ${flag_to_test}" ${test_name})
169
170  if(${test_name})
171    set(CMAKE_C_FLAGS "${flag} ${CMAKE_C_FLAGS}")
172  endif()
173
174  unset(test_name)
175  unset(flag_to_test)
176endforeach (flag)
177
178if(NOT LZ4_BUNDLED_MODE)
179  include(GNUInstallDirs)
180
181  install(TARGETS ${LZ4_PROGRAMS_BUILT}
182    BUNDLE	DESTINATION "${CMAKE_INSTALL_BINDIR}"
183    RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
184  install(TARGETS ${LZ4_LIBRARIES_BUILT}
185    LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
186    ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
187    RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
188  install(FILES
189    "${LZ4_LIB_SOURCE_DIR}/lz4.h"
190    "${LZ4_LIB_SOURCE_DIR}/lz4frame.h"
191    "${LZ4_LIB_SOURCE_DIR}/lz4hc.h"
192    DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
193  install(FILES "${LZ4_PROG_SOURCE_DIR}/lz4.1"
194    DESTINATION "${CMAKE_INSTALL_MANDIR}/man1")
195  install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblz4.pc"
196    DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
197
198  # install lz4cat and unlz4 symlinks on *nix
199  if(UNIX AND LZ4_BUILD_CLI)
200    install(CODE "
201      foreach(f lz4cat unlz4)
202        set(dest \"\$ENV{DESTDIR}${CMAKE_INSTALL_FULL_BINDIR}/\${f}\")
203        message(STATUS \"Symlinking: \${dest} -> lz4\")
204        execute_process(
205          COMMAND \"${CMAKE_COMMAND}\" -E create_symlink lz4 \"\${dest}\")
206      endforeach()
207    ")
208
209    # create manpage aliases
210    foreach(f lz4cat unlz4)
211      file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${f}.1" ".so man1/lz4.1\n")
212      install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${f}.1"
213        DESTINATION "${CMAKE_INSTALL_MANDIR}/man1")
214    endforeach()
215  endif(UNIX AND LZ4_BUILD_CLI)
216endif(NOT LZ4_BUNDLED_MODE)
217
218# pkg-config
219set(PREFIX "${CMAKE_INSTALL_PREFIX}")
220
221if("${CMAKE_INSTALL_FULL_LIBDIR}" STREQUAL "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
222  set(LIBDIR "\${prefix}/${CMAKE_INSTALL_LIBDIR}")
223else()
224  set(LIBDIR "${CMAKE_INSTALL_FULL_LIBDIR}")
225endif()
226
227if("${CMAKE_INSTALL_FULL_INCLUDEDIR}" STREQUAL "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}")
228  set(INCLUDEDIR "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
229else()
230  set(INCLUDEDIR "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
231endif()
232
233# for liblz4.pc substitution
234set(VERSION ${LZ4_VERSION_STRING})
235configure_file(${LZ4_LIB_SOURCE_DIR}/liblz4.pc.in liblz4.pc @ONLY)
236