Skip to content

Commit 9cd5310

Browse files
authored
Merge pull request #1770 from etcwilde/ewilde/target-variant-modules
Emit supplemental outputs for the target variant
2 parents bfc9c09 + c1dc6cb commit 9cd5310

9 files changed

+557
-160
lines changed

Sources/SwiftDriver/Driver/Driver.swift

+299-124
Large diffs are not rendered by default.

Sources/SwiftDriver/Jobs/APIDigesterJobs.swift

+21-7
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ extension Driver {
4646
var commandLine = [Job.ArgTemplate]()
4747
commandLine.appendFlag("-dump-sdk")
4848

49-
try addCommonDigesterOptions(&commandLine, modulePath: modulePath, mode: mode)
49+
try addCommonDigesterOptions(&commandLine,
50+
modulePath: modulePath,
51+
swiftModuleInterfacePath: self.moduleOutputPaths.swiftInterfacePath,
52+
mode: mode)
5053

5154
commandLine.appendFlag(.o)
5255
commandLine.appendPath(VirtualPath.lookup(outputPath))
@@ -69,12 +72,16 @@ extension Driver {
6972
case .api:
7073
return nil
7174
case .abi:
72-
return abiDescriptorPath
75+
return moduleOutputPaths.abiDescriptorFilePath
7376
}
7477
}
7578
guard let currentABI = getDescriptorPath(for: mode) else {
7679
// we don't have existing descriptor to use so we have to load the module from interface/swiftmodule
77-
return try digesterCompareToBaselineJob(modulePath: modulePath, baselinePath: baselinePath, mode: digesterMode)
80+
return try digesterCompareToBaselineJob(
81+
modulePath: modulePath,
82+
swiftModuleInterfacePath: self.moduleOutputPaths.swiftInterfacePath,
83+
baselinePath: baselinePath,
84+
mode: digesterMode)
7885
}
7986
var commandLine = [Job.ArgTemplate]()
8087
commandLine.appendFlag("-diagnose-sdk")
@@ -105,14 +112,20 @@ extension Driver {
105112
)
106113
}
107114

108-
mutating func digesterCompareToBaselineJob(modulePath: VirtualPath.Handle, baselinePath: VirtualPath.Handle, mode: DigesterMode) throws -> Job {
115+
mutating func digesterCompareToBaselineJob(modulePath: VirtualPath.Handle,
116+
swiftModuleInterfacePath: VirtualPath.Handle?,
117+
baselinePath: VirtualPath.Handle,
118+
mode: DigesterMode) throws -> Job {
109119
var commandLine = [Job.ArgTemplate]()
110120
commandLine.appendFlag("-diagnose-sdk")
111121
commandLine.appendFlag("-disable-fail-on-error")
112122
commandLine.appendFlag("-baseline-path")
113123
commandLine.appendPath(VirtualPath.lookup(baselinePath))
114124

115-
try addCommonDigesterOptions(&commandLine, modulePath: modulePath, mode: mode)
125+
try addCommonDigesterOptions(&commandLine,
126+
modulePath: modulePath,
127+
swiftModuleInterfacePath: swiftModuleInterfacePath,
128+
mode: mode)
116129

117130
var serializedDiagnosticsPath: VirtualPath.Handle?
118131
if let arg = parsedOptions.getLastArgument(.serializeBreakingChangesPath)?.asSingle {
@@ -130,7 +143,7 @@ extension Driver {
130143
var inputs: [TypedVirtualPath] = [.init(file: modulePath, type: .swiftModule),
131144
.init(file: baselinePath, type: mode.baselineFileType)]
132145
// If a module interface was emitted, treat it as an input in ABI mode.
133-
if let interfacePath = self.swiftInterfacePath, mode == .abi {
146+
if let interfacePath = swiftModuleInterfacePath, mode == .abi {
134147
inputs.append(.init(file: interfacePath, type: .swiftInterface))
135148
}
136149

@@ -147,6 +160,7 @@ extension Driver {
147160

148161
private mutating func addCommonDigesterOptions(_ commandLine: inout [Job.ArgTemplate],
149162
modulePath: VirtualPath.Handle,
163+
swiftModuleInterfacePath: VirtualPath.Handle?,
150164
mode: DigesterMode) throws {
151165
commandLine.appendFlag("-module")
152166
commandLine.appendFlag(moduleOutputInfo.name)
@@ -160,7 +174,7 @@ extension Driver {
160174
let searchPath = VirtualPath.lookup(modulePath).parentDirectory
161175
commandLine.appendFlag(.I)
162176
commandLine.appendPath(searchPath)
163-
if let interfacePath = self.swiftInterfacePath {
177+
if let interfacePath = swiftModuleInterfacePath {
164178
let interfaceSearchPath = VirtualPath.lookup(interfacePath).parentDirectory
165179
if interfaceSearchPath != searchPath {
166180
commandLine.appendFlag(.I)

Sources/SwiftDriver/Jobs/CompileJob.swift

+2
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ extension Driver {
277277
primaryInputs: primaryInputs,
278278
inputsGeneratingCodeCount: inputsGeneratingCodeCount,
279279
inputOutputMap: &inputOutputMap,
280+
moduleOutputInfo: self.moduleOutputInfo,
281+
moduleOutputPaths: self.moduleOutputPaths,
280282
includeModuleTracePath: emitModuleTrace,
281283
indexFilePath: indexFilePath)
282284

Sources/SwiftDriver/Jobs/EmitModuleJob.swift

+21-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ extension Driver {
1717
mutating func addCommonModuleOptions(
1818
commandLine: inout [Job.ArgTemplate],
1919
outputs: inout [TypedVirtualPath],
20+
moduleOutputPaths: SupplementalModuleTargetOutputPaths,
2021
isMergeModule: Bool
2122
) throws {
2223
// Add supplemental outputs.
@@ -28,16 +29,16 @@ extension Driver {
2829
outputs.append(.init(file: path, type: type))
2930
}
3031

31-
addSupplementalOutput(path: moduleDocOutputPath, flag: "-emit-module-doc-path", type: .swiftDocumentation)
32-
addSupplementalOutput(path: moduleSourceInfoPath, flag: "-emit-module-source-info-path", type: .swiftSourceInfoFile)
33-
addSupplementalOutput(path: swiftInterfacePath, flag: "-emit-module-interface-path", type: .swiftInterface)
34-
addSupplementalOutput(path: swiftPrivateInterfacePath, flag: "-emit-private-module-interface-path", type: .privateSwiftInterface)
32+
addSupplementalOutput(path: moduleOutputPaths.moduleDocOutputPath, flag: "-emit-module-doc-path", type: .swiftDocumentation)
33+
addSupplementalOutput(path: moduleOutputPaths.moduleSourceInfoPath, flag: "-emit-module-source-info-path", type: .swiftSourceInfoFile)
34+
addSupplementalOutput(path: moduleOutputPaths.swiftInterfacePath, flag: "-emit-module-interface-path", type: .swiftInterface)
35+
addSupplementalOutput(path: moduleOutputPaths.swiftPrivateInterfacePath, flag: "-emit-private-module-interface-path", type: .privateSwiftInterface)
3536
if let pkgName = packageName, !pkgName.isEmpty {
36-
addSupplementalOutput(path: swiftPackageInterfacePath, flag: "-emit-package-module-interface-path", type: .packageSwiftInterface)
37+
addSupplementalOutput(path: moduleOutputPaths.swiftPackageInterfacePath, flag: "-emit-package-module-interface-path", type: .packageSwiftInterface)
3738
}
3839
addSupplementalOutput(path: objcGeneratedHeaderPath, flag: "-emit-objc-header-path", type: .objcHeader)
3940
addSupplementalOutput(path: tbdPath, flag: "-emit-tbd-path", type: .tbd)
40-
addSupplementalOutput(path: apiDescriptorFilePath, flag: "-emit-api-descriptor-path", type: .jsonAPIDescriptor)
41+
addSupplementalOutput(path: moduleOutputPaths.apiDescriptorFilePath, flag: "-emit-api-descriptor-path", type: .jsonAPIDescriptor)
4142

4243
if isMergeModule {
4344
return
@@ -78,8 +79,13 @@ extension Driver {
7879
}
7980

8081
/// Form a job that emits a single module
81-
@_spi(Testing) public mutating func emitModuleJob(pchCompileJob: Job?) throws -> Job {
82-
let moduleOutputPath = moduleOutputInfo.output!.outputPath
82+
@_spi(Testing) public mutating func emitModuleJob(pchCompileJob: Job?, isVariantJob: Bool = false) throws -> Job {
83+
precondition(!isVariantJob || (isVariantJob &&
84+
variantModuleOutputInfo != nil && variantModuleOutputPaths != nil),
85+
"target variant module requested without a target variant")
86+
87+
let moduleOutputPath = isVariantJob ? variantModuleOutputInfo!.output!.outputPath : moduleOutputInfo.output!.outputPath
88+
let moduleOutputPaths = isVariantJob ? variantModuleOutputPaths! : moduleOutputPaths
8389
var commandLine: [Job.ArgTemplate] = swiftCompilerPrefixArgs.map { Job.ArgTemplate.flag($0) }
8490
var inputs: [TypedVirtualPath] = []
8591
var outputs: [TypedVirtualPath] = [
@@ -102,7 +108,12 @@ extension Driver {
102108
try addCommonFrontendOptions(commandLine: &commandLine, inputs: &inputs, kind: .emitModule)
103109
// FIXME: Add MSVC runtime library flags
104110

105-
try addCommonModuleOptions(commandLine: &commandLine, outputs: &outputs, isMergeModule: false)
111+
try addCommonModuleOptions(
112+
commandLine: &commandLine,
113+
outputs: &outputs,
114+
moduleOutputPaths: moduleOutputPaths,
115+
isMergeModule: false)
116+
106117
try addCommonSymbolGraphOptions(commandLine: &commandLine)
107118

108119
try commandLine.appendLast(.checkApiAvailabilityOnly, from: &parsedOptions)
@@ -114,7 +125,7 @@ extension Driver {
114125
let outputPath = VirtualPath.lookup(moduleOutputPath)
115126
commandLine.appendFlag(.o)
116127
commandLine.appendPath(outputPath)
117-
if let abiPath = abiDescriptorPath {
128+
if let abiPath = moduleOutputPaths.abiDescriptorFilePath {
118129
commandLine.appendFlag(.emitAbiDescriptorPath)
119130
commandLine.appendPath(abiPath.file)
120131
outputs.append(abiPath)

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

+18-10
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,8 @@ extension Driver {
551551
primaryInputs: [TypedVirtualPath],
552552
inputsGeneratingCodeCount: Int,
553553
inputOutputMap: inout [TypedVirtualPath: [TypedVirtualPath]],
554+
moduleOutputInfo: ModuleOutputInfo,
555+
moduleOutputPaths: SupplementalModuleTargetOutputPaths,
554556
includeModuleTracePath: Bool,
555557
indexFilePath: TypedVirtualPath?) throws -> [TypedVirtualPath] {
556558
var flaggedInputOutputPairs: [(flag: String, input: TypedVirtualPath?, output: TypedVirtualPath)] = []
@@ -597,7 +599,9 @@ extension Driver {
597599
}
598600

599601
/// Add all of the outputs needed for a given input.
600-
func addAllOutputsFor(input: TypedVirtualPath?) throws {
602+
func addAllOutputsFor(input: TypedVirtualPath?,
603+
moduleOutputInfo: ModuleOutputInfo,
604+
moduleOutputPaths: SupplementalModuleTargetOutputPaths) throws {
601605
if !emitModuleSeparately {
602606
// Generate the module files with the main job.
603607
try addOutputOfType(
@@ -607,12 +611,12 @@ extension Driver {
607611
flag: "-emit-module-path")
608612
try addOutputOfType(
609613
outputType: .swiftDocumentation,
610-
finalOutputPath: moduleDocOutputPath,
614+
finalOutputPath: moduleOutputPaths.moduleDocOutputPath,
611615
input: input,
612616
flag: "-emit-module-doc-path")
613617
try addOutputOfType(
614618
outputType: .swiftSourceInfoFile,
615-
finalOutputPath: moduleSourceInfoPath,
619+
finalOutputPath: moduleOutputPaths.moduleSourceInfoPath,
616620
input: input,
617621
flag: "-emit-module-source-info-path")
618622
}
@@ -650,10 +654,14 @@ extension Driver {
650654

651655
if compilerMode.usesPrimaryFileInputs {
652656
for input in primaryInputs {
653-
try addAllOutputsFor(input: input)
657+
try addAllOutputsFor(input: input,
658+
moduleOutputInfo: moduleOutputInfo,
659+
moduleOutputPaths: moduleOutputPaths)
654660
}
655661
} else {
656-
try addAllOutputsFor(input: nil)
662+
try addAllOutputsFor(input: nil,
663+
moduleOutputInfo: moduleOutputInfo,
664+
moduleOutputPaths: moduleOutputPaths)
657665

658666
if !emitModuleSeparately {
659667
// Outputs that only make sense when the whole module is processed
@@ -666,20 +674,20 @@ extension Driver {
666674

667675
try addOutputOfType(
668676
outputType: .swiftInterface,
669-
finalOutputPath: swiftInterfacePath,
677+
finalOutputPath: moduleOutputPaths.swiftInterfacePath,
670678
input: nil,
671679
flag: "-emit-module-interface-path")
672680

673681
try addOutputOfType(
674682
outputType: .privateSwiftInterface,
675-
finalOutputPath: swiftPrivateInterfacePath,
683+
finalOutputPath: moduleOutputPaths.swiftPrivateInterfacePath,
676684
input: nil,
677685
flag: "-emit-private-module-interface-path")
678686

679687
if let pkgName = packageName, !pkgName.isEmpty {
680688
try addOutputOfType(
681689
outputType: .packageSwiftInterface,
682-
finalOutputPath: swiftPackageInterfacePath,
690+
finalOutputPath: moduleOutputPaths.swiftPackageInterfacePath,
683691
input: nil,
684692
flag: "-emit-package-module-interface-path")
685693
}
@@ -689,7 +697,7 @@ extension Driver {
689697
input: nil,
690698
flag: "-emit-tbd-path")
691699

692-
if let abiDescriptorPath = abiDescriptorPath {
700+
if let abiDescriptorPath = moduleOutputPaths.abiDescriptorFilePath {
693701
try addOutputOfType(outputType: .jsonABIBaseline,
694702
finalOutputPath: abiDescriptorPath.fileHandle,
695703
input: nil,
@@ -698,7 +706,7 @@ extension Driver {
698706

699707
try addOutputOfType(
700708
outputType: .jsonAPIDescriptor,
701-
finalOutputPath: apiDescriptorFilePath,
709+
finalOutputPath: moduleOutputPaths.apiDescriptorFilePath,
702710
input: nil,
703711
flag: "-emit-api-descriptor-path")
704712
}

Sources/SwiftDriver/Jobs/MergeModuleJob.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ extension Driver {
5757
try addCommonFrontendOptions(commandLine: &commandLine, inputs: &inputs, kind: .mergeModule, bridgingHeaderHandling: .parsed)
5858
// FIXME: Add MSVC runtime library flags
5959

60-
try addCommonModuleOptions(commandLine: &commandLine, outputs: &outputs, isMergeModule: true)
60+
try addCommonModuleOptions(commandLine: &commandLine, outputs: &outputs, moduleOutputPaths: moduleOutputPaths, isMergeModule: true)
6161

6262
try addCommonSymbolGraphOptions(commandLine: &commandLine)
6363

6464
let outputPath = VirtualPath.lookup(moduleOutputInfo.output!.outputPath)
6565
commandLine.appendFlag(.o)
6666
commandLine.appendPath(outputPath)
6767

68-
if let abiPath = abiDescriptorPath {
68+
if let abiPath = moduleOutputPaths.abiDescriptorFilePath {
6969
commandLine.appendFlag(.emitAbiDescriptorPath)
7070
commandLine.appendPath(abiPath.file)
7171
outputs.append(abiPath)

Sources/SwiftDriver/Jobs/Planning.swift

+22-7
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,21 @@ extension Driver {
256256
)
257257
}
258258

259-
private mutating func addEmitModuleJob(addJobBeforeCompiles: (Job) -> Void, pchCompileJob: Job?) throws -> Job? {
260-
if emitModuleSeparately {
261-
let emitJob = try emitModuleJob(pchCompileJob: pchCompileJob)
262-
addJobBeforeCompiles(emitJob)
263-
return emitJob
264-
}
265-
return nil
259+
private mutating func addEmitModuleJob(
260+
addJobBeforeCompiles: (Job) -> Void,
261+
pchCompileJob: Job?,
262+
isVariantModule: Bool = false) throws -> Job? {
263+
// The target variant module is always emitted separately, so we need to
264+
// add an explicit job regardless of whether the primary target was
265+
// emitted separately
266+
if emitModuleSeparately || isVariantModule {
267+
let emitJob = try emitModuleJob(
268+
pchCompileJob: pchCompileJob,
269+
isVariantJob: isVariantModule)
270+
addJobBeforeCompiles(emitJob)
271+
return emitJob
272+
}
273+
return nil
266274
}
267275

268276
private mutating func addJobsFeedingLinker(
@@ -337,6 +345,13 @@ extension Driver {
337345
addLinkerInput: addLinkerInput)
338346
}
339347

348+
if variantModuleOutputInfo != nil {
349+
_ = try addEmitModuleJob(
350+
addJobBeforeCompiles: addJobBeforeCompiles,
351+
pchCompileJob: jobCreatingPch,
352+
isVariantModule: true)
353+
}
354+
340355
try addJobsForPrimaryInputs(
341356
addCompileJobGroup: addCompileJobGroup,
342357
addModuleInput: addModuleInput,

Sources/SwiftOptions/Options.swift

+16
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,14 @@ extension Option {
384384
public static let emitTbdPathEQ: Option = Option("-emit-tbd-path=", .joined, alias: Option.emitTbdPath, attributes: [.frontend, .noInteractive, .argumentIsPath, .supplementaryOutput, .cacheInvariant])
385385
public static let emitTbdPath: Option = Option("-emit-tbd-path", .separate, attributes: [.frontend, .noInteractive, .argumentIsPath, .supplementaryOutput, .cacheInvariant], metaVar: "<path>", helpText: "Emit the TBD file to <path>")
386386
public static let emitTbd: Option = Option("-emit-tbd", .flag, attributes: [.frontend, .noInteractive, .supplementaryOutput], helpText: "Emit a TBD file")
387+
public static let emitVariantAbiDescriptorPath: Option = Option("-emit-variant-abi-descriptor-path", .separate, attributes: [.frontend, .noDriver, .cacheInvariant], metaVar: "<path>", helpText: "Output the ABI descriptor of current target variant module to <path>")
388+
public static let emitVariantApiDescriptorPath: Option = Option("-emit-variant-api-descriptor-path", .separate, attributes: [.frontend, .noInteractive, .argumentIsPath, .supplementaryOutput, .cacheInvariant], metaVar: "<path>", helpText: "Output a JSON file describing the target variant module's API to <path>")
389+
public static let emitVariantModuleDocPath: Option = Option("-emit-variant-module-doc-path", .separate, attributes: [.frontend, .noDriver, .cacheInvariant], metaVar: "<path>", helpText: "Output module documentation file for the target variant to <path>")
390+
public static let emitVariantModuleInterfacePath: Option = Option("-emit-variant-module-interface-path", .separate, attributes: [.frontend, .noInteractive, .argumentIsPath, .supplementaryOutput, .cacheInvariant], metaVar: "<path>", helpText: "Output module interface file for the target variant to <path>")
391+
public static let emitVariantModulePath: Option = Option("-emit-variant-module-path", .separate, attributes: [.noInteractive, .argumentIsPath, .supplementaryOutput, .cacheInvariant], metaVar: "<path>", helpText: "Emit an importable module for the target variant at the specified path")
392+
public static let emitVariantModuleSourceInfoPath: Option = Option("-emit-variant-module-source-info-path", .separate, attributes: [.frontend, .noInteractive, .argumentIsPath, .supplementaryOutput], metaVar: "<path>", helpText: "Output module source info file for the target variant to <path>")
393+
public static let emitVariantPackageModuleInterfacePath: Option = Option("-emit-variant-package-module-interface-path", .separate, attributes: [.frontend, .noInteractive, .argumentIsPath, .supplementaryOutput, .cacheInvariant], metaVar: "<path>", helpText: "Output package module interface file for the target variant to <path>")
394+
public static let emitVariantPrivateModuleInterfacePath: Option = Option("-emit-variant-private-module-interface-path", .separate, attributes: [.frontend, .noInteractive, .argumentIsPath, .supplementaryOutput, .cacheInvariant], metaVar: "<path>", helpText: "Output private module interface file for the target variant to <path>")
387395
public static let emitVerboseSil: Option = Option("-emit-verbose-sil", .flag, attributes: [.helpHidden, .frontend, .noDriver], helpText: "Emit locations during SIL emission")
388396
public static let emptyAbiDescriptor: Option = Option("-empty-abi-descriptor", .flag, attributes: [.frontend, .noDriver], helpText: "Avoid printing actual module content into ABI descriptor file")
389397
public static let emptyBaseline: Option = Option("-empty-baseline", .flag, attributes: [.noDriver], helpText: "Use empty baseline for diagnostics")
@@ -1297,6 +1305,14 @@ extension Option {
12971305
Option.emitTbdPathEQ,
12981306
Option.emitTbdPath,
12991307
Option.emitTbd,
1308+
Option.emitVariantAbiDescriptorPath,
1309+
Option.emitVariantApiDescriptorPath,
1310+
Option.emitVariantModuleDocPath,
1311+
Option.emitVariantModuleInterfacePath,
1312+
Option.emitVariantModulePath,
1313+
Option.emitVariantModuleSourceInfoPath,
1314+
Option.emitVariantPackageModuleInterfacePath,
1315+
Option.emitVariantPrivateModuleInterfacePath,
13001316
Option.emitVerboseSil,
13011317
Option.emptyAbiDescriptor,
13021318
Option.emptyBaseline,

0 commit comments

Comments
 (0)