Skip to content

Linking executable target failed when swift-tools-version >= 5.5 #3645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Tracked by #3604
kateinoigakukun opened this issue Oct 3, 2021 · 4 comments
Closed
Tracked by #3604

Comments

@kateinoigakukun
Copy link
Member

With swift-wasm-5.5-SNAPSHOT-2021-10-02-a

$ swift package init --type executable
$ swift build --triple wasm32-unknown-wasi
wasm-ld: error: /Library/Developer/Toolchains/swift-wasm-5.5-SNAPSHOT-2021-10-02-a.xctoolchain/usr/share/wasi-sysroot/lib/wasm32-wasi/libc.a(__main_argc_argv.o): undefined symbol: main
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
[0/1] Linking tmp.i3oT7EM2.wasm

The latest SwiftPM rename main entry point name of executable target to avoid conflicting "main" with test target since swift-tools-version >= 5.5.
The main symbol is renamed to "{{module_name}}_main" and it's renamed again to be "main" when linking the executable target. The former renaming is done by Swift compiler, and the latter is done by linker, so SwiftPM passes some special linker flags for each platform.
But SwiftPM assumes that wasm-ld supports it by returning an empty array instead of nil even though wasm-ld doesn't support it yet. ​https://github.com/apple/swift-package-manager/blob/1be68e811d0d814ba7abbb8effee45f1e8e6ec0d/Sources/Build/BuildPlan.swift#L117-L126

Current workaround for this issue is to put an explicit main decl using @_cdecl like this:

// the original code is `print("Hello")`
@_cdecl("main")
func main(argc: Int32, argv: Int32) -> Int32 {
    print("Hello")
    return 0
}

For a short-term solution, we need to disable the renaming in the SwiftPM. But for a long-term solution, this renaming feature should be supported on wasm-ld, so we should send a patch for it into lld.

@MaxDesiatov
Copy link

Don't we run an integration test for this on CI? I'm surprised CI for 5.5 passed while this issue is still reproducible.

@kateinoigakukun
Copy link
Member Author

It seems our integration test only contains building library package. We need to add a test case for executable package also.

@kateinoigakukun
Copy link
Member Author

swiftlang/swift-package-manager#3804 has been merged

@MaxDesiatov
Copy link

It seems our integration test only contains building library package. We need to add a test case for executable package also.

I see an executable target in the integration tests package here though, or am I missing something? https://github.com/swiftwasm/integration-tests/blob/master/Package.swift

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants