summaryrefslogtreecommitdiff
path: root/mk
diff options
context:
space:
mode:
authorBen Gamari <ben@smart-cactus.org>2021-11-19 22:59:35 -0500
committerMarge Bot <ben+marge-bot@smart-cactus.org>2021-12-02 18:14:06 -0500
commitfab2579e63bb317d4c266d7b949cf96ad6e5d17b (patch)
treef74e0fe7c0e7596ecb6aa94ebfa9be363d0305b6 /mk
parent44c088631f2d14f25c9cefeee174db4576b4c5cc (diff)
downloadhaskell-fab2579e63bb317d4c266d7b949cf96ad6e5d17b.tar.gz
hadrian: Don't rely on realpath in bindist Makefile
As noted in #19963, `realpath` is not specified by POSIX and therefore cannot be assumed to be available. Here we provide a POSIX shell implementation of `realpath`, due to Julian Ospald and others. Closes #19963.
Diffstat (limited to 'mk')
-rwxr-xr-xmk/relpath.sh53
1 files changed, 53 insertions, 0 deletions
diff --git a/mk/relpath.sh b/mk/relpath.sh
new file mode 100755
index 0000000000..c2f82f430b
--- /dev/null
+++ b/mk/relpath.sh
@@ -0,0 +1,53 @@
+#!/bin/sh
+
+# POSIX shell implementation of `realpath --relative-to=$1 $2.
+# This is an adaptation of the implementation from
+# <https://github.com/Offirmo/offirmo-shell-lib>.
+
+# returns relative path to $2=$target from $1=$source
+## NOTE : path are compared in text only. They don’t have to exist
+## and they WONT be normalized/escaped
+## Result in "$return_value"# both $1 and $2 are absolute paths beginning with /
+
+src="$1"
+target="$2"
+
+common_part="$src"
+result=""
+
+while test "${target#$common_part}" = "${target}" ; do
+ #echo "common_part is now : \"$common_part\""
+ #echo "result is now : \"$result\""
+ #echo "target#common_part : \"${target#$common_part}\""
+ # no match, means that candidate common part is not correct
+ # go up one level (reduce common part)
+ common_part="$(dirname "$common_part")"
+ # and record that we went back
+ if test -z "$result" ; then
+ result=".."
+ else
+ result="../$result"
+ fi
+done
+
+#echo "common_part is : \"$common_part\""
+
+if test "$common_part" = "/" ; then
+ # special case for root (no common path)
+ result="$result/"
+fi
+
+# since we now have identified the common part,
+# compute the non-common part
+forward_part="${target#$common_part}"
+#echo "forward_part = \"$forward_part\""
+
+if test -n "$result" && test -n "$forward_part" ; then
+ #echo "(simple concat)"
+ result="$result$forward_part"
+elif test -n "$forward_part" ; then
+ #echo "(concat with slash removal)"
+ result="$(printf "%s" "$forward_part" | cut -c 1-)"
+fi
+
+printf "%s" "$result"