about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2023-12-22 00:47:01 +0100
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2023-12-23 00:13:09 +0100
commitec940748175eca4e476ed29fa537319eb090356a (patch)
treeaa9e84872259b3f07f5bfae8312f2493676ef803
parent2e52b08800d5213e6cc7d75559f62584e8e0eede (diff)
downloadrust-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.rs26
-rwxr-xr-xy.sh6
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 $@