(sigil build native)
Native Package Support
This module provides functions for detecting and building native packages (packages with C code) during the bundling process.
When an app depends on packages with native code (c-sources in their library definitions), we need to:
- Detect which packages have native code
- Generate an entrypoint that initializes all native modules
- Compile the native code
- Link a custom runtime
Exports
emit-abi-guardprocedureEmit a link-time reference to libsigil's ABI version symbol.
SIGIL_ABI_VERSION is defined in <sigil/sigil.h>. The macro SIGIL_ABI_FN(N) expands to sigil_abi_v<N>, a marker function that libsigil defines exactly at the version it was built with. The static function-pointer here forces the linker to resolve that symbol; if libsigil was compiled at a different ABI version, the link fails with an undefined-reference error instead of silently producing a binary that links a stale, mismatched archive.
attribute((used)) prevents -Wl,--gc-sections and -Wl,-dead_strip from removing the reference in stripped release builds. The #ifdef SIGIL_ABI_VERSION wrapper keeps the entrypoint backwards-compatible with sigil-lib versions that predate the SIGIL_ABI_VERSION macro (sigil-lib commit 69ea6a70). Without the guard, the emitted reference fails to compile against older sigil-lib headers, which silently breaks every downstream consumer pinned to a pre-0.13 sigil release as soon as the build binary advances. With the guard, old sigil-lib gets no protection but the build still succeeds; new sigil-lib gets the link-time enforcement.
package-has-native-code?procedureCheck for eager native initializers or deferred per-VM thunk contracts.
A library needs native-init and either C sources or an explicit native-modules declaration for a custom/prebuilt archive.
find-native-packagesprocedureFind all native packages from a list of loaded packages.
Returns a list of packages that have native code.
find-eager-native-packagesprocedurePackages whose archives belong in an eager executable link.
collect-deferred-native-librariesprocedureAll bundled deferred contracts, including unselected unavailable surfaces.
collect-native-init-functionsprocedureCollect all native-init function names from packages.
Returns a list of strings (C function names) in dependency order.
collect-eager-native-init-functionsprocedureInitializers backed by eager C objects, excluding generated thunk sets.
collect-native-include-dirsprocedureCollect all C include directories from native packages.
Returns an alist of (package-path . include-dirs).
collect-native-sourcesprocedureCollect all C source files from native packages.
Returns an alist of (package-name . source-patterns).
collect-native-link-flagsprocedureCollect all linker flags from native packages.
Returns a flat list of linker flags in dependency order.
link-flags: may be either a plain list of strings (static, no platform branching) OR a (lambda (ctx) ...) procedure that returns a list of strings. Procedural link-flags are called with ctx so the package can branch on (target-os ctx) / (target-arch ctx) — necessary for cross-compile configs where the host OS and the target OS differ. Without this, a Linux host cross-compiling for Windows would leak -lX11 / -lasound into the zig cc link line. See test-link-flags.sgl.
generate-native-entrypointprocedureGenerate the C code for a custom runtime entrypoint.
This creates a main.c that:
- Includes sigil.h and necessary headers
- Declares extern for each native init function
- Creates the VM and calls native init functions
- Reads manifest from bundled archive (if available)
- For scripts: loads the script with (load)
- For programs: imports entry module and calls (main)
init-functions: list of C function name strings entry-module: string like "(my-app main)" - fallback if no manifest
Returns the generated C code as a string.
entrypoint-headersprocedureGenerate common C headers for entrypoints.
entrypoint-native-declarationsprocedureGenerate native module forward declarations.
entrypoint-helper-functionsprocedureGenerate get_exe_dir and path_join helper functions.
entrypoint-main-prologueprocedureGenerate main function prologue: exe_dir detection and VM creation.
entrypoint-native-initprocedureGenerate native module initialization calls.
entrypoint-lib-path-setupprocedureGenerate library path setup (adds ../lib relative to binary).
entrypoint-main-epilogueprocedureGenerate main function epilogue: cleanup and return.
generate-dev-entrypointprocedureGenerate a dev build entrypoint with native module support.
This is similar to generate-native-entrypoint but includes DEV_BUILD features like boot-eval command and dev library path setup.
init-functions: list of C function name strings entry-module: string like "(sigil cli)"
Returns the generated C code as a string.
generate-test-entrypointprocedureGenerate a test harness entrypoint with native module support.
This generates a standalone test runner that initializes native modules and then calls (sigil test cli) test-main with command-line arguments.
init-functions: list of C function names to call for native init
Returns the generated C code as a string.