Skip to content

Commit 047e5a6

Browse files
Detect Embedded build mode by compilation condition
1 parent 64fb506 commit 047e5a6

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

Plugins/PackageToJS/Sources/PackageToJSPlugin.swift

+31-21
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,11 @@ struct PackageToJSPlugin: CommandPlugin {
161161
}
162162

163163
// Build products
164+
let selfPackage = try findSelfPackage(in: context.package)
164165
let productName = try buildOptions.product ?? deriveDefaultProduct(package: context.package)
165166
let build = try buildWasm(
166167
productName: productName,
168+
selfPackage: selfPackage,
167169
context: context,
168170
options: buildOptions.packageOptions
169171
)
@@ -178,14 +180,6 @@ struct PackageToJSPlugin: CommandPlugin {
178180
} else {
179181
context.pluginWorkDirectoryURL.appending(path: "Package")
180182
}
181-
guard
182-
let selfPackage = findPackageInDependencies(
183-
package: context.package,
184-
id: Self.JAVASCRIPTKIT_PACKAGE_ID
185-
)
186-
else {
187-
throw PackageToJSError("Failed to find JavaScriptKit in dependencies!?")
188-
}
189183
var make = MiniMake(
190184
explain: buildOptions.packageOptions.explain,
191185
printProgress: self.printProgress
@@ -226,9 +220,11 @@ struct PackageToJSPlugin: CommandPlugin {
226220
exit(1)
227221
}
228222

223+
let selfPackage = try findSelfPackage(in: context.package)
229224
let productName = "\(context.package.displayName)PackageTests"
230225
let build = try buildWasm(
231226
productName: productName,
227+
selfPackage: selfPackage,
232228
context: context,
233229
options: testOptions.packageOptions
234230
)
@@ -264,14 +260,6 @@ struct PackageToJSPlugin: CommandPlugin {
264260
} else {
265261
context.pluginWorkDirectoryURL.appending(path: "PackageTests")
266262
}
267-
guard
268-
let selfPackage = findPackageInDependencies(
269-
package: context.package,
270-
id: Self.JAVASCRIPTKIT_PACKAGE_ID
271-
)
272-
else {
273-
throw PackageToJSError("Failed to find JavaScriptKit in dependencies!?")
274-
}
275263
var make = MiniMake(
276264
explain: testOptions.packageOptions.explain,
277265
printProgress: self.printProgress
@@ -311,6 +299,7 @@ struct PackageToJSPlugin: CommandPlugin {
311299

312300
private func buildWasm(
313301
productName: String,
302+
selfPackage: Package,
314303
context: PluginContext,
315304
options: PackageToJS.PackageOptions
316305
) throws
@@ -331,11 +320,7 @@ struct PackageToJSPlugin: CommandPlugin {
331320
)
332321
parameters.echoLogs = true
333322
parameters.otherSwiftcFlags = ["-color-diagnostics"]
334-
let buildingForEmbedded =
335-
ProcessInfo.processInfo.environment["JAVASCRIPTKIT_EXPERIMENTAL_EMBEDDED_WASM"].flatMap(
336-
Bool.init
337-
) ?? false
338-
if !buildingForEmbedded {
323+
if !isBuildingForEmbedded(selfPackage: selfPackage) {
339324
// NOTE: We only support static linking for now, and the new SwiftDriver
340325
// does not infer `-static-stdlib` for WebAssembly targets intentionally
341326
// for future dynamic linking support.
@@ -355,6 +340,31 @@ struct PackageToJSPlugin: CommandPlugin {
355340
return try self.packageManager.build(.product(productName), parameters: parameters)
356341
}
357342

343+
/// Check if the build is for embedded WebAssembly
344+
private func isBuildingForEmbedded(selfPackage: Package) -> Bool {
345+
let coreTarget = selfPackage.targets.first { $0.name == "JavaScriptKit" }
346+
guard let swiftTarget = coreTarget as? SwiftSourceModuleTarget else {
347+
return false
348+
}
349+
// SwiftPM defines "Embedded" compilation condition when `Embedded` experimental
350+
// feature is enabled.
351+
// TODO: This should be replaced with a proper trait-based solution in the future.
352+
return swiftTarget.compilationConditions.contains("Embedded")
353+
}
354+
355+
/// Find JavaScriptKit package in the dependencies of the given package recursively
356+
private func findSelfPackage(in package: Package) throws -> Package {
357+
guard
358+
let selfPackage = findPackageInDependencies(
359+
package: package,
360+
id: Self.JAVASCRIPTKIT_PACKAGE_ID
361+
)
362+
else {
363+
throw PackageToJSError("Failed to find JavaScriptKit in dependencies!?")
364+
}
365+
return selfPackage
366+
}
367+
358368
/// Clean if the build graph of the packaging process has changed
359369
///
360370
/// This is especially important to detect user changes debug/release

0 commit comments

Comments
 (0)