diff --git a/Sources/ScipioKit/BuildOptions.swift b/Sources/ScipioKit/BuildOptions.swift index 87b3cf81..0eee5af2 100644 --- a/Sources/ScipioKit/BuildOptions.swift +++ b/Sources/ScipioKit/BuildOptions.swift @@ -1,7 +1,9 @@ import Foundation import OrderedCollections +import Basics struct BuildOptions: Hashable, Codable, Sendable { + init( buildConfiguration: BuildConfiguration, isDebugSymbolsEmbedded: Bool, diff --git a/Sources/ScipioKit/Producer/FrameworkProducer.swift b/Sources/ScipioKit/Producer/FrameworkProducer.swift index 4cfcf3d1..3b3c7c9f 100644 --- a/Sources/ScipioKit/Producer/FrameworkProducer.swift +++ b/Sources/ScipioKit/Producer/FrameworkProducer.swift @@ -14,6 +14,7 @@ struct FrameworkProducer { private let overwrite: Bool private let outputDir: URL private let fileSystem: any FileSystem + private let toolchainEnvironment: [String: String]? private var cacheStorage: (any CacheStorage)? { switch cacheMode { @@ -51,6 +52,7 @@ struct FrameworkProducer { cacheMode: Runner.Options.CacheMode, overwrite: Bool, outputDir: URL, + toolchainEnvironment: [String: String]? = nil, fileSystem: any FileSystem = localFileSystem ) { self.descriptionPackage = descriptionPackage @@ -59,6 +61,7 @@ struct FrameworkProducer { self.cacheMode = cacheMode self.overwrite = overwrite self.outputDir = outputDir + self.toolchainEnvironment = toolchainEnvironment self.fileSystem = fileSystem } diff --git a/Sources/ScipioKit/Producer/PIF/PIFCompiler.swift b/Sources/ScipioKit/Producer/PIF/PIFCompiler.swift index edf2fe35..bae94f27 100644 --- a/Sources/ScipioKit/Producer/PIF/PIFCompiler.swift +++ b/Sources/ScipioKit/Producer/PIF/PIFCompiler.swift @@ -10,6 +10,7 @@ struct PIFCompiler: Compiler { private let fileSystem: any FileSystem private let executor: any Executor private let buildOptionsMatrix: [String: BuildOptions] + private let toolchainEnvironment: [String: String]? private let buildParametersGenerator: BuildParametersGenerator @@ -17,12 +18,14 @@ struct PIFCompiler: Compiler { descriptionPackage: DescriptionPackage, buildOptions: BuildOptions, buildOptionsMatrix: [String: BuildOptions], + toolchainEnvironment: [String: String]? = nil, fileSystem: any FileSystem = TSCBasic.localFileSystem, executor: any Executor = ProcessExecutor() ) { self.descriptionPackage = descriptionPackage self.buildOptions = buildOptions self.buildOptionsMatrix = buildOptionsMatrix + self.toolchainEnvironment = toolchainEnvironment self.fileSystem = fileSystem self.executor = executor self.buildParametersGenerator = .init(buildOptions: buildOptions, fileSystem: fileSystem) @@ -30,7 +33,7 @@ struct PIFCompiler: Compiler { private func fetchDefaultToolchainBinPath() async throws -> AbsolutePath { let result = try await executor.execute("/usr/bin/xcrun", "xcode-select", "-p") - let rawString = try result.unwrapOutput() + let rawString = try result.unwrapOutput().trimmingCharacters(in: .whitespacesAndNewlines) let developerDirPath = try AbsolutePath(validating: rawString) let toolchainPath = try RelativePath(validating: "./Toolchains/XcodeDefault.xctoolchain/usr/bin") return developerDirPath.appending(toolchainPath) @@ -38,7 +41,7 @@ struct PIFCompiler: Compiler { private func makeToolchain(for sdk: SDK) async throws -> UserToolchain { let toolchainDirPath = try await fetchDefaultToolchainBinPath() - let toolchainGenerator = ToolchainGenerator(toolchainDirPath: toolchainDirPath) + let toolchainGenerator = ToolchainGenerator(toolchainDirPath: toolchainDirPath, environment: toolchainEnvironment) return try await toolchainGenerator.makeToolChain(sdk: sdk) } diff --git a/Sources/ScipioKit/Producer/PIF/ToolchainGenerator.swift b/Sources/ScipioKit/Producer/PIF/ToolchainGenerator.swift index d1e0d262..6a048093 100644 --- a/Sources/ScipioKit/Producer/PIF/ToolchainGenerator.swift +++ b/Sources/ScipioKit/Producer/PIF/ToolchainGenerator.swift @@ -2,6 +2,7 @@ import Foundation import TSCUtility #if compiler(>=6.0) @_spi(SwiftPMInternal) import PackageModel +@_spi(SwiftPMInternal) import struct Basics.Environment #else import PackageModel #endif @@ -10,16 +11,27 @@ import struct Basics.Triple struct ToolchainGenerator { private let toolchainDirPath: AbsolutePath + private let environment: [String: String]? private let executor: any Executor - init(toolchainDirPath: AbsolutePath, executor: any Executor = ProcessExecutor()) { + init( + toolchainDirPath: AbsolutePath, + environment: [String: String]? = nil, + executor: any Executor = ProcessExecutor() + ) { self.toolchainDirPath = toolchainDirPath + self.environment = environment self.executor = executor } func makeToolChain(sdk: SDK) async throws -> UserToolchain { let destination: SwiftSDK = try await makeDestination(sdk: sdk) - #if swift(>=5.10) + #if compiler(>=6.0) + return try UserToolchain( + swiftSDK: destination, + environment: environment.map(Environment.init) ?? .current + ) + #elseif swift(>=5.10) return try UserToolchain(swiftSDK: destination) #else return try UserToolchain(destination: destination) diff --git a/Sources/ScipioKit/Runner.swift b/Sources/ScipioKit/Runner.swift index 436971c8..22294061 100644 --- a/Sources/ScipioKit/Runner.swift +++ b/Sources/ScipioKit/Runner.swift @@ -234,6 +234,7 @@ extension Runner { public var cacheMode: CacheMode public var overwrite: Bool public var verbose: Bool + public var toolchainEnvironment: [String: String]? public init( baseBuildOptions: BuildOptions = .init(), @@ -241,7 +242,8 @@ extension Runner { shouldOnlyUseVersionsFromResolvedFile: Bool = false, cacheMode: CacheMode = .project, overwrite: Bool = false, - verbose: Bool = false + verbose: Bool = false, + toolchainEnvironment: [String: String]? = nil ) { self.buildOptionsContainer = BuildOptionsContainer( baseBuildOptions: baseBuildOptions, @@ -251,6 +253,7 @@ extension Runner { self.cacheMode = cacheMode self.overwrite = overwrite self.verbose = verbose + self.toolchainEnvironment = toolchainEnvironment } } }