Skip to content

Commit

Permalink
build: restore pie option
Browse files Browse the repository at this point in the history
Signed-off-by: He Xian <[email protected]>
  • Loading branch information
hexian000 committed Nov 22, 2024
1 parent fdbff60 commit 5bc4feb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
23 changes: 14 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ message(STATUS "Project version: ${PROJECT_VERSION_STRING}")

include(CTest)

option(ENABLE_SANITIZERS "Enable address, leak, undefined sanitizer in debug build" ON)
option(ENABLE_SANITIZERS "Enable address, leak, undefined sanitizer" OFF)
option(BUILD_STATIC "Build a static executable" OFF)
option(BUILD_PIE "Build a position independent executable" OFF)
option(ENABLE_MIMALLOC "Enable mimalloc" OFF)
option(FORCE_POSIX "Build the core features using POSIX.1 APIs only" OFF)
option(LINK_STATIC_LIBS "Link against static libraries" OFF)
Expand All @@ -62,46 +63,50 @@ option(ENABLE_RULESET "Enable ruleset scripting (requires Lua)" ON)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
if(BUILD_PIE)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()

if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffile-prefix-map=${PROJECT_SOURCE_DIR}/=")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -s")
set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} -s")
if(ENABLE_SANITIZERS)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address,leak,undefined -fstack-protector-all")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address,leak,undefined -static-libasan -static-liblsan -static-libubsan")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -fsanitize=address,leak,undefined -fstack-protector-all")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=address,leak,undefined -static-libasan -static-liblsan -static-libubsan")
endif()
if(CMAKE_INTERPROCEDURAL_OPTIMIZATION)
set(CMAKE_C_COMPILE_OPTIONS_IPO -flto=1 -fno-fat-lto-objects -flto-partition=one -fuse-linker-plugin)
endif()
if(CMAKE_POSITION_INDEPENDENT_CODE AND BUILD_STATIC)
if(BUILD_STATIC AND BUILD_PIE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-pie")
elseif(BUILD_STATIC)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
elseif(CMAKE_POSITION_INDEPENDENT_CODE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie")
endif()
if(LINK_STATIC_LIBS)
if(NOT BUILD_STATIC AND LINK_STATIC_LIBS)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc")
endif()
elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffile-compilation-dir=${PROJECT_SOURCE_DIR}")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -s")
set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} -s")
if(ENABLE_SANITIZERS)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address,leak,undefined -fstack-protector-all")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address,leak,undefined -static-libsan")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -fsanitize=address,leak,undefined -fstack-protector-all")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=address,leak,undefined -static-libsan")
endif()
if(CMAKE_INTERPROCEDURAL_OPTIMIZATION)
set(CMAKE_C_COMPILE_OPTIONS_IPO -flto)
endif()
if(CMAKE_POSITION_INDEPENDENT_CODE AND BUILD_STATIC)
if(BUILD_STATIC AND BUILD_PIE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-pie")
elseif(BUILD_STATIC)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
elseif(CMAKE_POSITION_INDEPENDENT_CODE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie")
endif()
if(LINK_STATIC_LIBS)
if(NOT BUILD_STATIC AND LINK_STATIC_LIBS)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc")
endif()
endif()
Expand Down
2 changes: 1 addition & 1 deletion m.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ case "$1" in
cmake \
-DCMAKE_BUILD_TYPE="Release" \
-DCMAKE_EXE_LINKER_FLAGS="-static-libgcc" \
-DENABLE_SANITIZERS=OFF \
..
cmake --build .
HOST="$(cc -dumpmachine)"
Expand Down Expand Up @@ -131,6 +130,7 @@ case "$1" in
cmake \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
-DCMAKE_BUILD_TYPE="Debug" \
-DENABLE_SANITIZERS=ON \
..
cp compile_commands.json ../
cmake --build .
Expand Down

0 comments on commit 5bc4feb

Please sign in to comment.