Skip to content

Commit 1ab1ede

Browse files
authored
Merge pull request #3001 from kateinoigakukun/katei/static-stdlib-wasm32-main
[WASM] Propagate -static-stdlib for wasm32
2 parents e74d91f + 6ae5b6c commit 1ab1ede

File tree

2 files changed

+82
-5
lines changed

2 files changed

+82
-5
lines changed

Sources/Build/BuildPlan.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,7 @@ public final class ProductBuildDescription {
11051105
if buildParameters.shouldLinkStaticSwiftStdlib {
11061106
if buildParameters.triple.isDarwin() {
11071107
diagnostics.emit(.swiftBackDeployError)
1108-
} else if buildParameters.triple.isLinux() {
1108+
} else if buildParameters.triple.isLinux() || buildParameters.triple.arch == .wasm32 {
11091109
args += ["-static-stdlib"]
11101110
}
11111111
}

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,8 +1577,8 @@ final class BuildPlanTests: XCTestCase {
15771577
func testWindowsTarget() throws {
15781578
let fs = InMemoryFileSystem(emptyFiles:
15791579
"/Pkg/Sources/exe/main.swift",
1580-
"/Pkg/Sources/lib/lib.c",
1581-
"/Pkg/Sources/lib/include/lib.h"
1580+
"/Pkg/Sources/lib/lib.c",
1581+
"/Pkg/Sources/lib/include/lib.h"
15821582
)
15831583

15841584
let diagnostics = DiagnosticsEngine()
@@ -1603,8 +1603,10 @@ final class BuildPlanTests: XCTestCase {
16031603
result.checkTargetsCount(2)
16041604

16051605
let lib = try result.target(for: "lib").clangTarget()
1606-
var args = ["-target", "x86_64-unknown-windows-msvc", "-g", "-gcodeview", "-O0", "-DSWIFT_PACKAGE=1", "-DDEBUG=1"]
1607-
args += ["-fblocks", "-I", "/Pkg/Sources/lib/include"]
1606+
let args = [
1607+
"-target", "x86_64-unknown-windows-msvc", "-g", "-gcodeview", "-O0",
1608+
"-DSWIFT_PACKAGE=1", "-DDEBUG=1", "-fblocks", "-I", "/Pkg/Sources/lib/include"
1609+
]
16081610
XCTAssertEqual(lib.basicArguments(), args)
16091611
XCTAssertEqual(lib.objects, [AbsolutePath("/path/to/build/debug/lib.build/lib.c.o")])
16101612
XCTAssertEqual(lib.moduleMap, AbsolutePath("/path/to/build/debug/lib.build/module.modulemap"))
@@ -1624,6 +1626,80 @@ final class BuildPlanTests: XCTestCase {
16241626
XCTAssertMatch(executablePathExtension, "exe")
16251627
}
16261628

1629+
func testWASITarget() throws {
1630+
let fs = InMemoryFileSystem(emptyFiles:
1631+
"/Pkg/Sources/app/main.swift",
1632+
"/Pkg/Sources/lib/lib.c",
1633+
"/Pkg/Sources/lib/include/lib.h"
1634+
)
1635+
1636+
let diagnostics = DiagnosticsEngine()
1637+
let graph = loadPackageGraph(
1638+
fs: fs, diagnostics: diagnostics,
1639+
manifests: [
1640+
Manifest.createV4Manifest(
1641+
name: "Pkg",
1642+
path: "/Pkg",
1643+
url: "/Pkg",
1644+
packageKind: .root,
1645+
targets: [
1646+
TargetDescription(name: "app", dependencies: ["lib"]),
1647+
TargetDescription(name: "lib", dependencies: []),
1648+
]),
1649+
]
1650+
)
1651+
XCTAssertNoDiagnostics(diagnostics)
1652+
1653+
var parameters = mockBuildParameters(destinationTriple: .wasi)
1654+
parameters.shouldLinkStaticSwiftStdlib = true
1655+
let result = BuildPlanResult(
1656+
plan: try BuildPlan(
1657+
buildParameters: parameters,
1658+
graph: graph,
1659+
diagnostics: diagnostics,
1660+
fileSystem: fs
1661+
)
1662+
)
1663+
result.checkProductsCount(1)
1664+
result.checkTargetsCount(2)
1665+
1666+
let lib = try result.target(for: "lib").clangTarget()
1667+
let args = [
1668+
"-target", "wasm32-unknown-wasi", "-g", "-O0", "-DSWIFT_PACKAGE=1", "-DDEBUG=1",
1669+
"-fblocks", "-fmodules", "-fmodule-name=lib", "-I", "/Pkg/Sources/lib/include",
1670+
"-fmodules-cache-path=/path/to/build/debug/ModuleCache"
1671+
]
1672+
XCTAssertEqual(lib.basicArguments(), args)
1673+
XCTAssertEqual(lib.objects, [AbsolutePath("/path/to/build/debug/lib.build/lib.c.o")])
1674+
XCTAssertEqual(lib.moduleMap, AbsolutePath("/path/to/build/debug/lib.build/module.modulemap"))
1675+
1676+
let exe = try result.target(for: "app").swiftTarget().compileArguments()
1677+
XCTAssertMatch(
1678+
exe,
1679+
[
1680+
"-swift-version", "4", "-enable-batch-mode", "-Onone", "-enable-testing", "-g",
1681+
.equal(j), "-DSWIFT_PACKAGE", "-DDEBUG","-Xcc",
1682+
"-fmodule-map-file=/path/to/build/debug/lib.build/module.modulemap",
1683+
"-I", "/Pkg/Sources/lib/include",
1684+
"-module-cache-path", "/path/to/build/debug/ModuleCache", .anySequence
1685+
]
1686+
)
1687+
1688+
XCTAssertEqual(
1689+
try result.buildProduct(for: "app").linkArguments(),
1690+
[
1691+
"/fake/path/to/swiftc", "-L", "/path/to/build/debug",
1692+
"-o", "/path/to/build/debug/app.wasm",
1693+
"-module-name", "app", "-static-stdlib", "-emit-executable",
1694+
"@/path/to/build/debug/app.product/Objects.LinkFileList",
1695+
"-target", "wasm32-unknown-wasi"
1696+
]
1697+
)
1698+
1699+
let executablePathExtension = try result.buildProduct(for: "app").binary.extension
1700+
XCTAssertEqual(executablePathExtension, "wasm")
1701+
}
1702+
16271703
func testIndexStore() throws {
16281704
let fs = InMemoryFileSystem(emptyFiles:
16291705
"/Pkg/Sources/exe/main.swift",
@@ -2627,4 +2703,5 @@ fileprivate extension TSCUtility.Triple {
26272703
static let arm64Linux = try! Triple("aarch64-unknown-linux-gnu")
26282704
static let arm64Android = try! Triple("aarch64-unknown-linux-android")
26292705
static let windows = try! Triple("x86_64-unknown-windows-msvc")
2706+
static let wasi = try! Triple("wasm32-unknown-wasi")
26302707
}

0 commit comments

Comments
 (0)