diff --git a/Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift b/Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift index a0c0a02a0..085f27f8f 100644 --- a/Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift +++ b/Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift @@ -160,8 +160,11 @@ extension WebAssemblyToolchain { throw Error.sanitizersUnsupportedForTarget(targetTriple.triple) } - guard !parsedOptions.hasArgument(.profileGenerate) else { - throw Error.profilingUnsupportedForTarget(targetTriple.triple) + if parsedOptions.hasArgument(.profileGenerate) { + let libProfile = VirtualPath.lookup(targetInfo.runtimeResourcePath.path) + .appending(components: "clang", "lib", targetTriple.osName, + "libclang_rt.profile-\(targetTriple.archName).a") + commandLine.appendPath(libProfile) } if let lto = lto { diff --git a/Tests/SwiftDriverTests/SwiftDriverTests.swift b/Tests/SwiftDriverTests/SwiftDriverTests.swift index 1fc27396b..118a9830e 100644 --- a/Tests/SwiftDriverTests/SwiftDriverTests.swift +++ b/Tests/SwiftDriverTests/SwiftDriverTests.swift @@ -4463,6 +4463,27 @@ final class SwiftDriverTests: XCTestCase { } #endif + // -profile-generate should add libclang_rt.profile for WebAssembly targets + try withTemporaryDirectory { resourceDir in + try localFileSystem.writeFileContents(resourceDir.appending(components: "wasi", "static-executable-args.lnk")) { + $0.send("garbage") + } + + for triple in ["wasm32-unknown-wasi", "wasm32-unknown-wasip1-threads"] { + var driver = try Driver(args: [ + "swiftc", "-profile-generate", "-target", triple, "test.swift", + "-resource-dir", resourceDir.pathString + ]) + let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs() + + XCTAssertEqual(plannedJobs.count, 2) + XCTAssertEqual(plannedJobs[0].kind, .compile) + + XCTAssertEqual(plannedJobs[1].kind, .link) + XCTAssert(plannedJobs[1].commandLine.containsPathWithBasename("libclang_rt.profile-wasm32.a")) + } + } + for explicitUseLd in [true, false] { var args = ["swiftc", "-profile-generate", "-target", "x86_64-unknown-windows-msvc", "test.swift"] if explicitUseLd {