blob: a451cb2e6a6c469b80d7b6997d4b8f0f028b6afe [file] [log] [blame]
Alexei Frolovedd2f142020-06-09 19:11:27 -07001# Copyright 2020 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
Armando Montanezfb3d3fb2020-06-09 18:12:12 -070015import("//build_overrides/pigweed.gni")
Wyatt Heplerd49f8fe2020-10-15 10:13:47 -070016
Alexei Frolov4c0428a2020-06-10 10:46:04 -070017declare_args() {
18 # The name of the GN target type used to build Pigweed executables.
19 #
20 # If this is a custom template, the .gni file containing the template must
21 # be imported at the top of the target configuration file to make it globally
22 # available.
23 pw_build_EXECUTABLE_TARGET_TYPE = "executable"
Armando Montanezfd5de702020-06-10 16:51:01 -070024
25 # The path to the .gni file that defines pw_build_EXECUTABLE_TARGET_TYPE.
26 #
27 # If pw_build_EXECUTABLE_TARGET_TYPE is not the default of `executable`, this
28 # .gni file is imported to provide the template definition.
29 pw_build_EXECUTABLE_TARGET_TYPE_FILE = ""
30}
31
32if (pw_build_EXECUTABLE_TARGET_TYPE != "executable" &&
33 pw_build_EXECUTABLE_TARGET_TYPE_FILE != "") {
34 import(pw_build_EXECUTABLE_TARGET_TYPE_FILE)
Alexei Frolov4c0428a2020-06-10 10:46:04 -070035}
Armando Montanezfb3d3fb2020-06-09 18:12:12 -070036
Alexei Frolovedd2f142020-06-09 19:11:27 -070037# TODO(frolv): The code in all of the templates below is duplicated, with the
38# exception of the target type. This file could be auto-generated with Python.
39
40_supported_toolchain_defaults = [
41 "configs",
42 "public_deps",
43]
44
45template("pw_source_set") {
Alexei Frolovedd2f142020-06-09 19:11:27 -070046 source_set(target_name) {
Alexei Frolov8ef706f2020-06-17 16:14:39 -070047 import("$dir_pw_build/defaults.gni")
Alexei Frolovedd2f142020-06-09 19:11:27 -070048 forward_variables_from(invoker, "*", _supported_toolchain_defaults)
49
Michael Spange5de2652020-06-11 20:10:38 -040050 if (!defined(configs)) {
Alexei Frolovedd2f142020-06-09 19:11:27 -070051 configs = []
52 }
Michael Spange5de2652020-06-11 20:10:38 -040053 if (defined(pw_build_defaults.configs)) {
54 configs += pw_build_defaults.configs
55 }
Alexei Frolovedd2f142020-06-09 19:11:27 -070056 if (defined(remove_configs)) {
57 if (remove_configs[0] == "*") {
58 configs = []
59 } else {
60 configs -= remove_configs
61 }
62 }
63 if (defined(invoker.configs)) {
64 configs += invoker.configs
65 }
66
67 if (defined(pw_build_defaults.public_deps)) {
68 public_deps = pw_build_defaults.public_deps
69 } else {
70 public_deps = []
71 }
72 if (defined(remove_public_deps)) {
73 if (remove_public_deps[0] == "*") {
74 public_deps = []
75 } else {
76 public_deps -= remove_public_deps
77 }
78 }
79 if (defined(invoker.public_deps)) {
80 public_deps += invoker.public_deps
81 }
82 }
83}
84
85template("pw_static_library") {
Michael Spangcdba5162020-06-12 14:45:07 -040086 _library_output_dir = "${target_out_dir}/lib"
Michael Spang524ead42020-06-12 14:23:40 -040087 if (defined(invoker.output_dir)) {
88 _library_output_dir = invoker.output_dir
89 }
90
Alexei Frolovedd2f142020-06-09 19:11:27 -070091 static_library(target_name) {
Alexei Frolov8ef706f2020-06-17 16:14:39 -070092 import("$dir_pw_build/defaults.gni")
Alexei Frolovedd2f142020-06-09 19:11:27 -070093 forward_variables_from(invoker, "*", _supported_toolchain_defaults)
94
Michael Spange5de2652020-06-11 20:10:38 -040095 if (!defined(configs)) {
Alexei Frolovedd2f142020-06-09 19:11:27 -070096 configs = []
97 }
Michael Spange5de2652020-06-11 20:10:38 -040098 if (defined(pw_build_defaults.configs)) {
99 configs += pw_build_defaults.configs
100 }
Alexei Frolovedd2f142020-06-09 19:11:27 -0700101 if (defined(remove_configs)) {
102 if (remove_configs[0] == "*") {
103 configs = []
104 } else {
105 configs -= remove_configs
106 }
107 }
108 if (defined(invoker.configs)) {
109 configs += invoker.configs
110 }
111
112 if (defined(pw_build_defaults.public_deps)) {
113 public_deps = pw_build_defaults.public_deps
114 } else {
115 public_deps = []
116 }
117 if (defined(remove_public_deps)) {
118 if (remove_public_deps[0] == "*") {
119 public_deps = []
120 } else {
121 public_deps -= remove_public_deps
122 }
123 }
124 if (defined(invoker.public_deps)) {
125 public_deps += invoker.public_deps
126 }
Michael Spang524ead42020-06-12 14:23:40 -0400127
128 output_dir = _library_output_dir
Alexei Frolovedd2f142020-06-09 19:11:27 -0700129 }
130}
131
132template("pw_shared_library") {
Michael Spangcdba5162020-06-12 14:45:07 -0400133 _library_output_dir = "${target_out_dir}/lib"
Michael Spang524ead42020-06-12 14:23:40 -0400134 if (defined(invoker.output_dir)) {
135 _library_output_dir = invoker.output_dir
136 }
137
Alexei Frolovedd2f142020-06-09 19:11:27 -0700138 shared_library(target_name) {
Alexei Frolov8ef706f2020-06-17 16:14:39 -0700139 import("$dir_pw_build/defaults.gni")
Alexei Frolovedd2f142020-06-09 19:11:27 -0700140 forward_variables_from(invoker, "*", _supported_toolchain_defaults)
141
Michael Spange5de2652020-06-11 20:10:38 -0400142 if (!defined(configs)) {
Alexei Frolovedd2f142020-06-09 19:11:27 -0700143 configs = []
144 }
Michael Spange5de2652020-06-11 20:10:38 -0400145 if (defined(pw_build_defaults.configs)) {
146 configs += pw_build_defaults.configs
147 }
Alexei Frolovedd2f142020-06-09 19:11:27 -0700148 if (defined(remove_configs)) {
149 if (remove_configs[0] == "*") {
150 configs = []
151 } else {
152 configs -= remove_configs
153 }
154 }
155 if (defined(invoker.configs)) {
156 configs += invoker.configs
157 }
158
159 if (defined(pw_build_defaults.public_deps)) {
160 public_deps = pw_build_defaults.public_deps
161 } else {
162 public_deps = []
163 }
164 if (defined(remove_public_deps)) {
165 if (remove_public_deps[0] == "*") {
166 public_deps = []
167 } else {
168 public_deps -= remove_public_deps
169 }
170 }
171 if (defined(invoker.public_deps)) {
172 public_deps += invoker.public_deps
173 }
Michael Spang524ead42020-06-12 14:23:40 -0400174
175 output_dir = _library_output_dir
Alexei Frolovedd2f142020-06-09 19:11:27 -0700176 }
177}
178
179# Wrapper for Pigweed executable build targets which uses a globally-defined,
180# configurable target type.
181template("pw_executable") {
Michael Spangcdba5162020-06-12 14:45:07 -0400182 _executable_output_dir = "${target_out_dir}/bin"
Michael Spang524ead42020-06-12 14:23:40 -0400183 if (defined(invoker.output_dir)) {
184 _executable_output_dir = invoker.output_dir
185 }
186
Alexei Frolov4c0428a2020-06-10 10:46:04 -0700187 target(pw_build_EXECUTABLE_TARGET_TYPE, target_name) {
Alexei Frolov8ef706f2020-06-17 16:14:39 -0700188 import("$dir_pw_build/defaults.gni")
Wyatt Heplerd49f8fe2020-10-15 10:13:47 -0700189
Alexei Frolovedd2f142020-06-09 19:11:27 -0700190 forward_variables_from(invoker, "*", _supported_toolchain_defaults)
191
Michael Spange5de2652020-06-11 20:10:38 -0400192 if (!defined(configs)) {
Alexei Frolovedd2f142020-06-09 19:11:27 -0700193 configs = []
194 }
Michael Spange5de2652020-06-11 20:10:38 -0400195 if (defined(pw_build_defaults.configs)) {
196 configs += pw_build_defaults.configs
197 }
Alexei Frolovedd2f142020-06-09 19:11:27 -0700198 if (defined(remove_configs)) {
199 if (remove_configs[0] == "*") {
200 configs = []
201 } else {
202 configs -= remove_configs
203 }
204 }
205 if (defined(invoker.configs)) {
206 configs += invoker.configs
207 }
208
209 if (defined(pw_build_defaults.public_deps)) {
210 public_deps = pw_build_defaults.public_deps
211 } else {
212 public_deps = []
213 }
214 if (defined(remove_public_deps)) {
215 if (remove_public_deps[0] == "*") {
216 public_deps = []
217 } else {
218 public_deps -= remove_public_deps
219 }
220 }
221 if (defined(invoker.public_deps)) {
222 public_deps += invoker.public_deps
223 }
Michael Spang524ead42020-06-12 14:23:40 -0400224
225 output_dir = _executable_output_dir
Alexei Frolovedd2f142020-06-09 19:11:27 -0700226 }
227}