diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2023-12-22 00:47:01 +0100 |
|---|---|---|
| committer | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2023-12-23 00:13:09 +0100 |
| commit | ec940748175eca4e476ed29fa537319eb090356a (patch) | |
| tree | aa9e84872259b3f07f5bfae8312f2493676ef803 | |
| parent | 2e52b08800d5213e6cc7d75559f62584e8e0eede (diff) | |
| download | rust-ec940748175eca4e476ed29fa537319eb090356a.tar.gz rust-ec940748175eca4e476ed29fa537319eb090356a.zip | |
Correctly take into account potential position of cargo command in `y.sh`
| -rw-r--r-- | build_system/src/cargo.rs | 26 | ||||
| -rwxr-xr-x | y.sh | 6 |
2 files changed, 23 insertions, 9 deletions
diff --git a/build_system/src/cargo.rs b/build_system/src/cargo.rs index 06b543a6cad..5f9de5e2eb6 100644 --- a/build_system/src/cargo.rs +++ b/build_system/src/cargo.rs @@ -6,6 +6,7 @@ use crate::utils::{ use std::collections::HashMap; use std::ffi::OsStr; +use std::path::PathBuf; fn args() -> Result<Option<Vec<String>>, String> { // We skip the binary and the "cargo" option. @@ -42,18 +43,31 @@ pub fn run() -> Result<(), String> { // We first need to go to the original location to ensure that the config setup will go as // expected. let current_dir = std::env::current_dir() + .and_then(|path| path.canonicalize()) .map_err(|error| format!("Failed to get current directory path: {:?}", error))?; let current_exe = std::env::current_exe() + .and_then(|path| path.canonicalize()) .map_err(|error| format!("Failed to get current exe path: {:?}", error))?; - let parent_dir = match current_exe.parent() { - Some(parent) => parent, - None => { + let mut parent_dir = current_exe + .components() + .map(|comp| comp.as_os_str()) + .collect::<Vec<_>>(); + // We run this script from "build_system/target/release/y", so we need to remove these elements. + for to_remove in &["y", "release", "target", "build_system"] { + if parent_dir + .last() + .map(|part| part == to_remove) + .unwrap_or(false) + { + parent_dir.pop(); + } else { return Err(format!( - "Cannot get parent of current executable path `{}`", - current_exe.display() + "Build script not executed from `build_system/target/release/y` (in path {})", + current_exe.display(), )); } - }; + } + let parent_dir = PathBuf::from(parent_dir.join(&OsStr::new("/"))); std::env::set_current_dir(&parent_dir).map_err(|error| { format!( "Failed to go to `{}` folder: {:?}", diff --git a/y.sh b/y.sh index 188109743e3..69d7917dd77 100755 --- a/y.sh +++ b/y.sh @@ -2,7 +2,7 @@ set -e echo "[BUILD] build system" 1>&2 -cd build_system +pushd $(dirname "$0")/build_system > /dev/null cargo build --release -cd .. -./build_system/target/release/y $@ +popd > /dev/null +$(dirname "$0")/build_system/target/release/y $@ |
