Something went wrong. Try again.
Zig build system for libgcrypt
Something went wrong. Try again.
zig-build-libgcrypt build.zig
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455const std = @import("std");
const version = "1.12.4";const version_number = "0x010c04";
const src_sources = &.{ "src/visibility.c", "src/misc.c", "src/global.c", "src/sexp.c", "src/hwfeatures.c", "src/stdmem.c", "src/secmem.c", "src/missing-string.c", "src/fips.c", "src/context.c", "src/const-time.c",};
const cipher_sources = &.{ // Framework, modes, MACs and KEMs. "cipher/cipher.c", "cipher/cipher-cbc.c", "cipher/cipher-cfb.c", "cipher/cipher-ofb.c", "cipher/cipher-ctr.c", "cipher/cipher-aeswrap.c", "cipher/cipher-ccm.c", "cipher/cipher-cmac.c", "cipher/cipher-gcm.c", "cipher/cipher-poly1305.c", "cipher/cipher-ocb.c", "cipher/cipher-xts.c", "cipher/cipher-eax.c", "cipher/cipher-siv.c", "cipher/cipher-gcm-siv.c", "cipher/pubkey.c", "cipher/pubkey-util.c", "cipher/md.c", "cipher/mac.c", "cipher/mac-hmac.c", "cipher/mac-cmac.c", "cipher/mac-gmac.c", "cipher/mac-poly1305.c", "cipher/poly1305.c", "cipher/kem.c", "cipher/sntrup761.c", "cipher/kem-ecc.c", "cipher/mceliece6688128f.c", "cipher/kdf.c", "cipher/primegen.c", "cipher/hash-common.c", "cipher/dsa-common.c", "cipher/rsa-common.c", // Ciphers. "cipher/arcfour.c", "cipher/blowfish.c", "cipher/cast5.c", "cipher/des.c", "cipher/rijndael.c", "cipher/twofish.c", "cipher/serpent.c", "cipher/rfc2268.c", "cipher/seed.c", "cipher/camellia.c", "cipher/camellia-glue.c", "cipher/idea.c", "cipher/salsa20.c", "cipher/gost28147.c", "cipher/chacha20.c", "cipher/sm4.c", "cipher/aria.c", // Digests. "cipher/crc.c", "cipher/gostr3411-94.c", "cipher/stribog.c", "cipher/md4.c", "cipher/md5.c", "cipher/rmd160.c", "cipher/sha256.c", "cipher/sha512.c", "cipher/keccak.c", "cipher/tiger.c", "cipher/whirlpool.c", "cipher/blake2.c", "cipher/sm3.c", "cipher/sha1.c", // KDFs. "cipher/scrypt.c", // Public key algorithms. "cipher/dsa.c", "cipher/rsa.c", "cipher/elgamal.c", "cipher/ecc.c", "cipher/ecc-curves.c", "cipher/ecc-misc.c", "cipher/ecc-ecdh.c", "cipher/ecc-ecdsa.c", "cipher/ecc-eddsa.c", "cipher/ecc-gost.c", "cipher/ecc-sm2.c", "cipher/kyber.c", "cipher/dilithium.c", "cipher/pubkey-dilithium.c",};
const random_sources = &.{ "random/random.c", "random/random-csprng.c", "random/random-drbg.c", "random/random-system.c", "random/rndjent.c", "random/rndhw.c", "random/rndgetentropy.c",};
const mpi_sources = &.{ "mpi/mpi-add.c", "mpi/mpi-bit.c", "mpi/mpi-cmp.c", "mpi/mpi-div.c", "mpi/mpi-gcd.c", "mpi/mpi-inline.c", "mpi/mpi-inv.c", "mpi/mpi-mul.c", "mpi/mpi-mod.c", "mpi/mpi-pow.c", "mpi/mpi-mpow.c", "mpi/mpi-scan.c", "mpi/mpicoder.c", "mpi/mpih-div.c", "mpi/mpih-mul.c", "mpi/mpih-pow.c", "mpi/mpih-const-time.c", "mpi/mpiutil.c", "mpi/ec.c", "mpi/ec-ed25519.c", "mpi/ec-nist.c", "mpi/ec-hw-s390x.c",};
/// The portable limb routines that mpi/config.links links into mpi/ when/// assembly is disabled.const mpi_generic_modules = &.{ "mpih-add1", "mpih-sub1", "mpih-mul1", "mpih-mul2", "mpih-mul3", "mpih-lshift", "mpih-rshift",};
/// The module list that mpi/config.links writes for a build without/// assembly. mpiutil.c reports it through gcry_control.const mod_source_info = blk: { var info: []const u8 = "static char mod_source_info[] =\n"; for (mpi_generic_modules) |module| info = info ++ " \":generic/" ++ module ++ ".c\"\n"; break :blk info ++ " ;\n";};
const upstream_tests = &.{ "version", "t-mpi-bit", "hmac", "keygrip", "t-sexp",};
pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); const source = b.dependency("libgcrypt_src", .{}); const gpg_error = b.dependency("libgpg_error", .{ .target = target, .optimize = optimize, }).artifact("gpg-error");
// Every generated header lives in src/ of one directory, because the // upstream tests include "../src/gcrypt.h" relative to an include path. const generated = b.addWriteFiles(); _ = generated.addCopyFile(configHeader(b, target.result).getOutputFile(), "src/config.h"); _ = generated.addCopyFile(gcryptHeader(b, source).getOutputFile(), "src/gcrypt.h"); _ = generated.add("src/mod-source-info.h", mod_source_info); // src/mpi.h includes "../mpi/mpi-asm-defs.h", which config.links links // from the selected module directory. _ = generated.addCopyFile(source.path("mpi/generic/mpi-asm-defs.h"), "mpi/mpi-asm-defs.h"); _ = generated.addCopyFile(gostSboxHeader(b, source), "cipher/gost-sb.h"); const generated_src = generated.getDirectory().path(b, "src");
const library_module = b.createModule(.{ .target = target, .optimize = optimize, .link_libc = true, }); library_module.addIncludePath(generated_src); library_module.addIncludePath(generated.getDirectory().path(b, "mpi")); library_module.addIncludePath(generated.getDirectory().path(b, "cipher")); library_module.addIncludePath(source.path("src")); library_module.addIncludePath(source.path("mpi")); library_module.addCMacro("HAVE_CONFIG_H", "1"); library_module.linkLibrary(gpg_error); inline for (.{ src_sources, cipher_sources, random_sources, mpi_sources, &.{"compat/compat.c"} }) |files| { library_module.addCSourceFiles(.{ .root = source.path(""), .files = files }); } inline for (mpi_generic_modules) |module| { library_module.addCSourceFiles(.{ .root = source.path("mpi/generic"), .files = &.{module ++ ".c"} }); }
const library = b.addLibrary(.{ .name = "gcrypt", .linkage = .static, .root_module = library_module, }); library.installHeader(generated_src.path(b, "gcrypt.h"), "gcrypt.h"); // gcrypt.h includes gpg-error.h, so users of the artifact need both. library.installLibraryHeaders(gpg_error); b.installArtifact(library); b.installArtifact(gpg_error);
const translate_c = b.addTranslateC(.{ .root_source_file = generated_src.path(b, "gcrypt.h"), .target = target, .optimize = optimize, }); translate_c.addIncludePath(gpg_error.getEmittedIncludeTree()); const gcrypt_module = translate_c.addModule("gcrypt"); gcrypt_module.linkLibrary(library);
const test_step = b.step("test", "Run the libgcrypt tests"); inline for (upstream_tests) |test_name| { const test_module = b.createModule(.{ .target = target, .optimize = optimize, .link_libc = true, }); test_module.addIncludePath(generated_src); test_module.addCMacro("HAVE_CONFIG_H", "1"); test_module.addCSourceFiles(.{ .root = source.path("tests"), .files = &.{test_name ++ ".c"}, }); test_module.linkLibrary(library);
const test_exe = b.addExecutable(.{ .name = test_name, .root_module = test_module, }); const run_test = b.addRunArtifact(test_exe); run_test.expectExitCode(0); // Nix's Zig wrapper can make native executables appear foreign in its sandbox. run_test.skip_foreign_checks = true; test_step.dependOn(&run_test.step); }
const smoke_test = b.addTest(.{ .name = "gcrypt-smoke-test", .root_module = b.createModule(.{ .root_source_file = b.path("tests/smoke.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "gcrypt", .module = gcrypt_module }}, }), }); const run_smoke_test = b.addRunArtifact(smoke_test); run_smoke_test.skip_foreign_checks = true; test_step.dependOn(&run_smoke_test.step);}
/// Generates gcrypt.h the way configure substitutes gcrypt.h.in.fn gcryptHeader(b: *std.Build, source: *std.Build.Dependency) *std.Build.Step.ConfigHeader { return b.addConfigHeader(.{ .style = .{ .autoconf_at = source.path("src/gcrypt.h.in") }, .include_path = "gcrypt.h", }, .{ .configure_input = "src/gcrypt.h. Generated from gcrypt.h.in by build.zig.", .VERSION = version, .VERSION_NUMBER = version_number, .emacs_local_vars_begin = "Local Variables:", .emacs_local_vars_read_only = "buffer-read-only: t", .emacs_local_vars_end = "End:", });}
/// Runs upstream's gost-s-box on the build machine to produce the/// expanded GOST 28147-89 S-boxes that gost28147.c includes.fn gostSboxHeader(b: *std.Build, source: *std.Build.Dependency) std.Build.LazyPath { // On Linux the tool is linked statically against musl so that it runs // without the host's dynamic loader, which the Nix build sandbox does // not provide. const host = if (b.graph.host.result.os.tag == .linux) b.resolveTargetQuery(.{ .abi = .musl }) else b.graph.host; const tool = b.addExecutable(.{ .name = "gost-s-box", .root_module = b.createModule(.{ .target = host, .link_libc = true, }), }); tool.root_module.addCSourceFiles(.{ .root = source.path("cipher"), .files = &.{"gost-s-box.c"} });
const run = b.addRunArtifact(tool); return run.addOutputFileArg("gost-sb.h");}
/// Sources test these with #ifdef, so a false value must stay undefined/// rather than become `#define NAME 0`.fn defineIf(condition: bool) ?u1 { return if (condition) 1 else null;}
/// The subset of configure's config.h that the compiled sources consult,/// with the values configure finds on each supported target, for a build/// with --disable-asm, --disable-jent-support and --enable-random=getentropy./// All supported targets are LP64 and little-endian.fn configHeader(b: *std.Build, target: std.Target) *std.Build.Step.ConfigHeader { const arch = target.cpu.arch; const supported_os = switch (target.os.tag) { .linux => target.abi == .gnu or target.abi == .musl, .macos => true, else => false, }; if ((arch != .x86_64 and arch != .aarch64) or !supported_os) std.process.fatal( "libgcrypt: unsupported target {s}-{s}-{s}; supported are x86_64 and aarch64 with linux-gnu, linux-musl or macos", .{ @tagName(arch), @tagName(target.os.tag), @tagName(target.abi) }, ); const linux = target.os.tag == .linux; if (target.isGnuLibC() and target.os.versionRange().gnuLibCVersion().?.order(.{ .major = 2, .minor = 25, .patch = 0 }) == .lt) std.process.fatal("libgcrypt: glibc 2.25 or later is required for getentropy", .{});
return b.addConfigHeader(.{ .include_path = "config.h" }, .{ // src/types.h refuses to be included before config.h without it. ._GCRYPT_CONFIG_H_INCLUDED = 1, ._GNU_SOURCE = 1, ._DARWIN_C_SOURCE = 1, ._REENTRANT = 1, ._GCRYPT_IN_LIBGCRYPT = 1,
.PACKAGE_VERSION = version, .VERSION = version, .BUILD_COMMITID = "9a53b01742be48c50c3f4340b8e839010bc55d92", .BUILD_TIMESTAMP = "<none>", .FIPS_MODULE_VERSION = "", .EGD_SOCKET_NAME = "", .NAME_OF_DEV_RANDOM = "/dev/random", .NAME_OF_DEV_URANDOM = "/dev/urandom",
.ASM_DISABLED = 1, .GCRY_USE_VISIBILITY = 1, .GPG_ERR_SOURCE_DEFAULT = .GPG_ERR_SOURCE_GCRYPT, .CAMELLIA_EXT_SYM_PREFIX = ._gcry_,
.SIZEOF_UNSIGNED_SHORT = target.cTypeByteSize(.ushort), .SIZEOF_UNSIGNED_INT = target.cTypeByteSize(.uint), .SIZEOF_UNSIGNED_LONG = target.cTypeByteSize(.ulong), .SIZEOF_UNSIGNED_LONG_LONG = target.cTypeByteSize(.ulonglong), .SIZEOF_UINT64_T = 8, .SIZEOF_UNSIGNED___INT128 = 16, .SIZEOF_VOID_P = target.ptrBitWidth() / 8,
.HAVE_BUILTIN_BSWAP32 = 1, .HAVE_BUILTIN_BSWAP64 = 1, .HAVE_BUILTIN_CLZ = 1, .HAVE_BUILTIN_CLZL = 1, .HAVE_BUILTIN_CTZ = 1, .HAVE_BUILTIN_CTZL = 1, .HAVE_GCC_ASM_CFI_DIRECTIVES = 1, .HAVE_GCC_ASM_ELF_DIRECTIVES = defineIf(linux), .HAVE_GCC_ASM_VOLATILE_MEMORY = 1, .HAVE_GCC_ATTRIBUTE_ALIGNED = 1, .HAVE_GCC_ATTRIBUTE_MAY_ALIAS = 1, .HAVE_GCC_ATTRIBUTE_OPTIMIZE = 1, .HAVE_GCC_ATTRIBUTE_PACKED = 1, .HAVE_GCC_STORAGE_CLASS__THREAD = 1, .HAVE_SYNC_SYNCHRONIZE = 1, .HAVE_VLA = 1,
.HAVE_INTTYPES_H = 1, .HAVE_STDINT_H = 1, .HAVE_SYS_RANDOM_H = 1, .HAVE_SYS_AUXV_H = defineIf(linux),
.HAVE_ATEXIT = 1, .HAVE_CLOCK_GETTIME = 1, .HAVE_EXPLICIT_BZERO = defineIf(linux), .HAVE_FCNTL = 1, .HAVE_FTRUNCATE = 1, .HAVE_GETAUXVAL = defineIf(linux), .HAVE_GETENTROPY = 1, .HAVE_GETPAGESIZE = 1, .HAVE_GETRUSAGE = 1, .HAVE_GETTIMEOFDAY = 1, .HAVE_MEMMOVE = 1, .HAVE_MLOCK = 1, .HAVE_MMAP = 1, .HAVE_RAISE = 1, .HAVE_STPCPY = 1, .HAVE_STRCASECMP = 1, .HAVE_STRTOUL = 1, .HAVE_SYSCALL = 1, .HAVE_SYSCONF = 1, .HAVE_SYSLOG = 1, .HAVE_USHORT = 1, .HAVE_WAIT4 = 1, .HAVE_WAITPID = 1,
.USE_RNDGETENTROPY = 1, .LIBGCRYPT_CIPHERS = "arcfour:blowfish:cast5:des:aes:twofish:serpent:rfc2268:seed:camellia:idea:salsa20:gost28147:chacha20:sm4:aria", .LIBGCRYPT_DIGESTS = "crc:gostr3411-94::md4:md5:rmd160:sha1:sha256:sha512:sha3:tiger:whirlpool:stribog:blake2:sm3", .LIBGCRYPT_PUBKEY_CIPHERS = "dsa:elgamal:rsa:ecc:kyber:dilithium", .USE_AES = 1, .USE_ARCFOUR = 1, .USE_ARIA = 1, .USE_BLAKE2 = 1, .USE_BLOWFISH = 1, .USE_CAMELLIA = 1, .USE_CAST5 = 1, .USE_CHACHA20 = 1, .USE_CRC = 1, .USE_DES = 1, .USE_DILITHIUM = 1, .USE_DSA = 1, .USE_ECC = 1, .USE_ELGAMAL = 1, .USE_GOST28147 = 1, .USE_GOST_R_3411_12 = 1, .USE_GOST_R_3411_94 = 1, .USE_IDEA = 1, .USE_KYBER = 1, .USE_MD4 = 1, .USE_MD5 = 1, .USE_RFC2268 = 1, .USE_RMD160 = 1, .USE_RSA = 1, .USE_SALSA20 = 1, .USE_SCRYPT = 1, .USE_SEED = 1, .USE_SERPENT = 1, .USE_SHA1 = 1, .USE_SHA256 = 1, .USE_SHA3 = 1, .USE_SHA512 = 1, .USE_SM3 = 1, .USE_SM4 = 1, .USE_TIGER = 1, .USE_TWOFISH = 1, .USE_WHIRLPOOL = 1,
// Only consulted by the upstream tests. .HAVE_FLOCKFILE = 1, });}