Skip to content

Commit 9af8515

Browse files
authored
[#122] Refactor setting the number of parallel jobs (#123)
* [#122] only set build_args when they exist * use --parallel to set the number of jobs * forgot semi-colon * transfer CARGO_MAKEFLAGS to MAKEFLAGS if the project is Makefile based
1 parent 453b8d8 commit 9af8515

File tree

1 file changed

+29
-48
lines changed

1 file changed

+29
-48
lines changed

src/lib.rs

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -782,54 +782,34 @@ impl Config {
782782
println!("CMake project was already configured. Skipping configuration step.");
783783
}
784784

785-
let mut makeflags = None;
786-
let mut parallel_flags = None;
787-
788-
if let Ok(s) = env::var("NUM_JOBS") {
789-
match generator.as_ref().map(|g| g.to_string_lossy()) {
790-
Some(ref g) if g.contains("Ninja") => {
791-
parallel_flags = Some(format!("-j{}", s));
792-
}
793-
Some(ref g) if g.contains("Visual Studio") => {
794-
parallel_flags = Some(format!("/MP{}", s));
795-
}
796-
Some(ref g) if g.contains("NMake") => {
797-
// NMake creates `Makefile`s, but doesn't understand `-jN`.
798-
}
799-
_ if fs::metadata(&build.join("Makefile")).is_ok() => {
800-
match env::var_os("CARGO_MAKEFLAGS") {
801-
// Only do this on non-windows and non-bsd
802-
// On Windows, we could be invoking make instead of
803-
// mingw32-make which doesn't work with our jobserver
804-
// bsdmake also does not work with our job server
805-
Some(ref s)
806-
if !(cfg!(windows)
807-
|| cfg!(target_os = "openbsd")
808-
|| cfg!(target_os = "netbsd")
809-
|| cfg!(target_os = "freebsd")
810-
|| cfg!(target_os = "bitrig")
811-
|| cfg!(target_os = "dragonflybsd")) =>
812-
{
813-
makeflags = Some(s.clone())
814-
}
815-
816-
// This looks like `make`, let's hope it understands `-jN`.
817-
_ => makeflags = Some(OsString::from(format!("-j{}", s))),
818-
}
819-
}
820-
_ => {}
821-
}
822-
}
823-
824785
// And build!
825786
let target = self.cmake_target.clone().unwrap_or("install".to_string());
826787
let mut cmd = Command::new(&executable);
788+
cmd.current_dir(&build);
789+
827790
for &(ref k, ref v) in c_compiler.env().iter().chain(&self.env) {
828791
cmd.env(k, v);
829792
}
830793

831-
if let Some(flags) = makeflags {
832-
cmd.env("MAKEFLAGS", flags);
794+
// If the generated project is Makefile based we should carefully transfer corresponding CARGO_MAKEFLAGS
795+
if fs::metadata(&build.join("Makefile")).is_ok() {
796+
match env::var_os("CARGO_MAKEFLAGS") {
797+
// Only do this on non-windows and non-bsd
798+
// On Windows, we could be invoking make instead of
799+
// mingw32-make which doesn't work with our jobserver
800+
// bsdmake also does not work with our job server
801+
Some(ref makeflags)
802+
if !(cfg!(windows)
803+
|| cfg!(target_os = "openbsd")
804+
|| cfg!(target_os = "netbsd")
805+
|| cfg!(target_os = "freebsd")
806+
|| cfg!(target_os = "bitrig")
807+
|| cfg!(target_os = "dragonflybsd")) =>
808+
{
809+
cmd.env("MAKEFLAGS", makeflags);
810+
}
811+
_ => {}
812+
}
833813
}
834814

835815
cmd.arg("--build").arg(".");
@@ -838,14 +818,15 @@ impl Config {
838818
cmd.arg("--target").arg(target);
839819
}
840820

841-
cmd.arg("--config")
842-
.arg(&profile)
843-
.arg("--")
844-
.args(&self.build_args)
845-
.current_dir(&build);
821+
cmd.arg("--config").arg(&profile);
822+
823+
if let Ok(s) = env::var("NUM_JOBS") {
824+
// See https://cmake.org/cmake/help/v3.12/manual/cmake.1.html#build-tool-mode
825+
cmd.arg("--parallel").arg(s);
826+
}
846827

847-
if let Some(flags) = parallel_flags {
848-
cmd.arg(flags);
828+
if !&self.build_args.is_empty() {
829+
cmd.arg("--").args(&self.build_args);
849830
}
850831

851832
run(&mut cmd, "cmake");

0 commit comments

Comments
 (0)