Mujoco WASM Build From Source

From Humanoid Robots Wiki
Revision as of 00:36, 21 July 2024 by Vrtnis (talk | contribs)
Jump to: navigation, search

Suggestions to Build MuJoCo WASM with Release 3.1.6

Note: These instructions relate to a WASM build MuJoCo from the DeepMind MuJoCo source on Github. If you want to build an existing WASM port of MuJoCo 2.3.1 check out MuJoCo_WASM

1. Clone the MuJoCo Repository:

   git clone --branch 3.1.6 https://github.com/deepmind/mujoco.git
   cd mujoco

2. Clone the Emscripten SDK Repository:

   git clone https://github.com/emscripten-core/emsdk.git
   cd emsdk

3. Install and Activate Emscripten:

   ./emsdk install latest
   ./emsdk activate latest
   source ./emsdk_env.sh
   cd ..

Proceed with the next steps once you've made suggested changes to your codebase to allow for a WASM build. You can see the changes here as a reference https://github.com/google-deepmind/mujoco/compare/main...vrtnis:mujoco:release-3.1.6

You probably have to disable several features of 3.1.6 to allow for a successful WASM build.


For example in CMakeLists.txt:


Build Options Modified The options to build examples, simulate library, and tests for MuJoCo have been turned off by default (changed from ON to OFF).

Emscripten Setup Added New configurations and target properties for building MuJoCo with Emscripten have been added. This includes defining source files, checking their existence, and setting specific properties and options for building WebAssembly (.wasm) and HTML output.

Target Library Links Updated Some target link options have been adjusted, including removing the lodepng library from the target link list.

Also, for example in mjxmacro.h you have to involve adding explicit casting to size_t for the calculations of key_mpos and key_mquat array sizes, ensuring correct memory allocation and preventing potential integer overflow issues.

4. Prepare the Build Environment:

   mkdir build
   cd build

5. Run Emscripten CMake Commands:

   emcmake cmake ..
   emmake make

6. Deploy and Run Locally:

   emrun --no_browser --port 8080 .

7. Optional Cleanup and Repeat Steps if Necessary:

   rm -rf *
   emcmake cmake ..
   emmake make

8. Install the Build:

   cmake --install .
   sudo cmake --install .


Notes

  • Ensure that the Emscripten environment is correctly activated before starting the build process.
  • Regularly clean the build directory to maintain a clean build environment.