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