Skip to content

Commit 92bf1e4

Browse files
authored
Merge pull request #354 from owenv/use-target-info
Remove `computeResourceDirPath` and rely on the one reported by `-print-target-info`
2 parents d32cd49 + 5181dd9 commit 92bf1e4

14 files changed

+92
-126
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ public struct Driver {
126126
frontendTargetInfo.targetVariant?.triple
127127
}
128128

129+
/// `true` if the driver should use the static resource directory.
130+
let useStaticResourceDir: Bool
131+
129132
/// The kind of driver.
130133
let driverKind: DriverKind
131134

@@ -342,12 +345,21 @@ public struct Driver {
342345
try Self.applyWorkingDirectory(workingDirectory, to: &self.parsedOptions)
343346
}
344347

348+
let staticExecutable = parsedOptions.hasFlag(positive: .staticExecutable,
349+
negative: .noStaticExecutable,
350+
default: false)
351+
let staticStdlib = parsedOptions.hasFlag(positive: .staticStdlib,
352+
negative: .noStaticStdlib,
353+
default: false)
354+
self.useStaticResourceDir = staticExecutable || staticStdlib
355+
345356
// Build the toolchain and determine target information.
346357
(self.toolchain, self.frontendTargetInfo, self.swiftCompilerPrefixArgs) =
347358
try Self.computeToolchain(
348359
&self.parsedOptions, diagnosticsEngine: diagnosticEngine,
349360
compilerMode: self.compilerMode, env: env,
350-
executor: self.executor, fileSystem: fileSystem)
361+
executor: self.executor, fileSystem: fileSystem,
362+
useStaticResourceDir: self.useStaticResourceDir)
351363

352364
// Classify and collect all of the input files.
353365
let inputFiles = try Self.collectInputFiles(&self.parsedOptions)
@@ -439,10 +451,6 @@ public struct Driver {
439451
parsedOptions: &parsedOptions,
440452
showJobLifecycle: showJobLifecycle)
441453

442-
// Local variable to alias the target triple, because self.targetTriple
443-
// is not available until the end of this initializer.
444-
let targetTriple = self.frontendTargetInfo.target.triple
445-
446454
self.importedObjCHeader = try Self.computeImportedObjCHeader(&parsedOptions, compilerMode: compilerMode, diagnosticEngine: diagnosticEngine)
447455
self.bridgingPrecompiledHeader = try Self.computeBridgingPrecompiledHeader(&parsedOptions,
448456
compilerMode: compilerMode,
@@ -453,7 +461,7 @@ public struct Driver {
453461
&parsedOptions,
454462
diagnosticEngine: diagnosticEngine,
455463
toolchain: toolchain,
456-
targetTriple: targetTriple)
464+
targetInfo: frontendTargetInfo)
457465

458466
// Supplemental outputs.
459467
self.dependenciesFilePath = try Self.computeSupplementaryOutputPath(
@@ -1446,7 +1454,7 @@ extension Driver {
14461454
_ parsedOptions: inout ParsedOptions,
14471455
diagnosticEngine: DiagnosticsEngine,
14481456
toolchain: Toolchain,
1449-
targetTriple: Triple
1457+
targetInfo: FrontendTargetInfo
14501458
) throws -> Set<Sanitizer> {
14511459

14521460
var set = Set<Sanitizer>()
@@ -1459,6 +1467,8 @@ extension Driver {
14591467
if args.isEmpty {
14601468
return set
14611469
}
1470+
1471+
let targetTriple = targetInfo.target.triple
14621472
// Find the sanitizer kind.
14631473
for arg in args {
14641474
guard let sanitizer = Sanitizer(rawValue: arg) else {
@@ -1473,7 +1483,7 @@ extension Driver {
14731483
// enabled.
14741484
var sanitizerSupported = try toolchain.runtimeLibraryExists(
14751485
for: sanitizer,
1476-
targetTriple: targetTriple,
1486+
targetInfo: targetInfo,
14771487
parsedOptions: &parsedOptions,
14781488
isShared: sanitizer != .fuzzer
14791489
)
@@ -1935,7 +1945,8 @@ extension Driver {
19351945
compilerMode: CompilerMode,
19361946
env: [String: String],
19371947
executor: DriverExecutor,
1938-
fileSystem: FileSystem
1948+
fileSystem: FileSystem,
1949+
useStaticResourceDir: Bool
19391950
) throws -> (Toolchain, FrontendTargetInfo, [String]) {
19401951
let explicitTarget = (parsedOptions.getLastArgument(.target)?.asSingle)
19411952
.map {
@@ -1997,6 +2008,7 @@ extension Driver {
19972008
sdkPath: sdkPath, resourceDirPath: resourceDirPath,
19982009
runtimeCompatibilityVersion:
19992010
parsedOptions.getLastArgument(.runtimeCompatibilityVersion)?.asSingle,
2011+
useStaticResourceDir: useStaticResourceDir,
20002012
swiftCompilerPrefixArgs: swiftCompilerPrefixArgs
20012013
),
20022014
capturingJSONOutputAs: FrontendTargetInfo.self,

Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extension DarwinToolchain {
4747
func addLinkRuntimeLibraryRPath(
4848
to commandLine: inout [Job.ArgTemplate],
4949
parsedOptions: inout ParsedOptions,
50-
targetTriple: Triple,
50+
targetInfo: FrontendTargetInfo,
5151
darwinLibName: String
5252
) throws {
5353
// Adding the rpaths might negatively interact when other rpaths are involved,
@@ -66,7 +66,7 @@ extension DarwinToolchain {
6666

6767

6868
let clangPath = try clangLibraryPath(
69-
for: targetTriple,
69+
for: targetInfo,
7070
parsedOptions: &parsedOptions)
7171
commandLine.appendFlag("-rpath")
7272
commandLine.appendPath(clangPath)
@@ -75,7 +75,7 @@ extension DarwinToolchain {
7575
func addLinkSanitizerLibArgsForDarwin(
7676
to commandLine: inout [Job.ArgTemplate],
7777
parsedOptions: inout ParsedOptions,
78-
targetTriple: Triple,
78+
targetInfo: FrontendTargetInfo,
7979
sanitizer: Sanitizer,
8080
isShared: Bool
8181
) throws {
@@ -87,21 +87,21 @@ extension DarwinToolchain {
8787

8888
let sanitizerName = try runtimeLibraryName(
8989
for: sanitizer,
90-
targetTriple: targetTriple,
90+
targetTriple: targetInfo.target.triple,
9191
isShared: isShared
9292
)
9393
try addLinkRuntimeLibrary(
9494
named: sanitizerName,
9595
to: &commandLine,
96-
for: targetTriple,
96+
for: targetInfo,
9797
parsedOptions: &parsedOptions
9898
)
9999

100100
if isShared {
101101
try addLinkRuntimeLibraryRPath(
102102
to: &commandLine,
103103
parsedOptions: &parsedOptions,
104-
targetTriple: targetTriple,
104+
targetInfo: targetInfo,
105105
darwinLibName: sanitizerName
106106
)
107107
}
@@ -110,13 +110,13 @@ extension DarwinToolchain {
110110
private func addProfileGenerationArgs(
111111
to commandLine: inout [Job.ArgTemplate],
112112
parsedOptions: inout ParsedOptions,
113-
targetTriple: Triple
113+
targetInfo: FrontendTargetInfo
114114
) throws {
115115
guard parsedOptions.hasArgument(.profileGenerate) else { return }
116-
let clangPath = try clangLibraryPath(for: targetTriple,
116+
let clangPath = try clangLibraryPath(for: targetInfo,
117117
parsedOptions: &parsedOptions)
118118

119-
for runtime in targetTriple.darwinPlatform!.profileLibraryNameSuffixes {
119+
for runtime in targetInfo.target.triple.darwinPlatform!.profileLibraryNameSuffixes {
120120
let clangRTPath = clangPath
121121
.appending(component: "libclang_rt.profile_\(runtime).a")
122122
commandLine.appendPath(clangRTPath)
@@ -221,7 +221,7 @@ extension DarwinToolchain {
221221
targetTriple.darwinPlatform!.with(.device)!.libraryNameSuffix
222222
let compilerRTPath =
223223
try clangLibraryPath(
224-
for: targetTriple,
224+
for: targetInfo,
225225
parsedOptions: &parsedOptions)
226226
.appending(component: "libclang_rt.\(darwinPlatformSuffix).a")
227227
if try fileSystem.exists(compilerRTPath) {
@@ -244,7 +244,7 @@ extension DarwinToolchain {
244244
try addLinkSanitizerLibArgsForDarwin(
245245
to: &commandLine,
246246
parsedOptions: &parsedOptions,
247-
targetTriple: targetTriple,
247+
targetInfo: targetInfo,
248248
sanitizer: sanitizer,
249249
isShared: sanitizer != .fuzzer
250250
)
@@ -282,7 +282,7 @@ extension DarwinToolchain {
282282
try addProfileGenerationArgs(
283283
to: &commandLine,
284284
parsedOptions: &parsedOptions,
285-
targetTriple: targetTriple
285+
targetInfo: targetInfo
286286
)
287287

288288
commandLine.appendFlags(

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,7 @@ extension Driver {
193193
commandLine.appendFlag(.resourceDir)
194194
commandLine.appendPath(frontendTargetInfo.runtimeResourcePath.path)
195195

196-
if parsedOptions.hasFlag(positive: .staticExecutable,
197-
negative: .noStaticExecutable,
198-
default: false) ||
199-
parsedOptions.hasFlag(positive: .staticStdlib,
200-
negative: .noStaticStdlib,
201-
default: false) {
196+
if self.useStaticResourceDir {
202197
commandLine.appendFlag("-use-static-resource-dir")
203198
}
204199

Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ extension GenericUnixToolchain {
132132
let hasRuntimeArgs = !(staticStdlib || staticExecutable)
133133

134134
let runtimePaths = try runtimeLibraryPaths(
135-
for: targetTriple,
135+
for: targetInfo,
136136
parsedOptions: &parsedOptions,
137137
sdkPath: targetInfo.sdkPath?.path,
138138
isShared: hasRuntimeArgs
@@ -149,15 +149,11 @@ extension GenericUnixToolchain {
149149
}
150150
}
151151

152-
let sharedResourceDirPath = try computeResourceDirPath(
153-
for: targetTriple,
154-
parsedOptions: &parsedOptions,
155-
isShared: true
156-
)
157-
158-
let swiftrtPath = sharedResourceDirPath
152+
let swiftrtPath = targetInfo.runtimeResourcePath.path
159153
.appending(
160-
components: String(majorArchitectureName(for: targetTriple)), "swiftrt.o"
154+
components: targetTriple.platformName() ?? "",
155+
String(majorArchitectureName(for: targetTriple)),
156+
"swiftrt.o"
161157
)
162158
commandLine.appendPath(swiftrtPath)
163159

@@ -199,11 +195,8 @@ extension GenericUnixToolchain {
199195
// Link the standard library. In two paths, we do this using a .lnk file
200196
// if we're going that route, we'll set `linkFilePath` to the path to that
201197
// file.
202-
var linkFilePath: VirtualPath? = try computeResourceDirPath(
203-
for: targetTriple,
204-
parsedOptions: &parsedOptions,
205-
isShared: false
206-
)
198+
var linkFilePath: VirtualPath? = targetInfo.runtimeResourcePath.path
199+
.appending(component: targetTriple.platformName() ?? "")
207200

208201
if staticExecutable {
209202
linkFilePath = linkFilePath?.appending(component: "static-executable-args.lnk")
@@ -241,8 +234,7 @@ extension GenericUnixToolchain {
241234
}
242235

243236
if parsedOptions.hasArgument(.profileGenerate) {
244-
let libProfile = sharedResourceDirPath
245-
.parentDirectory // remove platform name
237+
let libProfile = targetInfo.runtimeResourcePath.path
246238
.appending(components: "clang", "lib", targetTriple.osName,
247239
"libclang_rt.profile-\(targetTriple.archName).a")
248240
commandLine.appendPath(libProfile)

Sources/SwiftDriver/Jobs/InterpretJob.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extension Driver {
3838

3939
let extraEnvironment = try toolchain.platformSpecificInterpreterEnvironmentVariables(
4040
env: self.env, parsedOptions: &parsedOptions,
41-
sdkPath: frontendTargetInfo.sdkPath?.path, targetTriple: self.targetTriple)
41+
sdkPath: frontendTargetInfo.sdkPath?.path, targetInfo: self.frontendTargetInfo)
4242

4343
return Job(
4444
moduleName: moduleOutputInfo.name,

Sources/SwiftDriver/Jobs/Planning.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -438,15 +438,6 @@ extension Driver {
438438
if parsedOptions.hasArgument(.printTargetInfo) {
439439
let sdkPath = try parsedOptions.getLastArgument(.sdk).map { try VirtualPath(path: $0.asSingle) }
440440
let resourceDirPath = try parsedOptions.getLastArgument(.resourceDir).map { try VirtualPath(path: $0.asSingle) }
441-
var useStaticResourceDir = false
442-
if parsedOptions.hasFlag(positive: .staticExecutable,
443-
negative: .noStaticExecutable,
444-
default: false) ||
445-
parsedOptions.hasFlag(positive: .staticStdlib,
446-
negative: .noStaticStdlib,
447-
default: false) {
448-
useStaticResourceDir = true
449-
}
450441

451442
return try toolchain.printTargetInfoJob(target: targetTriple,
452443
targetVariant: targetVariantTriple,

Sources/SwiftDriver/Jobs/PrintTargetInfoJob.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ extension SwiftVersion: Codable {
9797
let librariesRequireRPath: Bool
9898
}
9999

100-
struct Paths: Codable {
100+
@_spi(Testing) public struct Paths: Codable {
101101
/// The path to the SDK, if provided.
102-
let sdkPath: TextualVirtualPath?
103-
let runtimeLibraryPaths: [TextualVirtualPath]
104-
let runtimeLibraryImportPaths: [TextualVirtualPath]
105-
let runtimeResourcePath: TextualVirtualPath
102+
public let sdkPath: TextualVirtualPath?
103+
public let runtimeLibraryPaths: [TextualVirtualPath]
104+
public let runtimeLibraryImportPaths: [TextualVirtualPath]
105+
public let runtimeResourcePath: TextualVirtualPath
106106
}
107107

108108
var compilerVersion: String
@@ -113,7 +113,7 @@ extension SwiftVersion: Codable {
113113

114114
// Make members of `FrontendTargetInfo.Paths` accessible on `FrontendTargetInfo`.
115115
extension FrontendTargetInfo {
116-
subscript<T>(dynamicMember dynamicMember: KeyPath<FrontendTargetInfo.Paths, T>) -> T {
116+
@_spi(Testing) public subscript<T>(dynamicMember dynamicMember: KeyPath<FrontendTargetInfo.Paths, T>) -> T {
117117
self.paths[keyPath: dynamicMember]
118118
}
119119
}

Sources/SwiftDriver/Jobs/Toolchain+InterpreterSupport.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ extension DarwinToolchain {
3535
env: [String : String],
3636
parsedOptions: inout ParsedOptions,
3737
sdkPath: VirtualPath?,
38-
targetTriple: Triple) throws -> [String: String] {
38+
targetInfo: FrontendTargetInfo) throws -> [String: String] {
3939
var envVars: [String: String] = [:]
4040

4141
let runtimePaths = try runtimeLibraryPaths(
42-
for: targetTriple,
42+
for: targetInfo,
4343
parsedOptions: &parsedOptions,
4444
sdkPath: sdkPath,
4545
isShared: true
@@ -63,11 +63,11 @@ extension GenericUnixToolchain {
6363
env: [String : String],
6464
parsedOptions: inout ParsedOptions,
6565
sdkPath: VirtualPath?,
66-
targetTriple: Triple) throws -> [String: String] {
66+
targetInfo: FrontendTargetInfo) throws -> [String: String] {
6767
var envVars: [String: String] = [:]
6868

6969
let runtimePaths = try runtimeLibraryPaths(
70-
for: targetTriple,
70+
for: targetInfo,
7171
parsedOptions: &parsedOptions,
7272
sdkPath: sdkPath,
7373
isShared: true

0 commit comments

Comments
 (0)