Skip to content

Commit 224fe6a

Browse files
authored
Handle external packages using CARGO_MANIFEST_DIR (#464)
1 parent 9c889b0 commit 224fe6a

File tree

12 files changed

+77
-4
lines changed

12 files changed

+77
-4
lines changed

.bazelignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
docs
22
examples
3+
examples/hello_cargo_manifest_dir

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# bazel
77
/bazel-*
88
/examples/bazel-*
9+
/examples/*/bazel-*
910
/docs/bazel-*
1011

1112
# rustfmt

WORKSPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ examples_deps()
5959

6060
load("@examples//:examples_transitive_deps.bzl", examples_transitive_deps = "transitive_deps")
6161

62-
examples_transitive_deps()
62+
examples_transitive_deps(is_top_level = True)
6363

6464
# Load all dependencies for docs
6565

examples/.bazelignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello_cargo_manifest_dir

examples/examples_transitive_deps.bzl

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,35 @@ There are some transitive dependencies of the dependencies of the examples'
44
dependencies. This file contains the required macros to pull these dependencies
55
"""
66

7+
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
78
load("@io_bazel_rules_rust//:workspace.bzl", "rust_workspace")
89
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
910

10-
def transitive_deps():
11-
"""Define transitive dependencies for `rules_rust` examples"""
11+
# buildifier: disable=unnamed-macro
12+
def transitive_deps(is_top_level = False):
13+
"""Define transitive dependencies for `rules_rust` examples
14+
15+
Args:
16+
is_top_level (bool, optional): Indicates wheather or not this is being called
17+
from the root WORKSPACE file of `rules_rust`. Defaults to False.
18+
"""
1219

1320
rules_proto_dependencies()
1421

1522
rules_proto_toolchains()
1623

1724
rust_workspace()
25+
26+
# Needed by the hello_uses_cargo_manifest_dir example.
27+
if is_top_level:
28+
maybe(
29+
native.local_repository,
30+
name = "hello_cargo_manifest_dir",
31+
path = "examples/hello_cargo_manifest_dir",
32+
)
33+
else:
34+
maybe(
35+
native.local_repository,
36+
name = "hello_cargo_manifest_dir",
37+
path = "hello_cargo_manifest_dir",
38+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library")
2+
3+
rust_library(
4+
name = "hello_cargo_manifest_dir",
5+
srcs = ["src/lib.rs"],
6+
data = ["include/included_file.rs.inc"],
7+
visibility = ["//visibility:public"],
8+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
local_repository(
2+
name = "io_bazel_rules_rust",
3+
path = "../..",
4+
)
5+
6+
load("@io_bazel_rules_rust//rust:repositories.bzl", "rust_repositories")
7+
8+
rust_repositories()
9+
10+
load("@io_bazel_rules_rust//:workspace.bzl", "rust_workspace")
11+
12+
rust_workspace()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I love veggies!
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
pub fn get_included_str() -> &'static str {
3+
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/include/included_file.rs.inc"))
4+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_test")
2+
3+
rust_test(
4+
name = "hello_uses_cargo_manifest_dir",
5+
srcs = ["src/lib.rs"],
6+
edition = "2018",
7+
deps = ["@hello_cargo_manifest_dir"],
8+
)

0 commit comments

Comments
 (0)