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 # Look for imports in the same directory as invoker.source by default. 71 default_import_dir = get_path_info(invoker.source, "abspath") 72 default_import_dir = get_path_info(default_import_dir, "dir") 73 default_import_dir = rebase_path(default_import_dir, root_build_dir) 74 75 action(target_name + "_header") { 76 script = "$dir_pw_third_party/emboss/embossc_runner.py" 77 78 args = [ 79 rebase_path("$dir_pw_third_party_emboss/embossc", root_build_dir), 80 "--generate", 81 "cc", 82 "--import-dir", 83 default_import_dir, 84 "--output-path", 85 rebase_path(output_path, root_build_dir), 86 rebase_path(invoker.source, root_build_dir), 87 ] 88 if (defined(invoker.import_dirs)) { 89 foreach(dir, invoker.import_dirs) { 90 args += [ 91 "--import-dir", 92 dir, 93 ] 94 } 95 } 96 97 sources = [ 98 "$dir_pw_third_party_emboss/compiler/back_end/__init__.py", 99 "$dir_pw_third_party_emboss/compiler/back_end/cpp/__init__.py", 100 "$dir_pw_third_party_emboss/compiler/back_end/cpp/emboss_codegen_cpp.py", 101 "$dir_pw_third_party_emboss/compiler/back_end/cpp/generated_code_templates", 102 "$dir_pw_third_party_emboss/compiler/back_end/cpp/header_generator.py", 103 "$dir_pw_third_party_emboss/compiler/back_end/util/__init__.py", 104 "$dir_pw_third_party_emboss/compiler/back_end/util/code_template.py", 105 "$dir_pw_third_party_emboss/compiler/front_end/__init__.py", 106 "$dir_pw_third_party_emboss/compiler/front_end/attribute_checker.py", 107 "$dir_pw_third_party_emboss/compiler/front_end/attributes.py", 108 "$dir_pw_third_party_emboss/compiler/front_end/constraints.py", 109 "$dir_pw_third_party_emboss/compiler/front_end/dependency_checker.py", 110 "$dir_pw_third_party_emboss/compiler/front_end/emboss_front_end.py", 111 "$dir_pw_third_party_emboss/compiler/front_end/error_examples", 112 "$dir_pw_third_party_emboss/compiler/front_end/expression_bounds.py", 113 "$dir_pw_third_party_emboss/compiler/front_end/glue.py", 114 "$dir_pw_third_party_emboss/compiler/front_end/lr1.py", 115 "$dir_pw_third_party_emboss/compiler/front_end/module_ir.py", 116 "$dir_pw_third_party_emboss/compiler/front_end/parser.py", 117 "$dir_pw_third_party_emboss/compiler/front_end/prelude.emb", 118 "$dir_pw_third_party_emboss/compiler/front_end/reserved_words", 119 "$dir_pw_third_party_emboss/compiler/front_end/symbol_resolver.py", 120 "$dir_pw_third_party_emboss/compiler/front_end/synthetics.py", 121 "$dir_pw_third_party_emboss/compiler/front_end/tokenizer.py", 122 "$dir_pw_third_party_emboss/compiler/front_end/type_check.py", 123 "$dir_pw_third_party_emboss/compiler/front_end/write_inference.py", 124 "$dir_pw_third_party_emboss/compiler/util/error.py", 125 "$dir_pw_third_party_emboss/compiler/util/expression_parser.py", 126 "$dir_pw_third_party_emboss/compiler/util/ir_pb2.py", 127 "$dir_pw_third_party_emboss/compiler/util/ir_util.py", 128 "$dir_pw_third_party_emboss/compiler/util/name_conversion.py", 129 "$dir_pw_third_party_emboss/compiler/util/parser_types.py", 130 "$dir_pw_third_party_emboss/compiler/util/simple_memoizer.py", 131 "$dir_pw_third_party_emboss/compiler/util/traverse_ir.py", 132 "$dir_pw_third_party_emboss/embossc", 133 invoker.source, 134 ] 135 136 # TODO(benlawson): Use a depfile for adding imports to inputs (https://github.com/google/emboss/issues/68). 137 if (defined(invoker.imports)) { 138 inputs = invoker.imports 139 } 140 141 outputs = [ compiled_header_path ] 142 } 143 144 config(target_name + "_emboss_config") { 145 include_dirs = [ 146 "$dir_pw_third_party_emboss", 147 root_gen_dir, 148 ] 149 } 150 151 source_set(target_name) { 152 forward_variables_from(invoker, "*") 153 154 sources = [ compiled_header_path ] 155 156 if (!defined(invoker.deps)) { 157 deps = [] 158 } 159 deps += [ "$dir_pw_third_party/emboss:cpp_utils" ] 160 public_deps = [ ":" + target_name + "_header" ] 161 162 if (!defined(invoker.public_configs)) { 163 public_configs = [] 164 } 165 public_configs += [ ":" + target_name + "_emboss_config" ] 166 } 167} 168