Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions Sources/SwiftDriver/Driver/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,14 @@ public struct Driver {
let importedObjCHeader: VirtualPath.Handle?

/// The path to the pch for the imported Objective-C header.
let bridgingPrecompiledHeader: VirtualPath.Handle?
lazy var bridgingPrecompiledHeader: VirtualPath.Handle? = {
let contextHash = try? explicitDependencyBuildPlanner?.getMainModuleContextHash()
return Self.computeBridgingPrecompiledHeader(&parsedOptions,
compilerMode: compilerMode,
importedObjCHeader: importedObjCHeader,
outputFileMap: outputFileMap,
contextHash: contextHash)
}()

/// Path to the dependencies file.
let dependenciesFilePath: VirtualPath.Handle?
Expand Down Expand Up @@ -801,10 +808,6 @@ public struct Driver {
recordedInputModificationDates: recordedInputModificationDates)

self.importedObjCHeader = try Self.computeImportedObjCHeader(&parsedOptions, compilerMode: compilerMode, diagnosticEngine: diagnosticEngine)
self.bridgingPrecompiledHeader = try Self.computeBridgingPrecompiledHeader(&parsedOptions,
compilerMode: compilerMode,
importedObjCHeader: importedObjCHeader,
outputFileMap: outputFileMap)

self.supportedFrontendFlags =
try Self.computeSupportedCompilerArgs(of: self.toolchain,
Expand All @@ -829,7 +832,7 @@ public struct Driver {
diagnosticsEngine.emit(.warning("-cache-compile-job cannot be used without explicit module build, turn off caching"),
location: nil)
self.enableCaching = false
} else if importedObjCHeader != nil && bridgingPrecompiledHeader == nil {
} else if importedObjCHeader != nil, !parsedOptions.hasFlag(positive: .enableBridgingPch, negative: .disableBridgingPch, default: true) {
diagnosticsEngine.emit(.warning("-cache-compile-job cannot be used with -disable-bridging-pch, turn off caching"),
location: nil)
self.enableCaching = false
Expand Down Expand Up @@ -1779,7 +1782,7 @@ extension Driver {
/// The swift-driver doesn't have actions, so the logic here takes the jobs and tries
/// to mimic the actions that would be created by the C++ driver and
/// prints them in *hopefully* the same order.
private func printActions(_ jobs: [Job]) {
private mutating func printActions(_ jobs: [Job]) {
defer {
stdoutStream.flush()
}
Expand Down Expand Up @@ -2872,23 +2875,29 @@ extension Driver {
static func computeBridgingPrecompiledHeader(_ parsedOptions: inout ParsedOptions,
compilerMode: CompilerMode,
importedObjCHeader: VirtualPath.Handle?,
outputFileMap: OutputFileMap?) throws -> VirtualPath.Handle? {
outputFileMap: OutputFileMap?,
contextHash: String?) -> VirtualPath.Handle? {
guard compilerMode.supportsBridgingPCH,
let input = importedObjCHeader,
parsedOptions.hasFlag(positive: .enableBridgingPch, negative: .disableBridgingPch, default: true) else {
return nil
}

if let outputPath = try outputFileMap?.existingOutput(inputFile: input, outputType: .pch) {
if let outputPath = try? outputFileMap?.existingOutput(inputFile: input, outputType: .pch) {
return outputPath
}

let inputFile = VirtualPath.lookup(input)
let pchFileName = inputFile.basenameWithoutExt.appendingFileTypeExtension(.pch)
let pchFile : String
let baseName = VirtualPath.lookup(input).basenameWithoutExt
if let hash = contextHash {
pchFile = baseName + "-" + hash + ".pch"
} else {
pchFile = baseName.appendingFileTypeExtension(.pch)
}
if let outputDirectory = parsedOptions.getLastArgument(.pchOutputDir)?.asSingle {
return try VirtualPath(path: outputDirectory).appending(component: pchFileName).intern()
return try? VirtualPath(path: outputDirectory).appending(component: pchFile).intern()
} else {
return try VirtualPath.createUniqueTemporaryFile(RelativePath(validating: pchFileName)).intern()
return try? VirtualPath.temporary(RelativePath(validating: pchFile)).intern()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,13 @@ public typealias ExternalTargetModuleDetailsMap = [ModuleDependencyId: ExternalT
commandLine.append(contentsOf: mainModuleDependenciesArgs.commandLine)
}

/// Get the context hash for the main module.
public func getMainModuleContextHash() throws -> String? {
let mainModuleId: ModuleDependencyId = .swift(dependencyGraph.mainModuleName)
let mainModuleDetails = try dependencyGraph.swiftModuleDetails(of: mainModuleId)
return mainModuleDetails.contextHash
}

/// Resolve all module dependencies of the main module and add them to the lists of
/// inputs and command line flags.
public mutating func resolveBridgingHeaderDependencies(inputs: inout [TypedVirtualPath],
Expand Down
28 changes: 21 additions & 7 deletions Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ extension Driver {
jobNeedPathRemap = false
}

// Check if dependency scanner has put the job into direct clang cc1 mode.
// If dependency scanner put us into direct cc1 mode, avoid adding `-Xcc` options, since
// dependency scanner already adds needed flags and -Xcc options known by swift-driver are
// clang driver flags but not it requires cc1 flags.
let directModuleCC1Mode = commandLine.contains(Job.ArgTemplate.flag("-direct-clang-cc1-module-build"))
func appendXccFlag(_ flag: String) {
guard !directModuleCC1Mode else { return }
commandLine.appendFlag(.Xcc)
commandLine.appendFlag(flag)
}

if let variant = parsedOptions.getLastArgument(.targetVariant)?.asSingle {
commandLine.appendFlag(.targetVariant)
commandLine.appendFlag(Triple(variant, normalizing: true).triple)
Expand All @@ -125,8 +136,7 @@ extension Driver {
try commandLine.appendLast(.enableExperimentalCxxInterop, from: &parsedOptions)
try commandLine.appendLast(.cxxInteroperabilityMode, from: &parsedOptions)
if let stdlibVariant = parsedOptions.getLastArgument(.experimentalCxxStdlib)?.asSingle {
commandLine.appendFlag("-Xcc")
commandLine.appendFlag("-stdlib=\(stdlibVariant)")
appendXccFlag("-stdlib=\(stdlibVariant)")
}

if isEmbeddedEnabled && parsedOptions.hasArgument(.enableLibraryEvolution) {
Expand Down Expand Up @@ -173,8 +183,7 @@ extension Driver {
try commandLine.appendAll(.vfsoverlay, from: &parsedOptions)

if let gccToolchain = parsedOptions.getLastArgument(.gccToolchain) {
commandLine.appendFlag(.Xcc)
commandLine.appendFlag("--gcc-toolchain=\(gccToolchain.asSingle)")
appendXccFlag("--gcc-toolchain=\(gccToolchain.asSingle)")
}

try commandLine.appendLast(.AssertConfig, from: &parsedOptions)
Expand Down Expand Up @@ -272,6 +281,11 @@ extension Driver {
if isFrontendArgSupported(.strictConcurrency) {
try commandLine.appendLast(.strictConcurrency, from: &parsedOptions)
}
if kind == .scanDependencies,
isFrontendArgSupported(.experimentalClangImporterDirectCc1Scan) {
try commandLine.appendAll(
.experimentalClangImporterDirectCc1Scan, from: &parsedOptions)
}

// Expand the -experimental-hermetic-seal-at-link flag
if parsedOptions.hasArgument(.experimentalHermeticSealAtLink) {
Expand Down Expand Up @@ -332,7 +346,7 @@ extension Driver {
try commandLine.appendLast(.disableSandbox, from: &parsedOptions)
}

if !(isCachingEnabled && useClangIncludeTree), let workingDirectory = workingDirectory {
if !directModuleCC1Mode, let workingDirectory = workingDirectory {
// Add -Xcc -working-directory before any other -Xcc options to ensure it is
// overridden by an explicit -Xcc -working-directory, although having a
// different working directory is probably incorrect.
Expand Down Expand Up @@ -402,8 +416,8 @@ extension Driver {
// Pass through any subsystem flags.
try commandLine.appendAll(.Xllvm, from: &parsedOptions)

// If using clang-include-tree, `-Xcc` should only be passed to scanDependencies job.
if (kind == .scanDependencies) || !(isCachingEnabled && useClangIncludeTree) {
// Pass through all -Xcc flags if not under directModuleCC1Mode.
if !directModuleCC1Mode {
try commandLine.appendAll(.Xcc, from: &parsedOptions)
}

Expand Down
Loading