1# Copyright 2021 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") 16 17# Note: In general, prefer to import target_types.gni rather than this file. 18# cc_executable.gni and cc_library.gni are both provided by target_types.gni. 19# 20# cc_library.gni is split out from cc_executable.gni because pw_executable 21# templates may need to create pw_source_set targets internally, and can't 22# import target_types.gni because it creates a circular import path. 23 24declare_args() { 25 # Additional build targets to add as dependencies for pw_executable, 26 # pw_static_library, and pw_shared_library targets. The 27 # $dir_pw_build:link_deps target pulls in these libraries. 28 # 29 # pw_build_LINK_DEPS can be used to break circular dependencies for low-level 30 # libraries such as pw_assert. 31 pw_build_LINK_DEPS = [] 32} 33 34# TODO(frolv): The code in all of the templates below is duplicated, with the 35# exception of the target type. This file could be auto-generated with Python. 36 37_supported_toolchain_defaults = [ 38 "configs", 39 "public_deps", 40] 41 42template("pw_source_set") { 43 _pw_source_files = [] 44 45 # Boilerplate for tracking target sources. For more information see 46 # https://pigweed.dev/pw_build/#target-types 47 if (defined(invoker.sources)) { 48 foreach(path, invoker.sources) { 49 _pw_source_files += [ path ] 50 } 51 } 52 if (defined(invoker.public)) { 53 foreach(path, invoker.public) { 54 _pw_source_files += [ path ] 55 } 56 } 57 58 source_set(target_name) { 59 import("$dir_pw_build/defaults.gni") 60 forward_variables_from(invoker, "*", _supported_toolchain_defaults) 61 62 # Ensure that we don't overwrite metadata forwared from the invoker above. 63 if (defined(metadata)) { 64 metadata.pw_source_files = _pw_source_files 65 } else { 66 metadata = { 67 pw_source_files = _pw_source_files 68 } 69 } 70 71 if (!defined(configs)) { 72 configs = [] 73 } 74 if (defined(pw_build_defaults.configs)) { 75 configs += pw_build_defaults.configs 76 } 77 if (defined(remove_configs)) { 78 if (remove_configs != [] && remove_configs[0] == "*") { 79 configs = [] 80 } else { 81 configs += remove_configs # Add configs in case they aren't already 82 configs -= remove_configs # present, then remove them. 83 } 84 } 85 if (defined(invoker.configs)) { 86 configs += invoker.configs 87 } 88 89 if (defined(pw_build_defaults.public_deps)) { 90 public_deps = pw_build_defaults.public_deps 91 } else { 92 public_deps = [] 93 } 94 if (defined(remove_public_deps)) { 95 if (remove_public_deps != [] && remove_public_deps[0] == "*") { 96 public_deps = [] 97 } else { 98 public_deps += remove_public_deps 99 public_deps -= remove_public_deps 100 } 101 } 102 if (defined(invoker.public_deps)) { 103 public_deps += invoker.public_deps 104 } 105 } 106} 107 108template("pw_static_library") { 109 _pw_source_files = [] 110 111 # Boilerplate for tracking target sources. For more information see 112 # https://pigweed.dev/pw_build/#target-types 113 if (defined(invoker.sources)) { 114 foreach(path, invoker.sources) { 115 _pw_source_files += [ path ] 116 } 117 } 118 if (defined(invoker.public)) { 119 foreach(path, invoker.public) { 120 _pw_source_files += [ path ] 121 } 122 } 123 124 _library_output_dir = "${target_out_dir}/lib" 125 if (defined(invoker.output_dir)) { 126 _library_output_dir = invoker.output_dir 127 } 128 129 static_library(target_name) { 130 import("$dir_pw_build/defaults.gni") 131 forward_variables_from(invoker, "*", _supported_toolchain_defaults) 132 133 # Ensure that we don't overwrite metadata forwared from the invoker above. 134 if (defined(metadata)) { 135 metadata.pw_source_files = _pw_source_files 136 } else { 137 metadata = { 138 pw_source_files = _pw_source_files 139 } 140 } 141 142 if (!defined(configs)) { 143 configs = [] 144 } 145 if (defined(pw_build_defaults.configs)) { 146 configs += pw_build_defaults.configs 147 } 148 if (defined(remove_configs)) { 149 if (remove_configs != [] && remove_configs[0] == "*") { 150 configs = [] 151 } else { 152 configs += remove_configs # Add configs in case they aren't already 153 configs -= remove_configs # present, then remove them. 154 } 155 } 156 if (defined(invoker.configs)) { 157 configs += invoker.configs 158 } 159 160 public_deps = [ "$dir_pw_build:link_deps" ] 161 if (defined(pw_build_defaults.public_deps)) { 162 public_deps += pw_build_defaults.public_deps 163 } 164 if (defined(remove_public_deps)) { 165 if (remove_public_deps != [] && remove_public_deps[0] == "*") { 166 public_deps = [] 167 } else { 168 public_deps += remove_public_deps 169 public_deps -= remove_public_deps 170 } 171 } 172 if (defined(invoker.public_deps)) { 173 public_deps += invoker.public_deps 174 } 175 176 output_dir = _library_output_dir 177 } 178} 179 180template("pw_shared_library") { 181 _pw_source_files = [] 182 183 # Boilerplate for tracking target sources. For more information see 184 # https://pigweed.dev/pw_build/#target-types 185 if (defined(invoker.sources)) { 186 foreach(path, invoker.sources) { 187 _pw_source_files += [ path ] 188 } 189 } 190 if (defined(invoker.public)) { 191 foreach(path, invoker.public) { 192 _pw_source_files += [ path ] 193 } 194 } 195 196 _library_output_dir = "${target_out_dir}/lib" 197 if (defined(invoker.output_dir)) { 198 _library_output_dir = invoker.output_dir 199 } 200 201 shared_library(target_name) { 202 import("$dir_pw_build/defaults.gni") 203 forward_variables_from(invoker, "*", _supported_toolchain_defaults) 204 205 # Ensure that we don't overwrite metadata forwared from the invoker above. 206 if (defined(metadata)) { 207 metadata.pw_source_files = _pw_source_files 208 } else { 209 metadata = { 210 pw_source_files = _pw_source_files 211 } 212 } 213 214 if (!defined(configs)) { 215 configs = [] 216 } 217 if (defined(pw_build_defaults.configs)) { 218 configs += pw_build_defaults.configs 219 } 220 if (defined(remove_configs)) { 221 if (remove_configs != [] && remove_configs[0] == "*") { 222 configs = [] 223 } else { 224 configs += remove_configs # Add configs in case they aren't already 225 configs -= remove_configs # present, then remove them. 226 } 227 } 228 if (defined(invoker.configs)) { 229 configs += invoker.configs 230 } 231 232 public_deps = [ "$dir_pw_build:link_deps" ] 233 if (defined(pw_build_defaults.public_deps)) { 234 public_deps += pw_build_defaults.public_deps 235 } 236 if (defined(remove_public_deps)) { 237 if (remove_public_deps != [] && remove_public_deps[0] == "*") { 238 public_deps = [] 239 } else { 240 public_deps += remove_public_deps 241 public_deps -= remove_public_deps 242 } 243 } 244 if (defined(invoker.public_deps)) { 245 public_deps += invoker.public_deps 246 } 247 248 output_dir = _library_output_dir 249 } 250} 251