blob: 74bd061aebf95a87fa8d2c56784521f5e7745c98 [file] [log] [blame]
# 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",
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,
]
}
source_set(target_name) {
forward_variables_from(invoker, "*")
sources = [ compiled_header_path ]
if (!defined(invoker.deps)) {
deps = []
}
deps += [ "$dir_pw_third_party/emboss:cpp_utils" ]
public_deps = [ ":" + target_name + "_header" ]
if (!defined(invoker.public_configs)) {
public_configs = []
}
public_configs += [ ":" + target_name + "_emboss_config" ]
}
}