Skip to content

Commit 25ea210

Browse files
committed
Set up an externally installed JSON config file for blocking driver features for given module names
1 parent 7038a52 commit 25ea210

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

Sources/SwiftDriver/Jobs/Planning.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import TSCBasic
1414
import SwiftOptions
15+
import Foundation
1516

1617
public enum PlanningError: Error, DiagnosticData {
1718
case replReceivedInput
@@ -438,6 +439,46 @@ extension Driver {
438439
return try mergeModuleJob(inputs: moduleInputs, inputsFromOutputs: moduleInputsFromJobOutputs)
439440
}
440441

442+
func getAdopterConfigPathFromXcodeDefaultToolchain() -> AbsolutePath? {
443+
let swiftPath = try? toolchain.resolvedTool(.swiftCompiler).path
444+
guard var swiftPath = swiftPath else {
445+
return nil
446+
}
447+
let toolchains = "Toolchains"
448+
guard swiftPath.components.contains(toolchains) else {
449+
return nil
450+
}
451+
while swiftPath.basename != toolchains {
452+
swiftPath = swiftPath.parentDirectory
453+
}
454+
assert(swiftPath.basename == toolchains)
455+
return swiftPath.appending(component: "XcodeDefault.xctoolchain")
456+
.appending(component: "usr")
457+
.appending(component: "local")
458+
.appending(component: "lib")
459+
.appending(component: "swift")
460+
.appending(component: "adopter_configs.json")
461+
}
462+
463+
@_spi(Testing) public struct AdopterConfig: Decodable {
464+
public let key: String
465+
public let moduleNames: [String]
466+
}
467+
468+
@_spi(Testing) public static func parseAdopterConfigs(_ config: AbsolutePath) -> [AdopterConfig] {
469+
let results = try? localFileSystem.readFileContents(config).withData {
470+
try JSONDecoder().decode([AdopterConfig].self, from: $0)
471+
}
472+
return results ?? []
473+
}
474+
475+
func getAdopterConfigsFromXcodeDefaultToolchain() -> [AdopterConfig] {
476+
if let config = getAdopterConfigPathFromXcodeDefaultToolchain() {
477+
return Driver.parseAdopterConfigs(config)
478+
}
479+
return []
480+
}
481+
441482
private mutating func addVerifyJobs(emitModuleJob: Job, addJob: (Job) -> Void )
442483
throws {
443484
// Turn this flag on by default with the env var or for public frameworks.

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6003,6 +6003,34 @@ final class SwiftDriverTests: XCTestCase {
60036003
}
60046004
}
60056005

6006+
func testAdopterConfigFile() throws {
6007+
try withTemporaryFile { file in
6008+
try localFileSystem.writeFileContents(file.path) {
6009+
$0 <<< "["
6010+
$0 <<< " {"
6011+
$0 <<< " \"key\": \"SkipFeature1\","
6012+
$0 <<< " \"moduleNames\": [\"foo\", \"bar\"]"
6013+
$0 <<< " }"
6014+
$0 <<< "]"
6015+
}
6016+
let configs = Driver.parseAdopterConfigs(file.path)
6017+
XCTAssertEqual(configs.count, 1)
6018+
XCTAssertEqual(configs[0].key, "SkipFeature1")
6019+
XCTAssertEqual(configs[0].moduleNames, ["foo", "bar"])
6020+
}
6021+
try withTemporaryFile { file in
6022+
try localFileSystem.writeFileContents(file.path) {
6023+
$0 <<< "][ malformed }{"
6024+
}
6025+
let configs = Driver.parseAdopterConfigs(file.path)
6026+
XCTAssertEqual(configs.count, 0)
6027+
}
6028+
do {
6029+
let configs = Driver.parseAdopterConfigs(AbsolutePath("/abc/c/a.json"))
6030+
XCTAssertEqual(configs.count, 0)
6031+
}
6032+
}
6033+
60066034
func testExtractLibraryLevel() throws {
60076035
try withTemporaryFile { file in
60086036
try localFileSystem.writeFileContents(file.path) { $0 <<< "// swift-module-flags: -library-level api" }

0 commit comments

Comments
 (0)