Skip to content
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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,16 @@ tasks:
- "//..."
test_targets:
- "//..."
expand_examples_ubuntu2004:
Copy link
Collaborator

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

name: Expand Examples
platform: ubuntu2004
working_directory: examples/expand
build_targets:
- "//..."
test_targets:
- "//..."
build_flags:
- "--config=expanded"
cc_common_link_ubuntu2004:
name: Build via cc_common.link
platform: ubuntu2004
Expand Down
1 change: 1 addition & 0 deletions examples/.bazelignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ android
cargo_manifest_dir/external_crate
crate_universe
crate_universe_unnamed
expand
ios
ios_build
10 changes: 10 additions & 0 deletions examples/expand/.bazelrc
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
62 changes: 62 additions & 0 deletions examples/expand/BUILD.bazel
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",
)
76 changes: 76 additions & 0 deletions examples/expand/README.md
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
```
16 changes: 16 additions & 0 deletions examples/expand/WORKSPACE.bazel
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",
)
11 changes: 11 additions & 0 deletions examples/expand/ok_binary.expand.expected.rs
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"],
&[]));
};
}
10 changes: 10 additions & 0 deletions examples/expand/ok_library.expand.expected.rs
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) {}

47 changes: 47 additions & 0 deletions examples/expand/ok_test.expand.expected.rs
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])
}
15 changes: 15 additions & 0 deletions examples/expand/src/lib.rs
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());
}
}
3 changes: 3 additions & 0 deletions examples/expand/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello world");
}
7 changes: 7 additions & 0 deletions rust/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ load(
_rust_clippy_aspect = "rust_clippy_aspect",
)
load("//rust/private:common.bzl", _rust_common = "rust_common")
load("//rust/private:expand.bzl", _rust_expand = "rust_expand", _rust_expand_aspect = "rust_expand_aspect")
load(
"//rust/private:rust.bzl",
_rust_binary = "rust_binary",
Expand Down Expand Up @@ -103,6 +104,12 @@ rust_clippy = _rust_clippy
capture_clippy_output = _capture_clippy_output
# See @rules_rust//rust/private:clippy.bzl for a complete description.

rust_expand_aspect = _rust_expand_aspect
# See @rules_rust//rust/private:expand.bzl for a complete description.

rust_expand = _rust_expand
# See @rules_rust//rust/private:expand.bzl for a complete description.

error_format = _error_format
# See @rules_rust//rust/private:rustc.bzl for a complete description.

Expand Down
Loading