Skip to content

Commit ff30436

Browse files
committed
refactoring: make CMakeCache.txt const plus split build and conf cmds
1 parent 3d60fcb commit ff30436

File tree

1 file changed

+47
-39
lines changed

1 file changed

+47
-39
lines changed

src/lib.rs

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ pub fn build<P: AsRef<Path>>(path: P) -> PathBuf {
9999
Config::new(path.as_ref()).build()
100100
}
101101

102+
static CMAKE_CACHE_FILE: &str = "CMakeCache.txt";
103+
102104
impl Config {
103105
/// Creates a new blank set of configuration to build the project specified
104106
/// at the path `path`.
@@ -383,14 +385,14 @@ impl Config {
383385

384386
// Build up the first cmake command to build the build system.
385387
let executable = env::var("CMAKE").unwrap_or("cmake".to_owned());
386-
let mut cmd = Command::new(&executable);
388+
let mut conf_cmd = Command::new(&executable);
387389

388390
if self.verbose_cmake {
389-
cmd.arg("-Wdev");
390-
cmd.arg("--debug-output");
391+
conf_cmd.arg("-Wdev");
392+
conf_cmd.arg("--debug-output");
391393
}
392394

393-
cmd.arg(&self.path).current_dir(&build);
395+
conf_cmd.arg(&self.path).current_dir(&build);
394396
let mut is_ninja = false;
395397
if let Some(ref generator) = self.generator {
396398
is_ninja = generator.to_string_lossy().contains("Ninja");
@@ -423,15 +425,15 @@ impl Config {
423425
(false, false) => fail("no valid generator found for GNU toolchain; MSYS or MinGW must be installed")
424426
};
425427

426-
cmd.arg("-G").arg(generator);
428+
conf_cmd.arg("-G").arg(generator);
427429
}
428430
} else {
429431
// If we're cross compiling onto windows, then set some
430432
// variables which will hopefully get things to succeed. Some
431433
// systems may need the `windres` or `dlltool` variables set, so
432434
// set them if possible.
433435
if !self.defined("CMAKE_SYSTEM_NAME") {
434-
cmd.arg("-DCMAKE_SYSTEM_NAME=Windows");
436+
conf_cmd.arg("-DCMAKE_SYSTEM_NAME=Windows");
435437
}
436438
if !self.defined("CMAKE_RC_COMPILER") {
437439
let exe = find_exe(c_compiler.path());
@@ -441,7 +443,7 @@ impl Config {
441443
if windres.is_file() {
442444
let mut arg = OsString::from("-DCMAKE_RC_COMPILER=");
443445
arg.push(&windres);
444-
cmd.arg(arg);
446+
conf_cmd.arg(arg);
445447
}
446448
}
447449
}
@@ -452,30 +454,32 @@ impl Config {
452454
// This also guarantees that NMake generator isn't chosen implicitly.
453455
let using_nmake_generator;
454456
if self.generator.is_none() {
455-
cmd.arg("-G").arg(self.visual_studio_generator(&target));
457+
conf_cmd
458+
.arg("-G")
459+
.arg(self.visual_studio_generator(&target));
456460
using_nmake_generator = false;
457461
} else {
458462
using_nmake_generator = self.generator.as_ref().unwrap() == "NMake Makefiles";
459463
}
460464
if !is_ninja && !using_nmake_generator {
461465
if target.contains("x86_64") {
462-
cmd.arg("-Thost=x64");
463-
cmd.arg("-Ax64");
466+
conf_cmd.arg("-Thost=x64");
467+
conf_cmd.arg("-Ax64");
464468
} else if target.contains("thumbv7a") {
465-
cmd.arg("-Thost=x64");
466-
cmd.arg("-Aarm");
469+
conf_cmd.arg("-Thost=x64");
470+
conf_cmd.arg("-Aarm");
467471
} else if target.contains("aarch64") {
468-
cmd.arg("-Thost=x64");
469-
cmd.arg("-AARM64");
472+
conf_cmd.arg("-Thost=x64");
473+
conf_cmd.arg("-AARM64");
470474
} else if target.contains("i686") {
471475
use cc::windows_registry::{find_vs_version, VsVers};
472476
match find_vs_version() {
473477
Ok(VsVers::Vs16) => {
474478
// 32-bit x86 toolset used to be the default for all hosts,
475479
// but Visual Studio 2019 changed the default toolset to match the host,
476480
// so we need to manually override it for x86 targets
477-
cmd.arg("-Thost=x86");
478-
cmd.arg("-AWin32");
481+
conf_cmd.arg("-Thost=x86");
482+
conf_cmd.arg("-AWin32");
479483
}
480484
_ => {}
481485
};
@@ -485,15 +489,15 @@ impl Config {
485489
}
486490
} else if target.contains("redox") {
487491
if !self.defined("CMAKE_SYSTEM_NAME") {
488-
cmd.arg("-DCMAKE_SYSTEM_NAME=Generic");
492+
conf_cmd.arg("-DCMAKE_SYSTEM_NAME=Generic");
489493
}
490494
} else if target.contains("solaris") {
491495
if !self.defined("CMAKE_SYSTEM_NAME") {
492-
cmd.arg("-DCMAKE_SYSTEM_NAME=SunOS");
496+
conf_cmd.arg("-DCMAKE_SYSTEM_NAME=SunOS");
493497
}
494498
}
495499
if let Some(ref generator) = self.generator {
496-
cmd.arg("-G").arg(generator);
500+
conf_cmd.arg("-G").arg(generator);
497501
}
498502
let profile = self.profile.clone().unwrap_or_else(|| {
499503
// Automatically set the `CMAKE_BUILD_TYPE` if the user did not
@@ -563,13 +567,13 @@ impl Config {
563567
os.push(k);
564568
os.push("=");
565569
os.push(v);
566-
cmd.arg(os);
570+
conf_cmd.arg(os);
567571
}
568572

569573
if !self.defined("CMAKE_INSTALL_PREFIX") {
570574
let mut dstflag = OsString::from("-DCMAKE_INSTALL_PREFIX=");
571575
dstflag.push(&dst);
572-
cmd.arg(dstflag);
576+
conf_cmd.arg(dstflag);
573577
}
574578

575579
let build_type = self
@@ -604,7 +608,7 @@ impl Config {
604608
flagsflag.push(" ");
605609
flagsflag.push(arg);
606610
}
607-
cmd.arg(flagsflag);
611+
conf_cmd.arg(flagsflag);
608612
}
609613

610614
// The visual studio generator apparently doesn't respect
@@ -628,7 +632,7 @@ impl Config {
628632
flagsflag.push(" ");
629633
flagsflag.push(arg);
630634
}
631-
cmd.arg(flagsflag);
635+
conf_cmd.arg(flagsflag);
632636
}
633637
}
634638

@@ -667,7 +671,7 @@ impl Config {
667671
.collect::<Vec<_>>();
668672
ccompiler = OsString::from_wide(&wchars);
669673
}
670-
cmd.arg(ccompiler);
674+
conf_cmd.arg(ccompiler);
671675
}
672676
};
673677

@@ -677,25 +681,28 @@ impl Config {
677681
}
678682

679683
if !self.defined("CMAKE_BUILD_TYPE") {
680-
cmd.arg(&format!("-DCMAKE_BUILD_TYPE={}", profile));
684+
conf_cmd.arg(&format!("-DCMAKE_BUILD_TYPE={}", profile));
681685
}
682686

683687
if self.verbose_make {
684-
cmd.arg("-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON");
688+
conf_cmd.arg("-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON");
685689
}
686690

687691
if !self.defined("CMAKE_TOOLCHAIN_FILE") {
688692
if let Ok(s) = env::var("CMAKE_TOOLCHAIN_FILE") {
689-
cmd.arg(&format!("-DCMAKE_TOOLCHAIN_FILE={}", s));
693+
conf_cmd.arg(&format!("-DCMAKE_TOOLCHAIN_FILE={}", s));
690694
}
691695
}
692696

693697
for &(ref k, ref v) in c_compiler.env().iter().chain(&self.env) {
694-
cmd.env(k, v);
698+
conf_cmd.env(k, v);
695699
}
696700

697-
if self.always_configure || !build.join("CMakeCache.txt").exists() {
698-
run(cmd.env("CMAKE_PREFIX_PATH", cmake_prefix_path), "cmake");
701+
if self.always_configure || !build.join(CMAKE_CACHE_FILE).exists() {
702+
run(
703+
conf_cmd.env("CMAKE_PREFIX_PATH", cmake_prefix_path),
704+
"cmake",
705+
);
699706
} else {
700707
println!("CMake project was already configured. Skipping configuration step.");
701708
}
@@ -741,32 +748,33 @@ impl Config {
741748

742749
// And build!
743750
let target = self.cmake_target.clone().unwrap_or("install".to_string());
744-
let mut cmd = Command::new(&executable);
751+
let mut build_cmd = Command::new(&executable);
745752
for &(ref k, ref v) in c_compiler.env().iter().chain(&self.env) {
746-
cmd.env(k, v);
753+
build_cmd.env(k, v);
747754
}
748755

749756
if let Some(flags) = makeflags {
750-
cmd.env("MAKEFLAGS", flags);
757+
build_cmd.env("MAKEFLAGS", flags);
751758
}
752759

753-
cmd.arg("--build").arg(".");
760+
build_cmd.arg("--build").arg(".");
754761

755762
if !self.no_build_target {
756-
cmd.arg("--target").arg(target);
763+
build_cmd.arg("--target").arg(target);
757764
}
758765

759-
cmd.arg("--config")
766+
build_cmd
767+
.arg("--config")
760768
.arg(&profile)
761769
.arg("--")
762770
.args(&self.build_args)
763771
.current_dir(&build);
764772

765773
if let Some(flags) = parallel_flags {
766-
cmd.arg(flags);
774+
build_cmd.arg(flags);
767775
}
768776

769-
run(&mut cmd, "cmake");
777+
run(&mut build_cmd, "cmake");
770778

771779
println!("cargo:root={}", dst.display());
772780
return dst;
@@ -814,7 +822,7 @@ impl Config {
814822
// isn't relevant to us but we canonicalize it here to ensure
815823
// we're both checking the same thing.
816824
let path = fs::canonicalize(&self.path).unwrap_or(self.path.clone());
817-
let mut f = match File::open(dir.join("CMakeCache.txt")) {
825+
let mut f = match File::open(dir.join(CMAKE_CACHE_FILE)) {
818826
Ok(f) => f,
819827
Err(..) => return,
820828
};

0 commit comments

Comments
 (0)