Zig build system for libgcrypt
README.md

zig-build-libgcrypt #

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

libgpg-error comes from zig-build-libgpg-error.

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 libgcrypt = b.dependency("libgcrypt", .{
    .target = target,
    .optimize = optimize,
});

Import the translated libgcrypt API as a Zig module:

exe.root_module.addImport("gcrypt", libgcrypt.module("gcrypt"));

The module links libgcrypt, libgpg-error and libc transitively. Its declarations, including those from gpg-error.h, can be used with const gcrypt = @import("gcrypt");.

Alternatively, link the C library artifact directly:

exe.root_module.linkLibrary(libgcrypt.artifact("gcrypt"));

The artifact links libgpg-error transitively and propagates the installed gcrypt.h, gpg-error.h and gpgrt.h include path.

Supported targets #

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

The build corresponds to configuring upstream with --disable-asm, --disable-jent-support and --enable-random=getentropy:

  • Only portable C is compiled. There is no assembly, no hardware feature detection and no CPU-specific acceleration, and the bignum code uses the generic limb routines. This is noticeably slower than a distribution build.
  • All cipher, digest, KDF and public key algorithms are enabled.
  • Random numbers come only from getentropy. The jitter entropy collector is not built.

dumpsexp, hmac256, mpicalc, libgcrypt-config and libgcrypt.pc are not built.

Build and test #

Zig 0.17.0 is required.

zig build
zig build test

The default build installs libgcrypt.a, libgpg-error.a and the public headers under zig-out/. The test step runs these upstream tests: version, t-mpi-bit, hmac, keygrip and t-sexp, plus a Zig smoke test through the gcrypt 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 configure and Makefiles generate a few files at build time:

  • config.h is written by build.zig and holds only the values the sources consult, set per target. They match what configure finds on each target.
  • gcrypt.h is produced from src/gcrypt.h.in with the version substituted.
  • mod-source-info.h and mpi/mpi-asm-defs.h are what mpi/config.links produces when it selects the generic limb routines.
  • Upstream's cipher/gost-s-box.c is built for the build machine and produces gost-sb.h.

Upstream source #

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

License #

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