Skip to content

Commit 30b7cc8

Browse files
committed
[core] Allow overriding the default toolchain using env TOOLCHAINS on Darwin
To match the behaviour of `xcrun`.
1 parent ec6c3da commit 30b7cc8

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

Sources/SKCore/ToolchainRegistry.swift

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ public final class ToolchainRegistry {
3636
/// Mutex for registering and accessing toolchains.
3737
var queue: DispatchQueue = DispatchQueue(label: "toolchain-registry-queue")
3838

39+
/// The currently selected toolchain identifier on Darwin.
40+
public internal(set) lazy var darwinToolchainOverride: String? = {
41+
if let id = getenv("TOOLCHAINS"), !id.isEmpty, id != "default" {
42+
return id
43+
}
44+
return nil
45+
}()
46+
3947
/// Creates an empty toolchain registry.
4048
public init() {}
4149
}
@@ -75,16 +83,16 @@ extension ToolchainRegistry {
7583

7684
/// The default toolchain.
7785
///
78-
/// On Darwin, this is typically the toolchain with the identifier
79-
/// `darwinDefaultToolchainIdentifier`, i.e. the default toolchain of the active Xcode. Otherwise
80-
/// it is the first toolchain that was registered, if any.
86+
/// On Darwin, this is typically the toolchain with the identifier `darwinToolchainIdentifier`,
87+
/// i.e. the default toolchain of the active Xcode. Otherwise it is the first toolchain that was
88+
/// registered, if any.
8189
///
8290
/// The default toolchain must be only of the registered toolchains.
8391
public var `default`: Toolchain? {
8492
get {
8593
return queue.sync {
8694
if _default == nil {
87-
if let tc = toolchainIdentifiers[ToolchainRegistry.darwinDefaultToolchainIdentifier] {
95+
if let tc = toolchainIdentifiers[darwinToolchainIdentifier] {
8896
_default = tc
8997
} else {
9098
_default = _toolchains.first
@@ -108,10 +116,16 @@ extension ToolchainRegistry {
108116
}
109117

110118
/// The standard default toolchain identifier on Darwin.
119+
public static let darwinDefaultToolchainIdentifier: String = "com.apple.dt.toolchain.XcodeDefault"
120+
121+
/// The current toolchain identifier on Darwin, which is either specified byt the `TOOLCHAINS`
122+
/// environment variable, or defaults to `darwinDefaultToolchainIdentifier`.
111123
///
112124
/// The value of `default.identifier` may be different if the default toolchain has been
113-
/// explicitly overridden, or if there is no toolchain with this identifier.
114-
public static let darwinDefaultToolchainIdentifier: String = "com.apple.dt.toolchain.XcodeDefault"
125+
/// explicitly overridden in code, or if there is no toolchain with this identifier.
126+
public var darwinToolchainIdentifier: String {
127+
return darwinToolchainOverride ?? ToolchainRegistry.darwinDefaultToolchainIdentifier
128+
}
115129

116130
/// All toolchains, in the order they were added.
117131
public var toolchains: [Toolchain] {

Tests/SKCoreTests/ToolchainRegistryTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ final class ToolchainRegistryTests: XCTestCase {
3737
Platform.currentPlatform = .darwin
3838

3939
let tr = ToolchainRegistry()
40+
tr.darwinToolchainOverride = nil
4041
XCTAssertNil(tr.default)
4142
let a = Toolchain(identifier: "a", displayName: "a", path: nil)
4243
try! tr.registerToolchain(a)
@@ -69,6 +70,7 @@ final class ToolchainRegistryTests: XCTestCase {
6970
#if os(macOS)
7071
let fs = InMemoryFileSystem()
7172
let tr1 = ToolchainRegistry(fs)
73+
tr1.darwinToolchainOverride = nil
7274

7375
let xcodeDeveloper = ToolchainRegistry.currentXcodeDeveloperPath!
7476
let toolchains = xcodeDeveloper.appending(components: "Toolchains")
@@ -90,6 +92,7 @@ final class ToolchainRegistryTests: XCTestCase {
9092
XCTAssertEqual(tr1.toolchains.count, 1)
9193

9294
let tr = ToolchainRegistry(fs)
95+
tr.darwinToolchainOverride = nil
9396

9497
XCTAssertEqual(tr.default?.identifier, ToolchainRegistry.darwinDefaultToolchainIdentifier)
9598
XCTAssertEqual(tr.default?.path, toolchains.appending(component: "XcodeDefault.xctoolchain"))
@@ -161,6 +164,11 @@ final class ToolchainRegistryTests: XCTestCase {
161164
let tc = Toolchain(path, fs)
162165
XCTAssertNotNil(tc)
163166
XCTAssertEqual(tc?.identifier, "org.fake.explicit")
167+
168+
let overrideReg = ToolchainRegistry(fs)
169+
overrideReg.darwinToolchainOverride = "org.fake.global.B"
170+
XCTAssertEqual(overrideReg.darwinToolchainIdentifier, "org.fake.global.B")
171+
XCTAssertEqual(overrideReg.default?.identifier, "org.fake.global.B")
164172
#endif
165173
}
166174

0 commit comments

Comments
 (0)