From 35fec47638fc7b1e956e28645045811b242addd6 Mon Sep 17 00:00:00 2001 From: Felix Hanau Date: Wed, 9 Oct 2024 19:09:32 -0400 Subject: [PATCH] Use lld to reduce macOS release binary size --- .bazelrc | 12 ++++++++++++ .github/workflows/release.yml | 2 ++ 2 files changed, 14 insertions(+) diff --git a/.bazelrc b/.bazelrc index c75186b55b5..02c001f1add 100644 --- a/.bazelrc +++ b/.bazelrc @@ -317,6 +317,18 @@ build:release_macos --config=release_unix # data in releases. build:release_macos --linkopt="-Wl,-no_data_in_code_info" build:release_macos --linkopt="-Wl,-no_function_starts" +# Enable lld identical code folding to significantly reduce binary size. +build:release_macos --config=macos_lld_icf + +# On macOS, compile using LLD (19 or higher is compatible with the default flags added by +# apple_support). Requires Homebrew's lld package to be installed. This is less CPU intensive than +# the system linker, but also slightly slower in terms of wall time since it is less parallel. More +# importantly, it allows us to enable LLD's ICF pass, which significantly decreases binary sizes. +# We also want to enable ICF for Linux, but there it causes warnings when dynamically linking with +# libc++. +build:macos_lld --linkopt="-fuse-ld=/opt/homebrew/bin/ld64.lld" +build:macos_lld_icf --config=macos_lld +build:macos_lld_icf --linkopt="-Wl,--icf=safe" build:release_windows --config=release # Windows uses /O2 as its preferred optimization setting and enabled by bazel in the opt diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 99063ace919..398ff8aeb6c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -96,6 +96,8 @@ jobs: if: runner.os == 'macOS' run: | sudo xcode-select -s "/Applications/Xcode_15.1.app" + # Install lld + brew install lld - name: Setup Windows if: runner.os == 'Windows' run: |