fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

Cameron MartinPreserve directory structure of source files when some are generated (#1526)cfcaf21d

0.11.0
3.5 KiB169 linesraw
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 = "generate_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 = "generate_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 = "use_generated_src",
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 = "use_generated_src_with_crate_root_defined",
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)
61
62rust_library(
63    name = "use_generated_src_as_crate_root",
64    srcs = [
65        "lib.rs",
66        "lib_for_src.rs",
67        ":src.rs",
68    ],
69    crate_root = ":src.rs",
70    edition = "2018",
71    rustc_flags = ["--cfg=generated_file_as_root"],
72    tags = ["norustfmt"],
73)
74
75rust_library(
76    name = "use_generated_src_with_crate_root_in_subdir",
77    srcs = [
78        "src/generated.rs",
79        "src/generated/submodule.rs",
80        "src/lib.rs",
81    ],
82    crate_root = "src/lib.rs",
83    edition = "2018",
84    tags = ["norustfmt"],
85)
86
87rust_test(
88    name = "use_generated_src_with_crate_root_in_subdir_test",
89    crate = "use_generated_src_with_crate_root_in_subdir",
90    edition = "2018",
91    tags = ["norustfmt"],
92)
93
94# When no lib.rs, main.rs file exists, we try to use the file that carries
95# the target's name as a crate_root.
96rust_library(
97    name = "src",
98    srcs = [
99        "lib_for_src.rs",
100        ":src.rs",
101    ],
102    edition = "2018",
103    rustc_flags = ["--cfg=generated_file_as_root"],
104    tags = ["norustfmt"],
105)
106
107rust_test(
108    name = "generated_src_test",
109    crate = ":use_generated_src_with_crate_root_defined",
110    edition = "2018",
111    tags = ["norustfmt"],
112)
113
114rust_binary(
115    name = "print_42",
116    srcs = [
117        "main.rs",
118        ":src.rs",
119    ],
120    edition = "2018",
121    tags = ["norustfmt"],
122)
123
124input_from_different_cfg(
125    name = "generated_in_different_cfg",
126)
127
128filegroup(
129    name = "input_from_different_cfg",
130    srcs = [":generated_in_different_cfg"],
131    output_group = "generated_file",
132)
133
134rust_library(
135    name = "generated_src_in_different_cfg_as_root",
136    srcs = [":input_from_different_cfg"],
137    edition = "2018",
138    tags = ["norustfmt"],
139)
140
141rust_test(
142    name = "generated_src_in_different_cfg_as_root_test",
143    crate = "generated_src_in_different_cfg_as_root",
144    edition = "2018",
145    tags = ["norustfmt"],
146)
147
148rust_library(
149    name = "generated_src_in_different_cfg",
150    srcs = [
151        "root.rs",
152        ":input_from_different_cfg",
153    ],
154    crate_root = "root.rs",
155    edition = "2018",
156    tags = ["norustfmt"],
157)
158
159rust_test(
160    name = "generated_src_in_different_cfg_test",
161    crate = "generated_src_in_different_cfg",
162    edition = "2018",
163    tags = ["norustfmt"],
164)
165
166bool_flag(
167    name = "change_cfg",
168    build_setting_default = False,
169)