Skip to content

Commit 0aa1090

Browse files
committed
Fix jobserver: Last --jobserver-auth wins
`--jobserver-auth=` is the only documented makeflags. `--jobserver-fds=` is actually an internal only makeflags, so we should always prefer `--jobserver-auth=`. Also, according to doc of makeflags, if there are multiple `--jobserver-auth=` the last one is used ref: rust-lang/jobserver-rs#67 Signed-off-by: Jiahao XU <[email protected]>
1 parent 0042c70 commit 0aa1090

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

src/parallel/job_token/mod.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,33 @@ mod inherited_jobserver {
8888
.or_else(|| var_os("MAKEFLAGS"))
8989
.or_else(|| var_os("MFLAGS"))?;
9090

91-
let inner = sys::JobServerClient::open(var)?;
91+
#[cfg(unix)]
92+
let var = std::os::unix::ffi::OsStrExt::as_bytes(var.as_os_str());
93+
#[cfg(not(unix))]
94+
let var = var.to_str()?.as_bytes();
9295

93-
Some(Self {
96+
let makeflags = var.split(u8::is_ascii_whitespace);
97+
98+
// `--jobserver-auth=` is the only documented makeflags.
99+
// `--jobserver-fds=` is actually an internal only makeflags, so we should
100+
// always prefer `--jobserver-auth=`.
101+
//
102+
// Also, according to doc of makeflags, if there are multiple `--jobserver-auth=`
103+
// the last one is used
104+
if let Some(flag) = makeflags
105+
.clone()
106+
.filter_map(|s| s.strip_prefix(b"--jobserver-auth="))
107+
.last()
108+
{
109+
sys::JobServerClient::open(flag)
110+
} else {
111+
sys::JobServerClient::open(
112+
makeflags
113+
.filter_map(|s| s.strip_prefix(b"--jobserver-fds="))
114+
.last()?,
115+
)
116+
}
117+
.map(|inner| Self {
94118
inner,
95119
global_implicit_token: AtomicBool::new(true),
96120
})

src/parallel/job_token/unix.rs

+5-18
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
use std::{
2-
ffi::{OsStr, OsString},
2+
ffi::OsStr,
33
fs::{self, File},
44
io::{self, Read, Write},
55
mem::ManuallyDrop,
66
os::{
77
raw::c_int,
8-
unix::{
9-
ffi::{OsStrExt, OsStringExt},
10-
prelude::*,
11-
},
8+
unix::{ffi::OsStrExt, prelude::*},
129
},
1310
path::Path,
1411
};
@@ -19,21 +16,11 @@ pub(super) struct JobServerClient {
1916
}
2017

2118
impl JobServerClient {
22-
pub(super) unsafe fn open(var: OsString) -> Option<Self> {
23-
let bytes = var.into_vec();
24-
25-
let s = bytes
26-
.split(u8::is_ascii_whitespace)
27-
.filter_map(|arg| {
28-
arg.strip_prefix(b"--jobserver-fds=")
29-
.or_else(|| arg.strip_prefix(b"--jobserver-auth="))
30-
})
31-
.find(|bytes| !bytes.is_empty())?;
32-
33-
if let Some(fifo) = s.strip_prefix(b"fifo:") {
19+
pub(super) unsafe fn open(var: &[u8]) -> Option<Self> {
20+
if let Some(fifo) = var.strip_prefix(b"fifo:") {
3421
Self::from_fifo(Path::new(OsStr::from_bytes(fifo)))
3522
} else {
36-
Self::from_pipe(OsStr::from_bytes(s).to_str()?)
23+
Self::from_pipe(OsStr::from_bytes(var).to_str()?)
3724
}
3825
}
3926

src/parallel/job_token/windows.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::{
2-
ffi::{CString, OsString},
3-
io, ptr,
4-
};
1+
use std::{ffi::CString, io, ptr, str};
52

63
use crate::windows::windows_sys::{
74
OpenSemaphoreA, ReleaseSemaphore, WaitForSingleObject, FALSE, HANDLE, SEMAPHORE_MODIFY_STATE,
@@ -16,8 +13,8 @@ unsafe impl Sync for JobServerClient {}
1613
unsafe impl Send for JobServerClient {}
1714

1815
impl JobServerClient {
19-
pub(super) unsafe fn open(var: OsString) -> Option<Self> {
20-
let var = var.to_str()?;
16+
pub(super) unsafe fn open(var: &[u8]) -> Option<Self> {
17+
let var = str::from_utf8(var).ok()?;
2118
if !var.is_ascii() {
2219
// `OpenSemaphoreA` only accepts ASCII, not utf-8.
2320
//
@@ -29,12 +26,7 @@ impl JobServerClient {
2926
return None;
3027
}
3128

32-
let s = var
33-
.split_ascii_whitespace()
34-
.filter_map(|arg| arg.strip_prefix("--jobserver-auth="))
35-
.find(|s| !s.is_empty())?;
36-
37-
let name = CString::new(s).ok()?;
29+
let name = CString::new(var).ok()?;
3830

3931
let sem = OpenSemaphoreA(
4032
THREAD_SYNCHRONIZE | SEMAPHORE_MODIFY_STATE,

0 commit comments

Comments
 (0)