From 52a70eece0f5c2b1707b3462d36084a66fa476fb Mon Sep 17 00:00:00 2001
From: Gilles Duboscq <gilles.m.duboscq@oracle.com>
Date: Thu, 9 Nov 2017 18:12:52 +0100
Subject: [PATCH] Fix `R_HOME_DIR` resolution to support symlinks

---
 mx.fastr/mx_fastr_dists.py | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/mx.fastr/mx_fastr_dists.py b/mx.fastr/mx_fastr_dists.py
index 0290d3551e..15e11e4160 100644
--- a/mx.fastr/mx_fastr_dists.py
+++ b/mx.fastr/mx_fastr_dists.py
@@ -122,10 +122,25 @@ class ReleaseBuildTask(mx.NativeBuildTask):
         with open(rcmd, 'w') as f:
             for line in lines:
                 if line.startswith('R_HOME_DIR='):
-                    f.write('R_HOME_DIR="$(dirname $0)/.."\n')
-                    # produces a canonical path
-                    line = 'R_HOME_DIR="$(unset CDPATH && cd ${R_HOME_DIR} && pwd)"\n'
-                f.write(line)
+                    f.write(
+"""
+source="${BASH_SOURCE[0]}"
+while [ -h "$source" ] ; do
+  prev_source="$source"
+  source="$(readlink "$source")";
+  if [[ "$source" != /* ]]; then
+    # if the link was relative, it was relative to where it came from
+    dir="$( cd -P "$( dirname "$prev_source" )" && pwd )"
+    source="$dir/$source"
+  fi
+done
+r_bin="$( cd -P "$( dirname "$source" )" && pwd )"
+R_HOME_DIR="$( dirname "$r_bin" )"
+""")
+                elif line.strip() == "#!/bin/sh":
+                    f.write("#!/usr/bin/env bash\n")
+                else:
+                    f.write(line)
         # jar files for the launchers
         jars_dir = join(bin_dir, 'fastr_jars')
         if not os.path.exists(jars_dir):
-- 
GitLab