Skip to content

Commit cf11a00

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 67b9ee0 commit cf11a00

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

cmake/modules/AddPureSwift.cmake

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,12 @@ function(add_pure_swift_host_library name)
297297
endif()
298298
endif()
299299

300+
# Enable build IDs
301+
if(SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_USE_BUILD_ID)
302+
target_link_options(${name} PRIVATE
303+
"SHELL:-Xlinker --build-id=sha1")
304+
endif()
305+
300306
# Export this target.
301307
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name})
302308
endfunction()
@@ -398,6 +404,12 @@ function(add_pure_swift_host_tool name)
398404
)
399405
endif()
400406

407+
# Enable build IDs
408+
if(SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_USE_BUILD_ID)
409+
target_link_options(${name} PRIVATE
410+
"SHELL:-Xlinker --build-id=sha1")
411+
endif()
412+
401413
# Workaround to touch the library and its objects so that we don't
402414
# continually rebuild (again, see corresponding change in swift-syntax).
403415
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: 14 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)
@@ -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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,11 @@ function(_add_target_variant_link_flags)
595595
endif()
596596
endif()
597597

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

0 commit comments

Comments
 (0)