Skip to content

Commit ef6c604

Browse files
committed
Automatically find toolchain from swift-syntax-dev-utils using xcrun on macOS
1 parent a7fa220 commit ef6c604

File tree

8 files changed

+54
-6
lines changed

8 files changed

+54
-6
lines changed

SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/commands/Build.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct Build: ParsableCommand {
2424
func run() throws {
2525
let executor = BuildExecutor(
2626
swiftPMBuilder: SwiftPMBuilder(
27-
toolchain: arguments.toolchain,
27+
toolchain: try arguments.toolchain,
2828
buildDir: arguments.buildDir,
2929
multirootDataFile: arguments.multirootDataFile,
3030
release: arguments.release,

SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/commands/GenerateSourceCode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct GenerateSourceCode: ParsableCommand {
2323

2424
func run() throws {
2525
let executor = GenerateSourceCodeExecutor(
26-
toolchain: arguments.toolchain,
26+
toolchain: try arguments.toolchain,
2727
verbose: arguments.verbose
2828
)
2929
try executor.run(sourceDir: Paths.sourcesDir)

SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/commands/Test.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct Test: ParsableCommand {
2424
func run() throws {
2525
let executor = TestExecutor(
2626
swiftPMBuilder: SwiftPMBuilder(
27-
toolchain: arguments.toolchain,
27+
toolchain: try arguments.toolchain,
2828
buildDir: arguments.buildDir,
2929
multirootDataFile: arguments.multirootDataFile,
3030
release: arguments.release,

SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/commands/VerifySourceCode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct VerifySourceCode: ParsableCommand {
3333
try VerifySpiYmlExecutor().run()
3434

3535
try VerifySourceCodeExecutor(
36-
toolchain: arguments.toolchain,
36+
toolchain: try arguments.toolchain,
3737
verbose: arguments.verbose
3838
).run()
3939
}

SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/common/BuildArguments.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,20 @@ import Foundation
1515

1616
struct BuildArguments: ParsableArguments {
1717
@Option(
18+
name: .customLong("toolchain"),
1819
help: "The path to the toolchain that shall be used to build SwiftSyntax.",
1920
transform: URL.init(fileURLWithPath:)
2021
)
21-
var toolchain: URL
22+
var _toolchain: URL? = defaultToolchain()
23+
24+
var toolchain: URL {
25+
get throws {
26+
guard let toolchain = self._toolchain else {
27+
throw ScriptExectutionError(message: "Toolchain could not be inferred. Specify toolchain using --toolchain")
28+
}
29+
return toolchain
30+
}
31+
}
2232

2333
@Option(
2434
help: """

SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/common/Paths.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ enum Paths {
7878
}
7979
}
8080

81+
static var xcrunExec: URL {
82+
get throws {
83+
return try lookupExecutable(for: "xcrun")
84+
}
85+
}
86+
8187
private static var envSearchPaths: [URL] {
8288
// Compute search paths from PATH variable.
8389
#if os(Windows)

SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/common/SourceCodeGeneratorArguments.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,20 @@ import Foundation
1515

1616
struct SourceCodeGeneratorArguments: ParsableArguments {
1717
@Option(
18+
name: .customLong("toolchain"),
1819
help: "The path to the toolchain that shall be used to build SwiftSyntax.",
1920
transform: URL.init(fileURLWithPath:)
2021
)
21-
var toolchain: URL
22+
var _toolchain: URL? = defaultToolchain()
23+
24+
var toolchain: URL {
25+
get throws {
26+
guard let toolchain = self._toolchain else {
27+
throw ScriptExectutionError(message: "Toolchain could not be inferred. Specify toolchain using --toolchain")
28+
}
29+
return toolchain
30+
}
31+
}
2232

2333
@Flag(help: "Enable verbose logging.")
2434
var verbose: Bool = false

SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/common/Utils.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,25 @@ func withTemporaryDirectory<T>(_ body: (URL) throws -> T) throws -> T {
2222
}
2323
return try body(tempDirURL)
2424
}
25+
26+
/// Infer the default toolchain using `xcrun`.
27+
///
28+
/// Returns `nil` on all platforms except for macOS.
29+
func defaultToolchain() -> URL? {
30+
#if os(macOS)
31+
do {
32+
let swiftcPath = try ProcessRunner(executableURL: try Paths.xcrunExec, arguments: ["--find", "swiftc"]).run(verbose: false).stdout.trimmingCharacters(
33+
in: .whitespacesAndNewlines
34+
)
35+
if swiftcPath.isEmpty {
36+
return nil
37+
}
38+
return URL(fileURLWithPath: swiftcPath).deletingLastPathComponent().deletingLastPathComponent()
39+
} catch {
40+
return nil
41+
}
42+
#else
43+
// Toolchain lookup not implemented without xcrun yet
44+
return nil
45+
#endif
46+
}

0 commit comments

Comments
 (0)