aboutsummaryrefslogtreecommitdiff
path: root/third_party/emboss/build_defs.gni
blob: e5ca2046f7bacae38129616c1b452afef139bc71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Copyright 2022 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

import("//build_overrides/pigweed.gni")
import("$dir_pw_third_party/emboss/emboss.gni")

# Compiles a .emb file into a .h file.
# $dir_pw_third_party_emboss must be set to the path of your Emboss
# installation.
#
# Parameters
#
#   source (required)
#     [path] The path to the .emb file that is to be compiled.
#
#   import_dirs (optional)
#     [list of paths] The list of directories to search for imported .emb files,
#     in the order they should be searched. The directory that `source` is in is
#     always searched first.
#
#   imports (optional)
#     [list of paths] Paths to any imported .emb files, including those imported
#     resursively.
#
#   deps (optional)
#     [list of targets] Paths to other emboss_cc_library targets that are
#     imported by this target.
template("emboss_cc_library") {
  assert(defined(invoker.source), "Need source arg for emboss_cc_library")
  assert(
      "$dir_pw_third_party_emboss" != "",
      "\$dir_pw_third_party_emboss must be set to the path of your Emboss installation")

  # The --output-path arg to the embossc script only specifies the
  # prefix of the path at which the generated header is placed. To this
  # prefix, the script appends the entire path of the input Emboss source,
  # which is provided as rebase_path(invoker.source, root_build_dir).

  # rebase_path(invoker.source, root_build_dir) will always start with a number
  # of updirs (e.g. "../../../").

  # In order for the compiled header path in the embossc script to resolve to
  # $target_gen_dir as we desire, we must provide an --output-path arg that
  # resolves to $target_gen_dir after rebase_path(invoker.source,
  # root_build_dir) is appended to it. To achieve this, we specify output-path
  # to be $root_gen_dir followed by a number of fake directories needed to
  # cancel out these starting updirs.
  compiled_header_path = "$target_gen_dir/" + invoker.source + ".h"
  path_sep = "/"
  elements = string_split(rebase_path(invoker.source, root_build_dir), path_sep)
  updirs = filter_include(elements, [ ".." ])

  fakedirs = []
  foreach(element, updirs) {
    fakedirs += [ "fake" ]
  }
  output_path = root_gen_dir + path_sep + string_join(path_sep, fakedirs)

  # Look for imports in the same directory as invoker.source by default.
  default_import_dir = get_path_info(invoker.source, "abspath")
  default_import_dir = get_path_info(default_import_dir, "dir")
  default_import_dir = rebase_path(default_import_dir, root_build_dir)

  action("${target_name}_header") {
    script = "$dir_pw_third_party/emboss/embossc_runner.py"

    args = [
      rebase_path("$dir_pw_third_party_emboss/embossc", root_build_dir),
      "--generate",
      "cc",
      "--import-dir",
      default_import_dir,
      "--output-path",
      rebase_path(output_path, root_build_dir),
      rebase_path(invoker.source, root_build_dir),
    ]
    if (defined(invoker.import_dirs)) {
      foreach(dir, invoker.import_dirs) {
        args += [
          "--import-dir",
          rebase_path(dir, root_build_dir),
        ]
      }
    }

    sources = [
      "$dir_pw_third_party_emboss/compiler/back_end/__init__.py",
      "$dir_pw_third_party_emboss/compiler/back_end/cpp/__init__.py",
      "$dir_pw_third_party_emboss/compiler/back_end/cpp/emboss_codegen_cpp.py",
      "$dir_pw_third_party_emboss/compiler/back_end/cpp/generated_code_templates",
      "$dir_pw_third_party_emboss/compiler/back_end/cpp/header_generator.py",
      "$dir_pw_third_party_emboss/compiler/back_end/util/__init__.py",
      "$dir_pw_third_party_emboss/compiler/back_end/util/code_template.py",
      "$dir_pw_third_party_emboss/compiler/front_end/__init__.py",
      "$dir_pw_third_party_emboss/compiler/front_end/attribute_checker.py",
      "$dir_pw_third_party_emboss/compiler/front_end/attributes.py",
      "$dir_pw_third_party_emboss/compiler/front_end/constraints.py",
      "$dir_pw_third_party_emboss/compiler/front_end/dependency_checker.py",
      "$dir_pw_third_party_emboss/compiler/front_end/emboss_front_end.py",
      "$dir_pw_third_party_emboss/compiler/front_end/error_examples",
      "$dir_pw_third_party_emboss/compiler/front_end/expression_bounds.py",
      "$dir_pw_third_party_emboss/compiler/front_end/glue.py",
      "$dir_pw_third_party_emboss/compiler/front_end/lr1.py",
      "$dir_pw_third_party_emboss/compiler/front_end/module_ir.py",
      "$dir_pw_third_party_emboss/compiler/front_end/parser.py",
      "$dir_pw_third_party_emboss/compiler/front_end/prelude.emb",
      "$dir_pw_third_party_emboss/compiler/front_end/reserved_words",
      "$dir_pw_third_party_emboss/compiler/front_end/symbol_resolver.py",
      "$dir_pw_third_party_emboss/compiler/front_end/synthetics.py",
      "$dir_pw_third_party_emboss/compiler/front_end/tokenizer.py",
      "$dir_pw_third_party_emboss/compiler/front_end/type_check.py",
      "$dir_pw_third_party_emboss/compiler/front_end/write_inference.py",
      "$dir_pw_third_party_emboss/compiler/util/error.py",
      "$dir_pw_third_party_emboss/compiler/util/expression_parser.py",
      "$dir_pw_third_party_emboss/compiler/util/ir_pb2.py",
      "$dir_pw_third_party_emboss/compiler/util/ir_util.py",
      "$dir_pw_third_party_emboss/compiler/util/name_conversion.py",
      "$dir_pw_third_party_emboss/compiler/util/parser_types.py",
      "$dir_pw_third_party_emboss/compiler/util/simple_memoizer.py",
      "$dir_pw_third_party_emboss/compiler/util/traverse_ir.py",
      "$dir_pw_third_party_emboss/embossc",
      invoker.source,
    ]

    # TODO(benlawson): Use a depfile for adding imports to inputs (https://github.com/google/emboss/issues/68).
    if (defined(invoker.imports)) {
      inputs = invoker.imports
    }

    outputs = [ compiled_header_path ]
  }

  config("${target_name}_emboss_config") {
    include_dirs = [
      "$dir_pw_third_party_emboss",
      root_gen_dir,
    ]
  }

  # Since the emboss_cc_library template is used in non-Pigweed environments, this target is not pw_source_set,
  # which restricts visibility by default.
  source_set(target_name) {
    forward_variables_from(invoker, "*")

    sources = [ compiled_header_path ]

    public_deps = [
      ":${target_name}_header",
      "$dir_pw_third_party/emboss:cpp_utils",
    ]

    if (!defined(invoker.public_configs)) {
      public_configs = []
    }
    public_configs += [ ":${target_name}_emboss_config" ]
  }
}