diff --git a/.cmake-format b/.cmake-format new file mode 100644 index 0000000..7e4660f --- /dev/null +++ b/.cmake-format @@ -0,0 +1,199 @@ +parse: + additional_commands: + _add_boost_lib: + kwargs: + CXXFLAGS_PRIVATE: '*' + DEFINE: '*' + DEFINE_PRIVATE: '*' + INCLUDE_PRIVATE: '*' + LINK: '*' + NAME: '1' + SOURCES: '*' + _add_boost_test: + kwargs: + DEFINE: '*' + LINK: '*' + NAME: '1' + RUN: '1' + TESTS: '*' + boost_test: + kwargs: + ARGUMENTS: '*' + LIBRARIES: '*' + NAME: '1' + PREFIX: '1' + SOURCES: '*' + TYPE: '1' + catch_discover_tests: + kwargs: + OUTPUT_DIR: 1 + OUTPUT_PREFIX: 1 + OUTPUT_SUFFIX: 1 + REPORTER: 1 + TEST_PREFIX: 1 + pargs: + flags: [] + nargs: '*' + conan_add_remote: + kwargs: + INDEX: 1 + NAME: 1 + URL: 1 + pargs: + flags: [] + nargs: '*' + conan_cmake_install: + kwargs: + BUILD: + + ENV: + + ENV_BUILD: + + ENV_HOST: + + GENERATOR: + + INSTALL_FOLDER: 1 + LOCKFILE: 1 + LOCKFILE_NODE_ID: 1 + LOCKFILE_OUT: 1 + OPTIONS: + + OPTIONS_BUILD: + + OPTIONS_HOST: + + PATH_OR_REFERENCE: 1 + PROFILE: + + PROFILE_BUILD: + + PROFILE_HOST: + + REFERENCE: 1 + REMOTE: 1 + SETTINGS: + + SETTINGS_BUILD: + + SETTINGS_HOST: + + pargs: + flags: [] + nargs: '*' + cpmaddpackage: + kwargs: + DOWNLOAD_COMMAND: 1 + DOWNLOAD_NAME: 1 + DOWNLOAD_NO_EXTRACT: 1 + DOWNLOAD_ONLY: 1 + FIND_PACKAGE_ARGUMENTS: 1 + FORCE: 1 + GITHUB_REPOSITORY: 1 + GITLAB_REPOSITORY: 1 + GIT_REPOSITORY: 1 + GIT_SHALLOW: 1 + GIT_TAG: 1 + HTTP_PASSWORD: 1 + HTTP_USERNAME: 1 + NAME: 1 + NO_CACHE: 1 + OPTIONS: + + SOURCE_DIR: 1 + SVN_REPOSITORY: 1 + SVN_REVISION: 1 + URL: 1 + URL_HASH: 1 + URL_MD5: 1 + VERSION: 1 + pargs: + flags: [] + nargs: '*' + spelling: CPMAddPackage + cpmdeclarepackage: + kwargs: + DOWNLOAD_COMMAND: 1 + DOWNLOAD_NAME: 1 + DOWNLOAD_NO_EXTRACT: 1 + DOWNLOAD_ONLY: 1 + FIND_PACKAGE_ARGUMENTS: 1 + FORCE: 1 + GITHUB_REPOSITORY: 1 + GITLAB_REPOSITORY: 1 + GIT_REPOSITORY: 1 + GIT_SHALLOW: 1 + GIT_TAG: 1 + HTTP_PASSWORD: 1 + HTTP_USERNAME: 1 + NAME: 1 + NO_CACHE: 1 + OPTIONS: + + SOURCE_DIR: 1 + SVN_REPOSITORY: 1 + SVN_REVISION: 1 + URL: 1 + URL_HASH: 1 + URL_MD5: 1 + VERSION: 1 + pargs: + flags: [] + nargs: '*' + spelling: CPMDeclarePackage + cpmfindpackage: + kwargs: + DOWNLOAD_COMMAND: 1 + DOWNLOAD_NAME: 1 + DOWNLOAD_NO_EXTRACT: 1 + DOWNLOAD_ONLY: 1 + FIND_PACKAGE_ARGUMENTS: 1 + FORCE: 1 + GITHUB_REPOSITORY: 1 + GITLAB_REPOSITORY: 1 + GIT_REPOSITORY: 1 + GIT_SHALLOW: 1 + GIT_TAG: 1 + HTTP_PASSWORD: 1 + HTTP_USERNAME: 1 + NAME: 1 + NO_CACHE: 1 + OPTIONS: + + SOURCE_DIR: 1 + SVN_REPOSITORY: 1 + SVN_REVISION: 1 + URL: 1 + URL_HASH: 1 + URL_MD5: 1 + VERSION: 1 + pargs: + flags: [] + nargs: '*' + spelling: CPMFindPackage + cpmgetpackageversion: + pargs: 2 + spelling: CPMGetPackageVersion + cpmregisterpackage: + pargs: 1 + spelling: CPMRegisterPackage + cpmusepackagelock: + pargs: 1 + spelling: CPMUsePackageLock + packageproject: + kwargs: + BINARY_DIR: 1 + COMPATIBILITY: 1 + DEPENDENCIES: + + DISABLE_VERSION_SUFFIX: 1 + EXPORT_HEADER: 1 + INCLUDE_DESTINATION: 1 + INCLUDE_DIR: 1 + NAME: 1 + NAMESPACE: 1 + VERSION: 1 + VERSION_HEADER: 1 + pargs: + flags: [] + nargs: '*' + spelling: packageProject +format: + line_width: 123 + tab_size: 4 + max_subgroups_hwrap: 4 + max_pargs_hwrap: 4 + max_rows_cmdline: 8 + separate_ctrl_name_with_space: false + separate_fn_name_with_space: false + dangle_parens: true + min_prefix_chars: 8 + max_lines_hwrap: 3 + line_ending: unix +markup: + bullet_char: '*' + enum_char: . + enable_markup: false diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d9b58d..8360103 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,35 +1,27 @@ -cmake_minimum_required(VERSION 2.4.4...3.15.0) -set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS ON) +# CMake 3.21 or later is required for PROJECT_IS_TOP_LEVEL +cmake_minimum_required(VERSION 3.21...3.30) -project(zlib C) +project(zlib VERSION 1.3.1.1 LANGUAGES C) -set(VERSION "1.3.1.1") - -option(ZLIB_BUILD_EXAMPLES "Enable Zlib Examples" ON) - -set(INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation directory for executables") -set(INSTALL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE PATH "Installation directory for libraries") -set(INSTALL_INC_DIR "${CMAKE_INSTALL_PREFIX}/include" CACHE PATH "Installation directory for headers") -set(INSTALL_MAN_DIR "${CMAKE_INSTALL_PREFIX}/share/man" CACHE PATH "Installation directory for manual pages") -set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_PREFIX}/share/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files") +# Where possible, projects should require CMake 3.21 or later and ensure policies CMP0077 and CMP0126 are set to NEW. +# This will prevent the project from discarding values set by parent projects as non-cache variables. +# XXX set(BUILD_SHARED_LIBS ON CACHE BOOL "Enable to build a shared library(DLL) instead a static one") +option(BUILD_SHARED_LIBS "Enable to build a shared library(DLL) instead a static one." ${PROJECT_IS_TOP_LEVEL}) +option(ZLIB_BUILD_TESTING "Enable to build the test too." ${PROJECT_IS_TOP_LEVEL}) +option(ZLIB_BUILD_EXAMPLES "Enable to build the examples too." ${PROJECT_IS_TOP_LEVEL}) include(CheckTypeSize) include(CheckFunctionExists) include(CheckIncludeFile) -include(CheckCSourceCompiles) -enable_testing() check_include_file(sys/types.h HAVE_SYS_TYPES_H) -check_include_file(stdint.h HAVE_STDINT_H) -check_include_file(stddef.h HAVE_STDDEF_H) +check_include_file(stdint.h HAVE_STDINT_H) +check_include_file(stddef.h HAVE_STDDEF_H) # # Check to see if we have large file support # set(CMAKE_REQUIRED_DEFINITIONS -D_LARGEFILE64_SOURCE=1) -# We add these other definitions here because CheckTypeSize.cmake -# in CMake 2.4.x does not automatically do so and we want -# compatibility with CMake 2.4.x. if(HAVE_SYS_TYPES_H) list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_SYS_TYPES_H) endif() @@ -41,7 +33,7 @@ if(HAVE_STDDEF_H) endif() check_type_size(off64_t OFF64_T) if(HAVE_OFF64_T) - add_definitions(-D_LARGEFILE64_SOURCE=1) + add_definitions(-D_LARGEFILE64_SOURCE=1) endif() set(CMAKE_REQUIRED_DEFINITIONS) # clear variable @@ -75,25 +67,19 @@ if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR AND RENAME_ZCO message(STATUS "to 'zconf.h.included' because this file is included with zlib") message(STATUS "but CMake generates it automatically in the build directory.") file(RENAME ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h.included) - endif() + endif() endif() set(ZLIB_PC ${CMAKE_CURRENT_BINARY_DIR}/zlib.pc) -configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/zlib.pc.cmakein - ${ZLIB_PC} @ONLY) -configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h.cmakein - ${CMAKE_CURRENT_BINARY_DIR}/zconf.h @ONLY) -include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR}) - +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/zlib.pc.cmakein ${ZLIB_PC} @ONLY) +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/zconf.h.cmakein ${CMAKE_CURRENT_BINARY_DIR}/zconf.h @ONLY) +# XXX include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR}) #============================================================================ # zlib #============================================================================ -set(ZLIB_PUBLIC_HDRS - ${CMAKE_CURRENT_BINARY_DIR}/zconf.h - zlib.h -) +set(ZLIB_PUBLIC_HDRS ${CMAKE_CURRENT_BINARY_DIR}/zconf.h zlib.h) set(ZLIB_PRIVATE_HDRS crc32.h deflate.h @@ -123,45 +109,44 @@ set(ZLIB_SRCS zutil.c ) -if(NOT MINGW) - set(ZLIB_DLL_SRCS - win32/zlib1.rc # If present will override custom build rule below. +if(NOT MINGW AND NOT MSYS) + set(ZLIB_DLL_SRCS win32/zlib1.rc # If present will override custom build rule below. ) endif() # parse the full version number from zlib.h and include in ZLIB_FULL_VERSION file(READ ${CMAKE_CURRENT_SOURCE_DIR}/zlib.h _zlib_h_contents) -string(REGEX REPLACE ".*#define[ \t]+ZLIB_VERSION[ \t]+\"([-0-9A-Za-z.]+)\".*" - "\\1" ZLIB_FULL_VERSION ${_zlib_h_contents}) +string(REGEX REPLACE ".*#define[ \t]+ZLIB_VERSION[ \t]+\"([-0-9A-Za-z.]+)\".*" "\\1" ZLIB_FULL_VERSION ${_zlib_h_contents}) -if(MINGW) +# TODO: add_library without SHARED/STATIC arg picks which one to use based on +# the variable BUILD_SHARED_LIBS. Maybe this could be simplified? (Herry Mallon) +if(BUILD_SHARED_LIBS AND MINGW) # This gets us DLL resource information when compiling on MinGW. if(NOT CMAKE_RC_COMPILER) set(CMAKE_RC_COMPILER windres.exe) endif() - add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj - COMMAND ${CMAKE_RC_COMPILER} - -D GCC_WINDRES - -I ${CMAKE_CURRENT_SOURCE_DIR} - -I ${CMAKE_CURRENT_BINARY_DIR} - -o ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj - -i ${CMAKE_CURRENT_SOURCE_DIR}/win32/zlib1.rc) + add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj + COMMAND ${CMAKE_RC_COMPILER} -D GCC_WINDRES -I ${CMAKE_CURRENT_SOURCE_DIR} -I ${CMAKE_CURRENT_BINARY_DIR} -o + ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj -i ${CMAKE_CURRENT_SOURCE_DIR}/win32/zlib1.rc + ) set(ZLIB_DLL_SRCS ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj) -endif(MINGW) +endif() -add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS}) -target_include_directories(zlib PUBLIC - $ - $) -add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS}) -target_include_directories(zlibstatic PUBLIC - $ - $) -set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL) -set_target_properties(zlib PROPERTIES SOVERSION 1) +if(BUILD_SHARED_LIBS) + add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS}) + set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL) + set_target_properties(zlib PROPERTIES SOVERSION 1) +else() + add_library(zlib STATIC ${ZLIB_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS}) +endif() -if(NOT CYGWIN) +target_include_directories( + zlib PUBLIC $ $ +) + +if(BUILD_SHARED_LIBS AND NOT CYGWIN) # This property causes shared libraries on Linux to have the full version # encoded into their final filename. We disable this on Cygwin because # it causes cygz-${ZLIB_FULL_VERSION}.dll to be created when cygz.dll @@ -174,50 +159,79 @@ endif() if(UNIX) # On unix-like platforms the library is almost always called libz - set_target_properties(zlib zlibstatic PROPERTIES OUTPUT_NAME z) - if(NOT APPLE AND NOT(CMAKE_SYSTEM_NAME STREQUAL AIX)) - set_target_properties(zlib PROPERTIES LINK_FLAGS "-Wl,--version-script,\"${CMAKE_CURRENT_SOURCE_DIR}/zlib.map\"") - endif() + set_target_properties(zlib PROPERTIES OUTPUT_NAME z) + if(NOT APPLE AND NOT (CMAKE_SYSTEM_NAME STREQUAL AIX)) + set_target_properties(zlib PROPERTIES LINK_FLAGS "-Wl,--version-script,\"${CMAKE_CURRENT_SOURCE_DIR}/zlib.map\"") + endif() elseif(BUILD_SHARED_LIBS AND WIN32) # Creates zlib1.dll when building shared library version set_target_properties(zlib PROPERTIES SUFFIX "1.dll") endif() -if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL ) - install(TARGETS zlib zlibstatic - RUNTIME DESTINATION "${INSTALL_BIN_DIR}" - ARCHIVE DESTINATION "${INSTALL_LIB_DIR}" - LIBRARY DESTINATION "${INSTALL_LIB_DIR}" ) -endif() -if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL ) - install(FILES ${ZLIB_PUBLIC_HDRS} DESTINATION "${INSTALL_INC_DIR}") -endif() -if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL ) - install(FILES zlib.3 DESTINATION "${INSTALL_MAN_DIR}/man3") -endif() -if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL ) - install(FILES ${ZLIB_PC} DESTINATION "${INSTALL_PKGCONFIG_DIR}") -endif() +add_library(zlib::zlib ALIAS zlib) +# Note also that CMake's own FindZLIB.cmake module defines a slightly different +# imported target named ZLIB::ZLIB (the upper/lowercase differences are meaningful). +add_library(ZLIB::ZLIB ALIAS zlib) #============================================================================ -# Example binaries +# Example binaries and ctests locatet at test subdir +# TODO: add_subdirectory(test) #============================================================================ -if(ZLIB_BUILD_EXAMPLES) +if(ZLIB_BUILD_TESTING) + enable_testing() + add_executable(example test/example.c) target_link_libraries(example zlib) add_test(example example) - add_executable(minigzip test/minigzip.c) - target_link_libraries(minigzip zlib) - if(HAVE_OFF64_T) add_executable(example64 test/example.c) target_link_libraries(example64 zlib) set_target_properties(example64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64") add_test(example64 example64) + endif() +endif() +if(ZLIB_BUILD_EXAMPLES) + add_executable(minigzip test/minigzip.c) + target_link_libraries(minigzip zlib) + + if(HAVE_OFF64_T) add_executable(minigzip64 test/minigzip.c) target_link_libraries(minigzip64 zlib) set_target_properties(minigzip64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64") endif() endif() + +#============================================================================ +# optional install part +#============================================================================ +if(SKIP_INSTALL_ALL) + return() +endif() + +if(NOT SKIP_INSTALL_LIBRARIES) + include(GNUInstallDirs) + include(CMakePackageConfigHelpers) + + set(ZLIB_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/zlib") + set(ZLIB_INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_LIBDIR}/share/pkgconfig") + write_basic_package_version_file("zlibConfigVersion.cmake" COMPATIBILITY SameMajorVersion) + + target_include_directories(zlib PUBLIC $) + install(FILES "${PROJECT_BINARY_DIR}/zlibConfigVersion.cmake" DESTINATION "${ZLIB_INSTALL_CMAKEDIR}") + install(TARGETS zlib + EXPORT zlibConfig + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ) + install(EXPORT zlibConfig NAMESPACE zlib:: DESTINATION "${ZLIB_INSTALL_CMAKEDIR}") +endif() +if(NOT SKIP_INSTALL_HEADERS) + install(FILES ${ZLIB_PUBLIC_HDRS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") +endif() +if(NOT SKIP_INSTALL_FILES) + install(FILES zlib.3 DESTINATION "${CMAKE_INSTALL_MANDIR}/man3") + install(FILES ${ZLIB_PC} DESTINATION "${ZLIB_INSTALL_PKGCONFIG_DIR}") +endif() diff --git a/README b/README index 75da520..a3f5c3d 100644 --- a/README +++ b/README @@ -13,11 +13,15 @@ the library is working correctly. Another example is given in the file test/minigzip.c. The compression library itself is composed of all source files in the root directory. -To compile all files and run the test program, follow the instructions given at -the top of Makefile.in. In short "./configure; make test", and if that goes -well, "make install" should work for most flavors of Unix. For Windows, use -one of the special makefiles in win32/ or contrib/vstudio/ . For VMS, use -make_vms.com. +To compile all files, run the tests, and install the libray follow this +instructions: + + cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF + cmake --build build + cmake --build build --target test + cmake --install build --prefix /tmp/zlib + +Tip: Try cmake --help to see all available generators on your system. Questions about zlib should be sent to , or to Gilles Vollant for the Windows DLL version. The zlib home page is