Skip to content

Commit c91fe8c

Browse files
committed
refactoring: make CMakeCache.txt const plus split build and conf cmds
1 parent c87b731 commit c91fe8c

File tree

1 file changed

+51
-43
lines changed

1 file changed

+51
-43
lines changed

src/lib.rs

+51-43
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ pub fn build<P: AsRef<Path>>(path: P) -> PathBuf {
102102
Config::new(path.as_ref()).build()
103103
}
104104

105+
static CMAKE_CACHE_FILE: &str = "CMakeCache.txt";
106+
105107
impl Config {
106108
/// Return explicitly set profile or infer `CMAKE_BUILD_TYPE` from Rust's compilation profile.
107109
///
@@ -479,14 +481,14 @@ impl Config {
479481

480482
// Build up the first cmake command to build the build system.
481483
let executable = env::var("CMAKE").unwrap_or("cmake".to_owned());
482-
let mut cmd = Command::new(&executable);
484+
let mut conf_cmd = Command::new(&executable);
483485

484486
if self.verbose_cmake {
485-
cmd.arg("-Wdev");
486-
cmd.arg("--debug-output");
487+
conf_cmd.arg("-Wdev");
488+
conf_cmd.arg("--debug-output");
487489
}
488490

489-
cmd.arg(&self.path).current_dir(&build);
491+
conf_cmd.arg(&self.path).current_dir(&build);
490492
let mut is_ninja = false;
491493
if let Some(ref generator) = self.generator {
492494
is_ninja = generator.to_string_lossy().contains("Ninja");
@@ -519,15 +521,15 @@ impl Config {
519521
(false, false) => fail("no valid generator found for GNU toolchain; MSYS or MinGW must be installed")
520522
};
521523

522-
cmd.arg("-G").arg(generator);
524+
conf_cmd.arg("-G").arg(generator);
523525
}
524526
} else {
525527
// If we're cross compiling onto windows, then set some
526528
// variables which will hopefully get things to succeed. Some
527529
// systems may need the `windres` or `dlltool` variables set, so
528530
// set them if possible.
529531
if !self.defined("CMAKE_SYSTEM_NAME") {
530-
cmd.arg("-DCMAKE_SYSTEM_NAME=Windows");
532+
conf_cmd.arg("-DCMAKE_SYSTEM_NAME=Windows");
531533
}
532534
if !self.defined("CMAKE_RC_COMPILER") {
533535
let exe = find_exe(c_compiler.path());
@@ -537,7 +539,7 @@ impl Config {
537539
if windres.is_file() {
538540
let mut arg = OsString::from("-DCMAKE_RC_COMPILER=");
539541
arg.push(&windres);
540-
cmd.arg(arg);
542+
conf_cmd.arg(arg);
541543
}
542544
}
543545
}
@@ -548,30 +550,32 @@ impl Config {
548550
// This also guarantees that NMake generator isn't chosen implicitly.
549551
let using_nmake_generator;
550552
if self.generator.is_none() {
551-
cmd.arg("-G").arg(self.visual_studio_generator(&target));
553+
conf_cmd
554+
.arg("-G")
555+
.arg(self.visual_studio_generator(&target));
552556
using_nmake_generator = false;
553557
} else {
554558
using_nmake_generator = self.generator.as_ref().unwrap() == "NMake Makefiles";
555559
}
556560
if !is_ninja && !using_nmake_generator {
557561
if target.contains("x86_64") {
558-
cmd.arg("-Thost=x64");
559-
cmd.arg("-Ax64");
562+
conf_cmd.arg("-Thost=x64");
563+
conf_cmd.arg("-Ax64");
560564
} else if target.contains("thumbv7a") {
561-
cmd.arg("-Thost=x64");
562-
cmd.arg("-Aarm");
565+
conf_cmd.arg("-Thost=x64");
566+
conf_cmd.arg("-Aarm");
563567
} else if target.contains("aarch64") {
564-
cmd.arg("-Thost=x64");
565-
cmd.arg("-AARM64");
568+
conf_cmd.arg("-Thost=x64");
569+
conf_cmd.arg("-AARM64");
566570
} else if target.contains("i686") {
567571
use cc::windows_registry::{find_vs_version, VsVers};
568572
match find_vs_version() {
569573
Ok(VsVers::Vs16) => {
570574
// 32-bit x86 toolset used to be the default for all hosts,
571575
// but Visual Studio 2019 changed the default toolset to match the host,
572576
// so we need to manually override it for x86 targets
573-
cmd.arg("-Thost=x86");
574-
cmd.arg("-AWin32");
577+
conf_cmd.arg("-Thost=x86");
578+
conf_cmd.arg("-AWin32");
575579
}
576580
_ => {}
577581
};
@@ -581,35 +585,35 @@ impl Config {
581585
}
582586
} else if target.contains("redox") {
583587
if !self.defined("CMAKE_SYSTEM_NAME") {
584-
cmd.arg("-DCMAKE_SYSTEM_NAME=Generic");
588+
conf_cmd.arg("-DCMAKE_SYSTEM_NAME=Generic");
585589
}
586590
} else if target.contains("solaris") {
587591
if !self.defined("CMAKE_SYSTEM_NAME") {
588-
cmd.arg("-DCMAKE_SYSTEM_NAME=SunOS");
592+
conf_cmd.arg("-DCMAKE_SYSTEM_NAME=SunOS");
589593
}
590594
} else if target.contains("apple-ios") || target.contains("apple-tvos") {
591595
// These two flags prevent CMake from adding an OSX sysroot, which messes up compilation.
592596
if !self.defined("CMAKE_OSX_SYSROOT") && !self.defined("CMAKE_OSX_DEPLOYMENT_TARGET") {
593-
cmd.arg("-DCMAKE_OSX_SYSROOT=/");
594-
cmd.arg("-DCMAKE_OSX_DEPLOYMENT_TARGET=");
597+
conf_cmd.arg("-DCMAKE_OSX_SYSROOT=/");
598+
conf_cmd.arg("-DCMAKE_OSX_DEPLOYMENT_TARGET=");
595599
}
596600
}
597601
if let Some(ref generator) = self.generator {
598-
cmd.arg("-G").arg(generator);
602+
conf_cmd.arg("-G").arg(generator);
599603
}
600604
let profile = self.get_profile();
601605
for &(ref k, ref v) in &self.defines {
602606
let mut os = OsString::from("-D");
603607
os.push(k);
604608
os.push("=");
605609
os.push(v);
606-
cmd.arg(os);
610+
conf_cmd.arg(os);
607611
}
608612

609613
if !self.defined("CMAKE_INSTALL_PREFIX") {
610614
let mut dstflag = OsString::from("-DCMAKE_INSTALL_PREFIX=");
611615
dstflag.push(&dst);
612-
cmd.arg(dstflag);
616+
conf_cmd.arg(dstflag);
613617
}
614618

615619
let build_type = self
@@ -644,7 +648,7 @@ impl Config {
644648
flagsflag.push(" ");
645649
flagsflag.push(arg);
646650
}
647-
cmd.arg(flagsflag);
651+
conf_cmd.arg(flagsflag);
648652
}
649653

650654
// The visual studio generator apparently doesn't respect
@@ -668,7 +672,7 @@ impl Config {
668672
flagsflag.push(" ");
669673
flagsflag.push(arg);
670674
}
671-
cmd.arg(flagsflag);
675+
conf_cmd.arg(flagsflag);
672676
}
673677
}
674678

@@ -707,7 +711,7 @@ impl Config {
707711
.collect::<Vec<_>>();
708712
ccompiler = OsString::from_wide(&wchars);
709713
}
710-
cmd.arg(ccompiler);
714+
conf_cmd.arg(ccompiler);
711715
}
712716
};
713717

@@ -717,26 +721,29 @@ impl Config {
717721
}
718722

719723
if !self.defined("CMAKE_BUILD_TYPE") {
720-
cmd.arg(&format!("-DCMAKE_BUILD_TYPE={}", profile));
724+
conf_cmd.arg(&format!("-DCMAKE_BUILD_TYPE={}", profile));
721725
}
722726

723727
if self.verbose_make {
724-
cmd.arg("-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON");
728+
conf_cmd.arg("-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON");
725729
}
726730

727731
if !self.defined("CMAKE_TOOLCHAIN_FILE") {
728732
if let Ok(s) = env::var("CMAKE_TOOLCHAIN_FILE") {
729-
cmd.arg(&format!("-DCMAKE_TOOLCHAIN_FILE={}", s));
733+
conf_cmd.arg(&format!("-DCMAKE_TOOLCHAIN_FILE={}", s));
730734
}
731735
}
732736

733737
for &(ref k, ref v) in c_compiler.env().iter().chain(&self.env) {
734-
cmd.env(k, v);
738+
conf_cmd.env(k, v);
735739
}
736740

737-
if self.always_configure || !build.join("CMakeCache.txt").exists() {
738-
cmd.args(&self.configure_args);
739-
run(cmd.env("CMAKE_PREFIX_PATH", cmake_prefix_path), "cmake");
741+
if self.always_configure || !build.join(CMAKE_CACHE_FILE).exists() {
742+
conf_cmd.args(&self.configure_args);
743+
run(
744+
conf_cmd.env("CMAKE_PREFIX_PATH", cmake_prefix_path),
745+
"cmake",
746+
);
740747
} else {
741748
println!("CMake project was already configured. Skipping configuration step.");
742749
}
@@ -782,32 +789,33 @@ impl Config {
782789

783790
// And build!
784791
let target = self.cmake_target.clone().unwrap_or("install".to_string());
785-
let mut cmd = Command::new(&executable);
792+
let mut build_cmd = Command::new(&executable);
786793
for &(ref k, ref v) in c_compiler.env().iter().chain(&self.env) {
787-
cmd.env(k, v);
794+
build_cmd.env(k, v);
788795
}
789796

790797
if let Some(flags) = makeflags {
791-
cmd.env("MAKEFLAGS", flags);
798+
build_cmd.env("MAKEFLAGS", flags);
792799
}
793800

794-
cmd.arg("--build").arg(".");
801+
build_cmd.arg("--build").arg(".");
795802

796803
if !self.no_build_target {
797-
cmd.arg("--target").arg(target);
804+
build_cmd.arg("--target").arg(target);
798805
}
799806

800-
cmd.arg("--config")
807+
build_cmd
808+
.arg("--config")
801809
.arg(&profile)
802810
.arg("--")
803811
.args(&self.build_args)
804812
.current_dir(&build);
805813

806814
if let Some(flags) = parallel_flags {
807-
cmd.arg(flags);
815+
build_cmd.arg(flags);
808816
}
809817

810-
run(&mut cmd, "cmake");
818+
run(&mut build_cmd, "cmake");
811819

812820
println!("cargo:root={}", dst.display());
813821
return dst;
@@ -826,7 +834,7 @@ impl Config {
826834
doesn't know how to generate cmake files for it, \
827835
can the `cmake` crate be updated?"
828836
),
829-
Err(msg) => panic!(msg),
837+
Err(msg) => panic!("{}", msg),
830838
};
831839
if ["i686", "x86_64", "thumbv7a", "aarch64"]
832840
.iter()
@@ -855,7 +863,7 @@ impl Config {
855863
// isn't relevant to us but we canonicalize it here to ensure
856864
// we're both checking the same thing.
857865
let path = fs::canonicalize(&self.path).unwrap_or(self.path.clone());
858-
let mut f = match File::open(dir.join("CMakeCache.txt")) {
866+
let mut f = match File::open(dir.join(CMAKE_CACHE_FILE)) {
859867
Ok(f) => f,
860868
Err(..) => return,
861869
};

0 commit comments

Comments
 (0)