Skip to content

Commit 2ddbf5b

Browse files
committed
[Build][Linux] Set --build-id=sha1 for the linker.
This means we will get build IDs in the tools and standard library, which is useful for debugging (it lets us associate debug symbols with the binaries later on, as well as allowing us to reliably identify exactly which binary we are looking at). rdar://116525111
1 parent 876c056 commit 2ddbf5b

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

cmake/modules/AddPureSwift.cmake

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,12 @@ function(add_pure_swift_host_library name)
302302

303303
_set_pure_swift_profile_flags(${name})
304304

305+
# Enable build IDs
306+
if(SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_USE_BUILD_ID)
307+
target_link_options(${name} PRIVATE
308+
"SHELL:-Xlinker --build-id=sha1")
309+
endif()
310+
305311
# Export this target.
306312
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name})
307313
endfunction()
@@ -403,6 +409,12 @@ function(add_pure_swift_host_tool name)
403409
)
404410
endif()
405411

412+
# Enable build IDs
413+
if(SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_USE_BUILD_ID)
414+
target_link_options(${name} PRIVATE
415+
"SHELL:-Xlinker --build-id=sha1")
416+
endif()
417+
406418
# Workaround to touch the library and its objects so that we don't
407419
# continually rebuild (again, see corresponding change in swift-syntax).
408420
add_custom_command(

cmake/modules/AddSwift.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,12 @@ function(_add_host_variant_link_flags target)
447447
"SHELL:-Xlinker -no_warn_duplicate_libraries")
448448
endif()
449449
450+
# Enable build IDs
451+
if(SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_USE_BUILD_ID)
452+
target_link_options(${target} PRIVATE
453+
"SHELL:-Xlinker --build-id=sha1")
454+
endif()
455+
450456
endfunction()
451457
452458
function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)

cmake/modules/SwiftConfigureSDK.cmake

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ endfunction()
168168
# SWIFT_SDK_${prefix}_IS_SIMULATOR Whether this is a simulator target.
169169
# SWIFT_SDK_${prefix}_ARCH_${ARCH}_TRIPLE Triple name
170170
# SWIFT_SDK_${prefix}_ARCH_${ARCH}_MODULE Module triple name for this SDK
171+
# SWIFT_SDK_${prefix}_USE_BUILD_ID Whether to pass --build-id to the linker
171172
macro(configure_sdk_darwin
172173
prefix name deployment_version xcrun_name
173174
triple_name module_name architectures)
@@ -214,6 +215,7 @@ macro(configure_sdk_darwin
214215
set(SWIFT_SDK_${prefix}_IMPORT_LIBRARY_SUFFIX "")
215216
set(SWIFT_SDK_${prefix}_STATIC_LINKING_SUPPORTED FALSE)
216217
set(SWIFT_SDK_${prefix}_STATIC_ONLY FALSE)
218+
set(SWIFT_SDK_${prefix}_USE_BUILD_ID FALSE)
217219
get_threading_package(${prefix} "darwin" SWIFT_SDK_${prefix}_THREADING_PACKAGE)
218220

219221
set(SWIFT_SDK_${prefix}_ARCHITECTURES ${architectures})
@@ -335,6 +337,15 @@ macro(configure_sdk_unix name architectures)
335337
set(SWIFT_SDK_${prefix}_STATIC_ONLY FALSE)
336338
endif()
337339

340+
if("${prefix}" STREQUAL "LINUX"
341+
OR "${prefix}" STREQUAL "ANDROID"
342+
OR "${prefix}" STREQUAL "FREEBSD"
343+
OR "${prefix}" STREQUAL "OPENBSD")
344+
set(SWIFT_SDK_${prefix}_USE_BUILD_ID TRUE)
345+
else()
346+
set(SWIFT_SDK_${prefix}_USE_BUILD_ID FALSE)
347+
endif()
348+
338349
# GCC on Linux is usually located under `/usr`.
339350
# However, Ubuntu 20.04 ships with another GCC installation under `/`, which
340351
# does not include libstdc++. Swift build scripts pass `--sysroot=/` to

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,11 @@ function(_add_target_variant_link_flags)
599599
endif()
600600
endif()
601601

602+
# Enable build-ids on non-Windows non-Darwin platforms
603+
if(SWIFT_SDK_${LFLAGS_SDK}_USE_BUILD_ID)
604+
list(APPEND result "-Wl,--build-id=sha1")
605+
endif()
606+
602607
# Enable dead stripping. Portions of this logic were copied from llvm's
603608
# `add_link_opts` function (which, perhaps, should have been used here in the
604609
# first place, but at this point it's hard to say whether that's feasible).

0 commit comments

Comments
 (0)