Skip to content

Commit f1418c7

Browse files
authored
Use OS groups and star imports to reduce cfg boilerplate. (#549)
* Use OS groups and star imports to reduce `cfg` boilerplate. Define cfg flags following the OS groups from the libc crate, linux_like, freebsdlike, netbsdlike, apple, and solarish, to factor out common cfg groups. And use `pub use foo::*;` instead of explicitly naming all the contents of `foo` in several places, reducing the need for `cfg` boilerplate. There's more that can be done here, but this is a start. * Simplify and fix `PROC_SUPER_MAGIC` etc.
1 parent d2ddce1 commit f1418c7

File tree

34 files changed

+198
-651
lines changed

34 files changed

+198
-651
lines changed

build.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn main() {
2626
let arch = var("CARGO_CFG_TARGET_ARCH").unwrap();
2727
let asm_name = format!("{}/{}.s", OUTLINE_PATH, arch);
2828
let asm_name_present = std::fs::metadata(&asm_name).is_ok();
29-
let os_name = var("CARGO_CFG_TARGET_OS").unwrap();
29+
let target_os = var("CARGO_CFG_TARGET_OS").unwrap();
3030
let pointer_width = var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap();
3131
let endian = var("CARGO_CFG_TARGET_ENDIAN").unwrap();
3232

@@ -69,7 +69,7 @@ fn main() {
6969
// install the toolchain for it.
7070
if feature_use_libc
7171
|| cfg_use_libc
72-
|| os_name != "linux"
72+
|| target_os != "linux"
7373
|| !asm_name_present
7474
|| is_unsupported_abi
7575
|| miri
@@ -106,6 +106,39 @@ fn main() {
106106
use_feature("thumb_mode");
107107
}
108108

109+
// Rust's libc crate groups some OS's together which have similar APIs;
110+
// create similarly-named features to make `cfg` tests more concise.
111+
if target_os == "freebsd" || target_os == "dragonfly" {
112+
use_feature("freebsdlike");
113+
}
114+
if target_os == "openbsd" || target_os == "netbsd" {
115+
use_feature("netbsdlike");
116+
}
117+
if target_os == "macos" || target_os == "ios" || target_os == "tvos" || target_os == "watchos" {
118+
use_feature("apple");
119+
}
120+
if target_os == "linux"
121+
|| target_os == "l4re"
122+
|| target_os == "android"
123+
|| target_os == "emscripten"
124+
{
125+
use_feature("linux_like");
126+
}
127+
if target_os == "solaris" || target_os == "illumos" {
128+
use_feature("solarish");
129+
}
130+
if target_os == "macos"
131+
|| target_os == "ios"
132+
|| target_os == "tvos"
133+
|| target_os == "watchos"
134+
|| target_os == "freebsd"
135+
|| target_os == "dragonfly"
136+
|| target_os == "openbsd"
137+
|| target_os == "netbsd"
138+
{
139+
use_feature("bsd");
140+
}
141+
109142
println!("cargo:rerun-if-env-changed=CARGO_CFG_RUSTIX_USE_EXPERIMENTAL_ASM");
110143
println!("cargo:rerun-if-env-changed=CARGO_CFG_RUSTIX_USE_LIBC");
111144

examples/process.rs

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -59,77 +59,28 @@ fn main() -> io::Result<()> {
5959
#[cfg(not(target_os = "openbsd"))]
6060
println!("As Limit: {:?}", getrlimit(Resource::As));
6161
#[cfg(not(any(
62-
target_os = "dragonfly",
63-
target_os = "freebsd",
62+
apple,
63+
freebsdlike,
64+
solarish,
6465
target_os = "haiku",
65-
target_os = "illumos",
66-
target_os = "ios",
67-
target_os = "macos",
6866
target_os = "netbsd",
6967
target_os = "openbsd",
70-
target_os = "solaris",
7168
)))]
7269
println!("Locks Limit: {:?}", getrlimit(Resource::Locks));
73-
#[cfg(not(any(
74-
target_os = "dragonfly",
75-
target_os = "freebsd",
76-
target_os = "haiku",
77-
target_os = "illumos",
78-
target_os = "ios",
79-
target_os = "macos",
80-
target_os = "netbsd",
81-
target_os = "openbsd",
82-
target_os = "solaris",
83-
)))]
70+
#[cfg(not(any(bsd, solarish, target_os = "haiku")))]
8471
println!("Sigpending Limit: {:?}", getrlimit(Resource::Sigpending));
85-
#[cfg(not(any(
86-
target_os = "dragonfly",
87-
target_os = "freebsd",
88-
target_os = "haiku",
89-
target_os = "illumos",
90-
target_os = "ios",
91-
target_os = "macos",
92-
target_os = "netbsd",
93-
target_os = "openbsd",
94-
target_os = "solaris",
95-
)))]
72+
#[cfg(not(any(bsd, solarish, target_os = "haiku")))]
9673
println!("Msgqueue Limit: {:?}", getrlimit(Resource::Msgqueue));
97-
#[cfg(not(any(
98-
target_os = "dragonfly",
99-
target_os = "freebsd",
100-
target_os = "haiku",
101-
target_os = "illumos",
102-
target_os = "ios",
103-
target_os = "macos",
104-
target_os = "netbsd",
105-
target_os = "openbsd",
106-
target_os = "solaris",
107-
)))]
74+
#[cfg(not(any(bsd, solarish, target_os = "haiku")))]
10875
println!("Nice Limit: {:?}", getrlimit(Resource::Nice));
109-
#[cfg(not(any(
110-
target_os = "dragonfly",
111-
target_os = "freebsd",
112-
target_os = "haiku",
113-
target_os = "illumos",
114-
target_os = "ios",
115-
target_os = "macos",
116-
target_os = "netbsd",
117-
target_os = "openbsd",
118-
target_os = "solaris",
119-
)))]
76+
#[cfg(not(any(bsd, solarish, target_os = "haiku")))]
12077
println!("Rtprio Limit: {:?}", getrlimit(Resource::Rtprio));
12178
#[cfg(not(any(
79+
bsd,
80+
solarish,
12281
target_os = "android",
123-
target_os = "dragonfly",
12482
target_os = "emscripten",
125-
target_os = "freebsd",
12683
target_os = "haiku",
127-
target_os = "illumos",
128-
target_os = "ios",
129-
target_os = "macos",
130-
target_os = "netbsd",
131-
target_os = "openbsd",
132-
target_os = "solaris",
13384
)))]
13485
println!("Rttime Limit: {:?}", getrlimit(Resource::Rttime));
13586
}

examples/stdio.rs

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,8 @@ fn show<Fd: AsFd>(fd: Fd) -> io::Result<()> {
129129
print!(" IMAXBEL");
130130
}
131131
#[cfg(not(any(
132-
target_os = "dragonfly",
132+
freebsdlike,
133133
target_os = "emscripten",
134-
target_os = "freebsd",
135134
target_os = "haiku",
136135
target_os = "illumos",
137136
target_os = "ios",
@@ -151,14 +150,7 @@ fn show<Fd: AsFd>(fd: Fd) -> io::Result<()> {
151150
if (term.c_oflag & OPOST) != 0 {
152151
print!(" OPOST");
153152
}
154-
#[cfg(not(any(
155-
target_os = "dragonfly",
156-
target_os = "freebsd",
157-
target_os = "ios",
158-
target_os = "macos",
159-
target_os = "netbsd",
160-
target_os = "redox",
161-
)))]
153+
#[cfg(not(any(apple, freebsdlike, target_os = "netbsd", target_os = "redox")))]
162154
if (term.c_oflag & OLCUC) != 0 {
163155
print!(" OLCUC");
164156
}
@@ -178,14 +170,7 @@ fn show<Fd: AsFd>(fd: Fd) -> io::Result<()> {
178170
if (term.c_oflag & ONLRET) != 0 {
179171
print!(" ONLRET");
180172
}
181-
#[cfg(not(any(
182-
target_os = "dragonfly",
183-
target_os = "freebsd",
184-
target_os = "ios",
185-
target_os = "macos",
186-
target_os = "netbsd",
187-
target_os = "openbsd",
188-
)))]
173+
#[cfg(not(bsd))]
189174
if (term.c_oflag & OFILL) != 0 {
190175
print!(" OFILL");
191176
}
@@ -200,57 +185,33 @@ fn show<Fd: AsFd>(fd: Fd) -> io::Result<()> {
200185
if (term.c_oflag & OFDEL) != 0 {
201186
print!(" OFDEL");
202187
}
203-
#[cfg(not(any(
204-
target_os = "dragonfly",
205-
target_os = "freebsd",
206-
target_os = "illumos",
207-
target_os = "ios",
208-
target_os = "macos",
209-
target_os = "netbsd",
210-
target_os = "openbsd",
211-
target_os = "redox",
212-
target_os = "solaris",
213-
)))]
188+
#[cfg(not(any(bsd, solarish, target_os = "redox")))]
214189
if (term.c_oflag & NLDLY) != 0 {
215190
print!(" NLDLY");
216191
}
217-
#[cfg(not(any(
218-
target_os = "dragonfly",
219-
target_os = "freebsd",
220-
target_os = "illumos",
221-
target_os = "ios",
222-
target_os = "macos",
223-
target_os = "netbsd",
224-
target_os = "openbsd",
225-
target_os = "redox",
226-
target_os = "solaris",
227-
)))]
192+
#[cfg(not(any(bsd, solarish, target_os = "redox")))]
228193
if (term.c_oflag & CRDLY) != 0 {
229194
print!(" CRDLY");
230195
}
231196
#[cfg(not(any(
197+
apple,
198+
netbsdlike,
199+
solarish,
232200
target_os = "dragonfly",
233-
target_os = "ios",
234-
target_os = "macos",
235-
target_os = "netbsd",
236-
target_os = "openbsd",
237-
target_os = "illumos",
238201
target_os = "redox",
239-
target_os = "solaris",
240202
)))]
241203
if (term.c_oflag & TABDLY) != 0 {
242204
print!(" TABDLY");
243205
}
244206
#[cfg(not(any(
207+
solarish,
245208
target_os = "dragonfly",
246209
target_os = "freebsd",
247-
target_os = "illumos",
248210
target_os = "ios",
249211
target_os = "macos",
250212
target_os = "netbsd",
251213
target_os = "openbsd",
252214
target_os = "redox",
253-
target_os = "solaris",
254215
)))]
255216
if (term.c_oflag & BSDLY) != 0 {
256217
print!(" BSDLY");

src/backend/libc/c.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pub(crate) use libc::*;
2+
3+
/// `PROC_SUPER_MAGIC`—The magic number for the procfs filesystem.
4+
#[cfg(all(any(target_os = "android", target_os = "linux"), target_env = "musl"))]
5+
pub(crate) const PROC_SUPER_MAGIC: u32 = 0x0000_9fa0;
6+
7+
/// `NFS_SUPER_MAGIC`—The magic number for the NFS filesystem.
8+
#[cfg(all(any(target_os = "android", target_os = "linux"), target_env = "musl"))]
9+
pub(crate) const NFS_SUPER_MAGIC: u32 = 0x0000_6969;

src/backend/libc/fs/dir.rs

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,18 @@ use crate::fs::{fcntl_getfl, fstat, openat, Mode, OFlags, Stat};
1616
target_os = "wasi",
1717
)))]
1818
use crate::fs::{fstatfs, StatFs};
19-
#[cfg(not(any(
20-
target_os = "haiku",
21-
target_os = "illumos",
22-
target_os = "redox",
23-
target_os = "solaris",
24-
target_os = "wasi",
25-
)))]
19+
#[cfg(not(any(solarish, target_os = "haiku", target_os = "redox", target_os = "wasi")))]
2620
use crate::fs::{fstatvfs, StatVfs};
2721
use crate::io;
2822
#[cfg(not(any(target_os = "fuchsia", target_os = "wasi")))]
2923
use crate::process::fchdir;
3024
#[cfg(target_os = "wasi")]
3125
use alloc::borrow::ToOwned;
32-
#[cfg(not(any(
33-
target_os = "android",
34-
target_os = "emscripten",
35-
target_os = "l4re",
36-
target_os = "linux",
37-
target_os = "openbsd",
38-
)))]
26+
#[cfg(not(any(linux_like, target_os = "openbsd")))]
3927
use c::dirent as libc_dirent;
40-
#[cfg(not(any(
41-
target_os = "android",
42-
target_os = "emscripten",
43-
target_os = "l4re",
44-
target_os = "linux",
45-
)))]
28+
#[cfg(not(linux_like))]
4629
use c::readdir as libc_readdir;
47-
#[cfg(any(
48-
target_os = "android",
49-
target_os = "emscripten",
50-
target_os = "l4re",
51-
target_os = "linux",
52-
))]
30+
#[cfg(linux_like)]
5331
use c::{dirent64 as libc_dirent, readdir64 as libc_readdir};
5432
use core::fmt;
5533
use core::mem::zeroed;

src/backend/libc/fs/syscalls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use crate::fd::{BorrowedFd, OwnedFd};
6262
use crate::ffi::CStr;
6363
#[cfg(any(target_os = "ios", target_os = "macos"))]
6464
use crate::ffi::CString;
65-
#[cfg(not(any(target_os = "illumos", target_os = "solaris")))]
65+
#[cfg(not(solarish))]
6666
use crate::fs::Access;
6767
#[cfg(not(any(
6868
target_os = "dragonfly",

0 commit comments

Comments
 (0)