blob: 33804c982ac97c92e256287afe07b40579aa2e43 [file] [log] [blame]
Alexei Frolov9c35aac2020-10-28 12:36:41 -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
15import("python_action.gni")
16
Wyatt Hepler0c84ab62021-03-09 08:37:19 -080017# Prints an error message and exits the build unsuccessfully. Either 'message'
18# or 'message_lines' must be specified, but not both.
Alexei Frolov9c35aac2020-10-28 12:36:41 -070019#
20# Args:
Wyatt Heplera91c0852022-02-14 09:11:27 -080021#
Wyatt Hepler0c84ab62021-03-09 08:37:19 -080022# message: The message to print. Use \n for newlines.
23# message_lines: List of lines to use for the message.
Wyatt Heplera91c0852022-02-14 09:11:27 -080024# visibility: GN visibility to apply to the underlying target.
Alexei Frolov9c35aac2020-10-28 12:36:41 -070025#
26template("pw_error") {
Wyatt Hepler0c84ab62021-03-09 08:37:19 -080027 assert(
28 defined(invoker.message) != defined(invoker.message_lines),
29 "pw_error requires either a 'message' string or a 'message_lines' list")
Alexei Frolov9c35aac2020-10-28 12:36:41 -070030
Wyatt Hepler0c84ab62021-03-09 08:37:19 -080031 if (defined(invoker.message_lines)) {
32 _message = string_join("\n", invoker.message_lines)
33 } else {
34 _message = invoker.message
35 }
36 assert(_message != "", "The message cannot be empty")
37
38 action(target_name) {
Alexei Frolov9c35aac2020-10-28 12:36:41 -070039 script = "$dir_pw_build/py/pw_build/error.py"
40 args = [
41 "--target",
Wyatt Hepler0c84ab62021-03-09 08:37:19 -080042 get_label_info(":$target_name", "label_with_toolchain"),
Alexei Frolov9c35aac2020-10-28 12:36:41 -070043 "--message",
Wyatt Hepler0c84ab62021-03-09 08:37:19 -080044 _message,
45 "--root",
Michael Spangc8b93902021-05-30 15:53:56 -040046 rebase_path("//", root_build_dir),
Wyatt Hepler0c84ab62021-03-09 08:37:19 -080047 "--out",
Michael Spangc8b93902021-05-30 15:53:56 -040048 ".",
Alexei Frolov9c35aac2020-10-28 12:36:41 -070049 ]
Wyatt Hepler0c84ab62021-03-09 08:37:19 -080050
51 # This output file is never created.
52 outputs = [ "$target_gen_dir/$target_name.build_error" ]
Wyatt Heplera91c0852022-02-14 09:11:27 -080053
54 forward_variables_from(invoker, [ "visibility" ])
55 }
56}
57
58# An assert that is evaluated at build time. The assertion is only checked if
59# this target is depended on by another target. If the assertion passes, nothing
60# happens. If it fails, the target prints an error message with pw_error.
61#
62# To enforce a pw_build_assert, targets add a dependency on a pw_build_assert.
63# Multiple targets may depend on the same pw_build_assert if the same assertion
64# applies.
65#
66# Args:
67#
68# condition: The assertion to verify.
69# message: The message to print. Use \n for newlines.
70# message_lines: List of lines to use for the message.
71# visibility: GN visibility to apply to the underlying target.
72#
73template("pw_build_assert") {
74 assert(defined(invoker.condition),
75 "pw_build_assert requires a boolean condition")
76 assert(defined(invoker.message) != defined(invoker.message_lines),
77 "pw_build_assert requires either 'message' or 'message_lines'")
78
79 _pw_error_variables = [
80 "message",
81 "message_lines",
82 "visibility",
83 ]
84
85 if (invoker.condition) {
86 not_needed(invoker, _pw_error_variables)
87 group(target_name) {
88 forward_variables_from(invoker, [ "visibility" ])
89 }
90 } else {
91 pw_error(target_name) {
92 forward_variables_from(invoker, _pw_error_variables)
93 }
Alexei Frolov9c35aac2020-10-28 12:36:41 -070094 }
95}