Configurations.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. include_guard(GLOBAL)
  9. # LY_CONFIGURATION_TYPES defines all the configuration types that O3DE supports
  10. # We dont set CMAKE_CONFIGURATION_TYPES directly because we want to be able to configure which
  11. # configuration types are supported in an SDK installation. SDK installations will fill a
  12. # CMAKE_CONFIGURATION_TYPES based on the configurations that were generated during the install process.
  13. # ly_append_configurations_options depends on LY_CONFIGURATION_TYPES being
  14. # set in order to successfully parse the arguments. Even for non-multi-config
  15. # generators, it needs to be set.
  16. set(LY_CONFIGURATION_TYPES "debug;profile;release" CACHE STRING "" FORCE)
  17. include(cmake/ConfigurationTypes.cmake)
  18. #! ly_append_configurations_options: adds options to the different configurations (debug, profile, release, etc)
  19. #
  20. # \arg:DEFINES
  21. # \arg:DEFINES_${CONFIGURATION}
  22. # \arg:COMPILATION
  23. # \arg:COMPILATION_${CONFIGURATION}
  24. # \arg:COMPILATION_C
  25. # \arg:COMPILATION_C_${CONFIGURATION}
  26. # \arg:COMPILATION_CXX
  27. # \arg:COMPILATION_CXX_${CONFIGURATION}
  28. # \arg:LINK
  29. # \arg:LINK_${CONFIGURATION}
  30. # \arg:LINK_STATIC
  31. # \arg:LINK_STATIC_${CONFIGURATION}
  32. # \arg:LINK_NON_STATIC
  33. # \arg:LINK_NON_STATIC_${CONFIGURATION}
  34. # \arg:LINK_EXE
  35. # \arg:LINK_EXE_${CONFIGURATION}
  36. # \arg:LINK_MODULE
  37. # \arg:LINK_MODULE_${CONFIGURATION}
  38. # \arg:LINK_SHARED
  39. # \arg:LINK_SHARED_${CONFIGURATION}
  40. #
  41. # Note: COMPILATION_C/COMPILATION_CXX are mutually exclusive with COMPILATION. You can only specify COMPILATION for C/C++ flags or
  42. # a combination of COMPILATION_C/COMPILATION_CXX for the separate c/c++ flags separately.
  43. function(ly_append_configurations_options)
  44. set(options)
  45. set(oneValueArgs)
  46. set(multiArgs
  47. DEFINES
  48. COMPILATION
  49. COMPILATION_C
  50. COMPILATION_CXX
  51. LINK
  52. LINK_STATIC
  53. LINK_NON_STATIC
  54. LINK_EXE
  55. LINK_MODULE
  56. LINK_SHARED
  57. )
  58. foreach(arg IN LISTS multiArgs)
  59. list(APPEND multiValueArgs ${arg})
  60. # we parse the parameters based on all configuration types so unknown configurations
  61. # are not passed as values to other parameters
  62. foreach(conf IN LISTS LY_CONFIGURATION_TYPES)
  63. string(TOUPPER ${conf} UCONF)
  64. list(APPEND multiValueArgs ${arg}_${UCONF})
  65. endforeach()
  66. endforeach()
  67. cmake_parse_arguments(ly_append_configurations_options "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  68. if(ly_append_configurations_options_DEFINES)
  69. add_compile_definitions(${ly_append_configurations_options_DEFINES})
  70. endif()
  71. if(ly_append_configurations_options_COMPILATION)
  72. string(REPLACE ";" " " COMPILATION_STR "${ly_append_configurations_options_COMPILATION}")
  73. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMPILATION_STR}" PARENT_SCOPE)
  74. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMPILATION_STR}" PARENT_SCOPE)
  75. endif()
  76. if(ly_append_configurations_options_COMPILATION_C)
  77. string(REPLACE ";" " " COMPILATION_STR "${ly_append_configurations_options_COMPILATION_C}")
  78. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMPILATION_STR}" PARENT_SCOPE)
  79. endif()
  80. if(ly_append_configurations_options_COMPILATION_CXX)
  81. string(REPLACE ";" " " COMPILATION_STR "${ly_append_configurations_options_COMPILATION_CXX}")
  82. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMPILATION_STR}" PARENT_SCOPE)
  83. endif()
  84. if(ly_append_configurations_options_LINK)
  85. string(REPLACE ";" " " LINK_STR "${ly_append_configurations_options_LINK}")
  86. set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} ${LINK_STR}" PARENT_SCOPE)
  87. set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${LINK_STR}" PARENT_SCOPE)
  88. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LINK_STR}" PARENT_SCOPE)
  89. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINK_OPTIONS}" PARENT_SCOPE)
  90. endif()
  91. if(ly_append_configurations_options_LINK_STATIC)
  92. string(REPLACE ";" " " LINK_STR "${ly_append_configurations_options_LINK_STATIC}")
  93. set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} ${LINK_STR}" PARENT_SCOPE)
  94. endif()
  95. if(ly_append_configurations_options_LINK_NON_STATIC)
  96. string(REPLACE ";" " " LINK_STR "${ly_append_configurations_options_LINK_NON_STATIC}")
  97. set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${LINK_STR}" PARENT_SCOPE)
  98. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LINK_STR}" PARENT_SCOPE)
  99. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINK_STR}" PARENT_SCOPE)
  100. endif()
  101. if(ly_append_configurations_options_LINK_EXE)
  102. string(REPLACE ";" " " LINK_STR "${ly_append_configurations_options_LINK_EXE}")
  103. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINK_STR}" PARENT_SCOPE)
  104. endif()
  105. if(ly_append_configurations_options_LINK_MODULE)
  106. string(REPLACE ";" " " LINK_STR "${ly_append_configurations_options_LINK_MODULE}")
  107. set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${LINK_STR}" PARENT_SCOPE)
  108. endif()
  109. if(ly_append_configurations_options_LINK_SHARED)
  110. string(REPLACE ";" " " LINK_STR "${ly_append_configurations_options_LINK_SHARED}")
  111. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LINK_STR}" PARENT_SCOPE)
  112. endif()
  113. # We only iterate for the actual configuration types
  114. foreach(conf IN LISTS CMAKE_CONFIGURATION_TYPES)
  115. string(TOUPPER ${conf} UCONF)
  116. if(ly_append_configurations_options_DEFINES_${UCONF})
  117. #Transform defines using generator expressions
  118. list(TRANSFORM ly_append_configurations_options_DEFINES_${UCONF} REPLACE "^.+$" "$<$<CONFIG:${conf}>:\\0>"
  119. OUTPUT_VARIABLE DEFINES_${UCONF}_GENEX)
  120. add_compile_definitions(${DEFINES_${UCONF}_GENEX})
  121. endif()
  122. if(ly_append_configurations_options_COMPILATION_${UCONF})
  123. string(REPLACE ";" " " COMPILATION_STR "${ly_append_configurations_options_COMPILATION_${UCONF}}")
  124. set(CMAKE_C_FLAGS_${UCONF} "${CMAKE_C_FLAGS_${UCONF}} ${COMPILATION_STR}" PARENT_SCOPE)
  125. set(CMAKE_CXX_FLAGS_${UCONF} "${CMAKE_CXX_FLAGS_${UCONF}} ${COMPILATION_STR}" PARENT_SCOPE)
  126. endif()
  127. if(ly_append_configurations_options_LINK_${UCONF})
  128. string(REPLACE ";" " " LINK_STR "${ly_append_configurations_options_LINK_${UCONF}}")
  129. set(CMAKE_STATIC_LINKER_FLAGS_${UCONF} "${CMAKE_STATIC_LINKER_FLAGS_${UCONF}} ${LINK_STR}" PARENT_SCOPE)
  130. set(CMAKE_MODULE_LINKER_FLAGS_${UCONF} "${CMAKE_MODULE_LINKER_FLAGS_${UCONF}} ${LINK_STR}" PARENT_SCOPE)
  131. set(CMAKE_SHARED_LINKER_FLAGS_${UCONF} "${CMAKE_SHARED_LINKER_FLAGS_${UCONF}} ${LINK_STR}" PARENT_SCOPE)
  132. set(CMAKE_EXE_LINKER_FLAGS_${UCONF} "${CMAKE_EXE_LINKER_FLAGS_${UCONF}} ${LINK_STR}" PARENT_SCOPE)
  133. endif()
  134. if(ly_append_configurations_options_LINK_STATIC_${UCONF})
  135. string(REPLACE ";" " " LINK_STR "${ly_append_configurations_options_LINK_STATIC_${UCONF}}")
  136. set(CMAKE_STATIC_LINKER_FLAGS_${UCONF} "${CMAKE_STATIC_LINKER_FLAGS_${UCONF}} ${LINK_STR}" PARENT_SCOPE)
  137. endif()
  138. if(ly_append_configurations_options_LINK_NON_STATIC_${UCONF})
  139. string(REPLACE ";" " " LINK_STR "${ly_append_configurations_options_LINK_NON_STATIC_${UCONF}}")
  140. set(CMAKE_MODULE_LINKER_FLAGS_${UCONF} "${CMAKE_MODULE_LINKER_FLAGS_${UCONF}} ${LINK_STR}" PARENT_SCOPE)
  141. set(CMAKE_SHARED_LINKER_FLAGS_${UCONF} "${CMAKE_SHARED_LINKER_FLAGS_${UCONF}} ${LINK_STR}" PARENT_SCOPE)
  142. set(CMAKE_EXE_LINKER_FLAGS_${UCONF} "${CMAKE_EXE_LINKER_FLAGS_${UCONF}} ${LINK_STR}" PARENT_SCOPE)
  143. endif()
  144. if(ly_append_configurations_options_LINK_EXE_${UCONF})
  145. string(REPLACE ";" " " LINK_STR "${ly_append_configurations_options_LINK_EXE_${UCONF}}")
  146. set(CMAKE_EXE_LINKER_FLAGS_${UCONF} "${CMAKE_EXE_LINKER_FLAGS_${UCONF}} ${LINK_STR}" PARENT_SCOPE)
  147. endif()
  148. if(ly_append_configurations_options_LINK_MODULE_${UCONF})
  149. string(REPLACE ";" " " LINK_STR "${ly_append_configurations_options_LINK_MODULE_${UCONF}}")
  150. set(CMAKE_MODULE_LINKER_FLAGS_${UCONF} "${CMAKE_MODULE_LINKER_FLAGS_${UCONF}} ${LINK_STR}" PARENT_SCOPE)
  151. endif()
  152. endforeach()
  153. endfunction()
  154. # Set the C++ standard that is being targeted to C++17
  155. set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ Standard to target")
  156. ly_set(CMAKE_CXX_STANDARD_REQUIRED ON)
  157. set(O3DE_STACK_CAPTURE_DEPTH 3 CACHE STRING "The depth of the callstack to capture when tracking allocations")
  158. get_property(_isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
  159. if(NOT _isMultiConfig)
  160. # No reason set CMAKE_BUILD_TYPE if it's a multiconfig generator.
  161. if(NOT CMAKE_BUILD_TYPE)
  162. message("No build type specified (CMAKE_BUILD_TYPE), defaulting to profile build")
  163. set(CMAKE_BUILD_TYPE profile CACHE STRING "" FORCE)
  164. endif()
  165. set_property(CACHE CMAKE_BUILD_TYPE PROPERTY HELPSTRING "Type of build (debug|profile|release)")
  166. # set options for cmake-gui
  167. set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "debug;profile;release")
  168. endif()
  169. unset(_isMultiConfig)
  170. # Now these CMake variables are cleared out in the cache here.
  171. # This must be done before including the Platform specific Configurations_<platform>.cmake files as non-standard configurations
  172. # such as profile doesn't already have a cache version of these variable.
  173. # For configurations such as debug and release, the CMake internal module files initialize variable such as
  174. # CMAKE_EXE_LINKER_FLAGS_DEBUG and CMAKE_EXE_LINKER_FLAGS_RELEASE, so the set call below for those CACHE variables do nothing.
  175. # But for profile this defines the cache variable for the first time, therefore allowing the ly_append_configurations_options() function
  176. # to append to the CMAKE_*_LINKER_FLAGS_PROFILE variables in the parent scope when it runs
  177. foreach(conf IN LISTS CMAKE_CONFIGURATION_TYPES)
  178. string(TOUPPER ${conf} UCONF)
  179. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${UCONF} ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/${conf} CACHE PATH "Installation directory for ${conf} ar")
  180. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${UCONF} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${conf} CACHE PATH "Installation directory for ${conf} libraries")
  181. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${UCONF} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${conf} CACHE PATH "Installation directory for ${conf} executables")
  182. set(CMAKE_STATIC_LINKER_FLAGS_${UCONF} CACHE STRING "Flags to pass to the archiver for ${conf}")
  183. set(CMAKE_MODULE_LINKER_FLAGS_${UCONF} CACHE STRING "Flags to pass to the linker when creating a module library for ${conf}")
  184. set(CMAKE_SHARED_LINKER_FLAGS_${UCONF} CACHE STRING "Flags to pass to the linker when creating a shared library for ${conf}")
  185. set(CMAKE_EXE_LINKER_FLAGS_${UCONF} CACHE STRING "Flags to pass to the linker when creating an executable for ${conf}")
  186. endforeach()
  187. # flags are defined per platform, follow platform files under Platform/<PlatformName>/Configurations_<platformname>(_<platformarchitecture>).cmake
  188. o3de_pal_dir(pal_dir ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Platform/${PAL_PLATFORM_NAME} "${O3DE_ENGINE_RESTRICTED_PATH}" "${LY_ROOT_FOLDER}")
  189. include(${pal_dir}/Configurations_${PAL_PLATFORM_NAME_LOWERCASE}${LY_ARCHITECTURE_NAME_EXTENSION}.cmake)