xref: /aosp_15_r20/external/skia/build/fuchsia/fidl_gen_cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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
46  args = parser.parse_args()
47
48  assert os.path.exists(args.fidlc_bin)
49  assert os.path.exists(args.fidlgen_bin)
50
51  fidl_files_by_name = GetFIDLFilesByLibraryName(args.sdk_base, args.root)
52
53  fidlc_command = [
54    args.fidlc_bin,
55    '--json',
56    args.json
57  ]
58
59  for _, fidl_files in fidl_files_by_name.iteritems():
60    fidlc_command.append('--files')
61    for fidl_file in fidl_files:
62      fidl_abspath = os.path.abspath('%s/%s' % (args.sdk_base, fidl_file))
63      fidlc_command.append(fidl_abspath)
64
65  subprocess.check_call(fidlc_command)
66
67  assert os.path.exists(args.json)
68
69  fidlgen_command = [
70    args.fidlgen_bin,
71    '-generators',
72    'cpp',
73    '-include-base',
74    args.include_base,
75    '-json',
76    args.json,
77    '-output-base',
78    args.output_base_cc
79  ]
80
81  subprocess.check_call(fidlgen_command)
82
83  return 0
84
85if __name__ == '__main__':
86  sys.exit(main())
87