Skip to content

Commit bcdb398

Browse files
mujoco: add package (#8810)
* mujoco: add package * fix qhull * fix bugs * remove lto * limit bsd
1 parent ca283fb commit bcdb398

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed

packages/m/mujoco/xmake.lua

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package("mujoco")
2+
set_homepage("https://mujoco.org/")
3+
set_description("Multi-Joint dynamics with Contact. A general purpose physics simulator.")
4+
set_license("Apache-2.0")
5+
6+
set_urls("https://github.com/google-deepmind/mujoco/archive/refs/tags/$(version).tar.gz",
7+
"https://github.com/google-deepmind/mujoco.git")
8+
9+
add_versions("3.4.0", "adff5e9397aac20189ee1525aabf1fbecc63c43697e8ad66a61220222983810f")
10+
11+
add_configs("simulate", {description = "Build simulate library for MuJoCo", default = false, type = "boolean"})
12+
add_configs("usd", {description = "Build with OpenUSD", default = false, type = "boolean"})
13+
14+
add_links("simulate", "mujoco")
15+
16+
add_deps("cmake")
17+
if is_subhost("windows") then
18+
add_deps("pkgconf")
19+
else
20+
add_deps("pkg-config")
21+
end
22+
add_deps("libccd", "lodepng", "qhull", "tinyobjloader", "tinyxml2", "trianglemeshdistance", "marchingcubecpp")
23+
24+
if on_check then
25+
on_check("android", function (package)
26+
local ndk = package:toolchain("ndk")
27+
local ndk_sdkver = ndk:config("ndk_sdkver")
28+
assert(ndk_sdkver and tonumber(ndk_sdkver) > 21, "package(mujoco) require ndk api level > 21")
29+
end)
30+
end
31+
32+
on_load(function (package)
33+
if package:config("usd") then
34+
package:add("deps", "usd")
35+
package:add("defines", "mjUSEUSD")
36+
end
37+
if package:config("simulate") then
38+
package:add("deps", "glfw")
39+
end
40+
41+
if not package:config("shared") then
42+
package:add("defines", "MJ_STATIC")
43+
end
44+
end)
45+
46+
on_install("!wasm and !bsd", function (package)
47+
if package:dep("qhull"):config("shared") then
48+
-- TODO: patch cmake target_link_libraries
49+
raise("package(mujoco) unsupported shared qhull library")
50+
end
51+
52+
-- support static build
53+
io.replace("CMakeLists.txt", "add_library(mujoco SHARED", "add_library(mujoco ", {plain = true})
54+
-- remove fetch content
55+
io.replace("CMakeLists.txt", "include(MujocoDependencies)", "", {plain = true})
56+
-- remove hardcode ccd and dynamic library export macro
57+
io.replace("CMakeLists.txt", "CCD_STATIC_DEFINE MUJOCO_DLL_EXPORTS", "", {plain = true})
58+
-- remove unused install target
59+
io.replace("CMakeLists.txt", "list(APPEND MUJOCO_TARGETS lodepng)", "", {plain = true})
60+
if package:is_plat("mingw") then
61+
-- mujoco.rc:23: syntax error
62+
io.replace("CMakeLists.txt", "set(MUJOCO_RESOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/dist/mujoco.rc)", "", {plain = true})
63+
-- remove /STACK:16777216
64+
io.replace("cmake/MujocoLinkOptions.cmake", "if(WIN32)", "if(0)", {plain = true})
65+
io.replace("simulate/cmake/MujocoLinkOptions.cmake", "if(WIN32)", "if(0)", {plain = true})
66+
end
67+
68+
io.replace("cmake/MujocoOptions.cmake", "set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)", "", {plain = true})
69+
io.replace("simulate/cmake/SimulateOptions.cmake", "set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)", "", {plain = true})
70+
71+
io.replace("cmake/MujocoOptions.cmake", "-Werror", "", {plain = true})
72+
io.replace("simulate/cmake/SimulateOptions.cmake", "-Werror", "", {plain = true})
73+
74+
io.replace("src/user/user_mesh.cc", [[#include "qhull_ra.h"]], "#include <libqhull_r/qhull_ra.h>", {plain = true})
75+
io.replace("src/user/user_mesh.cc",
76+
"#include <TriangleMeshDistance/include/tmd/TriangleMeshDistance.h>",
77+
"#include <tmd/TriangleMeshDistance.h>", {plain = true})
78+
79+
io.replace("CMakeLists.txt", [[target_link_libraries(
80+
mujoco
81+
PRIVATE ccd
82+
lodepng
83+
qhullstatic_r
84+
tinyobjloader
85+
tinyxml2
86+
)]], "", {plain = true})
87+
88+
local file = io.open("CMakeLists.txt", "a")
89+
if file then
90+
file:print([[
91+
if (BUILD_SHARED_LIBS)
92+
target_compile_definitions(mujoco PRIVATE MUJOCO_DLL_EXPORTS)
93+
else()
94+
target_compile_definitions(mujoco PUBLIC MJ_STATIC)
95+
endif()
96+
97+
find_package(ccd CONFIG REQUIRED)
98+
include(FindPkgConfig)
99+
pkg_search_module("lodepng" REQUIRED IMPORTED_TARGET "lodepng")
100+
find_package(Qhull CONFIG REQUIRED)
101+
find_package(tinyobjloader CONFIG REQUIRED)
102+
find_package(tinyxml2 CONFIG REQUIRED)
103+
pkg_search_module("trianglemeshdistance" REQUIRED IMPORTED_TARGET "trianglemeshdistance")
104+
pkg_search_module("marchingcubecpp" REQUIRED IMPORTED_TARGET "marchingcubecpp")
105+
target_link_libraries(mujoco PRIVATE
106+
ccd
107+
PkgConfig::lodepng
108+
Qhull::qhullstatic_r
109+
tinyobjloader::tinyobjloader
110+
tinyxml2::tinyxml2
111+
PkgConfig::trianglemeshdistance
112+
PkgConfig::marchingcubecpp
113+
)
114+
]])
115+
file:close()
116+
end
117+
118+
if package:config("simulate") then
119+
-- remove fetch content
120+
io.replace("simulate/CMakeLists.txt", "include(SimulateDependencies)", "", {plain = true})
121+
io.replace("simulate/CMakeLists.txt", "if(NOT TARGET lodepng)", "if(0)", {plain = true})
122+
io.replace("simulate/CMakeLists.txt",
123+
"add_library(libmujoco_simulate STATIC $<TARGET_OBJECTS:platform_ui_adapter> $<TARGET_OBJECTS:lodepng>)",
124+
[[
125+
add_library(libmujoco_simulate STATIC $<TARGET_OBJECTS:platform_ui_adapter>)
126+
include(FindPkgConfig)
127+
pkg_search_module("lodepng" REQUIRED IMPORTED_TARGET "lodepng")
128+
find_package(Qhull CONFIG REQUIRED)
129+
find_package(glfw3 REQUIRED CONFIG)
130+
target_link_libraries(libmujoco_simulate PRIVATE PkgConfig::lodepng Qhull::qhullstatic_r)
131+
]], {plain = true})
132+
end
133+
134+
local configs = {
135+
"-DMUJOCO_BUILD_EXAMPLES=OFF",
136+
"-DMUJOCO_BUILD_TESTS=OFF",
137+
"-DMUJOCO_BUILD_TESTS_WASM=OFF",
138+
"-DMUJOCO_SIMULATE_USE_SYSTEM_GLFW=ON",
139+
}
140+
table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:is_debug() and "Debug" or "Release"))
141+
table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
142+
table.insert(configs, "-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=" .. (package:config("lto") and "ON" or "OFF"))
143+
144+
table.insert(configs, "-DMUJOCO_BUILD_SIMULATE=" .. (package:config("simulate") and "ON" or "OFF"))
145+
table.insert(configs, "-DMUJOCO_WITH_USD=" .. (package:config("usd") and "ON" or "OFF"))
146+
147+
local opt = {}
148+
opt.cxflags = {}
149+
if package:has_tool("cc", "gcc") or package:is_plat("wasm") then
150+
table.insert(opt.cxflags, "-Wno-error=incompatible-pointer-types")
151+
end
152+
if package:is_plat("android", "bsd", "mingw") then
153+
-- src/engine/engine_util_errmem.c:107:6: error: #error "Thread-safe version of `localtime` is not present in the standard C library"
154+
table.insert(opt.cxflags, "-D_POSIX_C_SOURCE=200112L")
155+
end
156+
import("package.tools.cmake").install(package, configs, opt)
157+
end)
158+
159+
on_test(function (package)
160+
assert(package:has_cfuncs("mjv_defaultCamera", {includes = "mujoco/mujoco.h"}))
161+
end)

0 commit comments

Comments
 (0)