Skip to content

Automatically find toolchain from swift-syntax-dev-utils using xcrun on macOS #2427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct Build: ParsableCommand {
func run() throws {
let executor = BuildExecutor(
swiftPMBuilder: SwiftPMBuilder(
toolchain: arguments.toolchain,
toolchain: try arguments.toolchain,
buildDir: arguments.buildDir,
multirootDataFile: arguments.multirootDataFile,
release: arguments.release,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct GenerateSourceCode: ParsableCommand {

func run() throws {
let executor = GenerateSourceCodeExecutor(
toolchain: arguments.toolchain,
toolchain: try arguments.toolchain,
verbose: arguments.verbose
)
try executor.run(sourceDir: Paths.sourcesDir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct Test: ParsableCommand {
func run() throws {
let executor = TestExecutor(
swiftPMBuilder: SwiftPMBuilder(
toolchain: arguments.toolchain,
toolchain: try arguments.toolchain,
buildDir: arguments.buildDir,
multirootDataFile: arguments.multirootDataFile,
release: arguments.release,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct VerifySourceCode: ParsableCommand {
try VerifySpiYmlExecutor().run()

try VerifySourceCodeExecutor(
toolchain: arguments.toolchain,
toolchain: try arguments.toolchain,
verbose: arguments.verbose
).run()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@ import Foundation

struct BuildArguments: ParsableArguments {
@Option(
name: .customLong("toolchain"),
help: "The path to the toolchain that shall be used to build SwiftSyntax.",
transform: URL.init(fileURLWithPath:)
)
var toolchain: URL
var _toolchain: URL? = defaultToolchain()

var toolchain: URL {
get throws {
guard let toolchain = self._toolchain else {
throw ScriptExectutionError(message: "Toolchain could not be inferred. Specify toolchain using --toolchain")
}
return toolchain
}
}

@Option(
help: """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ enum Paths {
}
}

static var xcrunExec: URL {
get throws {
return try lookupExecutable(for: "xcrun")
}
}

private static var envSearchPaths: [URL] {
// Compute search paths from PATH variable.
#if os(Windows)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@ import Foundation

struct SourceCodeGeneratorArguments: ParsableArguments {
@Option(
name: .customLong("toolchain"),
help: "The path to the toolchain that shall be used to build SwiftSyntax.",
transform: URL.init(fileURLWithPath:)
)
var toolchain: URL
var _toolchain: URL? = defaultToolchain()

var toolchain: URL {
get throws {
guard let toolchain = self._toolchain else {
throw ScriptExectutionError(message: "Toolchain could not be inferred. Specify toolchain using --toolchain")
}
return toolchain
}
}

@Flag(help: "Enable verbose logging.")
var verbose: Bool = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,25 @@ func withTemporaryDirectory<T>(_ body: (URL) throws -> T) throws -> T {
}
return try body(tempDirURL)
}

/// Infer the default toolchain using `xcrun`.
///
/// Returns `nil` on all platforms except for macOS.
func defaultToolchain() -> URL? {
#if os(macOS)
do {
let swiftcPath = try ProcessRunner(executableURL: try Paths.xcrunExec, arguments: ["--find", "swiftc"]).run(verbose: false).stdout.trimmingCharacters(
in: .whitespacesAndNewlines
)
if swiftcPath.isEmpty {
return nil
}
return URL(fileURLWithPath: swiftcPath).deletingLastPathComponent().deletingLastPathComponent()
} catch {
return nil
}
#else
// Toolchain lookup not implemented without xcrun yet
return nil
#endif
}