@@ -39,6 +39,29 @@ function canonicalize_path()
3939 echo $new_path
4040}
4141
42+ # relative_to ORIGIN PATH
43+ # Compute the relative path from ORIGIN to PATH.
44+ function relative_to() {
45+ local out=
46+ # Split path into components
47+ local -a relto; IFS=" /\\ " read -ra relto <<< " $1"
48+ local -a path; IFS=" /\\ " read -ra path <<< " $2"
49+ local off=0
50+ while [[ " ${relto[$off]} " == " ${path[$off]} " ]]; do
51+ if [[ $off -eq ${# relto[@]} || $off -eq ${# path[@]} ]]; then
52+ break
53+ fi
54+ : $(( off++ ))
55+ done
56+ for (( i= $off ; i < ${# relto[@]} ; i++ )) ; do
57+ out=" $out ${out: +/ } .."
58+ done
59+ for (( i= $off ; i < ${# path[@]} ; i++ )) ; do
60+ out=" $out ${out: +/ }${path[$i]} "
61+ done
62+ echo " $out "
63+ }
64+
4265# Remove any relative entries, because we'll be changing CWD shortly.
4366LD_LIBRARY_PATH=$( canonicalize_path $LD_LIBRARY_PATH )
4467LIBRARY_PATH=$( canonicalize_path $LIBRARY_PATH )
@@ -48,7 +71,7 @@ name=$1
4871execroot=" $( pwd) "
4972setup=$execroot /$2
5073srcdir=$execroot /$3
51- pkgroot=" $( realpath $execroot /$4 /.. ) " # By definition (see ghc-pkg source code).
74+ pkgroot=" $( realpath $execroot /$( dirname $4 ) ) " # By definition (see ghc-pkg source code).
5275shift 4
5376
5477declare -a extra_args
@@ -113,8 +136,23 @@ library=($libdir/libHS*.a)
113136if [[ -n ${library+x} && -f $package_database /$name .conf ]]
114137then
115138 mv $libdir /libHS* .a $dynlibdir
116- sed ' s,library-dirs:.*,library-dirs: ${pkgroot}/lib,' \
117- $package_database /$name .conf > $package_database /$name .conf.tmp
139+ # The $execroot is an absolute path and should not leak into the output.
140+ # Replace each ocurrence of execroot by a path relative to ${pkgroot}.
141+ function replace_execroot() {
142+ local line
143+ local relpath
144+ while IFS=" " read -r line; do
145+ while [[ $line =~ (" $execroot " [^[:space:]]* ) ]]; do
146+ relpath=" $( relative_to " $pkgroot " " ${BASH_REMATCH[1]} " ) "
147+ line=" ${line/ ${BASH_REMATCH[1]} / \$\{ pkgroot\}\/ $relpath / } "
148+ done
149+ echo " $line "
150+ done
151+ }
152+ sed -e ' s,library-dirs:.*,library-dirs: ${pkgroot}/lib,' \
153+ $package_database /$name .conf \
154+ | replace_execroot \
155+ > $package_database /$name .conf.tmp
118156 mv $package_database /$name .conf.tmp $package_database /$name .conf
119157 %{ghc_pkg} recache --package-db=$package_database
120158fi
0 commit comments