xref: /aosp_15_r20/external/pigweed/pw_toolchain/non_c_toolchain.gni (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2021 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("//build_overrides/pigweed.gni")
16
17# Creates a toolchain that provides no C/C++ compiler. It can be used for
18# non-C/C++ languages or actions that should only happen once across all builds.
19# The toolchain cannot compile C/C++, and trying to use it to is an error.
20#
21# Args:
22#   command: Run this command if this toolchain is used to build C/C++ code.
23#
24template("pw_non_c_toolchain") {
25  # Import the universal stamp & copy tools.
26  import("$dir_pw_toolchain/universal_tools.gni")
27  _label = get_label_info(":$target_name", "label_no_toolchain")
28
29  # If the user tries to build a target with this toolchain, run a script that
30  # prints out an error.
31  _message =
32      "Attempted to use the $target_name toolchain to compile {{source}}.\n" +
33      "This toolchain cannot be used to compile C/C++ source code.\n\n" +
34      "This toolchain was either explicitly specified in a deps list with\n" +
35      "GN's :target($_label) syntax or was set as the\n" +
36      "default toolchain in the BUILDCONFIG.gn file.\n\n" +
37      "Ensure that no C/C++ GN targets are referred to with this toolchain,\n" +
38      "even transitively.\n\n" +
39      "See https://pigweed.dev/pw_toolchain for more information."
40
41  _command = string_join(" ",
42                         [
43                           "python",
44                           rebase_path("$dir_pw_build/py/pw_build/error.py",
45                                       root_build_dir),
46                           "--message \"$_message\"",
47                           "--target",
48                           _label,
49                           "--root",
50                           rebase_path("//", root_build_dir),
51                           "--out",
52                           ".",
53                         ])
54
55  if (defined(invoker.command)) {
56    _command = invoker.command
57  } else {
58    not_needed([ "invoker" ])
59  }
60
61  toolchain(target_name) {
62    tool("stamp") {
63      forward_variables_from(pw_universal_stamp, "*")
64    }
65
66    tool("copy") {
67      forward_variables_from(pw_universal_copy, "*")
68    }
69
70    tool("asm") {
71      command = _command
72      outputs =
73          [ "{{source_out_dir}}/{{target_output_name}}.{{source_file_part}}.o" ]
74    }
75
76    tool("cc") {
77      command = _command
78      outputs =
79          [ "{{source_out_dir}}/{{target_output_name}}.{{source_file_part}}.o" ]
80    }
81
82    tool("cxx") {
83      command = _command
84      outputs =
85          [ "{{source_out_dir}}/{{target_output_name}}.{{source_file_part}}.o" ]
86    }
87
88    # Can't use {{source}} for the linker, so replace it if it's in the command.
89    _command_no_source = string_replace(_command, "{{source}}", "C/C++ sources")
90
91    tool("link") {
92      command = _command_no_source
93      outputs = [ "{{output_dir}}/{{target_output_name}}{{output_extension}}" ]
94    }
95
96    tool("alink") {
97      command = _command_no_source
98      outputs = [ "{{output_dir}}/{{target_output_name}}{{output_extension}}" ]
99    }
100
101    tool("solink") {
102      command = _command_no_source
103      outputs = [ "{{output_dir}}/{{target_output_name}}{{output_extension}}" ]
104    }
105  }
106}
107