Skip to content

Commit

Permalink
pw_build: Add support for rust proc macros via pw_rust_proc_macro
Browse files Browse the repository at this point in the history
Change-Id: Ie5227a0afe50290e00be28284b7c3a4490d573a0
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/135478
Commit-Queue: Faraaz Sareshwala <[email protected]>
Reviewed-by: Taylor Cramer <[email protected]>
  • Loading branch information
fsareshwala authored and CQ Bot Account committed Jun 2, 2023
1 parent b258eb3 commit 30921d6
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 1 deletion.
2 changes: 2 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,9 @@ group("host_tools") {
if (pw_rust_ENABLE_EXPERIMENTAL_BUILD) {
deps += [
"$dir_pw_rust/examples/host_executable:hello($dir_pigweed/targets/host:host_clang_debug)",
"$dir_pw_rust/examples/host_executable:proc_macro($dir_pigweed/targets/host:host_clang_debug)",
"$dir_pw_rust/examples/host_executable:test_hello($dir_pigweed/targets/host:host_clang_debug)",
"$dir_pw_rust/examples/host_executable:test_proc_macro($dir_pigweed/targets/host:host_clang_debug)",
]
}
}
Expand Down
1 change: 1 addition & 0 deletions pw_build/gn_internal/build_target.gni
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ template("pw_internal_build_target") {
_builtin_target_types = [
"executable",
"rust_library",
"rust_proc_macro",
"shared_library",
"source_set",
"static_library",
Expand Down
5 changes: 4 additions & 1 deletion pw_build/rust_library.gni
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ template("pw_rust_library") {
}
}

underlying_target_type = "rust_library"
if (!defined(underlying_target_type)) {
underlying_target_type = "rust_library"
}

crate_name = string_replace(crate_name, "-", "_")
output_name = crate_name
output_dir = "${target_out_dir}/lib"
Expand Down
32 changes: 32 additions & 0 deletions pw_build/rust_proc_macro.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2023 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

import("//build_overrides/pigweed.gni")

import("$dir_pw_build/rust_library.gni")

# Note: In general, prefer to import target_types.gni rather than this file.
#
# A wrapper for GN's built-in rust_proc_macro target, this template wraps a
# configurable target type specified by the current toolchain to be used for all
# pw_rust_proc_macro targets.
#
# For more information on the features provided by this template, see the full
# docs at https://pigweed.dev/pw_build/?highlight=pw_rust_proc_macro.
template("pw_rust_proc_macro") {
pw_rust_library(target_name) {
forward_variables_from(invoker, "*")
underlying_target_type = "rust_proc_macro"
}
}
1 change: 1 addition & 0 deletions pw_build/target_types.gni
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ import("$dir_pw_build/cc_executable.gni")
import("$dir_pw_build/cc_library.gni")
import("$dir_pw_build/rust_executable.gni")
import("$dir_pw_build/rust_library.gni")
import("$dir_pw_build/rust_proc_macro.gni")
import("$dir_pw_build/rust_test.gni")
10 changes: 10 additions & 0 deletions pw_rust/examples/host_executable/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,13 @@ pw_rust_library("c") {
crate_root = "c/lib.rs"
sources = [ "c/lib.rs" ]
}

pw_rust_proc_macro("proc_macro") {
crate_root = "proc_macro/lib.rs"
sources = [ "proc_macro/lib.rs" ]
}

pw_rust_test("test_proc_macro") {
sources = [ "proc_macro/test.rs" ]
deps = [ ":proc_macro" ]
}
21 changes: 21 additions & 0 deletions pw_rust/examples/host_executable/proc_macro/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2023 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro]
pub fn fn_like_proc_macro(input: TokenStream) -> TokenStream {
input
}
21 changes: 21 additions & 0 deletions pw_rust/examples/host_executable/proc_macro/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2023 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

#[cfg(test)]
mod tests {
#[test]
fn test_proc_macro() {
proc_macro::fn_like_proc_macro!();
}
}
18 changes: 18 additions & 0 deletions pw_toolchain/generate_toolchain.gni
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,14 @@ template("generate_toolchain") {

_generate_rust_tools = defined(invoker.rustc)
if (_generate_rust_tools) {
if (toolchain_os == "mac") {
_dylib_extension = ".dylib"
} else if (toolchain_os == "win") {
_dylib_extension = ".dll"
} else {
_dylib_extension = ".so"
}

_rustc_command = string_join(
" ",
[
Expand Down Expand Up @@ -416,6 +424,16 @@ template("generate_toolchain") {
command = _rustc_command
outputs = [ _output ]
}

tool("rust_macro") {
description = "rustc {{output}}"
default_output_dir = "{{target_out_dir}}/lib"
depfile = "{{output}}.d"
output_prefix = "lib"
default_output_extension = _dylib_extension
command = _rustc_command
outputs = [ _output ]
}
}
}

Expand Down

0 comments on commit 30921d6

Please sign in to comment.