Skip to content

Commit d3a8297

Browse files
committed
Refactor backends.rs
Refactored to ensure the main cfg_if consists of getrandom_backends, then selection exclusively by target_os. This means ordering of branches is far less consequential. Instead, consequential ordering is nested within the arm that selects a particular OS.
1 parent 5e6b022 commit d3a8297

File tree

1 file changed

+144
-76
lines changed

1 file changed

+144
-76
lines changed

src/backends.rs

Lines changed: 144 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,45 @@ cfg_if! {
3535
} else if #[cfg(getrandom_backend = "extern_impl")] {
3636
pub(crate) mod extern_impl;
3737
pub use extern_impl::*;
38-
} else if #[cfg(all(target_os = "linux", target_env = ""))] {
39-
mod linux_raw;
40-
pub use linux_raw::*;
38+
} else if #[cfg(target_os = "linux")] {
39+
cfg_if! {
40+
if #[cfg(target_env = "")] {
41+
mod linux_raw;
42+
pub use linux_raw::*;
43+
} else if #[cfg(
44+
// Only on these `target_arch`es Rust supports Linux kernel versions (3.2+)
45+
// that precede the version (3.17) in which `getrandom(2)` was added:
46+
// https://doc.rust-lang.org/stable/rustc/platform-support.html
47+
any(
48+
target_arch = "aarch64",
49+
target_arch = "arm",
50+
target_arch = "powerpc",
51+
target_arch = "powerpc64",
52+
target_arch = "s390x",
53+
target_arch = "x86",
54+
target_arch = "x86_64",
55+
// Minimum supported Linux kernel version for MUSL targets
56+
// is not specified explicitly (as of Rust 1.77) and they
57+
// are used in practice to target pre-3.17 kernels.
58+
all(
59+
target_env = "musl",
60+
not(
61+
any(
62+
target_arch = "riscv64",
63+
target_arch = "riscv32",
64+
),
65+
),
66+
),
67+
),
68+
)] {
69+
mod use_file;
70+
mod linux_android_with_fallback;
71+
pub use linux_android_with_fallback::*;
72+
} else {
73+
mod getrandom;
74+
pub use getrandom::*;
75+
}
76+
}
4177
} else if #[cfg(target_os = "espidf")] {
4278
mod esp_idf;
4379
pub use esp_idf::*;
@@ -57,68 +93,61 @@ cfg_if! {
5793
))] {
5894
mod getentropy;
5995
pub use getentropy::*;
60-
} else if #[cfg(any(
61-
// Rust supports Android API level 19 (KitKat) [0] and the next upgrade targets
62-
// level 21 (Lollipop) [1], while `getrandom(2)` was added only in
63-
// level 23 (Marshmallow). Note that it applies only to the "old" `target_arch`es,
64-
// RISC-V Android targets sufficiently new API level, same will apply for potential
65-
// new Android `target_arch`es.
66-
// [0]: https://blog.rust-lang.org/2023/01/09/android-ndk-update-r25.html
67-
// [1]: https://github.com/rust-lang/rust/pull/120593
68-
all(
69-
target_os = "android",
70-
any(
71-
target_arch = "aarch64",
72-
target_arch = "arm",
73-
target_arch = "x86",
74-
target_arch = "x86_64",
75-
),
76-
),
77-
// Only on these `target_arch`es Rust supports Linux kernel versions (3.2+)
78-
// that precede the version (3.17) in which `getrandom(2)` was added:
79-
// https://doc.rust-lang.org/stable/rustc/platform-support.html
80-
all(
81-
target_os = "linux",
82-
any(
83-
target_arch = "aarch64",
84-
target_arch = "arm",
85-
target_arch = "powerpc",
86-
target_arch = "powerpc64",
87-
target_arch = "s390x",
88-
target_arch = "x86",
89-
target_arch = "x86_64",
90-
// Minimum supported Linux kernel version for MUSL targets
91-
// is not specified explicitly (as of Rust 1.77) and they
92-
// are used in practice to target pre-3.17 kernels.
93-
all(
94-
target_env = "musl",
95-
not(
96-
any(
97-
target_arch = "riscv64",
98-
target_arch = "riscv32",
99-
),
100-
),
96+
} else if #[cfg(target_os = "android")] {
97+
cfg_if! {
98+
if #[cfg(
99+
// Rust supports Android API level 19 (KitKat) [0] and the next upgrade targets
100+
// level 21 (Lollipop) [1], while `getrandom(2)` was added only in
101+
// level 23 (Marshmallow). Note that it applies only to the "old" `target_arch`es,
102+
// RISC-V Android targets sufficiently new API level, same will apply for potential
103+
// new Android `target_arch`es.
104+
// [0]: https://blog.rust-lang.org/2023/01/09/android-ndk-update-r25.html
105+
// [1]: https://github.com/rust-lang/rust/pull/120593
106+
any(
107+
target_arch = "aarch64",
108+
target_arch = "arm",
109+
target_arch = "x86",
110+
target_arch = "x86_64",
101111
),
102-
),
103-
)
104-
))] {
105-
mod use_file;
106-
mod linux_android_with_fallback;
107-
pub use linux_android_with_fallback::*;
112+
)] {
113+
mod use_file;
114+
mod linux_android_with_fallback;
115+
pub use linux_android_with_fallback::*;
116+
} else {
117+
mod getrandom;
118+
pub use getrandom::*;
119+
}
120+
}
108121
} else if #[cfg(any(
109-
target_os = "android",
110-
target_os = "linux",
111122
target_os = "dragonfly",
112123
target_os = "freebsd",
113124
target_os = "hurd",
114125
target_os = "illumos",
115126
target_os = "cygwin",
116-
// Check for target_arch = "arm" to only include the 3DS. Does not
117-
// include the Nintendo Switch (which is target_arch = "aarch64").
118-
all(target_os = "horizon", target_arch = "arm"),
119127
))] {
120128
mod getrandom;
121129
pub use getrandom::*;
130+
} else if #[cfg(target_os = "horizon")] {
131+
cfg_if! {
132+
// Check for target_arch = "arm" to only include the 3DS. Does not
133+
// include the Nintendo Switch (which is target_arch = "aarch64")
134+
if #[cfg(target_arch = "arm")] {
135+
mod getrandom;
136+
pub use getrandom::*;
137+
} else if #[cfg(target_arch = "aarch64")] {
138+
compile_error!(concat!(
139+
"The horizon operating system is not supported by default on the Nintendo Switch; \
140+
You may need to define a custom backend see: \
141+
https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#custom-backend"
142+
));
143+
} else {
144+
compile_error!(concat!(
145+
"The horizon operating system is only supported by default on the 3DS; \
146+
You may need to define a custom backend see: \
147+
https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#custom-backend"
148+
));
149+
}
150+
}
122151
} else if #[cfg(target_os = "solaris")] {
123152
mod solaris;
124153
pub use solaris::*;
@@ -136,7 +165,7 @@ cfg_if! {
136165
))] {
137166
mod apple_other;
138167
pub use apple_other::*;
139-
} else if #[cfg(all(target_arch = "wasm32", target_os = "wasi"))] {
168+
} else if #[cfg(target_os = "wasi")] {
140169
cfg_if! {
141170
if #[cfg(target_env = "p1")] {
142171
mod wasi_p1;
@@ -149,35 +178,74 @@ cfg_if! {
149178
} else if #[cfg(target_os = "hermit")] {
150179
mod hermit;
151180
pub use hermit::*;
152-
} else if #[cfg(all(target_arch = "x86_64", target_os = "motor"))] {
153-
mod rdrand;
154-
pub use rdrand::*;
181+
} else if #[cfg(target_os = "motor")] {
182+
cfg_if! {
183+
if #[cfg(target_arch = "x86_64")] {
184+
mod rdrand;
185+
pub use rdrand::*;
186+
} else {
187+
compile_error!(concat!(
188+
"The motor operating system is only supported by default on x86_64; \
189+
You may need to define a custom backend see: \
190+
https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#custom-backend"
191+
));
192+
}
193+
}
155194
} else if #[cfg(target_os = "vxworks")] {
156195
mod vxworks;
157196
pub use vxworks::*;
158197
} else if #[cfg(target_os = "solid_asp3")] {
159198
mod solid;
160199
pub use solid::*;
161-
} else if #[cfg(all(windows, target_vendor = "win7"))] {
162-
mod windows_legacy;
163-
pub use windows_legacy::*;
164-
} else if #[cfg(windows)] {
165-
mod windows;
166-
pub use windows::*;
167-
} else if #[cfg(all(target_arch = "x86_64", target_env = "sgx"))] {
168-
mod rdrand;
169-
pub use rdrand::*;
170-
} else if #[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))] {
200+
} else if #[cfg(target_os = "windows")] {
201+
cfg_if! {
202+
if #[cfg(target_vendor = "win7")] {
203+
mod windows_legacy;
204+
pub use windows_legacy::*;
205+
} else {
206+
mod windows;
207+
pub use windows::*;
208+
}
209+
}
210+
} else if #[cfg(target_os = "uefi")] {
211+
compile_error!(concat!(
212+
"UEFI is not supported by default; \
213+
Consider enabling the efi_rng backend see: \
214+
https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#opt-in-backends"
215+
));
216+
} else if #[cfg(any(
217+
target_os = "unknown",
218+
target_os = "none"
219+
))] {
171220
cfg_if! {
172-
if #[cfg(feature = "wasm_js")] {
173-
mod wasm_js;
174-
pub use wasm_js::*;
221+
if #[cfg(any(
222+
target_arch = "x86"
223+
target_arch = "x86_64",
224+
))] {
225+
mod rdrand;
226+
pub use rdrand::*;
227+
} else if #[cfg(target_arch = "aarch64")] {
228+
mod rndr;
229+
pub use rndr::*;
230+
} else if #[cfg(target_arch = "wasm32")] {
231+
cfg_if! {
232+
if #[cfg(feature = "wasm_js")] {
233+
mod wasm_js;
234+
pub use wasm_js::*;
235+
} else {
236+
compile_error!(concat!(
237+
"The wasm32-unknown-unknown targets are not supported by default; \
238+
you may need to enable the \"wasm_js\" crate feature. \
239+
For more information see: \
240+
https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#webassembly-support"
241+
));
242+
}
243+
}
175244
} else {
176245
compile_error!(concat!(
177-
"The wasm32-unknown-unknown targets are not supported by default; \
178-
you may need to enable the \"wasm_js\" crate feature. \
179-
For more information see: \
180-
https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#webassembly-support"
246+
"Architecture is not supported by default without a supported operating system; \
247+
You may need to define a custom backend see: \
248+
https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#custom-backend"
181249
));
182250
}
183251
}

0 commit comments

Comments
 (0)