diff --git a/Sources/Commands/SwiftTool.swift b/Sources/Commands/SwiftTool.swift index 63cb29dfb38..8fc45abb2d2 100644 --- a/Sources/Commands/SwiftTool.swift +++ b/Sources/Commands/SwiftTool.swift @@ -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 diff --git a/Sources/Workspace/UserToolchain.swift b/Sources/Workspace/UserToolchain.swift index 7cc716f1559..1d3baa1b7e4 100644 --- a/Sources/Workspace/UserToolchain.swift +++ b/Sources/Workspace/UserToolchain.swift @@ -102,8 +102,10 @@ 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 { @@ -111,6 +113,13 @@ public final class UserToolchain: Toolchain { } } + // 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") @@ -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. @@ -266,21 +272,20 @@ public final class UserToolchain: Toolchain { public init(destination: Destination, environment: [String: String] = ProcessEnv.vars) throws { 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) @@ -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 }