Skip to content

Commit

Permalink
Make dynlibs relative for OSX
Browse files Browse the repository at this point in the history
  • Loading branch information
earlephilhower committed Jan 2, 2024
1 parent 122f0b6 commit 01f4f96
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/build-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ jobs:
build:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
with:
submodules: false

- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: '13.1.0'
Expand Down Expand Up @@ -43,6 +47,13 @@ jobs:
cp -a picotool /tmp/picotool/.
cp -a ../LICENSE.TXT /tmp/picotool/.
cp -a ../README.md /tmp/picotool/.
# Make dynlibs relative
otool -L /tmp/picotool/picotool
(cwd=$(pwd); cd /tmp/picotool; bash $cwd/osx-relative-dynlib.sh)
otool -L /tmp/picotool/picotool
# Create tarball
tar cvf ../../picotool.macos.tar -C /tmp picotool
- name: Upload Picotool
Expand Down
43 changes: 43 additions & 0 deletions osx-relative-dynlib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash -eu

process_binary() {
local binary="$1"
echo "[*] Processing $binary..."

# Find all linked libraries from the Homebrew prefix
local libraries=(
$(otool -L "$binary" | grep '/usr/local/opt/' | awk '{print $1}')
)

# If this is a dylib, the first entry is the ID of the library itself, which
# we don't really care about.
if [ "${binary##*.}" = "dylib" ]; then
libraries=("${libraries[@]:1}")
fi

# When empty, skip the loop to avoid a Bash unbound variable error
if [ ${#libraries[@]} -le 0 ]; then
continue
fi

for lib in "${libraries[@]}"; do
local lib_basename=$(basename "$lib")

# Check that we have a copy of each linked library
if [ ! -f "$lib_basename" ]; then
echo " [-] Failed to find linked library $lib_basename"
continue
fi

# Change the path to be relative
install_name_tool -change "$lib" "@executable_path/$lib_basename" \
"$binary"
echo " [+] Made relative link to $lib_basename"
done
}


for binary in *.dylib $(find . -type f -perm +111); do
process_binary "$binary"
done

0 comments on commit 01f4f96

Please sign in to comment.