From 224fe6a81b8ffa9c585c75f49a41f9b9924d516d Mon Sep 17 00:00:00 2001 From: Sitaktif Date: Mon, 26 Oct 2020 08:46:33 +0000 Subject: [PATCH] Handle external packages using CARGO_MANIFEST_DIR (#464) --- .bazelignore | 1 + .gitignore | 1 + WORKSPACE | 2 +- examples/.bazelignore | 1 + examples/examples_transitive_deps.bzl | 25 +++++++++++++++++-- examples/hello_cargo_manifest_dir/BUILD | 8 ++++++ examples/hello_cargo_manifest_dir/WORKSPACE | 12 +++++++++ .../include/included_file.rs.inc | 1 + examples/hello_cargo_manifest_dir/src/lib.rs | 4 +++ examples/hello_uses_cargo_manifest_dir/BUILD | 8 ++++++ .../hello_uses_cargo_manifest_dir/src/lib.rs | 7 ++++++ rust/private/rustc.bzl | 11 +++++++- 12 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 examples/.bazelignore create mode 100644 examples/hello_cargo_manifest_dir/BUILD create mode 100644 examples/hello_cargo_manifest_dir/WORKSPACE create mode 100644 examples/hello_cargo_manifest_dir/include/included_file.rs.inc create mode 100644 examples/hello_cargo_manifest_dir/src/lib.rs create mode 100644 examples/hello_uses_cargo_manifest_dir/BUILD create mode 100644 examples/hello_uses_cargo_manifest_dir/src/lib.rs diff --git a/.bazelignore b/.bazelignore index 37fa09a7af..2f8c47fa39 100644 --- a/.bazelignore +++ b/.bazelignore @@ -1,2 +1,3 @@ docs examples +examples/hello_cargo_manifest_dir diff --git a/.gitignore b/.gitignore index 7f1e029d45..dd83850771 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ # bazel /bazel-* /examples/bazel-* +/examples/*/bazel-* /docs/bazel-* # rustfmt diff --git a/WORKSPACE b/WORKSPACE index 6123310f87..1f8161c3ae 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -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 diff --git a/examples/.bazelignore b/examples/.bazelignore new file mode 100644 index 0000000000..6d0dadddd2 --- /dev/null +++ b/examples/.bazelignore @@ -0,0 +1 @@ +hello_cargo_manifest_dir diff --git a/examples/examples_transitive_deps.bzl b/examples/examples_transitive_deps.bzl index a7996d3315..5ca117f7df 100644 --- a/examples/examples_transitive_deps.bzl +++ b/examples/examples_transitive_deps.bzl @@ -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", + ) diff --git a/examples/hello_cargo_manifest_dir/BUILD b/examples/hello_cargo_manifest_dir/BUILD new file mode 100644 index 0000000000..e20dc354be --- /dev/null +++ b/examples/hello_cargo_manifest_dir/BUILD @@ -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"], +) diff --git a/examples/hello_cargo_manifest_dir/WORKSPACE b/examples/hello_cargo_manifest_dir/WORKSPACE new file mode 100644 index 0000000000..ad319577bd --- /dev/null +++ b/examples/hello_cargo_manifest_dir/WORKSPACE @@ -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() diff --git a/examples/hello_cargo_manifest_dir/include/included_file.rs.inc b/examples/hello_cargo_manifest_dir/include/included_file.rs.inc new file mode 100644 index 0000000000..4091ebba06 --- /dev/null +++ b/examples/hello_cargo_manifest_dir/include/included_file.rs.inc @@ -0,0 +1 @@ +I love veggies! diff --git a/examples/hello_cargo_manifest_dir/src/lib.rs b/examples/hello_cargo_manifest_dir/src/lib.rs new file mode 100644 index 0000000000..b462aebe04 --- /dev/null +++ b/examples/hello_cargo_manifest_dir/src/lib.rs @@ -0,0 +1,4 @@ + +pub fn get_included_str() -> &'static str { + include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/include/included_file.rs.inc")) +} diff --git a/examples/hello_uses_cargo_manifest_dir/BUILD b/examples/hello_uses_cargo_manifest_dir/BUILD new file mode 100644 index 0000000000..be720291d1 --- /dev/null +++ b/examples/hello_uses_cargo_manifest_dir/BUILD @@ -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"], +) diff --git a/examples/hello_uses_cargo_manifest_dir/src/lib.rs b/examples/hello_uses_cargo_manifest_dir/src/lib.rs new file mode 100644 index 0000000000..8b761b88c2 --- /dev/null +++ b/examples/hello_uses_cargo_manifest_dir/src/lib.rs @@ -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") +} diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 5ad97c9eeb..9968f84f0e 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -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