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