EngineFinder.cmake 9.2 KB

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