fork/rules_rust

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

UebelAndreRefactored the Rustfmt tool to work cross-platform (#1375)94e0044a

0.8.1
2.8 KiB133 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
24rust_library(
25    name = "use_generated_src",
26    srcs = [
27        "lib.rs",
28        ":src.rs",
29    ],
30    edition = "2018",
31    tags = ["norustfmt"],
32)
33
34rust_library(
35    name = "use_generated_src_with_crate_root_defined",
36    srcs = [
37        "lib.rs",
38        ":src.rs",
39    ],
40    crate_root = "lib.rs",
41    edition = "2018",
42    tags = ["norustfmt"],
43)
44
45rust_library(
46    name = "use_generated_src_as_crate_root",
47    srcs = [
48        "lib.rs",
49        "lib_for_src.rs",
50        ":src.rs",
51    ],
52    crate_root = ":src.rs",
53    edition = "2018",
54    rustc_flags = ["--cfg=generated_file_as_root"],
55    tags = ["norustfmt"],
56)
57
58# When no lib.rs, main.rs file exists, we try to use the file that carries
59# the target's name as a crate_root.
60rust_library(
61    name = "src",
62    srcs = [
63        "lib_for_src.rs",
64        ":src.rs",
65    ],
66    edition = "2018",
67    rustc_flags = ["--cfg=generated_file_as_root"],
68    tags = ["norustfmt"],
69)
70
71rust_test(
72    name = "generated_src_test",
73    crate = ":use_generated_src_with_crate_root_defined",
74    edition = "2018",
75    tags = ["norustfmt"],
76)
77
78rust_binary(
79    name = "print_42",
80    srcs = [
81        "main.rs",
82        ":src.rs",
83    ],
84    edition = "2018",
85    tags = ["norustfmt"],
86)
87
88input_from_different_cfg(
89    name = "generated_in_different_cfg",
90)
91
92filegroup(
93    name = "input_from_different_cfg",
94    srcs = [":generated_in_different_cfg"],
95    output_group = "generated_file",
96)
97
98rust_library(
99    name = "generated_src_in_different_cfg_as_root",
100    srcs = [":input_from_different_cfg"],
101    edition = "2018",
102    tags = ["norustfmt"],
103)
104
105rust_test(
106    name = "generated_src_in_different_cfg_as_root_test",
107    crate = "generated_src_in_different_cfg_as_root",
108    edition = "2018",
109    tags = ["norustfmt"],
110)
111
112rust_library(
113    name = "generated_src_in_different_cfg",
114    srcs = [
115        "root.rs",
116        ":input_from_different_cfg",
117    ],
118    crate_root = "root.rs",
119    edition = "2018",
120    tags = ["norustfmt"],
121)
122
123rust_test(
124    name = "generated_src_in_different_cfg_test",
125    crate = "generated_src_in_different_cfg",
126    edition = "2018",
127    tags = ["norustfmt"],
128)
129
130bool_flag(
131    name = "change_cfg",
132    build_setting_default = False,
133)