Initial open sourcing of MuJoCo.

PiperOrigin-RevId: 450374687
Change-Id: Ie3225a46ce095fc28ae8e63c326a640261f562bb
This commit is contained in:
Saran Tunyasuvunakool
2022-05-23 01:08:10 -07:00
committed by Copybara-Service
parent 0e5d062302
commit 1913a02b40
275 changed files with 99607 additions and 935 deletions
+84 -45
View File
@@ -40,32 +40,60 @@ set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
separate_arguments(CMDLINE_LINK_OPTIONS UNIX_COMMAND ${CMAKE_SHARED_LINKER_FLAGS})
add_link_options(${CMDLINE_LINK_OPTIONS})
if(MSVC)
add_compile_options(/Gy /Gw /Oi)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-fdata-sections -ffunction-sections)
endif()
include(MujocoLinkOptions)
get_mujoco_extra_link_options(EXTRA_LINK_OPTIONS)
add_link_options(${EXTRA_LINK_OPTIONS})
if(APPLE)
add_compile_options(-Werror=partial-availability -Werror=unguarded-availability)
add_link_options(-Wl,-no_weak_imports)
include(MujocoMacOS)
enforce_mujoco_macosx_min_version()
if(WIN32)
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
endif()
include(MujocoHarden)
add_compile_options("${MUJOCO_HARDEN_COMPILE_OPTIONS}")
add_link_options("${MUJOCO_HARDEN_LINK_OPTIONS}")
find_package(Python3 COMPONENTS Interpreter Development)
include(FindOrFetch)
# ==================== MUJOCO LIBRARY ==========================================
if(NOT TARGET mujoco)
find_library(MUJOCO_LIBRARY mujoco mujoco.2.1.5 HINTS ${MUJOCO_LIBRARY_DIR} REQUIRED)
find_path(MUJOCO_INCLUDE mujoco.h HINTS ${MUJOCO_INCLUDE_DIR} REQUIRED)
message("MuJoCo is at ${MUJOCO_LIBRARY}")
message("MuJoCo headers are at ${MUJOCO_INCLUDE}")
add_library(mujoco SHARED IMPORTED)
if(APPLE)
# On macOS, check if we are using mujoco.framework first.
# Framework headers are searched differently from normal headers.
# We need to use -F instead of the usual target_include_directories.
find_path(MUJOCO_FRAMEWORK mujoco.Framework HINTS ${MUJOCO_FRAMEWORK_DIR})
if(MUJOCO_FRAMEWORK)
message("MuJoCo framework is at ${MUJOCO_FRAMEWORK}/mujoco.framework")
set(MUJOCO_LIBRARY ${MUJOCO_FRAMEWORK}/mujoco.framework/Versions/A/libmujoco.2.2.0.dylib)
target_compile_options(mujoco INTERFACE -F${MUJOCO_FRAMEWORK})
endif()
endif()
if(NOT MUJOCO_FRAMEWORK)
find_library(MUJOCO_LIBRARY mujoco mujoco.2.2.0 HINTS ${MUJOCO_LIBRARY_DIR} REQUIRED)
find_path(MUJOCO_INCLUDE mujoco/mujoco.h HINTS ${MUJOCO_INCLUDE_DIR} REQUIRED)
message("MuJoCo is at ${MUJOCO_LIBRARY}")
message("MuJoCo headers are at ${MUJOCO_INCLUDE}")
target_include_directories(mujoco INTERFACE "${MUJOCO_INCLUDE}")
endif()
if(WIN32)
set_target_properties(mujoco PROPERTIES IMPORTED_IMPLIB "${MUJOCO_LIBRARY}")
else()
set_target_properties(mujoco PROPERTIES IMPORTED_LOCATION "${MUJOCO_LIBRARY}")
endif()
target_include_directories(mujoco INTERFACE "${MUJOCO_INCLUDE}")
if(APPLE)
execute_process(
COMMAND otool -XD ${MUJOCO_LIBRARY}
@@ -88,18 +116,7 @@ if(NOT TARGET mujoco)
endif()
# ==================== ABSEIL ==================================================
if(APPLE)
set(ABSL_EXTRA_FETCH_ARGS
PATCH_COMMAND
"sed"
"-i"
" "
"s/-march=armv8-a+crypto/-mcpu=apple-m1+crypto/g"
"${CMAKE_BINARY_DIR}/_deps/abseil-cpp-src/absl/copts/GENERATED_AbseilCopts.cmake"
)
else()
set(ABSL_EXTRA_FETCH_ARGS "")
endif()
set(MUJOCO_PYTHON_ABSL_TARGETS absl::core_headers absl::flat_hash_map absl::span)
findorfetch(
USE_SYSTEM_PACKAGE
OFF
@@ -110,14 +127,26 @@ findorfetch(
GIT_REPO
https://github.com/abseil/abseil-cpp
GIT_TAG
215105818dfde3174fe799600bb0f3cae233d0bf # 20211102.0
78f9680225b9792c26dfdd99d0bd26c96de53dd4 # # Fixes universal builds for macOS
TARGETS
absl::core_headers
absl::flat_hash_map
absl::span
${ABSL_EXTRA_FETCH_ARGS}
${MUJOCO_PYTHON_ABSL_TARGETS}
EXCLUDE_FROM_ALL
)
foreach(absl_target IN ITEMS ${MUJOCO_PYTHON_ABSL_TARGETS})
get_target_property(absl_target_aliased ${absl_target} ALIASED_TARGET)
if(absl_target_aliased)
set(absl_target ${absl_target_aliased})
endif()
get_target_property(absl_target_type ${absl_target} TYPE)
if(NOT
${absl_target_type}
STREQUAL
"INTERFACE_LIBRARY"
)
target_compile_options(${absl_target} PRIVATE ${MUJOCO_MACOS_COMPILE_OPTIONS})
target_link_options(${absl_target} PRIVATE ${MUJOCO_MACOS_LINK_OPTIONS})
endif()
endforeach()
# ==================== EIGEN ===================================================
add_compile_definitions(EIGEN_MPL2_ONLY)
@@ -138,26 +167,21 @@ findorfetch(
)
# ==================== PYBIND11 ================================================
if(MUJOCO_PYBIND11_DIR)
FetchContent_Declare(pybind11 SOURCE_DIR ${MUJOCO_PYBIND11_DIR} EXCLUDE_FROM_ALL)
FetchContent_MakeAvailable(pybind11)
else()
findorfetch(
USE_SYSTEM_PACKAGE
OFF
PACKAGE_NAME
pybind11
LIBRARY_NAME
pybind11
GIT_REPO
https://github.com/pybind/pybind11
GIT_TAG
a8f1a5567608f346bdba293b3d062a288ee16cd4
TARGETS
pybind11::pybind11_headers
EXCLUDE_FROM_ALL
)
endif()
findorfetch(
USE_SYSTEM_PACKAGE
OFF
PACKAGE_NAME
pybind11
LIBRARY_NAME
pybind11
GIT_REPO
https://github.com/pybind/pybind11
GIT_TAG
a8f1a5567608f346bdba293b3d062a288ee16cd4
TARGETS
pybind11::pybind11_headers
EXCLUDE_FROM_ALL
)
# ==================== MUJOCO PYTHON BINDINGS ==================================
@@ -222,6 +246,7 @@ target_link_libraries(
structs_header
INTERFACE absl::flat_hash_map
absl::span
crossplatform
mujoco
raw
)
@@ -246,6 +271,19 @@ get_avx_compile_options(AVX_COMPILE_OPTIONS)
macro(mujoco_pybind11_module name)
pybind11_add_module(${name} ${ARGN})
target_compile_options(${name} PRIVATE ${AVX_COMPILE_OPTIONS})
if(NOT MSVC)
target_compile_options(${name} PRIVATE -Wall -Werror)
if(CMAKE_C_COMPILER_ID STREQUAL GNU)
target_compile_options(
${name}
PRIVATE -Wno-int-in-bool-context
-Wno-maybe-uninitialized
-Wno-sign-compare
-Wno-stringop-overflow
-Wno-stringop-truncation
)
endif()
endif()
set_target_properties(${name} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
if(APPLE)
add_custom_command(
@@ -323,6 +361,7 @@ mujoco_pybind11_module(
target_link_libraries(
_structs
PRIVATE absl::flat_hash_map
crossplatform
mujoco
raw
errors_header
+1 -1
View File
@@ -53,6 +53,6 @@ if _MUJOCO_GL not in ('disable', 'disabled', 'off', 'false', '0'):
else:
from mujoco.glfw import GLContext
HEADERS_DIR = os.path.join(os.path.dirname(__file__), 'include')
HEADERS_DIR = os.path.join(os.path.dirname(__file__), 'include/mujoco')
__version__ = mj_versionString() # pylint: disable=undefined-variable
+1 -1
View File
@@ -19,7 +19,7 @@
#include <sstream>
#include <type_traits>
#include <mujoco.h>
#include <mujoco/mujoco.h>
#include "errors.h"
#include "structs.h"
#include "raw.h"
@@ -71,7 +71,7 @@ struct {enum.name} {{
#include <tuple>
#include <utility>
#include <mujoco.h>
#include <mujoco/mujoco.h>
namespace mujoco::python_traits {{
@@ -91,7 +91,7 @@ struct {func.name} {{
#include <tuple>
#include <mujoco.h>
#include <mujoco/mujoco.h>
#include "util/crossplatform.h"
namespace mujoco::python_traits {{
+6 -6
View File
@@ -12,13 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <utility>
#include <vector>
#include <mjmodel.h>
#include <mjvisualize.h>
#include <mujoco.h>
#include <mujoco/mjmodel.h>
#include <mujoco/mjvisualize.h>
#include <mujoco/mujoco.h>
#include <pybind11/cast.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
namespace mujoco::python {
namespace {
@@ -32,7 +32,7 @@ py::tuple MakeTuple(
for (int i = 0; i < N; i++) {
result.append(py::str(strings[i]));
}
return result;
return std::move(result);
}
template <auto N>
@@ -44,7 +44,7 @@ py::tuple MakeTuple(const char* (&strings)[N][3]) {
py::str(strings[i][1]),
py::str(strings[i][2])));
}
return result;
return std::move(result);
}
PYBIND11_MODULE(_constants, pymodule) {
+2 -2
View File
@@ -20,7 +20,7 @@
#include <string>
#include <type_traits>
#include <mjexport.h>
#include <mujoco/mjexport.h>
#include "util/crossplatform.h"
#include "util/func_wrap.h"
#include <pybind11/pybind11.h>
@@ -109,7 +109,7 @@ class ErrorBase : public pybind11::builtin_exception {
static thread_local std::jmp_buf mju_error_jmp_buf;
static thread_local std::array<char, 1024> mju_error_msg{0};
static void MjErrorHandler(const char* msg) {
static inline void MjErrorHandler(const char* msg) {
std::strncpy(mju_error_msg.data(), msg, mju_error_msg.size());
std::longjmp(mju_error_jmp_buf, 1);
}
+1 -1
View File
@@ -23,7 +23,7 @@
#include <type_traits>
#include <Eigen/Core>
#include <mujoco.h>
#include <mujoco/mujoco.h>
#include "errors.h"
#include "structs.h"
#include "util/array_traits.h"
+5 -3
View File
@@ -15,7 +15,7 @@
#ifndef MUJOCO_PYTHON_INDEXER_XMACRO_H_
#define MUJOCO_PYTHON_INDEXER_XMACRO_H_
#include <mjxmacro.h>
#include <mujoco/mjxmacro.h>
#define MJMODEL_ACTUATOR \
X( int, actuator_, trntype, nu, 1 ) \
@@ -26,11 +26,13 @@
X( int, actuator_, group, nu, 1 ) \
X( mjtByte, actuator_, ctrllimited, nu, 1 ) \
X( mjtByte, actuator_, forcelimited, nu, 1 ) \
X( mjtByte, actuator_, actlimited, nu, 1 ) \
X( mjtNum, actuator_, dynprm, nu, mjNDYN ) \
X( mjtNum, actuator_, gainprm, nu, mjNGAIN ) \
X( mjtNum, actuator_, biasprm, nu, mjNBIAS ) \
X( mjtNum, actuator_, ctrlrange, nu, 2 ) \
X( mjtNum, actuator_, forcerange, nu, 2 ) \
X( mjtNum, actuator_, actrange, nu, 2 ) \
X( mjtNum, actuator_, gear, nu, 6 ) \
X( mjtNum, actuator_, cranklength, nu, 1 ) \
X( mjtNum, actuator_, acc0, nu, 1 ) \
@@ -361,8 +363,8 @@
X( mjtNum, , qfrc_bias, nv, 1 ) \
X( mjtNum, , qfrc_passive, nv, 1 ) \
X( mjtNum, , qfrc_actuator, nv, 1 ) \
X( mjtNum, , qfrc_unc, nv, 1 ) \
X( mjtNum, , qacc_unc, nv, 1 ) \
X( mjtNum, , qfrc_smooth, nv, 1 ) \
X( mjtNum, , qacc_smooth, nv, 1 ) \
X( mjtNum, , qfrc_constraint, nv, 1 ) \
X( mjtNum, , qfrc_inverse, nv, 1 )
+5 -4
View File
@@ -20,11 +20,10 @@
#include <variant>
#include <vector>
#include "errors.h"
#include "indexers.h"
#include "mjdata_meta.h"
#include "raw.h"
#include <pybind11/pybind11.h>
#include "util/crossplatform.h"
namespace mujoco::python {
@@ -218,13 +217,15 @@ MJDATA_VIEW_GROUPS
#undef XGROUP
#define MAKE_SHAPE(dim) \
[n = (dim)]() -> std::vector<int> { \
MUJOCO_DIAG_IGNORE_UNUSED_LAMBDA_CAPTURE \
[n = (dim)]() -> std::vector<int> { \
if constexpr (std::string_view(#dim) == std::string_view("1")) { \
return {}; \
} else { \
return {n}; \
} \
}()
}() \
MUJOCO_DIAG_UNIGNORE_UNUSED_LAMBDA_CAPTURE
#undef MJ_M
#define MJ_M(n) m_->n
+1 -1
View File
@@ -23,7 +23,7 @@
#include <vector>
#include <absl/container/flat_hash_map.h>
#include <mjxmacro.h>
#include <mujoco/mjxmacro.h>
#include "indexer_xmacro.h"
#include "mjdata_meta.h"
#include "raw.h"
+4 -2
View File
@@ -15,8 +15,9 @@
#ifndef MUJOCO_PYTHON_MJDATA_META_H_
#define MUJOCO_PYTHON_MJDATA_META_H_
#include <mjxmacro.h>
#include <mujoco/mjxmacro.h>
#include "raw.h"
#include "util/crossplatform.h"
namespace mujoco::python {
namespace _impl {
@@ -95,7 +96,8 @@ struct MjDataMetadata {
#undef X
dummy_() {}
bool dummy_; // Dummy variable to terminate X macro sequences.
// Dummy variable to terminate X macro sequences.
MUJOCO_MAYBE_UNUSED bool dummy_;
};
} // namespace mujoco::python
+4 -4
View File
@@ -15,10 +15,10 @@
#ifndef MUJOCO_PYTHON_RAW_H_
#define MUJOCO_PYTHON_RAW_H_
#include <mjdata.h>
#include <mjmodel.h>
#include <mjrender.h>
#include <mjvisualize.h>
#include <mujoco/mjdata.h>
#include <mujoco/mjmodel.h>
#include <mujoco/mjrender.h>
#include <mujoco/mjvisualize.h>
// Type aliases for MuJoCo C structs to allow us refer to consistently refer
// to them under the "raw" namespace.
+2 -2
View File
@@ -16,8 +16,8 @@
#include <cstdint>
#include <Eigen/Core>
#include <mjrender.h>
#include <mujoco.h>
#include <mujoco/mjrender.h>
#include <mujoco/mujoco.h>
#include "errors.h"
#include "function_traits.h"
#include "functions.h"
+1 -1
View File
@@ -17,7 +17,7 @@
#include <iostream>
#include <mjtnum.h>
#include <mujoco/mjtnum.h>
namespace mujoco::python::_impl {
+2 -2
View File
@@ -34,8 +34,8 @@
#include <vector>
#include <absl/container/flat_hash_map.h>
#include <mjxmacro.h>
#include <mujoco.h>
#include <mujoco/mjxmacro.h>
#include <mujoco/mujoco.h>
#include "errors.h"
#include "function_traits.h"
#include "indexers.h"
+3 -3
View File
@@ -25,8 +25,8 @@
#include <vector>
#include <absl/types/span.h>
#include <mujoco.h>
#include <mjxmacro.h>
#include <mujoco/mujoco.h>
#include <mujoco/mjxmacro.h>
#include "indexers.h"
#include "mjdata_meta.h"
#include "raw.h"
@@ -894,7 +894,7 @@ static InitPyArray(Shape&& shape, T* buf, pybind11::handle owner) {
out.append(InitPyArray(block_shape, &buf[i * block_size], owner));
}
}
return out;
return std::move(out);
}
// Same as above, but where we can determine array dimensions through the
+2 -6
View File
@@ -23,11 +23,6 @@ set(CMAKE_CXX_EXTENSIONS OFF)
# INTERPROCEDURAL_OPTIMIZATION is enforced when enabled.
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
if(APPLE)
add_compile_options(-Werror=partial-availability -Werror=unguarded-availability)
add_link_options(-Wl,-no_weak_imports)
endif()
add_library(crossplatform INTERFACE)
target_sources(crossplatform INTERFACE crossplatform.h)
set_target_properties(crossplatform PROPERTIES PUBLIC_HEADER crossplatform.h)
@@ -62,7 +57,8 @@ target_link_libraries(
func_traits
)
if(MUJOCO_TEST_PYTHON_UTIL)
if(BUILD_TESTING)
enable_testing()
add_executable(array_traits_test array_traits_test.cc)
target_link_libraries(
array_traits_test
+25
View File
@@ -48,4 +48,29 @@
#define MUJOCO_ALWAYS_INLINE_LAMBDA_MUTABLE
#endif
#ifndef MUJOCO_DIAG_IGNORE_UNUSED_LAMBDA_CAPTURE
#if defined(__clang__)
#define MUJOCO_DIAG_IGNORE_UNUSED_LAMBDA_CAPTURE \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wunused-lambda-capture\"")
#define MUJOCO_DIAG_UNIGNORE_UNUSED_LAMBDA_CAPTURE \
_Pragma("clang diagnostic pop")
#elif defined(__GNUC__)
#define MUJOCO_DIAG_IGNORE_UNUSED_LAMBDA_CAPTURE \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wunused-lambda-capture\"")
#define MUJOCO_DIAG_UNIGNORE_UNUSED_LAMBDA_CAPTURE _Pragma("GCC diagnostic pop")
#else
#define MUJOCO_DIAG_IGNORE_UNUSED_LAMBDA_CAPTURE
#define MUJOCO_DIAG_UNIGNORE_UNUSED_LAMBDA_CAPTURE
#endif
#endif
// GCC ignores [[maybe_unused]] and emits a -Wattributes
#if defined(__GNUC__) && !defined(__clang__)
#define MUJOCO_MAYBE_UNUSED
#else
#define MUJOCO_MAYBE_UNUSED [[maybe_unused]]
#endif
#endif // MUJOCO_PYTHON_UTIL_CROSSPLATFORM_H_