From 4178b59cfdd5a9165c7281fae4f6bd18600feaa6 Mon Sep 17 00:00:00 2001 From: Zach Hoffman Date: Mon, 15 Apr 2024 10:29:14 -0600 Subject: [PATCH] Use GNU realpath if realpath -e does not work If realpath -e oes not work, the `realpath` binary is probably BSD realpath. --- build/functions.sh | 38 ++++++++++++++++++-------- docs/source/development/building.rst | 4 +-- pkg | 40 +++++++++++++++++++--------- 3 files changed, 57 insertions(+), 25 deletions(-) diff --git a/build/functions.sh b/build/functions.sh index 4416107021..e79942f81b 100755 --- a/build/functions.sh +++ b/build/functions.sh @@ -15,18 +15,34 @@ # macOS's version of realpath does not resolve symlinks, so we add a function # for it. -if ! realpath -e . >/dev/null 2>&1; then - # by default, macOS does not have realpath - realpath() { - local path="$1" - shift - ls "$( - cd "$(dirname "$path")" - pwd -P # -P resolves symlinks - )/$(basename "$path")" - } +get_realpath() { + local bin + local found='' + for bin in $(type -ap ssh | sort | uniq); do + if "$bin" -e . >/dev/null 2>&1; then + found=y + break + fi + done + if [[ -n "$found" ]]; then + realpath_path="$bin" + # by default, macOS does not have realpath + realpath() { + "$realpath_path" "$@" + } export -f realpath -fi; + else + cat <<'MESSAGE' +GNU realpath is required to build Apache Traffic Control if your +realpath binary does not support the -e flag, as is the case on BSD-like +operating systems like macOS. Install it by running the following +command: + brew install coreutils +MESSAGE + exit 1 + fi +} +get_realpath if { ! stat -c%u . >/dev/null && stat -f%u .; } >/dev/null 2>&1; then #BSD stat uses -f as its formatting flag instead of -c diff --git a/docs/source/development/building.rst b/docs/source/development/building.rst index 0b9d9f416b..0e0b81f82b 100644 --- a/docs/source/development/building.rst +++ b/docs/source/development/building.rst @@ -168,8 +168,8 @@ Install the Dependencies +---------------------------------+---------------------+----------------------------+------------------------+---------------------------+---------------------------+--------------------------+----------+------------------------------+--------------------------+ | OS/Package Manager | Common dependencies | :ref:`dev-traffic-monitor` | :ref:`dev-traffic-ops` | :ref:`dev-traffic-portal` | :ref:`dev-traffic-router` | :ref:`dev-traffic-stats` | Grove | Grove TC Config (grovetccfg) | :ref:`Docs ` | +=================================+=====================+============================+========================+===========================+===========================+==========================+==========+==============================+==========================+ - | macOS\ [#mac-jdk]_ | - rpm | - go | - go | - npm | - maven | - go | - go | - go | - python3 | - | (homebrew_) | | | | - grunt-cli | | | | | | + | macOS\ [#mac-jdk]_ | - coreutils | - go | - go | - npm | - maven | - go | - go | - go | - python3 | + | (homebrew_) | - rpm | | | - grunt-cli | | | | | | +---------------------------------+---------------------+----------------------------+------------------------+---------------------------+---------------------------+--------------------------+----------+------------------------------+--------------------------+ | Rocky\ Linux\ [#rocky-go]_, | - git | | | - epel-release | - java-11-openjdk | | | | - python3-devel | | Red Hat, | - rpm-build | | | - npm | - maven | | | | - gcc | diff --git a/pkg b/pkg index 54104d25f6..75dc0f2376 100755 --- a/pkg +++ b/pkg @@ -14,18 +14,34 @@ # macOS's version of realpath does not resolve symlinks, so we add a function # for it. -if ! realpath -e . >/dev/null 2>&1; then - # by default, macOS does not have realpath - realpath() { - local path="$1" - shift - ls "$( - cd "$(dirname "$path")" - pwd -P # -P resolves symlinks - )/$(basename "$path")" - } +get_realpath() { + local bin + local found='' + for bin in $(type -ap ssh | sort | uniq); do + if "$bin" -e . >/dev/null 2>&1; then + found=y + break + fi + done + if [[ -n "$found" ]]; then + realpath_path="$bin" + # by default, macOS does not have realpath + realpath() { + "$realpath_path" "$@" + } export -f realpath -fi; + else + cat <<'MESSAGE' +GNU realpath is required to build Apache Traffic Control if your +realpath binary does not support the -e flag, as is the case on BSD-like +operating systems like macOS. Install it by running the following +command: + brew install coreutils +MESSAGE + exit 1 + fi +} +get_realpath # Files are relative to this script directory. SELF="${BASH_SOURCE[0]}" @@ -262,7 +278,7 @@ while (( "$#" )); do # where is the name of the specific service to be chained. The file may be a symlink to another # compose file, in which case the symlink will be followed before it is processed. if [ -e "$COMPOSE_FILE.$1" ] ; then - $SELF -f $(realpath "$COMPOSE_FILE.$1") $([ "$verbose" == 0 ] || echo "-v") $([ "$quiet" == 0 ] || echo "-q") $([ "$debug" == 0 ] || echo "-d") $([ "$build" == 0 ] || echo "-b") $("${COMPOSECMD[@]}" -f $(realpath "$COMPOSE_FILE.$1") config --services) + $SELF -f $(realpath -e "$COMPOSE_FILE.$1") $([ "$verbose" == 0 ] || echo "-v") $([ "$quiet" == 0 ] || echo "-q") $([ "$debug" == 0 ] || echo "-d") $([ "$build" == 0 ] || echo "-b") $("${COMPOSECMD[@]}" -f $(realpath -e "$COMPOSE_FILE.$1") config --services) chained_exit=$? [ $chained_exit == 0 ] || exit $chained_exit fi