Alexei Frolov | 9c35aac | 2020-10-28 12:36:41 -0700 | [diff] [blame] | 1 | # 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 | |
| 15 | import("python_action.gni") |
| 16 | |
Wyatt Hepler | 0c84ab6 | 2021-03-09 08:37:19 -0800 | [diff] [blame] | 17 | # Prints an error message and exits the build unsuccessfully. Either 'message' |
| 18 | # or 'message_lines' must be specified, but not both. |
Alexei Frolov | 9c35aac | 2020-10-28 12:36:41 -0700 | [diff] [blame] | 19 | # |
| 20 | # Args: |
Wyatt Hepler | a91c085 | 2022-02-14 09:11:27 -0800 | [diff] [blame] | 21 | # |
Wyatt Hepler | 0c84ab6 | 2021-03-09 08:37:19 -0800 | [diff] [blame] | 22 | # message: The message to print. Use \n for newlines. |
| 23 | # message_lines: List of lines to use for the message. |
Wyatt Hepler | a91c085 | 2022-02-14 09:11:27 -0800 | [diff] [blame] | 24 | # visibility: GN visibility to apply to the underlying target. |
Alexei Frolov | 9c35aac | 2020-10-28 12:36:41 -0700 | [diff] [blame] | 25 | # |
| 26 | template("pw_error") { |
Wyatt Hepler | 0c84ab6 | 2021-03-09 08:37:19 -0800 | [diff] [blame] | 27 | assert( |
| 28 | defined(invoker.message) != defined(invoker.message_lines), |
| 29 | "pw_error requires either a 'message' string or a 'message_lines' list") |
Alexei Frolov | 9c35aac | 2020-10-28 12:36:41 -0700 | [diff] [blame] | 30 | |
Wyatt Hepler | 0c84ab6 | 2021-03-09 08:37:19 -0800 | [diff] [blame] | 31 | 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 Frolov | 9c35aac | 2020-10-28 12:36:41 -0700 | [diff] [blame] | 39 | script = "$dir_pw_build/py/pw_build/error.py" |
| 40 | args = [ |
| 41 | "--target", |
Wyatt Hepler | 0c84ab6 | 2021-03-09 08:37:19 -0800 | [diff] [blame] | 42 | get_label_info(":$target_name", "label_with_toolchain"), |
Alexei Frolov | 9c35aac | 2020-10-28 12:36:41 -0700 | [diff] [blame] | 43 | "--message", |
Wyatt Hepler | 0c84ab6 | 2021-03-09 08:37:19 -0800 | [diff] [blame] | 44 | _message, |
| 45 | "--root", |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 46 | rebase_path("//", root_build_dir), |
Wyatt Hepler | 0c84ab6 | 2021-03-09 08:37:19 -0800 | [diff] [blame] | 47 | "--out", |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 48 | ".", |
Alexei Frolov | 9c35aac | 2020-10-28 12:36:41 -0700 | [diff] [blame] | 49 | ] |
Wyatt Hepler | 0c84ab6 | 2021-03-09 08:37:19 -0800 | [diff] [blame] | 50 | |
| 51 | # This output file is never created. |
| 52 | outputs = [ "$target_gen_dir/$target_name.build_error" ] |
Wyatt Hepler | a91c085 | 2022-02-14 09:11:27 -0800 | [diff] [blame] | 53 | |
| 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 | # |
| 73 | template("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 Frolov | 9c35aac | 2020-10-28 12:36:41 -0700 | [diff] [blame] | 94 | } |
| 95 | } |