From b9e3526e400aee10e361930b21d6e8a00af98b41 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 6 Mar 2020 19:13:29 +0900 Subject: [PATCH 1/4] Pass default wasi-sysroot path # Conflicts: # Sources/Workspace/UserToolchain.swift --- Sources/Commands/SwiftTool.swift | 23 +++++++++++++++++++++-- Sources/Workspace/UserToolchain.swift | 13 +++++++++---- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Sources/Commands/SwiftTool.swift b/Sources/Commands/SwiftTool.swift index 63cb29dfb38..0c31189a814 100644 --- a/Sources/Commands/SwiftTool.swift +++ b/Sources/Commands/SwiftTool.swift @@ -639,6 +639,10 @@ public class SwiftTool { } catch { return .failure(error) } + // Get the search paths from PATH. + let searchPaths = getEnvSearchPaths( + pathString: ProcessEnv.vars["PATH"], currentWorkingDirectory: localFileSystem.currentWorkingDirectory) + // Apply any manual overrides. if let triple = self.options.customCompileTriple { destination.target = triple @@ -648,6 +652,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, envSearchPaths: searchPaths) + destination.sdk = compilers.compile + .parentDirectory // bin + .parentDirectory // usr + .appending(components: "share", "wasi-sysroot") + } catch { + return .failure(error) + } } destination.archs = options.archs @@ -656,14 +672,17 @@ public class SwiftTool { return self._hostToolchain } - return Result(catching: { try UserToolchain(destination: destination) }) + return Result(catching: { try UserToolchain(destination: destination, searchPaths: searchPaths) }) }() /// Lazily compute the host toolchain used to compile the package description. private lazy var _hostToolchain: Result = { + // Get the search paths from PATH. + let searchPaths = getEnvSearchPaths( + pathString: ProcessEnv.vars["PATH"], currentWorkingDirectory: localFileSystem.currentWorkingDirectory) return Result(catching: { try UserToolchain(destination: Destination.hostDestination( - originalWorkingDirectory: self.originalWorkingDirectory)) + originalWorkingDirectory: self.originalWorkingDirectory), searchPaths: searchPaths) }) }() diff --git a/Sources/Workspace/UserToolchain.swift b/Sources/Workspace/UserToolchain.swift index 7cc716f1559..24c3efdac38 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, envSearchPaths: [AbsolutePath]) throws -> SwiftCompilers { func validateCompiler(at path: AbsolutePath?) throws { guard let path = path else { return } guard localFileSystem.isExecutableFile(path) else { @@ -111,6 +113,7 @@ public final class UserToolchain: Toolchain { } } + let lookup = { UserToolchain.lookup(variable: $0, searchPaths: envSearchPaths) } // Get overrides. let SWIFT_EXEC_MANIFEST = lookup("SWIFT_EXEC_MANIFEST") let SWIFT_EXEC = lookup("SWIFT_EXEC") @@ -264,20 +267,22 @@ public final class UserToolchain: Toolchain { + destination.extraSwiftCFlags } - public init(destination: Destination, environment: [String: String] = ProcessEnv.vars) throws { + public init(destination: Destination, searchPaths: [AbsolutePath], 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) + pathString: ProcessEnv.path, + currentWorkingDirectory: localFileSystem.currentWorkingDirectory + ) + searchPaths self.envSearchPaths = searchPaths // 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, envSearchPaths: searchPaths) self.swiftCompiler = swiftCompilers.compile self.archs = destination.archs From de56a8f2bfb7dbf485aad961da7c0cb286696132 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 6 Mar 2020 22:18:48 +0900 Subject: [PATCH 2/4] Fix building test support target --- Sources/SPMTestSupport/Resources.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sources/SPMTestSupport/Resources.swift b/Sources/SPMTestSupport/Resources.swift index 6d384f7f291..2e4af3c3d9d 100644 --- a/Sources/SPMTestSupport/Resources.swift +++ b/Sources/SPMTestSupport/Resources.swift @@ -59,7 +59,9 @@ public class Resources: ManifestResourceProvider { #else binDir = AbsolutePath(CommandLine.arguments[0], relativeTo: localFileSystem.currentWorkingDirectory!).parentDirectory #endif - toolchain = try! UserToolchain(destination: Destination.hostDestination(binDir)) + let searchPaths = getEnvSearchPaths( + pathString: ProcessEnv.vars["PATH"], currentWorkingDirectory: localFileSystem.currentWorkingDirectory) + toolchain = try! UserToolchain(destination: Destination.hostDestination(binDir), searchPaths: searchPaths) } /// True if SwiftPM has PackageDescription 4 runtime available. From 7d7980189b2c07f3fed9c4a602c25f52f3684a8d Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Fri, 18 Sep 2020 16:03:54 +0100 Subject: [PATCH 3/4] Get search paths only in `determineSwiftCompilers` --- Sources/Commands/SwiftTool.swift | 13 +++--------- Sources/SPMTestSupport/Resources.swift | 4 +--- Sources/Workspace/UserToolchain.swift | 28 +++++++++++++------------- 3 files changed, 18 insertions(+), 27 deletions(-) diff --git a/Sources/Commands/SwiftTool.swift b/Sources/Commands/SwiftTool.swift index 0c31189a814..8fc45abb2d2 100644 --- a/Sources/Commands/SwiftTool.swift +++ b/Sources/Commands/SwiftTool.swift @@ -639,10 +639,6 @@ public class SwiftTool { } catch { return .failure(error) } - // Get the search paths from PATH. - let searchPaths = getEnvSearchPaths( - pathString: ProcessEnv.vars["PATH"], currentWorkingDirectory: localFileSystem.currentWorkingDirectory) - // Apply any manual overrides. if let triple = self.options.customCompileTriple { destination.target = triple @@ -656,7 +652,7 @@ public class SwiftTool { // Set default SDK path when target is WASI whose SDK is embeded // in Swift toolchain do { - let compilers = try UserToolchain.determineSwiftCompilers(binDir: destination.binDir, envSearchPaths: searchPaths) + let compilers = try UserToolchain.determineSwiftCompilers(binDir: destination.binDir) destination.sdk = compilers.compile .parentDirectory // bin .parentDirectory // usr @@ -672,17 +668,14 @@ public class SwiftTool { return self._hostToolchain } - return Result(catching: { try UserToolchain(destination: destination, searchPaths: searchPaths) }) + return Result(catching: { try UserToolchain(destination: destination) }) }() /// Lazily compute the host toolchain used to compile the package description. private lazy var _hostToolchain: Result = { - // Get the search paths from PATH. - let searchPaths = getEnvSearchPaths( - pathString: ProcessEnv.vars["PATH"], currentWorkingDirectory: localFileSystem.currentWorkingDirectory) return Result(catching: { try UserToolchain(destination: Destination.hostDestination( - originalWorkingDirectory: self.originalWorkingDirectory), searchPaths: searchPaths) + originalWorkingDirectory: self.originalWorkingDirectory)) }) }() diff --git a/Sources/SPMTestSupport/Resources.swift b/Sources/SPMTestSupport/Resources.swift index 2e4af3c3d9d..6d384f7f291 100644 --- a/Sources/SPMTestSupport/Resources.swift +++ b/Sources/SPMTestSupport/Resources.swift @@ -59,9 +59,7 @@ public class Resources: ManifestResourceProvider { #else binDir = AbsolutePath(CommandLine.arguments[0], relativeTo: localFileSystem.currentWorkingDirectory!).parentDirectory #endif - let searchPaths = getEnvSearchPaths( - pathString: ProcessEnv.vars["PATH"], currentWorkingDirectory: localFileSystem.currentWorkingDirectory) - toolchain = try! UserToolchain(destination: Destination.hostDestination(binDir), searchPaths: searchPaths) + toolchain = try! UserToolchain(destination: Destination.hostDestination(binDir)) } /// True if SwiftPM has PackageDescription 4 runtime available. diff --git a/Sources/Workspace/UserToolchain.swift b/Sources/Workspace/UserToolchain.swift index 24c3efdac38..66f25ab3e27 100644 --- a/Sources/Workspace/UserToolchain.swift +++ b/Sources/Workspace/UserToolchain.swift @@ -105,7 +105,7 @@ public final class UserToolchain: Toolchain { public typealias SwiftCompilers = (compile: AbsolutePath, manifest: AbsolutePath) /// Determines the Swift compiler paths for compilation and manifest parsing. - public static func determineSwiftCompilers(binDir: AbsolutePath, envSearchPaths: [AbsolutePath]) throws -> SwiftCompilers { + 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 { @@ -113,6 +113,12 @@ 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") @@ -144,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. @@ -267,25 +270,22 @@ public final class UserToolchain: Toolchain { + destination.extraSwiftCFlags } - public init(destination: Destination, searchPaths: [AbsolutePath], environment: [String: String] = ProcessEnv.vars) throws { + public init(destination: Destination) throws { self.destination = destination - self.processEnvironment = environment // Get the search paths from PATH. - let searchPaths = getEnvSearchPaths( - pathString: ProcessEnv.path, + self.envSearchPaths = getEnvSearchPaths( + pathString: ProcessEnv.path, currentWorkingDirectory: localFileSystem.currentWorkingDirectory - ) + searchPaths - - self.envSearchPaths = searchPaths + ) // Get the binDir from destination. let binDir = destination.binDir - let swiftCompilers = try UserToolchain.determineSwiftCompilers(binDir: binDir, 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) @@ -302,7 +302,7 @@ 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: ProcessEnv.vars).spm_chomp()) } else { self.xctest = nil } From a8194a15f8141d1f2804bcc2b129985e0c23c0a9 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Tue, 22 Sep 2020 19:33:11 +0100 Subject: [PATCH 4/4] Bring back environment argument to UserToolchain --- Sources/Workspace/UserToolchain.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/Workspace/UserToolchain.swift b/Sources/Workspace/UserToolchain.swift index 66f25ab3e27..1d3baa1b7e4 100644 --- a/Sources/Workspace/UserToolchain.swift +++ b/Sources/Workspace/UserToolchain.swift @@ -270,7 +270,7 @@ public final class UserToolchain: Toolchain { + destination.extraSwiftCFlags } - public init(destination: Destination) throws { + public init(destination: Destination, environment: [String: String] = ProcessEnv.vars) throws { self.destination = destination // Get the search paths from PATH. @@ -302,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: ProcessEnv.vars).spm_chomp()) + self.xctest = try AbsolutePath( + validating: Process.checkNonZeroExit(arguments: xctestFindArgs, environment: environment + ).spm_chomp()) } else { self.xctest = nil }