xmake.lua 7.3 KB

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