-
Notifications
You must be signed in to change notification settings - Fork 2
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
WCLAP with Emscripten #1
base: main
Are you sure you want to change the base?
Conversation
if(APPLE) | ||
if(EMSCRIPTEN) | ||
set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-O3 -ffast-math -fno-exceptions -fno-rtti") | ||
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-O3 -ffast-math -fno-exceptions -fno-rtti -sSTANDALONE_WASM --no-entry -sEXPORTED_FUNCTIONS=_clap_entry,_malloc -s INITIAL_MEMORY=512kb -sALLOW_MEMORY_GROWTH -sALLOW_TABLE_GROWTH -sPURE_WASI --export-table") |
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.
So build type release and debug don’t do this in emscripten?
winder if we want an wclap.cmake with utilities?
I’m happy to merge this if you want tho
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.
Maybe they'd do -O3
. The others (-ffast-math -fno-exceptions -fno-rtti
) are my personal defaults, and should probably be removed to avoid confusion.
I previously couldn't get Release builds to work properly in a non-Emscripten project, but that's more about me being bad at CMake. 😅 If you want to refactor/rewrite this into something neater, that'd be fine by me!
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.
To explain the other linker options:
-sSTANDALONE_WASM
: this can probably be omitted - it's the default when generating a.wasm
anyway (with no JS loader), and is also implied by-sPURE_WASI
.--no-entry
: don't expect amain()
-sEXPORTED_FUNCTIONS=_clap_entry,_malloc
: not sure what the_
prefix is about. We obviously needclap_entry
(which is a pointer rather than a function, but that's just this flag being badly named). We also needmalloc
because the in order to pass in pointers to host structs, those structs need to be inside the memory WASM can see. This doesn't have to be the realmalloc()
though - just any function that the host can use to get some memory it knows the plugin isn't using for anything else.-s INITIAL_MEMORY=512kb -sALLOW_MEMORY_GROWTH
: The default is 16Mb with no memory growth. This is neater and feels more flexible (to me), but optional.-sALLOW_TABLE_GROWTH --export-table
: WASM functions aren't addressed by memory locations, but by indices in a "function table". Function pointers are integer indices into this table. In order for our host-owned structs to point to host-defined functions, the WASM module needs to (1) export this function table so it's visible, and (2) allow it to grow, so we can expand it and insert our host functions in there.-sPURE_WASI
: This disables the last piece of (fairly pointless) Emscripten magic. Without this, Emscripten adds an extra import function (emscripten_notify_memory_growth
) which gets called when the memory expands.
This is an awkward solution.
The main CLAP builds a dynamic library, but Emscripten views
.wasm
s as self-contained executables.