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/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/WORKSPACE b/examples/WORKSPACE index 7a647f7493..584d93ee68 100644 --- a/examples/WORKSPACE +++ b/examples/WORKSPACE @@ -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", +) 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..0740e663fe --- /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"], + deps = ["@hello_cargo_manifest_dir//:hello_cargo_manifest_dir"], + edition = "2018", +) 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 4c08f30bd2..fbcd015cc0 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -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