-
-
Notifications
You must be signed in to change notification settings - Fork 4
Targeting WASM
The framework supports targeting WASM as a platform. This guide will help you set up everything to be able to easily deploy to WASM.
To compile to WASM, you will need to install Emscripten, which is a toolchain for building C/C++ applications that can run on the web.
You need to manually install Emscripten, using the following commands:
user $ git clone https://github.com/emscripten-core/emsdk.git
user $ cd emsdk
user $ git pull
user $ ./emsdk install latest
user $ ./emsdk activate latest
user $ source ./emsdk_env.sh
On Windows, you need to have Python 3 installed. After that, run the following commands:
> git clone https://github.com/emscripten-core/emsdk.git
> cd emsdk
> git pull
> emsdk.bat install latest
> emsdk.bat activate latest
> emsdk_env.bat
On macOS, you can install Emscripten through homebrew:
user $ brew install emscripten
Once you have everything installed, run emcc --version
to check if everything is set up correctly. If everything is fine, you can try compiling a project.
To compile a project, do the following:
- Create a new folder for building under the root project directory, for example:
wasm-build
- Enter the folder
- Run
emcmake cmake ..
- Compile using the provided generated build system. For example, with
emmake
:emmake make -j <number of compile jobs>
- Serve the compiled files:
emrun <project name>.html
- Open the served URL in your browser
If built successfully, you should be presented with a page, similar to this:
![](https://private-user-images.githubusercontent.com/40400590/343984957-98800442-f68e-447d-902c-b7e91063de72.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjIxMTQ2MjIsIm5iZiI6MTcyMjExNDMyMiwicGF0aCI6Ii80MDQwMDU5MC8zNDM5ODQ5NTctOTg4MDA0NDItZjY4ZS00NDdkLTkwMmMtYjdlOTEwNjNkZTcyLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MjclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzI3VDIxMDUyMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTYxMDVmODdlMDFhNzY0ZDE3YmRjOWNiZmY3NmU5NGQxOTQ0OTQwNWQzZDVkMzQzMmY3MTQ1NzA3ODI0NGI3ZmUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.SniM4U3SP3K2eBlVb2UcnymtGwPUcXDDrsCaWP8tmq4)
When shipping an application for production, you might want to provide a custom HTML file. To do that, you can use the following base template code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<canvas id="canvas" oncontextmenu="event.preventDefault()"></canvas>
<script type='text/javascript'>
var Module = {
canvas: (function() { return document.getElementById('canvas'); })()
};
</script>
<script src="<project name>.js"></script>
</body>
</html>
Make sure to replace <project name>.js
with your own project's Javascript file.
After that, you can disable the USE_HTML_GENERATION
option in your Config/cmake/<project name>.cmake
file, like this:
set(USE_HTML_GENERATION OFF)
To enabled pre- and post-javascript, simply enable the following options in your Config/cmake/<your project>.cmake
file, like this:
set(ENABLE_PRE_SCRIPT ON)
set(ENABLE_POST_SCRIPT ON)
The 2 Javascript files in question can be found under the Config/WASM/
folder.
More information on when pre- and post-javascript files are called can be found here.
This project is supported by all the people who joined our discord server and became beta testers. If you want to join the discord you can click here.
- Home
- Beginner content
- Advanced content
- Loading dynamic libraries at runtime
- Understanding the library layout
- Compilation mode modifiers
- Adding plugin support to your application
- Production export and deployment
- Targeting WASM
- Developer and contributor resources
- Misc