Skip to content

Commit face056

Browse files
authored
[CMake] Respect LLVM instrumentation variables in libraries and executables (#73629)
In #71135 I added this code to `add_pure_swift_host_library` but failed to realize that it was also needed in `add_pure_swift_host_tool`. Extract the code into a helper function and invoke it from both functions in order to be able to instrument the `swift-plugin-server` and other pure Swift tools.
1 parent 2c6bfa2 commit face056

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

cmake/modules/AddPureSwift.cmake

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,26 @@ function(_set_pure_swift_link_flags name relpath_to_lib_dir)
102102
endif()
103103
endfunction()
104104

105+
function(_set_pure_swift_profile_flags target_name)
106+
# This replicates the code existing in LLVM llvm/cmake/modules/HandleLLVMOptions.cmake
107+
# The second part of the clause replicates the LINKER_IS_LLD_LINK of the
108+
# original.
109+
if(LLVM_BUILD_INSTRUMENTED AND NOT (SWIFT_COMPILER_IS_MSVC_LIKE AND SWIFT_USE_LINKER STREQUAL "lld"))
110+
string(TOUPPER "${LLVM_BUILD_INSTRUMENTED}" uppercase_LLVM_BUILD_INSTRUMENTED)
111+
if(LLVM_ENABLE_IR_PGO OR uppercase_LLVM_BUILD_INSTRUMENTED STREQUAL "IR")
112+
target_link_options(${target_name} PRIVATE
113+
"SHELL:-Xclang-linker -fprofile-generate=\"${LLVM_PROFILE_DATA_DIR}\"")
114+
elseif(uppercase_LLVM_BUILD_INSTRUMENTED STREQUAL "CSIR")
115+
target_link_options(${target_name} PRIVATE
116+
"SHELL:-Xclang-linker -fcs-profile-generate=\"${LLVM_CSPROFILE_DATA_DIR}\"")
117+
else()
118+
target_link_options(${target_name} PRIVATE
119+
"SHELL:-Xclang-linker -fprofile-instr-generate=\"${LLVM_PROFILE_FILE_PATTERN}\"")
120+
endif()
121+
endif()
122+
endfunction()
123+
124+
105125
# Add a new "pure" Swift host library.
106126
#
107127
# "Pure" Swift host libraries can only contain Swift code, and will be built
@@ -280,22 +300,7 @@ function(add_pure_swift_host_library name)
280300
)
281301
endif()
282302

283-
# This replicates he code existing in LLVM llvm/cmake/modules/HandleLLVMOptions.cmake
284-
# The second part of the clause replicates the LINKER_IS_LLD_LINK of the
285-
# original.
286-
if(LLVM_BUILD_INSTRUMENTED AND NOT (SWIFT_COMPILER_IS_MSVC_LIKE AND SWIFT_USE_LINKER STREQUAL "lld"))
287-
string(TOUPPER "${LLVM_BUILD_INSTRUMENTED}" uppercase_LLVM_BUILD_INSTRUMENTED)
288-
if(LLVM_ENABLE_IR_PGO OR uppercase_LLVM_BUILD_INSTRUMENTED STREQUAL "IR")
289-
target_link_options(${name} PRIVATE
290-
"SHELL:-Xclang-linker -fprofile-generate=\"${LLVM_PROFILE_DATA_DIR}\"")
291-
elseif(uppercase_LLVM_BUILD_INSTRUMENTED STREQUAL "CSIR")
292-
target_link_options(${name} PRIVATE
293-
"SHELL:-Xclang-linker -fcs-profile-generate=\"${LLVM_CSPROFILE_DATA_DIR}\"")
294-
else()
295-
target_link_options(${name} PRIVATE
296-
"SHELL:-Xclang-linker -fprofile-instr-generate=\"${LLVM_PROFILE_FILE_PATTERN}\"")
297-
endif()
298-
endif()
303+
_set_pure_swift_profile_flags(${name})
299304

300305
# Export this target.
301306
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name})
@@ -430,4 +435,6 @@ function(add_pure_swift_host_tool name)
430435
else()
431436
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name})
432437
endif()
438+
439+
_set_pure_swift_profile_flags(${name})
433440
endfunction()

0 commit comments

Comments
 (0)