EngineFinder.cmake 3.9 KB

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