|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright 2013 The Flutter Authors. All rights reserved. |
| 4 | +# Use of this source code is governed by a BSD-style license that can be |
| 5 | +# found in the LICENSE file. |
| 6 | + |
| 7 | +""" Generate C/C++ headers and source files from the set of FIDL files specified |
| 8 | +in the meta.json manifest. |
| 9 | +""" |
| 10 | + |
| 11 | + |
| 12 | +import argparse |
| 13 | +import collections |
| 14 | +import json |
| 15 | +import os |
| 16 | +import subprocess |
| 17 | +import sys |
| 18 | + |
| 19 | +def GetFIDLFilesRecursive(libraries, sdk_base, path): |
| 20 | + with open(path) as json_file: |
| 21 | + parsed = json.load(json_file) |
| 22 | + result = [] |
| 23 | + deps = parsed['deps'] |
| 24 | + for dep in deps: |
| 25 | + dep_meta_json = os.path.abspath('%s/fidl/%s/meta.json' % (sdk_base, dep)) |
| 26 | + GetFIDLFilesRecursive(libraries, sdk_base, dep_meta_json) |
| 27 | + libraries[parsed['name']] = result + parsed['sources'] |
| 28 | + |
| 29 | +def GetFIDLFilesByLibraryName(sdk_base, root): |
| 30 | + libraries = collections.OrderedDict() |
| 31 | + GetFIDLFilesRecursive(libraries, sdk_base, root) |
| 32 | + return libraries |
| 33 | + |
| 34 | +def main(): |
| 35 | + parser = argparse.ArgumentParser(); |
| 36 | + |
| 37 | + parser.add_argument('--fidlc-bin', dest='fidlc_bin', action='store', required=True) |
| 38 | + parser.add_argument('--fidlgen-bin', dest='fidlgen_bin', action='append', required=False) |
| 39 | + |
| 40 | + parser.add_argument('--sdk-base', dest='sdk_base', action='store', required=True) |
| 41 | + parser.add_argument('--root', dest='root', action='store', required=True) |
| 42 | + parser.add_argument('--json', dest='json', action='store', required=True) |
| 43 | + parser.add_argument('--fidlgen-output-root', dest='fidlgen_output_root', action='store', required=False) |
| 44 | + parser.add_argument('--target-api-level', dest='target_api_level', action='store', required=False) |
| 45 | + |
| 46 | + args = parser.parse_args() |
| 47 | + |
| 48 | + assert os.path.exists(args.fidlc_bin) |
| 49 | + |
| 50 | + fidl_files_by_name = GetFIDLFilesByLibraryName(args.sdk_base, args.root) |
| 51 | + |
| 52 | + fidlc_command = [ |
| 53 | + args.fidlc_bin, |
| 54 | + '--json', |
| 55 | + args.json |
| 56 | + ] |
| 57 | + |
| 58 | + if args.target_api_level: |
| 59 | + fidlc_command += [ |
| 60 | + '--available', |
| 61 | + 'fuchsia:{api_level}'.format(api_level=args.target_api_level), |
| 62 | + ] |
| 63 | + |
| 64 | + # Create an iterator that works on both python3 and python2 |
| 65 | + try: |
| 66 | + fidl_files_by_name_iter = list(fidl_files_by_name.items()) |
| 67 | + except AttributeError: |
| 68 | + fidl_files_by_name_iter = iter(fidl_files_by_name.items()) |
| 69 | + |
| 70 | + for _, fidl_files in fidl_files_by_name_iter: |
| 71 | + fidlc_command.append('--files') |
| 72 | + for fidl_file in fidl_files: |
| 73 | + fidl_abspath = os.path.abspath('%s/%s' % (args.sdk_base, fidl_file)) |
| 74 | + fidlc_command.append(fidl_abspath) |
| 75 | + |
| 76 | + subprocess.check_call(fidlc_command) |
| 77 | + |
| 78 | + if args.fidlgen_output_root: |
| 79 | + assert os.path.exists(args.json) |
| 80 | + for fidlgen_bin in args.fidlgen_bin: |
| 81 | + assert os.path.exists(fidlgen_bin) |
| 82 | + |
| 83 | + fidlgen_command = [ |
| 84 | + fidlgen_bin, |
| 85 | + '-json', |
| 86 | + args.json, |
| 87 | + '-root', |
| 88 | + args.fidlgen_output_root |
| 89 | + ] |
| 90 | + |
| 91 | + subprocess.check_call(fidlgen_command) |
| 92 | + else: |
| 93 | + # --fidlgen-bin and --fidlgen-output-root should be passed in together. |
| 94 | + assert not args.fidlgen_bin |
| 95 | + |
| 96 | + return 0 |
| 97 | + |
| 98 | +if __name__ == '__main__': |
| 99 | + sys.exit(main()) |
0 commit comments