Skip to content

Commit 955dd5a

Browse files
committed
[Linux] Add build-id support.
Add a `-build-id` option to the driver to let users control the build ID setting used by the linker. Pass `--build-id` to the linker by default, so that build IDs are enabled by default but using the linker's default type; if someone wishes to specify a particular option, they can do so using the new driver option. Also turn on build IDs when *building* the driver. rdar://116798309
1 parent b0300a8 commit 955dd5a

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ set(CMAKE_Swift_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
3131
set(CMAKE_Swift_LANGUAGE_VERSION 5)
3232
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
3333

34+
# Generate build-ids
35+
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin"
36+
AND NOT CMAKE_SYSTEM_NAME STREQUAL "Windows")
37+
add_link_options("LINKER:--build-id=sha1")
38+
endif()
39+
3440
# ensure Swift compiler can find _CSwiftScan
3541
add_compile_options($<$<COMPILE_LANGUAGE:Swift>:-I$<SEMICOLON>${CMAKE_CURRENT_SOURCE_DIR}/Sources/CSwiftScan/include>)
3642

Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift

+13
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ extension GenericUnixToolchain {
106106
commandLine.appendFlag("-pie")
107107
}
108108

109+
// On some platforms we want to enable --build-id
110+
if targetTriple.os == .linux
111+
|| targetTriple.os == .freeBSD
112+
|| targetTriple.os == .openbsd
113+
|| parsedOptions.hasArgument(.buildId) {
114+
commandLine.appendFlag("-Xlinker")
115+
if let buildId = parsedOptions.getLastArgument(.buildId)?.asSingle {
116+
commandLine.appendFlag("--build-id=\(buildId)")
117+
} else {
118+
commandLine.appendFlag("--build-id")
119+
}
120+
}
121+
109122
let staticStdlib = parsedOptions.hasFlag(positive: .staticStdlib,
110123
negative: .noStaticStdlib,
111124
default: false)

Sources/SwiftOptions/Options.swift

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ extension Option {
5858
public static let bridgingHeaderDirectoryForPrint: Option = Option("-bridging-header-directory-for-print", .separate, attributes: [.helpHidden, .frontend, .noDriver], metaVar: "<path>", helpText: "Directory for bridging header to be printed in compatibility header")
5959
public static let bridgingHeaderPchKey: Option = Option("-bridging-header-pch-key", .separate, attributes: [.helpHidden, .frontend, .noDriver], helpText: "Cache Key for bridging header pch")
6060
public static let bsdk: Option = Option("-bsdk", .joinedOrSeparate, attributes: [.noDriver, .argumentIsPath], helpText: "path to the baseline SDK to import frameworks")
61+
public static let buildIdEQ: Option = Option("-build-id=", .joined, alias: Option.buildId)
62+
public static let buildId: Option = Option("-build-id", .joinedOrSeparate, metaVar: "<build-id>", helpText: "Specify the build ID argument passed to linker")
6163
public static let buildModuleFromParseableInterface: Option = Option("-build-module-from-parseable-interface", .flag, alias: Option.compileModuleFromInterface, attributes: [.helpHidden, .frontend, .noDriver], group: .modes)
6264
public static let bypassBatchModeChecks: Option = Option("-bypass-batch-mode-checks", .flag, attributes: [.helpHidden, .frontend, .noDriver], helpText: "Bypass checks for batch-mode errors.")
6365
public static let bypassResilience: Option = Option("-bypass-resilience-checks", .flag, attributes: [.helpHidden, .frontend, .noDriver], helpText: "Ignore all checks for module resilience.")
@@ -921,6 +923,8 @@ extension Option {
921923
Option.bridgingHeaderDirectoryForPrint,
922924
Option.bridgingHeaderPchKey,
923925
Option.bsdk,
926+
Option.buildIdEQ,
927+
Option.buildId,
924928
Option.buildModuleFromParseableInterface,
925929
Option.bypassBatchModeChecks,
926930
Option.bypassResilience,

0 commit comments

Comments
 (0)