Skip to content

Commit b736a28

Browse files
Wasm/WASI: propagate PATH to UserToolchain to fix sysroot search (#2936)
Currently, when building for WASI, our users need to provide a `destination.json` file that contains a WASI sysroot path. It would be great if the sysroot path could be inferred automatically from PATH, when the appropriate WASI triple is requested as the destination platform. With this PR it's enough to pass `--triple wasm32-unknown-wasi` to `swift build` to build for WASI. Unused private `processEnvironment` property of `UserToolchain` is removed. Co-authored-by: Yuta Saito <[email protected]>
1 parent e0791b3 commit b736a28

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

Sources/Commands/SwiftTool.swift

+12
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,18 @@ public class SwiftTool {
648648
}
649649
if let sdk = self.options.customCompileSDK {
650650
destination.sdk = sdk
651+
} else if let target = destination.target, target.isWASI() {
652+
// Set default SDK path when target is WASI whose SDK is embeded
653+
// in Swift toolchain
654+
do {
655+
let compilers = try UserToolchain.determineSwiftCompilers(binDir: destination.binDir)
656+
destination.sdk = compilers.compile
657+
.parentDirectory // bin
658+
.parentDirectory // usr
659+
.appending(components: "share", "wasi-sysroot")
660+
} catch {
661+
return .failure(error)
662+
}
651663
}
652664
destination.archs = options.archs
653665

Sources/Workspace/UserToolchain.swift

+19-12
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,24 @@ public final class UserToolchain: Toolchain {
100100
#endif
101101
}
102102

103+
public typealias SwiftCompilers = (compile: AbsolutePath, manifest: AbsolutePath)
104+
103105
/// Determines the Swift compiler paths for compilation and manifest parsing.
104-
private static func determineSwiftCompilers(binDir: AbsolutePath, lookup: (String) -> AbsolutePath?, envSearchPaths: [AbsolutePath]) throws -> (compile: AbsolutePath, manifest: AbsolutePath) {
106+
public static func determineSwiftCompilers(binDir: AbsolutePath) throws -> SwiftCompilers {
105107
func validateCompiler(at path: AbsolutePath?) throws {
106108
guard let path = path else { return }
107109
guard localFileSystem.isExecutableFile(path) else {
108110
throw InvalidToolchainDiagnostic("could not find the `swiftc\(hostExecutableSuffix)` at expected path \(path)")
109111
}
110112
}
111113

114+
// Get the search paths from PATH.
115+
let envSearchPaths = getEnvSearchPaths(
116+
pathString: ProcessEnv.path,
117+
currentWorkingDirectory: localFileSystem.currentWorkingDirectory
118+
)
119+
120+
let lookup = { UserToolchain.lookup(variable: $0, searchPaths: envSearchPaths) }
112121
// Get overrides.
113122
let SWIFT_EXEC_MANIFEST = lookup("SWIFT_EXEC_MANIFEST")
114123
let SWIFT_EXEC = lookup("SWIFT_EXEC")
@@ -139,9 +148,6 @@ public final class UserToolchain: Toolchain {
139148
return lookupExecutablePath(filename: ProcessEnv.vars[variable], searchPaths: searchPaths)
140149
}
141150

142-
/// Environment to use when looking up tools.
143-
private let processEnvironment: [String: String]
144-
145151
/// Returns the path to clang compiler tool.
146152
public func getClangCompiler() throws -> AbsolutePath {
147153
// Check if we already computed.
@@ -272,21 +278,20 @@ public final class UserToolchain: Toolchain {
272278

273279
public init(destination: Destination, environment: [String: String] = ProcessEnv.vars) throws {
274280
self.destination = destination
275-
self.processEnvironment = environment
276281

277282
// Get the search paths from PATH.
278-
let searchPaths = getEnvSearchPaths(
279-
pathString: ProcessEnv.path, currentWorkingDirectory: localFileSystem.currentWorkingDirectory)
280-
281-
self.envSearchPaths = searchPaths
283+
self.envSearchPaths = getEnvSearchPaths(
284+
pathString: ProcessEnv.path,
285+
currentWorkingDirectory: localFileSystem.currentWorkingDirectory
286+
)
282287

283288
// Get the binDir from destination.
284289
let binDir = destination.binDir
285290

286-
let swiftCompilers = try UserToolchain.determineSwiftCompilers(binDir: binDir, lookup: { UserToolchain.lookup(variable: $0, searchPaths: searchPaths) }, envSearchPaths: searchPaths)
291+
let swiftCompilers = try UserToolchain.determineSwiftCompilers(binDir: binDir)
287292
self.swiftCompiler = swiftCompilers.compile
288293
self.archs = destination.archs
289-
294+
290295
// Use the triple from destination or compute the host triple using swiftc.
291296
var triple = destination.target ?? Triple.getHostTriple(usingSwiftCompiler: swiftCompilers.compile)
292297

@@ -303,7 +308,9 @@ public final class UserToolchain: Toolchain {
303308
if triple.isDarwin() {
304309
// FIXME: We should have some general utility to find tools.
305310
let xctestFindArgs = ["/usr/bin/xcrun", "--sdk", "macosx", "--find", "xctest"]
306-
self.xctest = try AbsolutePath(validating: Process.checkNonZeroExit(arguments: xctestFindArgs, environment: environment).spm_chomp())
311+
self.xctest = try AbsolutePath(
312+
validating: Process.checkNonZeroExit(arguments: xctestFindArgs, environment: environment
313+
).spm_chomp())
307314
} else {
308315
self.xctest = nil
309316
}

0 commit comments

Comments
 (0)