CMakeLists.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. # Copyright (c) Contributors to the Open 3D Engine Project.
  2. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR MIT
  5. # Currently we are in the Code folder: ${CMAKE_CURRENT_LIST_DIR}
  6. # Get the platform specific folder ${pal_dir} for the current folder: ${CMAKE_CURRENT_LIST_DIR}/Platform/${PAL_PLATFORM_NAME}
  7. # Note: o3de_pal_dir will take care of the details for us, as this may be a restricted platform
  8. # in which case it will see if that platform is present here or in the restricted folder.
  9. # i.e. It could here in our gem : Gems/ROS2/Code/Platform/<platorm_name> or
  10. # <restricted_folder>/<platform_name>/Gems/ROS2/Code
  11. o3de_pal_dir(pal_dir ${CMAKE_CURRENT_LIST_DIR}/Platform/${PAL_PLATFORM_NAME} "${gem_restricted_path}" "${gem_path}" "${gem_parent_relative_path}")
  12. # Now that we have the platform abstraction layer (PAL) folder for this folder, thats where we will find the
  13. # traits for this platform. Traits for a platform are defines for things like whether or not something in this gem
  14. # is supported by this platform.
  15. include(${pal_dir}/PAL_${PAL_PLATFORM_NAME_LOWERCASE}.cmake)
  16. # Check to see if building the Gem Modules are supported for the current platform
  17. if(NOT PAL_TRAIT_ROS2_SUPPORTED)
  18. return()
  19. endif()
  20. # If no ROS2 is found, no targets are valid for this Gem
  21. find_package(ROS2 MODULE)
  22. if (NOT ROS2_FOUND)
  23. message(FATAL_ERROR "Unable to detect a the ROS2 distribution on this system. Make sure it is installed and enabled.")
  24. return()
  25. endif()
  26. message(DEBUG "Building ${gem_name} Gem with ros2 $ENV{ROS_DISTRO}")
  27. # Check if ROS 2 distribution is cached
  28. get_property(ROS_DISTRO_TYPE CACHE ROS_DISTRO PROPERTY TYPE)
  29. # Perform checks with cached ROS 2 distribution and sourced distribution. Save the distribution into cache if it was not cached before or if they do not match end with error.
  30. if (NOT ROS_DISTRO_TYPE)
  31. set(ROS_DISTRO $ENV{ROS_DISTRO} CACHE STRING "ROS 2 distribution of current configuration" FORCE)
  32. elseif(NOT "$ENV{ROS_DISTRO}" STREQUAL "${ROS_DISTRO}")
  33. get_cmake_property(CACHED_VARIABLES CACHE_VARIABLES)
  34. set(ROS_DISTRO_COPY ${ROS_DISTRO})
  35. # Iterate over cached variables and unset them
  36. foreach(CACHED_VARIABLE ${CACHED_VARIABLES})
  37. unset(${CACHED_VARIABLE} CACHE)
  38. endforeach()
  39. message(FATAL_ERROR "ROS 2 distribution does not match. Configuration created for: ${ROS_DISTRO_COPY}, sourced distribution: $ENV{ROS_DISTRO}. Removed invalid configuration, please reconfigure project.")
  40. endif()
  41. # Add a custom target to always check during build if sourced and cached distributions match.
  42. add_custom_target(
  43. ROS2DistributionCheck ALL
  44. COMMENT "Checking if sourced ROS 2 distribution matches with the configuration"
  45. COMMAND ${CMAKE_COMMAND} -DROS_DISTRO=${ROS_DISTRO} -P ${CMAKE_CURRENT_SOURCE_DIR}/checkROS2Distribution.cmake
  46. )
  47. # Add the static target with the API Interface and the code
  48. # TODO: This target should be removed after API is fully implemented with buses
  49. ly_add_target(
  50. NAME ${gem_name}.Static STATIC
  51. NAMESPACE Gem
  52. FILES_CMAKE
  53. ros2_api_files.cmake
  54. ros2_shared_api_files.cmake
  55. INCLUDE_DIRECTORIES
  56. PUBLIC
  57. Include
  58. PRIVATE
  59. Source
  60. BUILD_DEPENDENCIES
  61. PUBLIC
  62. AZ::AzCore
  63. AZ::AzToolsFramework
  64. Gem::${gem_name}.Private.Object
  65. )
  66. # The ${gem_name}.API target declares the common interface that users of this gem should depend on in their targets
  67. ly_add_target(
  68. NAME ${gem_name}.API INTERFACE
  69. NAMESPACE Gem
  70. FILES_CMAKE
  71. ros2_api_files.cmake
  72. ${pal_dir}/ros2_api_files.cmake
  73. INCLUDE_DIRECTORIES
  74. INTERFACE
  75. Include
  76. BUILD_DEPENDENCIES
  77. INTERFACE
  78. AZ::AzCore
  79. )
  80. target_interface_depends_on_ros2_packages(${gem_name}.API rclcpp builtin_interfaces geometry_msgs)
  81. # The ${gem_name}.Private.Object target is an internal target
  82. # It should not be used outside of this Gems CMakeLists.txt
  83. ly_add_target(
  84. NAME ${gem_name}.Private.Object STATIC
  85. NAMESPACE Gem
  86. FILES_CMAKE
  87. ros2_private_files.cmake
  88. ${pal_dir}/ros2_private_files.cmake
  89. TARGET_PROPERTIES
  90. O3DE_PRIVATE_TARGET TRUE
  91. INCLUDE_DIRECTORIES
  92. PRIVATE
  93. Include
  94. Source
  95. BUILD_DEPENDENCIES
  96. PUBLIC
  97. AZ::AzCore
  98. AZ::AzFramework
  99. Gem::Atom_AtomBridge.Static # FollowingCameraComponent
  100. Gem::LevelGeoreferencing.API # ROS2SpawnerComponent
  101. )
  102. target_depends_on_ros2_packages(${gem_name}.Private.Object rclcpp builtin_interfaces std_msgs sensor_msgs nav_msgs tf2_ros ackermann_msgs gazebo_msgs vision_msgs control_msgs)
  103. # Here add ${gem_name} target, it depends on the Private Object library and Public API interface
  104. ly_add_target(
  105. NAME ${gem_name} ${PAL_TRAIT_MONOLITHIC_DRIVEN_MODULE_TYPE}
  106. NAMESPACE Gem
  107. FILES_CMAKE
  108. ros2_shared_files.cmake
  109. ${pal_dir}/ros2_shared_files.cmake
  110. INCLUDE_DIRECTORIES
  111. PUBLIC
  112. Include
  113. PRIVATE
  114. Source
  115. BUILD_DEPENDENCIES
  116. PUBLIC
  117. Gem::${gem_name}.API
  118. Gem::${gem_name}.Static
  119. PRIVATE
  120. Gem::${gem_name}.Private.Object
  121. )
  122. # Include the gem name into the Client Module source file
  123. # for use with the AZ_DECLARE_MODULE_CLASS macro
  124. # This is to allow renaming of the gem to also cause
  125. # the CreateModuleClass_Gem_<gem-name> function which
  126. # is used to bootstrap the gem in monolithic builds to link to the new gem name
  127. ly_add_source_properties(
  128. SOURCES
  129. Source/Clients/ROS2Module.cpp
  130. PROPERTY COMPILE_DEFINITIONS
  131. VALUES
  132. O3DE_GEM_NAME=${gem_name}
  133. O3DE_GEM_VERSION=${gem_version})
  134. # By default, we will specify that the above target ${gem_name} would be used by
  135. # Client and Server type targets when this gem is enabled. If you don't want it
  136. # active in Clients or Servers by default, delete one of both of the following lines:
  137. ly_create_alias(NAME ${gem_name}.Clients NAMESPACE Gem TARGETS Gem::${gem_name})
  138. ly_create_alias(NAME ${gem_name}.Servers NAMESPACE Gem TARGETS Gem::${gem_name})
  139. ly_create_alias(NAME ${gem_name}.Unified NAMESPACE Gem TARGETS Gem::${gem_name})
  140. # For the Client and Server variants of ${gem_name} Gem, an alias to the ${gem_name}.API target will be made
  141. ly_create_alias(NAME ${gem_name}.Clients.API NAMESPACE Gem TARGETS Gem::${gem_name}.API)
  142. ly_create_alias(NAME ${gem_name}.Servers.API NAMESPACE Gem TARGETS Gem::${gem_name}.API)
  143. ly_create_alias(NAME ${gem_name}.Unified.API NAMESPACE Gem TARGETS Gem::${gem_name}.API)
  144. # Add in CMake dependencies for each gem dependency listed in this gem's gem.json file
  145. # for the Clients, Servers, Unified gem variants
  146. o3de_add_variant_dependencies_for_gem_dependencies(GEM_NAME ${gem_name} VARIANTS Clients Servers Unified)
  147. # If we are on a host platform, we want to add the host tools targets like the ${gem_name}.Editor MODULE target
  148. if(PAL_TRAIT_BUILD_HOST_TOOLS)
  149. # The ${gem_name}.Editor.API target can be used by other gems that want to interact with the ${gem_name}.Editor module
  150. ly_add_target(
  151. NAME ${gem_name}.Editor.API INTERFACE
  152. NAMESPACE Gem
  153. FILES_CMAKE
  154. ros2_editor_api_files.cmake
  155. ${pal_dir}/ros2_editor_api_files.cmake
  156. INCLUDE_DIRECTORIES
  157. INTERFACE
  158. Include
  159. BUILD_DEPENDENCIES
  160. INTERFACE
  161. AZ::AzToolsFramework
  162. )
  163. # The ${gem_name}.Editor.Private.Object target is an internal target
  164. # which is only to be used by this gems CMakeLists.txt and any subdirectories
  165. # Other gems should not use this target
  166. ly_add_target(
  167. NAME ${gem_name}.Editor.Private.Object STATIC
  168. NAMESPACE Gem
  169. FILES_CMAKE
  170. ros2_editor_private_files.cmake
  171. TARGET_PROPERTIES
  172. O3DE_PRIVATE_TARGET TRUE
  173. INCLUDE_DIRECTORIES
  174. PRIVATE
  175. Include
  176. Source
  177. COMPILE_DEFINITIONS
  178. PRIVATE
  179. ROS2_EDITOR
  180. BUILD_DEPENDENCIES
  181. PUBLIC
  182. AZ::AzToolsFramework
  183. ${gem_name}.Private.Object
  184. ${gem_name}.Static
  185. )
  186. ly_add_target(
  187. NAME ${gem_name}.Editor GEM_MODULE
  188. NAMESPACE Gem
  189. AUTOMOC
  190. FILES_CMAKE
  191. ros2_editor_shared_files.cmake
  192. INCLUDE_DIRECTORIES
  193. PRIVATE
  194. Source
  195. PUBLIC
  196. Include
  197. BUILD_DEPENDENCIES
  198. PUBLIC
  199. Gem::${gem_name}.Editor.API
  200. PRIVATE
  201. Gem::${gem_name}.Editor.Private.Object
  202. )
  203. # Include the gem name into the Editor Module source file
  204. # for use with the AZ_DECLARE_MODULE_CLASS macro
  205. # This is to allow renaming of the gem to also cause
  206. # the CreateModuleClass_Gem_<gem-name> function which
  207. # is used to bootstrap the gem in monolithic builds to link to the new gem name
  208. ly_add_source_properties(
  209. SOURCES
  210. Source/Tools/ROS2EditorModule.cpp
  211. PROPERTY COMPILE_DEFINITIONS
  212. VALUES
  213. O3DE_GEM_NAME=${gem_name}
  214. O3DE_GEM_VERSION=${gem_version})
  215. # By default, we will specify that the above target ${gem_name} would be used by
  216. # Tool and Builder type targets when this gem is enabled. If you don't want it
  217. # active in Tools or Builders by default, delete one of both of the following lines:
  218. ly_create_alias(NAME ${gem_name}.Tools NAMESPACE Gem TARGETS Gem::${gem_name}.Editor)
  219. ly_create_alias(NAME ${gem_name}.Builders NAMESPACE Gem TARGETS Gem::${gem_name}.Editor)
  220. # For the Tools and Builders variants of ${gem_name} Gem, an alias to the ${gem_name}.Editor API target will be made
  221. ly_create_alias(NAME ${gem_name}.Tools.API NAMESPACE Gem TARGETS Gem::${gem_name}.Editor.API)
  222. ly_create_alias(NAME ${gem_name}.Builders.API NAMESPACE Gem TARGETS Gem::${gem_name}.Editor.API)
  223. # Add in CMake dependencies for each gem dependency listed in this gem's gem.json file
  224. # for the Tools and Builders gem variants
  225. o3de_add_variant_dependencies_for_gem_dependencies(GEM_NAME ${gem_name} VARIANTS Tools Builders)
  226. endif()
  227. ################################################################################
  228. # Tests
  229. ################################################################################
  230. # See if globally, tests are supported
  231. if(PAL_TRAIT_BUILD_TESTS_SUPPORTED)
  232. # We globally support tests, see if we support tests on this platform for ${gem_name}.Tests
  233. if(PAL_TRAIT_ROS2_TEST_SUPPORTED)
  234. # We support ${gem_name}.Tests on this platform, add dependency on the Private Object target
  235. ly_add_target(
  236. NAME ${gem_name}.Tests ${PAL_TRAIT_TEST_TARGET_TYPE}
  237. NAMESPACE Gem
  238. FILES_CMAKE
  239. ros2_tests_files.cmake
  240. INCLUDE_DIRECTORIES
  241. PRIVATE
  242. Tests
  243. Source
  244. Include
  245. BUILD_DEPENDENCIES
  246. PRIVATE
  247. AZ::AzTest
  248. AZ::AzFramework
  249. Gem::${gem_name}.Private.Object
  250. )
  251. # Add ${gem_name}.Tests to googletest
  252. ly_add_googletest(
  253. NAME Gem::${gem_name}.Tests
  254. )
  255. endif()
  256. # If we are a host platform we want to add tools test like editor tests here
  257. if(PAL_TRAIT_BUILD_HOST_TOOLS)
  258. # We are a host platform, see if Editor tests are supported on this platform
  259. if(PAL_TRAIT_ROS2_EDITOR_TEST_SUPPORTED)
  260. # We support ${gem_name}.Editor.Tests on this platform, add ${gem_name}.Editor.Tests target which depends on
  261. # private ${gem_name}.Editor.Private.Object target
  262. ly_add_target(
  263. NAME ${gem_name}.Editor.Tests ${PAL_TRAIT_TEST_TARGET_TYPE}
  264. NAMESPACE Gem
  265. FILES_CMAKE
  266. ros2_editor_tests_files.cmake
  267. INCLUDE_DIRECTORIES
  268. PRIVATE
  269. Tests
  270. Source
  271. Include
  272. BUILD_DEPENDENCIES
  273. PRIVATE
  274. AZ::AzTest
  275. AZ::AzTestShared
  276. Legacy::CryCommon
  277. Legacy::EditorCommon
  278. Legacy::Editor.Headers
  279. AZ::AzManipulatorTestFramework.Static
  280. Gem::${gem_name}.Editor.Private.Object
  281. Gem::${gem_name}.Static
  282. RUNTIME_DEPENDENCIES
  283. Legacy::Editor
  284. )
  285. # Add ${gem_name}.Editor.Tests to googletest
  286. ly_add_googletest(
  287. NAME Gem::${gem_name}.Editor.Tests
  288. )
  289. endif()
  290. endif()
  291. endif()