CMakeLists.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # Copyright 2021 DeepMind Technologies Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # https://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. cmake_minimum_required(VERSION 3.16)
  15. # INTERPROCEDURAL_OPTIMIZATION is enforced when enabled.
  16. set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
  17. # Default to GLVND if available.
  18. set(CMAKE_POLICY_DEFAULT_CMP0072 NEW)
  19. # This line has to appear before 'PROJECT' in order to be able to disable incremental linking
  20. set(MSVC_INCREMENTAL_DEFAULT ON)
  21. project(
  22. mujoco_samples
  23. VERSION 3.2.5
  24. DESCRIPTION "MuJoCo samples binaries"
  25. HOMEPAGE_URL "https://mujoco.org"
  26. )
  27. enable_language(C)
  28. enable_language(CXX)
  29. if(APPLE)
  30. enable_language(OBJC)
  31. enable_language(OBJCXX)
  32. endif()
  33. # Check if we are building as standalone project.
  34. set(SAMPLE_STANDALONE OFF)
  35. set(_INSTALL_SAMPLES ON)
  36. if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
  37. set(SAMPLE_STANDALONE ON)
  38. # If standalone, do not install the samples.
  39. set(_INSTALL_SAMPLES OFF)
  40. endif()
  41. list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
  42. if(SAMPLE_STANDALONE)
  43. include(SampleOptions)
  44. else()
  45. enforce_mujoco_macosx_min_version()
  46. endif()
  47. include(SampleDependencies)
  48. set(MUJOCO_SAMPLE_COMPILE_OPTIONS "${AVX_COMPILE_OPTIONS}" "${EXTRA_COMPILE_OPTIONS}")
  49. set(MUJOCO_SAMPLE_LINK_OPTIONS "${EXTRA_LINK_OPTIONS}")
  50. if(MUJOCO_HARDEN)
  51. if(WIN32)
  52. set(MUJOCO_SAMPLE_LINK_OPTIONS "${MUJOCO_SAMPLE_LINK_OPTIONS}" -Wl,/DYNAMICBASE)
  53. else()
  54. set(MUJOCO_SAMPLE_COMPILE_OPTIONS "${MUJOCO_SAMPLE_COMPILE_OPTIONS}" -fPIE)
  55. if(APPLE)
  56. set(MUJOCO_SAMPLE_LINK_OPTIONS "${MUJOCO_SAMPLE_LINK_OPTIONS}" -Wl,-pie)
  57. else()
  58. set(MUJOCO_SAMPLE_LINK_OPTIONS "${MUJOCO_SAMPLE_LINK_OPTIONS}" -pie)
  59. endif()
  60. endif()
  61. endif()
  62. # Build sample binaries
  63. add_executable(compile compile.cc)
  64. target_compile_options(compile PUBLIC ${MUJOCO_SAMPLE_COMPILE_OPTIONS})
  65. target_link_libraries(compile Threads::Threads)
  66. target_link_options(compile PRIVATE ${MUJOCO_SAMPLE_LINK_OPTIONS})
  67. add_executable(testspeed testspeed.cc)
  68. target_compile_options(testspeed PUBLIC ${MUJOCO_SAMPLE_COMPILE_OPTIONS})
  69. target_link_libraries(testspeed Threads::Threads)
  70. target_link_options(testspeed PRIVATE ${MUJOCO_SAMPLE_LINK_OPTIONS})
  71. target_link_libraries(compile mujoco::mujoco)
  72. target_link_libraries(testspeed mujoco::mujoco)
  73. # Build samples that require GLFW.
  74. add_executable(basic basic.cc)
  75. target_compile_options(basic PUBLIC ${MUJOCO_SAMPLE_COMPILE_OPTIONS})
  76. target_link_libraries(
  77. basic
  78. mujoco::mujoco
  79. glfw
  80. Threads::Threads
  81. )
  82. target_link_options(basic PRIVATE ${MUJOCO_SAMPLE_LINK_OPTIONS})
  83. add_executable(record record.cc array_safety.h)
  84. target_compile_options(record PUBLIC ${MUJOCO_SAMPLE_COMPILE_OPTIONS})
  85. target_link_libraries(
  86. record
  87. mujoco::mujoco
  88. glfw
  89. Threads::Threads
  90. )
  91. target_link_options(record PRIVATE ${MUJOCO_SAMPLE_LINK_OPTIONS})
  92. if(APPLE AND MUJOCO_BUILD_MACOS_FRAMEWORKS)
  93. embed_in_bundle(basic simulate)
  94. embed_in_bundle(compile simulate)
  95. embed_in_bundle(record simulate)
  96. embed_in_bundle(testspeed simulate)
  97. endif()
  98. # Do not install if macOS Bundles are created as RPATH is managed manually there.
  99. if(APPLE AND MUJOCO_BUILD_MACOS_FRAMEWORKS)
  100. set(_INSTALL_SAMPLES OFF)
  101. endif()
  102. if(_INSTALL_SAMPLES)
  103. include(TargetAddRpath)
  104. # Add support to RPATH for the samples.
  105. target_add_rpath(
  106. TARGETS
  107. basic
  108. compile
  109. record
  110. testspeed
  111. INSTALL_DIRECTORY
  112. "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}"
  113. LIB_DIRS
  114. "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}"
  115. DEPENDS
  116. MUJOCO_ENABLE_RPATH
  117. )
  118. install(
  119. TARGETS basic
  120. compile
  121. record
  122. testspeed
  123. EXPORT ${PROJECT_NAME}
  124. RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT samples
  125. LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT samples
  126. ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT samples
  127. BUNDLE DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT samples
  128. PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT samples
  129. )
  130. if(NOT MUJOCO_SAMPLES_USE_SYSTEM_GLFW)
  131. # We downloaded GLFW. Depending if it is a static or shared LIBRARY we might
  132. # need to install it.
  133. get_target_property(MJ_GLFW_LIBRARY_TYPE glfw TYPE)
  134. if(MJ_GLFW_LIBRARY_TYPE STREQUAL SHARED_LIBRARY)
  135. install(
  136. TARGETS glfw
  137. EXPORT ${PROJECT_NAME}
  138. RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT samples
  139. LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT samples
  140. ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT samples
  141. PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT samples
  142. )
  143. endif()
  144. endif()
  145. endif()