Skip to content
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

Task / Configurable runner environment #140

Merged
merged 23 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b8afb4b
Make runner environment configurable
dmhts Aug 27, 2024
52d04d8
Avoid environment affecting final hash
dmhts Sep 5, 2024
413a5c9
Exclude environment from CodingKeys
dmhts Sep 6, 2024
b6f27fc
Revert "Exclude environment from CodingKeys"
dmhts Sep 6, 2024
5c0f81e
Reapply "Exclude environment from CodingKeys"
dmhts Sep 6, 2024
7eab940
Add test ensuring environment not encoded in cache key
dmhts Sep 6, 2024
3fc23b1
Adapt default build options
dmhts Sep 8, 2024
913dafd
Add proper conformance of Environment to Hashable
dmhts Sep 9, 2024
12dfd7c
Xcode Beta 6 changes
dmhts Sep 9, 2024
28c0db1
Make linter happy
dmhts Sep 9, 2024
bd0a035
Support Swift <= 6.0
dmhts Sep 10, 2024
9d60543
Revert "Support Swift <= 6.0"
dmhts Sep 23, 2024
9b50517
Merge remote-tracking branch 'upstream/main' into task/configurable-r…
dmhts Sep 23, 2024
c3ac051
Use `[String: String]` instead of `Environment`
dmhts Sep 23, 2024
b3b0219
Remove extra new line
dmhts Sep 23, 2024
b5d5b57
Add compiler check for environment extension
dmhts Sep 23, 2024
24289cf
Remove extra import
dmhts Sep 23, 2024
834467e
Remove one more extra import
dmhts Sep 23, 2024
d171e0c
environment -> toolchainEnvironment
dmhts Sep 25, 2024
ef8fb17
Move environment from build to runner options
dmhts Sep 25, 2024
b8adc6c
Environment doesn't need to be hashable anymore
dmhts Sep 25, 2024
3ca0227
Remove environment leftovers from TargetBuildOptions
dmhts Sep 25, 2024
eaa8f2c
Keep dependencies at end of parameter list
dmhts Sep 25, 2024
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
30 changes: 29 additions & 1 deletion Sources/ScipioKit/BuildOptions.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import Foundation
import OrderedCollections
import Basics

struct BuildOptions: Hashable, Codable, Sendable {

private enum CodingKeys: CodingKey {
case buildConfiguration
case isDebugSymbolsEmbedded
case frameworkType
case sdks
case extraFlags
case extraBuildParameters
case enableLibraryEvolution
case customFrameworkModuleMapContents
}
dmhts marked this conversation as resolved.
Show resolved Hide resolved

init(
buildConfiguration: BuildConfiguration,
isDebugSymbolsEmbedded: Bool,
Expand All @@ -10,7 +23,8 @@ struct BuildOptions: Hashable, Codable, Sendable {
extraFlags: ExtraFlags?,
extraBuildParameters: ExtraBuildParameters?,
enableLibraryEvolution: Bool,
customFrameworkModuleMapContents: Data?
customFrameworkModuleMapContents: Data?,
toolchainEnvironment: [String: String]?
) {
self.buildConfiguration = buildConfiguration
self.isDebugSymbolsEmbedded = isDebugSymbolsEmbedded
Expand All @@ -20,6 +34,7 @@ struct BuildOptions: Hashable, Codable, Sendable {
self.extraBuildParameters = extraBuildParameters
self.enableLibraryEvolution = enableLibraryEvolution
self.customFrameworkModuleMapContents = customFrameworkModuleMapContents
self.toolchainEnvironment = toolchainEnvironment
}

let buildConfiguration: BuildConfiguration
Expand All @@ -33,6 +48,7 @@ struct BuildOptions: Hashable, Codable, Sendable {
/// - Note: It have to store the actual file contents rather than its path,
/// because the cache key should change when the file contents change.
let customFrameworkModuleMapContents: Data?
private(set) var toolchainEnvironment: [String: String]?
}

public struct ExtraFlags: Hashable, Codable, Sendable {
Expand Down Expand Up @@ -189,3 +205,15 @@ extension ExtraFlags {
)
}
}

#if compiler(>=6.0)

extension Environment: @retroactive Hashable {
public func hash(into hasher: inout Hasher) {
for item in self {
hasher.combine(item.value)
}
}
}

#endif
4 changes: 2 additions & 2 deletions Sources/ScipioKit/Producer/PIF/PIFCompiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ 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)
dmhts marked this conversation as resolved.
Show resolved Hide resolved
}

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: buildOptions.toolchainEnvironment)
return try await toolchainGenerator.makeToolChain(sdk: sdk)
}

Expand Down
16 changes: 14 additions & 2 deletions Sources/ScipioKit/Producer/PIF/ToolchainGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -11,15 +12,26 @@ import struct Basics.Triple
struct ToolchainGenerator {
private let toolchainDirPath: AbsolutePath
private let executor: any Executor
private let environment: [String: String]?

init(toolchainDirPath: AbsolutePath, executor: any Executor = ProcessExecutor()) {
init(
toolchainDirPath: AbsolutePath,
executor: any Executor = ProcessExecutor(),
environment: [String: String]? = nil
dmhts marked this conversation as resolved.
Show resolved Hide resolved
) {
self.toolchainDirPath = toolchainDirPath
self.executor = executor
self.environment = environment
}

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)
Expand Down
16 changes: 12 additions & 4 deletions Sources/ScipioKit/Runner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ extension Runner {
public var enableLibraryEvolution: Bool
/// An option indicates use custom modulemaps for distributionb
public var frameworkModuleMapGenerationPolicy: FrameworkModuleMapGenerationPolicy
public var toolchainEnvironment: [String: String]?

public init(
buildConfiguration: BuildConfiguration = .release,
Expand All @@ -142,7 +143,8 @@ extension Runner {
extraFlags: ExtraFlags? = nil,
extraBuildParameters: [String: String]? = nil,
enableLibraryEvolution: Bool = false,
frameworkModuleMapGenerationPolicy: FrameworkModuleMapGenerationPolicy = .autoGenerated
frameworkModuleMapGenerationPolicy: FrameworkModuleMapGenerationPolicy = .autoGenerated,
toolchainEnvironment: [String: String]? = nil
) {
self.buildConfiguration = buildConfiguration
self.platforms = platforms
Expand All @@ -153,6 +155,7 @@ extension Runner {
self.extraBuildParameters = extraBuildParameters
self.enableLibraryEvolution = enableLibraryEvolution
self.frameworkModuleMapGenerationPolicy = frameworkModuleMapGenerationPolicy
self.toolchainEnvironment = toolchainEnvironment
}
}
public struct TargetBuildOptions {
Expand All @@ -165,6 +168,7 @@ extension Runner {
public var extraBuildParameters: [String: String]?
public var enableLibraryEvolution: Bool?
public var frameworkModuleMapGenerationPolicy: FrameworkModuleMapGenerationPolicy?
public var environment: [String: String]?

public init(
buildConfiguration: BuildConfiguration? = nil,
Expand All @@ -175,7 +179,8 @@ extension Runner {
extraFlags: ExtraFlags? = nil,
extraBuildParameters: [String: String]? = nil,
enableLibraryEvolution: Bool? = nil,
frameworkModuleMapGenerationPolicy: FrameworkModuleMapGenerationPolicy? = nil
frameworkModuleMapGenerationPolicy: FrameworkModuleMapGenerationPolicy? = nil,
environment: [String: String]? = nil
dmhts marked this conversation as resolved.
Show resolved Hide resolved
) {
self.buildConfiguration = buildConfiguration
self.platforms = platforms
Expand All @@ -186,6 +191,7 @@ extension Runner {
self.extraFlags = extraFlags
self.enableLibraryEvolution = enableLibraryEvolution
self.frameworkModuleMapGenerationPolicy = frameworkModuleMapGenerationPolicy
self.environment = environment
}
}

Expand Down Expand Up @@ -296,7 +302,8 @@ extension Runner.Options.BuildOptions {
extraFlags: extraFlags,
extraBuildParameters: extraBuildParameters,
enableLibraryEvolution: enableLibraryEvolution,
customFrameworkModuleMapContents: customFrameworkModuleMapContents
customFrameworkModuleMapContents: customFrameworkModuleMapContents,
toolchainEnvironment: toolchainEnvironment
)
}

Expand Down Expand Up @@ -340,7 +347,8 @@ extension Runner.Options.BuildOptions {
extraFlags: mergedExtraFlags,
extraBuildParameters: mergedExtraBuildParameters,
enableLibraryEvolution: fetch(\.enableLibraryEvolution, by: \.enableLibraryEvolution),
frameworkModuleMapGenerationPolicy: fetch(\.frameworkModuleMapGenerationPolicy, by: \.frameworkModuleMapGenerationPolicy)
frameworkModuleMapGenerationPolicy: fetch(\.frameworkModuleMapGenerationPolicy, by: \.frameworkModuleMapGenerationPolicy),
toolchainEnvironment: toolchainEnvironment
)
}
}
Expand Down
6 changes: 5 additions & 1 deletion Tests/ScipioKitTests/CacheSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault
extraFlags: .init(swiftFlags: ["-D", "SOME_FLAG"]),
extraBuildParameters: ["SWIFT_OPTIMIZATION_LEVEL": "-Osize"],
enableLibraryEvolution: true,
customFrameworkModuleMapContents: Data(customModuleMap.utf8)
customFrameworkModuleMapContents: Data(customModuleMap.utf8),
toolchainEnvironment: [
"DEVELOPER_DIR": "/Xcode.app/Contents/Developer",
"PATH": "/Xcode.app/Contents/Developer:/usr/bin:/bin",
]
),
clangVersion: "clang-1400.0.29.102",
xcodeVersion: .init(xcodeVersion: "15.4", xcodeBuildVersion: "15F31d")
Expand Down
3 changes: 2 additions & 1 deletion Tests/ScipioKitTests/RunnerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ extension BuildOptions {
extraFlags: nil,
extraBuildParameters: nil,
enableLibraryEvolution: true,
customFrameworkModuleMapContents: nil
customFrameworkModuleMapContents: nil,
toolchainEnvironment: nil
)
}
Loading