write_build_config.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python
  2. # Copyright (c) 2021 Project CHIP Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """
  16. Writes a JSON file containing build configuration information.
  17. See build/chip/java/tests/expected_output/* for example build configuration
  18. files.
  19. """
  20. import json
  21. import optparse
  22. import os
  23. import sys
  24. def LoadBuildConfigs(paths):
  25. build_configs = []
  26. for path in paths:
  27. with open(path, 'r') as file:
  28. build_configs.append(json.load(file))
  29. return build_configs
  30. def ParseGnList(value):
  31. if not value:
  32. return []
  33. if value.startswith('[') and value.endswith(']'):
  34. gn_list = value.strip("[]").replace(
  35. "\"", "").replace(" ", "").split(",")
  36. if not gn_list[0]:
  37. return []
  38. else:
  39. return gn_list
  40. def GetAllDependentJars(deps_configs_data):
  41. configs_to_process = deps_configs_data
  42. deps_jars = set()
  43. while configs_to_process:
  44. deps_config = configs_to_process.pop()
  45. child_configs = LoadBuildConfigs(
  46. deps_config['deps_info']['deps_configs'])
  47. deps_jars.add(deps_config['deps_info']['jar_path'])
  48. configs_to_process += child_configs
  49. return deps_jars
  50. def main(argv):
  51. parser = optparse.OptionParser()
  52. parser.add_option('--build-config', help='Path to build_config output')
  53. parser.add_option('--deps-configs',
  54. help='GN-list of dependent build_config files')
  55. parser.add_option('--jar-path', help='Path to the .jar')
  56. options, args = parser.parse_args(argv)
  57. deps_configs_list = ParseGnList(options.deps_configs)
  58. deps_configs_data = LoadBuildConfigs(deps_configs_list)
  59. deps_jars_set = GetAllDependentJars(deps_configs_data)
  60. config = {
  61. "deps_info": {
  62. "name": os.path.basename(options.build_config),
  63. "jar_path": options.jar_path,
  64. # List of configs depended on by this config. Not recursive.
  65. "deps_configs": deps_configs_list,
  66. # List of all jars needed by all dependencies of this config (recursive).
  67. "deps_jars": list(deps_jars_set)
  68. }
  69. }
  70. with open(options.build_config, 'w') as file:
  71. json.dump(config, file)
  72. if __name__ == '__main__':
  73. sys.exit(main(sys.argv[1:]))