Something went wrong. Try again.
Zig build system for libgpg-error
Something went wrong. Try again.
zig-build-libgpg-error build.zig
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377const std = @import("std");
const version = "1.61";const version_number = "0x013d00";
const libgpg_error_sources = &.{ "argparse.c", "b64dec.c", "b64enc.c", "code-from-errno.c", "code-to-errno.c", "estream-printf.c", "estream.c", "init.c", "logging.c", "name-value.c", "posix-lock.c", "posix-thread.c", "spawn-posix.c", "stringutils.c", "strerror.c", "strlist.c", "strsource.c", "syscall-clamp.c", "sysutils.c", "version.c", "visibility.c",};
const upstream_tests = &.{ "t-b64", "t-lock", "t-name-value", "t-printf", "t-strerror", "t-stringutils", "t-strlist", "t-syserror", "t-version",};
pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); const source = b.dependency("libgpg_error_src", .{}); const src = source.path("src");
const generator = b.addExecutable(.{ .name = "generate", .root_module = b.createModule(.{ .root_source_file = b.path("tools/generate.zig"), .target = b.graph.host, }), });
const config_h = configHeader(b, target.result).getOutputFile(); // Every generated header lives in src/ of one directory, because the // upstream tests include "../src/gpgrt.h" relative to an include path. const generated = b.addWriteFiles(); _ = generated.addCopyFile(config_h, "src/config.h"); inline for (.{ .{ "err-codes-sym.h", "err-codes.h.in", &.{ "textidx=2", "nogettext" } }, .{ "err-sources-sym.h", "err-sources.h.in", &.{ "textidx=2", "nogettext" } }, .{ "errnos-sym.h", "errnos.in", &.{ "textidx=2", "nogettext", "prefix=GPG_ERR_", "namespace=errnos_" } }, }) |table| { const output = generate(b, generator, "strtable", src.path(b, table[1]), table[2]); _ = generated.addCopyFile(output, "src/" ++ table[0]); } _ = generated.addCopyFile(generate(b, generator, "code-to-errno", src.path(b, "errnos.in"), &.{}), "src/code-to-errno.h"); _ = generated.addCopyFile(codeFromErrno(b, generator, src, target.result), "src/code-from-errno.h"); const gpg_error_h = mkheader(b, source, config_h, target.result); _ = generated.addCopyFile(gpg_error_h, "src/gpg-error.h"); _ = generated.addCopyFile(gpg_error_h, "src/gpgrt.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(src); library_module.addCMacro("HAVE_CONFIG_H", "1"); // Upstream's defaults for the /usr/local prefix. NLS is disabled, so // LOCALEDIR only matters to code that is compiled out. library_module.addCMacro("LOCALEDIR", "\"/usr/local/share/locale\""); library_module.addCMacro("SYSCONFDIR", "\"/usr/local/etc\""); library_module.addCSourceFiles(.{ .root = src, .files = libgpg_error_sources, .flags = &.{"-fvisibility=hidden"}, });
const library = b.addLibrary(.{ .name = "gpg-error", .linkage = .static, .root_module = library_module, }); library.installHeader(generated_src.path(b, "gpg-error.h"), "gpg-error.h"); library.installHeader(generated_src.path(b, "gpgrt.h"), "gpgrt.h"); b.installArtifact(library);
const translate_c = b.addTranslateC(.{ .root_source_file = generated_src.path(b, "gpg-error.h"), .target = target, .optimize = optimize, }); const gpg_error_module = translate_c.addModule("gpg_error"); gpg_error_module.linkLibrary(library);
const test_step = b.step("test", "Run the generator and libgpg-error tests");
const generator_tests = b.addTest(.{ .name = "generate-test", .root_module = b.createModule(.{ .root_source_file = b.path("tools/generate.zig"), .target = b.graph.host, .optimize = optimize, }), }); test_step.dependOn(&b.addRunArtifact(generator_tests).step);
const parity_files = b.addWriteFiles(); _ = parity_files.addCopyFile(b.path("tools/generate.zig"), "generate.zig"); inline for (.{ "err-codes.h.in", "err-codes.h", "err-sources.h.in", "err-sources.h" }) |name| { _ = parity_files.addCopyFile(src.path(b, name), name); } const parity_tests = b.addTest(.{ .name = "generate-parity-test", .root_module = b.createModule(.{ .root_source_file = parity_files.addCopyFile(b.path("tools/parity_test.zig"), "parity_test.zig"), .target = b.graph.host, .optimize = optimize, }), }); test_step.dependOn(&b.addRunArtifact(parity_tests).step);
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 = "gpg-error-smoke-test", .root_module = b.createModule(.{ .root_source_file = b.path("tests/smoke.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "gpg_error", .module = gpg_error_module }}, }), }); const run_smoke_test = b.addRunArtifact(smoke_test); run_smoke_test.skip_foreign_checks = true; test_step.dependOn(&run_smoke_test.step);}
fn generate( b: *std.Build, generator: *std.Build.Step.Compile, command: []const u8, input: std.Build.LazyPath, options: []const []const u8,) std.Build.LazyPath { const run = b.addRunArtifact(generator); run.addArg(command); run.addFileArg(input); const output = run.addOutputFileArg(b.fmt("{s}.h", .{command})); run.addArgs(options); return output;}
/// Expands the errno names with the target's C preprocessor, which is where/// upstream's Makefile runs $(CPP) between mkerrcodes1.awk and mkerrcodes.c.fn codeFromErrno( b: *std.Build, generator: *std.Build.Step.Compile, src: std.Build.LazyPath, target: std.Target,) std.Build.LazyPath { const probe = generate(b, generator, "errno-probe", src.path(b, "errnos.in"), &.{}); const preprocess = b.addSystemCommand(&.{ b.graph.zig_exe, "cc", "-E", "-P", "-target" }); preprocess.addArg(target.zigTriple(b.allocator) catch @panic("OOM")); preprocess.addArg("-xc"); preprocess.addFileArg(probe); preprocess.addArg("-o"); const preprocessed = preprocess.addOutputFileArg("errno-probe.i"); return generate(b, generator, "code-from-errno", preprocessed, &.{});}
/// Runs upstream's mkheader to produce gpg-error.h. It reads its includes/// next to the template, including syscfg/lock-obj-pub.<triplet>.h, so the/// inputs are gathered into one directory under the triplet mkheader expects.fn mkheader( b: *std.Build, source: *std.Build.Dependency, config_h: std.Build.LazyPath, target: std.Target,) std.Build.LazyPath { const triplet = hostTriplet(b, target); const inputs = b.addWriteFiles(); inline for (.{ "err-codes.h.in", "err-sources.h.in", "errnos.in", "w32-add.h" }) |name| { _ = inputs.addCopyFile(source.path("src/" ++ name), name); } _ = inputs.addCopyFile( source.path(b.fmt("src/syscfg/lock-obj-pub.{s}.h", .{triplet.lock_obj})), b.fmt("syscfg/lock-obj-pub.{s}.h", .{triplet.name}), ); const template = inputs.addCopyFile(source.path("src/gpg-error.h.in"), "gpg-error.h.in");
// mkheader only runs on the build machine. On Linux it 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 = "mkheader", .root_module = b.createModule(.{ .target = host, .link_libc = true, }), }); tool.root_module.addCSourceFiles(.{ .root = source.path("src"), .files = &.{"mkheader.c"} });
const run = b.addRunArtifact(tool); run.addArgs(&.{ "--cross", triplet.name }); run.addFileArg(template); run.addFileArg(config_h); run.addArgs(&.{ version, version_number }); return run.captureStdOut(.{ .basename = "gpg-error.h" });}
const HostTriplet = struct { /// The autoconf host triplet that mkheader names the target by. name: []const u8, /// The triplet of the upstream lock object header to use for it. lock_obj: []const u8,};
fn hostTriplet(b: *std.Build, target: std.Target) HostTriplet { const arch = target.cpu.arch; if (arch == .x86_64 or arch == .aarch64) { const arch_name = @tagName(arch); switch (target.os.tag) { .linux => switch (target.abi) { .gnu => { const name = b.fmt("{s}-unknown-linux-gnu", .{arch_name}); return .{ .name = name, .lock_obj = name }; }, // Upstream has no aarch64 musl header. musl defines // pthread_mutex_t identically on all 64-bit architectures, // so the x86_64 one describes aarch64 as well. .musl => return .{ .name = b.fmt("{s}-unknown-linux-musl", .{arch_name}), .lock_obj = "x86_64-unknown-linux-musl", }, else => {}, }, .macos => { const name = b.fmt("{s}-apple-darwin", .{arch_name}); return .{ .name = name, .lock_obj = name }; }, else => {}, } } std.process.fatal( "libgpg-error: 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) }, );}
/// 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 and/// mkheader consult, with the values configure finds on each supported/// target. All supported targets are LP64.fn configHeader(b: *std.Build, target: std.Target) *std.Build.Step.ConfigHeader { const glibc = target.isGnuLibC(); const glibc_version = if (glibc) target.os.versionRange().gnuLibCVersion() else null; const glibcAtLeast = struct { fn f(v: ?std.SemanticVersion, minor: u32) bool { return if (v) |gv| gv.order(.{ .major = 2, .minor = minor, .patch = 0 }) != .lt else false; } }.f;
return b.addConfigHeader(.{ .include_path = "config.h" }, .{ ._GNU_SOURCE = 1, ._DARWIN_C_SOURCE = 1,
.PACKAGE = "libgpg-error", .PACKAGE_VERSION = version, .BUILD_COMMITID = "687c9da501c21a64f7dcedfa73146a4670009cea", .BUILD_TIMESTAMP = "<none>",
.GPGRT_USE_VISIBILITY = 1, .USE_POSIX_THREADS = 1, .USE_POSIX_THREADS_FROM_LIBC = 1,
.SIZEOF_INT = target.cTypeByteSize(.int), .SIZEOF_UNSIGNED_LONG = target.cTypeByteSize(.ulong), .SIZEOF_VOID_P = target.ptrBitWidth() / 8, .REPLACEMENT_FOR_OFF_T = "long",
.HAVE_INTMAX_T = 1, .HAVE_LONG_DOUBLE = 1, .HAVE_LONG_LONG_INT = 1, .HAVE_PTRDIFF_T = 1, .HAVE_UINTMAX_T = 1,
.HAVE_POLL_H = 1, .HAVE_PWD_H = 1, .HAVE_SIGNAL_H = 1, .HAVE_STDINT_H = 1, .HAVE_SYS_SELECT_H = 1, .HAVE_SYS_TIME_H = 1, .HAVE_SYS_TYPES_H = 1,
.HAVE_FLOCKFILE = 1, .HAVE_GETPWNAM = 1, .HAVE_GETPWUID = 1, .HAVE_GETRLIMIT = 1, .HAVE_INET_PTON = 1, .HAVE_LANGINFO_CODESET = 1, .HAVE_LANGINFO_THOUSEP = 1, .HAVE_SETENV = 1, .HAVE_SNPRINTF = 1, .HAVE_STAT = 1, .HAVE_STPCPY = 1, .HAVE_STRERROR_R = 1,
.HAVE_MEMRCHR = defineIf(target.os.tag == .linux), .STRERROR_R_CHAR_P = defineIf(glibc), .HAVE_GETDENTS64 = defineIf(glibcAtLeast(glibc_version, 30)), .HAVE_SYS_SINGLE_THREADED_H = defineIf(glibcAtLeast(glibc_version, 32)), .HAVE_CLOSEFROM = defineIf(glibcAtLeast(glibc_version, 34)),
// Only consulted by the upstream tests. .PACKAGE_NAME = "libgpg-error", .HAVE_STDLIB_H = 1, .HAVE_VASPRINTF = 1,
// configure's AH_BOTTOM block. ._ESTREAM_PRINTF_REALLOC = ._gpgrt_realloc, ._ESTREAM_PRINTF_EXTRA_INCLUDE = "gpgrt-int.h", .GPG_ERR_ENABLE_GETTEXT_MACROS = 1, .GPGRT_ENABLE_ES_MACROS = 1, .GPGRT_ENABLE_LOG_MACROS = 1, .GPGRT_ENABLE_ARGPARSE_MACROS = 1, .GPGRT_STD_COPYRIGHT_LINE = "Copyright (C) 2025 g10 Code GmbH", });}