Skip to content

Commit a14a5c7

Browse files
authored
Merge pull request #151 from DougGregor/print-target-info-all-the-args
Pass the SDK path and resource directory through to -print-target-info.
2 parents e1089b5 + b67e8ae commit a14a5c7

File tree

6 files changed

+57
-29
lines changed

6 files changed

+57
-29
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ public struct Driver {
143143
public let incrementalCompilation: IncrementalCompilation
144144

145145
/// The path of the SDK.
146-
public let sdkPath: String?
146+
public var sdkPath: String? {
147+
frontendTargetInfo.paths.sdkPath
148+
}
147149

148150
/// The path to the imported Objective-C header.
149151
public let importedObjCHeader: VirtualPath?
@@ -251,9 +253,14 @@ public struct Driver {
251253
self.optionTable = OptionTable()
252254
self.parsedOptions = try optionTable.parse(Array(args), for: self.driverKind)
253255

256+
// Determine the compilation mode.
257+
self.compilerMode = try Self.computeCompilerMode(&parsedOptions, driverKind: driverKind, diagnosticsEngine: diagnosticEngine)
258+
259+
// Build the toolchain and determine target information.
254260
(self.toolchain, self.frontendTargetInfo, self.swiftCompilerPrefixArgs) =
255261
try Self.computeToolchain(
256-
&self.parsedOptions, diagnosticsEngine: diagnosticEngine, env: env,
262+
&self.parsedOptions, diagnosticsEngine: diagnosticEngine,
263+
compilerMode: self.compilerMode, env: env,
257264
executor: self.executor, fileSystem: fileSystem)
258265

259266
// Compute the working directory.
@@ -296,9 +303,6 @@ public struct Driver {
296303
self.outputFileMap = outputFileMap
297304
}
298305

299-
// Determine the compilation mode.
300-
self.compilerMode = try Self.computeCompilerMode(&parsedOptions, driverKind: driverKind, diagnosticsEngine: diagnosticEngine)
301-
302306
// Figure out the primary outputs from the driver.
303307
(self.compilerOutputType, self.linkerOutputType) = Self.determinePrimaryOutputs(&parsedOptions, driverKind: driverKind, diagnosticsEngine: diagnosticEngine)
304308

@@ -333,8 +337,6 @@ public struct Driver {
333337
// Local variable to alias the target triple, because self.targetTriple
334338
// is not available until the end of this initializer.
335339
let targetTriple = self.frontendTargetInfo.target.triple
336-
self.sdkPath = Self.computeSDKPath(&parsedOptions, compilerMode: compilerMode, toolchain: toolchain, targetTriple: targetTriple,
337-
fileSystem: fileSystem, diagnosticsEngine: diagnosticEngine, env: env)
338340

339341
self.importedObjCHeader = try Self.computeImportedObjCHeader(&parsedOptions, compilerMode: compilerMode, diagnosticEngine: diagnosticEngine)
340342
self.bridgingPrecompiledHeader = try Self.computeBridgingPrecompiledHeader(&parsedOptions,
@@ -1411,25 +1413,20 @@ extension Driver {
14111413
_ parsedOptions: inout ParsedOptions,
14121414
compilerMode: CompilerMode,
14131415
toolchain: Toolchain,
1414-
targetTriple: Triple,
1416+
targetTriple: Triple?,
14151417
fileSystem: FileSystem,
14161418
diagnosticsEngine: DiagnosticsEngine,
14171419
env: [String: String]
1418-
) -> String? {
1420+
) -> VirtualPath? {
14191421
var sdkPath: String?
14201422

14211423
if let arg = parsedOptions.getLastArgument(.sdk) {
14221424
sdkPath = arg.asSingle
14231425
} else if let SDKROOT = env["SDKROOT"] {
14241426
sdkPath = SDKROOT
14251427
} else if compilerMode == .immediate || compilerMode == .repl {
1426-
if targetTriple.isMacOSX == true {
1427-
// In immediate modes, use the SDK provided by xcrun.
1428-
// This will prefer the SDK alongside the Swift found by "xcrun swift".
1429-
// We don't do this in compilation modes because defaulting to the
1430-
// latest SDK may not be intended.
1431-
sdkPath = try? toolchain.defaultSDKPath()?.pathString
1432-
}
1428+
// In immediate modes, query the toolchain for a default SDK.
1429+
sdkPath = try? toolchain.defaultSDKPath(targetTriple)?.pathString
14331430
}
14341431

14351432
// An empty string explicitly clears the SDK.
@@ -1451,16 +1448,18 @@ extension Driver {
14511448
path = AbsolutePath(sdkPath, relativeTo: cwd)
14521449
} else {
14531450
diagnosticsEngine.emit(.warning_no_such_sdk(sdkPath))
1454-
return sdkPath
1451+
return nil
14551452
}
14561453

14571454
if !fileSystem.exists(path) {
14581455
diagnosticsEngine.emit(.warning_no_such_sdk(sdkPath))
14591456
}
14601457
// .. else check if SDK is too old (we need target triple to diagnose that).
1458+
1459+
return .absolute(path)
14611460
}
14621461

1463-
return sdkPath
1462+
return nil
14641463
}
14651464
}
14661465

@@ -1570,6 +1569,7 @@ extension Driver {
15701569
static func computeToolchain(
15711570
_ parsedOptions: inout ParsedOptions,
15721571
diagnosticsEngine: DiagnosticsEngine,
1572+
compilerMode: CompilerMode,
15731573
env: [String: String],
15741574
executor: DriverExecutor,
15751575
fileSystem: FileSystem
@@ -1583,9 +1583,13 @@ extension Driver {
15831583
Triple($0, normalizing: true)
15841584
}
15851585

1586-
// FIXME: Compute these.
1587-
let sdkPath: VirtualPath? = nil
1588-
let resourceDirPath: VirtualPath? = nil
1586+
// Determine the resource directory.
1587+
let resourceDirPath: VirtualPath?
1588+
if let resourceDirArg = parsedOptions.getLastArgument(.resourceDir) {
1589+
resourceDirPath = try VirtualPath(path: resourceDirArg.asSingle)
1590+
} else {
1591+
resourceDirPath = nil
1592+
}
15891593

15901594
let toolchainType = try explicitTarget?.toolchainType(diagnosticsEngine) ??
15911595
defaultToolchainType
@@ -1609,6 +1613,12 @@ extension Driver {
16091613
swiftCompilerPrefixArgs = []
16101614
}
16111615

1616+
// Find the SDK, if any.
1617+
let sdkPath: VirtualPath? = Self.computeSDKPath(
1618+
&parsedOptions, compilerMode: compilerMode, toolchain: toolchain,
1619+
targetTriple: explicitTarget, fileSystem: fileSystem,
1620+
diagnosticsEngine: diagnosticsEngine, env: env)
1621+
16121622
// Query the frontend to for target information.
16131623
var info = try executor.execute(
16141624
job: toolchain.printTargetInfoJob(

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ extension Driver {
178178
commandLine.appendPath(.absolute(workingDirectory))
179179
}
180180

181+
// Resource directory.
182+
commandLine.appendFlag(.resourceDir)
183+
commandLine.appendPath(
184+
try AbsolutePath(validating: frontendTargetInfo.paths.runtimeResourcePath))
185+
181186
// -g implies -enable-anonymous-context-mangled-names, because the extra
182187
// metadata aids debugging.
183188
if parsedOptions.getLast(in: .g) != nil {

Sources/SwiftDriver/Jobs/PrintTargetInfoJob.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ public struct FrontendTargetInfo: Codable {
7272
}
7373

7474
struct Paths: Codable {
75+
/// The path to the SDK, if provided.
76+
let sdkPath: String?
7577
let runtimeLibraryPaths: [String]
7678
let runtimeLibraryImportPaths: [String]
7779
let runtimeResourcePath: String

Sources/SwiftDriver/Toolchains/DarwinToolchain.swift

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,23 @@ public final class DarwinToolchain: Toolchain {
120120
resourcesDirectory.map { $0.appending(RelativePath("../clang/lib/darwin/libclang_rt.osx.a")) }
121121
}
122122

123-
public func defaultSDKPath() throws -> AbsolutePath? {
124-
let result = try executor.checkNonZeroExit(
125-
args: "xcrun", "-sdk", "macosx", "--show-sdk-path",
126-
environment: env
127-
).spm_chomp()
128-
return AbsolutePath(result)
123+
public func defaultSDKPath(_ target: Triple?) throws -> AbsolutePath? {
124+
let hostIsMacOS: Bool
125+
#if os(macOS)
126+
hostIsMacOS = true
127+
#else
128+
hostIsMacOS = false
129+
#endif
130+
131+
if target?.isMacOSX == true || hostIsMacOS {
132+
let result = try executor.checkNonZeroExit(
133+
args: "xcrun", "-sdk", "macosx", "--show-sdk-path",
134+
environment: env
135+
).spm_chomp()
136+
return AbsolutePath(result)
137+
}
138+
139+
return nil
129140
}
130141

131142
public var shouldStoreInvocationInDebugInfo: Bool {

Sources/SwiftDriver/Toolchains/GenericUnixToolchain.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public final class GenericUnixToolchain: Toolchain {
7878
toolPaths[tool] = path
7979
}
8080

81-
public func defaultSDKPath() throws -> AbsolutePath? {
81+
public func defaultSDKPath(_ target: Triple?) throws -> AbsolutePath? {
8282
return nil
8383
}
8484

Sources/SwiftDriver/Toolchains/Toolchain.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public protocol Toolchain {
4545
func overrideToolPath(_ tool: Tool, path: AbsolutePath)
4646

4747
/// Returns path of the default SDK, if there is one.
48-
func defaultSDKPath() throws -> AbsolutePath?
48+
func defaultSDKPath(_ target: Triple?) throws -> AbsolutePath?
4949

5050
/// When the compiler invocation should be stored in debug information.
5151
var shouldStoreInvocationInDebugInfo: Bool { get }

0 commit comments

Comments
 (0)