Description
From @SteveSandersonMS link
I've been trying out the pinvoke generation and found it can't yet generate real WebAssembly imports. There are two main reasons:
It ignores pinvokes unless they correspond to an entry on _WasmNativeFileForLinking (even though when you want to generate an import, you won't be linking with any module for it)
When generating pinvoke-table.h, it never adds any import_module/import_name clang attributes, which means:
The module is always compiled as env (so it's impossible to reference most real-world libraries)
The symbol name is mangled
I was able to work around it via some MSBuild hackery, so can be fairly confident what would be the steps to solve this:
Change the pinvoke generator so that when a DllImport's module name does not correspond to an entry in _WasmNativeFileForLinking, it does still generate the pinvoke-table.h entry
... but those pinvoke-table.h entries must also be annotated with import_module/import_name
For example, given these two DllImports:
[LibraryImport("libSystem.Native")]
private static partial int Method1(int a);
[LibraryImport("mylib", EntryPoint = "this:should:not:be:mangled")]
private static partial int Method2(int a);
... the pinvoke-table.h definition should look like:
int Method1 (int);
attribute((import_module("mylib"), import_name("this:should:not:be:mangled"))) int Method2 (int);
The first one does not need any attribute because libSystem.Native does match up with one of the linker inputs (i.e., _WasmNativeFileForLinking entries). The existing codegen is correct.
The second one does need the clang import annotations because it doesn't match a _WasmNativeFileForLinking entry, so it has to become a real wasm import, and it's important to retain the real module name (mylib) and the real import name (this:should:not:be:mangled) even though it's not a legal C function name.
From @lewing
The goal of this issue is making sure that once the build completes the wasm module is able to have imports of <module>.<method>
with unmangled names for missing symbols, not to solve wasi component linking more generally.