| # Copyright 2018 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| # Defines a Rust procedural macro |
| # |
| # Parameters |
| # |
| # name |
| # Name of the crate as defined in its manifest file. If not specified, it is |
| # assumed to be the same as the target name. |
| # |
| # version (optional) |
| # Semver version of the crate as seen on crates.io. |
| # |
| # edition (optional) |
| # Edition of the Rust language to be used. |
| # Options are "2015" and "2018". Defaults to "2018". |
| # |
| # configs (optional) |
| # A list of config labels applying to this target. |
| # |
| # deps (optional) |
| # List of rust_library GN targets on which this crate depends. |
| # Third party crates can be included through paths like |
| # "//third_party/rust_crates:<cratename>". |
| # |
| # source_root (optional) |
| # Location of the crate root (e.g. `src/main.rs` or `src/lib.rs`). |
| # This defaults to `./src/main.rs` for binaries and `./src/lib.rs` for libraries, |
| # and should only be changed when absolutely necessary |
| # (such as in the case of generated code). |
| # |
| # Example of usage: |
| # |
| # rustc_macro("foo") { |
| # deps = [ |
| # "//garnet/public/rust/bar", |
| # "//third_party/rust_crates:serde", |
| # "//third_party/rust_crates:slab", |
| # ] |
| # } |
| template("rustc_macro") { |
| # Compiling procedural macros is... a bit awkward. |
| # |
| # Even though they're provided to crates that use them as if they were normal |
| # external crates, they're actually '.so'/'.dylib's that are compiled for the host machine |
| # and then linked into the compiler, so they and all their dependencies should |
| # be built for the host target. |
| # |
| # Once this is done, the resulting artifacts are copied into the Fuchsia target |
| # directories to act as if they had been built for Fuchsia. In order to avoid |
| # conflicts, the outputs of the original (host) artifact are built with a |
| # `_proc_macro` suffix added onto the end, which is removed when they're copied |
| # into the final target directory. |
| forward_variables_from(invoker, [ "visibility" ]) |
| |
| proc_macro_target = "${target_name}_proc_macro" |
| |
| # The actual host-target build of the proc macro crate. |
| if (is_host) { |
| if (defined(invoker.name)) { |
| package_name = invoker.name |
| } else { |
| package_name = target_name |
| } |
| crate_name = string_replace(package_name, "-", "_") |
| |
| if (!defined(invoker.source_root)) { |
| source_root = "src/lib.rs" |
| } else { |
| source_root = invoker.source_root |
| } |
| |
| rust_proc_macro(proc_macro_target) { |
| configs = [] |
| configs = invoker.configs |
| |
| not_needed(invoker, |
| [ |
| "version", |
| "non_rust_deps", |
| "force_opt", |
| ]) |
| crate_root = source_root |
| output_name = crate_name |
| if (defined(invoker.non_rust_deps)) { |
| data_deps = invoker.non_rust_deps |
| } |
| |
| if (defined(invoker.edition) && invoker.edition == "2015") { |
| configs -= [ "//build/config:rust_edition_2018" ] |
| configs += [ "//build/config:rust_edition_2015" ] |
| } |
| |
| forward_variables_from(invoker, |
| [ |
| "deps", |
| "testonly", |
| "visibility", |
| ]) |
| } |
| } else { |
| not_needed(invoker, "*") |
| } |
| |
| # redirect so that users don't need to be aware of host_toolchain requirement |
| group(target_name) { |
| public_deps = [ ":${proc_macro_target}($host_toolchain)" ] |
| } |
| } |