Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new option to build p4c with LTO #3470

Merged
merged 2 commits into from
Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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