CMakeLists.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. ly_add_target(
  61. NAME ${gem_name}.Static STATIC
  62. NAMESPACE Gem
  63. PLATFORM_INCLUDE_FILES
  64. ${CMAKE_CURRENT_LIST_DIR}/Platform/Common/ros2_${PAL_TRAIT_COMPILER_ID_LOWERCASE}.cmake
  65. FILES_CMAKE
  66. ros2_api_files.cmake
  67. ros2_shared_api_files.cmake
  68. INCLUDE_DIRECTORIES
  69. PUBLIC
  70. Include
  71. PRIVATE
  72. Source
  73. BUILD_DEPENDENCIES
  74. PUBLIC
  75. AZ::AzCore
  76. Gem::${gem_name}.Private.Object
  77. )
  78. # The ${gem_name}.API target declares the common interface that users of this gem should depend on in their targets
  79. ly_add_target(
  80. NAME ${gem_name}.API INTERFACE
  81. NAMESPACE Gem
  82. FILES_CMAKE
  83. ros2_api_files.cmake
  84. ${pal_dir}/ros2_api_files.cmake
  85. INCLUDE_DIRECTORIES
  86. INTERFACE
  87. Include
  88. BUILD_DEPENDENCIES
  89. INTERFACE
  90. AZ::AzCore
  91. )
  92. target_interface_depends_on_ros2_packages(${gem_name}.API rclcpp builtin_interfaces control_msgs geometry_msgs)
  93. # The ${gem_name}.Private.Object target is an internal target
  94. # It should not be used outside of this Gems CMakeLists.txt
  95. ly_add_target(
  96. NAME ${gem_name}.Private.Object STATIC
  97. NAMESPACE Gem
  98. FILES_CMAKE
  99. ros2_private_files.cmake
  100. ${pal_dir}/ros2_private_files.cmake
  101. TARGET_PROPERTIES
  102. O3DE_PRIVATE_TARGET TRUE
  103. INCLUDE_DIRECTORIES
  104. PRIVATE
  105. Include
  106. Source
  107. BUILD_DEPENDENCIES
  108. PUBLIC
  109. AZ::AzCore
  110. AZ::AzFramework
  111. Gem::Atom_AtomBridge.Static # FollowingCameraComponent
  112. Gem::LevelGeoreferencing.API # ROS2SpawnerComponent
  113. )
  114. 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)
  115. if (WITH_GAZEBO_MSGS)
  116. target_depends_on_ros2_package(${gem_name}.Private.Object gazebo_msgs REQUIRED)
  117. target_compile_definitions(${gem_name}.Private.Object PUBLIC "WITH_GAZEBO_MSGS")
  118. endif()
  119. # Here add ${gem_name} target, it depends on the Private Object library and Public API interface
  120. ly_add_target(
  121. NAME ${gem_name} ${PAL_TRAIT_MONOLITHIC_DRIVEN_MODULE_TYPE}
  122. NAMESPACE Gem
  123. FILES_CMAKE
  124. ros2_shared_files.cmake
  125. ${pal_dir}/ros2_shared_files.cmake
  126. INCLUDE_DIRECTORIES
  127. PUBLIC
  128. Include
  129. PRIVATE
  130. Source
  131. BUILD_DEPENDENCIES
  132. PUBLIC
  133. Gem::${gem_name}.API
  134. PRIVATE
  135. Gem::${gem_name}.Static
  136. Gem::${gem_name}.Private.Object
  137. )
  138. # Include the gem name into the Client Module source file
  139. # for use with the AZ_DECLARE_MODULE_CLASS macro
  140. # This is to allow renaming of the gem to also cause
  141. # the CreateModuleClass_Gem_<gem-name> function which
  142. # is used to bootstrap the gem in monolithic builds to link to the new gem name
  143. ly_add_source_properties(
  144. SOURCES
  145. Source/Clients/ROS2Module.cpp
  146. PROPERTY COMPILE_DEFINITIONS
  147. VALUES
  148. O3DE_GEM_NAME=${gem_name}
  149. O3DE_GEM_VERSION=${gem_version})
  150. # By default, we will specify that the above target ${gem_name} would be used by
  151. # Client and Server type targets when this gem is enabled. If you don't want it
  152. # active in Clients or Servers by default, delete one of both of the following lines:
  153. ly_create_alias(NAME ${gem_name}.Clients NAMESPACE Gem TARGETS Gem::${gem_name})
  154. ly_create_alias(NAME ${gem_name}.Servers NAMESPACE Gem TARGETS Gem::${gem_name})
  155. ly_create_alias(NAME ${gem_name}.Unified NAMESPACE Gem TARGETS Gem::${gem_name})
  156. # For the Client and Server variants of ${gem_name} Gem, an alias to the ${gem_name}.API target will be made
  157. ly_create_alias(NAME ${gem_name}.Clients.API NAMESPACE Gem TARGETS Gem::${gem_name}.API)
  158. ly_create_alias(NAME ${gem_name}.Servers.API NAMESPACE Gem TARGETS Gem::${gem_name}.API)
  159. ly_create_alias(NAME ${gem_name}.Unified.API NAMESPACE Gem TARGETS Gem::${gem_name}.API)
  160. # Add in CMake dependencies for each gem dependency listed in this gem's gem.json file
  161. # for the Clients, Servers, Unified gem variants
  162. o3de_add_variant_dependencies_for_gem_dependencies(GEM_NAME ${gem_name} VARIANTS Clients Servers Unified)
  163. # If we are on a host platform, we want to add the host tools targets like the ${gem_name}.Editor MODULE target
  164. if(PAL_TRAIT_BUILD_HOST_TOOLS)
  165. # The ${gem_name}.Editor.API target can be used by other gems that want to interact with the ${gem_name}.Editor module
  166. ly_add_target(
  167. NAME ${gem_name}.Editor.API INTERFACE
  168. NAMESPACE Gem
  169. FILES_CMAKE
  170. ros2_editor_api_files.cmake
  171. ${pal_dir}/ros2_editor_api_files.cmake
  172. INCLUDE_DIRECTORIES
  173. INTERFACE
  174. Include
  175. BUILD_DEPENDENCIES
  176. INTERFACE
  177. AZ::AzToolsFramework
  178. )
  179. # The ${gem_name}.Editor.Private.Object target is an internal target
  180. # which is only to be used by this gems CMakeLists.txt and any subdirectories
  181. # Other gems should not use this target
  182. ly_add_target(
  183. NAME ${gem_name}.Editor.Private.Object STATIC
  184. NAMESPACE Gem
  185. FILES_CMAKE
  186. ros2_editor_private_files.cmake
  187. TARGET_PROPERTIES
  188. O3DE_PRIVATE_TARGET TRUE
  189. INCLUDE_DIRECTORIES
  190. PRIVATE
  191. Include
  192. Source
  193. COMPILE_DEFINITIONS
  194. PRIVATE
  195. ROS2_EDITOR
  196. BUILD_DEPENDENCIES
  197. PUBLIC
  198. AZ::AzToolsFramework
  199. Gem::${gem_name}.Static
  200. )
  201. ly_add_target(
  202. NAME ${gem_name}.Editor GEM_MODULE
  203. NAMESPACE Gem
  204. AUTOMOC
  205. FILES_CMAKE
  206. ros2_editor_shared_files.cmake
  207. INCLUDE_DIRECTORIES
  208. PRIVATE
  209. Source
  210. PUBLIC
  211. Include
  212. BUILD_DEPENDENCIES
  213. PUBLIC
  214. Gem::${gem_name}.Editor.API
  215. PRIVATE
  216. Gem::${gem_name}.Editor.Private.Object
  217. )
  218. # Include the gem name into the Editor Module source file
  219. # for use with the AZ_DECLARE_MODULE_CLASS macro
  220. # This is to allow renaming of the gem to also cause
  221. # the CreateModuleClass_Gem_<gem-name> function which
  222. # is used to bootstrap the gem in monolithic builds to link to the new gem name
  223. ly_add_source_properties(
  224. SOURCES
  225. Source/Tools/ROS2EditorModule.cpp
  226. PROPERTY COMPILE_DEFINITIONS
  227. VALUES
  228. O3DE_GEM_NAME=${gem_name}
  229. O3DE_GEM_VERSION=${gem_version})
  230. # By default, we will specify that the above target ${gem_name} would be used by
  231. # Tool and Builder type targets when this gem is enabled. If you don't want it
  232. # active in Tools or Builders by default, delete one of both of the following lines:
  233. ly_create_alias(NAME ${gem_name}.Tools NAMESPACE Gem TARGETS Gem::${gem_name}.Editor)
  234. ly_create_alias(NAME ${gem_name}.Builders NAMESPACE Gem TARGETS Gem::${gem_name}.Editor)
  235. # For the Tools and Builders variants of ${gem_name} Gem, an alias to the ${gem_name}.Editor API target will be made
  236. ly_create_alias(NAME ${gem_name}.Tools.API NAMESPACE Gem TARGETS Gem::${gem_name}.Editor.API)
  237. ly_create_alias(NAME ${gem_name}.Builders.API NAMESPACE Gem TARGETS Gem::${gem_name}.Editor.API)
  238. # Add in CMake dependencies for each gem dependency listed in this gem's gem.json file
  239. # for the Tools and Builders gem variants
  240. o3de_add_variant_dependencies_for_gem_dependencies(GEM_NAME ${gem_name} VARIANTS Tools Builders)
  241. endif()
  242. ################################################################################
  243. # Tests
  244. ################################################################################
  245. # See if globally, tests are supported
  246. if(PAL_TRAIT_BUILD_TESTS_SUPPORTED)
  247. # We globally support tests, see if we support tests on this platform for ${gem_name}.Tests
  248. if(PAL_TRAIT_ROS2_TEST_SUPPORTED)
  249. # We support ${gem_name}.Tests on this platform, add dependency on the Private Object target
  250. ly_add_target(
  251. NAME ${gem_name}.Tests ${PAL_TRAIT_TEST_TARGET_TYPE}
  252. NAMESPACE Gem
  253. FILES_CMAKE
  254. ros2_tests_files.cmake
  255. INCLUDE_DIRECTORIES
  256. PRIVATE
  257. Tests
  258. Source
  259. Include
  260. BUILD_DEPENDENCIES
  261. PRIVATE
  262. AZ::AzTest
  263. AZ::AzFramework
  264. Gem::${gem_name}.Private.Object
  265. )
  266. # Add ${gem_name}.Tests to googletest
  267. ly_add_googletest(
  268. NAME Gem::${gem_name}.Tests
  269. )
  270. endif()
  271. # If we are a host platform we want to add tools test like editor tests here
  272. if(PAL_TRAIT_BUILD_HOST_TOOLS)
  273. # We are a host platform, see if Editor tests are supported on this platform
  274. if(PAL_TRAIT_ROS2_EDITOR_TEST_SUPPORTED)
  275. # We support ${gem_name}.Editor.Tests on this platform, add ${gem_name}.Editor.Tests target which depends on
  276. # private ${gem_name}.Editor.Private.Object target
  277. ly_add_target(
  278. NAME ${gem_name}.Editor.Tests ${PAL_TRAIT_TEST_TARGET_TYPE}
  279. NAMESPACE Gem
  280. FILES_CMAKE
  281. ros2_editor_tests_files.cmake
  282. INCLUDE_DIRECTORIES
  283. PRIVATE
  284. Tests
  285. Source
  286. Include
  287. BUILD_DEPENDENCIES
  288. PRIVATE
  289. AZ::AzTest
  290. AZ::AzManipulatorTestFramework.Static
  291. Gem::${gem_name}.Editor.Private.Object
  292. )
  293. # Add ${gem_name}.Editor.Tests to googletest
  294. ly_add_googletest(
  295. NAME Gem::${gem_name}.Editor.Tests
  296. )
  297. endif()
  298. endif()
  299. endif()