MujocoLinkOptions.cmake 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. include(CheckCSourceCompiles)
  15. # Gets the appropriate linker options for building MuJoCo, based on features available on the
  16. # linker.
  17. function(get_mujoco_extra_link_options OUTPUT_VAR)
  18. if(MSVC)
  19. set(EXTRA_LINK_OPTIONS /OPT:REF /OPT:ICF=5)
  20. else()
  21. set(EXTRA_LINK_OPTIONS)
  22. if(WIN32)
  23. set(CMAKE_REQUIRED_FLAGS "-fuse-ld=lld-link")
  24. check_c_source_compiles("int main() {}" SUPPORTS_LLD)
  25. if(SUPPORTS_LLD)
  26. set(EXTRA_LINK_OPTIONS
  27. ${EXTRA_LINK_OPTIONS}
  28. -fuse-ld=lld-link
  29. -Wl,/OPT:REF
  30. -Wl,/OPT:ICF
  31. )
  32. endif()
  33. else()
  34. set(CMAKE_REQUIRED_FLAGS "-fuse-ld=lld")
  35. check_c_source_compiles("int main() {}" SUPPORTS_LLD)
  36. if(SUPPORTS_LLD)
  37. set(EXTRA_LINK_OPTIONS ${EXTRA_LINK_OPTIONS} -fuse-ld=lld)
  38. else()
  39. set(CMAKE_REQUIRED_FLAGS "-fuse-ld=gold")
  40. check_c_source_compiles("int main() {}" SUPPORTS_GOLD)
  41. if(SUPPORTS_GOLD)
  42. set(EXTRA_LINK_OPTIONS ${EXTRA_LINK_OPTIONS} -fuse-ld=gold)
  43. endif()
  44. endif()
  45. set(CMAKE_REQUIRED_FLAGS ${EXTRA_LINK_OPTIONS} "-Wl,--gc-sections")
  46. check_c_source_compiles("int main() {}" SUPPORTS_GC_SECTIONS)
  47. if(SUPPORTS_GC_SECTIONS)
  48. set(EXTRA_LINK_OPTIONS ${EXTRA_LINK_OPTIONS} -Wl,--gc-sections)
  49. else()
  50. set(CMAKE_REQUIRED_FLAGS ${EXTRA_LINK_OPTIONS} "-Wl,-dead_strip")
  51. check_c_source_compiles("int main() {}" SUPPORTS_DEAD_STRIP)
  52. if(SUPPORTS_DEAD_STRIP)
  53. set(EXTRA_LINK_OPTIONS ${EXTRA_LINK_OPTIONS} -Wl,-dead_strip)
  54. endif()
  55. endif()
  56. endif()
  57. endif()
  58. set("${OUTPUT_VAR}"
  59. ${EXTRA_LINK_OPTIONS}
  60. PARENT_SCOPE
  61. )
  62. endfunction()