|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Install wrong architecture packages on macOS |
| 4 | +# - Can only install one package at a time, unless using "brew bundle" |
| 5 | +# - Uses `brew deps --tree` to loop over dependencies |
| 6 | +# - For speed, tries to NOT upgrade a package automatically |
| 7 | +# - To upgrade a package, use wrongbrew.sh uninstall <packagename> && wrongbrew.sh install <packagename> |
| 8 | +# - Based on the example from @maxirmx https://stackoverflow.com/a/70822921/3196753 |
| 9 | +# |
| 10 | +# https://gist.github.com/tresf/9a45e1400a91f4c9c14a2240967094ff |
| 11 | + |
| 12 | +# Halt on first error |
| 13 | +set -e |
| 14 | + |
| 15 | +export HOMEBREW_NO_AUTO_UPDATE=1 |
| 16 | + |
| 17 | +# The location of wrongbrew. (if you like to live in danger, set to /usr/local or /opt/homebrew) |
| 18 | +WRONGBREW_DIR="$HOME/wrongbrew" |
| 19 | + |
| 20 | +# The foreign arch (x86_64 or arm64) |
| 21 | +echo "==> Host CPU: $(sysctl -n machdep.cpu.brand_string)" |
| 22 | +if [[ $(sysctl -n machdep.cpu.brand_string) =~ "Apple" ]]; then |
| 23 | + WRONGBREW_ARCH=x86_64 |
| 24 | +else |
| 25 | + WRONGBREW_ARCH=arm64 |
| 26 | +fi |
| 27 | + |
| 28 | +# Determine the current os codename (e.g. "bigsur") |
| 29 | +# WRONGBREW_OS=bigsur |
| 30 | +WRONGBREW_OS=$(cat '/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf' |grep 'SOFTWARE LICENSE AGREEMENT FOR ' | awk -F ' FOR ' '{print $2}' | awk -F 'OS X |macOS ' '{print $2}' | tr '[:upper:]' '[:lower:]' | awk -F '\' '{print $1}') |
| 31 | + |
| 32 | +if [ "$WRONGBREW_ARCH" = "arm64" ]; then |
| 33 | + WRONGBREW_BOTTLE_TAG="${WRONGBREW_ARCH}_${WRONGBREW_OS}" |
| 34 | +else |
| 35 | + # Intel does not have arch suffix |
| 36 | + WRONGBREW_BOTTLE_TAG="${WRONGBREW_OS}" |
| 37 | +fi |
| 38 | + |
| 39 | +if [ ! -d "$WRONGBREW_DIR" ]; then |
| 40 | + echo "==> Configuring $WRONGBREW_DIR for $WRONGBREW_ARCH using bottle \"$WRONGBREW_BOTTLE_TAG\"..." |
| 41 | + |
| 42 | + # Prepare our directory |
| 43 | + rm -rf "$WRONGBREW_DIR" |
| 44 | + mkdir "$WRONGBREW_DIR" && curl -sL https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C "$WRONGBREW_DIR" |
| 45 | +fi |
| 46 | + |
| 47 | +WRONGBREW_BIN="$WRONGBREW_DIR/bin/brew" |
| 48 | + |
| 49 | +# Install a package manually by bottle tag |
| 50 | +install_single_package() { |
| 51 | + response=$("$WRONGBREW_BIN" fetch --force --bottle-tag="${WRONGBREW_BOTTLE_TAG}" "$1" | grep "Downloaded to") |
| 52 | + parsed=$(echo "$response" | awk -F ' to: ' '{print $2}') |
| 53 | + "$WRONGBREW_BIN" install "$parsed" || true # force continue because python |
| 54 | +} |
| 55 | + |
| 56 | +# Build a depenency tree |
| 57 | +install() { |
| 58 | + linkbin=false |
| 59 | + for param in "$@"; do |
| 60 | + if [ "$param" = "--link-bin" ]; then |
| 61 | + linkbin=true |
| 62 | + continue |
| 63 | + fi |
| 64 | + # Convert deps --tree to something sortable to install in natural order |
| 65 | + deps=($("$WRONGBREW_BIN" deps --tree "$param" | tr -c '[:alnum:]._-@\n' ' ' |sort |tr -d ' ' |uniq)) |
| 66 | + for dep in "${deps[@]}"; do |
| 67 | + # Check using "brew list" |
| 68 | + if "$WRONGBREW_BIN" list "$dep" &>/dev/null ; then |
| 69 | + # By default install will check for updates for already installed packages |
| 70 | + # Save some time by skipping a package if it's already installed |
| 71 | + echo "==> Skipping \"$dep\", already installed" |
| 72 | + continue |
| 73 | + fi |
| 74 | + install_single_package "$dep" |
| 75 | + done |
| 76 | + # Only link the original package |
| 77 | + if $linkbin ; then |
| 78 | + link_bin "$param" |
| 79 | + fi |
| 80 | + done |
| 81 | +} |
| 82 | + |
| 83 | +# Loop over a Brewfile |
| 84 | +bundle_install() { |
| 85 | + if [ -f "$1" ]; then |
| 86 | + echo "==> Brewfile found: $1" |
| 87 | + deps=($(cat Brewfile |xargs -L1 echo | awk -F 'brew ' '{print $2}')) |
| 88 | + for dep in "${deps[@]}"; do |
| 89 | + install "$dep" |
| 90 | + done |
| 91 | + else |
| 92 | + # Shamelessly trigger the native command's error message |
| 93 | + "$WRONGBREW_BIN" bundle |
| 94 | + fi |
| 95 | +} |
| 96 | + |
| 97 | +# Moves a wrong-arch "bin" directory out of the way, symlinks the native one provided by the native |
| 98 | +# version of brew. |
| 99 | +# |
| 100 | +# The "right" way to do this with CMake is to leverage CMAKE_FIND_ROOT... and friends |
| 101 | +# but this only fixes tools in "bin". Nested tools like "qmake" still can't be found. |
| 102 | +link_bin() { |
| 103 | + for param in "$@"; do |
| 104 | + # First, install the native tool(s) |
| 105 | + brew install "$param" |
| 106 | + |
| 107 | + # Second, rename the foriegn tool (e.g. bin_arm64) |
| 108 | + before="$("$WRONGBREW_BIN" --prefix "$param")/bin" |
| 109 | + after="${before}_${WRONGBREW_ARCH}" |
| 110 | + new="$(brew --prefix "$param")/bin" |
| 111 | + echo "==> Moving $before -> $after" |
| 112 | + mv "$before" "$after" |
| 113 | + |
| 114 | + # Last, link the native tool to the foriegn tool |
| 115 | + echo "==> Linking $before -> $new" |
| 116 | + ln -s "$new" "$before" |
| 117 | + done |
| 118 | +} |
| 119 | + |
| 120 | +# Backup params |
| 121 | +params="$@" |
| 122 | + |
| 123 | +# For sanity reasons, let's only install one package at a time |
| 124 | +while test $# -gt 0; do |
| 125 | + case "$1" in |
| 126 | + install) |
| 127 | + shift |
| 128 | + install $@ |
| 129 | + exit 0 |
| 130 | + ;; |
| 131 | + bundle) |
| 132 | + if [ "$2" = "install" ]; then |
| 133 | + if [ ! -z "$3" ]; then |
| 134 | + bundle_install "$3" |
| 135 | + else |
| 136 | + bundle_install "$(pwd)/Brewfile" |
| 137 | + fi |
| 138 | + exit 0 |
| 139 | + fi |
| 140 | + ;; |
| 141 | + *) |
| 142 | + shift |
| 143 | + ;; |
| 144 | + esac |
| 145 | +done |
| 146 | + |
| 147 | +# We're not installing a package, just pass the params to brew, I guess :) |
| 148 | +"$WRONGBREW_BIN" $params |
0 commit comments