Path: blob/main_old/scripts/gen_angle_gn_info_json.py
1693 views
#!/usr/bin/env python12# Copyright 2018 The ANGLE Project Authors. All rights reserved.3# Use of this source code is governed by a BSD-style license that can be4# found in the LICENSE file.56# This tool will create a json description of the GN build environment that7# can then be used by gen_angle_android_bp.py to build an Android.bp file for8# the Android Soong build system.9# The input to this tool is a list of GN labels for which to capture the build10# information in json:11#12# Generating angle.json needs to be done from within a Chromium build:13# cd <chromium>/src14# gen_angle_gn_info_json.py //third_party/angle:libGLESv2 //third_party/angle:libEGL15#16# This will output an angle.json that can be copied to the angle directory17# within Android.18#19# Optional arguments:20# --gn_out <file> GN output config to use (e.g., out/Default or out/Debug.)21# --output <file> json file to create, default is angle.json22#2324import argparse25import json26import logging27import subprocess28import sys293031def get_json_description(gn_out, target_name):32try:33text_desc = subprocess.check_output(['gn', 'desc', '--format=json', gn_out, target_name])34except subprocess.CalledProcessError as e:35logging.error("e.retcode = %s" % e.returncode)36logging.error("e.cmd = %s" % e.cmd)37logging.error("e.output = %s" % e.output)38try:39json_out = json.loads(text_desc)40except ValueError:41raise ValueError("Unable to decode JSON\ncmd: %s\noutput:\n%s" % (subprocess.list2cmdline(42['gn', 'desc', '--format=json', gn_out, target_name]), text_desc))4344return json_out454647def load_json_deps(desc, gn_out, target_name, all_desc, indent=" "):48"""Extracts dependencies from the given target json description49and recursively extracts json descriptions.5051desc: json description for target_name that includes dependencies52gn_out: GN output file with configuration info53target_name: name of target in desc to lookup deps54all_desc: dependent descriptions added here55indent: Print with indent to show recursion depth56"""57target = desc[target_name]58text_descriptions = []59for dep in target.get('deps', []):60if dep not in all_desc:61logging.debug("dep: %s%s" % (indent, dep))62new_desc = get_json_description(gn_out, dep)63all_desc[dep] = new_desc[dep]64load_json_deps(new_desc, gn_out, dep, all_desc, indent + " ")65else:66logging.debug("dup: %s%s" % (indent, dep))676869def create_build_description(gn_out, targets):70"""Creates the JSON build description by running GN."""7172logging.debug("targets = %s" % targets)73json_descriptions = {}74for target in targets:75logging.debug("target: %s" % (target))76target_desc = get_json_description(gn_out, target)77if (target in target_desc and target not in json_descriptions):78json_descriptions[target] = target_desc[target]79load_json_deps(target_desc, gn_out, target, json_descriptions)80else:81logging.debug("Invalid target: %s" % target)82return json_descriptions838485def main():86logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)87parser = argparse.ArgumentParser(88description='Generate json build information from a GN description.')89parser.add_argument(90'--gn_out',91help='GN output config to use (e.g., out/Default or out/Debug.)',92default='out/Default',93)94parser.add_argument(95'--output',96help='json file to create',97default='angle.json',98)99parser.add_argument(100'targets',101nargs=argparse.REMAINDER,102help='Targets to include in the json (e.g., "//libEGL")')103args = parser.parse_args()104105desc = create_build_description(args.gn_out, args.targets)106fh = open(args.output, "w")107fh.write(json.dumps(desc, indent=4, sort_keys=True))108fh.close()109110print("Output written to: %s" % args.output)111112113if __name__ == '__main__':114sys.exit(main())115116117