O3DEJson.cmake 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. include_guard()
  9. #! o3de_read_json_external_subdirs
  10. # Read the "external_subdirectories" array from a *.json file
  11. # External subdirectories are any folders with CMakeLists.txt in them
  12. # This could be regular subdirectories, Gems(contains an additional gem.json),
  13. # Restricted folders(contains an additional restricted.json), etc...
  14. #
  15. # \arg:output_external_subdirs name of output variable to store external subdirectories into
  16. # \arg:input_json_path path to the *.json file to load and read the external subdirectories from
  17. # \return: external subdirectories as is from the json file.
  18. function(o3de_read_json_external_subdirs output_external_subdirs input_json_path)
  19. o3de_read_json_array(json_array ${input_json_path} "external_subdirectories")
  20. set(${output_external_subdirs} ${json_array} PARENT_SCOPE)
  21. endfunction()
  22. #! read_json_array
  23. # Reads the a json array field into a cmake list variable
  24. function(o3de_read_json_array read_output_array input_json_path array_key)
  25. o3de_file_read_cache(${input_json_path} manifest_json_data)
  26. string(JSON array_count ERROR_VARIABLE manifest_json_error
  27. LENGTH ${manifest_json_data} ${array_key})
  28. if(manifest_json_error)
  29. # There is no key, return
  30. return()
  31. endif()
  32. if(array_count GREATER 0)
  33. math(EXPR array_range "${array_count}-1")
  34. foreach(array_index RANGE ${array_range})
  35. string(JSON array_element ERROR_VARIABLE manifest_json_error
  36. GET ${manifest_json_data} ${array_key} "${array_index}")
  37. if(manifest_json_error)
  38. message(WARNING "Error reading field at index ${array_index} in \"${array_key}\" JSON array: ${manifest_json_error}")
  39. return()
  40. endif()
  41. list(APPEND array_elements ${array_element})
  42. endforeach()
  43. endif()
  44. set(${read_output_array} ${array_elements} PARENT_SCOPE)
  45. endfunction()
  46. function(o3de_read_json_key output_value input_json_path key)
  47. o3de_file_read_cache(${input_json_path} manifest_json_data)
  48. string(JSON value ERROR_VARIABLE manifest_json_error GET ${manifest_json_data} ${key})
  49. if(manifest_json_error)
  50. message(WARNING "Error reading field at key ${key} in file \"${input_json_path}\" : ${manifest_json_error}")
  51. return()
  52. endif()
  53. set(${output_value} ${value} PARENT_SCOPE)
  54. endfunction()
  55. function(o3de_read_optional_json_key output_value input_json_path key)
  56. o3de_file_read_cache(${input_json_path} manifest_json_data)
  57. string(JSON value ERROR_VARIABLE manifest_json_error GET ${manifest_json_data} ${key})
  58. if(manifest_json_error)
  59. return()
  60. endif()
  61. set(${output_value} ${value} PARENT_SCOPE)
  62. endfunction()
  63. #! o3de_read_json_keys: read multiple json keys at once. More efficient
  64. # than using o3de_read_json_key multiple times.
  65. # \arg:input_json_path - the path to the json file
  66. # \args: pairs of 'key' and 'output_value'
  67. # e.g. To read the key1 and key2 values
  68. # o3de_read_json_keys(c:/myfile.json 'key1' out_key1_value 'key2' out_key2_value)
  69. function(o3de_read_json_keys input_json_path)
  70. o3de_file_read_cache(${input_json_path} manifest_json_data)
  71. unset(key)
  72. foreach(arg IN LISTS ARGN)
  73. if(NOT DEFINED key)
  74. set(key ${arg})
  75. else()
  76. string(JSON value ERROR_VARIABLE manifest_json_error GET ${manifest_json_data} ${key})
  77. if(manifest_json_error)
  78. message(WARNING "Error reading field at key ${key} in file \"${input_json_path}\" : ${manifest_json_error}")
  79. else()
  80. set(${arg} ${value} PARENT_SCOPE)
  81. endif()
  82. unset(key)
  83. endif()
  84. endforeach()
  85. endfunction()