• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2023 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
17import("$dir_pw_build/rust_library.gni")
18import("$dir_pw_toolchain/toolchain_args.gni")
19
20# Note: In general, prefer to import target_types.gni rather than this file.
21#
22# A wrapper for GN's built-in rust_proc_macro target, this template wraps a
23# configurable target type specified by the current toolchain to be used for all
24# pw_rust_proc_macro targets.
25#
26# Compiling procedural macros is... a bit awkward.
27#
28# Even though they're provided to crates that use them as if they were normal
29# external crates, they're actually '.so'/'.dylib's that are compiled for the
30# host machine and then linked into the compiler, so they and all their
31# dependencies should be built for the host target.
32#
33# For more information on the features provided by this template, see the full
34# docs at https://pigweed.dev/pw_build/?highlight=pw_rust_proc_macro.
35template("pw_rust_proc_macro") {
36  if (defined(pw_toolchain_SCOPE.rustc_macro_toolchain_name)) {
37    group(target_name) {
38      public_deps =
39          [ ":$target_name(${pw_toolchain_SCOPE.rustc_macro_toolchain_name})" ]
40    }
41    not_needed(invoker, "*")
42  } else if (defined(pw_toolchain_SCOPE.is_host_toolchain) &&
43             pw_toolchain_SCOPE.is_host_toolchain) {
44    pw_rust_library(target_name) {
45      forward_variables_from(invoker, "*")
46
47      if (!defined(rustflags)) {
48        rustflags = []
49      }
50
51      rustflags += [
52        "--extern",
53        "proc_macro",
54      ]
55      underlying_target_type = "rust_proc_macro"
56    }
57  } else {
58    assert(
59        false,
60        "proc_macro target ${target_name}(${pw_toolchain_SCOPE.name}) is not " +
61            "a host target, please specify a host toolchain through " +
62            "`rustc_macro_toolchain_name` for it to be compiled.")
63  }
64}
65