Skip to content

Commit 7b2e23e

Browse files
Dario Berzanodberzano
Dario Berzano
authored andcommitted
Add macOS relocation
LC_ID_DYLIB, LC_RPATH and LC_LOAD_DYLIB relocation ruels are added for macOS to the relocation script. By default relocate under bin, lib and lib64: override with `relocate_paths` in the recipe.
1 parent 9b4f8a0 commit 7b2e23e

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

alibuild_helpers/build.py

+3
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,8 @@ def doBuild(args, parser):
492492
spec["incremental_hash"] = ih.hexdigest()
493493
elif p in develPkgs:
494494
h(spec.get("devel_hash"))
495+
if args.architecture.startswith("osx") and "relocate_paths" in spec:
496+
h("relocate:"+" ".join(sorted(spec["relocate_paths"])))
495497
spec["hash"] = h.hexdigest()
496498
spec["deps_hash"] = dh.hexdigest()
497499
debug("Hash for recipe %s is %s" % (p, spec["hash"]))
@@ -910,6 +912,7 @@ def doBuild(args, parser):
910912
("PKGNAME", spec["package"]),
911913
("PKGREVISION", spec["revision"]),
912914
("PKGVERSION", spec["version"]),
915+
("RELOCATE_PATHS", " ".join(spec.get("relocate_paths", []))),
913916
("REQUIRES", " ".join(spec["requires"])),
914917
("RUNTIME_REQUIRES", " ".join(spec["runtime_requires"])),
915918
("WRITE_REPO", spec.get("write_repo", source)),

alibuild_helpers/build_template.sh

+45
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,51 @@ cat "$INSTALLROOT/.original-unrelocated" | xargs -n1 -I{} echo "sed -e \"s|/[^ ]
168168
if [[ ! -s "$INSTALLROOT/.original-unrelocated" && -f "$INSTALLROOT/etc/modulefiles/$PKGNAME" ]]; then
169169
echo "mv -f \$PP/etc/modulefiles/$PKGNAME \$PP/etc/modulefiles/${PKGNAME}.forced-relocation && sed -e \"s|[@][@]PKGREVISION[@]\$PH[@][@]|$PKGREVISION|g\" \$PP/etc/modulefiles/${PKGNAME}.forced-relocation > \$PP/etc/modulefiles/$PKGNAME" >> "$INSTALLROOT/relocate-me.sh"
170170
fi
171+
172+
# Find libraries and executables needing relocation on macOS
173+
if [[ ${ARCHITECTURE:0:3} == "osx" ]]; then
174+
175+
/usr/bin/find ${RELOCATE_PATHS:-bin lib lib64} -type f | \
176+
while read BIN; do
177+
MACHOTYPE=$(set +o pipefail; otool -h "$PWD/$BIN" 2> /dev/null | grep filetype -A1 | tail -n1 | awk '{print $5}')
178+
179+
# See mach-o/loader.h from XNU sources: 2 == executable, 6 == dylib
180+
if [[ $MACHOTYPE == 6 ]]; then
181+
# Only dylibs: relocate LC_ID_DYLIB
182+
if otool -D "$PWD/$BIN" 2> /dev/null | tail -n1 | grep -q $PKGHASH; then
183+
cat <<EOF >> "$INSTALLROOT/relocate-me.sh"
184+
install_name_tool -id \$(otool -D "\$PP/$BIN" | tail -n1 | sed -e "s|/[^ ]*INSTALLROOT/\$PH/\$OP|\$WORK_DIR/\$PP|g") "\$PP/$BIN"
185+
EOF
186+
fi
187+
fi
188+
189+
if [[ $MACHOTYPE == 2 || $MACHOTYPE == 6 ]]; then
190+
# Both libs and binaries: relocate LC_RPATH
191+
if otool -l "$PWD/$BIN" 2> /dev/null | grep -A2 LC_RPATH | grep path | grep -q $PKGHASH; then
192+
cat <<EOF >> "$INSTALLROOT/relocate-me.sh"
193+
OLD_RPATHS=\$(otool -l \$PP/$BIN | grep -A2 LC_RPATH | grep path | grep \$PH | sed -e 's|^.*path ||' -e 's| .*$||')
194+
for OLD_RPATH in \$OLD_RPATHS; do
195+
NEW_RPATH=\${OLD_RPATH/#*INSTALLROOT\/\$PH\/\$OP/\$WORK_DIR/\$PP}
196+
install_name_tool -rpath "\$OLD_RPATH" "\$NEW_RPATH" "\$PP/$BIN"
197+
done
198+
EOF
199+
fi
200+
201+
# Both libs and binaries: relocate LC_LOAD_DYLIB
202+
if otool -l "$PWD/$BIN" 2> /dev/null | grep -A2 LC_LOAD_DYLIB | grep name | grep -q $PKGHASH; then
203+
cat <<EOF >> "$INSTALLROOT/relocate-me.sh"
204+
OLD_LOAD_DYLIBS=\$(otool -l \$PP/$BIN | grep -A2 LC_LOAD_DYLIB | grep name | grep \$PH | sed -e 's|^.*name ||' -e 's| .*$||')
205+
for OLD_LOAD_DYLIB in \$OLD_LOAD_DYLIBS; do
206+
NEW_LOAD_DYLIB=\${OLD_LOAD_DYLIB/#*INSTALLROOT\/\$PH\/\$OP/\$WORK_DIR/\$PP}
207+
install_name_tool -change "\$OLD_LOAD_DYLIB" "\$NEW_LOAD_DYLIB" "\$PP/$BIN"
208+
done
209+
EOF
210+
fi
211+
fi
212+
done || true
213+
214+
fi
215+
171216
cat "$INSTALLROOT/relocate-me.sh"
172217
cat "$INSTALLROOT/.original-unrelocated" | xargs -n1 -I{} cp '{}' '{}'.unrelocated
173218
cd "$WORK_DIR/INSTALLROOT/$PKGHASH"

docs/reference.markdown

+6-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ An example recipe for `zlib` is the following:
3030
source: https://github.com/star-externals/zlib
3131
tag: v1.2.8
3232
---
33-
#!/bin/sh
33+
#!/bin/bash -ex
3434
./configure --prefix=$INSTALLROOT
3535
make ${JOBS+-j $JOBS}
3636
make install
@@ -88,7 +88,7 @@ The following entries are optional in the header:
8888
- `append_path`: same as `prepend_path` but paths are appended rather than
8989
prepended.
9090
- `requires`: a list of run-time and build-time dependency for the package. E.g.:
91-
91+
9292
package: AliRoot
9393
requires:
9494
- ROOT
@@ -99,7 +99,7 @@ The following entries are optional in the header:
9999
the dependency name. Such a reqular expression will be matched against the
100100
architecture provided via `--architecture` and if it does not match it will
101101
not be included. For instance:
102-
102+
103103
package: AliRoot-test
104104
requires:
105105
- "igprof:(?!osx).*"
@@ -127,6 +127,9 @@ The following entries are optional in the header:
127127
does not match, the check is skipped and the recipe is run. Using the switch
128128
`--always-prefer-system` runs the check always (even when the regular
129129
expression for the architecture does not match).
130+
- `relocate_paths`: a list of toplevel paths scanned recursively to perform
131+
relocation of executables and dynamic libraries **on macOS only**. If not
132+
specified defaults to `bin`, `lib` and `lib64`.
130133

131134
### The body
132135

0 commit comments

Comments
 (0)