CMakeLists.txt 13 KB

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