# 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) 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", "--no-cc-enum-traits", "--output-path", rebase_path(output_path, root_build_dir), rebase_path(invoker.source, root_build_dir), ] # Look for imports in the root directory and Pigweed root by default. # This is intended to match the official Bazel rule. default_import_dirs = [ rebase_path("//", root_build_dir), "$dir_pigweed", ] foreach(dir, default_import_dirs) { args += [ "--import-dir", rebase_path(dir, 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/__init__.py", "$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/attributes.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/__init__.py", "$dir_pw_third_party_emboss/compiler/util/attribute_util.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/resources.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" ] } }