trident
Native libraries: [[native]]
Declaring the C libraries a project links with [[native]] tables, how trident finds or compiles them, the native = true opt-in for dependencies that link native code, and building a C-callable library with --lib.
since 0.1.0-alpha.1linux
Description
A project whose code calls C functions names each C library it needs in a [[native]] table of its
trident.toml. trident finds the library, or compiles it from C sources you ship, and writes the
result into the build plan. The compiler never looks for libraries itself: it links exactly what the
plan lists. A file that declares foreign functions compiles only when its project has at least one
[[native]] table.
[[native]]
name = "z" # the name the code uses for this library
pkg = "zlib" # pkg-config package
lib = "z" # the -l name when pkg-config cannot help (default: name)
optional = false # true: build anyway when the library is missing
dynamic = false # true: load the library when the program first calls it
path = "" # a library file, a folder to search, or (dynamic) where to load from
sources = ["vendor/foo.c"] # C sources to compile into the program
cflags = "" # extra C compiler flags for sources
reentrant = false # the library tolerates being entered again while a callback from it waits
| Key | Meaning |
|---|---|
name |
Required. The name the code's foreign declarations use for this library: letters, digits, _, ., + and -, not starting with -. Unique within the manifest. c and m are not allowed: the C library and the maths library are always available. |
pkg |
A pkg-config package name, not starting with -. Optional. |
lib |
The name for -l when pkg-config does not know the library, with the same characters as name. Optional; the default is name. |
optional |
true lets the build go ahead without the library. Optional; default false. |
dynamic |
true loads the library while the program runs instead of linking it. Optional; default false. Cannot be combined with sources. |
path |
For a linked library: a library file, or a folder to search. For a dynamic one: a library file or a folder to load it from. Relative to the manifest. Optional. |
sources |
C source files (globs allowed) that trident compiles and links into the program. Optional. |
cflags |
Extra flags for compiling sources, separated by spaces. Optional. |
reentrant |
true declares that the library may be entered by another task while a callback from it is waiting. trident cannot check this. Optional; default false. |
How trident resolves a table
trident looks at the keys in this order and uses the first rule that applies.
sources: a vendored library. Each source is compiled with the C compiler ($CC, otherwisecc,clangorgcc) using-c -O2 -fPICand yourcflags, into its own object underbuild/native/<module>/<name>/, and every object is linked into the program. An object is compiled again only when the source, a header it includes, the flags or the compiler change. A source may be listed only once in a project, across all its tables. When a source does not compile, the compiler's own messages are shown, thenerror: vendored source 'bad.c' of [[native]] 'bad' (module 'app') failed to compile. When you build for the machine you are on, the objects are also linked into one shared library,build/native/<module>/<name>/lib<name>.so, which is whattrident run(the interpreters) loads, since an object file cannot be loaded on its own. It is rebuilt only when an object changes.dynamic = true: a library loaded at run time. Nothing is linked. Apaththat exists, or that contains a/, is passed on as an absolute path; it does not have to exist when you build. A bare file name such aslibssl.so.3is passed on unchanged, so the system's own library search finds it when the program runs.pathnames a file: a library file. That file is linked as it is.- Otherwise: a system library.
tridentaskspkg-config --libs <pkg>(thePKG_CONFIGenvironment variable replaces thepkg-configcommand; it may include leading arguments, such aspkg-config --static). If that fails or there is nopkg, it looks forlib<lib>.so,lib<lib>.a,lib<lib>.dylibor<lib>.dllinpathand in the C compiler's own library folders, and links with-l<lib>(with-L<path>whenpathis a folder).
A required library that is not found stops the build:
error: native library 'z' (pkg 'zlib') not found for module 'app'; install it or set optional = true
With optional = true the build goes ahead and records the library as unavailable. Calling one of its
functions then throws an exception that says the library was not available when the program was
built, so a program can check first and take another path.
Dependencies that link native code
Using a dependency that has [[native]] tables is a decision you make explicitly. Its [[dep]] entry
must say native = true, or nothing is built:
[[dep]]
path = "sqlite-bindings"
native = true
error: dependency 'sqlite-bindings' links native code ([[native]] name = "sqlite3"); add native = true to its [[dep]] entry in module 'app' to allow it
The rule holds at every step. If your project uses mid, and mid uses bindings, which has
[[native]] tables, then mid's own [[dep]] entry for bindings must say native = true, and the
error names mid. When two projects in the build both use bindings, each needs its own
native = true. Your project's own [[native]] tables need no opt-in.
Library names are program-wide
A foreign declaration finds its library by name alone, so a name means the same library everywhere
in the program. Several projects in one build may declare the same name only if trident resolves
their tables to the same kind, link flags, availability and reentrant mark; the library is then
linked once. A vendored library may be declared only once. Anything else is an error that names both
projects and the field that differs:
error: native library 'ssl' is declared by modules 'app' and 'net' with different kind ('dynamic' vs 'system'); library names are program-wide, so every declaration must agree
trident audit ends with a section that lists every project in the build that links native code,
how each of its tables was resolved, and whether every opt-in is in place; a missing opt-in or a
missing required library makes the audit fail. trident why <path> on a local dependency says
whether it links native code.
Building for another machine
With --target <triple>, trident looks for a library exactly as it does for the host, but with the
target's tools: <triple>-pkg-config (for Windows, x86_64-w64-mingw32-pkg-config; it honours
PKG_CONFIG_SYSROOT_DIR like any pkg-config), then path and the cross compiler's own library
folders. An optional library that is not found is recorded as unavailable. A required one that is not
found is still linked with -l<lib>, since the target's system may provide it, and trident prints a
warning that names the target. Vendored sources are compiled with clang -target <triple>, or with
<triple>-gcc when there is no clang; no shared library is made for the interpreters, which run
only on the machine you are on. A wasm32 target has no native libraries: there, foreign
functions are imports supplied by the page that loads the module. A table with only a name (no
pkg, lib, path or sources, and not dynamic) is allowed on wasm32; it permits the
project's foreign declarations and links nothing. Any other table is an error there:
error: [[native]] 'z' in module 'app': wasm32 has no native libraries; externs are imports
Building a library that C can call
trident build --lib shared builds a shared library instead of an executable, and
trident build --lib static a static one. Both also write a C header, by default to
build/<name>.h; --header <file> writes it somewhere else. --lib and --header are passed on to
the compiler unchanged and belong to build only.
A single file without a manifest
trident is the normal way to build code that calls C. To compile one file without a manifest, the
compiler's --native flag stands in for a [[native]] table.
Rules
nameis required, unique in the manifest, and may not becorm.nameandlibuse letters, digits,_,.,+and-and do not start with-;pkgdoes not start with-.dynamic = truetogether withsourcesis an error.- A dependency with
[[native]]tables needsnative = trueon the[[dep]]entry that uses it, at every step of the chain. The project's own tables need nothing. - A required library that cannot be found is an error that names the project; an optional one is recorded as unavailable.
- A library name means one library in the whole program: every project that declares it must agree, and a vendored library is declared once.
- A vendored source is listed once per project.
trident checkresolves every table but compiles no vendored sources.- On a
wasm32target only a table with nothing but anameis allowed; it links nothing. --libtakessharedorstaticand is accepted bytrident buildonly;--headerneeds--lib.
Examples
A program that links zlib from the system and a small C file shipped with it:
name = "packer"
entry = "main.lev"
sources = ["main.lev"]
[[native]]
name = "z"
pkg = "zlib"
[[native]]
name = "pt"
sources = ["csrc/pt.c"]
cflags = "-DNDEBUG"
A plugin that may be missing, loaded from a folder next to the program:
[[native]]
name = "plugin"
dynamic = true
optional = true
path = "plugins"
Inspecting and building:
trident audit
trident why sqlite-bindings
trident build . --lib shared --header include/packer.h
A binding keeps the C declarations in one namespace and puts ordinary Leviathan functions in front of them. The ordinary part is plain code that runs everywhere:
The Leviathan side of a binding
use io::console;
namespace Packer {
string describe(int raw, int packed) =>
"${raw} bytes -> ${packed} bytes (${(packed * 100) / raw}%)";
}
uses Packer;
console.writeln(describe(1000, 250));
1000 bytes -> 250 bytes (25%)
Notes
- Native code is for local dependencies only, by design: a repository dependency may not carry
[[native]]tables or C sources. Its stored copy holds just its declared sources, so any foreign declaration in it is refused by the capability check when your project compiles. Vendor such a package as a local (path) dependency to use its native code. trident add,removeandupdatekeep[[native]]tables andnative = truewhen they rewrite the manifest. Comments are not kept.- The compiler receives the result as plan rows: one row per project that may call C, and one row per
library (or per compiled object) with its kind, link flags, path, the shared library the
interpreters load for a vendored table, availability and the
reentrantmark.
See also
- The manifest: trident.toml — Every key of
trident.toml, how sources and assets are listed, and the three ways a project can choose its entry point. - Dependencies — Declaring local and repository dependencies, renaming a dependency's namespace with
as, development-only dependencies, and the rule that you may only use what you declared. - The trident commands — Every
tridentsubcommand with its arguments, grouped into building, managing dependencies, and publishing and auditing. - Cross-compilation: --target and per-target runtimes — Building executables for another operating system or processor, what the build needs for each target, and what a target can reject.