Skip to content

Commit 6e8ef82

Browse files
committed
[embeded] A number of changes to support embeded Swift.
1 parent a213096 commit 6e8ef82

File tree

5 files changed

+96
-19
lines changed

5 files changed

+96
-19
lines changed

Sources/SwiftDriver/Jobs/CompileJob.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,13 +363,24 @@ extension Driver {
363363
commandLine.appendFlag(map)
364364
}
365365

366+
let expirementalFeatures = parsedOptions.arguments(for: .enableExperimentalFeature)
367+
let embeddedEnabled = expirementalFeatures.map(\.argument).map(\.asSingle).contains("Embedded")
368+
366369
try commandLine.appendLast(.trackSystemDependencies, from: &parsedOptions)
367370
try commandLine.appendLast(.CrossModuleOptimization, from: &parsedOptions)
368371
try commandLine.appendLast(.ExperimentalPerformanceAnnotations, from: &parsedOptions)
369-
try commandLine.appendLast(.disableAutolinkingRuntimeCompatibility, from: &parsedOptions)
370-
try commandLine.appendLast(.runtimeCompatibilityVersion, from: &parsedOptions)
371-
try commandLine.appendLast(.disableAutolinkingRuntimeCompatibilityDynamicReplacements, from: &parsedOptions)
372-
try commandLine.appendLast(.disableAutolinkingRuntimeCompatibilityConcurrency, from: &parsedOptions)
372+
373+
if !embeddedEnabled {
374+
try commandLine.appendLast(.disableAutolinkingRuntimeCompatibility, from: &parsedOptions)
375+
try commandLine.appendLast(.runtimeCompatibilityVersion, from: &parsedOptions)
376+
try commandLine.appendLast(.disableAutolinkingRuntimeCompatibilityDynamicReplacements, from: &parsedOptions)
377+
try commandLine.appendLast(.disableAutolinkingRuntimeCompatibilityConcurrency, from: &parsedOptions)
378+
} else {
379+
try commandLine.appendFlag(.disableAutolinkingRuntimeCompatibility)
380+
try commandLine.appendFlag(.disableAutolinkingRuntimeCompatibilityDynamicReplacements)
381+
try commandLine.appendFlag(.disableAutolinkingRuntimeCompatibilityConcurrency)
382+
}
383+
373384
try commandLine.appendLast(.checkApiAvailabilityOnly, from: &parsedOptions)
374385

375386
try addCommonSymbolGraphOptions(commandLine: &commandLine,

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,11 @@ extension Driver {
107107
commandLine.appendFlag("-aarch64-use-tbi")
108108
}
109109

110+
let expirementalFeatures = parsedOptions.arguments(for: .enableExperimentalFeature)
111+
let embeddedEnabled = expirementalFeatures.map(\.argument).map(\.asSingle).contains("Embedded")
112+
110113
// Enable or disable ObjC interop appropriately for the platform
111-
if targetTriple.isDarwin {
114+
if targetTriple.isDarwin && !embeddedEnabled {
112115
commandLine.appendFlag(.enableObjcInterop)
113116
} else {
114117
commandLine.appendFlag(.disableObjcInterop)
@@ -122,6 +125,22 @@ extension Driver {
122125
commandLine.appendFlag("-stdlib=\(stdlibVariant)")
123126
}
124127

128+
if embeddedEnabled && parsedOptions.hasArgument(.enableLibraryEvolution) {
129+
diagnosticEngine.emit(.error_no_library_evolution_embedded)
130+
throw ErrorDiagnostics.emitted
131+
}
132+
133+
if embeddedEnabled &&
134+
(!parsedOptions.hasArgument(.wmo) || !parsedOptions.hasArgument(.wholeModuleOptimization)) {
135+
diagnosticEngine.emit(.error_need_wmo_embedded)
136+
throw ErrorDiagnostics.emitted
137+
}
138+
139+
if embeddedEnabled && parsedOptions.hasArgument(.enableObjcInterop) {
140+
diagnosticEngine.emit(.error_no_objc_interop_embedded)
141+
throw ErrorDiagnostics.emitted
142+
}
143+
125144
// Handle the CPU and its preferences.
126145
try commandLine.appendLast(.targetCpu, from: &parsedOptions)
127146

Sources/SwiftDriver/Jobs/Toolchain+LinkerSupport.swift

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,26 @@ extension DarwinToolchain {
117117
}
118118
}
119119

120-
for compatibilityLib in targetInfo.target.compatibilityLibraries {
121-
let shouldLink: Bool
122-
switch compatibilityLib.filter {
123-
case .all:
124-
shouldLink = true
125-
break
126-
127-
case .executable:
128-
shouldLink = linkerOutputType == .executable
129-
}
120+
let expirementalFeatures = parsedOptions.arguments(for: .enableExperimentalFeature)
121+
let embeddedEnabled = expirementalFeatures.map(\.argument).map(\.asSingle).contains("Embedded")
122+
123+
if !embeddedEnabled {
124+
for compatibilityLib in targetInfo.target.compatibilityLibraries {
125+
let shouldLink: Bool
126+
switch compatibilityLib.filter {
127+
case .all:
128+
shouldLink = true
129+
break
130+
131+
case .executable:
132+
shouldLink = linkerOutputType == .executable
133+
}
130134

131-
if shouldLink {
132-
// Old frontends don't set forceLoad at all; assume it's true in that case
133-
try addArgsForBackDeployLib("lib" + compatibilityLib.libraryName + ".a",
134-
forceLoad: compatibilityLib.forceLoad ?? true)
135+
if shouldLink {
136+
// Old frontends don't set forceLoad at all; assume it's true in that case
137+
try addArgsForBackDeployLib("lib" + compatibilityLib.libraryName + ".a",
138+
forceLoad: compatibilityLib.forceLoad ?? true)
139+
}
135140
}
136141
}
137142

Sources/SwiftDriver/Utilities/Diagnostics.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,16 @@ extension Diagnostic.Message {
166166
static func error_expected_frontend_command() -> Diagnostic.Message {
167167
.error("expected a swift frontend command")
168168
}
169+
170+
static var error_no_library_evolution_embedded: Diagnostic.Message {
171+
.error("Library evolution cannot be enabled with embedded Swift.")
172+
}
173+
174+
static var error_need_wmo_embedded: Diagnostic.Message {
175+
.error("Whole module optimization (wmo) must be enabled with embedded Swift.")
176+
}
177+
178+
static var error_no_objc_interop_embedded: Diagnostic.Message {
179+
.error("Objective-C interop cannot be enabled with embedded Swift.")
180+
}
169181
}

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6288,6 +6288,36 @@ final class SwiftDriverTests: XCTestCase {
62886288
}
62896289
}
62906290

6291+
func testEmbeddedSwiftOptions() throws {
6292+
do {
6293+
var driver = try Driver(args: ["swiftc", "-target", "arm64-apple-macosx10.13", "test.swift", "-enable-experimental-feature", "Embedded", "-parse-as-library", "-wmo", "-o", "a.out", "-module-name", "main"])
6294+
let plannedJobs = try driver.planBuild()
6295+
XCTAssertEqual(plannedJobs.count, 2)
6296+
let compileJob = plannedJobs[0]
6297+
let linkJob = plannedJobs[1]
6298+
XCTAssertTrue(compileJob.commandLine.contains(.flag("-disable-objc-interop")))
6299+
XCTAssertFalse(linkJob.commandLine.contains(.flag("-force_load")))
6300+
}
6301+
do {
6302+
let diags = DiagnosticsEngine()
6303+
var driver = try Driver(args: ["swiftc", "-target", "arm64-apple-macosx10.13", "test.swift", "-enable-experimental-feature", "Embedded", "-parse-as-library", "-wmo", "-o", "a.out", "-module-name", "main", "-enable-library-evolution"], diagnosticsEngine: diags)
6304+
_ = try driver.planBuild()
6305+
XCTAssertTrue(diags.diagnostics.first!.message.text == "Library evolution cannot be enabled with embedded Swift.")
6306+
} catch _ { }
6307+
do {
6308+
let diags = DiagnosticsEngine()
6309+
var driver = try Driver(args: ["swiftc", "-target", "arm64-apple-macosx10.13", "test.swift", "-enable-experimental-feature", "Embedded", "-parse-as-library", "-o", "a.out", "-module-name", "main"], diagnosticsEngine: diags)
6310+
_ = try driver.planBuild()
6311+
XCTAssertTrue(diags.diagnostics.first!.message.text == "Whole module optimization (wmo) must be enabled with embedded Swift.")
6312+
} catch _ { }
6313+
do {
6314+
let diags = DiagnosticsEngine()
6315+
var driver = try Driver(args: ["swiftc", "-target", "arm64-apple-macosx10.13", "test.swift", "-enable-experimental-feature", "Embedded", "-parse-as-library", "-wmo", "-o", "a.out", "-module-name", "main", "-enable-objc-interop"], diagnosticsEngine: diags)
6316+
_ = try driver.planBuild()
6317+
XCTAssertTrue(diags.diagnostics.first!.message.text == "Objective-C interop cannot be enabled with embedded Swift.")
6318+
} catch _ { }
6319+
}
6320+
62916321
func testVFSOverlay() throws {
62926322
do {
62936323
var driver = try Driver(args: ["swiftc", "-c", "-vfsoverlay", "overlay.yaml", "foo.swift"])

0 commit comments

Comments
 (0)