Skip to content

Wasm/WASI: propagate PATH to UserToolchain to fix sysroot search #2936

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 4 commits into from
Sep 28, 2020
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
12 changes: 12 additions & 0 deletions Sources/Commands/SwiftTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,18 @@ public class SwiftTool {
}
if let sdk = self.options.customCompileSDK {
destination.sdk = sdk
} else if let target = destination.target, target.isWASI() {
// Set default SDK path when target is WASI whose SDK is embeded
// in Swift toolchain
do {
let compilers = try UserToolchain.determineSwiftCompilers(binDir: destination.binDir)
destination.sdk = compilers.compile
.parentDirectory // bin
.parentDirectory // usr
.appending(components: "share", "wasi-sysroot")
} catch {
return .failure(error)
}
}
destination.archs = options.archs

Expand Down
31 changes: 19 additions & 12 deletions Sources/Workspace/UserToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,24 @@ public final class UserToolchain: Toolchain {
#endif
}

public typealias SwiftCompilers = (compile: AbsolutePath, manifest: AbsolutePath)

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

// Get the search paths from PATH.
let envSearchPaths = getEnvSearchPaths(
pathString: ProcessEnv.path,
currentWorkingDirectory: localFileSystem.currentWorkingDirectory
)

let lookup = { UserToolchain.lookup(variable: $0, searchPaths: envSearchPaths) }
// Get overrides.
let SWIFT_EXEC_MANIFEST = lookup("SWIFT_EXEC_MANIFEST")
let SWIFT_EXEC = lookup("SWIFT_EXEC")
Expand Down Expand Up @@ -141,9 +150,6 @@ public final class UserToolchain: Toolchain {
return lookupExecutablePath(filename: ProcessEnv.vars[variable], searchPaths: searchPaths)
}

/// Environment to use when looking up tools.
private let processEnvironment: [String: String]

/// Returns the path to clang compiler tool.
public func getClangCompiler() throws -> AbsolutePath {
// Check if we already computed.
Expand Down Expand Up @@ -266,21 +272,20 @@ public final class UserToolchain: Toolchain {

public init(destination: Destination, environment: [String: String] = ProcessEnv.vars) throws {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should retain the possibility to pass in a custom environment here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing it out, I've reverted that part of the change.

self.destination = destination
self.processEnvironment = environment

// Get the search paths from PATH.
let searchPaths = getEnvSearchPaths(
pathString: ProcessEnv.path, currentWorkingDirectory: localFileSystem.currentWorkingDirectory)

self.envSearchPaths = searchPaths
self.envSearchPaths = getEnvSearchPaths(
pathString: ProcessEnv.path,
currentWorkingDirectory: localFileSystem.currentWorkingDirectory
)

// Get the binDir from destination.
let binDir = destination.binDir

let swiftCompilers = try UserToolchain.determineSwiftCompilers(binDir: binDir, lookup: { UserToolchain.lookup(variable: $0, searchPaths: searchPaths) }, envSearchPaths: searchPaths)
let swiftCompilers = try UserToolchain.determineSwiftCompilers(binDir: binDir)
self.swiftCompiler = swiftCompilers.compile
self.archs = destination.archs

// Use the triple from destination or compute the host triple using swiftc.
var triple = destination.target ?? Triple.getHostTriple(usingSwiftCompiler: swiftCompilers.compile)

Expand All @@ -297,7 +302,9 @@ public final class UserToolchain: Toolchain {
if triple.isDarwin() {
// FIXME: We should have some general utility to find tools.
let xctestFindArgs = ["/usr/bin/xcrun", "--sdk", "macosx", "--find", "xctest"]
self.xctest = try AbsolutePath(validating: Process.checkNonZeroExit(arguments: xctestFindArgs, environment: environment).spm_chomp())
self.xctest = try AbsolutePath(
validating: Process.checkNonZeroExit(arguments: xctestFindArgs, environment: environment
).spm_chomp())
} else {
self.xctest = nil
}
Expand Down