- Zig 82%
- Nix 18%
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/genericis compiled, with the generic tuning thresholds frommpn/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 silentmpn_sec_*functions are included. - All of
mpz,mpq,mpf,mpn, the random number functions and thegmp_printfandgmp_scanffamilies are built. Thegmp_obstack_*functions exist only on glibc, as upstream. gmpxx, the documentation, the demos andgmp.pcare 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.defaultruns 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-musltarget.checks.symbolsbuilds the same tarball with upstream's own configure and Makefiles (--disable-assembly) and requires that itslibgmp.aexports exactly the same symbols as the Zig build. It also requires the samegmp.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.his written bybuild.zigand holds only the values the library sources consult, set per target. They match what configure finds on each target.gmp.his produced fromgmp-h.inwith 64-bit limbs and no nails, which holds for every supported target.gmp-mparam.hismpn/generic/gmp-mparam.h.- Upstream's
gen-fac,gen-sieve,gen-fib,gen-bases,gen-trialdivtab,gen-jacobitabandgen-psqrare built for the build machine and produce the constant tables, includingmpn/fib_table.candmpn/mp_bases.c. - Each
mpnfunction is compiled frommpn/genericwith-DOPERATION_<name>. Some functions share a source file and pick their variant through that define, for example the eight logical operations inlogops_n.c.
Upstream source #
- Version:
6.3.0 - Archive: https://gmplib.org/download/gmp/gmp-6.3.0.tar.xz
- SHA-256:
a3c2b80201b89e68616f4ad30bc66aee4927c3ce50e33929ca819d5c43538898
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.