Squashed 'third_party/rules_rust/' content from commit bf59038ca

git-subtree-dir: third_party/rules_rust
git-subtree-split: bf59038cac11798cbaef9f3bf965bad8182b97fa
Signed-off-by: Brian Silverman <bsilver16384@gmail.com>
Change-Id: I5a20e403203d670df467ea97dde9a4ac40339a8d
diff --git a/examples/env_locations/BUILD.bazel b/examples/env_locations/BUILD.bazel
new file mode 100644
index 0000000..82161bb
--- /dev/null
+++ b/examples/env_locations/BUILD.bazel
@@ -0,0 +1,49 @@
+load("@rules_rust//cargo:cargo_build_script.bzl", "cargo_build_script")
+load("@rules_rust//rust:defs.bzl", "rust_test")
+
+# generate a file
+genrule(
+    name = "data_generator",
+    outs = ["generated.data"],
+    cmd = "echo hello > $@",
+)
+
+_data = [
+    # we should be able to read non-generated source/data files
+    "source.file",
+    # and generated files as well
+    "generated.data",
+    # we should also be able to access external binaries
+    # such as protoc.
+    "@com_google_protobuf//:protoc",
+]
+
+cargo_build_script(
+    name = "build",
+    srcs = ["build.rs"],
+    build_script_env = {
+        "GENERATED_DATA": "$(location generated.data)",
+        "SOME_TOOL": "$(execpath @com_google_protobuf//:protoc)",
+        # both execpath and location should work
+        "SOURCE_FILE": "$(execpath source.file)",
+    },
+    data = _data,
+)
+
+rust_test(
+    name = "test",
+    srcs = [
+        "main.rs",
+    ],
+    data = _data,
+    edition = "2018",
+    rustc_env = {
+        "GENERATED_DATA_ABS": "$(execpath generated.data)",
+        "GENERATED_DATA_ROOT": "$(rootpath generated.data)",
+        "SOME_TOOL": "$(rootpath @com_google_protobuf//:protoc)",
+        "SOURCE_FILE": "$(rootpath source.file)",
+    },
+    deps = [
+        ":build",
+    ],
+)
diff --git a/examples/env_locations/build.rs b/examples/env_locations/build.rs
new file mode 100644
index 0000000..d791f5d
--- /dev/null
+++ b/examples/env_locations/build.rs
@@ -0,0 +1,17 @@
+use std::{env, fs};
+
+fn main() {
+    // our source file should be readable
+    let path = env::var("SOURCE_FILE").unwrap();
+    let generated_data = fs::read_to_string(&path).unwrap();
+    assert_eq!(generated_data, "source\n");
+
+    // our generated data file should be readable
+    let path = env::var("GENERATED_DATA").unwrap();
+    let generated_data = fs::read_to_string(&path).unwrap();
+    assert_eq!(generated_data, "hello\n");
+
+    // and we should be able to read (and thus execute) our tool
+    let path = env::var("SOME_TOOL").unwrap();
+    assert!(!fs::read(&path).unwrap().is_empty());
+}
diff --git a/examples/env_locations/main.rs b/examples/env_locations/main.rs
new file mode 100644
index 0000000..3b07b1d
--- /dev/null
+++ b/examples/env_locations/main.rs
@@ -0,0 +1,12 @@
+#[test]
+fn test() {
+    // our source file should be readable
+    let source_file = std::fs::read_to_string(env!("SOURCE_FILE")).unwrap();
+    assert_eq!(source_file, "source\n");
+    // our generated data file should be readable at run time and build time
+    let generated_data = std::fs::read_to_string(env!("GENERATED_DATA_ROOT")).unwrap();
+    let generated_data2 = include_str!(env!("GENERATED_DATA_ABS"));
+    assert_eq!(generated_data, generated_data2);
+    // and we should be able to read (and thus execute) our tool
+    assert!(!std::fs::read(env!("SOME_TOOL")).unwrap().is_empty());
+}
diff --git a/examples/env_locations/source.file b/examples/env_locations/source.file
new file mode 100644
index 0000000..5a18cd2
--- /dev/null
+++ b/examples/env_locations/source.file
@@ -0,0 +1 @@
+source