Symbol visibility tests

This commit is contained in:
2024-05-10 13:47:37 -07:00
parent f5920ba6c7
commit ba2b76cb13
11 changed files with 185 additions and 13 deletions

View File

@@ -28,7 +28,7 @@ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
endif()
add_compile_options(-fdata-sections -ffunction-sections -Wswitch-enum
-Werror=switch-enum -fPIC)
-Werror=switch-enum -fvisibility=hidden -fPIC)
set(full_relro_flags "-pie;LINKER:-z,relro,-z,now,-z,noexecstack")
cmake_push_check_state()
@@ -60,7 +60,8 @@ set(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES "")
add_subdirectory(third_party)
add_executable(versioned_map_main VersionedMap.cpp RootSet.cpp)
add_executable(versioned_map_main VersionedMap.cpp RootSet.cpp
$<TARGET_OBJECTS:xxhash>)
target_include_directories(versioned_map_main
PRIVATE ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(versioned_map_main PRIVATE nanobench xxhash)
@@ -71,11 +72,48 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug")
target_link_options(versioned_map_main PRIVATE -fsanitize=address,undefined)
endif()
add_library(versioned_map VersionedMap.cpp RootSet.cpp)
target_link_libraries(versioned_map PRIVATE xxhash)
target_compile_options(versioned_map PRIVATE -fno-exceptions)
target_include_directories(versioned_map PUBLIC ${CMAKE_SOURCE_DIR}/include)
set_target_properties(versioned_map PROPERTIES LINKER_LANGUAGE C)
add_library(${PROJECT_NAME}-object OBJECT VersionedMap.cpp RootSet.cpp)
target_compile_options(${PROJECT_NAME}-object PRIVATE -fno-exceptions -fno-rtti)
target_include_directories(${PROJECT_NAME}-object
PRIVATE ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(${PROJECT_NAME}-object PRIVATE xxhash)
add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.o
COMMAND ld -r $<TARGET_OBJECTS:${PROJECT_NAME}-object>
$<TARGET_OBJECTS:xxhash> -o ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.o
DEPENDS $<TARGET_OBJECTS:${PROJECT_NAME}-object> $<TARGET_OBJECTS:xxhash>
COMMAND_EXPAND_LISTS)
add_library(${PROJECT_NAME} SHARED ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.o)
set_target_properties(
${PROJECT_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY
"${CMAKE_BINARY_DIR}/versioned-map")
if(NOT CMAKE_BUILD_TYPE STREQUAL Debug)
set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE C)
endif()
if(HAS_VERSION_SCRIPT)
target_link_options(${PROJECT_NAME} PRIVATE
LINKER:--version-script=${CMAKE_SOURCE_DIR}/linker.map)
endif()
add_library(${PROJECT_NAME}-static STATIC ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.o)
if(NOT CMAKE_BUILD_TYPE STREQUAL Debug)
set_target_properties(${PROJECT_NAME}-static PROPERTIES LINKER_LANGUAGE C)
endif()
if(NOT APPLE)
add_custom_command(
TARGET ${PROJECT_NAME}-static
POST_BUILD
COMMAND
${CMAKE_OBJCOPY}
--keep-global-symbols=${CMAKE_SOURCE_DIR}/symbol-exports.txt
$<TARGET_FILE:${PROJECT_NAME}-static> || echo
"Proceeding with all symbols global in static library")
endif()
include(CTest)
@@ -101,4 +139,30 @@ if(BUILD_TESTING)
target_compile_options(rootset_test_tsan PRIVATE -fsanitize=thread -UNDEBUG)
target_link_options(rootset_test_tsan PRIVATE -fsanitize=thread)
add_test(NAME rootset_test_tsan COMMAND rootset_test)
# symbol visibility tests
if(NOT CMAKE_BUILD_TYPE STREQUAL Debug)
if(APPLE)
set(symbol_exports ${CMAKE_SOURCE_DIR}/apple-symbol-exports.txt)
set(symbol_imports ${CMAKE_SOURCE_DIR}/apple-symbol-imports.txt)
else()
set(symbol_exports ${CMAKE_SOURCE_DIR}/symbol-exports.txt)
if(CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64)
set(symbol_imports ${CMAKE_SOURCE_DIR}/aarch64-symbol-imports.txt)
else()
set(symbol_imports ${CMAKE_SOURCE_DIR}/symbol-imports.txt)
endif()
endif()
add_test(
NAME shared_symbols
COMMAND
${CMAKE_SOURCE_DIR}/test_symbols.sh $<TARGET_FILE:${PROJECT_NAME}>
${symbol_exports} ${symbol_imports})
add_test(
NAME static_symbols
COMMAND
${CMAKE_SOURCE_DIR}/test_symbols.sh
$<TARGET_FILE:${PROJECT_NAME}-static> ${symbol_exports}
${symbol_imports})
endif()
endif()