LYWrappers.cmake 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. set(LY_UNITY_BUILD ON CACHE BOOL "UNITY builds")
  9. include(CMakeFindDependencyMacro)
  10. include(cmake/LyAutoGen.cmake)
  11. o3de_pal_dir(pal_dir ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Platform/${PAL_PLATFORM_NAME} "${O3DE_ENGINE_RESTRICTED_PATH}" "${LY_ROOT_FOLDER}")
  12. include(${pal_dir}/LYWrappers_${PAL_PLATFORM_NAME_LOWERCASE}.cmake)
  13. # Not all platforms support unity builds
  14. if(LY_UNITY_BUILD AND NOT PAL_TRAIT_BUILD_UNITY_SUPPORTED)
  15. message(ERROR "LY_UNITY_BUILD is specified, but not supported for the current target platform")
  16. endif()
  17. define_property(TARGET PROPERTY GEM_MODULE
  18. BRIEF_DOCS "Defines a MODULE library as a Gem"
  19. FULL_DOCS [[
  20. Property which is set on targets that should be seen as gems
  21. This is used to determine whether this target should load as
  22. when used as a runtime dependency should load at the same time
  23. as its dependee
  24. ]]
  25. )
  26. define_property(TARGET PROPERTY RUNTIME_DEPENDENCIES_DEPENDS
  27. BRIEF_DOCS "Defines the dependencies the runtime dependencies of a target has"
  28. FULL_DOCS [[
  29. Property which is queried through generator expressions at the moment
  30. the target is declared so a custom command that will do the copies can
  31. be generated later. Custom commands need to be declared in the same folder
  32. the target is declared, however, runtime dependencies need all targets
  33. to be declared, so it is done towards the end of CMake parsing. When
  34. runtime dependencies are processed, this target property is filled so the
  35. right dependencies are set for the custom command.
  36. This property contains all the files that are going to be copied to the output
  37. when the target gets built.
  38. ]]
  39. )
  40. #! ly_add_target: adds a target and provides parameters for the common configurations.
  41. #
  42. # Adds a target (static/dynamic library, executable) and convenient wrappers around most
  43. # common parameters that need to be set.
  44. # This function also creates an interface to use for dependencies. The interface will be
  45. # named as "NAMESPACE::NAME"
  46. # Some examples:
  47. # ly_add_target(NAME mystaticlib STATIC FILES_CMAKE somestatic_files.cmake)
  48. # ly_add_target(NAME mydynamiclib SHARED FILES_CMAKE somedyn_files.cmake)
  49. # ly_add_target(NAME myexecutable EXECUTABLE FILES_CMAKE someexe_files.cmake)
  50. #
  51. # \arg:NAME name of the target
  52. # \arg:STATIC (bool) defines this target to be a static library
  53. # \arg:GEM_STATIC (bool) defines this target to be a static library while also setting the GEM_MODULE property
  54. # \arg:SHARED (bool) defines this target to be a dynamic library
  55. # \arg:GEM_SHARED (bool) defines this target to be a dynamic library while also setting the GEM_MODULE property
  56. # \arg:MODULE (bool) defines this target to be a module library
  57. # \arg:GEM_MODULE (bool) defines this target to be a module library while also marking the target as a "Gem" via the GEM_MODULE property
  58. # \arg:OBJECT (bool) defines this target to be an object library
  59. # \arg:INTERFACE (bool) defines this target to be an interface library. A ${NAME}_HEADERS project will be created for the IDE
  60. # The HEADERONLY option can be specified as an alternative
  61. # \arg:EXECUTABLE (bool) defines this target to be an executable
  62. # \arg:APPLICATION (bool) defines this target to be an application (executable that is not a console)
  63. # \arg:IMPORTED (bool) defines this target to be imported.
  64. # \arg:NAMESPACE namespace declaration for this target. It will be used for IDE and dependencies
  65. # \arg:OUTPUT_NAME (optional) overrides the name of the output target. If not specified, the name will be used.
  66. # \arg:OUTPUT_SUBDIRECTORY places the runtime binary in a subfolder within the output folder (this only affects to runtime binaries)
  67. # \arg:AUTOMOC enables Qt moc in the target
  68. # \arg:AUTOUIC enables Qt uic in the target
  69. # \arg:AUTORCC enables Qt rcc in the target
  70. # \arg:NO_UNITY Prevent the target from employing unity builds even when unity builds (LY_UNITY_BUILD) are enabled
  71. # \arg:FILES_CMAKE list of *_files.cmake files that contain files for this target
  72. # \arg:GENERATED_FILES list of files to add to this target that are generated out of some other task
  73. # \arg:INCLUDE_DIRECTORIES list of directories to use as include paths
  74. # \arg:BUILD_DEPENDENCIES list of interfaces this target depends on (could be a compilation dependency
  75. # if the dependency is only exposing an include path, or could be a linking
  76. # dependency is exposing a lib)
  77. # \arg:RUNTIME_DEPENDENCIES list of dependencies this target depends on at runtime
  78. # \arg:COMPILE_DEFINITIONS list of compilation definitions this target will use to compile
  79. # \arg:PLATFORM_INCLUDE_FILES *.cmake files which should contain platform specific configuration to be added to the target
  80. # Look at the documentation for the ly_configure_target_platform_properties() function below
  81. # for the list of variables that will be used by the target
  82. # \arg:TARGET_PROPERTIES additional properties to set to the target
  83. # \arg:AUTOGEN_RULES a set of AutoGeneration rules to be passed to the AzAutoGen expansion system
  84. function(ly_add_target)
  85. set(options STATIC SHARED MODULE GEM_STATIC GEM_MODULE OBJECT HEADERONLY EXECUTABLE APPLICATION IMPORTED AUTOMOC AUTOUIC AUTORCC NO_UNITY EXPORT_ALL_SYMBOLS)
  86. set(oneValueArgs NAME NAMESPACE OUTPUT_SUBDIRECTORY OUTPUT_NAME)
  87. set(multiValueArgs FILES_CMAKE GENERATED_FILES INCLUDE_DIRECTORIES COMPILE_DEFINITIONS BUILD_DEPENDENCIES RUNTIME_DEPENDENCIES PLATFORM_INCLUDE_FILES TARGET_PROPERTIES AUTOGEN_RULES)
  88. cmake_parse_arguments(ly_add_target "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  89. # Since the term "INTERFACE" used in other context such as INCLUDE_DIRECTORIES, COMPILE_DEFINITIONS to specifiy property visibility
  90. # It needs to be parsed after those arguments have been parsed to avoid the usage of INTERFACE as a visibility scope
  91. cmake_parse_arguments(ly_add_target "INTERFACE" "" "" ${ly_add_target_UNPARSED_ARGUMENTS})
  92. # Validate input arguments
  93. if(NOT ly_add_target_NAME)
  94. message(FATAL_ERROR "You must provide a name for the target")
  95. endif()
  96. # Map HEADERONLY option to INTERFACE
  97. if(ly_add_target_HEADERONLY)
  98. set(ly_add_target_INTERFACE ly_add_target_HEADERONLY)
  99. endif()
  100. if(NOT ly_add_target_IMPORTED AND NOT ly_add_target_INTERFACE)
  101. if(NOT ly_add_target_FILES_CMAKE)
  102. message(FATAL_ERROR "You must provide a list of _files.cmake files for the target")
  103. endif()
  104. endif()
  105. # If the GEM_MODULE tag is passed set the normal MODULE argument
  106. if(ly_add_target_GEM_MODULE)
  107. set(ly_add_target_MODULE ${ly_add_target_GEM_MODULE})
  108. endif()
  109. # If the GEM_STATIC tag is passed mark the target as STATIC
  110. if(ly_add_target_GEM_STATIC)
  111. set(ly_add_target_STATIC ${ly_add_target_GEM_STATIC})
  112. endif()
  113. # If the GEM_SHARED tag is passed mark the target as SHARED
  114. if(ly_add_target_GEM_SHARED)
  115. set(ly_add_target_SHARED ${ly_add_target_GEM_SHARED})
  116. endif()
  117. foreach(file_cmake ${ly_add_target_FILES_CMAKE})
  118. ly_include_cmake_file_list(${file_cmake})
  119. endforeach()
  120. unset(linking_options)
  121. unset(linking_count)
  122. unset(target_type_options)
  123. if(ly_add_target_STATIC)
  124. set(linking_options STATIC)
  125. set(target_type_options STATIC)
  126. set(linking_count "${linking_count}1")
  127. endif()
  128. if(ly_add_target_OBJECT)
  129. set(linking_options OBJECT)
  130. set(target_type_options OBJECT)
  131. set(linking_count "${linking_count}1")
  132. endif()
  133. if(ly_add_target_SHARED)
  134. set(linking_options SHARED)
  135. set(target_type_options SHARED)
  136. set(linking_count "${linking_count}1")
  137. endif()
  138. if(ly_add_target_MODULE)
  139. set(linking_options ${PAL_LINKOPTION_MODULE})
  140. set(target_type_options ${PAL_LINKOPTION_MODULE})
  141. set(linking_count "${linking_count}1")
  142. endif()
  143. if(ly_add_target_INTERFACE)
  144. set(linking_options INTERFACE)
  145. set(target_type_options INTERFACE)
  146. set(linking_count "${linking_count}1")
  147. endif()
  148. if(ly_add_target_EXECUTABLE)
  149. set(linking_options EXECUTABLE)
  150. set(linking_count "${linking_count}1")
  151. endif()
  152. if(ly_add_target_APPLICATION)
  153. set(linking_options APPLICATION)
  154. set(linking_count "${linking_count}1")
  155. endif()
  156. if(NOT ("${linking_count}" STREQUAL "1"))
  157. message(FATAL_ERROR "More than one of the following options [STATIC | SHARED | MODULE | OBJECT | INTERFACE | EXECUTABLE | APPLICATION ] was specified and they are mutually exclusive")
  158. endif()
  159. if(ly_add_target_IMPORTED)
  160. list(APPEND target_type_options IMPORTED GLOBAL)
  161. endif()
  162. if(ly_add_target_NAMESPACE)
  163. set(interface_name "${ly_add_target_NAMESPACE}::${ly_add_target_NAME}")
  164. else()
  165. set(interface_name "${ly_add_target_NAME}")
  166. endif()
  167. set(project_NAME ${ly_add_target_NAME})
  168. if(ly_add_target_EXECUTABLE)
  169. add_executable(${ly_add_target_NAME}
  170. ${target_type_options}
  171. ${ALLFILES} ${ly_add_target_GENERATED_FILES}
  172. )
  173. ly_apply_platform_properties(${ly_add_target_NAME})
  174. if(ly_add_target_IMPORTED)
  175. set_target_properties(${ly_add_target_NAME} PROPERTIES LINKER_LANGUAGE CXX)
  176. endif()
  177. elseif(ly_add_target_APPLICATION)
  178. add_executable(${ly_add_target_NAME}
  179. ${target_type_options}
  180. ${PAL_EXECUTABLE_APPLICATION_FLAG}
  181. ${ALLFILES} ${ly_add_target_GENERATED_FILES}
  182. )
  183. ly_apply_platform_properties(${ly_add_target_NAME})
  184. if(ly_add_target_IMPORTED)
  185. set_target_properties(${ly_add_target_NAME} PROPERTIES LINKER_LANGUAGE CXX)
  186. endif()
  187. elseif(ly_add_target_INTERFACE)
  188. add_library(${ly_add_target_NAME}
  189. ${target_type_options}
  190. ${ALLFILES} ${ly_add_target_GENERATED_FILES}
  191. )
  192. else()
  193. add_library(${ly_add_target_NAME}
  194. ${target_type_options}
  195. ${ALLFILES} ${ly_add_target_GENERATED_FILES}
  196. )
  197. ly_apply_platform_properties(${ly_add_target_NAME})
  198. endif()
  199. if(${ly_add_target_GENERATED_FILES})
  200. set_source_files_properties(${ly_add_target_GENERATED_FILES}
  201. PROPERTIES GENERATED TRUE
  202. )
  203. endif()
  204. if(${ly_add_target_EXECUTABLE} OR ${ly_add_target_APPLICATION})
  205. add_executable(${interface_name} ALIAS ${ly_add_target_NAME})
  206. else()
  207. add_library(${interface_name} ALIAS ${ly_add_target_NAME})
  208. endif()
  209. if(ly_add_target_OUTPUT_NAME)
  210. set_target_properties(${ly_add_target_NAME} PROPERTIES
  211. OUTPUT_NAME ${ly_add_target_OUTPUT_NAME}
  212. )
  213. endif()
  214. # Add the target dependencies so they can be walked later
  215. if(ly_add_target_BUILD_DEPENDENCIES)
  216. set_property(GLOBAL APPEND PROPERTY LY_ALL_TARGETS_${ly_add_target_NAME}_BUILD_DEPENDENCIES ${ly_add_target_BUILD_DEPENDENCIES})
  217. endif()
  218. if(ly_add_target_RUNTIME_DEPENDENCIES)
  219. set_property(GLOBAL APPEND PROPERTY LY_ALL_TARGETS_${ly_add_target_NAME}_RUNTIME_DEPENDENCIES ${ly_add_target_RUNTIME_DEPENDENCIES})
  220. endif()
  221. if (ly_add_target_SHARED OR ly_add_target_MODULE OR ly_add_target_EXECUTABLE OR ly_add_target_APPLICATION)
  222. if (ly_add_target_OUTPUT_SUBDIRECTORY)
  223. ly_handle_custom_output_directory(${ly_add_target_NAME} ${ly_add_target_OUTPUT_SUBDIRECTORY})
  224. else()
  225. ly_handle_custom_output_directory(${ly_add_target_NAME} "")
  226. endif()
  227. endif()
  228. if(ly_add_target_GEM_MODULE OR ly_add_target_GEM_STATIC OR ly_add_target_GEM_SHARED)
  229. set_target_properties(${ly_add_target_NAME} PROPERTIES GEM_MODULE TRUE)
  230. endif()
  231. if (ly_add_target_INCLUDE_DIRECTORIES)
  232. target_include_directories(${ly_add_target_NAME}
  233. ${ly_add_target_INCLUDE_DIRECTORIES}
  234. )
  235. endif()
  236. ly_apply_debug_strip_options(${ly_add_target_NAME})
  237. # Parse the 3rdParty library dependencies
  238. ly_parse_third_party_dependencies("${ly_add_target_BUILD_DEPENDENCIES}")
  239. ly_target_link_libraries(${ly_add_target_NAME}
  240. ${ly_add_target_BUILD_DEPENDENCIES}
  241. )
  242. if(ly_add_target_COMPILE_DEFINITIONS)
  243. target_compile_definitions(${ly_add_target_NAME}
  244. ${ly_add_target_COMPILE_DEFINITIONS}
  245. )
  246. endif()
  247. if(ly_add_target_SHARED AND ly_add_target_EXPORT_ALL_SYMBOLS)
  248. if(PAL_PLATFORM_NAME STREQUAL "Windows")
  249. set_target_properties(${ly_add_target_NAME} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
  250. else()
  251. target_compile_options(${ly_add_target_NAME} PRIVATE ${PAL_TRAIT_EXPORT_ALL_SYMBOLS_COMPILE_OPTIONS})
  252. endif()
  253. endif()
  254. # For any target that depends on AzTest and is built as an executable, an additional 'AZ_TEST_EXECUTABLE' define will
  255. # enable the 'AZ_UNIT_TEST_HOOK' macro to also implement main() so that running the executable directly will run
  256. # the AZ_UNIT_TEST_HOOK function
  257. if (${linking_options} STREQUAL "EXECUTABLE" AND "AZ::AzTest" IN_LIST ly_add_target_BUILD_DEPENDENCIES)
  258. target_compile_definitions(${ly_add_target_NAME}
  259. PRIVATE
  260. AZ_TEST_EXECUTABLE
  261. )
  262. endif()
  263. if(ly_add_target_TARGET_PROPERTIES)
  264. set_target_properties(${ly_add_target_NAME} PROPERTIES
  265. ${ly_add_target_TARGET_PROPERTIES})
  266. endif()
  267. set(unity_target_types SHARED MODULE EXECUTABLE APPLICATION STATIC OBJECT)
  268. if(linking_options IN_LIST unity_target_types)
  269. # For eligible target types, if unity builds (LY_UNITY_BUILD) is enabled and the target is not marked with 'NO_UNITY',
  270. # enable UNITY builds individually for the target
  271. if(LY_UNITY_BUILD AND NOT ${ly_add_target_NO_UNITY})
  272. set_target_properties(${ly_add_target_NAME} PROPERTIES
  273. UNITY_BUILD ON
  274. UNITY_BUILD_MODE BATCH
  275. )
  276. endif()
  277. endif()
  278. # IDE organization
  279. ly_source_groups_from_folders("${ALLFILES}")
  280. source_group("Generated Files" REGULAR_EXPRESSION "(${CMAKE_BINARY_DIR})") # Any file coming from the output folder
  281. ly_get_vs_folder_directory(${CMAKE_CURRENT_SOURCE_DIR} ide_path)
  282. set_property(TARGET ${project_NAME} PROPERTY FOLDER ${ide_path})
  283. if(ly_add_target_RUNTIME_DEPENDENCIES)
  284. ly_parse_third_party_dependencies("${ly_add_target_RUNTIME_DEPENDENCIES}")
  285. ly_add_dependencies(${ly_add_target_NAME} ${ly_add_target_RUNTIME_DEPENDENCIES})
  286. endif()
  287. ly_configure_target_platform_properties()
  288. # Handle Qt MOC, RCC, UIC
  289. # https://gitlab.kitware.com/cmake/cmake/issues/18749
  290. # AUTOMOC is supposed to always rebuild because it checks files that are not listed in the sources (like extra
  291. # "_p.h" headers) and which may change outside the visibility of the generator.
  292. # We are not using AUTOUIC because of:
  293. # https://gitlab.kitware.com/cmake/cmake/-/issues/18741
  294. # To overcome this problem, we manually wrap all the ui files listed in the target with qt5_wrap_ui
  295. foreach(prop IN ITEMS AUTOMOC AUTORCC)
  296. if(${ly_add_target_${prop}})
  297. set_property(TARGET ${ly_add_target_NAME} PROPERTY ${prop} ON)
  298. endif()
  299. endforeach()
  300. if(${ly_add_target_AUTOUIC})
  301. get_target_property(all_ui_sources ${ly_add_target_NAME} SOURCES)
  302. list(FILTER all_ui_sources INCLUDE REGEX "^.*\\.ui$")
  303. if(NOT all_ui_sources)
  304. message(FATAL_ERROR "Target ${ly_add_target_NAME} contains AUTOUIC but doesnt have any .ui file")
  305. endif()
  306. ly_qt_uic_target(${ly_add_target_NAME})
  307. endif()
  308. # Add dependencies that were added before this target was available
  309. get_property(additional_dependencies GLOBAL PROPERTY LY_DELAYED_DEPENDENCIES_${ly_add_target_NAME})
  310. if(additional_dependencies)
  311. ly_add_dependencies(${ly_add_target_NAME} ${additional_dependencies})
  312. # Clear the variable so we can track issues in case some dependency is added after
  313. set_property(GLOBAL PROPERTY LY_DELAYED_DEPENDENCIES_${ly_add_target_NAME})
  314. endif()
  315. # Store the target so we can walk through all of them in LocationDependencies.cmake
  316. set_property(GLOBAL APPEND PROPERTY LY_ALL_TARGETS ${interface_name})
  317. if(NOT ly_add_target_IMPORTED)
  318. # Store the aliased target into a DIRECTORY property
  319. set_property(DIRECTORY APPEND PROPERTY LY_DIRECTORY_TARGETS ${interface_name})
  320. # Store the directory path in a GLOBAL property so that it can be accessed
  321. # in the layout install logic. Skip if the directory has already been added
  322. get_property(ly_all_target_directories GLOBAL PROPERTY LY_ALL_TARGET_DIRECTORIES)
  323. if(NOT CMAKE_CURRENT_SOURCE_DIR IN_LIST ly_all_target_directories)
  324. set_property(GLOBAL APPEND PROPERTY LY_ALL_TARGET_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR})
  325. endif()
  326. endif()
  327. # Custom commands need to be declared in the same folder as the target that they use.
  328. # Not all the targets will require runtime dependencies, but we will generate at least an
  329. # empty file for them.
  330. set(runtime_dependencies_list SHARED MODULE EXECUTABLE APPLICATION)
  331. if(NOT ly_add_target_IMPORTED AND linking_options IN_LIST runtime_dependencies_list)
  332. get_property(is_multi_config_generator GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
  333. # XCode generator doesnt support different source files per configuration, so we cannot have
  334. # the runtime dependencies using file-tracking, instead, we will have them as a post build step
  335. # Non-multi config generators like Ninja (not "Ninja Multi-Config"), Makefiles, etc have trouble to
  336. # produce file-level dependencies per configuration, so we also default to use a post build step
  337. if(NOT is_multi_config_generator OR CMAKE_GENERATOR MATCHES Xcode)
  338. add_custom_command(TARGET ${ly_add_target_NAME} POST_BUILD
  339. COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/runtime_dependencies/$<CONFIG>/${ly_add_target_NAME}.cmake
  340. COMMENT "Copying ${ly_add_target_NAME} runtime dependencies to output..."
  341. DEPENDS ${CMAKE_BINARY_DIR}/runtime_dependencies/${ly_add_target_NAME}.cmake
  342. COMMENT "Copying runtime dependencies..."
  343. VERBATIM
  344. )
  345. else()
  346. # the stamp file will be the one that triggers the execution of the custom rule. At the end
  347. # of running the copy of runtime dependencies, the stamp file is touched so the timestamp is updated.
  348. # Adding a config as part of the name since the stamp file is added to the VS project.
  349. # Note the STAMP_OUTPUT_FILE need to match with the one used in runtime dependencies (e.g. RuntimeDependencies_common.cmake)
  350. set(STAMP_OUTPUT_FILE ${CMAKE_BINARY_DIR}/runtime_dependencies/$<CONFIG>/${ly_add_target_NAME}.stamp)
  351. add_custom_command(
  352. OUTPUT ${STAMP_OUTPUT_FILE}
  353. DEPENDS "$<GENEX_EVAL:$<TARGET_PROPERTY:${ly_add_target_NAME},RUNTIME_DEPENDENCIES_DEPENDS>>"
  354. COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/runtime_dependencies/$<CONFIG>/${ly_add_target_NAME}.cmake
  355. COMMENT "Copying ${ly_add_target_NAME} runtime dependencies to output..."
  356. VERBATIM
  357. )
  358. # Unfortunately the VS generator cannot deal with generation expressions as part of the file name, wrapping the
  359. # stamp file on each configuration so it gets properly excluded by the generator
  360. unset(stamp_files_per_config)
  361. foreach(conf IN LISTS CMAKE_CONFIGURATION_TYPES)
  362. set(stamp_file_conf ${CMAKE_BINARY_DIR}/runtime_dependencies/${conf}/${ly_add_target_NAME}.stamp)
  363. set_source_files_properties(${stamp_file_conf} PROPERTIES GENERATED TRUE SKIP_AUTOGEN TRUE)
  364. list(APPEND stamp_files_per_config $<$<CONFIG:${conf}>:${stamp_file_conf}>)
  365. endforeach()
  366. target_sources(${ly_add_target_NAME} PRIVATE ${stamp_files_per_config})
  367. endif()
  368. endif()
  369. if(ly_add_target_AUTOGEN_RULES)
  370. ly_add_autogen(
  371. NAME ${ly_add_target_NAME}
  372. INCLUDE_DIRECTORIES ${ly_add_target_INCLUDE_DIRECTORIES}
  373. AUTOGEN_RULES ${ly_add_target_AUTOGEN_RULES}
  374. ALLFILES ${ALLFILES}
  375. )
  376. endif()
  377. endfunction()
  378. #! ly_target_link_libraries: wraps target_link_libraries handling also MODULE linkage.
  379. # MODULE libraries cannot be passed to target_link_libraries. MODULE libraries are shared libraries that we
  380. # dont want to link against because they will be loaded dynamically. However, we want to include their public headers
  381. # and transition their public dependencies.
  382. # To achieve this, we delay the target_link_libraries call to after all targets are declared (see ly_delayed_target_link_libraries)
  383. #
  384. # Signature is the same as target_link_libraries:
  385. # target_link_libraries(<target> ... <item>... ...)
  386. #
  387. function(ly_target_link_libraries TARGET)
  388. if(NOT TARGET)
  389. message(FATAL_ERROR "You must provide a target")
  390. endif()
  391. set_property(TARGET ${TARGET} APPEND PROPERTY LY_DELAYED_LINK ${ARGN})
  392. set_property(GLOBAL APPEND PROPERTY LY_DELAYED_LINK_TARGETS ${TARGET}) # to walk them at the end
  393. endfunction()
  394. #! o3de_copy_targets_usage_requires: Copies the usage requirements of the input targets
  395. # For input target that have a TYPE of "MODULE_LIBRARY", its INTERFACE_* properties are copied,
  396. # since a MODULE_LIBRARY cannot be linked
  397. # For input targets that are not of "MODULE_LIBRARY" TYPE, they are addded as dependency of the
  398. # destination target through the target_link_libraries command
  399. # See: https://cmake.org/cmake/help/latest/prop_tgt/TYPE.html for list of available library types
  400. #
  401. # The primary purpose of this function is to aggregate the usage requirements of multiple dependencies
  402. # into an INTERFACE target, where it would act as a psuedo ALIAS target which can depend on multiple targets
  403. #\arg:TARGET destination INTERFACE target where usage requirements of the input targets will be aggregated into
  404. #\arg:SOURCE_TARGETS list of targets whose usage requirements will be copied from
  405. function(o3de_copy_targets_usage_requirements)
  406. set(oneValueArgs TARGET)
  407. set(multiValueArgs SOURCE_TARGETS)
  408. cmake_parse_arguments(usage_dependencies "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  409. if(NOT usage_dependencies_TARGET OR NOT TARGET ${usage_dependencies_TARGET})
  410. message(FATAL_ERROR "${CMAKE_CURRENT_FUNCTION} must be specified a TARGET argument")
  411. endif()
  412. get_target_property(item_type ${usage_dependencies_TARGET} TYPE)
  413. if (NOT item_type STREQUAL INTERFACE_LIBRARY)
  414. message(FATAL_ERROR "${CMAKE_CURRENT_FUNCTION} only supports copy of usage requirements to an INTERFACE TARGET")
  415. endif()
  416. set(dest_target ${usage_dependencies_TARGET})
  417. foreach(source_target IN LISTS usage_dependencies_SOURCE_TARGETS)
  418. unset(item_type)
  419. # Retrieve the resolved source target name
  420. set(source_target_de_alias ${source_target})
  421. if (TARGET ${source_target})
  422. get_property(item_type TARGET ${source_target} PROPERTY TYPE)
  423. ly_de_alias_target(${source_target} source_target_de_alias)
  424. endif()
  425. # If the source target does not exist the item_type is empty
  426. # In that case the TARGET is assumed to not be a MODULE_LIBRARY
  427. if(item_type STREQUAL MODULE_LIBRARY)
  428. # For module libraries the INTERFACE properties are copied over
  429. # Copy over include and system include directories
  430. target_include_directories(${dest_target} INTERFACE $<GENEX_EVAL:$<TARGET_PROPERTY:${source_target_de_alias},INTERFACE_INCLUDE_DIRECTORIES>>)
  431. target_include_directories(${dest_target} SYSTEM INTERFACE $<GENEX_EVAL:$<TARGET_PROPERTY:${source_target_de_alias},INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>>)
  432. # Copy over dependent link libraries and linker options
  433. target_link_libraries(${dest_target} INTERFACE $<GENEX_EVAL:$<TARGET_PROPERTY:${source_target_de_alias},INTERFACE_LINK_LIBRARIES>>)
  434. target_link_options(${dest_target} INTERFACE $<GENEX_EVAL:$<TARGET_PROPERTY:${source_target_de_alias},INTERFACE_LINK_OPTIONS>>)
  435. # Copy over compiler defintions and compiler options
  436. target_compile_definitions(${dest_target} INTERFACE $<GENEX_EVAL:$<TARGET_PROPERTY:${source_target_de_alias},INTERFACE_COMPILE_DEFINITIONS>>)
  437. target_compile_options(${dest_target} INTERFACE $<GENEX_EVAL:$<TARGET_PROPERTY:${source_target_de_alias},INTERFACE_COMPILE_OPTIONS>>)
  438. # Copy over source filenames; For the destionation INTERFACE target these will not compile,
  439. # but can be used as part of add_custom_command
  440. target_sources(${dest_target} INTERFACE $<GENEX_EVAL:$<TARGET_PROPERTY:${source_target_de_alias},INTERFACE_SOURCES>>)
  441. else()
  442. # The simpler case for non-MODULE targets is to copy the usage dependencies via target_link_libraries
  443. # This even works for source targets with a TYPE of "INTERFACE_LIBRARY" as its INTERFACE_* PROPERTIES
  444. # are transferred via target_link_libraries as well
  445. # https://cmake.org/cmake/help/latest/command/add_library.html#interface-libraries
  446. target_link_libraries(${dest_target} INTERFACE ${source_target_de_alias})
  447. endif()
  448. endforeach()
  449. endfunction()
  450. #! ly_delayed_target_link_libraries: internal function called by the root CMakeLists.txt after all targets
  451. # have been declared to determine if they are regularly
  452. # 1) If a MODULE is passed in the list of items, it will add the INTERFACE_INCLUDE_DIRECTORIES as include
  453. # directories of TARGET. It will also add the "INTERFACE_LINK_LIBRARIES" to TARGET. MODULEs cannot be
  454. # directly linked, but we can include the public headers and link against the things the MODULE expose
  455. # to link.
  456. # 2) If a target that has not yet been declared is passed, then it will defer it to after all targets are
  457. # declared. This way we can do a check again. We could delay the link to when
  458. # target is declared. This is needed for (1) since we dont know the type of target. This also addresses
  459. # another issue with target_link_libraries where it will only validate that a MODULE is not being passed
  460. # if the target is already declared, if not, it will fail later at linking time.
  461. function(ly_delayed_target_link_libraries)
  462. set(visibilities PRIVATE PUBLIC INTERFACE)
  463. get_property(additional_module_paths GLOBAL PROPERTY LY_ADDITIONAL_MODULE_PATH)
  464. list(APPEND CMAKE_MODULE_PATH ${additional_module_paths})
  465. get_property(delayed_targets GLOBAL PROPERTY LY_DELAYED_LINK_TARGETS)
  466. foreach(target ${delayed_targets})
  467. get_property(delayed_link TARGET ${target} PROPERTY LY_DELAYED_LINK)
  468. if(delayed_link)
  469. cmake_parse_arguments(ly_delayed_target_link_libraries "" "" "${visibilities}" ${delayed_link})
  470. foreach(visibility ${visibilities})
  471. foreach(alias_item ${ly_delayed_target_link_libraries_${visibility}})
  472. if(TARGET ${alias_item})
  473. get_target_property(item_type ${alias_item} TYPE)
  474. ly_de_alias_target(${alias_item} item)
  475. else()
  476. unset(item_type)
  477. set(item ${alias_item})
  478. endif()
  479. if(item_type STREQUAL MODULE_LIBRARY)
  480. target_include_directories(${target} ${visibility} $<GENEX_EVAL:$<TARGET_PROPERTY:${item},INTERFACE_INCLUDE_DIRECTORIES>>)
  481. target_link_libraries(${target} ${visibility} $<GENEX_EVAL:$<TARGET_PROPERTY:${item},INTERFACE_LINK_LIBRARIES>>)
  482. target_compile_definitions(${target} ${visibility} $<GENEX_EVAL:$<TARGET_PROPERTY:${item},INTERFACE_COMPILE_DEFINITIONS>>)
  483. target_compile_options(${target} ${visibility} $<GENEX_EVAL:$<TARGET_PROPERTY:${item},INTERFACE_COMPILE_OPTIONS>>)
  484. else()
  485. ly_parse_third_party_dependencies(${item})
  486. target_link_libraries(${target} ${visibility} ${item})
  487. endif()
  488. endforeach()
  489. endforeach()
  490. endif()
  491. endforeach()
  492. set_property(GLOBAL PROPERTY LY_DELAYED_LINK_TARGETS)
  493. endfunction()
  494. #! ly_parse_third_party_dependencies: Validates any 3rdParty library dependencies through the find_package command
  495. #
  496. # \arg:ly_THIRD_PARTY_LIBRARIES name of the target libraries to validate existance of through the find_package command.
  497. #
  498. function(ly_parse_third_party_dependencies ly_THIRD_PARTY_LIBRARIES)
  499. # Support for deprecated LY_VERSION_ENGINE_NAME used in 3p-package-source and misc 3p packages
  500. set(LY_VERSION_ENGINE_NAME ${O3DE_ENGINE_NAME})
  501. # Interface dependencies may require to find_packages. So far, we are just using packages for 3rdParty, so we will
  502. # search for those and automatically bring those packages. The naming convention used is 3rdParty::PackageName::OptionalInterface
  503. unset(all_thirdparty_dependencies_found)
  504. foreach(dependency ${ly_THIRD_PARTY_LIBRARIES})
  505. string(REPLACE "::" ";" dependency_list ${dependency})
  506. list(GET dependency_list 0 dependency_namespace)
  507. if(${dependency_namespace} STREQUAL "3rdParty")
  508. list(APPEND all_thirdparty_dependencies_found ${dependency})
  509. if (NOT TARGET ${dependency})
  510. if (O3DE_SCRIPT_ONLY)
  511. # we don't actually need 3p deps to try to download the package or call find_package.
  512. # instead we use pre-created part-of-the-installer 3p targets baked in from the above list
  513. # which will have been made at install time.
  514. # instead, we execute a pregenerated file that was created as part of install to
  515. # create this target:
  516. string(REPLACE "::" "__" CLEAN_TARGET_NAME "${dependency}")
  517. # not all 3ps actually exist as real targets, so this is an OPTIONAL include.
  518. include(${LY_ROOT_FOLDER}/cmake/3rdParty/Platform/${PAL_PLATFORM_NAME}/Default/${CLEAN_TARGET_NAME}.cmake OPTIONAL)
  519. else()
  520. list(GET dependency_list 1 dependency_package)
  521. list(LENGTH dependency_list dependency_list_length)
  522. ly_download_associated_package(${dependency_package})
  523. if (dependency_list_length GREATER 2)
  524. # There's an optional interface specified
  525. list(GET dependency_list 2 component)
  526. list(APPEND packages_with_components ${dependency_package})
  527. list(APPEND ${dependency_package}_components ${component})
  528. else()
  529. find_package(${dependency_package} REQUIRED MODULE)
  530. endif()
  531. endif()
  532. endif()
  533. endif()
  534. endforeach()
  535. foreach(dependency IN LISTS packages_with_components)
  536. find_package(${dependency} REQUIRED MODULE COMPONENTS ${${dependency}_components})
  537. endforeach()
  538. foreach(dependency ${all_thirdparty_dependencies_found})
  539. if (TARGET ${dependency})
  540. # keep track of all the 3p dependencies we actually depended on.
  541. get_property(o3de_all_3rdparty_targets GLOBAL PROPERTY O3DE_ALL_3RDPARTY_TARGETS)
  542. if(NOT ${dependency} IN_LIST o3de_all_3rdparty_targets)
  543. set_property(GLOBAL APPEND PROPERTY O3DE_ALL_3RDPARTY_TARGETS "${dependency}")
  544. endif()
  545. endif()
  546. endforeach()
  547. endfunction()
  548. #! ly_configure_target_platform_properties: Configures any platform specific properties on target
  549. #
  550. # Looks at the the following variables within the platform include file to set the equivalent target properties
  551. # LY_FILES_CMAKE -> extract list of files -> target_sources
  552. # LY_FILES -> target_source
  553. # LY_INCLUDE_DIRECTORIES -> target_include_directories
  554. # LY_COMPILE_DEFINITIONS -> target_compile_definitions
  555. # LY_COMPILE_OPTIONS -> target_compile_options
  556. # LY_LINK_OPTIONS -> target_link_options
  557. # LY_BUILD_DEPENDENCIES -> target_link_libraries
  558. # LY_RUNTIME_DEPENDENCIES -> ly_add_dependencies
  559. # LY_TARGET_PROPERTIES -> target_properties
  560. #
  561. macro(ly_configure_target_platform_properties)
  562. foreach(platform_include_file ${ly_add_target_PLATFORM_INCLUDE_FILES})
  563. set(LY_FILES_CMAKE)
  564. set(LY_FILES)
  565. set(LY_INCLUDE_DIRECTORIES)
  566. set(LY_COMPILE_DEFINITIONS)
  567. set(LY_COMPILE_OPTIONS)
  568. set(LY_LINK_OPTIONS)
  569. set(LY_BUILD_DEPENDENCIES)
  570. set(LY_RUNTIME_DEPENDENCIES)
  571. set(LY_TARGET_PROPERTIES)
  572. include(${platform_include_file} RESULT_VARIABLE ly_platform_cmake_file)
  573. if(NOT ly_platform_cmake_file)
  574. message(FATAL_ERROR "The supplied PLATFORM_INCLUDE_FILE(${platform_include_file}) cannot be included.\
  575. Parsing of target will halt")
  576. endif()
  577. if(ly_add_target_INTERFACE OR ly_add_target_IMPORTED)
  578. target_sources(${ly_add_target_NAME} INTERFACE ${platform_include_file})
  579. else()
  580. target_sources(${ly_add_target_NAME} PRIVATE ${platform_include_file})
  581. endif()
  582. ly_source_groups_from_folders("${platform_include_file}")
  583. if(LY_FILES_CMAKE)
  584. foreach(file_cmake ${LY_FILES_CMAKE})
  585. ly_include_cmake_file_list(${file_cmake})
  586. endforeach()
  587. target_sources(${ly_add_target_NAME} PRIVATE ${ALLFILES})
  588. ly_source_groups_from_folders("${ALLFILES}")
  589. endif()
  590. if(LY_FILES)
  591. target_sources(${ly_add_target_NAME} PRIVATE ${LY_FILES})
  592. endif()
  593. if (LY_INCLUDE_DIRECTORIES)
  594. target_include_directories(${ly_add_target_NAME} ${LY_INCLUDE_DIRECTORIES})
  595. endif()
  596. if(LY_COMPILE_DEFINITIONS)
  597. target_compile_definitions(${ly_add_target_NAME} ${LY_COMPILE_DEFINITIONS})
  598. endif()
  599. if(LY_COMPILE_OPTIONS)
  600. target_compile_options(${ly_add_target_NAME} ${LY_COMPILE_OPTIONS})
  601. endif()
  602. if(LY_LINK_OPTIONS)
  603. target_link_options(${ly_add_target_NAME} ${LY_LINK_OPTIONS})
  604. endif()
  605. if(LY_BUILD_DEPENDENCIES)
  606. ly_target_link_libraries(${ly_add_target_NAME} ${LY_BUILD_DEPENDENCIES})
  607. endif()
  608. if(LY_RUNTIME_DEPENDENCIES)
  609. ly_add_dependencies(${ly_add_target_NAME} ${LY_RUNTIME_DEPENDENCIES})
  610. endif()
  611. if(LY_TARGET_PROPERTIES)
  612. set_target_properties(${ly_add_target_NAME} PROPERTIES ${LY_TARGET_PROPERTIES})
  613. endif()
  614. endforeach()
  615. endmacro()
  616. #! ly_add_target_files: adds files to a target. This will copy the specified files to where the target is.
  617. #
  618. # \arg:TARGETS name of the targets that depends on this file
  619. # \arg:FILES files to copy
  620. # \arg:OUTPUT_SUBDIRECTORY (OPTIONAL) where to place the files relative to TARGET_NAME's output dir.
  621. # If not specified, they are located in the same folder as TARGET_NAME
  622. #
  623. function(ly_add_target_files)
  624. set(options)
  625. set(oneValueArgs OUTPUT_SUBDIRECTORY)
  626. set(multiValueArgs TARGETS FILES)
  627. cmake_parse_arguments(ly_add_target_files "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  628. # Validate input arguments
  629. if(NOT ly_add_target_files_TARGETS)
  630. message(FATAL_ERROR "You must provide at least one target")
  631. endif()
  632. if(NOT ly_add_target_files_FILES)
  633. message(FATAL_ERROR "You must provide at least a file to copy")
  634. endif()
  635. foreach(target ${ly_add_target_files_TARGETS})
  636. foreach(file ${ly_add_target_files_FILES})
  637. set_property(TARGET ${target} APPEND PROPERTY INTERFACE_LY_TARGET_FILES "${file}\n${ly_add_target_files_OUTPUT_SUBDIRECTORY}")
  638. endforeach()
  639. endforeach()
  640. endfunction()
  641. #! ly_add_source_properties: adds/appends properties to a source file.
  642. #
  643. # This wraps set_source_files_properties to be able to pass a property with multiple values
  644. # and get it appended instead of set.
  645. #
  646. # \arg:SOURCES list of sources to apply this property on
  647. # \arg:PROPERTY property to set
  648. # \arg:VALUES values to append
  649. #
  650. function(ly_add_source_properties)
  651. set(options)
  652. set(oneValueArgs PROPERTY)
  653. set(multiValueArgs SOURCES VALUES)
  654. cmake_parse_arguments(ly_add_source_properties "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  655. # Validate input arguments
  656. if(NOT ly_add_source_properties_SOURCES)
  657. message(FATAL_ERROR "You must provide at least one source")
  658. endif()
  659. if(NOT ly_add_source_properties_PROPERTY)
  660. message(FATAL_ERROR "You must provide a property")
  661. endif()
  662. foreach(file ${ly_add_source_properties_SOURCES})
  663. if(NOT IS_ABSOLUTE ${file})
  664. get_filename_component(file ${file} ABSOLUTE)
  665. endif()
  666. if(NOT EXISTS ${file})
  667. message(SEND_ERROR "File ${file} not found when setting property ${ly_add_source_properties_PROPERTY}")
  668. endif()
  669. endforeach()
  670. # We allow to pass empty values because in some cases we expand the values based on conditions
  671. # If the values are empty then this call does nothing
  672. if(ly_add_source_properties_VALUES)
  673. set_property(
  674. SOURCE ${ly_add_source_properties_SOURCES}
  675. APPEND PROPERTY ${ly_add_source_properties_PROPERTY} ${ly_add_source_properties_VALUES}
  676. )
  677. endif()
  678. endfunction()
  679. # given a target name, returns the "real" name of the target if its an alias.
  680. # this function recursively de-aliases
  681. function(ly_de_alias_target target_name output_variable_name)
  682. # its not okay to call get_target_property on a non-existent target
  683. if (NOT TARGET ${target_name})
  684. message(FATAL_ERROR "ly_de_alias_target called on non-existent target: ${target_name}")
  685. endif()
  686. while(target_name)
  687. set(de_aliased_target_name ${target_name})
  688. get_target_property(target_name ${target_name} ALIASED_TARGET)
  689. endwhile()
  690. if(NOT de_aliased_target_name)
  691. message(FATAL_ERROR "Empty de_aliased for ${target_name}")
  692. endif()
  693. set(${output_variable_name} ${de_aliased_target_name} PARENT_SCOPE)
  694. endfunction()
  695. #! ly_get_vs_folder_directory: Sets the Visual Studio folder name used for organizing vcxproj
  696. # in the IDE
  697. #
  698. # Visual Studio cannot load projects that with a ".." relative path or contain a colon ":" as part of its FOLDER
  699. # Therefore if the .vcxproj is absolute, the drive letter must be removed from the folder name
  700. #
  701. # What this method does is first check if the target being added to the Visual Studio solution is within
  702. # the LY_ROOT_FOLDER(i.e is the LY_ROOT_FOLDER a prefix of the target source directory)
  703. # If it is a relative path to the target is used as the folder name
  704. # Otherwise the target directory would either
  705. # 1. Be a path outside of the LY_ROOT_FOLDER on the same drive.
  706. # In that case forming a relative path would cause it to start with ".." which will not work
  707. # 2. Be an path outside of the LY_ROOT_FOLDER on a different drive
  708. # Here a relative path cannot be formed and therefore the path would start with "<drive>:/Path/To/Project"
  709. # Which shows up as unloaded due to containing a colon
  710. # In this scenario the relative part of the path from the drive letter is used as the FOLDER name
  711. # to make sure the projects show up in the loaded .sln
  712. function(ly_get_vs_folder_directory absolute_target_source_dir output_source_dir)
  713. # Get a relative directory to the LY_ROOT_FOLDER if possible for the Visual Studio solution hierarchy
  714. # If a relative path cannot be formed, then retrieve a path with the drive letter stripped from it
  715. cmake_path(IS_PREFIX LY_ROOT_FOLDER ${absolute_target_source_dir} is_target_prefix_of_engine_root)
  716. if(is_target_prefix_of_engine_root)
  717. cmake_path(RELATIVE_PATH absolute_target_source_dir BASE_DIRECTORY ${LY_ROOT_FOLDER} OUTPUT_VARIABLE relative_target_source_dir)
  718. else()
  719. cmake_path(IS_PREFIX CMAKE_SOURCE_DIR ${absolute_target_source_dir} is_target_prefix_of_source_dir)
  720. if(is_target_prefix_of_source_dir)
  721. cmake_path(RELATIVE_PATH absolute_target_source_dir BASE_DIRECTORY ${CMAKE_SOURCE_DIR} OUTPUT_VARIABLE relative_target_source_dir)
  722. else()
  723. cmake_path(GET absolute_target_source_dir RELATIVE_PART relative_target_source_dir)
  724. endif()
  725. endif()
  726. set(${output_source_dir} ${relative_target_source_dir} PARENT_SCOPE)
  727. endfunction()