Skip to content

Commit 432b4c1

Browse files
committed
Use cfg_if in libtest.
Simplifies some of the expressions, and provides a default.
1 parent 8c6c1dd commit 432b4c1

File tree

4 files changed

+98
-131
lines changed

4 files changed

+98
-131
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4552,6 +4552,7 @@ dependencies = [
45524552
name = "test"
45534553
version = "0.0.0"
45544554
dependencies = [
4555+
"cfg-if",
45554556
"core",
45564557
"getopts",
45574558
"libc",

src/libtest/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ path = "lib.rs"
1010
crate-type = ["dylib", "rlib"]
1111

1212
[dependencies]
13+
cfg-if = { version = "0.1.8", features = ['rustc-dep-of-std'] }
1314
getopts = { version = "0.2.21", features = ['rustc-dep-of-std'] }
1415
term = { path = "../libterm" }
1516
std = { path = "../libstd" }

src/libtest/helpers/concurrency.rs

Lines changed: 69 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -14,61 +14,36 @@ pub fn get_concurrency() -> usize {
1414
}
1515
Err(..) => num_cpus(),
1616
};
17+
}
1718

18-
#[cfg(windows)]
19-
#[allow(nonstandard_style)]
20-
fn num_cpus() -> usize {
21-
#[repr(C)]
22-
struct SYSTEM_INFO {
23-
wProcessorArchitecture: u16,
24-
wReserved: u16,
25-
dwPageSize: u32,
26-
lpMinimumApplicationAddress: *mut u8,
27-
lpMaximumApplicationAddress: *mut u8,
28-
dwActiveProcessorMask: *mut u8,
29-
dwNumberOfProcessors: u32,
30-
dwProcessorType: u32,
31-
dwAllocationGranularity: u32,
32-
wProcessorLevel: u16,
33-
wProcessorRevision: u16,
34-
}
35-
extern "system" {
36-
fn GetSystemInfo(info: *mut SYSTEM_INFO) -> i32;
37-
}
38-
unsafe {
39-
let mut sysinfo = std::mem::zeroed();
40-
GetSystemInfo(&mut sysinfo);
41-
sysinfo.dwNumberOfProcessors as usize
19+
cfg_if::cfg_if! {
20+
if #[cfg(windows)] {
21+
#[allow(nonstandard_style)]
22+
fn num_cpus() -> usize {
23+
#[repr(C)]
24+
struct SYSTEM_INFO {
25+
wProcessorArchitecture: u16,
26+
wReserved: u16,
27+
dwPageSize: u32,
28+
lpMinimumApplicationAddress: *mut u8,
29+
lpMaximumApplicationAddress: *mut u8,
30+
dwActiveProcessorMask: *mut u8,
31+
dwNumberOfProcessors: u32,
32+
dwProcessorType: u32,
33+
dwAllocationGranularity: u32,
34+
wProcessorLevel: u16,
35+
wProcessorRevision: u16,
36+
}
37+
extern "system" {
38+
fn GetSystemInfo(info: *mut SYSTEM_INFO) -> i32;
39+
}
40+
unsafe {
41+
let mut sysinfo = std::mem::zeroed();
42+
GetSystemInfo(&mut sysinfo);
43+
sysinfo.dwNumberOfProcessors as usize
44+
}
4245
}
43-
}
44-
45-
#[cfg(target_os = "vxworks")]
46-
fn num_cpus() -> usize {
47-
// FIXME: Implement num_cpus on vxWorks
48-
1
49-
}
50-
51-
#[cfg(target_os = "redox")]
52-
fn num_cpus() -> usize {
53-
// FIXME: Implement num_cpus on Redox
54-
1
55-
}
56-
57-
#[cfg(target_os = "hermit")]
58-
fn num_cpus() -> usize {
59-
// FIXME: Implement num_cpus on HermitCore
60-
1
61-
}
62-
63-
#[cfg(any(
64-
all(target_arch = "wasm32", not(target_os = "emscripten")),
65-
all(target_vendor = "fortanix", target_env = "sgx")
66-
))]
67-
fn num_cpus() -> usize {
68-
1
69-
}
70-
71-
#[cfg(any(
46+
} else if #[cfg(any(
7247
target_os = "android",
7348
target_os = "cloudabi",
7449
target_os = "emscripten",
@@ -78,23 +53,46 @@ pub fn get_concurrency() -> usize {
7853
target_os = "macos",
7954
target_os = "solaris",
8055
target_os = "illumos",
81-
))]
82-
fn num_cpus() -> usize {
83-
unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as usize }
84-
}
85-
86-
#[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd"))]
87-
fn num_cpus() -> usize {
88-
use std::ptr;
56+
))] {
57+
fn num_cpus() -> usize {
58+
unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as usize }
59+
}
60+
} else if #[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd"))] {
61+
fn num_cpus() -> usize {
62+
use std::ptr;
8963

90-
let mut cpus: libc::c_uint = 0;
91-
let mut cpus_size = std::mem::size_of_val(&cpus);
64+
let mut cpus: libc::c_uint = 0;
65+
let mut cpus_size = std::mem::size_of_val(&cpus);
9266

93-
unsafe {
94-
cpus = libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as libc::c_uint;
67+
unsafe {
68+
cpus = libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as libc::c_uint;
69+
}
70+
if cpus < 1 {
71+
let mut mib = [libc::CTL_HW, libc::HW_NCPU, 0, 0];
72+
unsafe {
73+
libc::sysctl(
74+
mib.as_mut_ptr(),
75+
2,
76+
&mut cpus as *mut _ as *mut _,
77+
&mut cpus_size as *mut _ as *mut _,
78+
ptr::null_mut(),
79+
0,
80+
);
81+
}
82+
if cpus < 1 {
83+
cpus = 1;
84+
}
85+
}
86+
cpus as usize
9587
}
96-
if cpus < 1 {
88+
} else if #[cfg(target_os = "openbsd")] {
89+
fn num_cpus() -> usize {
90+
use std::ptr;
91+
92+
let mut cpus: libc::c_uint = 0;
93+
let mut cpus_size = std::mem::size_of_val(&cpus);
9794
let mut mib = [libc::CTL_HW, libc::HW_NCPU, 0, 0];
95+
9896
unsafe {
9997
libc::sysctl(
10098
mib.as_mut_ptr(),
@@ -108,43 +106,12 @@ pub fn get_concurrency() -> usize {
108106
if cpus < 1 {
109107
cpus = 1;
110108
}
109+
cpus as usize
111110
}
112-
cpus as usize
113-
}
114-
115-
#[cfg(target_os = "openbsd")]
116-
fn num_cpus() -> usize {
117-
use std::ptr;
118-
119-
let mut cpus: libc::c_uint = 0;
120-
let mut cpus_size = std::mem::size_of_val(&cpus);
121-
let mut mib = [libc::CTL_HW, libc::HW_NCPU, 0, 0];
122-
123-
unsafe {
124-
libc::sysctl(
125-
mib.as_mut_ptr(),
126-
2,
127-
&mut cpus as *mut _ as *mut _,
128-
&mut cpus_size as *mut _ as *mut _,
129-
ptr::null_mut(),
130-
0,
131-
);
132-
}
133-
if cpus < 1 {
134-
cpus = 1;
111+
} else {
112+
// FIXME: implement on vxWorks, Redox, HermitCore, Haiku, l4re
113+
fn num_cpus() -> usize {
114+
1
135115
}
136-
cpus as usize
137-
}
138-
139-
#[cfg(target_os = "haiku")]
140-
fn num_cpus() -> usize {
141-
// FIXME: implement
142-
1
143-
}
144-
145-
#[cfg(target_os = "l4re")]
146-
fn num_cpus() -> usize {
147-
// FIXME: implement
148-
1
149116
}
150117
}

src/libtest/helpers/isatty.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
//! Helper module which provides a function to test
22
//! if stdout is a tty.
33
4-
#[cfg(any(
5-
target_os = "cloudabi",
6-
target_os = "hermit",
7-
all(target_arch = "wasm32", not(target_os = "emscripten")),
8-
all(target_vendor = "fortanix", target_env = "sgx")
9-
))]
10-
pub fn stdout_isatty() -> bool {
11-
// FIXME: Implement isatty on SGX
12-
false
13-
}
14-
#[cfg(unix)]
15-
pub fn stdout_isatty() -> bool {
16-
unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 }
17-
}
18-
#[cfg(windows)]
19-
pub fn stdout_isatty() -> bool {
20-
type DWORD = u32;
21-
type BOOL = i32;
22-
type HANDLE = *mut u8;
23-
type LPDWORD = *mut u32;
24-
const STD_OUTPUT_HANDLE: DWORD = -11i32 as DWORD;
25-
extern "system" {
26-
fn GetStdHandle(which: DWORD) -> HANDLE;
27-
fn GetConsoleMode(hConsoleHandle: HANDLE, lpMode: LPDWORD) -> BOOL;
28-
}
29-
unsafe {
30-
let handle = GetStdHandle(STD_OUTPUT_HANDLE);
31-
let mut out = 0;
32-
GetConsoleMode(handle, &mut out) != 0
4+
cfg_if::cfg_if! {
5+
if #[cfg(unix)] {
6+
pub fn stdout_isatty() -> bool {
7+
unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 }
8+
}
9+
} else if #[cfg(windows)] {
10+
pub fn stdout_isatty() -> bool {
11+
type DWORD = u32;
12+
type BOOL = i32;
13+
type HANDLE = *mut u8;
14+
type LPDWORD = *mut u32;
15+
const STD_OUTPUT_HANDLE: DWORD = -11i32 as DWORD;
16+
extern "system" {
17+
fn GetStdHandle(which: DWORD) -> HANDLE;
18+
fn GetConsoleMode(hConsoleHandle: HANDLE, lpMode: LPDWORD) -> BOOL;
19+
}
20+
unsafe {
21+
let handle = GetStdHandle(STD_OUTPUT_HANDLE);
22+
let mut out = 0;
23+
GetConsoleMode(handle, &mut out) != 0
24+
}
25+
}
26+
} else {
27+
// FIXME: Implement isatty on SGX
28+
pub fn stdout_isatty() -> bool {
29+
false
30+
}
3331
}
3432
}

0 commit comments

Comments
 (0)