EngineFinder.cmake 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # Edits to this file may be lost in upgrades. Instead of changing this file, use
  2. # the 'engine_finder_cmake_path' key in your project.json or user/project.json to specify
  3. # an alternate .cmake file to use instead of this one.
  4. include_guard()
  5. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/project.json)
  6. # Option 1: Use engine manually set in CMAKE_MODULE_PATH
  7. # CMAKE_MODULE_PATH must contain a path to an engine's cmake folder
  8. if(CMAKE_MODULE_PATH)
  9. foreach(module_path ${CMAKE_MODULE_PATH})
  10. cmake_path(SET module_engine_version_cmake_path "${module_path}/o3deConfigVersion.cmake")
  11. if(EXISTS "${module_engine_version_cmake_path}")
  12. include("${module_engine_version_cmake_path}")
  13. if(PACKAGE_VERSION_COMPATIBLE)
  14. message(STATUS "Selecting engine from CMAKE_MODULE_PATH '${module_path}'")
  15. return()
  16. else()
  17. message(WARNING "Not using engine from CMAKE_MODULE_PATH '${module_path}' because it is not compatible with this project.")
  18. endif()
  19. endif()
  20. endforeach()
  21. message(VERBOSE "No compatible engine found from CMAKE_MODULE_PATH '${CMAKE_MODULE_PATH}'.")
  22. endif()
  23. # before proceeding, check and set a global variable if its a "script only" project
  24. file(READ ${CMAKE_CURRENT_SOURCE_DIR}/project.json o3de_project_json)
  25. string(JSON project_is_script_only ERROR_VARIABLE json_error GET ${o3de_project_json} "script_only")
  26. if(project_is_script_only AND NOT json_error)
  27. # we only set it if there's no error and it is a script only project
  28. message(STATUS "This is a script-only project, no C++ compiler will be used")
  29. set_property(GLOBAL PROPERTY "O3DE_SCRIPT_ONLY" TRUE)
  30. endif()
  31. # Option 2: Use the engine from the 'engine_path' field in <project>/user/project.json
  32. cmake_path(SET O3DE_USER_PROJECT_JSON_PATH ${CMAKE_CURRENT_SOURCE_DIR}/user/project.json)
  33. if(EXISTS "${O3DE_USER_PROJECT_JSON_PATH}")
  34. file(READ "${O3DE_USER_PROJECT_JSON_PATH}" user_project_json)
  35. if(user_project_json)
  36. string(JSON user_project_engine_path ERROR_VARIABLE json_error GET ${user_project_json} engine_path)
  37. if(user_project_engine_path AND NOT json_error)
  38. cmake_path(SET user_engine_version_cmake_path "${user_project_engine_path}/cmake/o3deConfigVersion.cmake")
  39. if(EXISTS "${user_engine_version_cmake_path}")
  40. include("${user_engine_version_cmake_path}")
  41. if(PACKAGE_VERSION_COMPATIBLE)
  42. message(STATUS "Selecting engine '${user_project_engine_path}' from 'engine_path' in '<project>/user/project.json'.")
  43. list(APPEND CMAKE_MODULE_PATH "${user_project_engine_path}/cmake")
  44. return()
  45. else()
  46. message(FATAL_ERROR "The engine at '${user_project_engine_path}' from 'engine_path' in '${O3DE_USER_PROJECT_JSON_PATH}' is not compatible with this project. Please register this project with a compatible engine, or remove the local override by running:\nscripts\\o3de edit-project-properties -pp ${CMAKE_CURRENT_SOURCE_DIR} --user --engine-path \"\"")
  47. endif()
  48. else()
  49. message(FATAL_ERROR "This project cannot use the engine at '${user_project_engine_path}' because the version cmake file '${user_engine_version_cmake_path}' needed to check compatibility is missing. \nPlease register this project with a compatible engine, or remove the local override by running:\nscripts\\o3de edit-project-properties -pp ${CMAKE_CURRENT_SOURCE_DIR} --user --engine-path \"\"\nIf you want this project to use an older engine(not recommended), provide a custom EngineFinder.cmake using the o3de CLI's --engine-finder-cmake-path option. ")
  50. endif()
  51. elseif(json_error AND ${user_project_engine_path} STREQUAL "NOTFOUND")
  52. # When the value is just NOTFOUND that means there is a JSON
  53. # parsing error, and not simply a missing key
  54. message(FATAL_ERROR "Unable to read 'engine_path' from '${user_project_engine_path}'\nError: ${json-error}")
  55. endif()
  56. endif()
  57. endif()
  58. # Option 3: Find a compatible engine registered in ~/.o3de/o3de_manifest.json
  59. if(DEFINED ENV{USERPROFILE} AND EXISTS $ENV{USERPROFILE})
  60. set(manifest_path $ENV{USERPROFILE}/.o3de/o3de_manifest.json) # Windows
  61. else()
  62. set(manifest_path $ENV{HOME}/.o3de/o3de_manifest.json) # Unix
  63. endif()
  64. set(registration_error [=[
  65. To enable more verbose logging, run the cmake command again with '--log-level VERBOSE'
  66. Engine registration is required before configuring a project.
  67. Run 'scripts/o3de register --this-engine' from the engine root.
  68. ]=])
  69. # Create a list of all engines
  70. if(EXISTS ${manifest_path})
  71. file(READ ${manifest_path} manifest_json)
  72. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${manifest_path})
  73. string(JSON engines_count ERROR_VARIABLE json_error LENGTH ${manifest_json} engines)
  74. if(json_error)
  75. message(FATAL_ERROR "Unable to read key 'engines' from '${manifest_path}'\nError: ${json_error}\n${registration_error}")
  76. endif()
  77. string(JSON engines_type ERROR_VARIABLE json_error TYPE ${manifest_json} engines)
  78. if(json_error OR NOT ${engines_type} STREQUAL "ARRAY")
  79. message(FATAL_ERROR "Type of 'engines' in '${manifest_path}' is not a JSON ARRAY\nError: ${json_error}\n${registration_error}")
  80. endif()
  81. math(EXPR engines_count "${engines_count}-1")
  82. foreach(array_index RANGE ${engines_count})
  83. string(JSON manifest_engine_path ERROR_VARIABLE json_error GET ${manifest_json} engines "${array_index}")
  84. if(json_error)
  85. message(FATAL_ERROR "Unable to read 'engines/${array_index}' from '${manifest_path}'\nError: ${json_error}\n${registration_error}")
  86. endif()
  87. list(APPEND O3DE_ENGINE_PATHS ${manifest_engine_path})
  88. endforeach()
  89. elseif(NOT CMAKE_MODULE_PATH)
  90. message(FATAL_ERROR "O3DE Manifest file not found at '${manifest_path}'.\n${registration_error}")
  91. endif()
  92. # We cannot just run find_package() on the list of engine paths because
  93. # CMAKE_FIND_PACKAGE_SORT_ORDER sorts based on file name and chooses
  94. # the first package that returns PACKAGE_VERSION_COMPATIBLE
  95. set(O3DE_MOST_COMPATIBLE_ENGINE_PATH "")
  96. set(O3DE_MOST_COMPATIBLE_ENGINE_VERSION "")
  97. foreach(manifest_engine_path IN LISTS O3DE_ENGINE_PATHS)
  98. # Does this engine have a config version cmake file?
  99. cmake_path(SET version_cmake_path "${manifest_engine_path}/cmake/o3deConfigVersion.cmake")
  100. if(NOT EXISTS "${version_cmake_path}")
  101. message(VERBOSE "Ignoring '${manifest_engine_path}' because no config version cmake file was found at '${version_cmake_path}'")
  102. continue()
  103. endif()
  104. unset(PACKAGE_VERSION)
  105. unset(PACKAGE_VERSION_COMPATIBLE)
  106. include("${version_cmake_path}")
  107. # Follow the version checking convention from find_package(CONFIG)
  108. if(PACKAGE_VERSION_COMPATIBLE)
  109. if(NOT O3DE_MOST_COMPATIBLE_ENGINE_PATH)
  110. set(O3DE_MOST_COMPATIBLE_ENGINE_PATH "${manifest_engine_path}")
  111. set(O3DE_MOST_COMPATIBLE_ENGINE_VERSION ${PACKAGE_VERSION})
  112. message(VERBOSE "Found compatible engine '${manifest_engine_path}' with version '${PACKAGE_VERSION}'")
  113. elseif(${PACKAGE_VERSION} VERSION_GREATER ${O3DE_MOST_COMPATIBLE_ENGINE_VERSION})
  114. set(O3DE_MOST_COMPATIBLE_ENGINE_PATH "${manifest_engine_path}")
  115. set(O3DE_MOST_COMPATIBLE_ENGINE_VERSION ${PACKAGE_VERSION})
  116. message(VERBOSE "Found more compatible engine '${manifest_engine_path}' with version '${PACKAGE_VERSION}' because it has a greater version number.")
  117. else()
  118. message(VERBOSE "Not using engine '${manifest_engine_path}' with version '${PACKAGE_VERSION}' because it doesn't have a greater version number or has a different engine name.")
  119. endif()
  120. else()
  121. message(VERBOSE "Ignoring '${manifest_engine_path}' because it is not a compatible engine.")
  122. endif()
  123. endforeach()
  124. if(O3DE_MOST_COMPATIBLE_ENGINE_PATH)
  125. message(STATUS "Selecting engine '${O3DE_MOST_COMPATIBLE_ENGINE_PATH}'")
  126. # Make sure PACKAGE_VERSION_COMPATIBLE is set so Findo3de.cmake knows
  127. # compatibility was checked
  128. set(PACKAGE_VERSION_COMPATIBLE True)
  129. set(PACKAGE_VERSION O3DE_MOST_COMPATIBLE_ENGINE_VERSION)
  130. list(APPEND CMAKE_MODULE_PATH "${O3DE_MOST_COMPATIBLE_ENGINE_PATH}/cmake")
  131. return()
  132. endif()
  133. # No compatible engine was found.
  134. # Read the 'engine' field in project.json or user/project.json for more helpful messages
  135. if(user_project_json)
  136. string(JSON user_project_engine ERROR_VARIABLE json_error GET ${user_project_json} engine)
  137. endif()
  138. if(NOT user_project_engine)
  139. string(JSON project_engine ERROR_VARIABLE json_error GET ${o3de_project_json} engine)
  140. if(json_error AND ${project_engine} STREQUAL "NOTFOUND")
  141. message(FATAL_ERROR "Unable to read key 'engine' from 'project.json'\nError: ${json_error}")
  142. endif()
  143. endif()
  144. if(user_project_engine)
  145. message(FATAL_ERROR "The local '${O3DE_USER_PROJECT_JSON_PATH}' engine is '${user_project_engine}' but no compatible engine with that name and version was found. Please register the compatible engine, or remove the local engine override.\n${registration_error}")
  146. elseif(project_engine)
  147. message(FATAL_ERROR "The project.json engine is '${project_engine}' but no engine with that name and version was found.\n${registration_error}")
  148. else()
  149. set(project_registration_error [=[
  150. Project registration is required before configuring a project.
  151. Run 'scripts/o3de register -pp PROJECT_PATH --engine-path ENGINE_PATH' from the engine root.
  152. ]=])
  153. message(FATAL_ERROR "${project_registration_error}")
  154. endif()