CMakeLists.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. message(FATAL_ERROR "The ROS2 Gem is not supported on ${PAL_PLATFORM_NAME}")
  19. return()
  20. endif()
  21. # If no ROS2 is found, no targets are valid for this Gem
  22. find_package(ROS2 MODULE)
  23. if (NOT ROS2_FOUND)
  24. message(FATAL_ERROR "Unable to detect a the ROS2 distribution on this system. Make sure it is installed and enabled.")
  25. return()
  26. endif()
  27. message(DEBUG "Building ${gem_name} Gem with ros2 $ENV{ROS_DISTRO}")
  28. # Check if ROS 2 distribution is cached
  29. get_property(ROS_DISTRO_TYPE CACHE ROS_DISTRO PROPERTY TYPE)
  30. # 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.
  31. if (NOT ROS_DISTRO_TYPE)
  32. set(ROS_DISTRO $ENV{ROS_DISTRO} CACHE STRING "ROS 2 distribution of current configuration" FORCE)
  33. elseif(NOT "$ENV{ROS_DISTRO}" STREQUAL "${ROS_DISTRO}")
  34. get_cmake_property(CACHED_VARIABLES CACHE_VARIABLES)
  35. set(ROS_DISTRO_COPY ${ROS_DISTRO})
  36. # Iterate over cached variables and unset them
  37. foreach(CACHED_VARIABLE ${CACHED_VARIABLES})
  38. unset(${CACHED_VARIABLE} CACHE)
  39. endforeach()
  40. 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.")
  41. endif()
  42. # Add a custom target to always check during build if sourced and cached distributions match.
  43. add_custom_target(
  44. ROS2DistributionCheck ALL
  45. COMMENT "Checking if sourced ROS 2 distribution matches with the configuration"
  46. COMMAND ${CMAKE_COMMAND} -DROS_DISTRO=${ROS_DISTRO} -P ${CMAKE_CURRENT_SOURCE_DIR}/checkROS2Distribution.cmake
  47. )
  48. # Gazebo messages are optional, so we will only add the dependents if the package is found.
  49. # The gazebo_msgs package is EOL and will not be available in ROS 2 Kilted Kaiju.
  50. # If you need to use ContactSensor and/or ROS2 Spawner, please consider building gazebo_msgs from the source.
  51. find_package(gazebo_msgs QUIET)
  52. if (gazebo_msgs_FOUND)
  53. message(STATUS "Found gazebo_msgs package, enabling legacy features like ContactSensor Component and ROS2 Spawner Component")
  54. SET (WITH_GAZEBO_MSGS TRUE)
  55. else()
  56. message(STATUS "Could not find gazebo_msgs package, disabling legacy features like ContactSensor Component and ROS2 Spawner Component")
  57. SET(WITH_GAZEBO_MSGS FALSE)
  58. endif()
  59. # Add the static target with the API Interface and the code
  60. # TODO: This target should be removed after API is fully implemented with buses
  61. ly_add_target(
  62. NAME ${gem_name}.Static STATIC
  63. NAMESPACE Gem
  64. FILES_CMAKE
  65. ros2_api_files.cmake
  66. ros2_shared_api_files.cmake
  67. INCLUDE_DIRECTORIES
  68. PUBLIC
  69. Include
  70. PRIVATE
  71. Source
  72. BUILD_DEPENDENCIES
  73. PUBLIC
  74. AZ::AzCore
  75. AZ::AzToolsFramework
  76. Gem::${gem_name}.Private.Object
  77. )
  78. ly_add_target(
  79. NAME ${gem_name}.Editor.Static STATIC
  80. NAMESPACE Gem
  81. FILES_CMAKE
  82. ros2_editor_api_files.cmake
  83. ros2_editor_shared_api_files.cmake
  84. INCLUDE_DIRECTORIES
  85. PUBLIC
  86. Include
  87. PRIVATE
  88. Source
  89. BUILD_DEPENDENCIES
  90. PUBLIC
  91. AZ::AzCore
  92. AZ::AzToolsFramework
  93. Gem::${gem_name}.Editor.Private.Object
  94. )
  95. # The ${gem_name}.API target declares the common interface that users of this gem should depend on in their targets
  96. ly_add_target(
  97. NAME ${gem_name}.API INTERFACE
  98. NAMESPACE Gem
  99. FILES_CMAKE
  100. ros2_api_files.cmake
  101. ${pal_dir}/ros2_api_files.cmake
  102. INCLUDE_DIRECTORIES
  103. INTERFACE
  104. Include
  105. BUILD_DEPENDENCIES
  106. INTERFACE
  107. AZ::AzCore
  108. )
  109. target_interface_depends_on_ros2_packages(${gem_name}.API rclcpp builtin_interfaces control_msgs geometry_msgs)
  110. # The ${gem_name}.Private.Object target is an internal target
  111. # It should not be used outside of this Gems CMakeLists.txt
  112. ly_add_target(
  113. NAME ${gem_name}.Private.Object STATIC
  114. NAMESPACE Gem
  115. FILES_CMAKE
  116. ros2_private_files.cmake
  117. ${pal_dir}/ros2_private_files.cmake
  118. TARGET_PROPERTIES
  119. O3DE_PRIVATE_TARGET TRUE
  120. INCLUDE_DIRECTORIES
  121. PRIVATE
  122. Include
  123. Source
  124. BUILD_DEPENDENCIES
  125. PUBLIC
  126. AZ::AzCore
  127. AZ::AzFramework
  128. Gem::Atom_AtomBridge.Static # FollowingCameraComponent
  129. Gem::LevelGeoreferencing.API # ROS2SpawnerComponent
  130. )
  131. target_depends_on_ros2_packages(${gem_name}.Private.Object rclcpp builtin_interfaces std_msgs sensor_msgs nav_msgs tf2_ros ackermann_msgs vision_msgs control_msgs)
  132. if (WITH_GAZEBO_MSGS)
  133. target_depends_on_ros2_package(${gem_name}.Private.Object gazebo_msgs REQUIRED)
  134. target_compile_definitions(${gem_name}.Private.Object PUBLIC "WITH_GAZEBO_MSGS")
  135. endif()
  136. # Here add ${gem_name} target, it depends on the Private Object library and Public API interface
  137. ly_add_target(
  138. NAME ${gem_name} ${PAL_TRAIT_MONOLITHIC_DRIVEN_MODULE_TYPE}
  139. NAMESPACE Gem
  140. FILES_CMAKE
  141. ros2_shared_files.cmake
  142. ${pal_dir}/ros2_shared_files.cmake
  143. INCLUDE_DIRECTORIES
  144. PUBLIC
  145. Include
  146. PRIVATE
  147. Source
  148. BUILD_DEPENDENCIES
  149. PUBLIC
  150. Gem::${gem_name}.API
  151. PRIVATE
  152. Gem::${gem_name}.Static
  153. Gem::${gem_name}.Private.Object
  154. )
  155. # Include the gem name into the Client Module source file
  156. # for use with the AZ_DECLARE_MODULE_CLASS macro
  157. # This is to allow renaming of the gem to also cause
  158. # the CreateModuleClass_Gem_<gem-name> function which
  159. # is used to bootstrap the gem in monolithic builds to link to the new gem name
  160. ly_add_source_properties(
  161. SOURCES
  162. Source/Clients/ROS2Module.cpp
  163. PROPERTY COMPILE_DEFINITIONS
  164. VALUES
  165. O3DE_GEM_NAME=${gem_name}
  166. O3DE_GEM_VERSION=${gem_version})
  167. # By default, we will specify that the above target ${gem_name} would be used by
  168. # Client and Server type targets when this gem is enabled. If you don't want it
  169. # active in Clients or Servers by default, delete one of both of the following lines:
  170. ly_create_alias(NAME ${gem_name}.Clients NAMESPACE Gem TARGETS Gem::${gem_name})
  171. ly_create_alias(NAME ${gem_name}.Servers NAMESPACE Gem TARGETS Gem::${gem_name})
  172. ly_create_alias(NAME ${gem_name}.Unified NAMESPACE Gem TARGETS Gem::${gem_name})
  173. # For the Client and Server variants of ${gem_name} Gem, an alias to the ${gem_name}.API target will be made
  174. ly_create_alias(NAME ${gem_name}.Clients.API NAMESPACE Gem TARGETS Gem::${gem_name}.API)
  175. ly_create_alias(NAME ${gem_name}.Servers.API NAMESPACE Gem TARGETS Gem::${gem_name}.API)
  176. ly_create_alias(NAME ${gem_name}.Unified.API NAMESPACE Gem TARGETS Gem::${gem_name}.API)
  177. # Add in CMake dependencies for each gem dependency listed in this gem's gem.json file
  178. # for the Clients, Servers, Unified gem variants
  179. o3de_add_variant_dependencies_for_gem_dependencies(GEM_NAME ${gem_name} VARIANTS Clients Servers Unified)
  180. # If we are on a host platform, we want to add the host tools targets like the ${gem_name}.Editor MODULE target
  181. if(PAL_TRAIT_BUILD_HOST_TOOLS)
  182. # The ${gem_name}.Editor.API target can be used by other gems that want to interact with the ${gem_name}.Editor module
  183. ly_add_target(
  184. NAME ${gem_name}.Editor.API INTERFACE
  185. NAMESPACE Gem
  186. FILES_CMAKE
  187. ros2_editor_api_files.cmake
  188. ${pal_dir}/ros2_editor_api_files.cmake
  189. INCLUDE_DIRECTORIES
  190. INTERFACE
  191. Include
  192. BUILD_DEPENDENCIES
  193. INTERFACE
  194. AZ::AzToolsFramework
  195. )
  196. # The ${gem_name}.Editor.Private.Object target is an internal target
  197. # which is only to be used by this gems CMakeLists.txt and any subdirectories
  198. # Other gems should not use this target
  199. ly_add_target(
  200. NAME ${gem_name}.Editor.Private.Object STATIC
  201. NAMESPACE Gem
  202. FILES_CMAKE
  203. ros2_editor_private_files.cmake
  204. TARGET_PROPERTIES
  205. O3DE_PRIVATE_TARGET TRUE
  206. INCLUDE_DIRECTORIES
  207. PRIVATE
  208. Include
  209. Source
  210. COMPILE_DEFINITIONS
  211. PRIVATE
  212. ROS2_EDITOR
  213. BUILD_DEPENDENCIES
  214. PUBLIC
  215. AZ::AzToolsFramework
  216. Gem::${gem_name}.Static
  217. )
  218. ly_add_target(
  219. NAME ${gem_name}.Editor GEM_MODULE
  220. NAMESPACE Gem
  221. AUTOMOC
  222. FILES_CMAKE
  223. ros2_editor_shared_files.cmake
  224. INCLUDE_DIRECTORIES
  225. PRIVATE
  226. Source
  227. PUBLIC
  228. Include
  229. BUILD_DEPENDENCIES
  230. PUBLIC
  231. Gem::${gem_name}.Editor.API
  232. PRIVATE
  233. Gem::${gem_name}.Editor.Private.Object
  234. Gem::${gem_name}.Editor.Static
  235. )
  236. # Include the gem name into the Editor Module source file
  237. # for use with the AZ_DECLARE_MODULE_CLASS macro
  238. # This is to allow renaming of the gem to also cause
  239. # the CreateModuleClass_Gem_<gem-name> function which
  240. # is used to bootstrap the gem in monolithic builds to link to the new gem name
  241. ly_add_source_properties(
  242. SOURCES
  243. Source/Tools/ROS2EditorModule.cpp
  244. PROPERTY COMPILE_DEFINITIONS
  245. VALUES
  246. O3DE_GEM_NAME=${gem_name}
  247. O3DE_GEM_VERSION=${gem_version})
  248. # By default, we will specify that the above target ${gem_name} would be used by
  249. # Tool and Builder type targets when this gem is enabled. If you don't want it
  250. # active in Tools or Builders by default, delete one of both of the following lines:
  251. ly_create_alias(NAME ${gem_name}.Tools NAMESPACE Gem TARGETS Gem::${gem_name}.Editor)
  252. ly_create_alias(NAME ${gem_name}.Builders NAMESPACE Gem TARGETS Gem::${gem_name}.Editor)
  253. # For the Tools and Builders variants of ${gem_name} Gem, an alias to the ${gem_name}.Editor API target will be made
  254. ly_create_alias(NAME ${gem_name}.Tools.API NAMESPACE Gem TARGETS Gem::${gem_name}.Editor.API)
  255. ly_create_alias(NAME ${gem_name}.Builders.API NAMESPACE Gem TARGETS Gem::${gem_name}.Editor.API)
  256. # Add in CMake dependencies for each gem dependency listed in this gem's gem.json file
  257. # for the Tools and Builders gem variants
  258. o3de_add_variant_dependencies_for_gem_dependencies(GEM_NAME ${gem_name} VARIANTS Tools Builders)
  259. endif()
  260. ################################################################################
  261. # Tests
  262. ################################################################################
  263. # See if globally, tests are supported
  264. if(PAL_TRAIT_BUILD_TESTS_SUPPORTED)
  265. # We globally support tests, see if we support tests on this platform for ${gem_name}.Tests
  266. if(PAL_TRAIT_ROS2_TEST_SUPPORTED)
  267. # We support ${gem_name}.Tests on this platform, add dependency on the Private Object target
  268. ly_add_target(
  269. NAME ${gem_name}.Tests ${PAL_TRAIT_TEST_TARGET_TYPE}
  270. NAMESPACE Gem
  271. FILES_CMAKE
  272. ros2_tests_files.cmake
  273. INCLUDE_DIRECTORIES
  274. PRIVATE
  275. Tests
  276. Source
  277. Include
  278. BUILD_DEPENDENCIES
  279. PRIVATE
  280. AZ::AzTest
  281. AZ::AzFramework
  282. Gem::${gem_name}.Private.Object
  283. )
  284. # Add ${gem_name}.Tests to googletest
  285. ly_add_googletest(
  286. NAME Gem::${gem_name}.Tests
  287. )
  288. endif()
  289. # If we are a host platform we want to add tools test like editor tests here
  290. if(PAL_TRAIT_BUILD_HOST_TOOLS)
  291. # We are a host platform, see if Editor tests are supported on this platform
  292. if(PAL_TRAIT_ROS2_EDITOR_TEST_SUPPORTED)
  293. # We support ${gem_name}.Editor.Tests on this platform, add ${gem_name}.Editor.Tests target which depends on
  294. # private ${gem_name}.Editor.Private.Object target
  295. ly_add_target(
  296. NAME ${gem_name}.Editor.Tests ${PAL_TRAIT_TEST_TARGET_TYPE}
  297. NAMESPACE Gem
  298. FILES_CMAKE
  299. ros2_editor_tests_files.cmake
  300. INCLUDE_DIRECTORIES
  301. PRIVATE
  302. Tests
  303. Source
  304. Include
  305. BUILD_DEPENDENCIES
  306. PRIVATE
  307. AZ::AzTest
  308. AZ::AzTestShared
  309. Legacy::CryCommon
  310. Legacy::EditorCommon
  311. Legacy::Editor.Headers
  312. AZ::AzManipulatorTestFramework.Static
  313. Gem::${gem_name}.Editor.Private.Object
  314. Gem::${gem_name}.Editor.Static
  315. Gem::${gem_name}.Static
  316. RUNTIME_DEPENDENCIES
  317. Legacy::Editor
  318. )
  319. # Add ${gem_name}.Editor.Tests to googletest
  320. ly_add_googletest(
  321. NAME Gem::${gem_name}.Editor.Tests
  322. )
  323. endif()
  324. endif()
  325. endif()