Skip to content

Commit

Permalink
Auto merge of #10511 - weihanglo:issue-10477, r=ehuss
Browse files Browse the repository at this point in the history
Support inheriting jobserver fd for external subcommands
  • Loading branch information
bors committed Sep 2, 2022
2 parents 04a4081 + c1b90b9 commit 646e9a0
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 44 deletions.
11 changes: 6 additions & 5 deletions src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,12 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> Cli
};

let cargo_exe = config.cargo_exe()?;
let err = match ProcessBuilder::new(&command)
.env(cargo::CARGO_ENV, cargo_exe)
.args(args)
.exec_replace()
{
let mut cmd = ProcessBuilder::new(&command);
cmd.env(cargo::CARGO_ENV, cargo_exe).args(args);
if let Some(client) = config.jobserver_from_env() {
cmd.inherit_jobserver(client);
}
let err = match cmd.exec_replace() {
Ok(()) => return Ok(()),
Err(e) => e,
};
Expand Down
117 changes: 78 additions & 39 deletions tests/testsuite/jobserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,50 @@ use std::net::TcpListener;
use std::process::Command;
use std::thread;

use cargo_test_support::install::{assert_has_installed_exe, cargo_home};
use cargo_test_support::{cargo_exe, project};

#[cargo_test]
fn jobserver_exists() {
let p = project()
.file(
"build.rs",
r#"
use std::env;
const EXE_CONTENT: &str = r#"
use std::env;
fn main() {
let var = env::var("CARGO_MAKEFLAGS").unwrap();
let arg = var.split(' ')
.find(|p| p.starts_with("--jobserver"))
.unwrap();
let val = &arg[arg.find('=').unwrap() + 1..];
validate(val);
}
fn main() {
let var = env::var("CARGO_MAKEFLAGS").unwrap();
let arg = var.split(' ')
.find(|p| p.starts_with("--jobserver"))
.unwrap();
let val = &arg[arg.find('=').unwrap() + 1..];
validate(val);
}
#[cfg(unix)]
fn validate(s: &str) {
use std::fs::File;
use std::io::*;
use std::os::unix::prelude::*;
let fds = s.split(',').collect::<Vec<_>>();
println!("{}", s);
assert_eq!(fds.len(), 2);
unsafe {
let mut read = File::from_raw_fd(fds[0].parse().unwrap());
let mut write = File::from_raw_fd(fds[1].parse().unwrap());
let mut buf = [0];
assert_eq!(read.read(&mut buf).unwrap(), 1);
assert_eq!(write.write(&buf).unwrap(), 1);
}
}
#[cfg(unix)]
fn validate(s: &str) {
use std::fs::File;
use std::io::*;
use std::os::unix::prelude::*;
let fds = s.split(',').collect::<Vec<_>>();
println!("{}", s);
assert_eq!(fds.len(), 2);
unsafe {
let mut read = File::from_raw_fd(fds[0].parse().unwrap());
let mut write = File::from_raw_fd(fds[1].parse().unwrap());
let mut buf = [0];
assert_eq!(read.read(&mut buf).unwrap(), 1);
assert_eq!(write.write(&buf).unwrap(), 1);
}
}
#[cfg(windows)]
fn validate(_: &str) {
// a little too complicated for a test...
}
"#,
)
#[cfg(windows)]
fn validate(_: &str) {
// a little too complicated for a test...
}
"#;

#[cargo_test]
fn jobserver_exists() {
let p = project()
.file("build.rs", EXE_CONTENT)
.file("src/lib.rs", "")
.build();

Expand All @@ -58,6 +58,45 @@ fn jobserver_exists() {
p.cargo("build -j2").run();
}

#[cargo_test]
fn external_subcommand_inherits_jobserver() {
let make = if cfg!(windows) {
"mingw32-make"
} else {
"make"
};
if Command::new(make).arg("--version").output().is_err() {
return;
}

let name = "cargo-jobserver-check";
let p = project()
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "{name}"
version = "0.0.1"
"#
),
)
.file("src/main.rs", EXE_CONTENT)
.file(
"Makefile",
"\
all:
\t+$(CARGO) jobserver-check
",
)
.build();

p.cargo("install --path .").run();
assert_has_installed_exe(cargo_home(), name);

p.process(make).env("CARGO", cargo_exe()).arg("-j2").run();
}

#[cargo_test]
fn makes_jobserver_used() {
let make = if cfg!(windows) {
Expand Down

0 comments on commit 646e9a0

Please sign in to comment.