Skip to content
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

compile API to WASM #1073

Open
fr-an-k opened this issue Jan 16, 2025 · 4 comments
Open

compile API to WASM #1073

fr-an-k opened this issue Jan 16, 2025 · 4 comments

Comments

@fr-an-k
Copy link
Contributor

fr-an-k commented Jan 16, 2025

This compiles the library (libredwg.a) but a.wasm is 1kb.
How do I get a libredwg.wasm that contains the actual exports?

./autogen.sh
mkdir build
cd build
emconfigure ../configure --disable-bindings --disable-shared
make -j -s

Related to #876

@fr-an-k
Copy link
Contributor Author

fr-an-k commented Jan 16, 2025

I think it should work like this, but it doesn't:
(dwg.h)

#include <emscripten/emscripten.h>

... (EXPORT is defined here)

// Redefine it to also export all function to emscripten
#define EXPORT EXPORT EMSCRIPTEN_KEEPALIVE
...

@fr-an-k
Copy link
Contributor Author

fr-an-k commented Feb 11, 2025

starting with a clean clone and emscripten installation, I commented out m4_include([m4/ax_printf_size_t.m4]) in aclocal.m4 and ran sh ./autogen.sh but I still get this when running emconfigure ../configure --disable-bindings --disable-shared:

checking for printf() modifier to use with size_t... unknown
configure: error: cannot find a suitable modifier

@rurban
Copy link
Contributor

rurban commented Feb 11, 2025

As I said: #892 (comment)

@fr-an-k
Copy link
Contributor Author

fr-an-k commented Mar 6, 2025

I finally managed running LibreDWG in the browser.
This issue should IMO only be closed when emscripten/WASM is added to the automatic builds, suitable for the browser and Node.js but I don't have time for that yet.

FYI anyone needing this:

The error above was probably caused by a wrong CC environment variable or installation or something.

mkdir .build-wasm
mkdir -p bindings/js
cd bindings/js
npm i -g yarn
yarn create vite
./autogen.sh
cd .build-wasm
emconfigure ../configure --disable-bindings --disable-shared
emmake make
emcc -gsource-map --bind -s LINKABLE=1 -s EXPORTED_FUNCTIONS='["_dwg_convert_read","_dwg_convert_write","_dwg_convert_free","_malloc","_free"]' -s EXPORTED_RUNTIME_METHODS='["cwrap", "ccall","stringToUTF8","UTF8ToString"]' -o ../bindings/js/src/libredwg.js -O2 -sMODULARIZE=1 -s EXPORT_ES6=1 -s ENVIRONMENT=web -Isrc -I../include src/*.o programs/dwgconvert.o -s ALLOW_MEMORY_GROWTH=1 -s INITIAL_MEMORY=3GB --no-entry

I wrote a separate JSON conversion program using dwg_decode/dwg_decode directly (as well as dwg_fixup_BLOCKS_entities for DWG downgrades), since dependency on filenames as per the original API is unnecessary and undesirable in my opinion, although emscripten does provide a limited virtual fs. It's possible to call those functions directly from JS, without create a separate library/program.

I used Node.js and emscripten from the emsdk repository and installed a bunch of other stuff, but I'm not sure what's necessary.

The source map doesn't work but I can see file names and line numbers when WASM crashes.

I probably won't have time for a long time to create an automatic WASM build, javascript bindings or NPM library.

I need to figure out how to set SIDE_MODULE=2/MAIN_MODULE=2 to avoid all functions being exposed, but the LibreDWG API is in there and could be used (I prefer working with JSON however).

Currently there is a bug in Chrome 133 for debug symbols, so you need to use 134 beta or another browser. You probably also need the DWARF browser extension.

It seems there is a bug in emscripten occurring within calloc within hash_new (hash.c); it's unclear what could be causing this but the source code (dlmalloc function) is available here: https://github.com/emscripten-core/emscripten/blob/main/system/lib/dlmalloc.c.
Setting initial memory to 3GB circumvents the issue in my case, but it depends on what you do.

-O2 or -O3 also seems necessary to avoid an error of too many local variables; there might be a big buffer in the stack somewhere.

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

2 participants