Skip to content

Commit

Permalink
Split linker flags in separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Aug 18, 2020
1 parent 33e8548 commit 0679fc0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/builtins/default.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ cninja_require(compiler=clang)
cninja_require(lld)
# cninja_require(libcxx) - not useful as of now as linux distros default to libstdc++
cninja_require(visibility)
cninja_require(linker)
cninja_require(linkerwarnings)

# -pipe: Potentially makes the build faster
# -ffunction-sections, etc... : Allows to discard unused code more easily with --gc-sections
string(APPEND CMAKE_C_FLAGS_INIT " -pipe -ffunction-sections -fdata-sections")
string(APPEND CMAKE_CXX_FLAGS_INIT " -pipe -ffunction-sections -fdata-sections")
string(APPEND CMAKE_C_FLAGS_INIT " -pipe")
string(APPEND CMAKE_CXX_FLAGS_INIT " -pipe")

if(WIN32)
# Remove obnoxious default <windows.h> features
Expand Down
11 changes: 11 additions & 0 deletions src/builtins/earlybinding.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Force symbols in shared libraries to be loaded on startup instead of lazily loaded.
cninja_require(pre)

if(APPLE)
add_linker_flags(" -Wl,-z,now")
elseif(MSVC)
# Nothing special to do here, it's the default
else()
# For macOS this only works for executables
string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " -Wl,-bind_at_load")
endif()
22 changes: 22 additions & 0 deletions src/builtins/linker.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Discard unused symbols & libraries
cninja_require(pre)

# -ffunction-sections, etc... : Allows to discard unused code more easily with --gc-sections
string(APPEND CMAKE_C_FLAGS_INIT " -ffunction-sections -fdata-sections")
string(APPEND CMAKE_CXX_FLAGS_INIT " -ffunction-sections -fdata-sections")

if(NOT APPLE)
# In conjunction with ffunction-sections / fdata-sections, removes unused code
add_linker_flags(" -Wl,--gc-sections")

# Don't link against libraries of which no symbols are used
add_linker_flags(" -Wl,--as-needed")

# Make all platforms behave like Windows, which is in itself terrible but will
# spare you trying to debug why dynamic_cast of inline classes across DLLs
# doesn't work in MSW - simply don't write code assuming this works anywhere.
add_linker_flags(" -Bsymbolic -Bsymbolic-functions")
else()
# Apple way to strip unneeded libs:
add_linker_flags(" -Wl,-dead_strip -Wl,-dead_strip_dylibs")
endif()
1 change: 0 additions & 1 deletion src/builtins/linkerwarnings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ if(NOT "asan" IN_LIST CNINJA_FEATURES)
if((CNINJA_LINKER STREQUAL "lld") OR (CNINJA_LINKER STREQUAL "gold"))
set(temp_LINKER_WARNINGS
"-Wl,-z,defs \
-Wl,-z,now \
-Wl,--unresolved-symbols,report-all \
-Wl,--no-undefined \
-Wl,--no-allow-shlib-undefined \
Expand Down
34 changes: 9 additions & 25 deletions src/builtins/lld.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,16 @@ if(NOT APPLE)
add_linker_flags(" -fuse-ld=gold")
set(CNINJA_LINKER gold)
endif()
endif()

if(NOT APPLE)
# In conjunction with ffunction-sections / fdata-sections, removes unused code
add_linker_flags(" -Wl,--gc-sections")

# Don't link against libraries of which no symbols are used
add_linker_flags(" -Wl,--as-needed")

# Make all platforms behave like Windows, which is in itself terrible but will
# spare you trying to debug why dynamic_cast of inline classes across DLLs
# doesn't work in MSW - simply don't write code assuming this works anywhere.
add_linker_flags(" -Bsymbolic -Bsymbolic-functions")
else()
# Apple way to strip unneeded libs:
add_linker_flags(" -Wl,-dead_strip")
if(NOT WIN32 AND NOT APPLE)
# Make linking faster
# Except that gold segfaults with -flto and --threads...
if((CNINJA_LINKER STREQUAL "lld") OR NOT ("lto" IN_LIST CNINJA_FEATURES))
add_linker_flags(" -Wl,--threads")
endif()

if(NOT WIN32 AND NOT APPLE)
# Make linking faster
# Except that gold segfaults with -flto and --threads...
if((CNINJA_LINKER STREQUAL "lld") OR NOT ("lto" IN_LIST CNINJA_FEATURES))
add_linker_flags(" -Wl,--threads")
endif()

# Make debugging faster
# Pro tip (Thanks Milian !): also add "set index-cache on" to your .gdbinit
add_linker_flags(" -Wl,--gdb-index")
endif()
# Make debugging faster
# Pro tip (Thanks Milian !): also add "set index-cache on" to your .gdbinit
add_linker_flags(" -Wl,--gdb-index")
endif()

0 comments on commit 0679fc0

Please sign in to comment.