[llvm] [CMake] Support per-target linker flags (PR #68393)
Vincent Lee via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 16 15:59:03 PDT 2023
https://github.com/thevinster updated https://github.com/llvm/llvm-project/pull/68393
>From cc83200af3bd64f41195b6a11480578370faf52b Mon Sep 17 00:00:00 2001
From: Vincent Lee <leevince at fb.com>
Date: Thu, 5 Oct 2023 23:18:09 -0700
Subject: [PATCH 1/3] [CMake] Support per-target linker flags
---
llvm/cmake/modules/AddLLVM.cmake | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake
index 93011522e498e6e..4075e0bb9fe4bc4 100644
--- a/llvm/cmake/modules/AddLLVM.cmake
+++ b/llvm/cmake/modules/AddLLVM.cmake
@@ -726,6 +726,20 @@ function(llvm_add_library name)
endforeach()
endif()
+ if (${name} IN_LIST LLVM_CUSTOM_BINARIES)
+ if (NOT LLVM_${name}_EXE_LINKER_FLAGS AND NOT LLVM_${name}_SHARED_LINKER_FLAGS AND NOT LLVM_${name}_MODULE_LINKER_FLAGS)
+ message(FATAL_ERROR "Linker flags not provided for ${name} in LLVM_CUSTOM_BINARIES")
+ endif()
+ if (ARG_SHARED AND LLVM_${name}_SHARED_LINKER_FLAGS)
+ message(STATUS "Applying ${LLVM_${name}_SHARED_LINKER_FLAGS} to ${name}")
+ target_link_options(${name} PRIVATE ${LLVM_${name}_SHARED_LINKER_FLAGS})
+ endif()
+ if (ARG_MODULE AND LLVM_${name}_MODULE_LINKER_FLAGS)
+ message(STATUS "Applying ${LLVM_${name}_MODULE_LINKER_FLAGS} to ${name}")
+ target_link_options(${name} PRIVATE ${LLVM_${name}_MODULE_LINKER_FLAGS})
+ endif()
+ endif()
+
if(ARG_SHARED OR ARG_MODULE)
llvm_externalize_debuginfo(${name})
llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH})
@@ -1019,6 +1033,16 @@ macro(add_llvm_executable name)
endforeach()
endif( LLVM_COMMON_DEPENDS )
+ if (${name} IN_LIST LLVM_CUSTOM_BINARIES)
+ if (NOT LLVM_${name}_EXE_LINKER_FLAGS AND NOT LLVM_${name}_SHARED_LINKER_FLAGS AND NOT LLVM_${name}_MODULE_LINKER_FLAGS)
+ message(FATAL_ERROR "Linker flags not provided for ${name} in LLVM_CUSTOM_BINARIES")
+ endif()
+ if (LLVM_${name}_EXE_LINKER_FLAGS)
+ message(STATUS "Applying ${LLVM_${name}_EXE_LINKER_FLAGS} to ${name}")
+ target_link_options(${name} PRIVATE ${LLVM_${name}_EXE_LINKER_FLAGS})
+ endif()
+ endif()
+
if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
llvm_externalize_debuginfo(${name})
endif()
>From 5decd5957dd08ca364e10839bf8c7c5eca2ec665 Mon Sep 17 00:00:00 2001
From: Vincent Lee <leevince at fb.com>
Date: Fri, 6 Oct 2023 14:55:49 -0700
Subject: [PATCH 2/3] fixup! [CMake] Support per-target linker flags
---
llvm/cmake/modules/AddLLVM.cmake | 34 +++++++++++---------------------
llvm/docs/CMake.rst | 8 ++++++++
2 files changed, 20 insertions(+), 22 deletions(-)
diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake
index 4075e0bb9fe4bc4..b67dbb971ca5f25 100644
--- a/llvm/cmake/modules/AddLLVM.cmake
+++ b/llvm/cmake/modules/AddLLVM.cmake
@@ -726,19 +726,7 @@ function(llvm_add_library name)
endforeach()
endif()
- if (${name} IN_LIST LLVM_CUSTOM_BINARIES)
- if (NOT LLVM_${name}_EXE_LINKER_FLAGS AND NOT LLVM_${name}_SHARED_LINKER_FLAGS AND NOT LLVM_${name}_MODULE_LINKER_FLAGS)
- message(FATAL_ERROR "Linker flags not provided for ${name} in LLVM_CUSTOM_BINARIES")
- endif()
- if (ARG_SHARED AND LLVM_${name}_SHARED_LINKER_FLAGS)
- message(STATUS "Applying ${LLVM_${name}_SHARED_LINKER_FLAGS} to ${name}")
- target_link_options(${name} PRIVATE ${LLVM_${name}_SHARED_LINKER_FLAGS})
- endif()
- if (ARG_MODULE AND LLVM_${name}_MODULE_LINKER_FLAGS)
- message(STATUS "Applying ${LLVM_${name}_MODULE_LINKER_FLAGS} to ${name}")
- target_link_options(${name} PRIVATE ${LLVM_${name}_MODULE_LINKER_FLAGS})
- endif()
- endif()
+ add_custom_linker_flags(${name})
if(ARG_SHARED OR ARG_MODULE)
llvm_externalize_debuginfo(${name})
@@ -1033,15 +1021,7 @@ macro(add_llvm_executable name)
endforeach()
endif( LLVM_COMMON_DEPENDS )
- if (${name} IN_LIST LLVM_CUSTOM_BINARIES)
- if (NOT LLVM_${name}_EXE_LINKER_FLAGS AND NOT LLVM_${name}_SHARED_LINKER_FLAGS AND NOT LLVM_${name}_MODULE_LINKER_FLAGS)
- message(FATAL_ERROR "Linker flags not provided for ${name} in LLVM_CUSTOM_BINARIES")
- endif()
- if (LLVM_${name}_EXE_LINKER_FLAGS)
- message(STATUS "Applying ${LLVM_${name}_EXE_LINKER_FLAGS} to ${name}")
- target_link_options(${name} PRIVATE ${LLVM_${name}_EXE_LINKER_FLAGS})
- endif()
- endif()
+ add_custom_linker_flags(${name})
if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
llvm_externalize_debuginfo(${name})
@@ -1548,6 +1528,16 @@ macro(add_llvm_tool_subdirectory name)
add_llvm_external_project(${name})
endmacro(add_llvm_tool_subdirectory)
+macro(add_custom_linker_flags name)
+ if (${name} IN_LIST LLVM_CUSTOM_BINARIES)
+ if (NOT LLVM_${name}_LINKER_FLAGS)
+ message(FATAL_ERROR "LLVM_${name}_LINKER_FLAGS must be provided when using LLVM_CUSTOM_BINARIES")
+ endif()
+ message(STATUS "Applying ${LLVM_${name}_LINKER_FLAGS} to ${name}")
+ target_link_options(${name} PRIVATE ${LLVM_${name}_LINKER_FLAGS})
+ endif()
+endmacro()
+
function(get_project_name_from_src_var var output)
string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR"
MACHED_TOOL "${var}")
diff --git a/llvm/docs/CMake.rst b/llvm/docs/CMake.rst
index c70b6b8206c2f92..ec5ae5f70347a49 100644
--- a/llvm/docs/CMake.rst
+++ b/llvm/docs/CMake.rst
@@ -428,6 +428,14 @@ enabled sub-projects. Nearly all of these variable names begin with
$CMAKE_INSTALL_PREFIX/Toolchains containing an xctoolchain directory which can
be used to override the default system tools.
+**LLVM_CUSTOM_BINARIES**:STRING
+ If set to a semicolon separated list of targets, those targets will be appended
+ with custom linker flags from ``LLVM_<target>_LINKER_FLAGS``.
+
+ **LLVM_<target>_LINKER_FLAGS**:STRING
+ Defines the set of linker flags that should be applied to a <target> defined via
+ LLVM_CUSTOM_BINARIES.
+
**LLVM_DEFAULT_TARGET_TRIPLE**:STRING
LLVM target to use for code generation when no target is explicitly specified.
It defaults to "host", meaning that it shall pick the architecture
>From ac470a0e8fe1cb80597f232b1920ef82d54958d4 Mon Sep 17 00:00:00 2001
From: Vincent Lee <leevince at fb.com>
Date: Mon, 16 Oct 2023 15:58:40 -0700
Subject: [PATCH 3/3] fixup! [CMake] Support per-target linker flags
---
llvm/cmake/modules/AddLLVM.cmake | 5 +----
llvm/docs/CMake.rst | 9 ++-------
2 files changed, 3 insertions(+), 11 deletions(-)
diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake
index b67dbb971ca5f25..72661594f643fae 100644
--- a/llvm/cmake/modules/AddLLVM.cmake
+++ b/llvm/cmake/modules/AddLLVM.cmake
@@ -1529,10 +1529,7 @@ macro(add_llvm_tool_subdirectory name)
endmacro(add_llvm_tool_subdirectory)
macro(add_custom_linker_flags name)
- if (${name} IN_LIST LLVM_CUSTOM_BINARIES)
- if (NOT LLVM_${name}_LINKER_FLAGS)
- message(FATAL_ERROR "LLVM_${name}_LINKER_FLAGS must be provided when using LLVM_CUSTOM_BINARIES")
- endif()
+ if (LLVM_${name}_LINKER_FLAGS)
message(STATUS "Applying ${LLVM_${name}_LINKER_FLAGS} to ${name}")
target_link_options(${name} PRIVATE ${LLVM_${name}_LINKER_FLAGS})
endif()
diff --git a/llvm/docs/CMake.rst b/llvm/docs/CMake.rst
index ec5ae5f70347a49..c76cb8a789beee7 100644
--- a/llvm/docs/CMake.rst
+++ b/llvm/docs/CMake.rst
@@ -428,13 +428,8 @@ enabled sub-projects. Nearly all of these variable names begin with
$CMAKE_INSTALL_PREFIX/Toolchains containing an xctoolchain directory which can
be used to override the default system tools.
-**LLVM_CUSTOM_BINARIES**:STRING
- If set to a semicolon separated list of targets, those targets will be appended
- with custom linker flags from ``LLVM_<target>_LINKER_FLAGS``.
-
- **LLVM_<target>_LINKER_FLAGS**:STRING
- Defines the set of linker flags that should be applied to a <target> defined via
- LLVM_CUSTOM_BINARIES.
+**LLVM_<target>_LINKER_FLAGS**:STRING
+ Defines the set of linker flags that should be applied to a <target>.
**LLVM_DEFAULT_TARGET_TRIPLE**:STRING
LLVM target to use for code generation when no target is explicitly specified.
More information about the llvm-commits
mailing list