-
Notifications
You must be signed in to change notification settings - Fork 444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rust_expand
and rust_expand_aspect
for generating macro expanded source files.
#1643
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ android | |
cargo_manifest_dir/external_crate | ||
crate_universe | ||
crate_universe_unnamed | ||
expand | ||
ios | ||
ios_build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# `.bazelrc` is a Bazel configuration file. | ||
# https://bazel.build/docs/best-practices#bazelrc-file | ||
|
||
# Enable rustc expand targets | ||
build:expanded --aspects=@rules_rust//rust:defs.bzl%rust_expand_aspect | ||
build:expanded --output_groups=+expanded | ||
|
||
# This import should always be last to allow users to override | ||
# settings for local development. | ||
try-import %workspace%/user.bazelrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
load("@bazel_skylib//rules:diff_test.bzl", "diff_test") | ||
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_expand", "rust_library", "rust_test") | ||
|
||
package(default_visibility = ["//test:__subpackages__"]) | ||
|
||
# Declaration of targets. | ||
|
||
rust_binary( | ||
name = "ok_binary", | ||
srcs = ["src/main.rs"], | ||
edition = "2021", | ||
) | ||
|
||
rust_library( | ||
name = "ok_library", | ||
srcs = ["src/lib.rs"], | ||
edition = "2021", | ||
) | ||
|
||
rust_test( | ||
name = "ok_test", | ||
srcs = ["src/lib.rs"], | ||
edition = "2021", | ||
) | ||
|
||
# Expand targets. | ||
|
||
rust_expand( | ||
name = "ok_binary_expand", | ||
deps = [":ok_binary"], | ||
) | ||
|
||
rust_expand( | ||
name = "ok_library_expand", | ||
deps = [":ok_library"], | ||
) | ||
|
||
rust_expand( | ||
name = "ok_test_expand", | ||
testonly = True, | ||
deps = [":ok_test"], | ||
) | ||
|
||
# Assert on expanded targets. | ||
|
||
diff_test( | ||
name = "ok_binary_expand_test", | ||
file1 = ":ok_binary.expand.expected.rs", | ||
file2 = ":ok_binary_expand", | ||
) | ||
|
||
diff_test( | ||
name = "ok_library_expand_test", | ||
file1 = ":ok_library.expand.expected.rs", | ||
file2 = ":ok_library_expand", | ||
) | ||
|
||
diff_test( | ||
name = "ok_test_expand_test", | ||
file1 = ":ok_test.expand.expected.rs", | ||
file2 = ":ok_test_expand", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
### Expand Example | ||
|
||
Rustc can be used to expand all macros so that you can inspect the generated source files easier. | ||
|
||
This feature is enabled via `-Zunpretty=expanded`. The `-Z` flag is only available in the nightly | ||
version of `rustc`. | ||
|
||
|
||
### Expanding | ||
|
||
Build and test your targets normally. | ||
|
||
``` | ||
bazel build //:ok_binary | ||
INFO: Analyzed target //:ok_binary (0 packages loaded, 0 targets configured). | ||
INFO: Found 1 target... | ||
Target //:ok_binary up-to-date: | ||
bazel-bin/ok_binary | ||
INFO: Elapsed time: 0.081s, Critical Path: 0.00s | ||
INFO: 1 process: 1 internal. | ||
INFO: Build completed successfully, 1 total action | ||
``` | ||
|
||
Use the aspect to generate the expanded files in as a one-off build. | ||
|
||
``` | ||
bazel build --config=expanded //:ok_binary | ||
INFO: Analyzed target //:ok_binary (1 packages loaded, 2 targets configured). | ||
INFO: Found 1 target... | ||
Aspect @rules_rust//rust/private:expand.bzl%rust_expand_aspect of //:ok_binary up-to-date: | ||
bazel-bin/ok_binary.expand.rs | ||
INFO: Elapsed time: 0.149s, Critical Path: 0.00s | ||
INFO: 1 process: 1 internal. | ||
INFO: Build completed successfully, 1 total action | ||
``` | ||
|
||
Targeting tests is valid as well. | ||
|
||
``` | ||
bazel build --config=expanded //:ok_test | ||
INFO: Analyzed target //:ok_test (0 packages loaded, 2 targets configured). | ||
INFO: Found 1 target... | ||
Aspect @rules_rust//rust/private:expand.bzl%rust_expand_aspect of //:ok_test up-to-date: | ||
bazel-bin/test-397521499/ok_test.expand.rs | ||
INFO: Elapsed time: 0.113s, Critical Path: 0.00s | ||
INFO: 1 process: 1 internal. | ||
INFO: Build completed successfully, 1 total action | ||
``` | ||
|
||
Finally, manually wire up a `rust_expand` target explicitly if you want a target to build. | ||
|
||
``` | ||
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_expand") | ||
|
||
rust_binary( | ||
name = "ok_binary", | ||
srcs = ["src/main.rs"], | ||
edition = "2021", | ||
) | ||
|
||
rust_expand( | ||
name = "ok_binary_expand", | ||
deps = [":ok_binary"], | ||
) | ||
``` | ||
|
||
``` | ||
bazel build //:ok_binary_expand | ||
INFO: Analyzed target //:ok_binary_expand (0 packages loaded, 1 target configured). | ||
INFO: Found 1 target... | ||
Target //:ok_binary_expand up-to-date: | ||
bazel-bin/ok_binary.expand.rs | ||
INFO: Elapsed time: 0.090s, Critical Path: 0.00s | ||
INFO: 1 process: 1 internal. | ||
INFO: Build completed successfully, 1 total action | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
workspace(name = "examples") | ||
|
||
local_repository( | ||
name = "rules_rust", | ||
path = "../../", | ||
) | ||
|
||
load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains") | ||
|
||
rules_rust_dependencies() | ||
|
||
rust_register_toolchains( | ||
edition = "2021", | ||
iso_date = "2022-11-01", | ||
version = "nightly", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#![feature(prelude_import)] | ||
#[prelude_import] | ||
use std::prelude::rust_2021::*; | ||
#[macro_use] | ||
extern crate std; | ||
fn main() { | ||
{ | ||
::std::io::_print(::core::fmt::Arguments::new_v1(&["Hello world\n"], | ||
&[])); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(prelude_import)] | ||
#[prelude_import] | ||
use std::prelude::rust_2021::*; | ||
#[macro_use] | ||
extern crate std; | ||
pub fn greeting() -> String { "Hello World".to_owned() } | ||
|
||
// too_many_args/clippy.toml will require no more than 2 args. | ||
pub fn with_args(_: u32, _: u32, _: u32) {} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#![feature(prelude_import)] | ||
#[prelude_import] | ||
use std::prelude::rust_2021::*; | ||
#[macro_use] | ||
extern crate std; | ||
pub fn greeting() -> String { "Hello World".to_owned() } | ||
|
||
// too_many_args/clippy.toml will require no more than 2 args. | ||
pub fn with_args(_: u32, _: u32, _: u32) {} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
extern crate test; | ||
#[cfg(test)] | ||
#[rustc_test_marker = "tests::it_works"] | ||
pub const it_works: test::TestDescAndFn = | ||
test::TestDescAndFn { | ||
desc: test::TestDesc { | ||
name: test::StaticTestName("tests::it_works"), | ||
ignore: false, | ||
ignore_message: ::core::option::Option::None, | ||
compile_fail: false, | ||
no_run: false, | ||
should_panic: test::ShouldPanic::No, | ||
test_type: test::TestType::UnitTest, | ||
}, | ||
testfn: test::StaticTestFn(|| | ||
test::assert_test_result(it_works())), | ||
}; | ||
fn it_works() { | ||
match (&greeting(), &"Hello World".to_owned()) { | ||
(left_val, right_val) => { | ||
if !(*left_val == *right_val) { | ||
let kind = ::core::panicking::AssertKind::Eq; | ||
::core::panicking::assert_failed(kind, &*left_val, | ||
&*right_val, ::core::option::Option::None); | ||
} | ||
} | ||
}; | ||
} | ||
} | ||
#[rustc_main] | ||
pub fn main() -> () { | ||
extern crate test; | ||
test::test_main_static(&[&it_works]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
pub fn greeting() -> String { | ||
"Hello World".to_owned() | ||
} | ||
|
||
// too_many_args/clippy.toml will require no more than 2 args. | ||
pub fn with_args(_: u32, _: u32, _: u32) {} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
#[test] | ||
fn it_works() { | ||
assert_eq!(greeting(), "Hello World".to_owned()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
println!("Hello world"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of adding a new example, would you be willing to some integration tests for this in
test
As of #1671 you should be able to gain access to a nightly toolchain in the main repo via a transition on
@rules_rust//rust/toolchain/channel