xref: /aosp_15_r20/external/bazelbuild-rules_rust/test/generated_inputs/external_repo.bzl (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1"""External repository for `generated_inputs` tests"""
2
3_BUILD_FILE_CONTENT = """
4load("@rules_rust//rust:defs.bzl", "rust_library")
5load("@bazel_skylib//rules:write_file.bzl", "write_file")
6
7write_file(
8    name = "generate_src",
9    out = "src.rs",
10    content = ["pub fn forty_two() -> i32 { 42 }"],
11)
12
13rust_library(
14    name = "generated_inputs_external_repo",
15    srcs = [
16        "lib.rs",
17        ":generate_src",
18    ],
19    edition = "2021",
20    visibility = ["//visibility:public"],
21)
22"""
23
24_LIB_RS_CONTENT = """
25mod src;
26
27pub fn forty_two_from_generated_src() -> String {
28    format!("{}", src::forty_two())
29}
30
31#[cfg(test)]
32mod test {
33    #[test]
34    fn test_forty_two_as_string() {
35        assert_eq!(super::forty_two_from_generated_src(), "42");
36    }
37}
38"""
39
40def _generated_inputs_in_external_repo_impl(repository_ctx):
41    # Create repository files (not in the root directory)
42    repo_path = repository_ctx.path("lib")
43    repository_ctx.file(
44        "{}/BUILD.bazel".format(repo_path),
45        content = _BUILD_FILE_CONTENT,
46    )
47    repository_ctx.file(
48        "{}/lib.rs".format(repo_path),
49        content = _LIB_RS_CONTENT,
50    )
51
52_generated_inputs_in_external_repo = repository_rule(
53    implementation = _generated_inputs_in_external_repo_impl,
54    doc = (
55        "A test repository rule providing a Rust library using generated sources"
56    ),
57)
58
59def generated_inputs_in_external_repo():
60    """Define the a test repository with Rust library using generated sources"""
61
62    _generated_inputs_in_external_repo(
63        name = "generated_inputs_in_external_repo",
64    )
65    return [struct(repo = "generated_inputs_in_external_repo", is_dev_dep = True)]
66