Best practice to add JS/WASM support to existing C++ library?

68 views
Skip to first unread message

Chris Hafey

unread,
Mar 31, 2020, 11:14:26 AM3/31/20
to emscripten-discuss
Hi All,

I would like to add a JS/WASM build of an existing C++ library (https://github.com/team-charls/charls) and am looking for best practices on how to do so. Here are some options:

1) Create a new git repository that has the files files specific to the JS/WASM build (e.g. CMakeLists.txt, js warpper, package.json, etc).  In this case, I would checkout the C++ library as a subfolder (or use git submodules) in this new project.  I like this because it is a clean separation of concerns, but requires more work to keep things in sync.
2) Add the JS/WASM files to the existing repository.  This is nice because its all one repository, but makes things more complicated for those that just want the C/C++ version.  In this case, I would create a package.json to the root and create a "js" directory at the root which contains the JS/WASM specific bits (including the resulting JS/WASM code)
3) Combine the above two using a mono-repo pattern.

Any thoughts would be appreciated, thanks!

Chris

Floh

unread,
Mar 31, 2020, 6:39:13 PM3/31/20
to emscripten-discuss
In my cross-platform headers (https://github.com/floooh/sokol) the emscripten code path is selected with a config-define, and the required Javascript code is embedded right in the C source files via emscripten's EM_JS macro (for instance here: https://github.com/floooh/sokol/blob/a662517e772d30b0889d68d83e7ec7cb395d89be/sokol_audio.h#L1294).

That way the emscripten version looks exactly the same to a user of that library as the other platform-specific code paths: just a bunch a C sources which are added to a project as usual, configured with preprocessor defines.

I'm "delegating" the entire build system problem to the library user though. For my projects I'm using cmake with a custom toolchain file for emscripten, but the emscripten SDK also comes with one (https://github.com/emscripten-core/emscripten/blob/master/cmake/Modules/Platform/Emscripten.cmake), so it should be possible to add only minimal emscripten-specific code to the project's standard CMakeLists.txt file (or maybe even none at all).

Cheers,
-Floh.

Chris Hafey

unread,
Apr 16, 2020, 2:33:48 PM4/16/20
to emscripten-discuss
Hi Floh - thank you for your response.  I looked at your project and learned some new things.  I think our use cases are a bit different though - I want to publish a JS/WASM module/library on npm that is built from a C++ library where your project ends up building the entire application.  I tried a few different things and have settled on creating a separate github repository for the JS/WASM build that references the original C++ library via a git submodule.  You can see my prototype/example of this working here:


Any feedback would be appreciated!

J Decker

unread,
Apr 16, 2020, 2:41:50 PM4/16/20
to emscripten-discuss
On Thu, Apr 16, 2020 at 11:33 AM Chris Hafey <cha...@gmail.com> wrote:
Hi Floh - thank you for your response.  I looked at your project and learned some new things.  I think our use cases are a bit different though - I want to publish a JS/WASM module/library on npm that is built from a C++ library where your project ends up building the entire application.  I tried a few different things and have settled on creating a separate github repository for the JS/WASM build that references the original C++ library via a git submodule.  You can see my prototype/example of this working here:


There's like npm install windows-build-tools.  Is there a npm install emsciprten-build-tools or something so you CAN build it?
 

Any feedback would be appreciated!


On Tuesday, March 31, 2020 at 5:39:13 PM UTC-5, Floh wrote:
In my cross-platform headers (https://github.com/floooh/sokol) the emscripten code path is selected with a config-define, and the required Javascript code is embedded right in the C source files via emscripten's EM_JS macro (for instance here: https://github.com/floooh/sokol/blob/a662517e772d30b0889d68d83e7ec7cb395d89be/sokol_audio.h#L1294).

That way the emscripten version looks exactly the same to a user of that library as the other platform-specific code paths: just a bunch a C sources which are added to a project as usual, configured with preprocessor defines.

I'm "delegating" the entire build system problem to the library user though. For my projects I'm using cmake with a custom toolchain file for emscripten, but the emscripten SDK also comes with one (https://github.com/emscripten-core/emscripten/blob/master/cmake/Modules/Platform/Emscripten.cmake), so it should be possible to add only minimal emscripten-specific code to the project's standard CMakeLists.txt file (or maybe even none at all).

Cheers,
-Floh.

On Tuesday, 31 March 2020 17:14:26 UTC+2, Chris Hafey wrote:
Hi All,

I would like to add a JS/WASM build of an existing C++ library (https://github.com/team-charls/charls) and am looking for best practices on how to do so. Here are some options:

1) Create a new git repository that has the files files specific to the JS/WASM build (e.g. CMakeLists.txt, js warpper, package.json, etc).  In this case, I would checkout the C++ library as a subfolder (or use git submodules) in this new project.  I like this because it is a clean separation of concerns, but requires more work to keep things in sync.
2) Add the JS/WASM files to the existing repository.  This is nice because its all one repository, but makes things more complicated for those that just want the C/C++ version.  In this case, I would create a package.json to the root and create a "js" directory at the root which contains the JS/WASM specific bits (including the resulting JS/WASM code)
3) Combine the above two using a mono-repo pattern.

Any thoughts would be appreciated, thanks!

Chris

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/0d6213ba-94cd-4902-9ab2-a7b4d4e996c9%40googlegroups.com.

Chris Hafey

unread,
Apr 16, 2020, 5:17:43 PM4/16/20
to emscripten-discuss
Not that I know of, but there is a docker container with EMSCRIPTEN build tools on it which works great.  I am actually using it with Visual Studio Code Remote Containers in this project:

To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages