sigildocs

(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:

  1. Detect which packages have native code
  2. Generate an entrypoint that initializes all native modules
  3. Compile the native code
  4. Link a custom runtime

Exports

Emit a link-time reference to libsigil's ABI version symbol.

SIGILABIVERSION is defined in <sigil/sigil.h>. The macro SIGILABIFN(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 SIGILABIVERSION 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.

Check if a package has native code (C sources with native-init).

A package has native code if it has a libraries: field with at least one library that has c-sources and native-init.

Find all native packages from a list of loaded packages.

Returns a list of packages that have native code.

Collect all native-init function names from packages.

Returns a list of strings (C function names) in dependency order.

Collect all C include directories from native packages.

Returns an alist of (package-path . include-dirs).

Collect all C source files from native packages.

Returns an alist of (package-name . source-patterns).

Generate the C code for a custom runtime entrypoint.

This creates a main.c that:

  1. Includes sigil.h and necessary headers
  2. Declares extern for each native init function
  3. Creates the VM and calls native init functions
  4. Reads manifest from bundled archive (if available)
  5. For scripts: loads the script with (load)
  6. 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.

Generate common C headers for entrypoints.

Generate native module forward declarations.

Generate getexedir and path_join helper functions.

Generate main function prologue: exe_dir detection and VM creation.

Generate native module initialization calls.

Generate library path setup (adds ../lib relative to binary).

Generate main function epilogue: cleanup and return.

Generate 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 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.