Zig build system for gmp
README.md

zig-build-gmp #

Zig 0.17 build package for the GNU MP Library 6.3.0. It builds gmp directly from the official release tarball as a static library and exposes both the library artifact and raw translate-C bindings. Autotools, m4 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 gmp = b.dependency("gmp", .{
    .target = target,
    .optimize = optimize,
});

Import the translated gmp API as a Zig module:

exe.root_module.addImport("gmp", gmp.module("gmp"));

The module links gmp and libc transitively and can be used with const gmp = @import("gmp");. The mpz_*, mpq_* and mpf_* names work as in C. translate-C cannot expand the token-pasting __MPN() macro behind the mpn_* names, so call those functions by their symbol names instead (gmp.__gmpn_sec_powm for mpn_sec_powm). The version string is gmp.__gmp_version.

Alternatively, link the C library artifact directly:

exe.root_module.linkLibrary(gmp.artifact("gmp"));

The artifact propagates the installed gmp.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 build corresponds to configuring upstream with --disable-assembly --disable-cxx --disable-shared:

  • Only the portable C code in mpn/generic is compiled, with the generic tuning thresholds from mpn/generic/gmp-mparam.h. There is no assembly and no CPU-specific tuning, so this is noticeably slower than a distribution build. The side-channel silent mpn_sec_* functions are included.
  • All of mpz, mpq, mpf, mpn, the random number functions and the gmp_printf and gmp_scanf families are built. The gmp_obstack_* functions exist only on glibc, as upstream.
  • gmpxx, the documentation, the demos and gmp.pc are not built.

Build and test #

Zig 0.17.0 is required.

zig build
zig build test

The default build installs libgmp.a and gmp.h under zig-out/. The test step runs a Zig smoke test through the gmp module: mpz arithmetic, an mpz_powm known answer and an mpn_sec_powm known answer.

The Nix flake provides the pinned Zig toolchain and two checks:

nix develop
nix flake check
  • checks.default runs the build and the test step. Inside the Nix build sandbox Zig skips natively linked test executables, so on Linux it also runs the test step for the static <arch>-linux-musl target.
  • checks.symbols builds the same tarball with upstream's own configure and Makefiles (--disable-assembly) and requires that its libgmp.a exports exactly the same symbols as the Zig build. It also requires the same gmp.h, apart from the line Zig's ConfigHeader adds at the top and the recorded compiler name and flags (__GMP_CC, __GMP_CFLAGS).

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 library sources consult, set per target. They match what configure finds on each target.
  • gmp.h is produced from gmp-h.in with 64-bit limbs and no nails, which holds for every supported target.
  • gmp-mparam.h is mpn/generic/gmp-mparam.h.
  • Upstream's gen-fac, gen-sieve, gen-fib, gen-bases, gen-trialdivtab, gen-jacobitab and gen-psqr are built for the build machine and produce the constant tables, including mpn/fib_table.c and mpn/mp_bases.c.
  • Each mpn function is compiled from mpn/generic with -DOPERATION_<name>. Some functions share a source file and pick their variant through that define, for example the eight logical operations in logops_n.c.

Upstream source #

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

License #

Like the GNU MP Library itself, this package is licensed under the GNU Lesser General Public License, version 3 or later, or the GNU General Public License, version 2 or later, or both in parallel. See LICENSE.