Skip to content

Commit

Permalink
fix: the cmake run command
Browse files Browse the repository at this point in the history
    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
  • Loading branch information
ZengGengSen committed Dec 4, 2023
1 parent c4a60dd commit a8732ec
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ pub struct Config {
static_crt: Option<bool>,
uses_cxx11: bool,
always_configure: bool,
no_build_target: bool,
verbose_cmake: bool,
verbose_make: bool,
pic: Option<bool>,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}

Expand All @@ -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
}
Expand Down

0 comments on commit a8732ec

Please sign in to comment.