xref: /aosp_15_r20/external/bazelbuild-rules_rust/test/generated_inputs/BUILD.bazel (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
2load("@bazel_skylib//rules:write_file.bzl", "write_file")
3load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
4load(":input_from_different_cfg.bzl", "input_from_different_cfg")
5
6write_file(
7    name = "gen_src",
8    out = "src.rs",
9    content = """
10#[cfg(not(generated_file_as_root))]
11pub fn forty_two() -> i32 { 42 }
12
13#[cfg(generated_file_as_root)]
14mod lib_for_src;
15
16#[cfg(generated_file_as_root)]
17pub fn get_forty_two_as_string() -> String {
18    format!("{}", lib_for_src::forty_two())
19}
20""".splitlines(),
21    newline = "unix",
22)
23
24write_file(
25    name = "gen_src_generated",
26    out = "src/generated.rs",
27    content = """
28mod submodule;
29
30#[cfg(test)]
31#[test]
32fn test_foo() {
33    assert_eq!(submodule::foo(), "foo");
34}
35""".splitlines(),
36    newline = "unix",
37)
38
39rust_library(
40    name = "libgensrc",
41    srcs = [
42        "lib.rs",
43        "submodule/mod.rs",
44        ":src.rs",
45    ],
46    edition = "2018",
47    tags = ["norustfmt"],
48)
49
50rust_library(
51    name = "libgensrc_with_crate_root",
52    srcs = [
53        "lib.rs",
54        "submodule/mod.rs",
55        ":src.rs",
56    ],
57    crate_root = "lib.rs",
58    edition = "2018",
59    tags = ["norustfmt"],
60    visibility = ["//visibility:public"],
61)
62
63rust_library(
64    name = "libgensrc_as_crate_root",
65    srcs = [
66        "lib.rs",
67        "lib_for_src.rs",
68        ":src.rs",
69    ],
70    crate_root = ":src.rs",
71    edition = "2018",
72    rustc_flags = ["--cfg=generated_file_as_root"],
73    tags = ["norustfmt"],
74)
75
76rust_library(
77    name = "lib_with_crate_root_in_subdir",
78    srcs = [
79        "src/generated.rs",
80        "src/generated/submodule.rs",
81        "src/lib.rs",
82    ],
83    crate_root = "src/lib.rs",
84    edition = "2018",
85    tags = ["norustfmt"],
86)
87
88rust_test(
89    name = "lib_with_srcs_in_subdir_test",
90    srcs = [
91        "src/generated.rs",
92        "src/generated/submodule.rs",
93        "src/lib.rs",
94    ],
95    edition = "2018",
96    tags = ["norustfmt"],
97)
98
99rust_test(
100    name = "lib_with_crate_root_in_subdir_test",
101    crate = "lib_with_crate_root_in_subdir",
102    edition = "2018",
103    tags = ["norustfmt"],
104)
105
106# When no lib.rs, main.rs file exists, we try to use the file that carries
107# the target's name as a crate_root.
108rust_library(
109    name = "src",
110    srcs = [
111        "lib_for_src.rs",
112        ":src.rs",
113    ],
114    edition = "2018",
115    rustc_flags = ["--cfg=generated_file_as_root"],
116    tags = ["norustfmt"],
117)
118
119rust_test(
120    name = "gensrc_test",
121    crate = ":libgensrc_with_crate_root",
122    edition = "2018",
123    tags = ["norustfmt"],
124)
125
126rust_binary(
127    name = "print_42",
128    srcs = [
129        "main.rs",
130        ":src.rs",
131    ],
132    edition = "2018",
133    tags = ["norustfmt"],
134)
135
136input_from_different_cfg(
137    name = "generated_in_different_cfg",
138)
139
140filegroup(
141    name = "input_from_different_cfg",
142    srcs = [":generated_in_different_cfg"],
143    output_group = "generated_file",
144)
145
146rust_library(
147    name = "gen_src_in_different_cfg_as_root",
148    srcs = [":input_from_different_cfg"],
149    edition = "2018",
150    tags = ["norustfmt"],
151)
152
153rust_test(
154    name = "gen_src_in_different_cfg_as_root_test",
155    crate = "gen_src_in_different_cfg_as_root",
156    edition = "2018",
157    tags = ["norustfmt"],
158)
159
160rust_library(
161    name = "gen_src_in_different_cfg",
162    srcs = [
163        "root.rs",
164        ":input_from_different_cfg",
165    ],
166    crate_root = "root.rs",
167    edition = "2018",
168    tags = ["norustfmt"],
169)
170
171rust_test(
172    name = "gen_src_in_different_cfg_test",
173    crate = "gen_src_in_different_cfg",
174    edition = "2018",
175    tags = ["norustfmt"],
176)
177
178bool_flag(
179    name = "change_cfg",
180    build_setting_default = False,
181)
182
183rust_test(
184    name = "gen_inputs_external_repo_test",
185    # This is regression testing a specific failure case for generated files _not_ in the root
186    # of an external repository.
187    crate = "@generated_inputs_in_external_repo//lib:generated_inputs_external_repo",
188    edition = "2021",
189)
190