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

[CMake] Support per-target linker flags #68393

Merged
merged 3 commits into from
Oct 17, 2023
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
11 changes: 11 additions & 0 deletions llvm/cmake/modules/AddLLVM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,8 @@ function(llvm_add_library name)
endforeach()
endif()

add_custom_linker_flags(${name})

if(ARG_SHARED OR ARG_MODULE)
llvm_externalize_debuginfo(${name})
llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH})
Expand Down Expand Up @@ -1019,6 +1021,8 @@ macro(add_llvm_executable name)
endforeach()
endif( LLVM_COMMON_DEPENDS )

add_custom_linker_flags(${name})

if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO)
llvm_externalize_debuginfo(${name})
endif()
Expand Down Expand Up @@ -1524,6 +1528,13 @@ macro(add_llvm_tool_subdirectory name)
add_llvm_external_project(${name})
endmacro(add_llvm_tool_subdirectory)

macro(add_custom_linker_flags name)
if (LLVM_${name}_LINKER_FLAGS)
message(STATUS "Applying ${LLVM_${name}_LINKER_FLAGS} to ${name}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd omit this to avoid the extra build spam (or make it optional).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What did you mean by optional here? Like a CMake flag that enables the message? In general, I find this message to be quite helpful when debug whether targets were applied with custom flags without needing to go into the CMakeCache.txt.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to land this as is, and if this becomes very build spammy, we can figure out other approaches to disable it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative to an option would be to use the DEBUG mode instead of STATUS so developers can use CMAKE_MESSAGE_LOG_LEVEL to suppress the output if desired.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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}")
Expand Down
3 changes: 3 additions & 0 deletions llvm/docs/CMake.rst
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,9 @@ 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_<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.
It defaults to "host", meaning that it shall pick the architecture
Expand Down