From a8732ec3b08d4eec20f1394f946d34a9097f3b72 Mon Sep 17 00:00:00 2001 From: ZengGengSen Date: Mon, 4 Dec 2023 22:02:23 +0800 Subject: [PATCH 1/2] fix: the cmake run command 1. Use the -S option to specify the source directory 2. Use the -B option to specify the build directory 3. If no target is specified, the build command does not know the specific target 4. Add an install instruction at the end --- src/lib.rs | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 035cead..38ef9f5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,7 +76,6 @@ pub struct Config { static_crt: Option, uses_cxx11: bool, always_configure: bool, - no_build_target: bool, verbose_cmake: bool, verbose_make: bool, pic: Option, @@ -200,7 +199,6 @@ impl Config { static_crt: None, uses_cxx11: false, always_configure: true, - no_build_target: false, verbose_cmake: false, verbose_make: false, pic: None, @@ -289,14 +287,6 @@ impl Config { self } - /// Disables the cmake target option for this compilation. - /// - /// Note that this isn't related to the target triple passed to the compiler! - pub fn no_build_target(&mut self, no_build_target: bool) -> &mut Config { - self.no_build_target = no_build_target; - self - } - /// Sets the host triple for this compilation. /// /// This is automatically scraped from `$HOST` which is set for Cargo @@ -570,7 +560,14 @@ impl Config { cmd.arg("--debug-output"); } - cmd.arg(&self.path).current_dir(&build); + // not use the current dir, should use -B and -S + // In some cases, every time the project build.rs + // is changed, cmake is reloaded without reading + // the CMakeCache.txt + cmd.current_dir(&build); + cmd.arg("-S").arg(&self.path); + cmd.arg("-B").arg(&build); + let mut is_ninja = false; if let Some(ref generator) = generator { is_ninja = generator.to_string_lossy().contains("Ninja"); @@ -847,13 +844,11 @@ impl Config { } } - cmd.arg("--build").arg("."); + // use the absolute path, not use relective path + cmd.arg("--build").arg(&build); - if !self.no_build_target { - let target = self - .cmake_target - .clone() - .unwrap_or_else(|| "install".to_string()); + // some projects may not have install targets + if let Some(target) = self.cmake_target.clone() { cmd.arg("--target").arg(target); } @@ -874,6 +869,13 @@ impl Config { run(&mut cmd, "cmake"); + // run this install command + // projects that do not have an install + // target will work just fine + let mut cmd = self.cmake_build_command(&target); + cmd.arg("--install").arg(&build); + run(&mut cmd, "cmake"); + println!("cargo:root={}", dst.display()); dst } From 5cae3302ae5fe6bdec3650c0132bcf9114973e8c Mon Sep 17 00:00:00 2001 From: ZengGengSen Date: Mon, 4 Dec 2023 22:47:33 +0800 Subject: [PATCH 2/2] fix(cmake): fix the install command error 1. The last installation instruction, in the case of mul-config, will cause an error and require the addition of the parameter --config 'build_type' 2. The penultimate build instruction used an error in --config, which may cause the profile and build_type to be unequal --- src/lib.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 38ef9f5..725afe7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -561,8 +561,8 @@ impl Config { } // not use the current dir, should use -B and -S - // In some cases, every time the project build.rs - // is changed, cmake is reloaded without reading + // In some cases, every time the project build.rs + // is changed, cmake is reloaded without reading // the CMakeCache.txt cmd.current_dir(&build); cmd.arg("-S").arg(&self.path); @@ -694,8 +694,8 @@ impl Config { .defines .iter() .find(|&&(ref a, _)| a == "CMAKE_BUILD_TYPE") - .map(|x| x.1.to_str().unwrap()) - .unwrap_or(&profile); + .map(|x| String::from(x.1.to_str().unwrap())) + .unwrap_or(profile.clone()); let build_type_upcase = build_type .chars() .flat_map(|c| c.to_uppercase()) @@ -852,7 +852,7 @@ impl Config { cmd.arg("--target").arg(target); } - cmd.arg("--config").arg(&profile); + cmd.arg("--config").arg(&build_type); // --parallel requires CMake 3.12: // https://cmake.org/cmake/help/latest/release/3.12.html#command-line @@ -874,6 +874,8 @@ impl Config { // target will work just fine let mut cmd = self.cmake_build_command(&target); cmd.arg("--install").arg(&build); + cmd.arg("--config").arg(&build_type); + run(&mut cmd, "cmake"); println!("cargo:root={}", dst.display());