xref: /aosp_15_r20/external/bazelbuild-rules_rust/examples/env_locations/BUILD.bazel (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1load("@rules_rust//cargo:defs.bzl", "cargo_build_script")
2load("@rules_rust//rust:defs.bzl", "rust_test")
3
4# generate a file
5genrule(
6    name = "data_generator",
7    outs = ["generated.data"],
8    cmd = "echo hello > $@",
9)
10
11_data = [
12    # we should be able to read non-generated source/data files
13    "source.file",
14    # and generated files as well
15    "generated.data",
16    # we should also be able to access external binaries
17    # such as protoc.
18    "@com_google_protobuf//:protoc",
19]
20
21cargo_build_script(
22    name = "build",
23    srcs = ["build.rs"],
24    build_script_env = {
25        "GENERATED_DATA": "$(location generated.data)",
26        "SOME_TOOL": "$(execpath @com_google_protobuf//:protoc)",
27        # both execpath and location should work
28        "SOURCE_FILE": "$(execpath source.file)",
29    },
30    data = _data,
31)
32
33rust_test(
34    name = "test",
35    srcs = [
36        "main.rs",
37    ],
38    data = _data,
39    edition = "2018",
40    rustc_env = {
41        "GENERATED_DATA_ABS": "$(execpath generated.data)",
42        "GENERATED_DATA_ROOT": "$(rootpath generated.data)",
43        "SOME_TOOL": "$(rootpath @com_google_protobuf//:protoc)",
44        "SOURCE_FILE": "$(rootpath source.file)",
45    },
46    deps = [
47        ":build",
48    ],
49)
50