Skip to content

Commit

Permalink
[Linux] Add build-id support.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
al45tair committed Jul 23, 2024
1 parent 661e0bc commit 5540412
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ set(CMAKE_Swift_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
set(CMAKE_Swift_LANGUAGE_VERSION 5)
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)

# Generate build-ids
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin"
AND NOT CMAKE_SYSTEM_NAME STREQUAL "Windows")
add_link_options("LINKER:--build-id=sha1")
endif()

# ensure Swift compiler can find _CSwiftScan
add_compile_options($<$<COMPILE_LANGUAGE:Swift>:-I$<SEMICOLON>${CMAKE_CURRENT_SOURCE_DIR}/Sources/CSwiftScan/include>)

Expand Down
13 changes: 13 additions & 0 deletions Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ extension GenericUnixToolchain {
commandLine.appendFlag("-pie")
}

// On some platforms we want to enable --build-id
if targetTriple.os == .linux
|| targetTriple.os == .freeBSD
|| targetTriple.os == .openbsd
|| parsedOptions.hasArgument(.buildId) {
commandLine.appendFlag("-Xlinker")
if let buildId = parsedOptions.getLastArgument(.buildId)?.asSingle {
commandLine.appendFlag("--build-id=\(buildId)")
} else {
commandLine.appendFlag("--build-id")
}
}

let staticStdlib = parsedOptions.hasFlag(positive: .staticStdlib,
negative: .noStaticStdlib,
default: false)
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftOptions/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ extension Option {
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")
public static let bridgingHeaderPchKey: Option = Option("-bridging-header-pch-key", .separate, attributes: [.helpHidden, .frontend, .noDriver], helpText: "Cache Key for bridging header pch")
public static let bsdk: Option = Option("-bsdk", .joinedOrSeparate, attributes: [.noDriver, .argumentIsPath], helpText: "path to the baseline SDK to import frameworks")
public static let buildIdEQ: Option = Option("-build-id=", .joined, alias: Option.buildId)
public static let buildId: Option = Option("-build-id", .joinedOrSeparate, metaVar: "<build-id>", helpText: "Specify the build ID argument passed to linker")
public static let buildModuleFromParseableInterface: Option = Option("-build-module-from-parseable-interface", .flag, alias: Option.compileModuleFromInterface, attributes: [.helpHidden, .frontend, .noDriver], group: .modes)
public static let bypassBatchModeChecks: Option = Option("-bypass-batch-mode-checks", .flag, attributes: [.helpHidden, .frontend, .noDriver], helpText: "Bypass checks for batch-mode errors.")
public static let bypassResilience: Option = Option("-bypass-resilience-checks", .flag, attributes: [.helpHidden, .frontend, .noDriver], helpText: "Ignore all checks for module resilience.")
Expand Down Expand Up @@ -929,6 +931,8 @@ extension Option {
Option.bridgingHeaderDirectoryForPrint,
Option.bridgingHeaderPchKey,
Option.bsdk,
Option.buildIdEQ,
Option.buildId,
Option.buildModuleFromParseableInterface,
Option.bypassBatchModeChecks,
Option.bypassResilience,
Expand Down

0 comments on commit 5540412

Please sign in to comment.