From b8483a16017057b6b8e2ad3dbe54ac02621329ca Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Fri, 22 Jul 2022 20:11:46 +0100 Subject: [PATCH] Refactore the compiler/linker options to add asan only on executables --- CMakeLists.txt | 3 --- builder | 21 +++++++-------------- core/lib/serene/CMakeLists.txt | 12 +++++------- libserene/lib/CMakeLists.txt | 24 ++---------------------- serene-repl/CMakeLists.txt | 12 ++++++++++++ serenec/CMakeLists.txt | 8 ++++++-- 6 files changed, 32 insertions(+), 48 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a5fd66a..98fba15 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -110,7 +110,6 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) $<$:-fno-omit-frame-pointer> $<$:-fomit-frame-pointer> - $<$:-fsanitize=address> $<$:-O3> $<$:-fmerge-all-constants> @@ -120,8 +119,6 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) # We enforce the lld linker -fuse-ld=lld -Wl,-gc-sections - $<$:-fsanitize-address-globals-dead-stripping> - $<$:-fsanitize=address> $<$:-s> # Do not link against shared libraries #--static diff --git a/builder b/builder index c9bbcae..5e14023 100755 --- a/builder +++ b/builder @@ -156,8 +156,8 @@ function build-tidy() { ## Builds the project using clang-tidy (It takes longer function build-release() { ## Builds the project in "Release" mode clean pushed_build - cmake -G Ninja -DCMAKE_BUILD_TYPE=Release "$ROOT_DIR" - cmake --build . + cmake -G Ninja -DCMAKE_BUILD_TYPE=Release "${CMAKEARGS[@]}" "$ROOT_DIR" + cmake --build . --config Release popd_build } @@ -180,18 +180,15 @@ function clean() { ## Cleans up the source dir and removes the build } function run() { ## Runs `serenec` and passes all the given aruguments to it - LD_PRELOAD=$(clang -print-file-name=libclang_rt.asan-x86_64.so) \ - "$BUILD_DIR"/serenec/serenec "$@" + "$BUILD_DIR"/serenec/serenec "$@" } function lldb-run() { ## Runs `serenec` under lldb - LD_PRELOAD=$(clang -print-file-name=libclang_rt.asan-x86_64.so) \ - lldb -- "$BUILD_DIR"/serenec/serenec "$@" + lldb -- "$BUILD_DIR"/serenec/serenec "$@" } function repl() { ## Runs `serene-repl` and passes all the given aruguments to it - LD_PRELOAD=$(clang -print-file-name=libclang_rt.asan-x86_64.so) \ - "$BUILD_DIR"/serene-repl/serene-repl "$@" + "$BUILD_DIR"/serene-repl/serene-repl "$@" } function memcheck-serene() { ## Runs `valgrind` to check `serenec` birany @@ -209,13 +206,11 @@ function tests() { ## Runs all the test cases local test_file="$BUILD_DIR/$proj/tests/${proj}Tests" if [[ -f "$test_file" ]]; then - LD_PRELOAD=$(clang -print-file-name=libclang_rt.asan-x86_64.so) \ - eval "$test_file ${*:2}" + eval "$test_file ${*:2}" fi done else - LD_PRELOAD=$(clang -print-file-name=libclang_rt.asan-x86_64.so) \ - eval "$BUILD_DIR/$1/tests/$1Tests ${*:2}" + eval "$BUILD_DIR/$1/tests/$1Tests ${*:2}" fi } @@ -303,8 +298,6 @@ function setup() { ## Setup the working directory and make it ready for developm else error "Python is required to setup pre-commit" fi - # rm -rfv "$ME/.git/hooks/pre-commit" - # ln -s "$ME/scripts/pre-commit" "$ME/.git/hooks/pre-commit" } function setup-dev() { ## Setup the container like env to build/develop Serene (requires sudo access) diff --git a/core/lib/serene/CMakeLists.txt b/core/lib/serene/CMakeLists.txt index 71900c1..9b9058f 100644 --- a/core/lib/serene/CMakeLists.txt +++ b/core/lib/serene/CMakeLists.txt @@ -23,7 +23,7 @@ if(NOT APPLE) endif() set(SOURCES core.cpp) -add_library(core STATIC +add_library(core OBJECT ${SOURCES}) #add_custom_target(serene.core) @@ -41,14 +41,12 @@ set_target_properties(core PROPERTIES LINK_WHAT_YOU_USE TRUE CXX_INCLUDE_WHAT_YOU_USE "${iwyu_path}" C_INCLUDE_WHAT_YOU_USE "${iwyu_path}" - # LTO support + # LTO support we need the actual object file + # LTO will export them to llvm IR INTERPROCEDURAL_OPTIMIZATION FALSE) -target_compile_options(core PUBLIC -fsanitize=address -static-libsan --static) -target_link_options(core PUBLIC -fsanitize=address -static-libsan --static) - -get_target_property(MFLAGS core COMPILE_OPTIONS) -message(STATUS "------ ${MFLAGS}") +target_compile_options(core PRIVATE --static) +target_link_options(core PRIVATE --static) if(SERENE_ENABLE_TIDY) set_target_properties(core PROPERTIES CXX_CLANG_TIDY ${CLANG_TIDY_PATH}) diff --git a/libserene/lib/CMakeLists.txt b/libserene/lib/CMakeLists.txt index b00549f..254bce7 100644 --- a/libserene/lib/CMakeLists.txt +++ b/libserene/lib/CMakeLists.txt @@ -47,35 +47,15 @@ set_target_properties(serene PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE) target_compile_options(serene - PRIVATE - $<$:-static-libsan>) + PRIVATE) target_link_options(serene - PRIVATE - $<$:-shared-libsan>) - + PRIVATE) if(SERENE_ENABLE_TIDY) set_target_properties(serene PROPERTIES CXX_CLANG_TIDY ${CLANG_TIDY_PATH}) endif() -# Do we need to build serene as a shared lib? default is "yes" -if(SERENE_SHARED_LIB) - - # We need to use libsan as a shared lib on debug mode. The - # target executable has to be built with `-fsanitize=address` - # as well and it has to run with: - # LD_PRELOAD=$(clang -print-file-name=libclang_rt.asan-x86_64.so) - target_compile_options(serene PRIVATE - $<$:-shared-libsan> - ) - target_link_options( - serene PRIVATE - $<$:-shared-libsan> - ) -endif() - - if (CPP_20_SUPPORT) target_compile_features(serene PUBLIC cxx_std_20) else() diff --git a/serene-repl/CMakeLists.txt b/serene-repl/CMakeLists.txt index 8ccb208..4f70997 100644 --- a/serene-repl/CMakeLists.txt +++ b/serene-repl/CMakeLists.txt @@ -42,6 +42,18 @@ target_link_libraries(serene-repl Serene::lib ) +target_compile_options(serene-repl + PRIVATE + $<$:-fsanitize=address> + $<$:-static-libsan> + ) + +target_link_options(serene-repl + PRIVATE + $<$:-fsanitize=address> + $<$:-static-libsan> + ) + target_include_directories(serene-repl PRIVATE ${PROJECT_BINARY_DIR}) target_include_directories(serene-repl PRIVATE ${INCLUDE_DIR}) diff --git a/serenec/CMakeLists.txt b/serenec/CMakeLists.txt index 31a2d5f..0e9fd56 100644 --- a/serenec/CMakeLists.txt +++ b/serenec/CMakeLists.txt @@ -27,11 +27,15 @@ set_target_properties(serenec PROPERTIES target_compile_options(serenec PRIVATE - $<$:-static-libsan>) + $<$:-fsanitize=address> + $<$:-static-libsan> + ) target_link_options(serenec PRIVATE - $<$:-shared-libsan>) + $<$:-fsanitize=address> + $<$:-static-libsan> + ) add_dependencies(serenec Serene::core)