Skip to content

[build][cmake] Added emscripten build options#5180

Merged
raysan5 merged 2 commits intoraysan5:masterfrom
brccabral:emscripten_options
Sep 12, 2025
Merged

[build][cmake] Added emscripten build options#5180
raysan5 merged 2 commits intoraysan5:masterfrom
brccabral:emscripten_options

Conversation

@brccabral
Copy link
Contributor

Missing options for compiling as Web.

@raysan5 raysan5 changed the title Emscripten options [build][cmake] Added emscripten build options Sep 12, 2025
@raysan5 raysan5 merged commit 71321d1 into raysan5:master Sep 12, 2025
16 checks passed
@raysan5
Copy link
Owner

raysan5 commented Sep 12, 2025

@brccabral Thanks for the review and clarifications!

@Peter0x44
Copy link
Contributor

Are you sure this is a good idea? -sASYNCIFY is not needed if you use emscripten_set_main_loop and apparently makes perf worse.

@Peter0x44
Copy link
Contributor

IMO this should be reverted.

@raysan5
Copy link
Owner

raysan5 commented Sep 12, 2025

@Peter0x44 thanks for the review and the comments! As per mi understanding, this CMake is intended only for raylib examples collection, that requires -sASYNCIFY... or I missunderstood it? @brccabral please, can you confirm?

And yeah, -sASYNCIFY affects performance, it must not be used if using emscripten_set_main_loop().

@Peter0x44
Copy link
Contributor

Peter0x44 commented Sep 12, 2025

If I'm reading this correctly, it will make sure any time you link with raylib through cmake, -sASYNCIFY gets passed. When you might not want it.
It's not specific to the examples.

@Peter0x44
Copy link
Contributor

Peter0x44 commented Sep 12, 2025

If the intention is that it only passes this to the examples, then IMO, it should be the examples CMakeLists.txt providing the link options, not raylib.

@brccabral
Copy link
Contributor Author

Well, the option -sUSE_GLFW=3 was already there, I saw the others in the examples/build_example_web.bat and thought this was the way Raylib uses Emscripten.
I am fine with having link options in the examples (SDL does it).
It would be nice having a specific example, but I am not sure how emscripten_set_main_loop() works.
SDL examples sets only -sALLOW_MEMORY_GROWTH=1, no -sASYNCIFY ...

@Peter0x44
Copy link
Contributor

-sUSE_GLFW=3 is fine because it is required 100% of the time. The other two flags are not, and ASYNCIFY has negative consequences!

It's unacceptable to require those flags for everyone consuming raylib through cmake.

core_basic_window_web.c demonstrates how to use emscripten_set_main_loop.
I'll submit a PR reverting this tomorrow if no one else gets to it first.

@brccabral
Copy link
Contributor Author

This line is in examples/CMakeLists.txt

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s WASM=1 -s ASYNCIFY -s ALLOW_MEMORY_GROWTH=1 --shell-file ${CMAKE_SOURCE_DIR}/src/shell.html")

@brccabral
Copy link
Contributor Author

Where is core_basic_window_web.c? https://github.com/raysan5/raylib/tree/master/examples/core ?

@Peter0x44
Copy link
Contributor

Ray deleted it in this commit.
c17f82a

@brccabral
Copy link
Contributor Author

brccabral commented Sep 13, 2025

Found something, the browser error was caused by emscripten_sleep(12); in src/platforms/rcore_web.c.
There is no Javascript error if I comment this line.
Edit: after reading the comment, looks like WindowShouldClose() should not be used on web...

bool WindowShouldClose(void)
{
    // Emscripten Asyncify is required to run synchronous code in asynchronous JS
    // REF: https://emscripten.org/docs/porting/asyncify.html

    // WindowShouldClose() is not called on a web-ready raylib application if using emscripten_set_main_loop()
    // and encapsulating one frame execution on a UpdateDrawFrame() function,
    // allowing the browser to manage execution asynchronously

    // Optionally we can manage the time we give-control-back-to-browser if required,
    // but it seems below line could generate stuttering on some browsers
    emscripten_sleep(12);

    return false;
}

@brccabral
Copy link
Contributor Author

This file contains a PLATFORM_WEB example projects/CMake/core_basic_window.c, but its projects/CMake/CMakeLists.txt uses -s ASYNCIFY

@raysan5
Copy link
Owner

raysan5 commented Sep 13, 2025

Ray deleted it in this commit. c17f82a

Yeah, I'm reorganizing examples at the moment and I just deleted it temporally to place it somewhere else. It was just a few days ago, sorry for the confusion.

@brccabral When building for web, it's recommended to use the emscripten_set_main_loop() mechanism provided by emscriptem but that's not always possible so ASYNCIFY alternative is available, with some performance hit. Examples structure is common for all platforms so using that ASYNCIFY approach but users creating code from scratch with web in mind (specially if it is a big project) should use the emscripten_set_main_loop() approach.

When using ASYNCIFY, emscripten must be notified when it has time to manage the loop, so emscripten_sleep() is required... but never when using emscripten_set_main_loop().

psxdev pushed a commit to raylib4Consoles/raylib that referenced this pull request Sep 19, 2025
* [cmake] export automatically raylib definitions and compile/link options

* [cmake] pass emscripten options to consumer project
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

Successfully merging this pull request may close these issues.

3 participants