Skip to content

Commit

Permalink
Enable ICF and LTO (#33560)
Browse files Browse the repository at this point in the history
Summary:
X-link: facebook/react-native#33560
Pull Request resolved: facebook#720

Turn on ICF for the build whenever it is available. This requires us to
link with LLD, which is only the default starting in NDK r23, so
manually specify LLD for the Android build.

Also turn on LTO for the CircleCI build, so we generate highly
optimised binaries for distribution.

Changelog: [Internal]

Reviewed By: jpporto

Differential Revision: D35344254

fbshipit-source-id: 64b37e6a7817a7b7826cc7480468367b95d63c61
  • Loading branch information
neildhar authored and facebook-github-bot committed Apr 5, 2022
1 parent 359b0ce commit 1fabbb2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ jobs:
name: Build Hermes for Linux
command: |
cd "$HERMES_WS_DIR"
cmake -S hermes -B build_release -G Ninja -DHERMES_STATIC_LINK=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS=-s -DCMAKE_C_FLAGS=-s
cmake -S hermes -B build_release -G Ninja -DHERMES_STATIC_LINK=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=True -DCMAKE_CXX_FLAGS=-s -DCMAKE_C_FLAGS=-s
cd build_release
ninja github-cli-release
- run:
Expand Down Expand Up @@ -399,7 +399,7 @@ jobs:
$Env:PATH += ";$Env:CMAKE_DIR;$Env:MSBUILD_DIR"
$Env:ICU_ROOT = "$Env:HERMES_WS_DIR\icu"
cd $Env:HERMES_WS_DIR
cmake -S hermes -B build_release -G 'Visual Studio 16 2019' -Ax64 -DCMAKE_BUILD_TYPE=Release -DHERMES_ENABLE_WIN10_ICU_FALLBACK=OFF -DHERMES_GITHUB_RESOURCE_DIR="$Env:HERMES_WS_DIR\deps"
cmake -S hermes -B build_release -G 'Visual Studio 16 2019' -Ax64 -DCMAKE_BUILD_TYPE=Release -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=True -DHERMES_ENABLE_WIN10_ICU_FALLBACK=OFF -DHERMES_GITHUB_RESOURCE_DIR="$Env:HERMES_WS_DIR\deps"
if (-not $?) { throw "Failed to configure Hermes" }
cd build_release
cmake --build . --target github-cli-release --config Release
Expand Down
19 changes: 10 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,15 @@ if (HERMES_STATIC_LINK)
set(CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_STATIC_LIBRARY_SUFFIX}")
endif()

# Check if the linker supports --gc-sections
# Check if the linker supports --gc-sections and ICF.
# We can't simply CHECK_CXX_COMPILER_FLAG("-Wl,--gc-sections" ..) because CMake
# will compile and link separately and only passes the flag during compilation.
# TODO: Use check_linker_flag once we have CMake 3.18.
set(OLD_CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "-Wl,--gc-sections")
set(CMAKE_EXE_LINKER_FLAGS "${OLD_CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
CHECK_CXX_COMPILER_FLAG("" HAVE_GC_SECTIONS)
set(CMAKE_EXE_LINKER_FLAGS "${OLD_CMAKE_EXE_LINKER_FLAGS} -Wl,--icf=safe")
CHECK_CXX_COMPILER_FLAG("" HAVE_ICF)
set(CMAKE_EXE_LINKER_FLAGS "${OLD_CMAKE_EXE_LINKER_FLAGS}")

if(HAVE_GC_SECTIONS)
Expand All @@ -306,6 +309,11 @@ if(HAVE_GC_SECTIONS)
list(APPEND HERMES_EXTRA_LINKER_FLAGS "LINKER:--gc-sections")
endif()

if(HAVE_ICF)
add_flag_if_supported("-faddrsig" ADDRSIG)
list(APPEND HERMES_EXTRA_LINKER_FLAGS "LINKER:--icf=safe")
endif()

# Make the HERMES_RELEASE_VERSION accessible for version printing in C++.
add_definitions(-DHERMES_RELEASE_VERSION="${HERMES_RELEASE_VERSION}")

Expand Down Expand Up @@ -423,13 +431,6 @@ else()
set(HERMES_ASSUMED_BUILD_MODE_IN_LIT_TEST "opt")
endif()

if (NOT (GENERATOR_IS_MULTI_CONFIG OR CMAKE_BUILD_TYPE STREQUAL Debug OR CMAKE_COMPILER_IS_GNUCXX))
# Enable LTO if we are not multi config generator and not a DEBUG build
# and not GCC
# GCC currently fails to link Hermes with LTO (see t16557748)
option(HERMES_ENABLE_LTO "Build Hermes with LTO" ON)
endif()

if (GCC_COMPATIBLE)
# Don't export symbols unless we explicitly say so
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
Expand Down
1 change: 1 addition & 0 deletions android/hermes/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ android {
arguments "-DHERMES_FACEBOOK_BUILD=${rootProject.ext.facebookBuild}"
arguments "-DANDROID_STL=c++_shared"
arguments "-DANDROID_PIE=True"
arguments "-DANDROID_LD=lld"
arguments "-DIMPORT_HERMESC=${rootProject.ext.hermesC}"
arguments "-DFBSOURCE_DIR=${rootProject.ext.fbsource}"
arguments "-DHERMES_SLOW_DEBUG=False"
Expand Down

0 comments on commit 1fabbb2

Please sign in to comment.