Skip to content

Commit 4c3b0cc

Browse files
committed
Hide all symbols by default
This prevents unmarked symbols, which we consider to be internals, from being available on the dynamic library's dynamic symbol table (EXPORT table on Windows). Furthermore we split out a static library. This allows us to expose the library internals to unit tests only, through this static library. This works because symbol hiding only affects dynamic linking, not static linking.
1 parent 3eef825 commit 4c3b0cc

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
cmake_minimum_required(VERSION 3.1)
22

3+
if(NOT CMAKE_VERSION VERSION_LESS "3.3")
4+
# Don't ignore visibility related properties for non-SHARED targets
5+
cmake_policy(SET CMP0063 NEW)
6+
endif()
7+
38
project(Cucumber-Cpp)
49

510
option(BUILD_SHARED_LIBS "Generate shared libraries" OFF)

src/CMakeLists.txt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,28 @@ if(CMAKE_EXTRA_GENERATOR OR MSVC_IDE)
4444
list(APPEND CUKE_SOURCES ${CUKE_HEADERS})
4545
endif()
4646

47+
# Library for unit tests relying on internals
48+
add_library(cucumber-cpp-internal STATIC ${CUKE_SOURCES})
49+
4750
add_library(cucumber-cpp-nomain ${CUKE_SOURCES})
4851
add_library(cucumber-cpp ${CUKE_SOURCES} main.cpp)
4952

50-
set_target_properties(cucumber-cpp cucumber-cpp-nomain PROPERTIES
51-
DEFINE_SYMBOL cucumber_cpp_EXPORTS
53+
set_target_properties(
54+
cucumber-cpp-internal
55+
cucumber-cpp
56+
cucumber-cpp-nomain
57+
PROPERTIES
58+
DEFINE_SYMBOL cucumber_cpp_EXPORTS
59+
CXX_VISIBILITY_PRESET hidden
60+
VISIBILITY_INLINES_HIDDEN ON
5261
)
5362

5463
generate_export_header(cucumber-cpp
5564
EXPORT_FILE_NAME "cucumber-cpp/internal/CukeExport.hpp"
5665
)
5766

5867
foreach(TARGET
68+
cucumber-cpp-internal
5969
cucumber-cpp-nomain
6070
cucumber-cpp
6171
)
@@ -75,6 +85,11 @@ foreach(TARGET
7585
json_spirit.header
7686
${CUKE_EXTRA_PRIVATE_LIBRARIES}
7787
)
88+
# Don't export or import symbols for statically linked libraries
89+
get_property(type TARGET ${TARGET} PROPERTY TYPE)
90+
if(NOT "${type}" MATCHES "^(SHARED|MODULE)_LIBRARY$")
91+
target_compile_definitions(${TARGET} PUBLIC CUCUMBER_CPP_STATIC_DEFINE)
92+
endif()
7893
if(MINGW)
7994
target_link_libraries(${TARGET}
8095
PRIVATE

tests/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function(cuke_add_driver_test TEST_FILE)
22
get_filename_component(TEST_NAME ${TEST_FILE} NAME)
33
message(STATUS "Adding " ${TEST_NAME})
44
add_executable(${TEST_NAME} ${TEST_FILE}.cpp)
5-
target_link_libraries(${TEST_NAME} PRIVATE cucumber-cpp-nomain ${ARGN})
5+
target_link_libraries(${TEST_NAME} PRIVATE cucumber-cpp-internal ${ARGN})
66
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
77
endfunction()
88

@@ -11,7 +11,7 @@ if(TARGET GMock::Main)
1111
get_filename_component(TEST_NAME ${TEST_FILE} NAME)
1212
message(STATUS "Adding " ${TEST_NAME})
1313
add_executable(${TEST_NAME} ${TEST_FILE}.cpp)
14-
target_link_libraries(${TEST_NAME} PRIVATE cucumber-cpp-nomain ${ARGN} GMock::Main)
14+
target_link_libraries(${TEST_NAME} PRIVATE cucumber-cpp-internal ${ARGN} GMock::Main)
1515
gtest_add_tests(${TEST_NAME} "" ${TEST_FILE}.cpp)
1616
# Run all tests in executable at once too. This ensures that the used fixtures get tested
1717
# properly too. Additionally gather the output in jUnit compatible output for CI.

0 commit comments

Comments
 (0)