Skip to content

Commit ddbb4f6

Browse files
committed
Make bridgingPrecompiledHeader a let constant
1 parent 5e7743e commit ddbb4f6

File tree

5 files changed

+51
-25
lines changed

5 files changed

+51
-25
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
13+
import Foundation
1214
import TSCBasic
1315
import TSCUtility
1416

@@ -125,7 +127,7 @@ public struct Driver {
125127
public let importedObjCHeader: VirtualPath?
126128

127129
/// The path to the pch for the imported Objective-C header.
128-
public var bridgingPrecompiledHeader: VirtualPath?
130+
public let bridgingPrecompiledHeader: VirtualPath?
129131

130132
/// Path to the dependencies file.
131133
public let dependenciesFilePath: VirtualPath?
@@ -292,6 +294,7 @@ public struct Driver {
292294
self.sdkPath = Self.computeSDKPath(&parsedOptions, compilerMode: compilerMode, toolchain: toolchain, diagnosticsEngine: diagnosticEngine, env: env)
293295

294296
self.importedObjCHeader = try Self.computeImportedObjCHeader(&parsedOptions, compilerMode: compilerMode, diagnosticEngine: diagnosticEngine)
297+
self.bridgingPrecompiledHeader = try Self.computeBridgingPrecompiledHeader(&parsedOptions, importedObjCHeader: importedObjCHeader)
295298

296299
self.enabledSanitizers = try Self.parseSanitizerArgValues(&parsedOptions, diagnosticEngine: diagnosticEngine, toolchain: toolchain, targetTriple: targetTriple)
297300

@@ -1335,11 +1338,39 @@ extension Driver {
13351338
diagnosticEngine.emit(.error_bridging_header_module_interface)
13361339
}
13371340

1338-
return try VirtualPath(path: objcHeaderPathArg.asSingle)
1341+
let objcHeader = try VirtualPath(path: objcHeaderPathArg.asSingle)
1342+
1343+
if objcHeader.extension != FileType.objcHeader.rawValue {
1344+
diagnosticEngine.emit(.error_objc_header_not_header)
1345+
}
1346+
1347+
return objcHeader
1348+
}
1349+
1350+
/// Compute the path of the generated bridging PCH for the Objective-C header.
1351+
static func computeBridgingPrecompiledHeader(_ parsedOptions: inout ParsedOptions, importedObjCHeader: VirtualPath?) throws -> VirtualPath? {
1352+
guard let input = importedObjCHeader,
1353+
parsedOptions.hasFlag(positive: .enableBridgingPch, negative: .disableBridgingPch, default: true) else {
1354+
return nil
1355+
}
1356+
// FIXME: should have '-.*' at the end of the filename, similar to llvm::sys::fs::createTemporaryFile
1357+
let pchFileName = input.basenameWithoutExt.appendingFileTypeExtension(.pch)
1358+
let output: VirtualPath
1359+
if let outputDirectory = parsedOptions.getLastArgument(.pchOutputDir)?.asSingle {
1360+
let outputPath = (outputDirectory as NSString).appendingPathComponent(pchFileName)
1361+
output = try VirtualPath(path: outputPath)
1362+
} else {
1363+
output = .temporary(RelativePath(pchFileName))
1364+
}
1365+
return output
13391366
}
13401367
}
13411368

13421369
extension Diagnostic.Message {
1370+
static var error_objc_header_not_header: Diagnostic.Message {
1371+
.error("ObjC header needs to be .h file")
1372+
}
1373+
13431374
static var error_framework_bridging_header: Diagnostic.Message {
13441375
.error("using bridging headers with framework targets is unsupported")
13451376
}

Sources/SwiftDriver/Jobs/CompileJob.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extension Driver {
2626
}
2727

2828
/// Add the compiler inputs for a frontend compilation job, and return the corresponding primary set of outputs.
29-
mutating func addCompileInputs(primaryInputs: [TypedVirtualPath], inputs: inout [TypedVirtualPath], commandLine: inout [Job.ArgTemplate]) -> [TypedVirtualPath] {
29+
func addCompileInputs(primaryInputs: [TypedVirtualPath], inputs: inout [TypedVirtualPath], commandLine: inout [Job.ArgTemplate]) -> [TypedVirtualPath] {
3030
// Collect the set of input files that are part of the Swift compilation.
3131
let swiftInputFiles: [TypedVirtualPath] = inputFiles.compactMap { inputFile in
3232
if inputFile.type.isPartOfSwiftCompilation {

Sources/SwiftDriver/Jobs/GeneratePCHJob.swift

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import TSCBasic
1515
import TSCUtility
1616

1717
extension Driver {
18-
mutating func generatePCHJob(input: TypedVirtualPath) throws -> Job {
18+
mutating func generatePCHJob(input: TypedVirtualPath, output: TypedVirtualPath) throws -> Job {
1919
var inputs = [TypedVirtualPath]()
2020
var outputs = [TypedVirtualPath]()
2121

@@ -25,6 +25,8 @@ extension Driver {
2525
commandLine.appendPath(input.file)
2626
commandLine.appendFlag(.emitPch)
2727

28+
outputs.append(output)
29+
2830
try addCommonFrontendOptions(commandLine: &commandLine)
2931

3032
if let importedObjCHeader = importedObjCHeader {
@@ -34,6 +36,7 @@ extension Driver {
3436

3537
try commandLine.appendLast(.indexStorePath, from: &parsedOptions)
3638

39+
// TODO: Should this just be pch output with extension changed?
3740
if parsedOptions.hasArgument(.serializeDiagnostics), let outputDirectory = parsedOptions.getLastArgument(.pchOutputDir)?.asSingle {
3841
commandLine.appendFlag(.serializeDiagnosticsPath)
3942
let path: VirtualPath
@@ -52,20 +55,12 @@ extension Driver {
5255
outputs.append(.init(file: path, type: .diagnostics))
5356
}
5457

55-
// FIXME: should have '-.*' at the end of the filename, similar to llvm::sys::fs::createTemporaryFile
56-
let pchFileName = input.file.basenameWithoutExt.appendingFileTypeExtension(.pch)
57-
let output: VirtualPath
58-
if let outputDirectory = parsedOptions.getLastArgument(.pchOutputDir)?.asSingle {
59-
let outputPath = (outputDirectory as NSString).appendingPathComponent(pchFileName)
60-
output = try VirtualPath(path: outputPath)
58+
if parsedOptions.hasArgument(.pchOutputDir) {
6159
try commandLine.appendLast(.pchOutputDir, from: &parsedOptions)
6260
} else {
63-
output = .temporary(RelativePath(pchFileName))
6461
commandLine.appendFlag(.o)
65-
commandLine.appendPath(output)
62+
commandLine.appendPath(output.file)
6663
}
67-
outputs.append(.init(file: output, type: .pch))
68-
bridgingPrecompiledHeader = output
6964

7065
return Job(
7166
kind: .generatePCH,

Sources/SwiftDriver/Jobs/Planning.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ extension Driver {
4747
}
4848

4949
if let importedObjCHeader = importedObjCHeader,
50-
parsedOptions.hasFlag(positive: .enableBridgingPch, negative: .disableBridgingPch, default: true),
51-
importedObjCHeader.extension == FileType.objcHeader.rawValue {
52-
jobs.append(try generatePCHJob(input: .init(file: importedObjCHeader, type: .objcHeader)))
50+
let bridgingPrecompiledHeader = bridgingPrecompiledHeader {
51+
jobs.append(try generatePCHJob(input: .init(file: importedObjCHeader, type: .objcHeader),
52+
output: .init(file: bridgingPrecompiledHeader, type: .pch)))
5353
}
5454

5555
// If we should create emit module job, do so.

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,10 +1337,10 @@ final class SwiftDriverTests: XCTestCase {
13371337
XCTAssertEqual(plannedJobs[0].inputs[0].file, .relative(RelativePath("TestInputHeader.h")))
13381338
XCTAssertEqual(plannedJobs[0].inputs[0].type, .objcHeader)
13391339
XCTAssertEqual(plannedJobs[0].outputs.count, 2)
1340-
XCTAssertEqual(plannedJobs[0].outputs[0].file, .temporary(RelativePath("TestInputHeader.dia")))
1341-
XCTAssertEqual(plannedJobs[0].outputs[0].type, .diagnostics)
1342-
XCTAssertEqual(plannedJobs[0].outputs[1].file, try VirtualPath(path: "/pch/TestInputHeader.pch"))
1343-
XCTAssertEqual(plannedJobs[0].outputs[1].type, .pch)
1340+
XCTAssertEqual(plannedJobs[0].outputs[0].file, try VirtualPath(path: "/pch/TestInputHeader.pch"))
1341+
XCTAssertEqual(plannedJobs[0].outputs[0].type, .pch)
1342+
XCTAssertEqual(plannedJobs[0].outputs[1].file, .temporary(RelativePath("TestInputHeader.dia")))
1343+
XCTAssertEqual(plannedJobs[0].outputs[1].type, .diagnostics)
13441344
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-serialize-diagnostics-path")))
13451345
XCTAssert(plannedJobs[0].commandLine.contains(.path(.temporary(RelativePath("TestInputHeader.dia")))))
13461346
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-emit-pch")))
@@ -1363,10 +1363,10 @@ final class SwiftDriverTests: XCTestCase {
13631363
XCTAssertEqual(plannedJobs[0].inputs[0].file, .relative(RelativePath("TestInputHeader.h")))
13641364
XCTAssertEqual(plannedJobs[0].inputs[0].type, .objcHeader)
13651365
XCTAssertEqual(plannedJobs[0].outputs.count, 2)
1366-
XCTAssertNotNil(plannedJobs[0].outputs[0].file.name.range(of: #"/pch/TestInputHeader-.*.dia"#, options: .regularExpression))
1367-
XCTAssertEqual(plannedJobs[0].outputs[0].type, .diagnostics)
1368-
XCTAssertEqual(plannedJobs[0].outputs[1].file, try VirtualPath(path: "/pch/TestInputHeader.pch"))
1369-
XCTAssertEqual(plannedJobs[0].outputs[1].type, .pch)
1366+
XCTAssertEqual(plannedJobs[0].outputs[0].file, try VirtualPath(path: "/pch/TestInputHeader.pch"))
1367+
XCTAssertEqual(plannedJobs[0].outputs[0].type, .pch)
1368+
XCTAssertNotNil(plannedJobs[0].outputs[1].file.name.range(of: #"/pch/TestInputHeader-.*.dia"#, options: .regularExpression))
1369+
XCTAssertEqual(plannedJobs[0].outputs[1].type, .diagnostics)
13701370
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-serialize-diagnostics-path")))
13711371
XCTAssert(plannedJobs[0].commandLine.contains {
13721372
guard case .path(let path) = $0 else { return false }

0 commit comments

Comments
 (0)