asdf-macos-app
2026-11-29
ASDF extension that builds macOS .app bundles from SBCL images.
asdf-macos-app
An ASDF extension that builds a macOS .app bundle from an SBCL image.
(defsystem "editor" :defsystem-depends-on ("asdf-macos-app") :class :macos-app-system :build-operation "macos-app-op" :entry-point "editor:main" :version "1.4.2" :bundle-identifier "com.example.editor" :bundle-name "Editor" :bundle-icon "res/icon.png" :code-signing-identity "Developer ID Application: Jane Doe (ABCDE12345)" :depends-on ("cffi" "editor-core") :components ((:file "main")))
$ sbcl --eval '(asdf:make "editor")' --quit
; dumping image: /usr/local/bin/sbcl --dynamic-space-size 8192 ...
; built /path/to/Editor.app/
Or from Lisp: (macos-app:make-app "editor").
A second build reuses the core of the first (; image is current, reusing it)
while it is newer than every source in the system's dependency closure, and
every foreign library it had open is still where it was and no newer than it.
A library moved or replaced, as by brew upgrade, means a fresh dump.
(macos-app:make-app "editor" :force-image t) always dumps.
utc-status is built this way
and ships with it: a menu-bar clock whose
release is a signed,
notarised disk image that opens on any Mac without a warning. It is worth a look
as a worked example — the .asd that produces
it
is a dozen options, and it exercises the parts most likely to bite:
LSUIElement for an app with no Dock icon, an icon generated at build time from
a PNG, a Developer ID taken from the environment so a clone still builds, and
notarisation of both the bundle and the disk image around it.
Two decisions that shape the design
The image dump kills the process. save-lisp-and-die never returns, so
nothing that must happen after the executable exists — codesigning, dylib
relocation, verification — can run in the same image. macos-app-op therefore
creates the bundle skeleton, spawns a child SBCL to perform
macos-app-image-op (which dumps the core into Contents/Resources/), and then
finishes the bundle in the parent. The child is told where to write via the
ASDF_MACOS_APP_BUNDLE environment variable, and gets an explicit source
registry naming every system in the resolved dependency closure — inheriting
CL_SOURCE_REGISTRY is not enough, since the parent may have found systems
through asdf:*central-registry* or a search function the child cannot see.
That registry travels inside the bootstrap file rather than the environment,
which has no length limit. The child does not get --no-userinit, so an
ocicl-style registry set up in your init file still applies on top.
The core is a resource, not an executable. save-lisp-and-die :executable t
appends the core to the SBCL runtime's Mach-O — past the end of __LINKEDIT
and past the code signature — and codesign then refuses the file outright:
$ codesign --force --sign - some-dumped-image
some-dumped-image: main executable failed strict validation
Measured: __LINKEDIT and the signature both end at byte 410,952 of a
47,782,952-byte image. That is 47MB of trailing data no signature can cover, and
it holds for a Developer ID as much as for ad hoc, and whether or not the old
signature is stripped first. An executable image cannot be signed, so it cannot
be notarised, so it cannot be shipped.
So the bundle holds three things instead of one:
Contents/Resources/sbcl.core— the dumped core, sealed as an ordinary resource. Tampering with it failscodesign --verify.Contents/MacOS/<exe>— a copy of the SBCL runtime, which is a clean Mach-O that signs and verifies.Contents/MacOS/sbcl.core— a relative symlink to the core.
The symlink is what reconciles two constraints that otherwise conflict. With no
--core argument and no SBCL_HOME — which is exactly what LaunchServices gives
a double-clicked app — the runtime looks for a core of that name beside its own
executable. But the core cannot actually live in MacOS/: codesign treats every
file there as nested code and refuses the bundle (code object is not signed at all / In subcomponent: …/MacOS/sbcl.core), whatever its permissions. A symlink
is found by the runtime and sealed as a resource by codesign.
No C launcher, and so still no C toolchain in the build.
The executable is never rewritten. install_name_tool is not run on it. So no
-add_rpath on the executable. Instead, every dylib is copied into
Contents/Frameworks, its own dependencies are rewritten to
@loader_path/<name>, and at startup the runtime pushes
Contents/Frameworks/ onto cffi:*foreign-library-directories*. CFFI then
resolves libraries to absolute paths inside the bundle. It does this in a UIOP
image restore hook that runs before every other one, so a library that closes
its foreign libraries before the dump and reopens them in a restore hook of
its own (im does) finds the bundle's copies, not those in CFFI's default
directories such as /opt/homebrew/lib.
A library that is open when the image is dumped is reopened from the
bundle. SBCL reopens such a library itself at startup, before any Lisp of
yours or of this library runs, by the path it was opened at — which in the
build was Homebrew's. Left alone, the application would use Homebrew's copy
and ignore its own, and on a Mac without Homebrew it would not start. So just
before the dump, each open library that is being bundled has its path changed
to @executable_path/../Frameworks/<name>, which dlopen expands and which
is right wherever the bundle is moved to. The build says which:
; /opt/homebrew/opt/openssl/lib/libssl.dylib will be opened as @executable_path/../Frameworks/libssl.4.dylib
Check it in a running application with lsof -p <pid> | grep dylib.
Layout produced
Editor.app/Contents/
Info.plist
PkgInfo "APPL????"
MacOS/editor a copy of the SBCL runtime
MacOS/sbcl.core -> ../Resources/sbcl.core
Resources/sbcl.core the dumped core, sealed by the signature
Resources/editor.icns
Resources/entitlements.plist
Resources/foreign-libraries.sexp manifest written by the child
Frameworks/*.dylib
Options
All are defsystem keywords on :macos-app-system.
| Keyword | Default | Notes |
|---|---|---|
:bundle-identifier | — | required, CFBundleIdentifier |
:bundle-name | :build-pathname, else capitalized system name | also names the .app |
:bundle-display-name | bundle name | |
:bundle-executable | downcased system name | Contents/MacOS/<this> |
:bundle-short-version | :version | CFBundleShortVersionString |
:bundle-icon | none | .icns copied; .png converted via sips + iconutil |
:bundle-minimum-system-version | "11.0" | |
:bundle-agent | nil | LSUIElement; menubar apps with no Dock icon |
:bundle-high-resolution | t | |
:bundle-category | none | LSApplicationCategoryType |
:bundle-principal-class | none | NSPrincipalClass; a Cocoa app wants "NSApplication" |
:bundle-copyright | none | |
:bundle-url-schemes | none | list of strings |
:bundle-document-types | none | list of plist-DSL dicts |
:bundle-info-plist | none | alist merged over the generated keys |
:bundle-resources | none | files/dirs copied into Contents/Resources; an entry is a path or (path . "relative/destination") |
:bundle-sbcl-contribs | t | copy the building SBCL's contrib/ into Contents/Resources/sbcl/, so require works in the app |
:bundle-runtime | the building SBCL's | a relinked SBCL runtime to copy into Contents/MacOS/ instead; must be from the same SBCL build. The App Sandbox needs one linked with -framework CoreFoundation, or sharedApplication aborts |
:bundle-runtime-frameworks | none | frameworks to link the runtime against, e.g. ("CoreFoundation") for the App Sandbox. Linked from the building SBCL's own objects, so it needs an SBCL built --with-sb-linkable-runtime; any other is refused before the image is dumped. Exclusive with :bundle-runtime |
:bundle-strip-runtime | nil | strip -S the runtime in Contents/MacOS/, removing the debug map's absolute paths into SBCL's build tree (/Users/<you>/...); symbols and build ID stay |
:bundle-log | t | redirect the app's stdio to a log file at runtime |
:bundle-log-max-bytes | 1 MB | rotate the log past this; nil disables |
:bundle-foreign-libraries | none | extra dylibs beyond what CFFI reports |
:bundle-output-directory | system source dir | where the .app lands |
:code-signing-identity | nil | nil = unsigned, "-" = ad hoc, else a Developer ID |
:entitlements | :sbcl-default | or a pathname, or nil |
:hardened-runtime | t | |
:compression | nil | passed to uiop:dump-image (needs zstd-enabled SBCL) |
The plist DSL: strings, integers, reals, :true/:false,
(:array v ...), (:dict ("Key" . v) ...), (:data "base64"),
(:date "...").
Runtime API
Available inside the built app:
macos-app:running-in-bundle-pmacos-app:bundle-root— the.apppathname, found by walking up fromargv0macos-app:bundle-resource—Contents/Resources, optionally joined with a relative pathmacos-app:bundle-frameworks
The generated toplevel wraps your :entry-point: it locates the bundle, sets
*default-pathname-defaults* to the home directory (Finder launches with
cwd = /), registers the Frameworks directory with CFFI, strips legacy
-psn_* arguments, and redirects output to ~/Library/Logs/<name>.log, since
a Finder-launched process has nowhere else to write. Set MACOS_APP_LOG in the
environment to send that somewhere else, or bind macos-app:*log-to-file* to
nil to keep stdio as-is.
Signing
:entitlements :sbcl-default writes the three keys an SBCL image needs under
the hardened runtime:
com.apple.security.cs.allow-unsigned-executable-memorycom.apple.security.cs.allow-jitcom.apple.security.cs.disable-library-validation
Nested code is signed innermost first, then the bundle — --deep is deprecated
by Apple. "Nested code" is not just Frameworks/*.dylib: it covers dylibs in
subdirectories, .framework and .xpc bundles signed as units rather than
walked into, helper executables sitting beside the main one in
Contents/MacOS, and login items under Contents/Library. The build then runs
codesign --verify --deep --strict and fails loudly if that does not pass.
:bundle-resources destinations are checked before anything is copied: they
must stay inside Contents/Resources (checked both as a path and through the
truename of the deepest existing ancestor), must not take a name the build
itself generates (<exe>.icns, entitlements.plist,
foreign-libraries.sexp), and two resources may not install to the same
place. Symbolic links are refused rather than followed, since copying through
one is how a resource escapes the bundle.
Notarization is a separate step, since it needs credentials and network:
(macos-app:notarize "Editor.app" :keychain-profile "notary")
after xcrun notarytool store-credentials notary.
Building off macOS
macos-app:*allow-non-macos-build* assembles the layout and Info.plist on any
host, skipping everything that needs otool, install_name_tool, codesign, sips
or plutil. The result is not shippable, and says so: a file called
Contents/INCOMPLETE-BUILD explains what was left out. That marker is acted
on rather than merely advisory — such a build refuses to replace a complete
bundle at the same path (bind macos-app:*replace-complete-bundle* to
override), and notarize refuses it outright. macos-app:complete-bundle-p
answers the question directly. The test suite uses this mode; nothing else
should.
On macOS the build additionally checks the tools are actually installed, since
a clean system has none of them until xcode-select --install has been run.
Tests
$ sbcl --eval '(asdf:test-system "asdf-macos-app")' --quit
202 checks, 0 failures
CI runs the suite on both Linux and macOS. The Linux leg covers layout,
plists, staging, freshness, the child protocol and the Mach-O parsers; the
macOS leg is the one that actually runs plutil, ad-hoc codesign and
codesign --verify against a real SBCL image, which is the riskiest part of
the design. Exactly one test signs, so a signing failure reports as one
failure instead of taking out every build test.
The fixture systems under tests/fixture are checked in as .asd.in and
renamed when the suite copies them to a scratch directory, so a recursive
source registry over this repository does not register them.
The build tests shell out to a real SBCL and dump images, so the suite takes
around thirty seconds. It works in a scratch directory and redirects the built
app's log with MACOS_APP_LOG, so it does not touch your home directory. The
dylib copy-and-rewrite loop in relocate-foreign-libraries is the one part
with no coverage: it needs real Mach-O files and install_name_tool. Its
parsers are covered against captured otool output.
Known limits and things to check
-
SBCL_HOMEin the environment loads the wrong core. The executable is the SBCL runtime and findssbcl.corebeside itself — unlessSBCL_HOMEis set, which wins, and the runtime then loads whatever core lives there. The application drops into a plain SBCL REPL and never starts. LaunchServices sets noSBCL_HOME, so a double-clicked app is unaffected; a bundle launched from a shell that has one is not, and Homebrew'ssbclis a wrapper script that exports it. There is no fix without a launcher binary that passes--coreexplicitly, which would mean a C toolchain in the build. Launch the bundle withenv -u SBCL_HOMEif you need to run it from such a shell. -
The banner. A separate core prints SBCL's startup banner unless
--noinformis passed, and LaunchServices passes nothing. It is printed by the C runtime before Lisp starts, so no:toplevelcan suppress it; only an embedded executable core does, and that is the thing that cannot be signed. For a bundle it goes to the log nobody reads. This is the one thing the old layout did better. -
The hardened runtime needs
disable-library-validation. A signed runtime refuses to load Homebrew dylibs —libzstdfor a compression-enabled SBCL — on a Team ID mismatch.:entitlements :sbcl-defaultalready carries that key; a bundle signed without it fails at launch rather than at build. -
Always launch the signed bundle before shipping it.
verify-signaturecatches a file-level failure, not a runtime one. -
Universal binaries. Not supported.
lipocannot merge two SBCL cores. Build arm64 and x86_64 bundles separately. -
Non-CFFI foreign libraries. Only CFFI's
list-foreign-librariesis consulted automatically. Anything loaded throughsb-aliendirectly must be listed in:bundle-foreign-libraries. -
Two dylibs with the same basename will collide in
Frameworks/; the build errors rather than silently picking one. -
The core runs in its bundle and nowhere else once it has a bundled library:
@executable_pathis the runtime inContents/MacOS. Started some other way —sbcl --core Contents/Resources/sbcl.core— it will not find those libraries. -
A library opened by a bare name, found by dyld on its own search path rather than at a path, is neither bundled nor repointed: there is no file to copy. Open it by path, or list it in
:bundle-foreign-libraries. -
SBCL only. The child-process invocation and
sb-ext:posix-environare SBCL-specific;macos-app:*child-lisp*and*child-lisp-options*let you point at a different runtime.