Skip to content

Commit 8ce6875

Browse files
dpaolielloAmanieu
authored andcommitted
Remove libc dependency on Windows by using Win32 to get env vars
1 parent dee4c16 commit 8ce6875

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

Diff for: .github/workflows/main.yml

+9-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
steps:
1616
- uses: actions/checkout@v4
1717
- name: Install Rust
18-
run: rustup update nightly && rustup default nightly
18+
run: rustup update nightly --no-self-update && rustup default nightly
1919
- run: ci/style.sh
2020

2121
docs:
@@ -25,7 +25,7 @@ jobs:
2525
steps:
2626
- uses: actions/checkout@v4
2727
- name: Install Rust
28-
run: rustup update nightly && rustup default nightly
28+
run: rustup update nightly --no-self-update && rustup default nightly
2929
- run: ci/dox.sh
3030
env:
3131
CI: 1
@@ -45,18 +45,22 @@ jobs:
4545
steps:
4646
- uses: actions/checkout@v4
4747
- name: Install Rust
48-
run: rustup update nightly && rustup default nightly
48+
run: rustup update nightly --no-self-update && rustup default nightly
4949
- run: cargo test --manifest-path crates/stdarch-verify/Cargo.toml
5050

5151
env_override:
5252
name: Env Override
5353
needs: [style]
54-
runs-on: ubuntu-latest
54+
runs-on: ${{ matrix.os }}
55+
strategy:
56+
matrix:
57+
os: [ubuntu-latest, windows-latest]
5558
steps:
5659
- uses: actions/checkout@v4
5760
- name: Install Rust
58-
run: rustup update nightly && rustup default nightly
61+
run: rustup update nightly --no-self-update && rustup default nightly
5962
- run: RUST_STD_DETECT_UNSTABLE=avx cargo test --features=std_detect_env_override --manifest-path crates/std_detect/Cargo.toml env_override_no_avx
63+
shell: bash
6064

6165
test:
6266
needs: [style]

Diff for: crates/std_detect/src/detect/cache.rs

+34-9
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,42 @@ impl Cache {
118118

119119
cfg_if::cfg_if! {
120120
if #[cfg(feature = "std_detect_env_override")] {
121+
#[inline]
122+
fn disable_features(disable: &[u8], value: &mut Initializer) {
123+
if let Ok(disable) = core::str::from_utf8(disable) {
124+
for v in disable.split(" ") {
125+
let _ = super::Feature::from_str(v).map(|v| value.unset(v as u32));
126+
}
127+
}
128+
}
129+
121130
#[inline]
122131
fn initialize(mut value: Initializer) -> Initializer {
123-
let env = unsafe {
124-
libc::getenv(b"RUST_STD_DETECT_UNSTABLE\0".as_ptr() as *const libc::c_char)
125-
};
126-
if !env.is_null() {
127-
let len = unsafe { libc::strlen(env) };
128-
let env = unsafe { core::slice::from_raw_parts(env as *const u8, len) };
129-
if let Ok(disable) = core::str::from_utf8(env) {
130-
for v in disable.split(" ") {
131-
let _ = super::Feature::from_str(v).map(|v| value.unset(v as u32));
132+
const RUST_STD_DETECT_UNSTABLE: &[u8] = b"RUST_STD_DETECT_UNSTABLE\0";
133+
cfg_if::cfg_if! {
134+
if #[cfg(windows)] {
135+
use alloc::vec;
136+
#[link(name = "kernel32")]
137+
extern "system" {
138+
fn GetEnvironmentVariableA(name: *const u8, buffer: *mut u8, size: u32) -> u32;
139+
}
140+
let len = unsafe { GetEnvironmentVariableA(RUST_STD_DETECT_UNSTABLE.as_ptr(), core::ptr::null_mut(), 0) };
141+
if len > 0 {
142+
// +1 to include the null terminator.
143+
let mut env = vec![0; len as usize + 1];
144+
let len = unsafe { GetEnvironmentVariableA(RUST_STD_DETECT_UNSTABLE.as_ptr(), env.as_mut_ptr(), len + 1) };
145+
if len > 0 {
146+
disable_features(&env[..len as usize], &mut value);
147+
}
148+
}
149+
} else {
150+
let env = unsafe {
151+
libc::getenv(RUST_STD_DETECT_UNSTABLE.as_ptr() as *const libc::c_char)
152+
};
153+
if !env.is_null() {
154+
let len = unsafe { libc::strlen(env) };
155+
let env = unsafe { core::slice::from_raw_parts(env as *const u8, len) };
156+
disable_features(env, &mut value);
132157
}
133158
}
134159
}

0 commit comments

Comments
 (0)