Skip to content

Commit

Permalink
Handle external packages using CARGO_MANIFEST_DIR (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
sitaktif committed Oct 26, 2020
1 parent 9c889b0 commit 224fe6a
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 4 deletions.
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
docs
examples
examples/hello_cargo_manifest_dir
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# bazel
/bazel-*
/examples/bazel-*
/examples/*/bazel-*
/docs/bazel-*

# rustfmt
Expand Down
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ examples_deps()

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

examples_transitive_deps()
examples_transitive_deps(is_top_level = True)

# Load all dependencies for docs

Expand Down
1 change: 1 addition & 0 deletions examples/.bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello_cargo_manifest_dir
25 changes: 23 additions & 2 deletions examples/examples_transitive_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,35 @@ There are some transitive dependencies of the dependencies of the examples'
dependencies. This file contains the required macros to pull these dependencies
"""

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

def transitive_deps():
"""Define transitive dependencies for `rules_rust` examples"""
# buildifier: disable=unnamed-macro
def transitive_deps(is_top_level = False):
"""Define transitive dependencies for `rules_rust` examples
Args:
is_top_level (bool, optional): Indicates wheather or not this is being called
from the root WORKSPACE file of `rules_rust`. Defaults to False.
"""

rules_proto_dependencies()

rules_proto_toolchains()

rust_workspace()

# Needed by the hello_uses_cargo_manifest_dir example.
if is_top_level:
maybe(
native.local_repository,
name = "hello_cargo_manifest_dir",
path = "examples/hello_cargo_manifest_dir",
)
else:
maybe(
native.local_repository,
name = "hello_cargo_manifest_dir",
path = "hello_cargo_manifest_dir",
)
8 changes: 8 additions & 0 deletions examples/hello_cargo_manifest_dir/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library")

rust_library(
name = "hello_cargo_manifest_dir",
srcs = ["src/lib.rs"],
data = ["include/included_file.rs.inc"],
visibility = ["//visibility:public"],
)
12 changes: 12 additions & 0 deletions examples/hello_cargo_manifest_dir/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
local_repository(
name = "io_bazel_rules_rust",
path = "../..",
)

load("@io_bazel_rules_rust//rust:repositories.bzl", "rust_repositories")

rust_repositories()

load("@io_bazel_rules_rust//:workspace.bzl", "rust_workspace")

rust_workspace()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I love veggies!
4 changes: 4 additions & 0 deletions examples/hello_cargo_manifest_dir/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

pub fn get_included_str() -> &'static str {
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/include/included_file.rs.inc"))
}
8 changes: 8 additions & 0 deletions examples/hello_uses_cargo_manifest_dir/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_test")

rust_test(
name = "hello_uses_cargo_manifest_dir",
srcs = ["src/lib.rs"],
edition = "2018",
deps = ["@hello_cargo_manifest_dir"],
)
7 changes: 7 additions & 0 deletions examples/hello_uses_cargo_manifest_dir/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

use hello_cargo_manifest_dir::get_included_str;

#[test]
fn test_lib_with_include() {
assert_eq!(get_included_str(), "I love veggies!\n")
}
11 changes: 10 additions & 1 deletion rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,18 @@ def construct_arguments(
# may not follow the `src/lib.rs` convention. As such we use `ctx.build_file_path` mapped into the
# `exec_root`. Since we cannot (seemingly) get the `exec_root` from starlark, we cheat a little
# and use `${pwd}` which resolves the `exec_root` at action execution time.
#
# Unfortunately, ctx.build_file_path isn't relative to the exec_root for external repositories in
# which case we use ctx.label.workspace_root to complete the path.
args.add("--subst", "pwd=${pwd}")

env["CARGO_MANIFEST_DIR"] = "${pwd}/" + ctx.build_file_path[:ctx.build_file_path.rfind("/")]
workspace_root_items = ctx.label.workspace_root.split("/")
if (len(workspace_root_items) >= 2 and
workspace_root_items[0] == "external" and
workspace_root_items[-1] == ctx.build_file_path.split("/")[0]):
workspace_root_items = workspace_root_items[:-1]

env["CARGO_MANIFEST_DIR"] = "${pwd}/" + "/".join(workspace_root_items + ctx.build_file_path.split("/")[:-1])

if out_dir != None:
env["OUT_DIR"] = "${pwd}/" + out_dir
Expand Down

0 comments on commit 224fe6a

Please sign in to comment.