swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.18k stars 10.32k forks source link

@_cdecl doesn’t work in emdedded dependency #74328

Open MihaelIsaev opened 2 months ago

MihaelIsaev commented 2 months ago

Description

Bug in Embedded Swift with @_cdecl inside a dependency package.

Toolchain: swift-wasm-DEVELOPMENT-SNAPSHOT-2024-06-09-a Build command: swift build -c release --triple wasm32-unknown-none-wasm

Compilation error

Building for production...
error: link command failed with exit code 1 (use -v to see invocation)
clang: warning: argument unused during compilation: '-F/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks' [-Wunused-command-line-argument]
wasm-ld: error: .build/wasm32-unknown-none-wasm/release/_CLib.build/_CLib.c.o: undefined symbol: _swiftMethod
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[11/12] Linking App.wasm

Reproduction

// Sources/App/App.swift
import Lib1

@_expose(wasm, "main")
func main() {
    __hello()
}

@_extern(c)
func __hello()

Lib1 package code

// Sources/_CLib/_CLib.c
extern void _swiftMethod();

void __hello() {
    _swiftMethod();
}

// Sources/Lib1/Lib1.swift
import _CLib

@_cdecl("_swiftMethod")
public func _swiftMethod() {}

Expected behavior

@_cdecl("_swiftMethod") should be taken by the compiler from the Lib1

Environment

Apple Swift version 6.0-dev (LLVM 579155491d559cc, Swift 64869e5a42221b2) Target: arm64-apple-macosx14.0

Additional information

CDeclDependencyBug.zip – project that reproduces the issue

MihaelIsaev commented 2 months ago

@kubamracek thank you for taking on this issue.

If you can't reproduce the problem with the provided project, please check this repository where the issue can be easily replicated.

In the LightWebApp remove the code following the section in Sources/LightWebApp/LightWebApp.swift:

// The following code is a hack to make JavaScriptKit compile
// since in the current `swift-wasm-DEVELOPMENT-SNAPSHOT-2024-06-13`
// snapshot _cdecl won't be taken from a dependency

This should lead to the following compiler error:

Building for production...
error: link command failed with exit code 1 (use -v to see invocation)
clang: warning: argument unused during compilation: '-F/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks' [-Wunused-command-line-argument]
wasm-ld: error: .build/wasm32-unknown-none-wasm/release/_CJavaScriptKitEmbedded.build/_CJavaScriptKit.c.o: undefined symbol: _call_host_function_impl
wasm-ld: error: .build/wasm32-unknown-none-wasm/release/_CJavaScriptKitEmbedded.build/_CJavaScriptKit.c.o: undefined symbol: _free_host_function_impl
wasm-ld: error: .build/wasm32-unknown-none-wasm/release/_CJavaScriptKitEmbedded.build/_CJavaScriptKit.c.o: undefined symbol: _library_features
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[6/7] Linking LightWebApp.wasm