1#
2#  Copyright 2024 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
17# Generate c++ files for each aconfig file
18#
19# Parameters:
20#   sources: aconfig source files
21#   package: aconfig package
22template("aconfig") {
23  assert(defined(invoker.sources), "sources must be set")
24  assert(defined(invoker.package), "package must be set")
25
26  outdir = rebase_path(target_gen_dir)
27
28  aconfig_cpp_file_name = string_replace(invoker.package, ".", "_")
29
30  aconfig_declarations = []
31
32  foreach(source, invoker.sources) {
33    source = rebase_path(source)
34    aconfig_declarations += [ "--declarations=${source}" ]
35  }
36
37  aconfig_cache = "${target_name}_cache"
38
39  action(aconfig_cache) {
40    script = "//common-mk/file_generator_wrapper.py"
41    args = [
42             "aconfig",
43             "create-cache",
44             "--package=${invoker.package}",
45             "--cache=${outdir}/${aconfig_cache}",
46           ] + aconfig_declarations
47
48    sources = invoker.sources
49    outputs = [ "${outdir}/${aconfig_cache}" ]
50  }
51
52  action("${target_name}_cpp") {
53    script = "//common-mk/file_generator_wrapper.py"
54    args = [
55      "aconfig",
56      "create-cpp-lib",
57      "--cache=${outdir}/${aconfig_cache}",
58      "--out=${outdir}",
59    ]
60
61    outputs = [
62      "${outdir}/include/${aconfig_cpp_file_name}.h",
63      "${outdir}/${aconfig_cpp_file_name}.cc",
64    ]
65
66    deps = [ ":${aconfig_cache}" ]
67  }
68
69  all_dependent_config_name = "_${target_name}_all_dependent_config"
70  config(all_dependent_config_name) {
71    include_dirs = [ "${outdir}/include" ]
72  }
73
74  static_library(target_name) {
75    sources = [ "${outdir}/${aconfig_cpp_file_name}.cc" ]
76    deps = [ ":${target_name}_cpp" ] + invoker.deps
77    all_dependent_configs = [ ":${all_dependent_config_name}" ]
78  }
79}
80