Skip to content

Commit aa6ff0b

Browse files
committed
Add relocate_bottle function to handle HOMEBREW_PREFIX and HOMEBREW_CELLAR placeholders
1 parent a4fef0f commit aa6ff0b

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

‎cmake/macos-brew-deps.sh‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,38 @@ fetch_intel_bottle() {
1616
digest=$(echo "$index" | python3 -c 'import sys,json; print(next(m["annotations"]["sh.brew.bottle.digest"] for m in json.load(sys.stdin)["manifests"] if m["annotations"].get("org.opencontainers.image.ref.name","").endswith(".sonoma") and m["platform"]["architecture"]=="amd64"))')
1717
curl -fsSL -H "Authorization: Bearer $token" -o "/tmp/$3.tar.gz" "https://ghcr.io/v2/homebrew/core/$1/blobs/sha256:$digest"
1818
tar -xzf "/tmp/$3.tar.gz" -C "$cellar"
19+
relocate_bottle "$cellar/$3"
1920
mkdir -p "$(brew --prefix)/opt"
2021
ln -sfn "$cellar/$3/$2" "$(brew --prefix)/opt/$3"
2122
}
2223

24+
# Replace the @@HOMEBREW_PREFIX@@ / @@HOMEBREW_CELLAR@@ placeholders that bottles
25+
# ship with (normally done by `brew install` when pouring a bottle).
26+
relocate_bottle() {
27+
local dir="$1" prefix cellar f old
28+
prefix="$(brew --prefix)"
29+
cellar="$(brew --cellar)"
30+
_reloc() { local s="${1//@@HOMEBREW_PREFIX@@/$prefix}"; echo "${s//@@HOMEBREW_CELLAR@@/$cellar}"; }
31+
# text files (scripts, .pc, .cmake, clang config, ...)
32+
grep -rIl '@@HOMEBREW_' "$dir" 2>/dev/null | while IFS= read -r f; do
33+
sed -i '' -e "s|@@HOMEBREW_PREFIX@@|$prefix|g" -e "s|@@HOMEBREW_CELLAR@@|$cellar|g" "$f"
34+
done
35+
# Mach-O files: dependent libraries, install names and rpaths
36+
find "$dir" -type f ! -type l | while IFS= read -r f; do
37+
file "$f" | grep -q 'Mach-O' || continue
38+
chmod u+w "$f"
39+
otool -L "$f" | awk 'NR>1{print $1}' | grep '@@HOMEBREW_' | while IFS= read -r old; do
40+
install_name_tool -change "$old" "$(_reloc "$old")" "$f"
41+
done
42+
old="$(otool -D "$f" | sed -n '2p')"
43+
if [[ "$old" == *@@HOMEBREW_* ]]; then install_name_tool -id "$(_reloc "$old")" "$f"; fi
44+
otool -l "$f" | awk '/cmd LC_RPATH/{getline; getline; print $2}' | grep '@@HOMEBREW_' | while IFS= read -r old; do
45+
install_name_tool -rpath "$old" "$(_reloc "$old")" "$f"
46+
done
47+
codesign -f -s - "$f" 2>/dev/null || true # ad-hoc re-sign after editing
48+
done
49+
}
50+
2351
install_macos_brew_deps() {
2452
brew update || true # allow failure
2553
if [[ "$(uname -m)" == "x86_64" ]]; then
@@ -40,4 +68,7 @@ install_macos_brew_deps() {
4068
fi
4169
# Both kegs are keg-only, so put them on PATH explicitly.
4270
export PATH="$(brew --prefix llvm@19)/bin:$(brew --prefix lld@19)/bin:$PATH"
71+
# fail fast if the (manually installed) toolchain doesn't run
72+
clang --version
73+
ld.lld --version || true
4374
}

0 commit comments

Comments
 (0)