diff --git a/Sources/Build/BuildDescription/ProductBuildDescription.swift b/Sources/Build/BuildDescription/ProductBuildDescription.swift index 2eec24fa713..efdc497f14b 100644 --- a/Sources/Build/BuildDescription/ProductBuildDescription.swift +++ b/Sources/Build/BuildDescription/ProductBuildDescription.swift @@ -122,15 +122,6 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription return ["-Xlinker", "-dead_strip"] } else if triple.isWindows() { return ["-Xlinker", "/OPT:REF"] - } else if triple.arch == .wasm32 { - // FIXME: wasm-ld strips data segments referenced through __start/__stop symbols - // during GC, and it removes Swift metadata sections like swift5_protocols - // We should add support of SHF_GNU_RETAIN-like flag for __attribute__((retain)) - // to LLVM and wasm-ld - // This workaround is required for not only WASI but also all WebAssembly triples - // using wasm-ld (e.g. wasm32-unknown-unknown). So this branch is conditioned by - // arch == .wasm32 - return [] } else { return ["-Xlinker", "--gc-sections"] } diff --git a/Tests/BuildTests/CrossCompilationBuildPlanTests.swift b/Tests/BuildTests/CrossCompilationBuildPlanTests.swift index e492abfc35a..be15e15af0e 100644 --- a/Tests/BuildTests/CrossCompilationBuildPlanTests.swift +++ b/Tests/BuildTests/CrossCompilationBuildPlanTests.swift @@ -70,6 +70,39 @@ final class CrossCompilationBuildPlanTests: XCTestCase { ) } + func testWasmTargetRelease() throws { + let pkgPath = AbsolutePath("/Pkg") + + let (graph, fs, observabilityScope) = try trivialPackageGraph(pkgRootPath: pkgPath) + + var parameters = mockBuildParameters( + config: .release, targetTriple: .wasi, linkerDeadStrip: true + ) + parameters.linkingParameters.shouldLinkStaticSwiftStdlib = true + let result = try BuildPlanResult(plan: BuildPlan( + buildParameters: parameters, + graph: graph, + fileSystem: fs, + observabilityScope: observabilityScope + )) + let buildPath = result.plan.productsBuildPath + + let appBuildDescription = try result.buildProduct(for: "app") + XCTAssertEqual( + try appBuildDescription.linkArguments(), + [ + result.plan.destinationBuildParameters.toolchain.swiftCompilerPath.pathString, + "-L", buildPath.pathString, + "-o", buildPath.appending(components: "app.wasm").pathString, + "-module-name", "app", "-static-stdlib", "-emit-executable", + "-Xlinker", "--gc-sections", + "@\(buildPath.appending(components: "app.product", "Objects.LinkFileList"))", + "-target", "wasm32-unknown-wasi", + "-g", + ] + ) + } + func testWASITarget() throws { let pkgPath = AbsolutePath("/Pkg")