Skip to content

Commit 60284ed

Browse files
authored
run.sh: force MacOs to run the executable natively when on Apple Silicon (#79)
* run.sh: force MacOs to run the executable natively when on Apple Silicon * Fix non-quoted `DYLD_INSERT_LIBRARIES` expansion * Convert to using `exec arch`
1 parent a93c3fe commit 60284ed

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

assets/nix/run.sh

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ abs_path() {
123123
echo "$(cd "$(dirname "$1")" && pwd)/$(basename "$1")"
124124
}
125125

126-
# Set executable path and the extension to use for the libdoorstop shared object
126+
# Set executable path and the extension to use for the libdoorstop shared object as well as check whether we're running on Apple Silicon
127127
os_type="$(uname -s)"
128128
case ${os_type} in
129129
Linux*)
@@ -148,6 +148,14 @@ case ${os_type} in
148148
;;
149149
esac
150150
lib_extension="dylib"
151+
152+
# CPUs for Apple Silicon are in the format "Apple M.."
153+
cpu_type="$(sysctl -n machdep.cpu.brand_string)"
154+
case "${cpu_type}" in
155+
Apple*)
156+
is_apple_silicon=1
157+
;;
158+
esac
151159
;;
152160
*)
153161
# alright whos running games on freebsd
@@ -309,4 +317,14 @@ else
309317
export DYLD_INSERT_LIBRARIES="${doorstop_name}:${DYLD_INSERT_LIBRARIES}"
310318
fi
311319

312-
exec "$executable_path" "$@"
320+
if [ -n "${is_apple_silicon}" ]; then
321+
export ARCHPREFERENCE="arm64,x86_64"
322+
323+
# We need to use arch for Apple Silicon to allow the executable to be run natively as otherwise if
324+
# the executable is universal, supporting both x86_64 and arm64, MacOs will still run it as x86_64
325+
# if the parent process is running as x86.
326+
# arch also strips the DYLD_INSERT_LIBRARIES env var so we have to pass that in manually
327+
exec arch -e DYLD_INSERT_LIBRARIES="${DYLD_INSERT_LIBRARIES}" "$executable_path" "$@"
328+
else
329+
exec "$executable_path" "$@"
330+
fi

0 commit comments

Comments
 (0)