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 15import("python_action.gni") 16 17# Prints an error message and exits the build unsuccessfully. Either 'message' 18# or 'message_lines' must be specified, but not both. 19# 20# Args: 21# 22# message: The message to print. Use \n for newlines. 23# message_lines: List of lines to use for the message. 24# visibility: GN visibility to apply to the underlying target. 25# 26template("pw_error") { 27 assert( 28 defined(invoker.message) != defined(invoker.message_lines), 29 "pw_error requires either a 'message' string or a 'message_lines' list") 30 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) { 39 script = "$dir_pw_build/py/pw_build/error.py" 40 args = [ 41 "--target", 42 get_label_info(":$target_name", "label_with_toolchain"), 43 "--message", 44 _message, 45 "--root", 46 rebase_path("//", root_build_dir), 47 "--out", 48 ".", 49 ] 50 51 # This output file is never created. 52 outputs = [ "$target_gen_dir/$target_name.build_error" ] 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# 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 } 94 } 95} 96