xref: /aosp_15_r20/external/pigweed/pw_build/rust_proc_macro.gni (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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.is_host_toolchain) &&
37      pw_toolchain_SCOPE.is_host_toolchain) {
38    pw_rust_library(target_name) {
39      forward_variables_from(invoker, "*")
40
41      if (!defined(rustflags)) {
42        rustflags = []
43      }
44
45      rustflags += [
46        "--extern",
47        "proc_macro",
48      ]
49      underlying_target_type = "rust_proc_macro"
50    }
51  } else {
52    assert(
53        defined(pw_toolchain_SCOPE.rustc_macro_toolchain_name),
54        "Rust macro crates are compiled with host toolchain. Please provided " +
55            "a `rustc_macro_toolchain_name` for this device toolchain.")
56
57    toolchain_name = pw_toolchain_SCOPE.rustc_macro_toolchain_name
58    group(target_name) {
59      public_deps = [ ":$target_name($toolchain_name)" ]
60    }
61    not_needed(invoker, "*")
62  }
63}
64