Skip to content

Commit

Permalink
Fix header-only tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cschreib committed Sep 1, 2023
1 parent 8cb7367 commit 4cd9143
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 68 deletions.
71 changes: 54 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,24 @@ else()
set(SNITCH_SOURCES ${SNITCH_SOURCES_INDIVIDUAL})
endif()

function(configure_snitch_exports TARGET)
if (BUILD_SHARED_LIBS)
target_compile_definitions(${TARGET} PRIVATE SNITCH_EXPORTS)
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" OR MINGW)
# Nothing to do; default is already to hide symbols unless exported.
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR
CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR
CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
# Set default visibility to "hidden" so only exported symbols are visible.
target_compile_options(${TARGET} PRIVATE -fvisibility=hidden)
target_compile_options(${TARGET} PRIVATE -fvisibility-inlines-hidden)
endif()
endif()
endfunction()

if (NOT SNITCH_HEADER_ONLY)
# Build as a standard library (static or dynamic) with header.
add_library(snitch
${SNITCH_INCLUDES}
${SNITCH_SOURCES})

add_library(snitch ${SNITCH_INCLUDES} ${SNITCH_SOURCES})
add_library(snitch::snitch ALIAS snitch)
set_target_properties(snitch PROPERTIES EXPORT_NAME snitch::snitch)

Expand All @@ -129,18 +141,7 @@ if (NOT SNITCH_HEADER_ONLY)
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>)

if (BUILD_SHARED_LIBS)
target_compile_definitions(snitch PRIVATE SNITCH_EXPORTS)
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" OR MINGW)
# Nothing to do; default is already to hide symbols unless exported.
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR
CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR
CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
# Set default visibility to "hidden" so only exported symbols are visible.
target_compile_options(snitch PRIVATE -fvisibility=hidden)
target_compile_options(snitch PRIVATE -fvisibility-inlines-hidden)
endif()
endif()
configure_snitch_exports(snitch)

install(
FILES ${SNITCH_INCLUDES}
Expand All @@ -163,10 +164,10 @@ else()
add_library(snitch::snitch ALIAS snitch)
set_target_properties(snitch PROPERTIES EXPORT_NAME snitch::snitch)

target_compile_features(snitch INTERFACE cxx_std_20)
target_include_directories(snitch INTERFACE
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>)
target_compile_features(snitch INTERFACE cxx_std_20)

install(
FILES ${PROJECT_BINARY_DIR}/snitch/snitch_all.hpp
Expand Down Expand Up @@ -196,5 +197,41 @@ install(FILES
# Setup tests
if (SNITCH_DO_TEST)
enable_testing()

# We need to use a different snitch configuration for tests, so we can't reuse
# the library that was built above. Create a copy.
function(configure_snitch_for_tests TARGET CHOSEN_INTERFACE)
target_compile_definitions(${TARGET} ${CHOSEN_INTERFACE}
SNITCH_MAX_TEST_CASES=200
SNITCH_MAX_EXPR_LENGTH=128
SNITCH_MAX_MESSAGE_LENGTH=128
SNITCH_MAX_TEST_NAME_LENGTH=128
SNITCH_MAX_CAPTURE_LENGTH=128
SNITCH_DEFINE_MAIN=0)
endfunction()

if (NOT SNITCH_HEADER_ONLY)
add_library(snitch-testlib ${SNITCH_INCLUDES} ${SNITCH_SOURCES})
add_dependencies(snitch-testlib snitch)

target_compile_features(snitch-testlib PUBLIC cxx_std_20)
target_include_directories(snitch-testlib PUBLIC
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/src
${PROJECT_BINARY_DIR})

configure_snitch_exports(snitch-testlib)
configure_snitch_for_tests(snitch-testlib PUBLIC)
else()
add_library(snitch-testlib INTERFACE ${PROJECT_BINARY_DIR}/snitch/snitch_all.hpp)
add_dependencies(snitch-testlib snitch)

target_compile_features(snitch-testlib INTERFACE cxx_std_20)
target_include_directories(snitch-testlib INTERFACE ${PROJECT_BINARY_DIR})

configure_snitch_for_tests(snitch-testlib INTERFACE)
target_compile_definitions(snitch-testlib INTERFACE SNITCH_TEST_HEADER_ONLY)
endif()

add_subdirectory(tests)
endif()
51 changes: 0 additions & 51 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,57 +37,6 @@ function(add_platform_definitions TARGET)
endif()
endfunction()

# We need to use a different snitch configuration for tests, so we can't reuse
# the library that was built at the higher level. We need to create a local copy
# here.
function(configure_snitch_for_tests CHOSEN_INTERFACE)
target_compile_definitions(snitch-testlib ${CHOSEN_INTERFACE}
SNITCH_MAX_TEST_CASES=200
SNITCH_MAX_EXPR_LENGTH=128
SNITCH_MAX_MESSAGE_LENGTH=128
SNITCH_MAX_TEST_NAME_LENGTH=128
SNITCH_MAX_CAPTURE_LENGTH=128
SNITCH_DEFINE_MAIN=0)
endfunction()

if (NOT SNITCH_HEADER_ONLY)
# Build as a standard library (static or dynamic) with header.
add_library(snitch-testlib
${SNITCH_INCLUDES}
${SNITCH_SOURCES})
add_dependencies(snitch-testlib snitch)

target_compile_features(snitch-testlib PUBLIC cxx_std_20)
target_include_directories(snitch-testlib PUBLIC
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/src
${PROJECT_BINARY_DIR})

if (BUILD_SHARED_LIBS)
target_compile_definitions(snitch-testlib PRIVATE SNITCH_EXPORTS)
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" OR MINGW)
# Nothing to do; default is already to hide symbols unless exported.
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR
CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR
CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
# Set default visibility to "hidden" so only exported symbols are visible.
target_compile_options(snitch-testlib PRIVATE -fvisibility=hidden)
target_compile_options(snitch-testlib PRIVATE -fvisibility-inlines-hidden)
endif()
endif()

configure_snitch_for_tests(PUBLIC)
else()
add_library(snitch-testlib INTERFACE ${PROJECT_BINARY_DIR}/snitch/snitch_all.hpp)
add_dependencies(snitch-testlib snitch)

target_compile_features(snitch-testlib INTERFACE cxx_std_20)
target_include_directories(snitch-testlib INTERFACE ${PROJECT_BINARY_DIR})

configure_snitch_for_tests(INTERFACE)
target_compile_definitions(snitch-testlib INTERFACE SNITCH_TEST_HEADER_ONLY)
endif()

set(TEST_UTILITY_FILES
${PROJECT_SOURCE_DIR}/tests/testing.cpp
${PROJECT_SOURCE_DIR}/tests/testing_event.cpp
Expand Down

0 comments on commit 4cd9143

Please sign in to comment.