Skip to content

Commit 644d672

Browse files
committed
refactoring: make CMakeCache.txt const plus split build and conf cmds
1 parent dee2a1b commit 644d672

File tree

1 file changed

+47
-39
lines changed

1 file changed

+47
-39
lines changed

src/lib.rs

+47-39
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
/// Return explicitly set profile or infer `CMAKE_BUILD_TYPE` from Rust's compilation profile.
104106
///
@@ -454,14 +456,14 @@ impl Config {
454456

455457
// Build up the first cmake command to build the build system.
456458
let executable = env::var("CMAKE").unwrap_or("cmake".to_owned());
457-
let mut cmd = Command::new(&executable);
459+
let mut conf_cmd = Command::new(&executable);
458460

459461
if self.verbose_cmake {
460-
cmd.arg("-Wdev");
461-
cmd.arg("--debug-output");
462+
conf_cmd.arg("-Wdev");
463+
conf_cmd.arg("--debug-output");
462464
}
463465

464-
cmd.arg(&self.path).current_dir(&build);
466+
conf_cmd.arg(&self.path).current_dir(&build);
465467
let mut is_ninja = false;
466468
if let Some(ref generator) = self.generator {
467469
is_ninja = generator.to_string_lossy().contains("Ninja");
@@ -494,15 +496,15 @@ impl Config {
494496
(false, false) => fail("no valid generator found for GNU toolchain; MSYS or MinGW must be installed")
495497
};
496498

497-
cmd.arg("-G").arg(generator);
499+
conf_cmd.arg("-G").arg(generator);
498500
}
499501
} else {
500502
// If we're cross compiling onto windows, then set some
501503
// variables which will hopefully get things to succeed. Some
502504
// systems may need the `windres` or `dlltool` variables set, so
503505
// set them if possible.
504506
if !self.defined("CMAKE_SYSTEM_NAME") {
505-
cmd.arg("-DCMAKE_SYSTEM_NAME=Windows");
507+
conf_cmd.arg("-DCMAKE_SYSTEM_NAME=Windows");
506508
}
507509
if !self.defined("CMAKE_RC_COMPILER") {
508510
let exe = find_exe(c_compiler.path());
@@ -512,7 +514,7 @@ impl Config {
512514
if windres.is_file() {
513515
let mut arg = OsString::from("-DCMAKE_RC_COMPILER=");
514516
arg.push(&windres);
515-
cmd.arg(arg);
517+
conf_cmd.arg(arg);
516518
}
517519
}
518520
}
@@ -523,30 +525,32 @@ impl Config {
523525
// This also guarantees that NMake generator isn't chosen implicitly.
524526
let using_nmake_generator;
525527
if self.generator.is_none() {
526-
cmd.arg("-G").arg(self.visual_studio_generator(&target));
528+
conf_cmd
529+
.arg("-G")
530+
.arg(self.visual_studio_generator(&target));
527531
using_nmake_generator = false;
528532
} else {
529533
using_nmake_generator = self.generator.as_ref().unwrap() == "NMake Makefiles";
530534
}
531535
if !is_ninja && !using_nmake_generator {
532536
if target.contains("x86_64") {
533-
cmd.arg("-Thost=x64");
534-
cmd.arg("-Ax64");
537+
conf_cmd.arg("-Thost=x64");
538+
conf_cmd.arg("-Ax64");
535539
} else if target.contains("thumbv7a") {
536-
cmd.arg("-Thost=x64");
537-
cmd.arg("-Aarm");
540+
conf_cmd.arg("-Thost=x64");
541+
conf_cmd.arg("-Aarm");
538542
} else if target.contains("aarch64") {
539-
cmd.arg("-Thost=x64");
540-
cmd.arg("-AARM64");
543+
conf_cmd.arg("-Thost=x64");
544+
conf_cmd.arg("-AARM64");
541545
} else if target.contains("i686") {
542546
use cc::windows_registry::{find_vs_version, VsVers};
543547
match find_vs_version() {
544548
Ok(VsVers::Vs16) => {
545549
// 32-bit x86 toolset used to be the default for all hosts,
546550
// but Visual Studio 2019 changed the default toolset to match the host,
547551
// so we need to manually override it for x86 targets
548-
cmd.arg("-Thost=x86");
549-
cmd.arg("-AWin32");
552+
conf_cmd.arg("-Thost=x86");
553+
conf_cmd.arg("-AWin32");
550554
}
551555
_ => {}
552556
};
@@ -556,29 +560,29 @@ impl Config {
556560
}
557561
} else if target.contains("redox") {
558562
if !self.defined("CMAKE_SYSTEM_NAME") {
559-
cmd.arg("-DCMAKE_SYSTEM_NAME=Generic");
563+
conf_cmd.arg("-DCMAKE_SYSTEM_NAME=Generic");
560564
}
561565
} else if target.contains("solaris") {
562566
if !self.defined("CMAKE_SYSTEM_NAME") {
563-
cmd.arg("-DCMAKE_SYSTEM_NAME=SunOS");
567+
conf_cmd.arg("-DCMAKE_SYSTEM_NAME=SunOS");
564568
}
565569
}
566570
if let Some(ref generator) = self.generator {
567-
cmd.arg("-G").arg(generator);
571+
conf_cmd.arg("-G").arg(generator);
568572
}
569573
let profile = self.get_profile();
570574
for &(ref k, ref v) in &self.defines {
571575
let mut os = OsString::from("-D");
572576
os.push(k);
573577
os.push("=");
574578
os.push(v);
575-
cmd.arg(os);
579+
conf_cmd.arg(os);
576580
}
577581

578582
if !self.defined("CMAKE_INSTALL_PREFIX") {
579583
let mut dstflag = OsString::from("-DCMAKE_INSTALL_PREFIX=");
580584
dstflag.push(&dst);
581-
cmd.arg(dstflag);
585+
conf_cmd.arg(dstflag);
582586
}
583587

584588
let build_type = self
@@ -613,7 +617,7 @@ impl Config {
613617
flagsflag.push(" ");
614618
flagsflag.push(arg);
615619
}
616-
cmd.arg(flagsflag);
620+
conf_cmd.arg(flagsflag);
617621
}
618622

619623
// The visual studio generator apparently doesn't respect
@@ -637,7 +641,7 @@ impl Config {
637641
flagsflag.push(" ");
638642
flagsflag.push(arg);
639643
}
640-
cmd.arg(flagsflag);
644+
conf_cmd.arg(flagsflag);
641645
}
642646
}
643647

@@ -676,7 +680,7 @@ impl Config {
676680
.collect::<Vec<_>>();
677681
ccompiler = OsString::from_wide(&wchars);
678682
}
679-
cmd.arg(ccompiler);
683+
conf_cmd.arg(ccompiler);
680684
}
681685
};
682686

@@ -686,25 +690,28 @@ impl Config {
686690
}
687691

688692
if !self.defined("CMAKE_BUILD_TYPE") {
689-
cmd.arg(&format!("-DCMAKE_BUILD_TYPE={}", profile));
693+
conf_cmd.arg(&format!("-DCMAKE_BUILD_TYPE={}", profile));
690694
}
691695

692696
if self.verbose_make {
693-
cmd.arg("-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON");
697+
conf_cmd.arg("-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON");
694698
}
695699

696700
if !self.defined("CMAKE_TOOLCHAIN_FILE") {
697701
if let Ok(s) = env::var("CMAKE_TOOLCHAIN_FILE") {
698-
cmd.arg(&format!("-DCMAKE_TOOLCHAIN_FILE={}", s));
702+
conf_cmd.arg(&format!("-DCMAKE_TOOLCHAIN_FILE={}", s));
699703
}
700704
}
701705

702706
for &(ref k, ref v) in c_compiler.env().iter().chain(&self.env) {
703-
cmd.env(k, v);
707+
conf_cmd.env(k, v);
704708
}
705709

706-
if self.always_configure || !build.join("CMakeCache.txt").exists() {
707-
run(cmd.env("CMAKE_PREFIX_PATH", cmake_prefix_path), "cmake");
710+
if self.always_configure || !build.join(CMAKE_CACHE_FILE).exists() {
711+
run(
712+
conf_cmd.env("CMAKE_PREFIX_PATH", cmake_prefix_path),
713+
"cmake",
714+
);
708715
} else {
709716
println!("CMake project was already configured. Skipping configuration step.");
710717
}
@@ -750,32 +757,33 @@ impl Config {
750757

751758
// And build!
752759
let target = self.cmake_target.clone().unwrap_or("install".to_string());
753-
let mut cmd = Command::new(&executable);
760+
let mut build_cmd = Command::new(&executable);
754761
for &(ref k, ref v) in c_compiler.env().iter().chain(&self.env) {
755-
cmd.env(k, v);
762+
build_cmd.env(k, v);
756763
}
757764

758765
if let Some(flags) = makeflags {
759-
cmd.env("MAKEFLAGS", flags);
766+
build_cmd.env("MAKEFLAGS", flags);
760767
}
761768

762-
cmd.arg("--build").arg(".");
769+
build_cmd.arg("--build").arg(".");
763770

764771
if !self.no_build_target {
765-
cmd.arg("--target").arg(target);
772+
build_cmd.arg("--target").arg(target);
766773
}
767774

768-
cmd.arg("--config")
775+
build_cmd
776+
.arg("--config")
769777
.arg(&profile)
770778
.arg("--")
771779
.args(&self.build_args)
772780
.current_dir(&build);
773781

774782
if let Some(flags) = parallel_flags {
775-
cmd.arg(flags);
783+
build_cmd.arg(flags);
776784
}
777785

778-
run(&mut cmd, "cmake");
786+
run(&mut build_cmd, "cmake");
779787

780788
println!("cargo:root={}", dst.display());
781789
return dst;
@@ -823,7 +831,7 @@ impl Config {
823831
// isn't relevant to us but we canonicalize it here to ensure
824832
// we're both checking the same thing.
825833
let path = fs::canonicalize(&self.path).unwrap_or(self.path.clone());
826-
let mut f = match File::open(dir.join("CMakeCache.txt")) {
834+
let mut f = match File::open(dir.join(CMAKE_CACHE_FILE)) {
827835
Ok(f) => f,
828836
Err(..) => return,
829837
};

0 commit comments

Comments
 (0)