1#
2#  Copyright 2021 Google, Inc.
3#
4#  Licensed under the Apache License, Version 2.0 (the "License");
5#  you may not use this file except in compliance with the License.
6#  You may obtain a copy of the License at:
7#
8#  http://www.apache.org/licenses/LICENSE-2.0
9#
10#  Unless required by applicable law or agreed to in writing, software
11#  distributed under the License is distributed on an "AS IS" BASIS,
12#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13#  See the License for the specific language governing permissions and
14#  limitations under the License.
15
16# Generate bundled flat buffers
17#
18# Args:
19#   include_dir: Path to include directory
20#   sources: Flatbuffer source files
21#   gen_header [optional]: Whether to generate headers.
22template("bt_flatc_binary_schema") {
23  action_name = "${target_name}_gen"
24  action(action_name) {
25    forward_variables_from(invoker,
26                           [
27                             "include_dir",
28                             "sources",
29                           ])
30    assert(defined(include_dir), "include_dir must be set")
31    assert(defined(sources), "sources must be set")
32
33    gen_header = defined(invoker.gen_header) && invoker.gen_header
34
35    script = "//common-mk/file_generator_wrapper.py"
36    binfile = "flatc"
37    args = [
38      binfile,
39      "-I",
40      "${include_dir}",
41      "-b",
42      "--schema",
43      "-o",
44      "${target_gen_dir}",
45    ]
46
47    bfbs_names = []
48    srclist = []
49    outputs = []
50
51    foreach(s, sources) {
52      srclist += [ rebase_path(s) ]
53
54      # bfbs get generated into ${target_gen_root} directory
55      name = string_replace(get_path_info(s, "file"), ".fbs", ".bfbs")
56      bfbs_names += [ "${target_gen_dir}/${name}" ]
57      outputs += [ "${target_gen_dir}/${name}" ]
58
59      # headers get generated ito subdirectories based on relative path
60      if (gen_header) {
61        header_name = string_replace(s, ".fbs", ".h")
62        outputs += [ "${target_gen_dir}/${header_name}" ]
63      }
64    }
65
66    # Generate header file as well
67    if (gen_header) {
68      args += [ "--cpp" ]
69    }
70
71    # Actual input files at the end
72    args += srclist
73
74    metadata = {
75      bfbs_outputs = bfbs_names
76    }
77  }
78
79  all_dependent_config_name = "_${target_name}_all_dependent_config"
80  config(all_dependent_config_name) {
81    # Since each header will be generated into a subdirectory, add them to the
82    # include dirs as well
83    gen_dirs = []
84    foreach(s, invoker.sources) {
85      gen_dirs += [ get_path_info(s, "gen_dir") ]
86    }
87
88    include_dirs = [ "${target_gen_dir}" ] + gen_dirs
89  }
90
91  generated_file(target_name) {
92    outputs = [ "${target_gen_dir}/${target_name}.files" ]
93    output_conversion = "list lines"
94    data_keys = [ "bfbs_outputs" ]
95
96    all_dependent_configs = [ ":${all_dependent_config_name}" ]
97    if (defined(invoker.all_dependent_configs)) {
98      all_dependent_configs += invoker.all_dependent_configs
99    }
100
101    deps = [ ":${action_name}" ]
102    if (defined(invoker.deps)) {
103      deps += invoker.deps
104    }
105
106    if (defined(invoker.configs)) {
107      configs += invoker.configs
108    }
109  }
110}
111
112# Generate bundled header
113template("bt_flatc_bundler") {
114  action_name = "${target_name}_gen"
115  action(action_name) {
116    forward_variables_from(invoker, [ "deps" ])
117    assert(defined(deps), "deps must be set")
118    assert(defined(invoker.root_name), "root_name must be set")
119    assert(defined(invoker.filename), "filename must be set")
120    assert(defined(invoker.namespace), "namespace must be set")
121
122    files_list = []
123    foreach(s, deps) {
124      name = get_label_info(s, "name")
125      gen_dir = get_label_info(s, "target_gen_dir")
126      files_list += [ "${gen_dir}/${name}.files" ]
127    }
128
129    script = "//bt/gd/dumpsys/bundler/extract_files_and_call.py"
130    binfile = "${root_out_dir}/bluetooth_flatbuffer_bundler"
131    args = files_list
132    args += [
133      "--",
134      binfile,
135      "-w",
136      "-m",
137      "${invoker.root_name}",
138      "-f",
139      "${invoker.filename}",
140      "-n",
141      "${invoker.namespace}",
142      "-g",
143      "${target_gen_dir}",
144    ]
145
146    outputs = [
147      "${target_gen_dir}/${invoker.filename}.h",
148      "${target_gen_dir}/${invoker.filename}",
149    ]
150
151    metadata = {
152      all_outputs = outputs
153    }
154  }
155
156  all_dependent_config_name = "_${target_name}_all_dependent_config"
157  config(all_dependent_config_name) {
158    include_dirs = [ "${target_gen_dir}" ]
159  }
160
161  generated_file(target_name) {
162    outputs = [ "${target_gen_dir}/${target_name}.files" ]
163    output_conversion = "list lines"
164    data_keys = [ "all_outputs" ]
165
166    all_dependent_configs = [ ":${all_dependent_config_name}" ]
167    if (defined(invoker.all_dependent_configs)) {
168      all_dependent_configs += invoker.all_dependent_configs
169    }
170
171    deps = [ ":${action_name}" ]
172    if (defined(invoker.deps)) {
173      deps += invoker.deps
174    }
175
176    if (defined(invoker.configs)) {
177      configs += invoker.configs
178    }
179  }
180}
181