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

refactor: clean up CMakelists.txt #4779

Merged
merged 7 commits into from
Sep 26, 2024
Merged
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
80 changes: 60 additions & 20 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,30 @@ set_target_properties(${PROJECT_NAME} PROPERTIES SOVERSION ${VERSION_MAJOR})

set(CMAKE_C_FLAGS_DEBUGOPT "")

target_compile_options(${PROJECT_NAME} PRIVATE -pedantic -std=gnu99 -Wall -Wimplicit -Wunused -Wcomment -Wchar-subscripts
-Wuninitialized -Wshadow -Wcast-align -Wwrite-strings -Wno-deprecated-declarations -Wno-unknown-pragmas -Wformat-security
-Wno-missing-braces -Wsign-compare -Wno-strict-prototypes -Wa,--noexecstack
target_compile_options(${PROJECT_NAME} PRIVATE
-pedantic
-std=gnu99
-Wall
-Wcast-align
-Wchar-subscripts
-Wcomment
-Wformat-security
-Wimplicit
-Wshadow
-Wsign-compare
-Wuninitialized
-Wunused
-Wwrite-strings

# Assembler Options
-Wa,--noexecstack

# Suppressed Warnings
-Wno-deprecated-declarations
# GCC 4 fails to parse our macros with a "missing-braces" error
-Wno-missing-braces
jmayclin marked this conversation as resolved.
Show resolved Hide resolved
-Wno-strict-prototypes
-Wno-unknown-pragmas
)

if (S2N_WERROR_ALL)
Expand Down Expand Up @@ -445,11 +466,14 @@ target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURR
if (BUILD_TESTING)
enable_testing()

############################################################################
jmayclin marked this conversation as resolved.
Show resolved Hide resolved
################### build testlib (utility library) ########################
############################################################################

file(GLOB TESTLIB_SRC "tests/testlib/*.c")
file(GLOB EXAMPLES_SRC "docs/examples/*.c")
file(GLOB TESTLIB_HEADERS "tests/testlib/*.h" "tests/s2n_test.h")

add_library(testss2n STATIC ${TESTLIB_HEADERS} ${TESTLIB_SRC} ${EXAMPLES_SRC})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Headers shouldn't be included as part of the add_library directive. Generally the add_library directive is just used for things that generate object files. Header should then be added through target_include_directories, but that is already configured to add the tests/ path.

add_library(testss2n STATIC ${TESTLIB_SRC} ${EXAMPLES_SRC})
target_include_directories(testss2n PUBLIC tests)
target_compile_options(testss2n PRIVATE -std=gnu99)
target_link_libraries(testss2n PUBLIC ${PROJECT_NAME})
Expand All @@ -460,17 +484,18 @@ if (BUILD_TESTING)
endif()

if (S2N_INTERN_LIBCRYPTO)
# if libcrypto was interned, rewrite libcrypto symbols so use of internal functions will link correctly
# if libcrypto was interned, rewrite libcrypto symbols so use of internal
# functions will link correctly
add_custom_command(
TARGET testss2n POST_BUILD
COMMAND
objcopy --redefine-syms libcrypto.symbols lib/libtestss2n.a
)
endif()

#run unit tests
file (GLOB TEST_LD_PRELOAD "tests/LD_PRELOAD/*.c")
Copy link
Contributor Author

@jmayclin jmayclin Sep 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this because it is unused. I searched the entire codebase (not just CMakeLists.txt) and found that this library is only used when unit tests are run through make. See the PR overview for my thoughts on why we should totally delete this library.

add_library(allocator_overrides SHARED ${TEST_LD_PRELOAD})
############################################################################
########################## configure unit tests ############################
############################################################################

set(UNIT_TEST_ENVS S2N_DONT_MLOCK=1)
if (TSAN OR ASAN)
Expand Down Expand Up @@ -500,12 +525,16 @@ if (BUILD_TESTING)
endif()
message(STATUS "Running tests with environment: ${UNIT_TEST_ENVS}")

############################################################################
############################ build unit tests ##############################
############################################################################

file(GLOB UNITTESTS_SRC "tests/unit/*.c")
foreach(test_case ${UNITTESTS_SRC})
string(REGEX REPLACE ".+\\/(.+)\\.c" "\\1" test_case_name ${test_case})
# NAME_WE: name without extension
get_filename_component(test_case_name ${test_case} NAME_WE)
jmayclin marked this conversation as resolved.
Show resolved Hide resolved

add_executable(${test_case_name} ${test_case})
target_include_directories(${test_case_name} PRIVATE ./)
target_link_libraries(${test_case_name} PRIVATE testss2n)
lrstewart marked this conversation as resolved.
Show resolved Hide resolved
if (S2N_INTERN_LIBCRYPTO)
# if libcrypto was interned, rewrite libcrypto symbols so use of internal functions will link correctly
Expand All @@ -516,19 +545,28 @@ if (BUILD_TESTING)
)
endif()
target_compile_options(${test_case_name} PRIVATE
-Wall -Wimplicit -Wunused -Wcomment -Wchar-subscripts -Wuninitialized
-Wshadow -Wcast-align -Wwrite-strings -Wformat-security
-Wno-deprecated-declarations -Wno-unknown-pragmas -Wno-deprecated
-fPIC -D_POSIX_C_SOURCE=200809L -std=gnu99)
-Wall -Wimplicit -Wunused -Wcomment -Wchar-subscripts -Wuninitialized
-Wshadow -Wcast-align -Wwrite-strings -Wformat-security
-Wno-deprecated-declarations -Wno-unknown-pragmas -Wno-deprecated -std=gnu99
lrstewart marked this conversation as resolved.
Show resolved Hide resolved
)
if (S2N_LTO)
target_compile_options(${test_case_name} PRIVATE -flto)
endif()
add_test(NAME ${test_case_name} COMMAND $<TARGET_FILE:${test_case_name}> WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests/unit)

add_test(
NAME ${test_case_name}
COMMAND $<TARGET_FILE:${test_case_name}>
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests/unit
)
set_property(TEST ${test_case_name} PROPERTY LABELS "unit")
set_property(TEST ${test_case_name} PROPERTY ENVIRONMENT ${UNIT_TEST_ENVS})

endforeach(test_case)

############################################################################
######################### build utility binaries ###########################
############################################################################

add_executable(s2nc "bin/s2nc.c" "bin/echo.c" "bin/https.c" "bin/common.c")
target_link_libraries(s2nc ${PROJECT_NAME})
target_compile_options(s2nc PRIVATE -std=gnu99)
Expand Down Expand Up @@ -603,7 +641,9 @@ if (BUILD_TESTING)
file(GLOB TESTLIB_SRC "tests/testlib/*.c")
file(GLOB TESTLIB_HEADERS "tests/testlib/*.h" "tests/s2n_test.h")

add_library(fuzztest SHARED ${TESTLIB_HEADERS} ${TESTLIB_SRC})
# This must be a shared object so that symbols can be overridden by the
# fuzz test specific LD_PRELOAD libraries.
add_library(fuzztest SHARED ${TESTLIB_SRC})
target_include_directories(fuzztest PUBLIC tests)
target_link_libraries(fuzztest PUBLIC ${PROJECT_NAME})

Expand Down Expand Up @@ -650,14 +690,15 @@ if (BUILD_TESTING)
get_filename_component(TEST_NAME ${src} NAME_WE)

add_executable(${TEST_NAME} ${src})
target_include_directories(${TEST_NAME} PRIVATE ./)

target_compile_options(${TEST_NAME} PRIVATE
-g -Wno-unknown-pragmas -Wno-unused-result
)
target_link_options(${TEST_NAME} PRIVATE
-fsanitize=fuzzer -lstdc++
)
target_link_libraries(${TEST_NAME} PRIVATE
fuzztest
-fsanitize=fuzzer -lstdc++
)

# Set the output directory for the fuzzing binaries
Expand Down Expand Up @@ -701,7 +742,6 @@ install(
RUNTIME DESTINATION bin COMPONENT Runtime
)


configure_file("cmake/${PROJECT_NAME}-config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake"
@ONLY)
Expand Down
Loading