blob: 6d2c10de970bf6f22dba7b3277056718fdcf083f [file] [log] [blame]
John Rosasco24cbdab2019-09-25 14:14:35 -07001#!/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-header', dest='output_header_c', action='store', required=True)
46 parser.add_argument('--output-c-tables', dest='output_c_tables', action='store', required=True)
47
48 args = parser.parse_args()
49
50 assert os.path.exists(args.fidlc_bin)
51 assert os.path.exists(args.fidlgen_bin)
52
53 fidl_files_by_name = GetFIDLFilesByLibraryName(args.sdk_base, args.root)
54
55 fidlc_command = [
56 args.fidlc_bin,
57 '--c-header',
58 args.output_header_c,
59 '--tables',
60 args.output_c_tables,
61 '--json',
62 args.json
63 ]
64
65 for _, fidl_files in fidl_files_by_name.iteritems():
66 fidlc_command.append('--files')
67 for fidl_file in fidl_files:
68 fidl_abspath = os.path.abspath('%s/%s' % (args.sdk_base, fidl_file))
69 fidlc_command.append(fidl_abspath)
70
71 subprocess.check_call(fidlc_command)
72
73 assert os.path.exists(args.json)
74
75 fidlgen_command = [
76 args.fidlgen_bin,
77 '-generators',
78 'cpp',
79 '-include-base',
80 args.include_base,
81 '-json',
82 args.json,
83 '-output-base',
84 args.output_base_cc
85 ]
86
87 subprocess.check_call(fidlgen_command)
88
89 return 0
90
91if __name__ == '__main__':
92 sys.exit(main())