-
Notifications
You must be signed in to change notification settings - Fork 3.4k
CMake Toolkit: Shared Library as Side Module #16281
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
base: main
Are you sure you want to change the base?
Conversation
Thanks @ax3l ! I don't know much about CMake, but the flags seem right. However there are test failures here. Let us know if you need help debugging those. |
bad64da
to
a766f89
Compare
Thanks a lot! Let's see if keeping the default in emscripten to "static" already solves this. If you have pointers where we can add a new test for building a shared library, then I would add that to the PR :) |
I cannot see why the tests are failing, is there an easy way to increase verbosity in CI or run them locally? |
Running them locally should be easy, see the docs which are here: https://emscripten.org/docs/getting_started/test-suite.html For example, For more details you can read the test code in |
Related upstream issue: https://gitlab.kitware.com/cmake/cmake/-/issues/23237 |
I see when executing
I am not sure why, is there another flag we should add to compile/link of shared libs? :) I also tried |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! If we can make the default of BUILD_SHARED_LIBS=OFF
work I would support this change.
@@ -6457,7 +6457,7 @@ def line_splitter(data): | |||
Path('codec/CMakeFiles/j2k_to_image.dir/convert.c.o'), | |||
Path('codec/CMakeFiles/j2k_to_image.dir/__/common/color.c.o'), | |||
Path('bin/libopenjpeg.a')], | |||
configure=['cmake', '.'], | |||
configure=['cmake', '.', '-DBUILD_SHARED_LIBS=OFF'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this needed if the default is set of OFF above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder as well. Without it the test_openjpeg
does not seem to build
libopenjpeg/CMakeFiles/openjpeg.dir/build.make:388: bin/libopenjpeg.so.1.4.0
Is the toolchain file even used here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, it's because ./tests/third_party/openjpeg/CMakeLists.txt
lists:
# OpenJPEG build configuration options.
OPTION(BUILD_SHARED_LIBS "Build OpenJPEG shared library and link executables against it." ON)
Before this change, this was force-changed to static (in add_library
) by the global property TARGET_SUPPORTS_SHARED_LIBS FALSE
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see.. I'm ok with the explicit extra -DBUILD_SHARED_LIBS=OFF
here then . And yes the emcmake toolchain is used for all our cmake builds.
set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-s SIDE_MODULE=1") # instead of -shared | ||
set(CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS "-s SIDE_MODULE=1") # instead of -shared | ||
set(CMAKE_STRIP FALSE) # not supported | ||
set(BUILD_SHARED_LIBS OFF) # default to static libs, even if we allow shared ones |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not really needed, default is also static.
set(BUILD_SHARED_LIBS OFF) # default to static libs, even if we allow shared ones |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our goal should be this table.
cmake | user option | library type |
---|---|---|
add_library(foo) |
static | |
add_library(foo) |
-DBUILD_SHARED_LIBS=ON | dynamic |
add_library(foo SHARED) |
dynamic | |
add_library(foo) option(BUILD_SHARED_LIBS "" ON) |
static | |
add_library(foo) option(BUILD_SHARED_LIBS "" ON) |
-DBUILD_SHARED_LIBS=ON | dynamic |
The most important thing will be that option(BUILD_SHARED_LIBS "" ON)
should be overridden to OFF for compatibility unless BUILD_SHARED_LIBS=ON
is explicitly specified.
Just adding configure=['cmake', '.', '-DBUILD_SHARED_LIBS=OFF']
will be sufficient for this and set(BUILD_SHARED_LIBS OFF)
can be deleted.
The other solution will be that caching BUILD_SHARED_LIBS
, this will overwrite option(BUILD_SHARED_LIBS "" ON)
.
set(BUILD_SHARED_LIBS OFF) # default to static libs, even if we allow shared ones | |
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build WebAssembly dynamic libraries. Default value is 'OFF'.") # default to static libs, even if we allow shared ones |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @nokotan,
what you write in this table is very reasonable, but there is a misunderstanding on your side how CMake works.
Yes, add_library
defaults will be based on the BUILD_SHARED_LIBS
default. That one is static now.
No, if a user sets either set(BUILD_SHARED_LIBS ON)
or option(BUILD_SHARED_LIBS "" ON)
this will be overwritten. This is a clear user decision then (in their CMakeLists.txt
) and we cannot overwrite this without disabling shared library support altogether (which was done prior to this PR). In other worlds: the 2nd last row in your table is not possible.
All other rows are exactly with this PR as you also wrote.
I hope this clarifies a bit, please let me know if you have more questions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The question remains, option(BUILD_SHARED_LIBS "" ON)
seems to do nothing when BUILD_SHARED_LIBS
is set by user or is already cached, after CMake 3.13. (CMP0077)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is exactly how all cached variables and options work in CMake.
Yes, user CLI parameters, toolchain files and cache files can be used by users to overwrite the default of an option()
- that is fine and how all CMake options should work. If we would mingle with that behavior, then we would break a lot of package management software and stray from community standards.
Thanks! It's only a few libs in the tests where we explicitly do shared builds that fail now. Before this, the property We need to decide for the failing tests if we declare the exports or keep them to static. |
For |
A rebase should fix that issue I think. |
5904687
to
a90e000
Compare
Thx, rebased :) I see this in my PR for tests:
Can someone please approve me? :) |
Whats the deal with |
The Simply did not work with what it picked for |
Is the llvm-strip is supposed to work and I believe it does, but I'll need to take a look at the specific issue here. |
Oh, it definitely did not pick that one. Let's set it here. Let me try a path. Update: uses now |
21ff633
to
4dc580a
Compare
Enable support to build shared libraries as side modules. This should simplify builds with CMake that currently have to reside to manual link steps.
For now, hoping/assuming the first tool found in `PATH` will be compatible.
dc93eea
to
afb0439
Compare
Hi, I have been using these flags for my project separately but it would also be great if support to build shared libraries as side modules is supported from emscripten's side. Is some movement expected here to maybe finish of the pending work if any ? |
Enable support to build shared libraries as side modules. This should simplify builds with CMake that currently have to reside to manual link steps.
X-ref: