Osprey exports and externs: one app for the Mac and iPhone with Swift

Osprey compiles to native code with a plain C interface (the iOS target specification defines it), so a Swift app can link it like any C library. This video follows one real app, Osprey Storage, across that boundary. Osprey Storage is a disk browser for the Mac and the iPhone: it measures a folder, hidden files included, and ranks what takes up the space. Its logic is written in Osprey and its platform code in Swift.

Osprey exports and externs: one app for the Mac and iPhone with Swift (4:21). Captions are on; choose a chapter to jump to it.

What the video covers

Interop here means two languages calling each other inside one process. The calls go both ways:

  • Swift calls Osprey. The compiler builds the project into a static library, libOspreyApp.a, with a C header, libOspreyApp.h. Entry-source functions with supported scalar signatures become C prototypes that Swift calls through a bridging header.
  • Osprey calls Swift. Osprey declares host functions with extern; Swift defines them with @_cdecl. Disk operations live in StorageServices.swift, and Photos operations in PhotoLibrary.swift.

The video then shows how the app stays testable. Osprey only reaches the Swift functions through an algebraic effect handler at the boundary. A test handler answers the same effect with fixed data, so the whole browser runs in tests with no Swift and no disk. Finally, it shows one Xcode target building the same Osprey code for the Mac, the iPhone and the iPhone simulator.

The recording predates the Full Disk Access argument and Photos handler. Use the current walkthrough for runnable code.

Get it working

You need:

  • An Apple silicon Mac with Xcode, selected with xcode-select. Add the iOS simulator runtime in Xcode if you want the iPhone build.
  • Rust, to build the Osprey compiler.
  • The LLVM/clang toolchain described in Install Osprey.

Clone the repository and run the app from its root:

git clone https://github.com/Nimblesite/osprey.git
cd osprey
make -C src/toolchain/targets/macos storage                 # build the compiler, the Mac runtime and the app, then open it
src/osprey/examples/macos/run.sh ios-sim      # build the same app and launch it in the iPhone simulator

Run the checks the video mentions:

target/release/osprey src/osprey/examples/macos/storage/test --run   # the Osprey tests, with no Swift and no disk
src/osprey/examples/macos/run.sh --smoke                             # the real walk and screens on this Mac

On Mac, the app starts at the whole disk. macOS can ask for access to protected folders during the walk; refused folders count as unreadable. Without Full Disk Access, other apps' data stays guarded until you choose a folder in the system picker or grant access in Settings. The example's README explains the permission flow.

Choose a folder to rank it, click a folder to open it, or use the eye button (⇧⌘.) to exclude hidden files from the totals. On Mac, right-click a folder to open it in Finder. The iPhone app measures its own container and, with permission, the photo library.

Read the walkthrough

Build a Mac and iPhone app with Osprey and Swift writes out the same steps with the full code. For an app that also handles SQLite and HTTP through effects, see iOS and Android apps. Osprey is actively evolving.

Transcript

  1. This is Osprey Storage: one app for the Mac and the iPhone. Its logic is written in Osprey, and its platform code in Swift. Here's how the two work together.
  2. The app measures a folder, hidden files included, and ranks what takes up the space.
  3. Open any folder, and it ranks what's inside, all the way down.
  4. The work is split in two. Osprey owns the browser: its state, its events, the ranking, the hidden-file rule, and every screen.
  5. Swift owns what only the platform can do: the window, the folder picker, and the walk over the disk.
  6. Osprey exposes the app as two functions. Start builds the first screen for a home folder.
  7. Dispatch takes the state the last screen came with, and one event, and returns the next screen.
  8. The compiler builds the project for the Mac into a static library, and writes a C header beside it. Every export gets a plain C prototype: Osprey strings become char pointers, and integers become sixty-four bit ints.
  9. Swift includes that header through a bridging header, initializes Osprey once, and asks it for the first screen.
  10. Each tap or toggle goes back through dispatch, with the state Swift was handed, which it keeps as opaque text.
  11. Calls go the other way too. Osprey declares each function it needs from the host with extern.
  12. Storage scan, for example, lists one folder of the measured walk.
  13. The same header lists these as imports, functions the host must define with C linkage.
  14. Here is storage scan's prototype: a C string in, a sixty-four bit int out.
  15. Swift defines it with @_cdecl, so the linker finds exactly that C name and signature.
  16. The body reads the tree Swift measured, and returns how many entries the folder has.
  17. The browser's logic never calls those externs directly. It renders through a Disk Facts effect.
  18. At the boundary, this handler answers that effect with the Swift functions.
  19. That makes the Osprey side testable on its own. The test suite answers the same effect with fixed data.
  20. So the browser and every screen run natively in the tests, with no Swift and no disk.
  21. Each screen Osprey returns is a tree of native nodes. Here, the Hidden Files toggle is a node, with its value and the event it sends.
  22. A shared SwiftUI renderer draws that node as a real toggle, and sends its event straight back to Osprey.
  23. Flip it in the app, and the hidden folders drop out of every total.
  24. One Xcode target builds for the Mac, the iPhone, and the simulator. Its settings name the platform for each SDK.
  25. Then it links the Osprey library built for that platform, and imports its header through the bridging header.
  26. So the same Osprey code runs on iPhone, drawn with the phone's own controls, dark mode and larger text included.
  27. Now run the Osprey side's tests. They answer the Disk Facts effect with fixed data, so the browser runs with no Swift, no window and no disk. This one checks the ranking under the hidden-files setting.
  28. These check every screen against what the disk holds, and what an iPhone lets an app see.
  29. All seven pass, in a few seconds.
  30. Now change the rule that decides what's hidden. In sight says an entry is shown when hidden files are on, or when the entry isn't hidden. Replace the whole body with true, so hidden entries always show.
  31. Counted builds on it, so the totals change too. Nothing in Swift has changed.
  32. Run the tests again. The ranking test fails: hidden entries like Library and dot cache now appear, and the hidden contents are counted in the total.
  33. The screens test fails too. The hidden folder is no longer gone, and the size isn't the visible size any more.
  34. Five pass, two fail, and each failure names the check and the values. The rule lives in one Osprey function, and the tests guard it on both platforms at once. Put it back, and all seven pass.
  35. To try it, run the three commands on screen: the storage target for the Mac, the run script for the iPhone simulator, and the tests, which need no Swift at all. Osprey is actively evolving. Try it on your projects.