1#!/usr/bin/env python
2
3# Copyright 2019 Google LLC.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""
8 Generate C/C++ headers and source files from the set of FIDL files
9 specified in the meta.json manifest.
10"""
11
12import argparse
13import collections
14import json
15import os
16import subprocess
17import sys
18
19def 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
29def GetFIDLFilesByLibraryName(sdk_base, root):
30  libraries = collections.OrderedDict()
31  GetFIDLFilesRecursive(libraries, sdk_base, root)
32  return libraries
33
34def 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='store', required=True)
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('--include-base', dest='include_base', action='store', required=True)
44  parser.add_argument('--output-base-cc', dest='output_base_cc', action='store', required=True)
45  parser.add_argument('--output-c-tables', dest='output_c_tables', action='store', required=True)
46
47  args = parser.parse_args()
48
49  assert os.path.exists(args.fidlc_bin)
50  assert os.path.exists(args.fidlgen_bin)
51
52  fidl_files_by_name = GetFIDLFilesByLibraryName(args.sdk_base, args.root)
53
54  fidlc_command = [
55    args.fidlc_bin,
56    '--tables',
57    args.output_c_tables,
58    '--json',
59    args.json
60  ]
61
62  for _, fidl_files in fidl_files_by_name.iteritems():
63    fidlc_command.append('--files')
64    for fidl_file in fidl_files:
65      fidl_abspath = os.path.abspath('%s/%s' % (args.sdk_base, fidl_file))
66      fidlc_command.append(fidl_abspath)
67
68  subprocess.check_call(fidlc_command)
69
70  assert os.path.exists(args.json)
71
72  fidlgen_command = [
73    args.fidlgen_bin,
74    '-generators',
75    'cpp',
76    '-include-base',
77    args.include_base,
78    '-json',
79    args.json,
80    '-output-base',
81    args.output_base_cc
82  ]
83
84  subprocess.check_call(fidlgen_command)
85
86  return 0
87
88if __name__ == '__main__':
89  sys.exit(main())
90