xref: /aosp_15_r20/external/pigweed/third_party/emboss/build_defs.gni (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2022 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15import("//build_overrides/pigweed.gni")
16import("$dir_pw_third_party/emboss/emboss.gni")
17
18# Compiles a .emb file into a .h file.
19# $dir_pw_third_party_emboss must be set to the path of your Emboss
20# installation.
21#
22# Parameters
23#
24#   source (required)
25#     [path] The path to the .emb file that is to be compiled.
26#
27#   import_dirs (optional)
28#     [list of paths] The list of directories to search for imported .emb files,
29#     in the order they should be searched. The directory that `source` is in is
30#     always searched first.
31#
32#   imports (optional)
33#     [list of paths] Paths to any imported .emb files, including those imported
34#     resursively.
35#
36#   deps (optional)
37#     [list of targets] Paths to other emboss_cc_library targets that are
38#     imported by this target.
39template("emboss_cc_library") {
40  assert(defined(invoker.source), "Need source arg for emboss_cc_library")
41  assert(
42      "$dir_pw_third_party_emboss" != "",
43      "\$dir_pw_third_party_emboss must be set to the path of your Emboss installation")
44
45  # The --output-path arg to the embossc script only specifies the
46  # prefix of the path at which the generated header is placed. To this
47  # prefix, the script appends the entire path of the input Emboss source,
48  # which is provided as rebase_path(invoker.source, root_build_dir).
49
50  # rebase_path(invoker.source, root_build_dir) will always start with a number
51  # of updirs (e.g. "../../../").
52
53  # In order for the compiled header path in the embossc script to resolve to
54  # $target_gen_dir as we desire, we must provide an --output-path arg that
55  # resolves to $target_gen_dir after rebase_path(invoker.source,
56  # root_build_dir) is appended to it. To achieve this, we specify output-path
57  # to be $root_gen_dir followed by a number of fake directories needed to
58  # cancel out these starting updirs.
59  compiled_header_path = "$target_gen_dir/" + invoker.source + ".h"
60  path_sep = "/"
61  elements = string_split(rebase_path(invoker.source, root_build_dir), path_sep)
62  updirs = filter_include(elements, [ ".." ])
63
64  fakedirs = []
65  foreach(element, updirs) {
66    fakedirs += [ "fake" ]
67  }
68  output_path = root_gen_dir + path_sep + string_join(path_sep, fakedirs)
69
70  action("${target_name}_header") {
71    script = "$dir_pw_third_party/emboss/embossc_runner.py"
72
73    args = [
74      rebase_path("$dir_pw_third_party_emboss/embossc", root_build_dir),
75      "--generate",
76      "cc",
77      "--no-cc-enum-traits",
78      "--output-path",
79      rebase_path(output_path, root_build_dir),
80      rebase_path(invoker.source, root_build_dir),
81    ]
82
83    # Look for imports in the root directory and Pigweed root by default.
84    # This is intended to match the official Bazel rule.
85    default_import_dirs = [
86      rebase_path("//", root_build_dir),
87      "$dir_pigweed",
88    ]
89    foreach(dir, default_import_dirs) {
90      args += [
91        "--import-dir",
92        rebase_path(dir, root_build_dir),
93      ]
94    }
95
96    if (defined(invoker.import_dirs)) {
97      foreach(dir, invoker.import_dirs) {
98        args += [
99          "--import-dir",
100          rebase_path(dir, root_build_dir),
101        ]
102      }
103    }
104
105    sources = [
106      "$dir_pw_third_party_emboss/compiler/__init__.py",
107      "$dir_pw_third_party_emboss/compiler/back_end/__init__.py",
108      "$dir_pw_third_party_emboss/compiler/back_end/cpp/__init__.py",
109      "$dir_pw_third_party_emboss/compiler/back_end/cpp/attributes.py",
110      "$dir_pw_third_party_emboss/compiler/back_end/cpp/emboss_codegen_cpp.py",
111      "$dir_pw_third_party_emboss/compiler/back_end/cpp/generated_code_templates",
112      "$dir_pw_third_party_emboss/compiler/back_end/cpp/header_generator.py",
113      "$dir_pw_third_party_emboss/compiler/back_end/util/__init__.py",
114      "$dir_pw_third_party_emboss/compiler/back_end/util/code_template.py",
115      "$dir_pw_third_party_emboss/compiler/front_end/__init__.py",
116      "$dir_pw_third_party_emboss/compiler/front_end/attribute_checker.py",
117      "$dir_pw_third_party_emboss/compiler/front_end/attributes.py",
118      "$dir_pw_third_party_emboss/compiler/front_end/constraints.py",
119      "$dir_pw_third_party_emboss/compiler/front_end/dependency_checker.py",
120      "$dir_pw_third_party_emboss/compiler/front_end/emboss_front_end.py",
121      "$dir_pw_third_party_emboss/compiler/front_end/error_examples",
122      "$dir_pw_third_party_emboss/compiler/front_end/expression_bounds.py",
123      "$dir_pw_third_party_emboss/compiler/front_end/glue.py",
124      "$dir_pw_third_party_emboss/compiler/front_end/lr1.py",
125      "$dir_pw_third_party_emboss/compiler/front_end/module_ir.py",
126      "$dir_pw_third_party_emboss/compiler/front_end/parser.py",
127      "$dir_pw_third_party_emboss/compiler/front_end/prelude.emb",
128      "$dir_pw_third_party_emboss/compiler/front_end/reserved_words",
129      "$dir_pw_third_party_emboss/compiler/front_end/symbol_resolver.py",
130      "$dir_pw_third_party_emboss/compiler/front_end/synthetics.py",
131      "$dir_pw_third_party_emboss/compiler/front_end/tokenizer.py",
132      "$dir_pw_third_party_emboss/compiler/front_end/type_check.py",
133      "$dir_pw_third_party_emboss/compiler/front_end/write_inference.py",
134      "$dir_pw_third_party_emboss/compiler/util/__init__.py",
135      "$dir_pw_third_party_emboss/compiler/util/attribute_util.py",
136      "$dir_pw_third_party_emboss/compiler/util/error.py",
137      "$dir_pw_third_party_emboss/compiler/util/expression_parser.py",
138      "$dir_pw_third_party_emboss/compiler/util/ir_data.py",
139      "$dir_pw_third_party_emboss/compiler/util/ir_data_fields.py",
140      "$dir_pw_third_party_emboss/compiler/util/ir_data_utils.py",
141      "$dir_pw_third_party_emboss/compiler/util/ir_util.py",
142      "$dir_pw_third_party_emboss/compiler/util/name_conversion.py",
143      "$dir_pw_third_party_emboss/compiler/util/parser_types.py",
144      "$dir_pw_third_party_emboss/compiler/util/resources.py",
145      "$dir_pw_third_party_emboss/compiler/util/simple_memoizer.py",
146      "$dir_pw_third_party_emboss/compiler/util/traverse_ir.py",
147      "$dir_pw_third_party_emboss/embossc",
148      invoker.source,
149    ]
150
151    # TODO(benlawson): Use a depfile for adding imports to inputs (https://github.com/google/emboss/issues/68).
152    if (defined(invoker.imports)) {
153      inputs = invoker.imports
154    }
155
156    outputs = [ compiled_header_path ]
157  }
158
159  config("${target_name}_emboss_config") {
160    include_dirs = [
161      "$dir_pw_third_party_emboss",
162      root_gen_dir,
163    ]
164  }
165
166  # Since the emboss_cc_library template is used in non-Pigweed environments, this target is not pw_source_set,
167  # which restricts visibility by default.
168  source_set(target_name) {
169    forward_variables_from(invoker, "*")
170
171    sources = [ compiled_header_path ]
172
173    if (!defined(invoker.public_deps)) {
174      public_deps = []
175    }
176    public_deps += [
177      ":${target_name}_header",
178      "$dir_pw_third_party/emboss:cpp_utils",
179    ]
180
181    if (!defined(invoker.public_configs)) {
182      public_configs = []
183    }
184    public_configs += [ ":${target_name}_emboss_config" ]
185  }
186}
187