[clang] [lldb] [llvm] [cmake] Build executables with -no_exported_symbols when building Apple toolchain (PR #87684)
Cyndy Ishida via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 5 13:20:08 PDT 2024
https://github.com/cyndyishida updated https://github.com/llvm/llvm-project/pull/87684
>From 3ac6872328334384fa20998541fac841add767d9 Mon Sep 17 00:00:00 2001
From: Cyndy Ishida <cyndy_ishida at apple.com>
Date: Thu, 4 Apr 2024 12:08:28 -0700
Subject: [PATCH 1/4] [cmake] Build executables with -no_exported_symbols when
building Apple toolchain
Building the Apple way turns off plugin support, meaning we don't need to be exporting unloadable symbols from all executables.
While deadstripping effects aren't expected to change, enabling this across all tools prevents the creation of export tries. This saves us ~3.5 MB's in just the universal build of `clang`.
---
clang/cmake/caches/Apple-stage2.cmake | 1 +
lldb/cmake/caches/Apple-lldb-base.cmake | 1 +
llvm/CMakeLists.txt | 3 +++
llvm/cmake/modules/AddLLVM.cmake | 30 ++++++++++++++++++-------
llvm/docs/CMake.rst | 4 ++++
5 files changed, 31 insertions(+), 8 deletions(-)
diff --git a/clang/cmake/caches/Apple-stage2.cmake b/clang/cmake/caches/Apple-stage2.cmake
index 72cdedd611bc96..faf61fd1fe9ecb 100644
--- a/clang/cmake/caches/Apple-stage2.cmake
+++ b/clang/cmake/caches/Apple-stage2.cmake
@@ -15,6 +15,7 @@ set(LLVM_ENABLE_ZLIB ON CACHE BOOL "")
set(LLVM_ENABLE_BACKTRACES OFF CACHE BOOL "")
set(LLVM_ENABLE_MODULES ON CACHE BOOL "")
set(LLVM_EXTERNALIZE_DEBUGINFO ON CACHE BOOL "")
+set(LLVM_ENABLE_NO_EXPORTED_SYMBOLS ON CACHE BOOL "")
set(CLANG_PLUGIN_SUPPORT OFF CACHE BOOL "")
set(CLANG_SPAWN_CC1 ON CACHE BOOL "")
set(BUG_REPORT_URL "http://developer.apple.com/bugreporter/" CACHE STRING "")
diff --git a/lldb/cmake/caches/Apple-lldb-base.cmake b/lldb/cmake/caches/Apple-lldb-base.cmake
index 4d4f02bfae95bd..021538896b2346 100644
--- a/lldb/cmake/caches/Apple-lldb-base.cmake
+++ b/lldb/cmake/caches/Apple-lldb-base.cmake
@@ -3,6 +3,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE BOOL "")
set(LLVM_TARGETS_TO_BUILD X86;ARM;AArch64 CACHE STRING "")
set(LLVM_ENABLE_ASSERTIONS ON CACHE BOOL "")
+set(LLVM_ENABLE_NO_EXPORTED_SYMBOLS ON CACHE BOOL "")
set(LIBCXX_ENABLE_SHARED OFF CACHE BOOL "")
set(LIBCXX_ENABLE_STATIC OFF CACHE BOOL "")
diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt
index 6f5647d70d8bc1..7e393acacb80d8 100644
--- a/llvm/CMakeLists.txt
+++ b/llvm/CMakeLists.txt
@@ -673,6 +673,9 @@ option(LLVM_USE_OPROFILE
option(LLVM_EXTERNALIZE_DEBUGINFO
"Generate dSYM files and strip executables and libraries (Darwin Only)" OFF)
+option(LLVM_ENABLE_NO_EXPORTED_SYMBOLS
+ "When building executables, disable any symbol exports (Darwin Only)" OFF)
+
set(LLVM_CODESIGNING_IDENTITY "" CACHE STRING
"Sign executables and dylibs with the given identity or skip if empty (Darwin Only)")
diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake
index 745935f1405170..141a97c852e24f 100644
--- a/llvm/cmake/modules/AddLLVM.cmake
+++ b/llvm/cmake/modules/AddLLVM.cmake
@@ -258,15 +258,24 @@ if (NOT DEFINED LLVM_LINKER_DETECTED AND NOT WIN32)
endif()
endif()
- # Apple's linker complains about duplicate libraries, which CMake likes to do
- # to support ELF platforms. To silence that warning, we can use
- # -no_warn_duplicate_libraries, but only in versions of the linker that
- # support that flag.
- if(NOT LLVM_USE_LINKER AND ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
+ if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
include(CheckLinkerFlag)
- check_linker_flag(C "-Wl,-no_warn_duplicate_libraries" LLVM_LINKER_SUPPORTS_NO_WARN_DUPLICATE_LIBRARIES)
- else()
- set(LLVM_LINKER_SUPPORTS_NO_WARN_DUPLICATE_LIBRARIES OFF CACHE INTERNAL "")
+ # Linkers that support Darwin allow a setting to internalize all symbol exports,
+ # aiding in reducing binary size and often is applicable for executables.
+ check_linker_flag(C "-Wl,-no_exported_symbols" LLVM_LINKER_SUPPORTS_NO_EXPORTED_SYMBOLS)
+
+ if (NOT LLVM_USE_LINKER)
+ # Apple's linker complains about duplicate libraries, which CMake likes to do
+ # to support ELF platforms. To silence that warning, we can use
+ # -no_warn_duplicate_libraries, but only in versions of the linker that
+ # support that flag.
+ check_linker_flag(C "-Wl,-no_warn_duplicate_libraries" LLVM_LINKER_SUPPORTS_NO_WARN_DUPLICATE_LIBRARIES)
+ else()
+ set(LLVM_LINKER_SUPPORTS_NO_WARN_DUPLICATE_LIBRARIES OFF CACHE INTERNAL "")
+ endif()
+
+ else()
+ set(LLVM_LINKER_SUPPORTS_NO_EXPORTED_SYMBOLS OFF CACHE INTERNAL "")
endif()
endif()
@@ -1029,6 +1038,11 @@ macro(add_llvm_executable name)
add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
endif(LLVM_EXPORTED_SYMBOL_FILE)
+ if (LLVM_ENABLE_NO_EXPORTED_SYMBOLS AND LLVM_LINKER_SUPPORTS_NO_EXPORTED_SYMBOLS)
+ set_property(TARGET ${name} APPEND_STRING PROPERTY
+ LINK_FLAGS " -Wl,-no_exported_symbols")
+ endif()
+
if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
set(USE_SHARED USE_SHARED)
endif()
diff --git a/llvm/docs/CMake.rst b/llvm/docs/CMake.rst
index d2f66d71d39afb..aaf0610034cf20 100644
--- a/llvm/docs/CMake.rst
+++ b/llvm/docs/CMake.rst
@@ -654,6 +654,10 @@ enabled sub-projects. Nearly all of these variable names begin with
Generate dSYM files and strip executables and libraries (Darwin Only).
Defaults to OFF.
+**LLVM_ENABLE_NO_EXPORTED_SYMBOLS**:BOOL
+ When building executables, disable any symbol exports (Darwin Only).
+ Defaults to OFF.
+
**LLVM_FORCE_USE_OLD_TOOLCHAIN**:BOOL
If enabled, the compiler and standard library versions won't be checked. LLVM
may not compile at all, or might fail at runtime due to known bugs in these
>From 9efdaa7aeae569b256ca4e32c8bcd2fa2064c6f4 Mon Sep 17 00:00:00 2001
From: Cyndy Ishida <cyndy_ishida at apple.com>
Date: Thu, 4 Apr 2024 14:05:57 -0700
Subject: [PATCH 2/4] Invert LLVM_ENABLE_NO_EXPORTED_SYMBOLS ->
LLVM_ENABLE_EXPORTED_SYMBOLS
---
clang/cmake/caches/Apple-stage2.cmake | 2 +-
lldb/cmake/caches/Apple-lldb-base.cmake | 2 +-
llvm/CMakeLists.txt | 4 ++--
llvm/cmake/modules/AddLLVM.cmake | 2 +-
llvm/docs/CMake.rst | 7 ++++---
5 files changed, 9 insertions(+), 8 deletions(-)
diff --git a/clang/cmake/caches/Apple-stage2.cmake b/clang/cmake/caches/Apple-stage2.cmake
index faf61fd1fe9ecb..7fc3f18153f0b5 100644
--- a/clang/cmake/caches/Apple-stage2.cmake
+++ b/clang/cmake/caches/Apple-stage2.cmake
@@ -15,7 +15,7 @@ set(LLVM_ENABLE_ZLIB ON CACHE BOOL "")
set(LLVM_ENABLE_BACKTRACES OFF CACHE BOOL "")
set(LLVM_ENABLE_MODULES ON CACHE BOOL "")
set(LLVM_EXTERNALIZE_DEBUGINFO ON CACHE BOOL "")
-set(LLVM_ENABLE_NO_EXPORTED_SYMBOLS ON CACHE BOOL "")
+set(LLVM_ENABLE_EXPORTED_SYMBOLS OFF CACHE BOOL "")
set(CLANG_PLUGIN_SUPPORT OFF CACHE BOOL "")
set(CLANG_SPAWN_CC1 ON CACHE BOOL "")
set(BUG_REPORT_URL "http://developer.apple.com/bugreporter/" CACHE STRING "")
diff --git a/lldb/cmake/caches/Apple-lldb-base.cmake b/lldb/cmake/caches/Apple-lldb-base.cmake
index 021538896b2346..34d603bade6487 100644
--- a/lldb/cmake/caches/Apple-lldb-base.cmake
+++ b/lldb/cmake/caches/Apple-lldb-base.cmake
@@ -3,7 +3,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE BOOL "")
set(LLVM_TARGETS_TO_BUILD X86;ARM;AArch64 CACHE STRING "")
set(LLVM_ENABLE_ASSERTIONS ON CACHE BOOL "")
-set(LLVM_ENABLE_NO_EXPORTED_SYMBOLS ON CACHE BOOL "")
+set(LLVM_ENABLE_EXPORTED_SYMBOLS OFF CACHE BOOL "")
set(LIBCXX_ENABLE_SHARED OFF CACHE BOOL "")
set(LIBCXX_ENABLE_STATIC OFF CACHE BOOL "")
diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt
index 7e393acacb80d8..6e038320376eaf 100644
--- a/llvm/CMakeLists.txt
+++ b/llvm/CMakeLists.txt
@@ -673,8 +673,8 @@ option(LLVM_USE_OPROFILE
option(LLVM_EXTERNALIZE_DEBUGINFO
"Generate dSYM files and strip executables and libraries (Darwin Only)" OFF)
-option(LLVM_ENABLE_NO_EXPORTED_SYMBOLS
- "When building executables, disable any symbol exports (Darwin Only)" OFF)
+option(LLVM_ENABLE_EXPORTED_SYMBOLS
+ "Preserve exported symbols in executables" ON)
set(LLVM_CODESIGNING_IDENTITY "" CACHE STRING
"Sign executables and dylibs with the given identity or skip if empty (Darwin Only)")
diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake
index 141a97c852e24f..eb5df7fa814b47 100644
--- a/llvm/cmake/modules/AddLLVM.cmake
+++ b/llvm/cmake/modules/AddLLVM.cmake
@@ -1038,7 +1038,7 @@ macro(add_llvm_executable name)
add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
endif(LLVM_EXPORTED_SYMBOL_FILE)
- if (LLVM_ENABLE_NO_EXPORTED_SYMBOLS AND LLVM_LINKER_SUPPORTS_NO_EXPORTED_SYMBOLS)
+ if (NOT LLVM_ENABLE_EXPORTED_SYMBOLS AND LLVM_LINKER_SUPPORTS_NO_EXPORTED_SYMBOLS)
set_property(TARGET ${name} APPEND_STRING PROPERTY
LINK_FLAGS " -Wl,-no_exported_symbols")
endif()
diff --git a/llvm/docs/CMake.rst b/llvm/docs/CMake.rst
index aaf0610034cf20..3231d3af121dee 100644
--- a/llvm/docs/CMake.rst
+++ b/llvm/docs/CMake.rst
@@ -654,9 +654,10 @@ enabled sub-projects. Nearly all of these variable names begin with
Generate dSYM files and strip executables and libraries (Darwin Only).
Defaults to OFF.
-**LLVM_ENABLE_NO_EXPORTED_SYMBOLS**:BOOL
- When building executables, disable any symbol exports (Darwin Only).
- Defaults to OFF.
+**LLVM_ENABLE_EXPORTED_SYMBOLS**:BOOL
+ When building executables, preserve symbol exports. Defaults to ON.
+ You can use this option to disable exported symbols on all executable build
+ targets (Darwin Only).
**LLVM_FORCE_USE_OLD_TOOLCHAIN**:BOOL
If enabled, the compiler and standard library versions won't be checked. LLVM
>From e5abff4453ee00d26b5d65373e518cf6dfa1769d Mon Sep 17 00:00:00 2001
From: Cyndy Ishida <cyndy_ishida at apple.com>
Date: Fri, 5 Apr 2024 11:48:21 -0700
Subject: [PATCH 3/4]
s/LLVM_ENABLE_EXPORTED_SYMBOLS/LLVM_ENABLE_EXPORTED_SYMBOLS_IN_EXECUTABLES/
---
clang/cmake/caches/Apple-stage2.cmake | 2 +-
lldb/cmake/caches/Apple-lldb-base.cmake | 2 +-
llvm/CMakeLists.txt | 2 +-
llvm/cmake/modules/AddLLVM.cmake | 2 +-
llvm/docs/CMake.rst | 6 +++---
5 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/clang/cmake/caches/Apple-stage2.cmake b/clang/cmake/caches/Apple-stage2.cmake
index 7fc3f18153f0b5..ede256a2da6b8f 100644
--- a/clang/cmake/caches/Apple-stage2.cmake
+++ b/clang/cmake/caches/Apple-stage2.cmake
@@ -15,7 +15,7 @@ set(LLVM_ENABLE_ZLIB ON CACHE BOOL "")
set(LLVM_ENABLE_BACKTRACES OFF CACHE BOOL "")
set(LLVM_ENABLE_MODULES ON CACHE BOOL "")
set(LLVM_EXTERNALIZE_DEBUGINFO ON CACHE BOOL "")
-set(LLVM_ENABLE_EXPORTED_SYMBOLS OFF CACHE BOOL "")
+set(LLVM_ENABLE_EXPORTED_SYMBOLS_IN_EXECUTABLES OFF CACHE BOOL "")
set(CLANG_PLUGIN_SUPPORT OFF CACHE BOOL "")
set(CLANG_SPAWN_CC1 ON CACHE BOOL "")
set(BUG_REPORT_URL "http://developer.apple.com/bugreporter/" CACHE STRING "")
diff --git a/lldb/cmake/caches/Apple-lldb-base.cmake b/lldb/cmake/caches/Apple-lldb-base.cmake
index 34d603bade6487..6c3fa4346d7e8a 100644
--- a/lldb/cmake/caches/Apple-lldb-base.cmake
+++ b/lldb/cmake/caches/Apple-lldb-base.cmake
@@ -3,7 +3,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE BOOL "")
set(LLVM_TARGETS_TO_BUILD X86;ARM;AArch64 CACHE STRING "")
set(LLVM_ENABLE_ASSERTIONS ON CACHE BOOL "")
-set(LLVM_ENABLE_EXPORTED_SYMBOLS OFF CACHE BOOL "")
+set(LLVM_ENABLE_EXPORTED_SYMBOLS_IN_EXECUTABLES OFF CACHE BOOL "")
set(LIBCXX_ENABLE_SHARED OFF CACHE BOOL "")
set(LIBCXX_ENABLE_STATIC OFF CACHE BOOL "")
diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt
index 6e038320376eaf..b4f47771443258 100644
--- a/llvm/CMakeLists.txt
+++ b/llvm/CMakeLists.txt
@@ -673,7 +673,7 @@ option(LLVM_USE_OPROFILE
option(LLVM_EXTERNALIZE_DEBUGINFO
"Generate dSYM files and strip executables and libraries (Darwin Only)" OFF)
-option(LLVM_ENABLE_EXPORTED_SYMBOLS
+option(LLVM_ENABLE_EXPORTED_SYMBOLS_IN_EXECUTABLES
"Preserve exported symbols in executables" ON)
set(LLVM_CODESIGNING_IDENTITY "" CACHE STRING
diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake
index eb5df7fa814b47..ee4e574f08624f 100644
--- a/llvm/cmake/modules/AddLLVM.cmake
+++ b/llvm/cmake/modules/AddLLVM.cmake
@@ -1038,7 +1038,7 @@ macro(add_llvm_executable name)
add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
endif(LLVM_EXPORTED_SYMBOL_FILE)
- if (NOT LLVM_ENABLE_EXPORTED_SYMBOLS AND LLVM_LINKER_SUPPORTS_NO_EXPORTED_SYMBOLS)
+ if (NOT LLVM_ENABLE_EXPORTED_SYMBOLS_IN_EXECUTABLES AND LLVM_LINKER_SUPPORTS_NO_EXPORTED_SYMBOLS)
set_property(TARGET ${name} APPEND_STRING PROPERTY
LINK_FLAGS " -Wl,-no_exported_symbols")
endif()
diff --git a/llvm/docs/CMake.rst b/llvm/docs/CMake.rst
index 3231d3af121dee..7a809d44d3d819 100644
--- a/llvm/docs/CMake.rst
+++ b/llvm/docs/CMake.rst
@@ -654,10 +654,10 @@ enabled sub-projects. Nearly all of these variable names begin with
Generate dSYM files and strip executables and libraries (Darwin Only).
Defaults to OFF.
-**LLVM_ENABLE_EXPORTED_SYMBOLS**:BOOL
+**LLVM_ENABLE_EXPORTED_SYMBOLS_IN_EXECUTABLES**:BOOL
When building executables, preserve symbol exports. Defaults to ON.
- You can use this option to disable exported symbols on all executable build
- targets (Darwin Only).
+ You can use this option to disable exported symbols from all
+ executables (Darwin Only).
**LLVM_FORCE_USE_OLD_TOOLCHAIN**:BOOL
If enabled, the compiler and standard library versions won't be checked. LLVM
>From 3ed42ad17f0b91c5bf02af40933be1ea36f39576 Mon Sep 17 00:00:00 2001
From: Cyndy Ishida <cyndy_ishida at apple.com>
Date: Fri, 5 Apr 2024 13:19:15 -0700
Subject: [PATCH 4/4] Make mis-config fatal
---
llvm/cmake/modules/AddLLVM.cmake | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake
index ee4e574f08624f..f8a17f645e25ba 100644
--- a/llvm/cmake/modules/AddLLVM.cmake
+++ b/llvm/cmake/modules/AddLLVM.cmake
@@ -1038,9 +1038,14 @@ macro(add_llvm_executable name)
add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
endif(LLVM_EXPORTED_SYMBOL_FILE)
- if (NOT LLVM_ENABLE_EXPORTED_SYMBOLS_IN_EXECUTABLES AND LLVM_LINKER_SUPPORTS_NO_EXPORTED_SYMBOLS)
- set_property(TARGET ${name} APPEND_STRING PROPERTY
- LINK_FLAGS " -Wl,-no_exported_symbols")
+ if (NOT LLVM_ENABLE_EXPORTED_SYMBOLS_IN_EXECUTABLES)
+ if(LLVM_LINKER_SUPPORTS_NO_EXPORTED_SYMBOLS)
+ set_property(TARGET ${name} APPEND_STRING PROPERTY
+ LINK_FLAGS " -Wl,-no_exported_symbols")
+ else()
+ message(FATAL_ERROR
+ "LLVM_ENABLE_EXPORTED_SYMBOLS_IN_EXECUTABLES cannot be disabled when linker does not support \"-no_exported_symbols\"")
+ endif()
endif()
if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB)
More information about the cfe-commits
mailing list