Skip to content

Commit cc25000

Browse files
authored
[CMake] Respect LLVM instrumentation variables in AddPureSwift.cmake (#71135)
If one is building Swift against an instrumented LLVM build, when the functions in AddPureSwift.cmake tries to link Swift code that also links LLVM instrumented code, the linking will fail because the linker will not use the profiling runtimes. The modifications replicate the existing code in LLVM HandleLLVMOptions.cmake, but adapted to Swift variables where possible. The necessary arguments are forwarded through `-Xclang-linker` because the spelling of the Swift driver does not give access to all the options that Clang provides. This has been tested with an unified build, but a standalone build could still pass the `LLVM_*` variables to the Swift build system to make use of this code.
1 parent 499b218 commit cc25000

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

cmake/modules/AddPureSwift.cmake

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,23 @@ function(add_pure_swift_host_library name)
264264
)
265265
endif()
266266

267+
# This replicates he code existing in LLVM llvm/cmake/modules/HandleLLVMOptions.cmake
268+
# The second part of the clause replicates the LINKER_IS_LLD_LINK of the
269+
# original.
270+
if(LLVM_BUILD_INSTRUMENTED AND NOT (SWIFT_COMPILER_IS_MSVC_LIKE AND SWIFT_USE_LINKER STREQUAL "lld"))
271+
string(TOUPPER "${LLVM_BUILD_INSTRUMENTED}" uppercase_LLVM_BUILD_INSTRUMENTED)
272+
if(LLVM_ENABLE_IR_PGO OR uppercase_LLVM_BUILD_INSTRUMENTED STREQUAL "IR")
273+
target_link_options(${name} PRIVATE
274+
"SHELL:-Xclang-linker -fprofile-generate=\"${LLVM_PROFILE_DATA_DIR}\"")
275+
elseif(uppercase_LLVM_BUILD_INSTRUMENTED STREQUAL "CSIR")
276+
target_link_options(${name} PRIVATE
277+
"SHELL:-Xclang-linker -fcs-profile-generate=\"${LLVM_CSPROFILE_DATA_DIR}\"")
278+
else()
279+
target_link_options(${name} PRIVATE
280+
"SHELL:-Xclang-linker -fprofile-instr-generate=\"${LLVM_PROFILE_FILE_PATTERN}\"")
281+
endif()
282+
endif()
283+
267284
# Export this target.
268285
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name})
269286
endfunction()

0 commit comments

Comments
 (0)