summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLucas CHOLLET <lucas.chollet@free.fr>2022-12-06 18:17:41 +0100
committerLinus Groh <mail@linusgroh.de>2022-12-14 16:43:44 +0000
commit0fde7fe3c5f1f4c8732a54eafe6ddfab6e7b54a8 (patch)
treebf9bba44696e7cb5c666c9ff01cab2ae9ecaded2
parentb409d3cf88964ddac599d7573b982e7a4bb25fb3 (diff)
downloadserenity-0fde7fe3c5f1f4c8732a54eafe6ddfab6e7b54a8.zip
Meta: Factorize path resolution
This patch adds the `find_executable()` function that will hopefully find executables in a distro-agnostic way and that is (hopefully as well) easily upgradable. The function uses some bash functionalities. So, we now require bash for each script that includes `.shell_include.sh`.
-rwxr-xr-xMeta/.shell_include.sh31
-rwxr-xr-xMeta/build-image-extlinux.sh2
-rwxr-xr-xMeta/build-image-grub.sh2
-rwxr-xr-xMeta/build-image-limine.sh2
-rwxr-xr-xMeta/build-image-qemu.sh35
-rwxr-xr-xMeta/build-native-partition.sh2
6 files changed, 35 insertions, 39 deletions
diff --git a/Meta/.shell_include.sh b/Meta/.shell_include.sh
index 9e82d6557b..0672595a38 100755
--- a/Meta/.shell_include.sh
+++ b/Meta/.shell_include.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/usr/bin/env bash
# shellcheck disable=SC2034
# SC2034: "Variable appears unused. Verify it or export it."
# Those are intentional here, as the file is meant to be included elsewhere.
@@ -16,3 +16,32 @@ die() {
echo "die: $*"
exit 1
}
+
+find_executable() {
+ paths=("/usr/sbin" "/sbin")
+
+ if [ "$(uname -s)" = "Darwin" ]; then
+ paths+=("/usr/local/opt/e2fsprogs/bin" "/usr/local/opt/e2fsprogs/sbin")
+ paths+=("/opt/homebrew/opt/e2fsprogs/bin" "/opt/homebrew/opt/e2fsprogs/sbin")
+ fi
+
+ executable="${1}"
+
+ # Prefer tools from PATH over fallback paths
+ if command -v "${executable}"; then
+ return 0
+ fi
+
+ for path in "${paths[@]}"; do
+ if command -v "${path}/${executable}"; then
+ return 0
+ fi
+ done
+
+ # We return the executable's name back to provide meaningful messages on future failure
+ echo "${executable}"
+}
+
+FUSE2FS_PATH="$(find_executable fuse2fs)"
+RESIZE2FS_PATH="$(find_executable resize2fs)"
+E2FSCK="$(find_executable e2fsck)"
diff --git a/Meta/build-image-extlinux.sh b/Meta/build-image-extlinux.sh
index a755780950..9b4dcae772 100755
--- a/Meta/build-image-extlinux.sh
+++ b/Meta/build-image-extlinux.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/usr/bin/env bash
set -e
diff --git a/Meta/build-image-grub.sh b/Meta/build-image-grub.sh
index e909247162..71ef767001 100755
--- a/Meta/build-image-grub.sh
+++ b/Meta/build-image-grub.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/usr/bin/env bash
set -e
diff --git a/Meta/build-image-limine.sh b/Meta/build-image-limine.sh
index f3bd996c86..06ccd4d8f8 100755
--- a/Meta/build-image-limine.sh
+++ b/Meta/build-image-limine.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/usr/bin/env bash
set -e
diff --git a/Meta/build-image-qemu.sh b/Meta/build-image-qemu.sh
index 58f061ccaf..e021ce3f6a 100755
--- a/Meta/build-image-qemu.sh
+++ b/Meta/build-image-qemu.sh
@@ -1,17 +1,4 @@
-#!/bin/sh
-
-# Note: This is done before `set -e` to let `command` fail if needed
-FUSE2FS_PATH=$(command -v fuse2fs)
-RESIZE2FS_PATH=$(command -v resize2fs)
-
-if [ -z "$FUSE2FS_PATH" ]; then
- FUSE2FS_PATH=/usr/sbin/fuse2fs
-fi
-
-if [ -z "$RESIZE2FS_PATH" ]; then
- RESIZE2FS_PATH=/usr/sbin/resize2fs
-fi
-
+#!/usr/bin/env bash
set -e
SCRIPT_DIR="$(dirname "${0}")"
@@ -42,26 +29,6 @@ else
: "${SUDO_UID:=0}" "${SUDO_GID:=0}"
fi
-if [ "$(uname -s)" = "Darwin" ]; then
- export PATH="/usr/local/opt/e2fsprogs/bin:$PATH"
- export PATH="/usr/local/opt/e2fsprogs/sbin:$PATH"
- export PATH="/opt/homebrew/opt/e2fsprogs/bin:$PATH"
- export PATH="/opt/homebrew/opt/e2fsprogs/sbin:$PATH"
-
- E2FSCK="e2fsck"
- RESIZE2FS_PATH="resize2fs"
-elif [ ! -f "$E2FSCK" ]; then
- E2FSCK="$(command -v e2fsck)"
-
- if [ ! -f "$E2FSCK" ]; then
- E2FSCK="/usr/sbin/e2fsck"
- if [ ! -f "$E2FSCK" ]; then
- E2FSCK="/sbin/e2fsck"
- fi
- fi
-fi
-
-
# Prepend the toolchain qemu directory so we pick up QEMU from there
PATH="$SCRIPT_DIR/../Toolchain/Local/qemu/bin:$PATH"
diff --git a/Meta/build-native-partition.sh b/Meta/build-native-partition.sh
index 2dedb86ddd..34237f59a3 100755
--- a/Meta/build-native-partition.sh
+++ b/Meta/build-native-partition.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/usr/bin/env bash
set -e