Skip to content

Commit 98747d5

Browse files
authored
Fix .wasm product paths for wasm32-unknown-none triple (#7355)
Absence of this extension on produced binaries makes it inconvenient to work with triple, as you have to add that extension manually after the fact. For examples, HTTP servers frequently don't infer correct mime-type for these binaries, which confuses browsers that try to load these binaries.
1 parent c82304c commit 98747d5

File tree

9 files changed

+325
-112
lines changed

9 files changed

+325
-112
lines changed

Sources/Basics/Triple+Basics.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ extension Triple {
2424
}
2525

2626
extension Triple {
27+
public var isWasm: Bool {
28+
[.wasm32, .wasm64].contains(self.arch)
29+
}
30+
2731
public func isApple() -> Bool {
2832
vendor == .apple
2933
}
@@ -148,6 +152,10 @@ extension Triple {
148152
}
149153

150154
public var executableExtension: String {
155+
guard !self.isWasm else {
156+
return ".wasm"
157+
}
158+
151159
guard let os = self.os else {
152160
return ""
153161
}
@@ -157,8 +165,6 @@ extension Triple {
157165
return ""
158166
case .linux, .openbsd:
159167
return ""
160-
case .wasi:
161-
return ".wasm"
162168
case .win32:
163169
return ".exe"
164170
case .noneOS:

Sources/Build/BuildDescription/ClangTargetBuildDescription.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public final class ClangTargetBuildDescription {
181181
}
182182
}
183183

184-
/// An array of tuple containing filename, source, object and dependency path for each of the source in this target.
184+
/// An array of tuples containing filename, source, object and dependency path for each of the source in this target.
185185
public func compilePaths()
186186
throws -> [(filename: RelativePath, source: AbsolutePath, object: AbsolutePath, deps: AbsolutePath)]
187187
{

Sources/SPMBuildCore/BuildParameters/BuildParameters.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public struct BuildParameters: Encodable {
260260
case .library(.automatic), .plugin:
261261
fatalError()
262262
case .test:
263-
guard !self.triple.isWASI() else {
263+
guard !self.triple.isWasm else {
264264
return try RelativePath(validating: "\(product.name).wasm")
265265
}
266266
switch testingParameters.library {
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2014-2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import struct Basics.AbsolutePath
14+
import class Basics.ObservabilitySystem
15+
import class Basics.ObservabilityScope
16+
import struct PackageGraph.PackageGraph
17+
import class PackageModel.Manifest
18+
import struct PackageModel.ProductDescription
19+
import struct PackageModel.TargetDescription
20+
import protocol TSCBasic.FileSystem
21+
import class TSCBasic.InMemoryFileSystem
22+
23+
@_spi(SwiftPMInternal)
24+
public typealias MockPackageGraph = (
25+
graph: PackageGraph,
26+
fileSystem: any FileSystem,
27+
observabilityScope: ObservabilityScope
28+
)
29+
30+
@_spi(SwiftPMInternal)
31+
public func macrosPackageGraph() throws -> MockPackageGraph {
32+
let fs = InMemoryFileSystem(emptyFiles:
33+
"/swift-firmware/Sources/Core/source.swift",
34+
"/swift-firmware/Sources/HAL/source.swift",
35+
"/swift-firmware/Tests/CoreTests/source.swift",
36+
"/swift-firmware/Tests/HALTests/source.swift",
37+
"/swift-mmio/Sources/MMIO/source.swift",
38+
"/swift-mmio/Sources/MMIOMacros/source.swift",
39+
"/swift-syntax/Sources/SwiftSyntax/source.swift",
40+
"/swift-syntax/Tests/SwiftSyntaxTests/source.swift"
41+
)
42+
43+
let observability = ObservabilitySystem.makeForTesting()
44+
let graph = try loadPackageGraph(
45+
fileSystem: fs,
46+
manifests: [
47+
Manifest.createRootManifest(
48+
displayName: "swift-firmware",
49+
path: "/swift-firmware",
50+
dependencies: [
51+
.localSourceControl(
52+
path: "/swift-mmio",
53+
requirement: .upToNextMajor(from: "1.0.0")
54+
)
55+
],
56+
products: [
57+
ProductDescription(
58+
name: "Core",
59+
type: .executable,
60+
targets: ["Core"]
61+
)
62+
],
63+
targets: [
64+
TargetDescription(
65+
name: "Core",
66+
dependencies: ["HAL"],
67+
type: .executable
68+
),
69+
TargetDescription(
70+
name: "HAL",
71+
dependencies: [.product(name: "MMIO", package: "swift-mmio")]
72+
),
73+
TargetDescription(name: "CoreTests", dependencies: ["Core"], type: .test),
74+
TargetDescription(name: "HALTests", dependencies: ["HAL"], type: .test),
75+
]
76+
),
77+
Manifest.createFileSystemManifest(
78+
displayName: "swift-mmio",
79+
path: "/swift-mmio",
80+
dependencies: [
81+
.localSourceControl(
82+
path: "/swift-syntax",
83+
requirement: .upToNextMajor(from: "1.0.0")
84+
)
85+
],
86+
products: [
87+
ProductDescription(
88+
name: "MMIO",
89+
type: .library(.automatic),
90+
targets: ["MMIO"]
91+
)
92+
],
93+
targets: [
94+
TargetDescription(
95+
name: "MMIO",
96+
dependencies: [.target(name: "MMIOMacros")]
97+
),
98+
TargetDescription(
99+
name: "MMIOMacros",
100+
dependencies: [.product(name: "SwiftSyntax", package: "swift-syntax")],
101+
type: .macro
102+
)
103+
]
104+
),
105+
Manifest.createFileSystemManifest(
106+
displayName: "swift-syntax",
107+
path: "/swift-syntax",
108+
products: [
109+
ProductDescription(
110+
name: "SwiftSyntax",
111+
type: .library(.automatic),
112+
targets: ["SwiftSyntax"]
113+
)
114+
],
115+
targets: [
116+
TargetDescription(name: "SwiftSyntax", dependencies: []),
117+
TargetDescription(name: "SwiftSyntaxTests", dependencies: ["SwiftSyntax"], type: .test),
118+
]
119+
),
120+
],
121+
observabilityScope: observability.topScope
122+
)
123+
124+
XCTAssertNoDiagnostics(observability.diagnostics)
125+
126+
return (graph, fs, observability.topScope)
127+
}
128+
129+
@_spi(SwiftPMInternal)
130+
public func trivialPackageGraph(pkgRootPath: AbsolutePath) throws -> MockPackageGraph {
131+
let fs = InMemoryFileSystem(
132+
emptyFiles:
133+
"/Pkg/Sources/app/main.swift",
134+
"/Pkg/Sources/lib/lib.c",
135+
"/Pkg/Sources/lib/include/lib.h",
136+
"/Pkg/Tests/test/TestCase.swift"
137+
)
138+
139+
let observability = ObservabilitySystem.makeForTesting()
140+
let graph = try loadPackageGraph(
141+
fileSystem: fs,
142+
manifests: [
143+
Manifest.createRootManifest(
144+
displayName: "Pkg",
145+
path: "/Pkg",
146+
targets: [
147+
TargetDescription(name: "app", dependencies: ["lib"]),
148+
TargetDescription(name: "lib", dependencies: []),
149+
TargetDescription(name: "test", dependencies: ["lib"], type: .test),
150+
]
151+
),
152+
],
153+
observabilityScope: observability.topScope
154+
)
155+
XCTAssertNoDiagnostics(observability.diagnostics)
156+
157+
return (graph, fs, observability.topScope)
158+
}

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -3431,111 +3431,6 @@ final class BuildPlanTests: XCTestCase {
34313431
XCTAssertMatch(executablePathExtension, "exe")
34323432
}
34333433

3434-
func testWASITarget() throws {
3435-
let Pkg: AbsolutePath = "/Pkg"
3436-
3437-
let fs = InMemoryFileSystem(
3438-
emptyFiles:
3439-
Pkg.appending(components: "Sources", "app", "main.swift").pathString,
3440-
Pkg.appending(components: "Sources", "lib", "lib.c").pathString,
3441-
Pkg.appending(components: "Sources", "lib", "include", "lib.h").pathString,
3442-
Pkg.appending(components: "Tests", "test", "TestCase.swift").pathString
3443-
)
3444-
3445-
let observability = ObservabilitySystem.makeForTesting()
3446-
let graph = try loadPackageGraph(
3447-
fileSystem: fs,
3448-
manifests: [
3449-
Manifest.createRootManifest(
3450-
displayName: "Pkg",
3451-
path: .init(validating: Pkg.pathString),
3452-
targets: [
3453-
TargetDescription(name: "app", dependencies: ["lib"]),
3454-
TargetDescription(name: "lib", dependencies: []),
3455-
TargetDescription(name: "test", dependencies: ["lib"], type: .test),
3456-
]
3457-
),
3458-
],
3459-
observabilityScope: observability.topScope
3460-
)
3461-
XCTAssertNoDiagnostics(observability.diagnostics)
3462-
3463-
var parameters = mockBuildParameters(targetTriple: .wasi)
3464-
parameters.linkingParameters.shouldLinkStaticSwiftStdlib = true
3465-
let result = try BuildPlanResult(plan: BuildPlan(
3466-
buildParameters: parameters,
3467-
graph: graph,
3468-
fileSystem: fs,
3469-
observabilityScope: observability.topScope
3470-
))
3471-
result.checkProductsCount(2)
3472-
result
3473-
.checkTargetsCount(5) // There are two additional targets on non-Apple platforms, for test discovery and
3474-
// test entry point
3475-
3476-
let buildPath = result.plan.productsBuildPath
3477-
3478-
let lib = try result.target(for: "lib").clangTarget()
3479-
let args = [
3480-
"-target", "wasm32-unknown-wasi",
3481-
"-O0", "-DSWIFT_PACKAGE=1", "-DDEBUG=1",
3482-
"-fblocks",
3483-
"-I", Pkg.appending(components: "Sources", "lib", "include").pathString,
3484-
"-g",
3485-
]
3486-
XCTAssertEqual(try lib.basicArguments(isCXX: false), args)
3487-
XCTAssertEqual(try lib.objects, [buildPath.appending(components: "lib.build", "lib.c.o")])
3488-
XCTAssertEqual(lib.moduleMap, buildPath.appending(components: "lib.build", "module.modulemap"))
3489-
3490-
let exe = try result.target(for: "app").swiftTarget().compileArguments()
3491-
XCTAssertMatch(
3492-
exe,
3493-
[
3494-
"-swift-version", "4", "-enable-batch-mode", "-Onone", "-enable-testing",
3495-
.equal(self.j), "-DSWIFT_PACKAGE", "-DDEBUG", "-Xcc",
3496-
"-fmodule-map-file=\(buildPath.appending(components: "lib.build", "module.modulemap"))",
3497-
"-Xcc", "-I", "-Xcc", "\(Pkg.appending(components: "Sources", "lib", "include"))",
3498-
"-module-cache-path", "\(buildPath.appending(components: "ModuleCache"))", .anySequence,
3499-
"-g", .anySequence,
3500-
]
3501-
)
3502-
3503-
let appBuildDescription = try result.buildProduct(for: "app")
3504-
XCTAssertEqual(
3505-
try appBuildDescription.linkArguments(),
3506-
[
3507-
result.plan.destinationBuildParameters.toolchain.swiftCompilerPath.pathString,
3508-
"-L", buildPath.pathString,
3509-
"-o", buildPath.appending(components: "app.wasm").pathString,
3510-
"-module-name", "app", "-static-stdlib", "-emit-executable",
3511-
"@\(buildPath.appending(components: "app.product", "Objects.LinkFileList"))",
3512-
"-target", "wasm32-unknown-wasi",
3513-
"-g",
3514-
]
3515-
)
3516-
3517-
let executablePathExtension = try appBuildDescription.binaryPath.extension
3518-
XCTAssertEqual(executablePathExtension, "wasm")
3519-
3520-
let testBuildDescription = try result.buildProduct(for: "PkgPackageTests")
3521-
XCTAssertEqual(
3522-
try testBuildDescription.linkArguments(),
3523-
[
3524-
result.plan.destinationBuildParameters.toolchain.swiftCompilerPath.pathString,
3525-
"-L", buildPath.pathString,
3526-
"-o", buildPath.appending(components: "PkgPackageTests.wasm").pathString,
3527-
"-module-name", "PkgPackageTests",
3528-
"-emit-executable",
3529-
"@\(buildPath.appending(components: "PkgPackageTests.product", "Objects.LinkFileList"))",
3530-
"-target", "wasm32-unknown-wasi",
3531-
"-g",
3532-
]
3533-
)
3534-
3535-
let testPathExtension = try testBuildDescription.binaryPath.extension
3536-
XCTAssertEqual(testPathExtension, "wasm")
3537-
}
3538-
35393434
func testEntrypointRenaming() throws {
35403435
let fs = InMemoryFileSystem(
35413436
emptyFiles:

0 commit comments

Comments
 (0)