EngineFinder.cmake 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. # Read the engine name from the project_json file
  13. file(READ ${CMAKE_CURRENT_SOURCE_DIR}/project.json project_json)
  14. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/project.json)
  15. string(JSON LY_ENGINE_NAME_TO_USE ERROR_VARIABLE json_error GET ${project_json} engine)
  16. if(json_error)
  17. message(FATAL_ERROR "Unable to read key 'engine' from 'project.json'\nError: ${json_error}")
  18. endif()
  19. if(CMAKE_MODULE_PATH)
  20. foreach(module_path ${CMAKE_MODULE_PATH})
  21. if(EXISTS ${module_path}/Findo3de.cmake)
  22. file(READ ${module_path}/../engine.json engine_json)
  23. string(JSON engine_name ERROR_VARIABLE json_error GET ${engine_json} engine_name)
  24. if(json_error)
  25. message(FATAL_ERROR "Unable to read key 'engine_name' from 'engine.json'\nError: ${json_error}")
  26. endif()
  27. if(LY_ENGINE_NAME_TO_USE STREQUAL engine_name)
  28. return() # Engine being forced through CMAKE_MODULE_PATH
  29. endif()
  30. endif()
  31. endforeach()
  32. endif()
  33. if(DEFINED ENV{USERPROFILE} AND EXISTS $ENV{USERPROFILE})
  34. set(manifest_path $ENV{USERPROFILE}/.o3de/o3de_manifest.json) # Windows
  35. else()
  36. set(manifest_path $ENV{HOME}/.o3de/o3de_manifest.json) # Unix
  37. endif()
  38. set(registration_error [=[
  39. Engine registration is required before configuring a project.
  40. Run 'scripts/o3de register --this-engine' from the engine root.
  41. ]=])
  42. # Read the ~/.o3de/o3de_manifest.json file and look through the 'engines_path' object.
  43. # Find a key that matches LY_ENGINE_NAME_TO_USE and use that as the engine path.
  44. if(EXISTS ${manifest_path})
  45. file(READ ${manifest_path} manifest_json)
  46. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${manifest_path})
  47. string(JSON engines_path_count ERROR_VARIABLE json_error LENGTH ${manifest_json} engines_path)
  48. if(json_error)
  49. message(FATAL_ERROR "Unable to read key 'engines_path' from '${manifest_path}'\nError: ${json_error}\n${registration_error}")
  50. endif()
  51. string(JSON engines_path_type ERROR_VARIABLE json_error TYPE ${manifest_json} engines_path)
  52. if(json_error OR NOT ${engines_path_type} STREQUAL "OBJECT")
  53. message(FATAL_ERROR "Type of 'engines_path' in '${manifest_path}' is not a JSON Object\nError: ${json_error}")
  54. endif()
  55. math(EXPR engines_path_count "${engines_path_count}-1")
  56. foreach(engine_path_index RANGE ${engines_path_count})
  57. string(JSON engine_name ERROR_VARIABLE json_error MEMBER ${manifest_json} engines_path ${engine_path_index})
  58. if(json_error)
  59. message(FATAL_ERROR "Unable to read 'engines_path/${engine_path_index}' from '${manifest_path}'\nError: ${json_error}")
  60. endif()
  61. if(LY_ENGINE_NAME_TO_USE STREQUAL engine_name)
  62. string(JSON engine_path ERROR_VARIABLE json_error GET ${manifest_json} engines_path ${engine_name})
  63. if(json_error)
  64. message(FATAL_ERROR "Unable to read value from 'engines_path/${engine_name}'\nError: ${json_error}")
  65. endif()
  66. if(engine_path)
  67. list(APPEND CMAKE_MODULE_PATH "${engine_path}/cmake")
  68. return()
  69. endif()
  70. endif()
  71. endforeach()
  72. message(FATAL_ERROR "The project.json uses engine name '${LY_ENGINE_NAME_TO_USE}' but no engine with that name has been registered.\n${registration_error}")
  73. else()
  74. # If the user is passing CMAKE_MODULE_PATH we assume thats where we will find the engine
  75. if(NOT CMAKE_MODULE_PATH)
  76. message(FATAL_ERROR "O3DE Manifest file not found.\n${registration_error}")
  77. endif()
  78. endif()