Add JavaScript bindings and WASM support

Co-authored-by: Matija Kecman <matijak@google.com>
Co-authored-by: Sebastian Noreña Rendón <sebas.norena@creativa77.com.ar>
Co-authored-by: Kyle Bayes <kylebayes@google.com>
PiperOrigin-RevId: 826094938
Change-Id: Id2658a187bc078056f4410c71202696753319e64
This commit is contained in:
Matias Manevi
2025-10-30 10:37:06 -07:00
committed by Copybara-Service
parent 10f4b36fb7
commit 76e64d96bb
56 changed files with 24362 additions and 22 deletions
+42 -16
View File
@@ -39,14 +39,20 @@ enable_language(CXX)
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
option(MUJOCO_BUILD_EXAMPLES "Build samples for MuJoCo" ON)
option(MUJOCO_BUILD_SIMULATE "Build simulate library for MuJoCo" ON)
option(MUJOCO_BUILD_STUDIO "Build studio library for MuJoCo" OFF)
option(MUJOCO_BUILD_TESTS "Build tests for MuJoCo" ON)
option(MUJOCO_TEST_PYTHON_UTIL "Build and test utility libraries for Python bindings" ON)
option(MUJOCO_WITH_USD "Build with OpenUSD" OFF)
option(MUJOCO_USE_FILAMENT "Use filament rendering" OFF)
option(MUJOCO_USE_FILAMENT_VULKAN "Use vulkan backend for filament rendering" OFF)
if(NOT EMSCRIPTEN)
option(MUJOCO_BUILD_EXAMPLES "Build samples for MuJoCo" ON)
option(MUJOCO_BUILD_SIMULATE "Build simulate library for MuJoCo" ON)
option(MUJOCO_BUILD_STUDIO "Build studio library for MuJoCo" OFF)
option(MUJOCO_BUILD_TESTS "Build tests for MuJoCo" ON)
option(MUJOCO_TEST_PYTHON_UTIL "Build and test utility libraries for Python bindings" ON)
option(MUJOCO_WITH_USD "Build with OpenUSD" OFF)
option(MUJOCO_USE_FILAMENT "Use filament rendering" OFF)
option(MUJOCO_USE_FILAMENT_VULKAN "Use vulkan backend for filament rendering" OFF)
endif()
if(EMSCRIPTEN)
option(MUJOCO_BUILD_TESTS_WASM "Build tests for WASM bindings" ON)
endif()
# Option to provide a path to an existing USD build directory or to Houdini HFS directory.
set(USD_DIR "" CACHE PATH "Path to an existing USD build directory.")
@@ -100,7 +106,13 @@ else()
set(MUJOCO_RESOURCE_FILES "")
endif()
add_library(mujoco SHARED ${MUJOCO_RESOURCE_FILES})
# Emscripten does not support SHARED libs
if(NOT EMSCRIPTEN)
add_library(mujoco SHARED ${MUJOCO_RESOURCE_FILES})
else()
add_library(mujoco STATIC ${MUJOCO_RESOURCE_FILES})
endif()
target_include_directories(
mujoco
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
@@ -108,27 +120,36 @@ target_include_directories(
PRIVATE src
)
add_subdirectory(plugin/elasticity)
add_subdirectory(plugin/actuator)
add_subdirectory(plugin/sensor)
add_subdirectory(plugin/sdf)
if(NOT EMSCRIPTEN)
add_subdirectory(plugin/elasticity)
add_subdirectory(plugin/actuator)
add_subdirectory(plugin/sensor)
add_subdirectory(plugin/sdf)
endif()
add_subdirectory(src/engine)
add_subdirectory(src/user)
add_subdirectory(src/xml)
add_subdirectory(src/thread)
if(MUJOCO_USE_FILAMENT)
if(MUJOCO_USE_FILAMENT AND NOT EMSCRIPTEN)
# Note that, by default, the "src/render" and "src/ui" code is added directly
# into the "mujoco" target. However, "mujoco::filament" is a separate,
# explicit target. Therefore, if you want to use MuJoCo with Filament, you
# will need to explicitly add the "mujoco::filament" target as a link
# dependency in your project.
add_subdirectory(src/experimental/filament)
else()
elseif(NOT EMSCRIPTEN)
add_subdirectory(src/render)
add_subdirectory(src/ui)
endif()
if(EMSCRIPTEN)
add_subdirectory(wasm)
if(MUJOCO_BUILD_TESTS_WASM)
add_subdirectory(wasm/tests)
endif()
endif()
target_compile_definitions(mujoco PRIVATE _GNU_SOURCE CCD_STATIC_DEFINE MUJOCO_DLL_EXPORTS -DMC_IMPLEM_ENABLE)
if(MUJOCO_ENABLE_AVX_INTRINSICS)
@@ -235,9 +256,14 @@ if(BUILD_TESTING AND MUJOCO_BUILD_TESTS)
endif()
if(NOT (APPLE AND MUJOCO_BUILD_MACOS_FRAMEWORKS))
set(MUJOCO_TARGETS mujoco)
if (EMSCRIPTEN)
list(APPEND MUJOCO_TARGETS lodepng)
endif()
# Install the libraries.
install(
TARGETS mujoco
TARGETS ${MUJOCO_TARGETS}
EXPORT ${PROJECT_NAME}
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT runtime
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT runtime