Skip to content

Commit 35f123b

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 35f123b

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

cmake/modules/AddPureSwift.cmake

+12
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

+6
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

+14
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)
@@ -216,6 +217,9 @@ macro(configure_sdk_darwin
216217
set(SWIFT_SDK_${prefix}_STATIC_ONLY FALSE)
217218
get_threading_package(${prefix} "darwin" SWIFT_SDK_${prefix}_THREADING_PACKAGE)
218219

220+
# On Darwin we get UUIDs automatically, without the --build-id flag
221+
set(SWIFT_SDK_${prefix}_USE_BUILD_ID FALSE)
222+
219223
set(SWIFT_SDK_${prefix}_ARCHITECTURES ${architectures})
220224
if(SWIFT_DARWIN_SUPPORTED_ARCHS)
221225
list_intersect(
@@ -335,6 +339,15 @@ macro(configure_sdk_unix name architectures)
335339
set(SWIFT_SDK_${prefix}_STATIC_ONLY FALSE)
336340
endif()
337341

342+
if("${prefix}" STREQUAL "LINUX"
343+
OR "${prefix}" STREQUAL "ANDROID"
344+
OR "${prefix}" STREQUAL "FREEBSD"
345+
OR "${prefix}" STREQUAL "OPENBSD")
346+
set(SWIFT_SDK_${prefix}_USE_BUILD_ID TRUE)
347+
else()
348+
set(SWIFT_SDK_${prefix}_USE_BUILD_ID FALSE)
349+
endif()
350+
338351
# GCC on Linux is usually located under `/usr`.
339352
# However, Ubuntu 20.04 ships with another GCC installation under `/`, which
340353
# does not include libstdc++. Swift build scripts pass `--sysroot=/` to
@@ -494,6 +507,7 @@ macro(configure_sdk_windows name environment architectures)
494507
set(SWIFT_SDK_${prefix}_IMPORT_LIBRARY_SUFFIX ".lib")
495508
set(SWIFT_SDK_${prefix}_STATIC_LINKING_SUPPORTED FALSE)
496509
set(SWIFT_SDK_${prefix}_STATIC_ONLY FALSE)
510+
set(SWIFT_SDK_${prefix}_USE_BUILD_ID FALSE)
497511
get_threading_package(${prefix} "win32" SWIFT_SDK_${prefix}_THREADING_PACKAGE)
498512

499513
foreach(arch ${architectures})

stdlib/cmake/modules/AddSwiftStdlib.cmake

+5
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)