Skip to content

SHARED / dynamic library with CMake #15276

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
DerThorsten opened this issue Oct 11, 2021 · 11 comments
Closed

SHARED / dynamic library with CMake #15276

DerThorsten opened this issue Oct 11, 2021 · 11 comments

Comments

@DerThorsten
Copy link

DerThorsten commented Oct 11, 2021

Hi,
I have a question wrt shared / dynamic linking.
While I can successful generated a shared library with emscripten when I use no build system with smth like

em++ -c -fpic package_a/a.cpp -s SIDE_MODULE=1 -s EXPORT_ALL=1  -s LLD_REPORT_UNDEFINED -Ipackage_a -shared -o liba.wasm

I cannot make cmake generate a shared / dynamic library.
I try to pass the required SIDE_MODULE flag via target_compile_options / target_link_options but I still get the error message that the the target platform does not support dynamic linking.

    add_library(${name} SHARED ${ALL_FILES})
    SET(EMS_OPTS "-s SIDE_MODULE=1 -shared -s EXPORT_ALL=1 ")
    target_compile_options(${name}  PUBLIC ${EMS_OPTS})
    target_link_options(${name}     PUBLIC ${EMS_OPTS})
  ADD_LIBRARY called with SHARED option but the target platform does not
  support dynamic linking.  Building a STATIC library instead.  This may lead
  to problems.

I know that static libraries are recommended but I try to compile LLVM / clang / cling itself for emscripten and I need support for dlopen.
When I compile everything with static linking I do not get support for dlopen and get error messages when I use the generated code.

Is there any minimal example how to generate shared libs from cmake?

@DerThorsten DerThorsten changed the title SHARED library with Cmake SHARED library with CMake Oct 11, 2021
@DerThorsten DerThorsten changed the title SHARED library with CMake SHARED / dynamic library with CMake Oct 11, 2021
@DerThorsten
Copy link
Author

the error I see when I compile everything as static as possible is:


RuntimeError: abort(RuntimeError: abort(To use dlopen, you need to use Emscripten's linking support, see https://github.com/emscripten-core/emscripten/wiki/Linking).

@DerThorsten
Copy link
Author

besides this issue cling builds like a charm.
The overall goal here is to make this interactive c++ jupyter kernel run in jupyterlite
This might be the last tiny issue to get this running in the browser

@kripken
Copy link
Member

kripken commented Oct 11, 2021

You can build with something like cmake -G Ninja and then ninja -v to get logging of the commands emitted. Likely it is not passing the link flag somehow. CMake can be pretty confusing there...

For a working example of passing such flags, binaryen's CMakeLists.txt might be helpful. Here is a flag:

https://github.com/WebAssembly/binaryen/blob/072c60c9314ca37f0f4a9393bb81dabd1be5a6b5/CMakeLists.txt#L272

And here is the helper function:

https://github.com/WebAssembly/binaryen/blob/072c60c9314ca37f0f4a9393bb81dabd1be5a6b5/CMakeLists.txt#L71-L76

@DerThorsten
Copy link
Author

thank you very much, it seems to work indeed when I pass the flags as shown above.
It gets a bit trickier in this way to have different link flags for different targets (ie SIDE_MODULE vs MAIN_MODULE) but I can live with that.
Thanks for the fast answer and this awesome project!

@sbc100
Copy link
Collaborator

sbc100 commented Oct 13, 2021

I'm pretty sure you can configure llvm to avoid depending on dlopen. It doesn't seem like it should be an essentail part of clang or llvm. You should be able build a fully statistically linked version.

@DerThorsten
Copy link
Author

DerThorsten commented Oct 14, 2021

@sbc100 It would be awesome to get ride of the dlopen.
I tried to set all the related things here to 0 https://github.com/llvm-mirror/llvm/blob/2c4ca6832fa6b306ee6a7010bfb80a3f2596f824/include/llvm/Config/config.h.cmake#L50
and I even removed all intannces of dlopen etc from the codebase.
Btw my overall goal is to get cling running.
Not yet 100 % if the problem comes from clang or cling.

If you have any hints, I would be super thankfull.
Having cling in the browser would be an awesome feature for https://jupyterlite.readthedocs.io/en/latest/ (ie a in browser version of jupyter / https://github.com/jupyter-xeus/xeus-cling )

@sbc100
Copy link
Collaborator

sbc100 commented Oct 14, 2021

One thing you can try is adding -Wl,--trace-symbol=dlopen. This will tell you why the dlopen symbol is getting included in the final link.

Another approach would be to remove these stub functions:

_dlopen_js: function(filename, flag) {
abort("To use dlopen, you need to use Emscripten's linking support, see https://github.com/emscripten-core/emscripten/wiki/Linking");
},
_emscripten_dlopen_js: function(filename, flags, user_data, onsuccess, onerror) {
abort("To use dlopen, you need to use Emscripten's linking support, see https://github.com/emscripten-core/emscripten/wiki/Linking");
},
_dlsym_js: function(handle, symbol) {
abort("To use dlopen, you need to use Emscripten's linking support, see https://github.com/emscripten-core/emscripten/wiki/Linking");
},

That way you will get a link error instead of a compile error when attempting to include dlopen.

sbc100 added a commit that referenced this issue Oct 14, 2021
Only if ALLOW_UNIMPLEMENTED_SYSCALLS (which is also set by STRICT)
is used.

This was previously only ever a runtime error.

Should help with #15276 (comment)
@DerThorsten
Copy link
Author

will give this a try as soon as I can, thanks for the hints! very much appreciated!

sbc100 added a commit that referenced this issue Oct 14, 2021
Only if ALLOW_UNIMPLEMENTED_SYSCALLS (which is also set by STRICT)
is used.

This was previously only ever a runtime error.

Should help with #15276 (comment)
@hoodmane
Copy link
Collaborator

I am running into the same problem with Pyodide trying to build geos. We really do want to dynamically link geos, static linking is not an option. How do we get CMake to work? Should I open a separate issue?

@hoodmane
Copy link
Collaborator

I tried adding set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE) to the beginning of CMakeLists but this had no effect.

sbc100 added a commit that referenced this issue Oct 20, 2021
Only if ALLOW_UNIMPLEMENTED_SYSCALLS (which is also set by STRICT)
is used.

This was previously only ever a runtime error.

Should help with #15276 (comment)
@ax3l
Copy link

ax3l commented Feb 14, 2022

I think I found a way how we can overwrite this manually:
https://cmake.org/pipermail/cmake/2018-December/068771.html

# CLI
... -DCMAKE_PROJECT_INCLUDE=overwriteProp.cmake
# overwriteProp.cmake
set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)
set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-s SIDE_MODULE=1")
set(CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS "-s SIDE_MODULE=1")
set(CMAKE_STRIP FALSE)  # used by default in pybind11 on .so modules

Used in: pyodide/pyodide#2169

sbc100 added a commit that referenced this issue Mar 29, 2022
Only if ALLOW_UNIMPLEMENTED_SYSCALLS (which is also set by STRICT)
is used.

This was previously only ever a runtime error.

Should help with #15276 (comment)
sbc100 added a commit that referenced this issue Mar 29, 2022
Only if ALLOW_UNIMPLEMENTED_SYSCALLS (which is also set by STRICT)
is used.

This was previously only ever a runtime error.

Should help with #15276 (comment)
sbc100 added a commit that referenced this issue Mar 29, 2022
…15293)

Only if ALLOW_UNIMPLEMENTED_SYSCALLS (which is also set by STRICT)
is used.  This was previously only ever a runtime error.

Should help with #15276 (comment)
ytnuf added a commit to ytnuf/gdextension_example_bullet_shower that referenced this issue Jan 1, 2025
luadebug added a commit to luadebug/xmake-repo that referenced this issue Jan 15, 2025
star-hengxing added a commit to xmake-io/xmake-repo that referenced this issue Jan 20, 2025
* init

* try to help WASM emscripten-core/emscripten#15276

* retry

* fixup

* fixup

* testing wchar_t

* testing no exceptions

* testing exports

* test for exports

* test header only option, cpp file should be in include folder as well

* Update xmake.lua

* Update xmake.lua

* Make wchar turned off by default

---------

Co-authored-by: star9029 <[email protected]>
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

5 participants