Skip to content

Commit

Permalink
Respect ENABLE_LTO even for static build, but keep LTO default for st…
Browse files Browse the repository at this point in the history
…atic (#4320)
  • Loading branch information
vlstill authored Jan 5, 2024
1 parent fa4cc97 commit b082745
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,19 @@ OPTION (ENABLE_P4C_GRAPHS "Build the p4c-graphs backend" ON)
OPTION (ENABLE_PROTOBUF_STATIC "Link against Protobuf statically" ON)
OPTION (ENABLE_GC "Use libgc" ON)
OPTION (ENABLE_MULTITHREAD "Use multithreading" OFF)
OPTION (ENABLE_LTO "Enable Link Time Optimization (LTO)" OFF)
OPTION (ENABLE_WERROR "Treat warnings as errors" OFF)
OPTION (ENABLE_SANITIZERS "Enable sanitizers" OFF)
option(BUILD_STATIC_RELEASE_SANS_GLIBC
OPTION (BUILD_STATIC_RELEASE_SANS_GLIBC
"Build a (mostly) statically linked release binary. Glibc is linked dynamically." OFF
)
if (BUILD_STATIC_RELEASE_SANS_GLIBC)
# We want to optimize the binary size for a static release binary by default.
set (_ENABLE_LTO_DEFAULT ON)
else()
set (_ENABLE_LTO_DEFAULT OFF)
endif()
OPTION (ENABLE_LTO "Enable Link Time Optimization (LTO)" ${_ENABLE_LTO_DEFAULT})

OPTION (BUILD_AUTO_VAR_INIT_PATTERN "Initialize variables with pattern during build" OFF)
OPTION (ENABLE_IWYU "Enable checking includes with IWYU" OFF)
# Support a legacy option. TODO: Remove?
Expand Down Expand Up @@ -282,19 +289,10 @@ if (ENABLE_LTO AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
message(WARNING "LLD does not work with GCC's LTO object format, switching to Gold.")
set(BUILD_LINK_WITH_LLD OFF)
endif ()
add_cxx_compiler_option ("-flto")
add_cxx_compiler_option("-flto")
endif ()

if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# We want to optimize the binary size for a static release binary.
# This only works with modern versions of GCC.
if (BUILD_STATIC_RELEASE_SANS_GLIBC AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 8.0)
if (BUILD_LINK_WITH_LLD)
message(WARNING "LLD does not work with GCC's LTO object format, switching to Gold.")
set(BUILD_LINK_WITH_LLD OFF)
endif ()
add_cxx_compiler_option ("-flto")
endif ()
set(_LD_USED "default system")
set(_LD_OPT_USED "")
macro(check_linker BUILD_LINK_WITH_VAR LINKER_OPT LINKER_NAME)
Expand Down

0 comments on commit b082745

Please sign in to comment.