Skip to content

Commit

Permalink
Handle external packages using CARGO_MANIFEST_DIR
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Wagner-Hall <dwagnerhall@apple.com>
  • Loading branch information
sitaktif and illicitonion committed Oct 23, 2020
1 parent b2a482f commit bb61346
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 1 deletion.
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
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
6 changes: 6 additions & 0 deletions examples/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ deps()
load(":examples_transitive_deps.bzl", "transitive_deps")

transitive_deps()

# For the hello_uses_cargo_manifest_dir example.
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"],
deps = ["@hello_cargo_manifest_dir//:hello_cargo_manifest_dir"],
edition = "2018",
)
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 @@ -442,9 +442,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 bb61346

Please sign in to comment.