add a README.md to lib #52
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
libvmz
libvmzis the bridge layer that lets Zig boot a macOSVirtualization.frameworkLinux VM without writing the VM host in Swift.The Swift side owns the real
VZVirtualMachine. The Zig side talks to itthrough the small C ABI declared in
libvmz.h.Files
libvmz.swift: Swift implementation. It builds and starts theVZVirtualMachine, handles Virtualization.framework callbacks, and exports C symbols with@_cdecl.libvmz.h: C header consumed by Zig, C, or C-compatible callers. This is the public ABI for the bridge.examples/: standalone Zig example that links the Swift bridge directly.extra/: older/minimal standalone Swift launcher and helper scripts. This isuseful for comparing the pure Swift flow with the Zig-plus-bridge flow.
Why It Exists
Apple's VM APIs are exposed through Swift/Objective-C frameworks.
vmzis a ZigCLI, so it needs a narrow native bridge for the parts that must talk to
Virtualization.framework.libvmzkeeps that boundary small:VZLinuxBootLoader,VZVirtualMachineConfiguration,VZVirtualMachine, NAT networking, serial attachment, delegate callbacks, andthe run loop needed for Virtualization.framework callbacks.
API Shape
The C API is intentionally opaque:
VMZConfigdescribes the VM: kernel path, initrd path, kernel command line, serial input pipe fd, CPU count, memory size, and NAT networking.VMZHandleis an opaque Swift-managed VM engine pointer.VMZErrorRefis an opaque Swift-managed error pointer.vmz_validate_config()checks whether Virtualization.framework accepts aconfiguration.vmz_create()creates the Swift VM engine and returns aVMZHandle.vmz_start()starts the VM asynchronously.vmz_request_stop()asks the guest to shut down gracefully.vmz_force_stop()force-stops the VM asynchronously.vmz_run_loop()enters the platform run loop so async VM callbacks can fire.Callbacks report startup completion, graceful guest shutdown, and VM runtime errors.
Typical Lifecycle
VMZConfig, passing the pipe's read fd asread_pipe_fd.vmz_validate_config().vmz_create()and keep the returnedVMZHandle.vmz_start().vmz_run_loop()on the main thread.VMZErrorRef.See
src/vmz.zigfor the production caller andexamples/main.zigfor a smaller direct example.Ownership Rules
VMZConfigmust be valid null-terminated UTF-8 strings for theduration of the call that receives the struct.
VMZHandleis owned by the Swift bridge. Callers must treat it as opaque andnever dereference or free it directly.
VMZErrorRefvalues returned through callbacks or out-parameters are owned bythe caller and must be released with
vmz_error_free().vmz_error_message()returns memory owned by the error object. Do not free themessage pointer directly.
vmz_force_stop(), treat theVMZHandleas invalid.Building
The top-level
zig buildcompileslibvmz.swiftinto a static Swift library,links it into the Zig executable, links the required Swift runtime libraries and
Apple frameworks, then signs the executable with the virtualization entitlement.
To build the standalone example:
The VM host requires Apple silicon macOS with
Virtualization.frameworkavailable and the app signed with the virtualization entitlement.