This is an automated email from the ASF dual-hosted git repository.

zrhoffman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new 98687d5574 Add realpath support to pkg (#7981)
98687d5574 is described below

commit 98687d5574dc8218f5e667f933251fe33ee4b73b
Author: Zach Hoffman <[email protected]>
AuthorDate: Mon Jun 3 22:23:48 2024 -0600

    Add realpath support to pkg (#7981)
    
    * Add realpath support to pkg
    
    The `pkg` script (along with our build scripts) makes use of `realpath`.
    But unlike the build scripts, `pkg` does not have a backup `realpath`
    function if the host OS does not have `realpath`.
    apache/trafficcontrol#7981 adds a backup `realpath` function to the
    `pkg` script.
    
    * realpath -e -> realpath
    
    * Improve realpath function and detection
    
    * Use GNU realpath if realpath -e does not work
    
    If realpath -e oes not work, the `realpath` binary is probably BSD
    realpath.
    
    * Add grealpath to paths to check for macOS/FreeBSD support
---
 build/functions.sh                   | 48 ++++++++++++++++++++++++++++--------
 docs/source/development/building.rst |  4 +--
 pkg                                  | 39 +++++++++++++++++++++++++++++
 3 files changed, 79 insertions(+), 12 deletions(-)

diff --git a/build/functions.sh b/build/functions.sh
index 718516d833..541ddf5985 100755
--- a/build/functions.sh
+++ b/build/functions.sh
@@ -13,16 +13,44 @@
 #
 # shellcheck shell=ash
 
-if ! type -p realpath; then
-       # by default, macOS does not have realpath
-       realpath() {
-               ls "$(
-                       cd "$(dirname "$0")"
-                       pwd -P # -P resolves symlinks
-               )/$(basename "$0")"
-       }
-       export -f realpath
-fi;
+# macOS's version of realpath does not resolve symlinks, so we add a function
+# for it.
+get_realpath() {
+       local bin
+       local found=''
+       first_realpath="$(type -P realpath)"
+       for bin in $(type -aP grealpath realpath | uniq); do
+               if "$bin" -e . >/dev/null 2>&1; then
+                       found=y
+                       break
+               fi
+       done
+       if [[ -n "$found" ]]; then
+               if [[ "$first_realpath" == "$bin" ]]; then
+                       # Default realpath works.
+                       return
+               fi
+               realpath_path="$bin"
+               # by default, macOS does not have realpath
+               eval "$(<<FUNCTION cat
+               realpath() {
+                       "$realpath_path" "\$@"
+               }
+FUNCTION
+               )"
+               export -f realpath
+       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 <docs-guide>` |
        
+=================================+=====================+============================+========================+===========================+===========================+==========================+==========+==============================+==========================+
-       | 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 a9efc2b4af..2a60d6dfcc 100755
--- a/pkg
+++ b/pkg
@@ -12,6 +12,45 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+# macOS's version of realpath does not resolve symlinks, so we add a function
+# for it.
+get_realpath() {
+       local bin
+       local found=''
+       first_realpath="$(type -P realpath)"
+       for bin in $(type -aP grealpath realpath | uniq); do
+               if "$bin" -e . >/dev/null 2>&1; then
+                       found=y
+                       break
+               fi
+       done
+       if [[ -n "$found" ]]; then
+               if [[ "$first_realpath" == "$bin" ]]; then
+                       # Default realpath works.
+                       return
+               fi
+               realpath_path="$bin"
+               # by default, macOS does not have realpath
+               eval "$(<<FUNCTION cat
+               realpath() {
+                       "$realpath_path" "\$@"
+               }
+FUNCTION
+               )"
+               export -f realpath
+       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]}"
 cd "$( dirname "${BASH_SOURCE[0]}" )"

Reply via email to