Difference between revisions of "Mujoco WASM Build From Source"

From Humanoid Robots Wiki
Jump to: navigation, search
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
==== Suggestions to Build MuJoCo WASM with Release 3.1.6 ====
 
==== 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]]
+
Note: These instructions relate to a WASM build MuJoCo from the DeepMind MuJoCo source on Github.  If you want to build using an existing WASM port of MuJoCo 2.3.1 check out [[MuJoCo_WASM]]
  
 
1. '''Clone the MuJoCo Repository:'''
 
1. '''Clone the MuJoCo Repository:'''
Line 25: Line 25:
 
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/3.1.6...vrtnis:mujoco:release-3.1.6
 
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/3.1.6...vrtnis:mujoco:release-3.1.6
  
You could also run git show 5c4b86b b35d837 85f7539 6f173ea 4142aa5 7e2fb2f b0e4341 a4b173e ad49419 21e4e6c c18e6f5 836bbd9 534e43e 0b75f5f > changes.txt
+
You could also run git show to see specific changes that have been made to the original mujoco 3.1.6. files e.g. ,5c4b86b > changes.txt
  
As of this writing, you'd need have to disable several features of 3.1.6 to allow for a successful WASM build.
+
 
 +
==== Specific ideas for changes ====
 +
As of this writing, you'd need to disable several features of 3.1.6 to allow for a successful WASM build.
  
 
For example in CMakeLists.txt, the options to build examples, simulate library, and tests for MuJoCo have been turned off by default (changed from ON to OFF). 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. Some target link options have been adjusted, including removing the lodepng library from the target link list.
 
For example in CMakeLists.txt, the options to build examples, simulate library, and tests for MuJoCo have been turned off by default (changed from ON to OFF). 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. 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.
+
* `src/user/user_objects.cc`, comment out the line that includes `lodepng.h`. Replace the bodies of the `mjCHField::LoadPNG` and `mjCTexture::LoadPNG` functions with a single return statement.
 +
*
 +
* `src/engine/engine_util_errmem.c`, update the preprocessor condition in the `mju_writeLog` function by replacing `__STDC_VERSION_TIME_H__` with `__EMSCRIPTEN__` in the `#if` directive. The line should now include `__EMSCRIPTEN__` in the condition.
 +
*
 +
* `src/engine/engine_crossplatform.h`, add a conditional block specifically for Emscripten within the preprocessor directive. Inside this block, include the `sort_r.h` header and define the `mjQUICKSORT` and `quicksortfunc` macros appropriately. This will separate the handling for Apple, Emscripten, and other platforms.
 +
*
 +
* `cmake/MujocoOptions.cmake` file, remove the `-Wno-int-in-bool-context` compiler warning flag from the list of warnings.
 +
 
 +
In the `CMakeLists.txt` file, make the following changes:
 +
* Change the default values of the options to disable the building of examples, simulate library, tests, and Python utility libraries by setting them to OFF:
 +
  * Set `MUJOCO_BUILD_EXAMPLES` to OFF.
 +
  * Set `MUJOCO_BUILD_SIMULATE` to OFF.
 +
  * Set `MUJOCO_BUILD_TESTS` to OFF.
 +
  * Set `MUJOCO_TEST_PYTHON_UTIL` to OFF.
 +
* Remove the `lodepng` library from the `target_link_libraries` list for the `mujoco` target.
 +
 
 +
Also, for example, in `mjxmacro.h`, add 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.
 +
 
  
 
We'd suggest taking a look at https://github.com/stillonearth/MuJoCo-WASM/issues/1 (older 2.3.1 build but still relevant)
 
We'd suggest taking a look at https://github.com/stillonearth/MuJoCo-WASM/issues/1 (older 2.3.1 build but still relevant)
Line 67: Line 86:
 
   emmake make
 
   emmake make
 
   </syntaxhighlight>
 
   </syntaxhighlight>
 
 
  
 
=== Notes ===
 
=== Notes ===
 
* Ensure that the Emscripten environment is correctly activated before starting the build process.
 
* Ensure that the Emscripten environment is correctly activated before starting the build process.
 
* Regularly clean the build directory to maintain a clean build environment.
 
* Regularly clean the build directory to maintain a clean build environment.

Latest revision as of 22:29, 22 July 2024

Suggestions to Build MuJoCo WASM with Release 3.1.6[edit]

Note: These instructions relate to a WASM build MuJoCo from the DeepMind MuJoCo source on Github. If you want to build using 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/3.1.6...vrtnis:mujoco:release-3.1.6

You could also run git show to see specific changes that have been made to the original mujoco 3.1.6. files e.g. ,5c4b86b > changes.txt


Specific ideas for changes[edit]

As of this writing, you'd need to disable several features of 3.1.6 to allow for a successful WASM build.

For example in CMakeLists.txt, the options to build examples, simulate library, and tests for MuJoCo have been turned off by default (changed from ON to OFF). 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. Some target link options have been adjusted, including removing the lodepng library from the target link list.

  • `src/user/user_objects.cc`, comment out the line that includes `lodepng.h`. Replace the bodies of the `mjCHField::LoadPNG` and `mjCTexture::LoadPNG` functions with a single return statement.
  • `src/engine/engine_util_errmem.c`, update the preprocessor condition in the `mju_writeLog` function by replacing `__STDC_VERSION_TIME_H__` with `__EMSCRIPTEN__` in the `#if` directive. The line should now include `__EMSCRIPTEN__` in the condition.
  • `src/engine/engine_crossplatform.h`, add a conditional block specifically for Emscripten within the preprocessor directive. Inside this block, include the `sort_r.h` header and define the `mjQUICKSORT` and `quicksortfunc` macros appropriately. This will separate the handling for Apple, Emscripten, and other platforms.
  • `cmake/MujocoOptions.cmake` file, remove the `-Wno-int-in-bool-context` compiler warning flag from the list of warnings.

In the `CMakeLists.txt` file, make the following changes:

  • Change the default values of the options to disable the building of examples, simulate library, tests, and Python utility libraries by setting them to OFF:
 * Set `MUJOCO_BUILD_EXAMPLES` to OFF.
 * Set `MUJOCO_BUILD_SIMULATE` to OFF.
 * Set `MUJOCO_BUILD_TESTS` to OFF.
 * Set `MUJOCO_TEST_PYTHON_UTIL` to OFF.
  • Remove the `lodepng` library from the `target_link_libraries` list for the `mujoco` target.

Also, for example, in `mjxmacro.h`, add 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.


We'd suggest taking a look at https://github.com/stillonearth/MuJoCo-WASM/issues/1 (older 2.3.1 build but still relevant)


4. Prepare the Build Environment:

   mkdir build
   cd build

5. Run Emscripten CMake Commands:

   emcmake cmake ..
   emmake make

WASM Build Terminal Output


6. Deploy and Run Locally:

   emrun --no_browser --port 8080 .

WASM Build Running In Browser

WASM Build bin folder

7. Optional Cleanup and Repeat Steps if Necessary:

   rm -rf *
   emcmake cmake ..
   emmake make

Notes[edit]

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