Skip to content

Commit

Permalink
fix several issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Calsign committed Oct 21, 2023
1 parent 6d455f9 commit b764243
Show file tree
Hide file tree
Showing 18 changed files with 74 additions and 5 deletions.
11 changes: 11 additions & 0 deletions generation_tests/cargo/all_kinds/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
rust_library(
name = "all_kinds_lib",
srcs = ["src/lib.rs"],
compile_data = [":Cargo.toml"],
crate_name = "all_kinds",
visibility = ["//visibility:public"],
)

rust_binary(
name = "all_kinds",
srcs = ["src/main.rs"],
compile_data = [":Cargo.toml"],
visibility = ["//visibility:public"],
)

rust_binary(
name = "another_executable",
srcs = ["src/bin/another_executable.rs"],
compile_data = [":Cargo.toml"],
visibility = ["//visibility:public"],
)

Expand All @@ -27,12 +30,14 @@ rust_binary(
"src/bin/multi_file_executable/main.rs",
"src/bin/multi_file_executable/some_module.rs",
],
compile_data = [":Cargo.toml"],
visibility = ["//visibility:public"],
)

rust_binary(
name = "named_executable",
srcs = ["src/bin/named_executable.rs"],
compile_data = [":Cargo.toml"],
visibility = ["//visibility:public"],
)

Expand All @@ -42,18 +47,21 @@ rust_test(
"tests/multi_file_test/main.rs",
"tests/multi_file_test/test_module.rs",
],
compile_data = [":Cargo.toml"],
visibility = ["//visibility:public"],
)

rust_test(
name = "some_integration_test",
srcs = ["tests/some_integration_test.rs"],
compile_data = [":Cargo.toml"],
visibility = ["//visibility:public"],
)

rust_binary(
name = "large_input",
srcs = ["benches/large_input.rs"],
compile_data = [":Cargo.toml"],
tags = ["bench"],
visibility = ["//visibility:public"],
)
Expand All @@ -64,6 +72,7 @@ rust_binary(
"benches/multi_file_bench/bench_module.rs",
"benches/multi_file_bench/main.rs",
],
compile_data = [":Cargo.toml"],
tags = ["bench"],
visibility = ["//visibility:public"],
)
Expand All @@ -74,13 +83,15 @@ rust_binary(
"examples/multi_file_example/ex_module.rs",
"examples/multi_file_example/main.rs",
],
compile_data = [":Cargo.toml"],
tags = ["example"],
visibility = ["//visibility:public"],
)

rust_binary(
name = "simple",
srcs = ["examples/simple.rs"],
compile_data = [":Cargo.toml"],
tags = ["example"],
visibility = ["//visibility:public"],
)
2 changes: 2 additions & 0 deletions generation_tests/cargo/basic/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ rust_library(
"src/foobar.rs",
"src/lib.rs",
],
compile_data = [":Cargo.toml"],
crate_name = "basic",
visibility = ["//visibility:public"],
)

rust_binary(
name = "basic",
srcs = ["src/main.rs"],
compile_data = [":Cargo.toml"],
visibility = ["//visibility:public"],
deps = [":basic_lib"],
)
9 changes: 9 additions & 0 deletions generation_tests/cargo/crate_tests/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,61 @@ load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
rust_library(
name = "all_kinds_lib",
srcs = ["src/lib.rs"],
compile_data = [":Cargo.toml"],
crate_name = "all_kinds",
visibility = ["//visibility:public"],
)

rust_binary(
name = "all_kinds",
srcs = ["src/main.rs"],
compile_data = [":Cargo.toml"],
visibility = ["//visibility:public"],
)

rust_test(
name = "some_integration_test",
srcs = ["tests/some_integration_test.rs"],
compile_data = [":Cargo.toml"],
visibility = ["//visibility:public"],
)

rust_binary(
name = "large_input",
srcs = ["benches/large_input.rs"],
compile_data = [":Cargo.toml"],
tags = ["bench"],
visibility = ["//visibility:public"],
)

rust_binary(
name = "simple",
srcs = ["examples/simple.rs"],
compile_data = [":Cargo.toml"],
tags = ["example"],
visibility = ["//visibility:public"],
)

rust_test(
name = "all_kinds_lib_test",
compile_data = [":Cargo.toml"],
crate = ":all_kinds_lib",
)

rust_test(
name = "all_kinds_test",
compile_data = [":Cargo.toml"],
crate = ":all_kinds",
)

rust_test(
name = "large_input_test",
compile_data = [":Cargo.toml"],
crate = ":large_input",
)

rust_test(
name = "simple_test",
compile_data = [":Cargo.toml"],
crate = ":simple",
)
3 changes: 3 additions & 0 deletions generation_tests/cargo/dependencies/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
rust_library(
name = "dependencies_lib",
srcs = ["src/lib.rs"],
compile_data = [":Cargo.toml"],
crate_name = "dependencies",
visibility = ["//visibility:public"],
)

rust_binary(
name = "dependencies",
srcs = ["src/main.rs"],
compile_data = [":Cargo.toml"],
visibility = ["//visibility:public"],
deps = [":dependencies_lib"],
)

rust_test(
name = "integration_test",
srcs = ["tests/integration_test.rs"],
compile_data = [":Cargo.toml"],
visibility = ["//visibility:public"],
deps = [":dependencies_lib"],
)
1 change: 1 addition & 0 deletions generation_tests/cargo/mixed_modes/cargo_a/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ load("@rules_rust//rust:defs.bzl", "rust_library")
rust_library(
name = "cargo_a",
srcs = ["src/lib.rs"],
compile_data = [":Cargo.toml"],
visibility = ["//visibility:public"],
)
1 change: 1 addition & 0 deletions generation_tests/cargo/mixed_modes/cargo_b/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ load("@rules_rust//rust:defs.bzl", "rust_library")
rust_library(
name = "cargo_b",
srcs = ["src/lib.rs"],
compile_data = [":Cargo.toml"],
visibility = ["//visibility:public"],
deps = ["//cargo_a"],
)
2 changes: 2 additions & 0 deletions generation_tests/cargo/multi_package_workspace/a/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library")
rust_library(
name = "a_lib",
srcs = ["src/lib.rs"],
compile_data = [":Cargo.toml"],
crate_name = "a",
visibility = ["//visibility:public"],
)

rust_binary(
name = "a",
srcs = ["src/main.rs"],
compile_data = [":Cargo.toml"],
visibility = ["//visibility:public"],
deps = [":a_lib"],
)
1 change: 1 addition & 0 deletions generation_tests/cargo/multi_package_workspace/b/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@rules_rust//rust:defs.bzl", "rust_library")
rust_library(
name = "b",
srcs = ["src/lib.rs"],
compile_data = [":Cargo.toml"],
visibility = ["//visibility:public"],
deps = ["//a:a_lib"],
)
1 change: 1 addition & 0 deletions generation_tests/cargo/multi_package_workspace/c/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@rules_rust//rust:defs.bzl", "rust_library")
rust_library(
name = "c",
srcs = ["src/lib.rs"],
compile_data = [":Cargo.toml"],
visibility = ["//visibility:public"],
deps = [
"//a:a_lib",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@rules_rust//rust:defs.bzl", "rust_library")
rust_library(
name = "nested_package",
srcs = ["src/lib.rs"],
compile_data = [":Cargo.toml"],
visibility = ["//visibility:public"],
deps = [
"//a:a_lib",
Expand Down
1 change: 1 addition & 0 deletions generation_tests/cargo/nested_modules/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ rust_library(
"src/c/mod.rs",
"src/lib.rs",
],
compile_data = [":Cargo.toml"],
visibility = ["//visibility:public"],
)
3 changes: 3 additions & 0 deletions generation_tests/cargo/update_rule/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
rust_library(
name = "update_rule_lib",
srcs = ["src/lib.rs"],
compile_data = [":Cargo.toml"],
crate_name = "update_rule",
visibility = ["//visibility:public"],
)

rust_binary(
name = "update_rule",
srcs = ["src/main.rs"],
compile_data = [":Cargo.toml"],
visibility = ["//visibility:private"],
deps = [":update_rule_lib"],
)

rust_test(
name = "integration_test",
srcs = ["tests/integration_test.rs"],
compile_data = [":Cargo.toml"],
visibility = ["//visibility:public"],
deps = [":update_rule_lib"],
)
1 change: 1 addition & 0 deletions proto/messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ message CargoTomlRequest {
message CargoCrateInfo {
string name = 1;
repeated string srcs = 2;
bool proc_macro = 3;
}

message CargoTomlResponse {
Expand Down
10 changes: 8 additions & 2 deletions rust_language/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ import (
// modules provided by rust
var Builtins = map[string]bool{
// standard library
"std": true,
"core": true,
"std": true,
"core": true,
"alloc": true,
"test": true,

// proc macros
"proc_macro": true,

// tools
"rustfmt": true,
"clippy": true,

// primitive types
// https://doc.rust-lang.org/std/primitive/index.html
"bool": true,
Expand Down
9 changes: 8 additions & 1 deletion rust_language/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,12 @@ func (l *rustLang) generateRulesFromCargo(args language.GenerateArgs) language.G
}
}

l.generateCargoRule(args.Config, &args, response.Library, "rust_library", suffix, []string{}, &result)
kind := "rust_library"
if response.Library.ProcMacro {
kind = "rust_proc_macro"
}

l.generateCargoRule(args.Config, &args, response.Library, kind, suffix, []string{}, &result)
}
for _, binary := range response.Binaries {
l.generateCargoRule(args.Config, &args, binary, "rust_binary", "", []string{}, &result)
Expand Down Expand Up @@ -329,6 +334,7 @@ func (l *rustLang) generateRulesFromCargo(args language.GenerateArgs) language.G

testRule := rule.NewRule("rust_test", *testRuleName)
testRule.SetAttr("crate", ":"+ruleData.rule.Name())
testRule.SetAttr("compile_data", []string{":Cargo.toml"})

result.Gen = append(result.Gen, testRule)
result.Imports = append(result.Imports, RuleData{
Expand Down Expand Up @@ -380,6 +386,7 @@ func (l *rustLang) generateCargoRule(c *config.Config, args *language.GenerateAr
newRule := rule.NewRule(kind, targetName)
newRule.SetAttr("srcs", srcs)
newRule.SetAttr("visibility", []string{"//visibility:public"})
newRule.SetAttr("compile_data", []string{":Cargo.toml"})

if targetName != crateName {
newRule.SetAttr("crate_name", crateName)
Expand Down
18 changes: 18 additions & 0 deletions rust_parser/lockfile_crates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ pub fn get_bazel_lockfile_crates(lockfile_path: PathBuf) -> Result<Vec<Package>,
add_crate(&dep.id, false);
}

for dep in workspace_crate
.common_attrs
.deps_dev
.get_iter(None)
.unwrap()
{
add_crate(&dep.id, false);
}

for proc_macro_dep in workspace_crate
.common_attrs
.proc_macro_deps
Expand All @@ -65,6 +74,15 @@ pub fn get_bazel_lockfile_crates(lockfile_path: PathBuf) -> Result<Vec<Package>,
{
add_crate(&proc_macro_dep.id, true);
}

for proc_macro_dep in workspace_crate
.common_attrs
.proc_macro_deps_dev
.get_iter(None)
.unwrap()
{
add_crate(&proc_macro_dep.id, true);
}
}

Ok(crates)
Expand Down
1 change: 1 addition & 0 deletions rust_parser/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ fn build_crate_info(product: cargo_toml::Product) -> CargoCrateInfo {
if let Some(path) = product.path {
crate_info.set_srcs(RepeatedField::from_vec(vec![path]));
}
crate_info.proc_macro = product.proc_macro;

crate_info
}
Expand Down
4 changes: 2 additions & 2 deletions rust_parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl<'ast> AstVisitor<'ast> {
fn add_import<I: Into<Ident<'ast>>>(&mut self, ident: I) {
let ident = ident.into();

if ident == "crate" || ident == "super" {
if ident == "crate" || ident == "super" || ident == "self" {
// these are keywords referring to the current crate; not an import
return;
}
Expand Down Expand Up @@ -403,7 +403,7 @@ impl<'ast> Visit<'ast> for AstVisitor<'ast> {
if ident == "test" {
self.hints.has_test = true;
is_test_only = true;
} else if ident == "proc_macro" {
} else if ident == "proc_macro" || ident == "proc_macro_attribute" {
self.hints.has_proc_macro = true;
}
}
Expand Down

0 comments on commit b764243

Please sign in to comment.