-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
159 lines (140 loc) · 4.58 KB
/
Copy pathCMakeLists.txt
File metadata and controls
159 lines (140 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Copyright (c) 2025 Robotics and AI Institute LLC. All rights reserved.
cmake_minimum_required(VERSION 3.15)
# Set policy CMP0148 to NEW to use FindPython instead of deprecated FindPythonInterp/FindPythonLibs
if(POLICY CMP0148)
cmake_policy(SET CMP0148 NEW)
endif()
project(mujoco_extensions)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# Use RPATH settings to avoid library path conflicts
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
# Find required packages
find_package(OpenMP REQUIRED)
find_package(Eigen3 REQUIRED)
# Find Python FIRST (before pybind11) to avoid deprecated FindPythonInterp
# Use CONDA_PREFIX to avoid picking up venv/system Python headers
if(DEFINED ENV{CONDA_PREFIX})
set(Python_ROOT_DIR $ENV{CONDA_PREFIX})
endif()
find_package(Python COMPONENTS Interpreter Development.Module REQUIRED)
# Tell pybind11 to use modern FindPython
set(PYBIND11_FINDPYTHON ON)
find_package(pybind11 REQUIRED)
execute_process(
COMMAND ${Python_EXECUTABLE} -c "import mujoco, os; print(os.path.dirname(mujoco.__file__))"
OUTPUT_VARIABLE MJ_PKG_ROOT OUTPUT_STRIP_TRAILING_WHITESPACE
)
file(GLOB MUJOCO_LIB_FILES "${MJ_PKG_ROOT}/libmujoco.so*" "${MJ_PKG_ROOT}/libmujoco.*.dylib")
if(MUJOCO_LIB_FILES)
list(GET MUJOCO_LIB_FILES 0 MUJOCO_LIB)
else()
message(FATAL_ERROR "MuJoCo library not found in ${MJ_PKG_ROOT}")
endif()
set(MJINC_DIR "${MJ_PKG_ROOT}/include")
# Find site-packages directory for output
execute_process(
COMMAND ${Python_EXECUTABLE} -c "import site; print(site.getsitepackages()[0])"
OUTPUT_VARIABLE SITE_PACKAGES_DIR OUTPUT_STRIP_TRAILING_WHITESPACE
)
# Find ONNX Runtime from conda/pixi environment
if(DEFINED ENV{CONDA_PREFIX})
set(CONDA_PREFIX $ENV{CONDA_PREFIX})
# Check for both Linux (.so) and macOS (.dylib) extensions
file(GLOB ONNXRUNTIME_LIB_FILES
"${CONDA_PREFIX}/lib/libonnxruntime.so*"
"${CONDA_PREFIX}/lib/libonnxruntime*.dylib") # Added for Mac
if(ONNXRUNTIME_LIB_FILES)
list(GET ONNXRUNTIME_LIB_FILES 0 ONNXRUNTIME_LIB)
message(STATUS "Found ONNX Runtime: ${ONNXRUNTIME_LIB}")
else()
message(FATAL_ERROR "ONNX Runtime library not found in ${CONDA_PREFIX}/lib")
endif()
# Resolve include directory across possible package layouts.
# Common locations include:
# - ${CONDA_PREFIX}/include
# - ${CONDA_PREFIX}/include/onnxruntime
# - ${CONDA_PREFIX}/include/onnxruntime/core/session
find_path(ONNXRUNTIME_INCLUDE
NAMES onnxruntime_cxx_api.h
HINTS
"${CONDA_PREFIX}/include"
"${CONDA_PREFIX}/include/onnxruntime"
PATH_SUFFIXES
""
"onnxruntime"
"onnxruntime/core/session"
)
if(NOT ONNXRUNTIME_INCLUDE)
message(FATAL_ERROR
"ONNX Runtime headers not found. Expected onnxruntime_cxx_api.h under ${CONDA_PREFIX}/include")
endif()
else()
message(FATAL_ERROR "CONDA_PREFIX not set. Please run within pixi environment.")
endif()
message(STATUS "MuJoCo library: ${MUJOCO_LIB}")
message(STATUS "MuJoCo include: ${MJINC_DIR}")
message(STATUS "ONNX Runtime library: ${ONNXRUNTIME_LIB}")
message(STATUS "ONNX Runtime include: ${ONNXRUNTIME_INCLUDE}")
message(STATUS "Site packages: ${SITE_PACKAGES_DIR}")
include_directories(
${CMAKE_SOURCE_DIR}
${MJINC_DIR}
${ONNXRUNTIME_INCLUDE}
)
# --- ONNX Interface Library ---
add_library(onnx_interface STATIC
onnx_interface/onnx_interface.cpp
)
target_include_directories(onnx_interface PUBLIC
${CMAKE_SOURCE_DIR}
${ONNXRUNTIME_INCLUDE}
)
target_link_libraries(onnx_interface PUBLIC
Eigen3::Eigen
${ONNXRUNTIME_LIB}
)
# --- System Library ---
add_library(system STATIC
system/system_class.cpp
system/system_utils.cpp
)
target_include_directories(system PUBLIC
${CMAKE_SOURCE_DIR}
${MJINC_DIR}
)
target_link_libraries(system PUBLIC
Eigen3::Eigen
${MUJOCO_LIB}
onnx_interface
)
# --- Policy Rollout pybind module ---
pybind11_add_module(policy_rollout_pybind
policy_rollout/pybind/pybind.cpp
policy_rollout/pybind/policy_rollout.cpp
)
if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
target_compile_options(policy_rollout_pybind PRIVATE
-mtune=native
)
else()
target_compile_options(policy_rollout_pybind PRIVATE
-fopenmp
-mtune=native
)
endif()
target_link_libraries(policy_rollout_pybind PRIVATE
Eigen3::Eigen
OpenMP::OpenMP_CXX
system
${MUJOCO_LIB}
)
set_target_properties(policy_rollout_pybind PROPERTIES
BUILD_RPATH "$ORIGIN/../../mujoco"
INSTALL_RPATH "$ORIGIN/../../mujoco"
BUILD_RPATH_USE_ORIGIN TRUE
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/policy_rollout"
)