Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ape/ape.S
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,14 @@ apesh: .ascii "\n@\n#'\"\n" // sixth edition shebang
.ascii "t=\"${TMPDIR:-${HOME:-.}}/.ape-"
.ascii APE_VERSION_STR
.ascii "\"\n"
#if SupportsXnu()
// Add an architecture suffix to the temporary APE Loader
// name to differentiate between x86_64 and arm64.
.ascii "[ -d /Applications ] && "
.ascii "t=\"${t}-$("
.ascii "uname -m 2>/dev/null || printf 'unknown'"
.ascii ")\"\n"
#endif /* SupportsXnu() */
.ascii "[ -x \"$t\" ] || {\n"
.ascii "mkdir -p \"${t%/*}\" &&\n"
.ascii "dd if=\"$o\" of=\"$t.$$\" skip="
Expand Down
8 changes: 8 additions & 0 deletions ape/ape.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@
#define APE_VERSION_STR_(x, y) APE_VERSION_STR__(x, y)
#define APE_VERSION_NOTE_(x, y) (100000000 * (x) + 1000000 * (y))

#if defined(__x86_64__)
#define GetXnuSuffix() "-x86_64"
#elif defined(__aarch64__)
#define GetXnuSuffix() "-arm64"
#else
#define GetXnuSuffix() "-unknown"
#endif

#endif /* COSMOPOLITAN_APE_APE_H_ */
7 changes: 7 additions & 0 deletions ape/apeuninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,17 @@ for x in .ape \
.ape-1.7 \
.ape-1.8 \
.ape-1.9 \
.ape-1.10-arm64 \
.ape-1.10-x86_64 \
.ape-1.10; do
rm -f \
~/$x \
/tmp/$x \
o/tmp/$x \
"${TMPDIR:-/tmp}/$x"
rm -f \
~/$x \
/tmp/$x \
o/tmp/$x \
"${TMPDIR:-/tmp}/$x.c"
done
7 changes: 6 additions & 1 deletion libc/proc/execve-sysv.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ int sys_execve(const char *prog, char *const argv[], char *const envp[]) {
RetryExecve("/usr/bin/ape", shargs, envp);
}
char *buf = alloca(PATH_MAX);
const char *name = "/.ape-" APE_VERSION_STR;
const char *name = "";
if (IsXnu()) {
name = "/.ape-" APE_VERSION_STR GetXnuSuffix();
} else {
name = "/.ape-" APE_VERSION_STR;
}
InitExecve();
RetryExecve(Join(g_execve.tmpdir, name, buf), shargs, envp);
RetryExecve(Join(g_execve.home, name, buf), shargs, envp);
Expand Down
9 changes: 8 additions & 1 deletion tool/build/apelink.c
Original file line number Diff line number Diff line change
Expand Up @@ -2016,7 +2016,14 @@ int main(int argc, char *argv[]) {

// otherwise try to use the ad-hoc self-extracted loader, securely
if (loaders.n) {
p = stpcpy(p, "t=\"${TMPDIR:-${HOME:-.}}/.ape-" APE_VERSION_STR "\"\n"
p = stpcpy(p, "t=\"${TMPDIR:-${HOME:-.}}/.ape-" APE_VERSION_STR);
if (support_vector & _HOSTXNU) {
// Add an architecture suffix to the temporary APE Loader
// name to differentiate between x86_64 and arm64.
p = stpcpy(p, "$([ -d /Applications ] && ( printf '-'; ( uname -m "
"2>/dev/null || printf 'unknown')))");
}
p = stpcpy(p, "\"\n"
"[ x\"$1\" != x--assimilate ] && "
"[ -x \"$t\" ] && "
"exec \"$t\" \"$o\" \"$@\"\n");
Expand Down
6 changes: 6 additions & 0 deletions tool/build/pledge.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,10 +555,16 @@ void ApplyFilesystemPolicy(unsigned long ipromises) {
if ((p = getenv("TMPDIR"))) {
UnveilIfExists(
__join_paths(buf, sizeof(buf), p, ".ape-" APE_VERSION_STR), "rx");
UnveilIfExists(__join_paths(buf, sizeof(buf), p,
".ape-" APE_VERSION_STR GetXnuSuffix()),
"rx");
}
if ((p = getenv("HOME"))) {
UnveilIfExists(
__join_paths(buf, sizeof(buf), p, ".ape-" APE_VERSION_STR), "rx");
UnveilIfExists(__join_paths(buf, sizeof(buf), p,
".ape-" APE_VERSION_STR GetXnuSuffix()),
"rx");
}
Comment on lines 555 to 568
Copy link
Author

@BenceSzalai BenceSzalai May 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably these could be wrapped in a condition as well to only run on xnu, but I don't know what / if there is an appropriate check available in this context.

}
}
Expand Down
3 changes: 2 additions & 1 deletion tool/cosmocc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ On Apple Silicon, `aarch64-unknown-cosmo-cc` produces ELF binaries. If
you build a hello world program, then you need to say `ape ./hello`. If
you don't have an `ape` command then run `cc -o ape bin/ape-m1.c` which
should be moved to `/usr/local/bin/ape`. Your APE interpreter might
already exist under a path like `$TMPDIR/.ape-1.10`. It's important to
already exist under a path like `$TMPDIR/.ape-1.10-arm64` or
`$TMPDIR/.ape-1.10-x86_64` if you are using Rosetta. It's important to
note this is only a gotcha for the cross compiler. Your `cosmocc`
compiler wraps the actual ELF binaries with a shell script that'll
extract and compile an APE loader automatically, as needed. This also
Expand Down