diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 1295c7e..6f0f497 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -8,13 +8,12 @@ env: jobs: build: - name: ${{ matrix.preset }}-${{ matrix.config }}-${{ matrix.lfs }}-${{ matrix.sanitize }} + name: ${{ matrix.preset }}-${{ matrix.config }}-${{ matrix.sanitize }} strategy: fail-fast: false matrix: preset: [linux-clang, linux-gcc, macos, windows] config: [Debug, Release] - lfs: [OFF, ON] sanitize: [address, thread, undefined, leak, memory] exclude: - { preset: linux-gcc, sanitize: memory } @@ -49,7 +48,6 @@ jobs: cmake --preset ${{ matrix.preset }} -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED=NO -DCPM_SOURCE_CACHE="${{ env.cpm-path }}" - -DZLIB_ENABLE_LFS=${{ matrix.lfs }} -DZLIB_SANITIZE=${{ matrix.sanitize }} - name: Build @@ -62,13 +60,12 @@ jobs: run: cmake --build --preset ${{ matrix.preset }} --config ${{ matrix.config }} -t install build-mobile: - name: ${{ matrix.preset }}-${{ matrix.config }}-${{ matrix.lfs }} + name: ${{ matrix.preset }}-${{ matrix.config }} strategy: fail-fast: false matrix: preset: [android, ios] config: [Debug, Release] - lfs: [OFF, ON] include: - { preset: android, os: ubuntu-latest } - { preset: ios, os: macos-latest } @@ -91,7 +88,6 @@ jobs: run: > cmake --preset ${{ matrix.preset }} -DCPM_SOURCE_CACHE=${{ env.cpm-path }} - -DZLIB_ENABLE_LFS=${{ matrix.lfs }} - name: Build run: cmake --build --preset ${{ matrix.preset }} --config ${{ matrix.config }} @@ -100,14 +96,13 @@ jobs: run: cmake --build --preset ${{ matrix.preset }} --config ${{ matrix.config }} -t install build-bsd: - name: ${{ matrix.preset }}-${{ matrix.arch }}-${{ matrix.config }}-${{ matrix.lfs }} + name: ${{ matrix.preset }}-${{ matrix.arch }}-${{ matrix.config }} strategy: fail-fast: false matrix: preset: [freebsd, netbsd, openbsd] arch: [arm64, x86_64] config: [Debug, Release] - lfs: [OFF, ON] exclude: - { preset: netbsd, arch: arm64 } include: @@ -133,7 +128,6 @@ jobs: -DCMAKE_BUILD_TYPE=${{ matrix.config }} \ -DCMAKE_INSTALL_PREFIX=${{ github.workspace }}/install \ -DCPM_SOURCE_CACHE=${{ env.cpm-path }} \ - -DZLIB_ENABLE_LFS=${{ matrix.lfs }} \ -DZLIB_INSTALL=ON \ -DZLIB_TEST=ON cmake --build build @@ -143,13 +137,12 @@ jobs: cmake --install build build-msys2: - name: msys2-${{ matrix.preset }}-${{ matrix.config }}-${{ matrix.lfs }} + name: msys2-${{ matrix.preset }}-${{ matrix.config }} strategy: fail-fast: false matrix: preset: [mingw64, mingw32, ucrt64, clang64] config: [Debug, Release] - lfs: [OFF, ON] include: - { preset: mingw64, env: x86_64 } - { preset: mingw32, env: i686 } @@ -181,7 +174,6 @@ jobs: -DCMAKE_BUILD_TYPE=${{ matrix.config }} -DCMAKE_INSTALL_PREFIX=${{ github.workspace }}/install -DCPM_SOURCE_CACHE=${{ env.cpm-path }} - -DZLIB_ENABLE_LFS=${{ matrix.lfs }} -DZLIB_INSTALL=ON -DZLIB_TEST=ON diff --git a/CMakeLists.txt b/CMakeLists.txt index 992a1e4..c6c15a3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.24) +cmake_minimum_required(VERSION 3.25) project( zlib-cmake @@ -8,13 +8,13 @@ project( LANGUAGES C ) -include(CheckIncludeFile) +include(CheckTypeSize) +include(CheckFunctionExists) include(FetchContent) include(cmake/GetCPM.cmake) include(cmake/UseSanitizer.cmake) # Custom options -option(ZLIB_ENABLE_LFS "Enable Large-File Support (LFS) on 32-bit system" OFF) option(ZLIB_INSTALL "Install zlib and CMake targets" OFF) option(ZLIB_TEST "Enable testing and build tests" OFF) @@ -35,7 +35,6 @@ CPMAddPackage( ) # Modify zlib source -check_include_file(unistd.h Z_HAVE_UNISTD_H) file(REMOVE ${zlib-source_SOURCE_DIR}/CMakeLists.txt ${zlib-source_SOURCE_DIR}/zconf.h @@ -130,11 +129,21 @@ if(WIN32 AND BUILD_SHARED_LIBS) target_compile_definitions(ZLIB PUBLIC ZLIB_DLL) endif() -if(ZLIB_ENABLE_LFS) - target_compile_definitions(ZLIB PUBLIC - _LARGEFILE64_SOURCE - _FILE_OFFSET_BITS=64 - ) +block() + +set(CMAKE_REQUIRED_DEFINITIONS _LARGEFILE64_SOURCE) +check_type_size(off64_t OFF64_T) + +if(HAVE_OFF64_T) + target_compile_definitions(ZLIB PUBLIC _LARGEFILE64_SOURCE) +endif() + +endblock() + +check_function_exists(fseeko HAVE_FSEEKO) + +if(NOT HAVE_FSEEKO) + target_compile_definitions(ZLIB PUBLIC NO_FSEEKO) endif() # Install zlib @@ -182,19 +191,34 @@ endif() if(ZLIB_TEST AND NOT CMAKE_CROSSCOMPILING) include(CTest) - add_executable(example ${zlib_SOURCE_DIR}/test/example.c) - target_link_libraries(example PRIVATE ZLIB::ZLIB) - add_test( - NAME test_example - COMMAND example - ) + function(add_test_files) + foreach(test_file IN LISTS ARGN) + message(STATUS "add ${test_file}") - add_executable(infcover ${zlib_SOURCE_DIR}/test/infcover.c) - target_link_libraries(infcover PRIVATE ZLIB::ZLIB) - add_test( - NAME test_infcover - COMMAND infcover - ) + add_executable(${test_file} ${zlib_SOURCE_DIR}/test/${test_file}.c) + target_link_libraries(${test_file} PRIVATE ZLIB::ZLIB) + add_test( + NAME test_${test_file} + COMMAND ${test_file} + ) + list(APPEND ZLIB_TEST_LIST "test_${test_file}") + + if(HAVE_OFF64_T) + add_executable(${test_file}64 ${zlib_SOURCE_DIR}/test/${test_file}.c) + target_link_libraries(${test_file}64 PRIVATE ZLIB::ZLIB) + target_compile_definitions(${test_file}64 PRIVATE _FILE_OFFSET_BITS=64) + add_test( + NAME test_${test_file}64 + COMMAND ${test_file}64 + ) + list(APPEND ZLIB_TEST_LIST "test_${test_file}64") + endif() + endforeach() + + return(PROPAGATE ZLIB_TEST_LIST) + endfunction() + + add_test_files(example infcover) add_executable(minigzip ${zlib_SOURCE_DIR}/test/minigzip.c) target_link_libraries(minigzip PRIVATE ZLIB::ZLIB) @@ -213,6 +237,29 @@ if(ZLIB_TEST AND NOT CMAKE_CROSSCOMPILING) COMMAND cmake -P ${CMAKE_CURRENT_BINARY_DIR}/$/test_minigzip.cmake COMMAND_EXPAND_LISTS ) + list(APPEND ZLIB_TEST_LIST test_minigzip) + + if(HAVE_OFF64_T) + add_executable(minigzip64 ${zlib_SOURCE_DIR}/test/minigzip.c) + target_link_libraries(minigzip64 PRIVATE ZLIB::ZLIB) + target_compile_definitions(minigzip64 PRIVATE _FILE_OFFSET_BITS=64) + file( + GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/$/test_minigzip64.cmake + CONTENT + "execute_process( + COMMAND cmake -E echo hello world + COMMAND $ + COMMAND $ -d + COMMAND_ERROR_IS_FATAL ANY + )" + ) + add_test( + NAME test_minigzip64 + COMMAND cmake -P ${CMAKE_CURRENT_BINARY_DIR}/$/test_minigzip64.cmake + COMMAND_EXPAND_LISTS + ) + list(APPEND ZLIB_TEST_LIST test_minigzip64) + endif() if(WIN32 AND BUILD_SHARED_LIBS) add_custom_command( @@ -220,11 +267,19 @@ if(ZLIB_TEST AND NOT CMAKE_CROSSCOMPILING) COMMAND ${CMAKE_COMMAND} -E copy_if_different $ $ COMMAND_EXPAND_LISTS ) + + if(HAVE_OFF64_T) + add_custom_command( + TARGET minigzip64 POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different $ $ + COMMAND_EXPAND_LISTS + ) + endif() endif() if(MSVC) cmake_path(GET CMAKE_C_COMPILER PARENT_PATH VS_PATH) - set_tests_properties(test_example test_infcover test_minigzip + set_tests_properties(${ZLIB_TEST_LIST} PROPERTIES ENVIRONMENT PATH=${VS_PATH} ) endif() diff --git a/README.md b/README.md index f7eb3ef..14d3e75 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,10 @@ Build [zlib](https://github.com/madler/zlib) using modern CMake and override the ## CMake Options -| Option | Default | Description | -| ----------------- | ------- | ------------------------------------------------ | -| `ZLIB_ENABLE_LFS` | `OFF` | Enable Large-File Support (LFS) on 32-bit system | -| `ZLIB_INSTALL` | `OFF` | Install zlib and CMake targets | -| `ZLIB_TEST` | `OFF` | Enable testing and build tests | +| Option | Default | Description | +| -------------- | ------- | ------------------------------ | +| `ZLIB_INSTALL` | `OFF` | Install zlib and CMake targets | +| `ZLIB_TEST` | `OFF` | Enable testing and build tests | - `CPM_SOURCE_CACHE` - Set to `/path/to/cache` to reuse downloaded source code