EngineFinder.cmake 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # {BEGIN_LICENSE}
  2. #
  3. # Copyright (c) Contributors to the Open 3D Engine Project.
  4. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  5. #
  6. # SPDX-License-Identifier: Apache-2.0 OR MIT
  7. #
  8. #
  9. # {END_LICENSE}
  10. # This file is copied during engine registration. Edits to this file will be lost next
  11. # time a registration happens.
  12. include_guard()
  13. # Read the engine name from the project_json file
  14. file(READ ${CMAKE_CURRENT_LIST_DIR}/project.json project_json)
  15. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/project.json)
  16. string(JSON LY_ENGINE_NAME_TO_USE ERROR_VARIABLE json_error GET ${project_json} engine)
  17. if(json_error)
  18. message(FATAL_ERROR "Unable to read key 'engine' from 'project.json', error: ${json_error}")
  19. endif()
  20. if(DEFINED ENV{USERPROFILE} AND EXISTS $ENV{USERPROFILE})
  21. set(manifest_path $ENV{USERPROFILE}/.o3de/o3de_manifest.json) # Windows
  22. else()
  23. set(manifest_path $ENV{HOME}/.o3de/o3de_manifest.json) # Unix
  24. endif()
  25. # Read the ~/.o3de/o3de_manifest.json file and look through the 'engines_path' object.
  26. # Find a key that matches LY_ENGINE_NAME_TO_USE and use that as the engine path.
  27. if(EXISTS ${manifest_path})
  28. file(READ ${manifest_path} manifest_json)
  29. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${manifest_path})
  30. string(JSON engines_path_count ERROR_VARIABLE json_error LENGTH ${manifest_json} engines_path)
  31. if(json_error)
  32. message(FATAL_ERROR "Unable to read key 'engines_path' from '${manifest_path}', error: ${json_error}")
  33. endif()
  34. string(JSON engines_path_type ERROR_VARIABLE json_error TYPE ${manifest_json} engines_path)
  35. if(json_error OR NOT ${engines_path_type} STREQUAL "OBJECT")
  36. message(FATAL_ERROR "Type of 'engines_path' in '${manifest_path}' is not a JSON Object, error: ${json_error}")
  37. endif()
  38. math(EXPR engines_path_count "${engines_path_count}-1")
  39. foreach(engine_path_index RANGE ${engines_path_count})
  40. string(JSON engine_name ERROR_VARIABLE json_error MEMBER ${manifest_json} engines_path ${engine_path_index})
  41. if(json_error)
  42. message(FATAL_ERROR "Unable to read 'engines_path/${engine_path_index}' from '${manifest_path}', error: ${json_error}")
  43. endif()
  44. if(LY_ENGINE_NAME_TO_USE STREQUAL engine_name)
  45. string(JSON engine_path ERROR_VARIABLE json_error GET ${manifest_json} engines_path ${engine_name})
  46. if(json_error)
  47. message(FATAL_ERROR "Unable to read value from 'engines_path/${engine_name}', error: ${json_error}")
  48. endif()
  49. if(engine_path)
  50. list(APPEND CMAKE_MODULE_PATH "${engine_path}/cmake")
  51. break()
  52. endif()
  53. endif()
  54. endforeach()
  55. else()
  56. # If the user is passing CMAKE_MODULE_PATH we assume thats where we will find the engine
  57. if(NOT CMAKE_MODULE_PATH)
  58. message(FATAL_ERROR "Engine registration is required before configuring a project. Please register an engine by running 'scripts/o3de register --this-engine'")
  59. endif()
  60. endif()