Repository navigation
Expand file tree
/
Copy pathfix-mac-binaries.sh
More file actions
executable file
·139 lines (122 loc) · 5.07 KB
/
Copy pathfix-mac-binaries.sh
File metadata and controls
executable file
·139 lines (122 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
#
# Fix the bundled macOS binaries: point them at their own libneko, then sign
# them with a Developer ID identity.
#
# Run this after every Haxe/Neko update, on a machine holding the
# "Developer ID Application" certificate and its private key.
#
# ./fix-mac-binaries.sh
#
# See ./fix-linux-binaries.sh for the Linux counterpart.
#
# Upstream ships haxe/haxelib-bin with /usr/local/lib as its only @rpath, so
# it loads whatever libneko happens to sit there (a Homebrew one, typically)
# instead of the copy shipped right next to it. Under the hardened runtime
# that is a hard failure: library validation requires the loaded dylib to
# carry the same Team ID as the process.
#
# Environment:
# SIGN_IDENTITY codesign identity (default: the Jeremy FAIVRE Developer ID)
# NOTARY_PROFILE if set, submit a zip of mac/ to notarytool using this
# stored credential profile (see `xcrun notarytool store-credentials`)
# KEEP_USR_LOCAL set to 1 to keep /usr/local/lib as an rpath fallback
#
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MAC_DIR="$ROOT/mac"
NEKO_DIR="$MAC_DIR/neko"
[ -d "$MAC_DIR" ] || { echo "error: no mac/ directory next to this script" >&2; exit 1; }
SIGN_IDENTITY="${SIGN_IDENTITY:-Developer ID Application: Jeremy FAIVRE (BUK697M6FJ)}"
if ! security find-identity -v -p codesigning | grep -qF "$SIGN_IDENTITY"; then
echo "error: signing identity not found in keychain:" >&2
echo " $SIGN_IDENTITY" >&2
echo >&2
echo "available identities:" >&2
security find-identity -v -p codesigning >&2
exit 1
fi
# Relative path from a binary's directory up to mac/neko, e.g. "mac/haxe"
# -> "../neko". Kept in sync with the same helper in fix-linux-binaries.sh.
rel_to_neko() {
local bindir="$1" platform="$2" sub up rest
[ "$bindir" = "$platform" ] && { printf 'neko'; return; }
sub="${bindir#"$platform"/}"
[ "$sub" = "neko" ] && { printf '.'; return; }
up=""
rest="$sub"
while [ -n "$rest" ]; do
up="../$up"
case "$rest" in */*) rest="${rest%/*}";; *) rest="";; esac
done
printf '%s' "${up}neko"
}
# Every Mach-O file we ship, dylibs/ndlls first so they are signed before the
# executables that load them.
#
# The list is built ONCE up front: codesign writes transient *.cstemp files
# next to each target, and a lazily-streamed `find` would pick those up.
BINARIES=()
collect() {
local f
while IFS= read -r f; do
case "$f" in *.cstemp) continue;; esac
file "$f" | grep -q 'Mach-O' && BINARIES+=("$f")
done < <(find "$MAC_DIR" -type f \( -name '*.dylib' -o -name '*.ndll' \) | sort)
while IFS= read -r f; do
case "$f" in *.dylib|*.ndll|*.cstemp) continue;; esac
file "$f" | grep -q 'Mach-O' && BINARIES+=("$f")
done < <(find "$MAC_DIR" -type f -perm -u+x | sort)
}
collect
echo "==> ${#BINARIES[@]} Mach-O files"
rpaths_of() {
otool -l "$1" | awk '/LC_RPATH/{r=1;next} r&&/^ *path /{print $2;r=0}' | sort -u
}
# --- 1. rpaths ------------------------------------------------------------
# Must happen BEFORE signing: editing load commands invalidates a signature.
echo "==> fixing rpaths"
for bin in "${BINARIES[@]}"; do
otool -L "$bin" | grep -q '@rpath/libneko' || continue
rel="$(rel_to_neko "$(dirname "$bin")" "$MAC_DIR")"
want="@executable_path/$rel"
[ "$rel" = "." ] && want="@executable_path/"
have="$(rpaths_of "$bin")"
if ! printf '%s\n' "$have" | grep -qxF "$want"; then
install_name_tool -add_rpath "$want" "$bin" 2>/dev/null
echo " fixed ${bin#$ROOT/}: added $want"
fi
if [ "${KEEP_USR_LOCAL:-0}" != "1" ] && printf '%s\n' "$have" | grep -qxF '/usr/local/lib'; then
install_name_tool -delete_rpath '/usr/local/lib' "$bin" 2>/dev/null
echo " fixed ${bin#$ROOT/}: removed /usr/local/lib"
fi
done
# --- 2. sign --------------------------------------------------------------
echo "==> signing as: $SIGN_IDENTITY"
for bin in "${BINARIES[@]}"; do
codesign --force --timestamp --options runtime --sign "$SIGN_IDENTITY" "$bin" 2>/dev/null
echo " signed ${bin#$ROOT/}"
done
# --- 3. verify ------------------------------------------------------------
echo "==> verifying"
for bin in "${BINARIES[@]}"; do
codesign --verify --strict "$bin" || { echo " FAILED: $bin" >&2; exit 1; }
done
echo "==> smoke test"
"$MAC_DIR/haxe/haxelib" version
"$MAC_DIR/haxe/haxe" -version
"$NEKO_DIR/neko" -version
# --- 4. optional notarization --------------------------------------------
# notarytool only accepts .zip/.pkg/.dmg, and a ticket CANNOT be stapled onto
# loose Mach-O executables -- `stapler` only handles .app, .pkg and .dmg.
# Files obtained via `git clone` never carry a com.apple.quarantine xattr, so
# Gatekeeper does not assess them anyway. This step is therefore only useful
# if these binaries also get shipped as a browser-downloadable archive.
if [ -n "${NOTARY_PROFILE:-}" ]; then
echo "==> notarizing"
zip="$(mktemp -d)/haxe-mac.zip"
/usr/bin/ditto -c -k --keepParent "$MAC_DIR" "$zip"
xcrun notarytool submit "$zip" --keychain-profile "$NOTARY_PROFILE" --wait
echo " submitted: $zip (ticket cannot be stapled to loose binaries)"
fi
echo "==> ok"