EngineFinder.cmake 3.7 KB

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