4 +cmake_minimum_required(VERSION 3.12)
8 + DESCRIPTION "This Bzip2/libbz2 a program and library for lossless block-sorting data compression."
11 +# See versioning rule:
12 +# http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
14 +# KEEP THESE IN SYNC WITH meson.build OR STUFF WILL BREAK!
19 +set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
21 +include_directories(${PROJECT_BINARY_DIR})
23 +math(EXPR LT_SOVERSION "${LT_CURRENT} - ${LT_AGE}")
24 +set(LT_VERSION "${LT_SOVERSION}.${LT_AGE}.${LT_REVISION}")
25 +set(PACKAGE_VERSION ${PROJECT_VERSION})
27 +set(ENABLE_APP_DEFAULT ON)
28 +set(ENABLE_EXAMPLES_DEFAULT OFF)
29 +set(ENABLE_DOCS_DEFAULT OFF)
30 +include(CMakeOptions.txt)
32 +if(ENABLE_LIB_ONLY AND (ENABLE_APP OR ENABLE_EXAMPLES))
33 + # Remember when disabled options are disabled for later diagnostics.
34 + set(ENABLE_LIB_ONLY_DISABLED_OTHERS 1)
36 + set(ENABLE_LIB_ONLY_DISABLED_OTHERS 0)
40 + set(ENABLE_EXAMPLES OFF)
43 +# Do not disable assertions based on CMAKE_BUILD_TYPE.
44 +foreach(_build_type Release MinSizeRel RelWithDebInfo)
46 + string(TOUPPER CMAKE_${_lang}_FLAGS_${_build_type} _var)
47 + string(REGEX REPLACE "(^|)[/-]D *NDEBUG($|)" " " ${_var} "${${_var}}")
51 +# Support the latest c++ standard available.
52 +include(CheckCCompilerFlag)
53 +include(CheckCXXCompilerFlag)
55 +function(extract_valid_c_flags varname)
57 + foreach(flag IN LISTS ARGN)
58 + string(REGEX REPLACE "[^a-zA-Z0-9_]+" "_" flag_var ${flag})
59 + set(flag_var "C_FLAG_${flag_var}")
60 + check_c_compiler_flag("${flag}" "${flag_var}")
62 + set(valid_flags "${valid_flags} ${flag}")
65 + set(${varname} "${valid_flags}" PARENT_SCOPE)
67 +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
68 + set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the build type" FORCE)
70 + # Include "None" as option to disable any additional (optimization) flags,
71 + # relying on just CMAKE_C_FLAGS and CMAKE_CXX_FLAGS (which are empty by
72 + # default). These strings are presented in cmake-gui.
73 + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
74 + None Debug Release MinSizeRel RelWithDebInfo)
77 +# Install a symlink of script to the "bin" directory.
78 +# Not intended for use on Windows.
79 +function(install_script_symlink original symlink)
80 + add_custom_command(OUTPUT ${symlink}
81 + COMMAND ${CMAKE_COMMAND} -E create_symlink ${original} ${symlink}
83 + COMMENT "Generating symbolic link ${symlink} of ${original}")
84 + add_custom_target(${symlink}_tgt ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${symlink})
85 + install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${symlink} DESTINATION ${CMAKE_INSTALL_BINDIR})
88 +# Install a symlink of binary target to the "bin" directory.
89 +# On Windows, it will be a copy instead of a symlink.
90 +function(install_target_symlink original symlink)
93 + set(symlink "${symlink}.exe")
95 + set(op create_symlink)
97 + add_custom_command(TARGET ${original} POST_BUILD
98 + COMMAND ${CMAKE_COMMAND} -E ${op} $<TARGET_FILE_NAME:${original}> ${symlink}
99 + WORKING_DIRECTORY $<TARGET_FILE_DIR:${original}>
100 + COMMENT "Generating symbolic link (or copy) ${symlink} of ${original}")
101 + install(PROGRAMS $<TARGET_FILE_DIR:${original}>/${symlink} DESTINATION ${CMAKE_INSTALL_BINDIR})
104 +include(GNUInstallDirs)
106 +# Checks for header files.
107 +include(CheckIncludeFile)
108 +check_include_file(arpa/inet.h HAVE_ARPA_INET_H)
109 +check_include_file(fcntl.h HAVE_FCNTL_H)
110 +check_include_file(inttypes.h HAVE_INTTYPES_H)
111 +check_include_file(limits.h HAVE_LIMITS_H)
112 +check_include_file(netdb.h HAVE_NETDB_H)
113 +check_include_file(netinet/in.h HAVE_NETINET_IN_H)
114 +check_include_file(pwd.h HAVE_PWD_H)
115 +check_include_file(sys/socket.h HAVE_SYS_SOCKET_H)
116 +check_include_file(sys/time.h HAVE_SYS_TIME_H)
117 +check_include_file(syslog.h HAVE_SYSLOG_H)
118 +check_include_file(time.h HAVE_TIME_H)
119 +check_include_file(unistd.h HAVE_UNISTD_H)
121 +include(CheckTypeSize)
122 +# Checks for typedefs, structures, and compiler characteristics.
124 +check_type_size("ssize_t" SIZEOF_SSIZE_T)
125 +if(NOT SIZEOF_SSIZE_T)
126 + # ssize_t is a signed type in POSIX storing at least -1.
127 + # Set it to "int" to match the behavior of AC_TYPE_SSIZE_T (autotools).
131 +include(CheckStructHasMember)
132 +check_struct_has_member("struct tm" tm_gmtoff time.h HAVE_STRUCT_TM_TM_GMTOFF)
134 +include(CheckSymbolExists)
135 +# XXX does this correctly detect initgroups (un)availability on cygwin?
136 +check_symbol_exists(initgroups grp.h HAVE_DECL_INITGROUPS)
137 +if(NOT HAVE_DECL_INITGROUPS AND HAVE_UNISTD_H)
138 + # FreeBSD declares initgroups() in unistd.h
139 + check_symbol_exists(initgroups unistd.h HAVE_DECL_INITGROUPS2)
140 + if(HAVE_DECL_INITGROUPS2)
141 + set(HAVE_DECL_INITGROUPS 1)
146 +if(CMAKE_C_COMPILER_ID MATCHES "MSVC")
148 + set(WARNCFLAGS /WX)
152 + extract_valid_c_flags(WARNCFLAGS -Werror)
156 + # Please keep this list in sync with meson.build
157 + extract_valid_c_flags(WARNCFLAGS
160 + -Wmissing-prototypes
161 + -Wstrict-prototypes
162 + -Wmissing-declarations
164 + -Wdeclaration-after-statement
184 + -Wextended-offsetof
185 + -Wformat-nonliteral
186 + -Wlanguage-extension-token
187 + -Wmissing-field-initializers
189 + -Wmissing-variable-declarations
190 + # -Wpadded # Not used because we cannot change public structs
192 + # -Wswitch-enum # Not used because this basically disallows default case
193 + -Wunreachable-code-break
198 + -Wno-format-nonliteral # This is required because we pass format string as "const char*.
206 +#add_definitions(-DHAVE_CONFIG_H)
207 +#configure_file(cmakeconfig.h.in config.h)
209 +# autotools-compatible names
210 +# Sphinx expects relative paths in the .rst files. Use the fact that the files
211 +# below are all one directory level deep.
212 +file(RELATIVE_PATH top_srcdir ${CMAKE_CURRENT_BINARY_DIR}/dir ${CMAKE_CURRENT_SOURCE_DIR})
213 +file(RELATIVE_PATH top_builddir ${CMAKE_CURRENT_BINARY_DIR}/dir ${CMAKE_CURRENT_BINARY_DIR})
214 +set(abs_top_srcdir ${CMAKE_CURRENT_SOURCE_DIR})
215 +set(abs_top_builddir ${CMAKE_CURRENT_BINARY_DIR})
216 +# bzip2.pc (pkg-config file)
217 +set(prefix ${CMAKE_INSTALL_PREFIX})
218 +set(exec_prefix ${CMAKE_INSTALL_PREFIX})
219 +set(bindir ${CMAKE_INSTALL_FULL_BINDIR})
220 +set(sbindir ${CMAKE_INSTALL_FULL_SBINDIR})
221 +set(libdir ${CMAKE_INSTALL_FULL_LIBDIR})
222 +set(includedir ${CMAKE_INSTALL_FULL_INCLUDEDIR})
223 +set(VERSION ${PACKAGE_VERSION})
226 +# The build targets.
227 +# In a larger project, the following would be in subdirectories and
228 +# These targets would be included with `add_subdirectory()`
239 +# The bz2 OBJECT-library, required for bzip2, bzip2recover.
240 +add_library(bz2_ObjLib OBJECT)
241 +target_sources(bz2_ObjLib
242 + PRIVATE ${BZ2_SOURCES}
243 + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/bzlib_private.h
244 + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/bzlib.h)
246 +if(ENABLE_SHARED_LIB)
247 + # The libbz2 shared library.
248 + add_library(bz2 SHARED ${BZ2_RES})
250 + PRIVATE ${BZ2_SOURCES}
251 + ${CMAKE_CURRENT_SOURCE_DIR}/libbz2.def
252 + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/bzlib_private.h
253 + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/bzlib.h)
254 + # Always use '-fPIC'/'-fPIE' option for shared libraries.
255 + set_property(TARGET bz2 PROPERTY POSITION_INDEPENDENT_CODE ON)
256 + set_target_properties(bz2 PROPERTIES
257 + COMPILE_FLAGS "${WARNCFLAGS}"
258 + VERSION ${LT_VERSION} SOVERSION ${LT_SOVERSION})
259 + install(TARGETS bz2 DESTINATION ${CMAKE_INSTALL_LIBDIR})
260 + install(FILES bzlib.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
263 + # Hack to support the old libbz2.so.1.0 version by including an extra copy.
264 + # Technically the old SONAME is not libtool compatible.
265 + # This hack is to support binary compatibility with libbz2 in some distro packages.
266 + if(UNIX AND NOT APPLE)
267 + add_library(bz2_old_soname SHARED ${BZ2_RES})
268 + target_sources(bz2_old_soname
269 + PRIVATE ${BZ2_SOURCES}
270 + ${CMAKE_CURRENT_SOURCE_DIR}/libbz2.def
271 + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/bzlib_private.h
272 + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/bzlib.h
274 + set_target_properties(bz2_old_soname PROPERTIES
275 + COMPILE_FLAGS "${WARNCFLAGS}"
276 + VERSION ${LT_SOVERSION}.${LT_AGE} SOVERSION ${LT_SOVERSION}.${LT_AGE}
279 + install(TARGETS bz2_old_soname DESTINATION ${CMAKE_INSTALL_LIBDIR})
284 +if(ENABLE_STATIC_LIB)
285 + # The libbz2 static library.
286 + add_library(bz2_static STATIC)
287 + target_sources(bz2_static
288 + PRIVATE ${BZ2_SOURCES}
289 + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/bzlib_private.h
290 + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/bzlib.h)
291 + set_target_properties(bz2_static PROPERTIES
292 + COMPILE_FLAGS "${WARNCFLAGS}"
293 + VERSION ${LT_VERSION}
294 + SOVERSION ${LT_SOVERSION}
295 + ARCHIVE_OUTPUT_NAME bz2_static)
296 + target_compile_definitions(bz2_static PUBLIC BZ2_STATICLIB)
297 + install(TARGETS bz2_static DESTINATION ${CMAKE_INSTALL_LIBDIR})
298 + install(FILES bzlib.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
302 + # The bzip2 executable.
303 + add_executable(bzip2)
304 + target_sources(bzip2
306 + target_link_libraries(bzip2
307 + PRIVATE bz2_ObjLib)
309 + target_compile_definitions(bzip2 PUBLIC BZ_LCCWIN32 BZ_UNIX=0)
311 + target_compile_definitions(bzip2 PUBLIC BZ_LCCWIN32=0 BZ_UNIX)
313 + install(TARGETS bzip2 DESTINATION ${CMAKE_INSTALL_BINDIR})
315 + # Create bzip2 copies bzcat and bunzip.
316 + # The default behavior is altered in bzip2.c code by checking the program name.
317 + install_target_symlink(bzip2 bzcat)
318 + install_target_symlink(bzip2 bunzip)
320 + # The bzip2recover executable.
321 + add_executable(bzip2recover)
322 + target_sources(bzip2recover
323 + PRIVATE bzip2recover.c)
324 + target_link_libraries(bzip2recover
325 + PRIVATE bz2_ObjLib)
327 + target_compile_definitions(bzip2recover PUBLIC BZ_LCCWIN32 BZ_UNIX=0)
329 + target_compile_definitions(bzip2recover PUBLIC BZ_LCCWIN32=0 BZ_UNIX)
331 + install(TARGETS bzip2recover DESTINATION ${CMAKE_INSTALL_BINDIR})
333 + if(ENABLE_EXAMPLES)
334 + if(ENABLE_SHARED_LIB)
335 + # The dlltest executable.
336 + add_executable(dlltest)
337 + target_sources(dlltest
339 + target_link_libraries(dlltest bz2)
340 + install(TARGETS dlltest DESTINATION ${CMAKE_INSTALL_BINDIR})
345 + # Install shell scripts, and renamed copies.
346 + install(PROGRAMS bzdiff bzgrep bzmore
347 + DESTINATION ${CMAKE_INSTALL_BINDIR})
349 + install_script_symlink(bzdiff bzcmp)
351 + install_script_symlink(bzgrep bzegrep)
352 + install_script_symlink(bzgrep bzfgrep)
354 + install_script_symlink(bzmore bzless)
359 +++ b/CMakeOptions.txt
361 +# Features that can be enabled for cmake (see CMakeLists.txt)
363 +option(ENABLE_WERROR "Turn on compile time warnings")
365 +option(ENABLE_DEBUG "Turn on debug output")
367 +option(ENABLE_APP "Build applications (bzip2, and bzip2recover)"
368 + ${ENABLE_APP_DEFAULT})
370 +option(ENABLE_DOCS "Generate documentation"
371 + ${ENABLE_DOCS_DEFAULT})
373 +option(ENABLE_EXAMPLES "Build examples"
374 + ${ENABLE_EXAMPLES_DEFAULT})
376 +option(ENABLE_LIB_ONLY "Build libbz2 only. This is a short hand for -DENABLE_APP=0 -DENABLE_EXAMPLES=0")
378 +option(ENABLE_STATIC_LIB "Build libbz2 in static mode also")
380 +option(ENABLE_SHARED_LIB "Build libbz2 as a shared library" ON)
382 +option(USE_OLD_SONAME "Use libbz2.so.1.0 for compatibility with old Makefiles" OFF)