Skip to content

Commit 7172392

Browse files
committed
Adopt the usage of in-process '-print-target-info' libSwiftScan API on the main compilation code-path
1 parent 4e697c7 commit 7172392

File tree

5 files changed

+46
-36
lines changed

5 files changed

+46
-36
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2903,13 +2903,10 @@ extension Driver {
29032903
let frontendOverride = try FrontendOverride(&parsedOptions, diagnosticsEngine)
29042904
frontendOverride.setUpForTargetInfo(toolchain)
29052905
defer { frontendOverride.setUpForCompilation(toolchain) }
2906-
return try executor.execute(
2907-
job: toolchain.printTargetInfoJob(target: nil, targetVariant: nil,
2908-
swiftCompilerPrefixArgs:
2909-
frontendOverride.prefixArgsForTargetInfo),
2910-
capturingJSONOutputAs: FrontendTargetInfo.self,
2911-
forceResponseFiles: false,
2912-
recordedInputModificationDates: [:]).target.triple
2906+
return try Self.computeTargetInfo(target: nil, targetVariant: nil,
2907+
swiftCompilerPrefixArgs: frontendOverride.prefixArgsForTargetInfo,
2908+
toolchain: toolchain, fileSystem: fileSystem,
2909+
executor: executor).target.triple
29132910
}
29142911

29152912
static func computeToolchain(
@@ -2963,18 +2960,16 @@ extension Driver {
29632960

29642961
// Query the frontend for target information.
29652962
do {
2966-
var info: FrontendTargetInfo = try executor.execute(
2967-
job: toolchain.printTargetInfoJob(
2968-
target: explicitTarget, targetVariant: explicitTargetVariant,
2969-
sdkPath: sdkPath, resourceDirPath: resourceDirPath,
2970-
runtimeCompatibilityVersion:
2971-
parsedOptions.getLastArgument(.runtimeCompatibilityVersion)?.asSingle,
2972-
useStaticResourceDir: useStaticResourceDir,
2973-
swiftCompilerPrefixArgs: frontendOverride.prefixArgsForTargetInfo
2974-
),
2975-
capturingJSONOutputAs: FrontendTargetInfo.self,
2976-
forceResponseFiles: false,
2977-
recordedInputModificationDates: [:])
2963+
var info: FrontendTargetInfo =
2964+
try Self.computeTargetInfo(target: explicitTarget, targetVariant: explicitTargetVariant,
2965+
sdkPath: sdkPath, resourceDirPath: resourceDirPath,
2966+
runtimeCompatibilityVersion:
2967+
parsedOptions.getLastArgument(.runtimeCompatibilityVersion)?.asSingle,
2968+
useStaticResourceDir: useStaticResourceDir,
2969+
swiftCompilerPrefixArgs: frontendOverride.prefixArgsForTargetInfo,
2970+
toolchain: toolchain, fileSystem: fileSystem,
2971+
executor: executor)
2972+
29782973

29792974
// Parse the runtime compatibility version. If present, it will override
29802975
// what is reported by the frontend.

Sources/SwiftDriver/Execution/DriverExecutor.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ extension DriverExecutor {
8787
capturingJSONOutputAs outputType: T.Type,
8888
forceResponseFiles: Bool,
8989
recordedInputModificationDates: [TypedVirtualPath: TimePoint]) throws -> T {
90+
//print(">>> EXEC: \(try self.description(of: job, forceResponseFiles: false))")
9091
let result = try execute(job: job,
9192
forceResponseFiles: forceResponseFiles,
9293
recordedInputModificationDates: recordedInputModificationDates)

Sources/SwiftDriver/Jobs/PrintTargetInfoJob.swift

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,30 @@ extension Driver {
185185
if let swiftScanLibPath = optionalSwiftScanLibPath,
186186
fileSystem.exists(swiftScanLibPath) {
187187
let libSwiftScanInstance = try SwiftScan(dylib: swiftScanLibPath)
188-
if libSwiftScanInstance.canQueryTargetInfo(),
189-
libSwiftScanInstance.supportsStringDispose() {
188+
if libSwiftScanInstance.canQueryTargetInfo() {
190189
let targetInfoData = try libSwiftScanInstance.queryTargetInfoJSON(invocationCommand: invocationCommand)
191-
return try JSONDecoder().decode(FrontendTargetInfo.self, from: targetInfoData)
190+
do {
191+
return try JSONDecoder().decode(FrontendTargetInfo.self, from: targetInfoData)
192+
} catch let decodingError as DecodingError {
193+
let stringToDecode = String(data: targetInfoData, encoding: .utf8)
194+
let errorDesc: String
195+
switch decodingError {
196+
case let .typeMismatch(type, context):
197+
errorDesc = "type mismatch: \(type), path: \(context.codingPath)"
198+
case let .valueNotFound(type, context):
199+
errorDesc = "value missing: \(type), path: \(context.codingPath)"
200+
case let .keyNotFound(key, context):
201+
errorDesc = "key missing: \(key), path: \(context.codingPath)"
202+
case let .dataCorrupted(context):
203+
errorDesc = "data corrupted at path: \(context.codingPath)"
204+
@unknown default:
205+
errorDesc = "unknown decoding error"
206+
}
207+
throw Error.unableToDecodeFrontendTargetInfo(
208+
stringToDecode,
209+
invocationCommand,
210+
errorDesc)
211+
}
192212
}
193213
}
194214
return nil

Sources/SwiftDriver/SwiftScan/SwiftScan.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,10 @@ internal extension swiftscan_diagnostic_severity_t {
333333
invocationStringArray)
334334
}
335335
let targetInfoStringRef = api.swiftscan_compiler_target_info_query(invocation)
336-
defer { api.swiftscan_string_dispose(targetInfoStringRef) }
337336
let targetInfoString = try toSwiftString(targetInfoStringRef)
337+
if supportsStringDispose() {
338+
api.swiftscan_string_dispose(targetInfoStringRef)
339+
}
338340
let targetInfoData = Data(targetInfoString.utf8)
339341
return targetInfoData
340342
}

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4770,7 +4770,7 @@ final class SwiftDriverTests: XCTestCase {
47704770

47714771
func testPrintTargetInfo() throws {
47724772
do {
4773-
var driver = try Driver(args: ["swift", "-print-target-info", "-sdk", "bar", "-resource-dir", "baz"])
4773+
var driver = try Driver(args: ["swift", "-print-target-info", "-target", "arm64-apple-ios12.0", "-sdk", "bar", "-resource-dir", "baz"])
47744774
let plannedJobs = try driver.planBuild()
47754775
XCTAssertTrue(plannedJobs.count == 1)
47764776
let job = plannedJobs[0]
@@ -4781,18 +4781,6 @@ final class SwiftDriverTests: XCTestCase {
47814781
XCTAssertTrue(job.commandLine.contains(.flag("-resource-dir")))
47824782
}
47834783

4784-
do {
4785-
let targetInfoArgs = ["-print-target-info", "-sdk", "bar", "-resource-dir", "baz"]
4786-
let driver = try Driver(args: ["swift"] + targetInfoArgs)
4787-
let swiftScanLibPath = try XCTUnwrap(driver.toolchain.lookupSwiftScanLib())
4788-
if localFileSystem.exists(swiftScanLibPath) {
4789-
let libSwiftScanInstance = try SwiftScan(dylib: swiftScanLibPath)
4790-
if libSwiftScanInstance.canQueryTargetInfo() {
4791-
XCTAssertTrue(try driver.verifyBeingAbleToQueryTargetInfoInProcess(invocationCommand: targetInfoArgs))
4792-
}
4793-
}
4794-
}
4795-
47964784
do {
47974785
struct MockExecutor: DriverExecutor {
47984786
let resolver: ArgsResolver
@@ -4815,7 +4803,11 @@ final class SwiftDriverTests: XCTestCase {
48154803
}
48164804
}
48174805

4806+
// Override path to libSwiftScan to force the fallback of using the executor
4807+
var hideSwiftScanEnv = ProcessEnv.vars
4808+
hideSwiftScanEnv["SWIFT_DRIVER_SWIFTSCAN_LIB"] = "/bad/path/lib_InternalSwiftScan.dylib"
48184809
XCTAssertThrowsError(try Driver(args: ["swift", "-print-target-info"],
4810+
env: hideSwiftScanEnv,
48194811
executor: MockExecutor(resolver: ArgsResolver(fileSystem: InMemoryFileSystem())))) {
48204812
error in
48214813
if case .unableToDecodeFrontendTargetInfo = error as? Driver.Error {}

0 commit comments

Comments
 (0)