emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

--embind-emit-tsd / --emit-tsd do not work with dynamic linkage

Open slade87 opened this issue 1 year ago • 2 comments

In case a WASM binary is linked dynamically the option to emit typescript interface does not work. The main cause seems to be that the generated wasm and js file are copied to a temp directory during running emitting the typescript interfaces while all of the possible dependencies are not.

As a workaround I changed link.py #https://github.com/emscripten-core/emscripten/blob/63c648fa17be49fa9640d4a40c8ba2f7a9b2d444/tools/link.py#L1963 to the following content:

outfile_wasm = in_temp('tsgen_a.out.wasm')
import pathlib, glob, shutil
root_dir =  str(pathlib.Path(wasm_target).parent.resolve())
out_temp = str(pathlib.Path(outfile_js).parent.resolve())
for file in glob.glob(root_dir + r'/*.so*'):
    logger.info('Copying: ' + file)
    shutil.copy(src=file, dst=out_temp)"

The example workaround above simply copies all .so files existent in the same location as the wasm/js file to the same location in the temp directory. With the workaround the dynamic lib is properly loaded and the typescript interfaces are emitted. There is most likely a much more sophisticated solution to that problem.

Version of emscripten/emsdk: 3.1.56

Failing command line in full: The issue occurs during linking when option --emit-tsd or --embind-emit-tsd is performed

Full link command and output with -v appended: As soon as the generated WASM binary contains the following dynamic link section: dylink.0 .so NAMEOFLIB.so the linking step fails within: function run_embind_gen (https://github.com/emscripten-core/emscripten/blob/63c648fa17be49fa9640d4a40c8ba2f7a9b2d444/tools/link.py#L1924C5-L1924C19) due to node execution failure while trying to load any dynamically linked dependencies in: out = shared.run_js_tool(outfile_js, [], node_args, stdout=PIPE)

Possible solution During linkage all dependencies are already propagated to emscripten for the actual linking step, ensure these dependencies are not only put into the dynlink.0 section but also properly copied/symlinked from their origin to the temporary folder used for generating tsd utilizing node when using --embind-emit-tsd / --emit-tsd.

slade87 avatar Apr 30 '24 12:04 slade87

I'm not sure just bundling the .so files that happen to live next the output binary is the most correct thing to do here.

Are all of your .so files listed on the command line when builting your main module? If so we can maybe just copy them based on the command line.

sbc100 avatar Apr 30 '24 14:04 sbc100

Yes they are passed to link step, so it should be possible to copy them from the command line arguments

slade87 avatar Apr 30 '24 15:04 slade87