Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake: new option BUILD_NO_STDIO to enable MINIZ_NO_STDIO #316

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 31 additions & 23 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ endif()
set(MINIZ_API_VERSION 3)
set(MINIZ_MINOR_VERSION 0)
set(MINIZ_PATCH_VERSION 2)
set(MINIZ_VERSION
set(MINIZ_VERSION
${MINIZ_API_VERSION}.${MINIZ_MINOR_VERSION}.${MINIZ_PATCH_VERSION})

if(CMAKE_BUILD_TYPE STREQUAL "")
Expand All @@ -42,6 +42,7 @@ option(BUILD_FUZZERS "Build fuzz targets" OFF)
option(AMALGAMATE_SOURCES "Amalgamate sources into miniz.h/c" OFF)
option(BUILD_HEADER_ONLY "Build a header-only version" OFF)
option(BUILD_SHARED_LIBS "Build shared library instead of static" OFF)
option(BUILD_NO_STDIO" Build a without stdio version" OFF)
option(BUILD_TESTS "Build tests" ${MINIZ_STANDALONE_PROJECT})
option(INSTALL_PROJECT "Install project" ${MINIZ_STANDALONE_PROJECT})

Expand Down Expand Up @@ -85,14 +86,14 @@ if(AMALGAMATE_SOURCES)
"${AMAL_MINIZ_C}" "\n#endif // MINIZ_HEADER_FILE_ONLY\n")
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/amalgamation/miniz.h "${AMAL_MINIZ_H}")
add_library(${PROJECT_NAME} INTERFACE)

# Might not be a good idea to force this on the library user
# as it could bloat the global namespace
# https://github.com/libevent/libevent/issues/460
# target_compile_definitions(${PROJECT_NAME}
# target_compile_definitions(${PROJECT_NAME}
# INTERFACE $<$<C_COMPILER_ID:GNU>:_GNU_SOURCE>)
set_property(TARGET ${PROJECT_NAME} APPEND

set_property(TARGET ${PROJECT_NAME} APPEND
PROPERTY INTERFACE_INCLUDE_DIRECTORIES
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/amalgamation>
$<INSTALL_INTERFACE:include>
Expand All @@ -111,7 +112,7 @@ if(AMALGAMATE_SOURCES)
endif(BUILD_HEADER_ONLY)

set(INSTALL_HEADERS ${CMAKE_CURRENT_BINARY_DIR}/amalgamation/miniz.h)

file(GLOB_RECURSE ZIP_FILES RELATIVE "${CMAKE_CURRENT_BINARY_DIR}/amalgamation" "${CMAKE_CURRENT_BINARY_DIR}/amalgamation/*")
file(GLOB_RECURSE ZIP_FILES2 RELATIVE "${CMAKE_SOURCE_DIR}" "${CMAKE_SOURCE_DIR}/examples/*")
list(APPEND ZIP_FILES ${ZIP_FILES2})
Expand All @@ -131,7 +132,7 @@ if(AMALGAMATE_SOURCES)
DEPENDS ${ZIP_FILES}
COMMENT "Zipping to ${CMAKE_CURRENT_BINARY_DIR}/miniz.zip."
)

add_custom_target(
create_zip ALL
DEPENDS "${ZIP_OUT_FN}"
Expand All @@ -152,20 +153,20 @@ else(AMALGAMATE_SOURCES)

set_property(TARGET ${PROJECT_NAME} PROPERTY VERSION ${MINIZ_VERSION})
set_property(TARGET ${PROJECT_NAME} PROPERTY SOVERSION ${MINIZ_API_VERSION})

target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<INSTALL_INTERFACE:include>
)

file(GLOB INSTALL_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
list(APPEND
INSTALL_HEADERS ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}_export.h)
endif(AMALGAMATE_SOURCES)

if(NOT BUILD_HEADER_ONLY)
target_compile_definitions(${PROJECT_NAME}
target_compile_definitions(${PROJECT_NAME}
PRIVATE $<$<C_COMPILER_ID:GNU>:_GNU_SOURCE>)

# pkg-config file
Expand All @@ -178,6 +179,10 @@ if(NOT BUILD_HEADER_ONLY)
endif()
endif()

if(BUILD_NO_STDIO)
target_compile_definitions(${PROJECT_NAME} PRIVATE MINIZ_NO_STDIO)
endif()

set_property(TARGET ${PROJECT_NAME} PROPERTY
INTERFACE_${PROJECT_NAME}_MAJOR_VERSION ${MINIZ_API_VERSION})
set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY
Expand Down Expand Up @@ -242,8 +247,10 @@ if(BUILD_EXAMPLES)

add_executable(example1 ${EXAMPLE1_SRC_LIST})
target_link_libraries(example1 miniz)
add_executable(example2 ${EXAMPLE2_SRC_LIST})
target_link_libraries(example2 miniz)
if(NOT BUILD_NO_STDIO)
add_executable(example2 ${EXAMPLE2_SRC_LIST})
target_link_libraries(example2 miniz)
endif()
add_executable(example3 ${EXAMPLE3_SRC_LIST})
target_link_libraries(example3 miniz)
add_executable(example4 ${EXAMPLE4_SRC_LIST})
Expand Down Expand Up @@ -298,17 +305,18 @@ if(BUILD_FUZZERS)
endif()

if(BUILD_TESTS)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED YES)

add_executable(catch_tests tests/main.cpp
tests/catch_amalgamated.cpp)
target_link_libraries(catch_tests miniz)

enable_testing()

add_test(NAME catch_tests
COMMAND $<TARGET_FILE:catch_tests>)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED YES)

add_executable(catch_tests tests/main.cpp tests/catch_amalgamated.cpp)
if(BUILD_NO_STDIO)
target_compile_definitions(catch_tests PRIVATE -DMINIZ_NO_STDIO)
endif()
target_link_libraries(catch_tests miniz)

enable_testing()

add_test(NAME catch_tests COMMAND $<TARGET_FILE:catch_tests>)
endif()

set(INCLUDE_INSTALL_DIR "include")
Expand Down
6 changes: 5 additions & 1 deletion tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

#ifdef _WIN32
#define unlink _unlink
#else
#include <unistd.h>
#endif

#ifndef MINIZ_NO_STDIO
bool create_test_zip()
{
unlink("test.zip");
Expand Down Expand Up @@ -79,6 +82,7 @@ TEST_CASE("Zip writer tests")
}
}
}
#endif

TEST_CASE("Tinfl / tdefl tests")
{
Expand Down Expand Up @@ -124,4 +128,4 @@ TEST_CASE("Tinfl / tdefl tests")
REQUIRE(decomp_len == uncomp_len);
REQUIRE(memcmp(decomp_buf, p, uncomp_len) == 0);
}
}
}
Loading