EngineFinder.cmake 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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_LIST_DIR}/project.json project_json)
  13. string(JSON LY_ENGINE_NAME_TO_USE ERROR_VARIABLE json_error GET ${project_json} engine)
  14. if(json_error)
  15. message(FATAL_ERROR "Unable to read key 'engine' from 'project.json', error: ${json_error}")
  16. endif()
  17. if(DEFINED ENV{USERPROFILE} AND EXISTS $ENV{USERPROFILE})
  18. set(manifest_path $ENV{USERPROFILE}/.o3de/o3de_manifest.json) # Windows
  19. else()
  20. set(manifest_path $ENV{HOME}/.o3de/o3de_manifest.json) # Unix
  21. endif()
  22. # Read the ~/.o3de/o3de_manifest.json file and look through the 'engines_path' object.
  23. # Find a key that matches LY_ENGINE_NAME_TO_USE and use that as the engine path.
  24. if(EXISTS ${manifest_path})
  25. file(READ ${manifest_path} manifest_json)
  26. string(JSON engines_path_count ERROR_VARIABLE json_error LENGTH ${manifest_json} engines_path)
  27. if(json_error)
  28. message(FATAL_ERROR "Unable to read key 'engines_path' from '${manifest_path}', error: ${json_error}")
  29. endif()
  30. string(JSON engines_path_type ERROR_VARIABLE json_error TYPE ${manifest_json} engines_path)
  31. if(json_error OR NOT ${engines_path_type} STREQUAL "OBJECT")
  32. message(FATAL_ERROR "Type of 'engines_path' in '${manifest_path}' is not a JSON Object, error: ${json_error}")
  33. endif()
  34. math(EXPR engines_path_count "${engines_path_count}-1")
  35. foreach(engine_path_index RANGE ${engines_path_count})
  36. string(JSON engine_name ERROR_VARIABLE json_error MEMBER ${manifest_json} engines_path ${engine_path_index})
  37. if(json_error)
  38. message(FATAL_ERROR "Unable to read 'engines_path/${engine_path_index}' from '${manifest_path}', error: ${json_error}")
  39. endif()
  40. if(LY_ENGINE_NAME_TO_USE STREQUAL engine_name)
  41. string(JSON engine_path ERROR_VARIABLE json_error GET ${manifest_json} engines_path ${engine_name})
  42. if(json_error)
  43. message(FATAL_ERROR "Unable to read value from 'engines_path/${engine_name}', error: ${json_error}")
  44. endif()
  45. if(engine_path)
  46. list(APPEND CMAKE_MODULE_PATH "${engine_path}/cmake")
  47. break()
  48. endif()
  49. endif()
  50. endforeach()
  51. else()
  52. # If the user is passing CMAKE_MODULE_PATH we assume thats where we will find the engine
  53. if(NOT CMAKE_MODULE_PATH)
  54. message(FATAL_ERROR "Engine registration is required before configuring a project. Please register an engine by running 'scripts/o3de register --this-engine'")
  55. endif()
  56. endif()