Skip to content

Commit

Permalink
Support Swift <= 6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dmhts committed Sep 10, 2024
1 parent 28c0db1 commit bd0a035
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 6 deletions.
33 changes: 31 additions & 2 deletions Sources/ScipioKit/BuildOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ struct BuildOptions: Hashable, Codable, Sendable {
extraFlags: ExtraFlags?,
extraBuildParameters: ExtraBuildParameters?,
enableLibraryEvolution: Bool,
customFrameworkModuleMapContents: Data?,
environment: Environment?
customFrameworkModuleMapContents: Data?
) {
self.buildConfiguration = buildConfiguration
self.isDebugSymbolsEmbedded = isDebugSymbolsEmbedded
Expand All @@ -34,8 +33,33 @@ struct BuildOptions: Hashable, Codable, Sendable {
self.extraBuildParameters = extraBuildParameters
self.enableLibraryEvolution = enableLibraryEvolution
self.customFrameworkModuleMapContents = customFrameworkModuleMapContents
}

#if compiler(>=6.0)
init(
buildConfiguration: BuildConfiguration,
isDebugSymbolsEmbedded: Bool,
frameworkType: FrameworkType,
sdks: Set<SDK>,
extraFlags: ExtraFlags?,
extraBuildParameters: ExtraBuildParameters?,
enableLibraryEvolution: Bool,
customFrameworkModuleMapContents: Data?,
environment: Environment?
) {
self.init(
buildConfiguration: buildConfiguration,
isDebugSymbolsEmbedded: isDebugSymbolsEmbedded,
frameworkType: frameworkType,
sdks: sdks,
extraFlags: extraFlags,
extraBuildParameters: extraBuildParameters,
enableLibraryEvolution: enableLibraryEvolution,
customFrameworkModuleMapContents: customFrameworkModuleMapContents
)
self.environment = environment
}
#endif

let buildConfiguration: BuildConfiguration
let isDebugSymbolsEmbedded: Bool
Expand All @@ -48,7 +72,10 @@ 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?

#if compiler(>=6.0)
private(set) var environment: Environment?
#endif
}

public struct ExtraFlags: Hashable, Codable, Sendable {
Expand Down Expand Up @@ -206,10 +233,12 @@ 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: 4 additions & 0 deletions Sources/ScipioKit/Producer/PIF/PIFCompiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ struct PIFCompiler: Compiler {

private func makeToolchain(for sdk: SDK) async throws -> UserToolchain {
let toolchainDirPath = try await fetchDefaultToolchainBinPath()
#if compiler(>=6.0)
let toolchainGenerator = ToolchainGenerator(toolchainDirPath: toolchainDirPath, environment: buildOptions.environment)
#else
let toolchainGenerator = ToolchainGenerator(toolchainDirPath: toolchainDirPath)
#endif
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,28 +2,40 @@ import Foundation
import TSCUtility
#if compiler(>=6.0)
@_spi(SwiftPMInternal) import PackageModel
import struct Basics.Environment
#else
import PackageModel
#endif
import TSCBasic
import struct Basics.Triple
import struct Basics.Environment

struct ToolchainGenerator {
private let toolchainDirPath: AbsolutePath
private let executor: any Executor

#if compiler(>=6.0)
private let environment: Environment?
#endif

#if compiler(>=6.0)
init(toolchainDirPath: AbsolutePath, executor: any Executor = ProcessExecutor(), environment: Environment? = nil) {
self.toolchainDirPath = toolchainDirPath
self.executor = executor
self.environment = environment
}
#else
init(toolchainDirPath: AbsolutePath, executor: any Executor = ProcessExecutor()) {
self.toolchainDirPath = toolchainDirPath
self.executor = executor
}
#endif

func makeToolChain(sdk: SDK) async throws -> UserToolchain {
let destination: SwiftSDK = try await makeDestination(sdk: sdk)
#if swift(>=5.10)
#if swift(>=6.0)
return try UserToolchain(swiftSDK: destination, environment: environment ?? .current)
#elseif swift(>=5.10)
return try UserToolchain(swiftSDK: destination)
#else
return try UserToolchain(destination: destination)
#endif
Expand Down
62 changes: 60 additions & 2 deletions Sources/ScipioKit/Runner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import ScipioStorage
import struct TSCBasic.AbsolutePath
import protocol TSCBasic.FileSystem
import var TSCBasic.localFileSystem
#if compiler(>=6.0)
import struct Basics.Environment
#endif

public typealias PlatformMatrix = [String: Set<SDK>]

Expand Down Expand Up @@ -133,7 +135,10 @@ extension Runner {
public var enableLibraryEvolution: Bool
/// An option indicates use custom modulemaps for distributionb
public var frameworkModuleMapGenerationPolicy: FrameworkModuleMapGenerationPolicy

#if compiler(>=6.0)
public var environment: Environment?
#endif

public init(
buildConfiguration: BuildConfiguration = .release,
Expand All @@ -144,8 +149,7 @@ extension Runner {
extraFlags: ExtraFlags? = nil,
extraBuildParameters: [String: String]? = nil,
enableLibraryEvolution: Bool = false,
frameworkModuleMapGenerationPolicy: FrameworkModuleMapGenerationPolicy = .autoGenerated,
environment: Environment? = nil
frameworkModuleMapGenerationPolicy: FrameworkModuleMapGenerationPolicy = .autoGenerated
) {
self.buildConfiguration = buildConfiguration
self.platforms = platforms
Expand All @@ -156,8 +160,35 @@ extension Runner {
self.extraBuildParameters = extraBuildParameters
self.enableLibraryEvolution = enableLibraryEvolution
self.frameworkModuleMapGenerationPolicy = frameworkModuleMapGenerationPolicy
}

#if compiler(>=6.0)
public init(
buildConfiguration: BuildConfiguration = .release,
platforms: PlatformSpecifier = .manifest,
isSimulatorSupported: Bool = false,
isDebugSymbolsEmbedded: Bool = false,
frameworkType: FrameworkType = .dynamic,
extraFlags: ExtraFlags? = nil,
extraBuildParameters: [String: String]? = nil,
enableLibraryEvolution: Bool = false,
frameworkModuleMapGenerationPolicy: FrameworkModuleMapGenerationPolicy = .autoGenerated,
environment: Environment? = nil
) {
self.init(
buildConfiguration: buildConfiguration,
platforms: platforms,
isSimulatorSupported: isSimulatorSupported,
isDebugSymbolsEmbedded: isDebugSymbolsEmbedded,
frameworkType: frameworkType,
extraFlags: extraFlags,
extraBuildParameters: extraBuildParameters,
enableLibraryEvolution: enableLibraryEvolution,
frameworkModuleMapGenerationPolicy: frameworkModuleMapGenerationPolicy
)
self.environment = environment
}
#endif
}
public struct TargetBuildOptions {
public var buildConfiguration: BuildConfiguration?
Expand Down Expand Up @@ -295,6 +326,7 @@ extension Runner.Options.BuildOptions {
try fileSystem.readFileContents(url.absolutePath.spmAbsolutePath)
}

#if compiler(>=6.0)
return BuildOptions(
buildConfiguration: buildConfiguration,
isDebugSymbolsEmbedded: isDebugSymbolsEmbedded,
Expand All @@ -306,6 +338,18 @@ extension Runner.Options.BuildOptions {
customFrameworkModuleMapContents: customFrameworkModuleMapContents,
environment: environment
)
#else
return BuildOptions(
buildConfiguration: buildConfiguration,
isDebugSymbolsEmbedded: isDebugSymbolsEmbedded,
frameworkType: frameworkType,
sdks: Set(sdks),
extraFlags: extraFlags,
extraBuildParameters: extraBuildParameters,
enableLibraryEvolution: enableLibraryEvolution,
customFrameworkModuleMapContents: customFrameworkModuleMapContents
)
#endif
}

private func detectSDKsToBuild(
Expand Down Expand Up @@ -339,6 +383,7 @@ extension Runner.Options.BuildOptions {
let mergedExtraFlags = extraFlags?
.concatenating(overridingOptions.extraFlags) ?? overridingOptions.extraFlags

#if compiler(>=6.0)
return .init(
buildConfiguration: fetch(\.buildConfiguration, by: \.buildConfiguration),
platforms: fetch(\.platforms, by: \.platforms),
Expand All @@ -351,6 +396,19 @@ extension Runner.Options.BuildOptions {
frameworkModuleMapGenerationPolicy: fetch(\.frameworkModuleMapGenerationPolicy, by: \.frameworkModuleMapGenerationPolicy),
environment: environment
)
#else
return .init(
buildConfiguration: fetch(\.buildConfiguration, by: \.buildConfiguration),
platforms: fetch(\.platforms, by: \.platforms),
isSimulatorSupported: fetch(\.isSimulatorSupported, by: \.isSimulatorSupported),
isDebugSymbolsEmbedded: fetch(\.isDebugSymbolsEmbedded, by: \.isDebugSymbolsEmbedded),
frameworkType: fetch(\.frameworkType, by: \.frameworkType),
extraFlags: mergedExtraFlags,
extraBuildParameters: mergedExtraBuildParameters,
enableLibraryEvolution: fetch(\.enableLibraryEvolution, by: \.enableLibraryEvolution),
frameworkModuleMapGenerationPolicy: fetch(\.frameworkModuleMapGenerationPolicy, by: \.frameworkModuleMapGenerationPolicy)
)
#endif
}
}

Expand Down
20 changes: 20 additions & 0 deletions Tests/ScipioKitTests/CacheSystemTests.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Foundation
@testable import ScipioKit
import XCTest
#if compiler(>=6.0)
@_spi(SwiftPMInternal) import struct Basics.Environment
#endif

final class ClangCheckerTests: XCTestCase {
private let clangVersion = """
Expand All @@ -28,6 +30,7 @@ InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault
}

func testEncodeCacheKey() throws {
#if compiler(>=6.0)
let cacheKey = SwiftPMCacheKey(targetName: "MyTarget",
pin: .revision("111111111"),
buildOptions: .init(buildConfiguration: .release,
Expand All @@ -46,6 +49,23 @@ InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault
clangVersion: "clang-1400.0.29.102",
xcodeVersion: .init(xcodeVersion: "15.4", xcodeBuildVersion: "15F31d")
)
#else
let cacheKey = SwiftPMCacheKey(targetName: "MyTarget",
pin: .revision("111111111"),
buildOptions: .init(buildConfiguration: .release,
isDebugSymbolsEmbedded: false,
frameworkType: .dynamic,
sdks: [.iOS],
extraFlags: .init(swiftFlags: ["-D", "SOME_FLAG"]),
extraBuildParameters: ["SWIFT_OPTIMIZATION_LEVEL": "-Osize"],
enableLibraryEvolution: true,
customFrameworkModuleMapContents: Data(customModuleMap.utf8)
),
clangVersion: "clang-1400.0.29.102",
xcodeVersion: .init(xcodeVersion: "15.4", xcodeBuildVersion: "15F31d")
)
#endif

let encoder = JSONEncoder()
encoder.outputFormatting = [.sortedKeys, .prettyPrinted]
let data = try encoder.encode(cacheKey)
Expand Down
13 changes: 13 additions & 0 deletions Tests/ScipioKitTests/RunnerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ framework module MyTarget {
}

extension BuildOptions {
#if compiler(>=6.0)
fileprivate static let `default`: Self = .init(
buildConfiguration: .release,
isDebugSymbolsEmbedded: false,
Expand All @@ -719,4 +720,16 @@ extension BuildOptions {
customFrameworkModuleMapContents: nil,
environment: nil
)
#else
fileprivate static let `default`: Self = .init(
buildConfiguration: .release,
isDebugSymbolsEmbedded: false,
frameworkType: .dynamic,
sdks: [.iOS],
extraFlags: nil,
extraBuildParameters: nil,
enableLibraryEvolution: true,
customFrameworkModuleMapContents: nil
)
#endif
}

0 comments on commit bd0a035

Please sign in to comment.