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#   message: The message to print. Use \n for newlines.
22#   message_lines: List of lines to use for the message.
23#
24template("pw_error") {
25  assert(
26      defined(invoker.message) != defined(invoker.message_lines),
27      "pw_error requires either a 'message' string or a 'message_lines' list")
28
29  if (defined(invoker.message_lines)) {
30    _message = string_join("\n", invoker.message_lines)
31  } else {
32    _message = invoker.message
33  }
34  assert(_message != "", "The message cannot be empty")
35
36  action(target_name) {
37    script = "$dir_pw_build/py/pw_build/error.py"
38    args = [
39      "--target",
40      get_label_info(":$target_name", "label_with_toolchain"),
41      "--message",
42      _message,
43      "--root",
44      rebase_path("//"),
45      "--out",
46      rebase_path(root_build_dir),
47    ]
48
49    # This output file is never created.
50    outputs = [ "$target_gen_dir/$target_name.build_error" ]
51  }
52}
53