Zig build system for libgpg-error
README.md

zig-build-libgpg-error #

Zig 0.17 build package for libgpg-error 1.61. It builds libgpg-error directly from the official release tarball as a static library and exposes both the library artifact and raw translate-C bindings. Autotools, awk and a system C compiler are not required.

Use as a dependency #

Add this package to your project's build.zig.zon, then pass the target and optimization mode when requesting the dependency:

const libgpg_error = b.dependency("libgpg_error", .{
    .target = target,
    .optimize = optimize,
});

Import the translated libgpg-error API as a Zig module:

exe.root_module.addImport("gpg_error", libgpg_error.module("gpg_error"));

The module links libgpg-error and libc transitively. Its declarations can be used with const gpg_error = @import("gpg_error");.

Alternatively, link the C library artifact directly:

exe.root_module.linkLibrary(libgpg_error.artifact("gpg-error"));

The artifact propagates the installed gpg-error.h and gpgrt.h include path.

Supported targets #

x86_64 and aarch64, each with linux-gnu, linux-musl or macos. The build stops with an error for any other target. Windows is not supported.

The library is built with threading (POSIX threads) and without NLS. The gpg-error command line tool, gpgrt-config, gpg-error.pc and the m4 files are not built. The compiled-in configuration directory is upstream's default for the /usr/local prefix, /usr/local/etc.

Build and test #

Zig 0.17.0 is required.

zig build
zig build test

The default build installs the static library and public headers under zig-out/. The test step runs the generator's unit tests, checks the generator against the headers shipped in the tarball, and runs these upstream tests: t-b64, t-lock, t-name-value, t-printf, t-strerror, t-stringutils, t-strlist, t-syserror and t-version, plus a Zig smoke test through the gpg_error module.

The Nix flake provides the pinned Zig toolchain and a build check:

nix develop
nix flake check

Inside the Nix build sandbox Zig skips natively linked test executables, so on Linux the check also runs the test step for the static <arch>-linux-musl target.

How the build works #

Upstream's Makefile generates most of libgpg-error's headers at build time:

  • tools/generate.zig replaces the awk scripts (mkstrtable.awk, mkerrnos.awk, mkerrcodes*.awk) and mkerrcodes.c. The errno values come from running zig cc -E for the target.
  • Upstream's mkheader.c is built for the build machine and produces gpg-error.h (and its copy gpgrt.h). It reads the lock object layout from the matching src/syscfg/lock-obj-pub.*.h. aarch64 musl reuses the x86_64 musl file, because musl's pthread_mutex_t is the same on both.
  • config.h is written by build.zig and holds only the values the sources consult, set per target.

Upstream source #

The archive is pinned by its Zig package hash in build.zig.zon.

License #

Like libgpg-error itself, this package is licensed under the GNU Lesser General Public License, version 2.1 or later. See LICENSE.