Skip to content

Commit

Permalink
Add new option to build p4c with LTO (#3470)
Browse files Browse the repository at this point in the history
* Add new option to build p4c with LTO

* Warn if GCC used with LLD, switch to Gold
  • Loading branch information
davidbolvansky authored Sep 3, 2022
1 parent ead1595 commit 41d4d12
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ OPTION (ENABLE_PROTOBUF_STATIC "Link against Protobuf statically" ON)
OPTION (ENABLE_GC "Use libgc" ON)
OPTION (ENABLE_MULTITHREAD "Use multithreading" OFF)
OPTION (ENABLE_GMP "Use GMP library" ON)
OPTION (ENABLE_LTO "Enable Link Time Optimization (LTO)" OFF)
OPTION (BUILD_STATIC_RELEASE "Build a statically linked release binary" OFF)

set (P4C_DRIVER_NAME "p4c" CACHE STRING "Customize the name of the driver script")
Expand Down Expand Up @@ -220,10 +221,24 @@ add_cxx_compiler_option ("-Wno-deprecated-declarations")
# If we're on GCC or Clang, use the prefer LLD or Gold linker if available.
set(BUILD_LINK_WITH_GOLD ON CACHE BOOL "Use Gold linker for build if available")
set(BUILD_LINK_WITH_LLD ON CACHE BOOL "Use LLD linker for build if available (overrides BUILD_LINK_WITH_GOLD)")

# Build with LTO
if (ENABLE_LTO AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
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 ()

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 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")
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ sudo dpkg -i /path/to/package.deb
- `-DENABLE_MULTITHREAD=ON|OFF`. Use multithreading. Default is
OFF.
- `-DENABLE_GMP=ON|OFF`. Use the GMP library. Default is ON.
- `-DBUILD_LINK_WITH_GOLD=ON|OFF`. Use Gold linker for build if available.
- `-DBUILD_LINK_WITH_LLD=ON|OFF`. Use LLD linker for build if available (overrides BUILD_LINK_WITH_GOLD).
- `-DENABLE_LTO=ON|OFF`. Use Link Time Optimization (LTO). Default is OFF.
If adding new targets to this build system, please see
[instructions](#defining-new-cmake-targets).
Expand Down

0 comments on commit 41d4d12

Please sign in to comment.