const std = @import("std"); const GitVersion = @import("build/GitVersion.zig"); const Coverage = @import("build/Coverage.zig"); pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); const version = GitVersion.getVersion(b, .{}); const download_geoip = b.option(bool, "download-geoip", "Download GeoIP database for tests") orelse false; const build_options = b.addOptions(); build_options.addOption([]const u8, "version", version); build_options.addOption(bool, "download_geoip", download_geoip); build_options.addOption([]const u8, "cache_dir", resolveCacheDir(b)); const httpz = b.dependency("httpz", .{ .target = target, .optimize = optimize, }); const zeit = b.dependency("zeit", .{ .target = target, .optimize = optimize, }); const srf = b.dependency("srf", .{ .target = target, .optimize = optimize, }); const openflights = b.dependency("openflights", .{}); const maxminddb_upstream = b.dependency("maxminddb", .{}); // Build sunriset as a static library const sunriset = b.addLibrary(.{ .name = "sunriset", .linkage = .static, .root_module = b.createModule(.{ .target = target, .optimize = optimize, .link_libc = true, }), }); sunriset.root_module.addIncludePath(b.path("libs/sunriset")); sunriset.root_module.addCSourceFiles(.{ .root = b.path("libs/sunriset"), .files = &.{ "sunriset.c", }, .flags = &.{ "-D_DEFAULT_SOURCE", "-DSUNRISET_NO_MAIN" }, }); sunriset.root_module.linkSystemLibrary("m", .{}); // Build phoon as a static library const phoon = b.addLibrary(.{ .name = "phoon", .linkage = .static, .root_module = b.createModule(.{ .target = target, .optimize = optimize, .link_libc = true, }), }); phoon.root_module.addIncludePath(b.path("libs/phoon_14Aug2014")); phoon.root_module.addCSourceFiles(.{ .root = b.path("libs/phoon_14Aug2014"), .files = &.{ "astro.c", "date_parse.c", }, .flags = &.{ "-std=c99", "-D_DEFAULT_SOURCE" }, }); phoon.root_module.linkSystemLibrary("m", .{}); // Build libmaxminddb as a static library const maxminddb = b.addLibrary(.{ .name = "maxminddb", .linkage = .static, .root_module = b.createModule(.{ .target = target, .optimize = optimize, .link_libc = true, }), }); // Generate maxminddb_config.h const maxminddb_config = b.addConfigHeader(.{ .style = .blank, .include_path = "maxminddb_config.h", }, .{ .PACKAGE_VERSION = "1.11.0", // Represent mmdb_uint128_t as a 16-byte array rather than // `unsigned int __attribute__((__mode__(TI)))`. // // translate-c cannot represent the `mode(TI)` attribute, so with // MMDB_UINT128_USING_MODE Zig computed sizeof(MMDB_entry_data_s) == 32 // / alignof == 8 while clang used 48 / 16. Every MMDB_get_value call // then wrote `entry_data->offset` (at C offset 32) past the end of the // 32-byte variable Zig had reserved on the stack, corrupting whatever // happened to be adjacent. // // With a byte array both sides agree (40 / 8). Nothing here reads // uint128-typed database fields, so this only affects layout. .MMDB_UINT128_IS_BYTE_ARRAY = 1, }); maxminddb.root_module.addConfigHeader(maxminddb_config); maxminddb.root_module.addIncludePath(maxminddb_upstream.path("include")); maxminddb.root_module.addIncludePath(maxminddb_upstream.path("src")); maxminddb.root_module.addCSourceFiles(.{ .root = maxminddb_upstream.path(""), .files = &.{ "src/data-pool.c", "src/maxminddb.c", }, .flags = &.{}, }); maxminddb.installHeadersDirectory(maxminddb_upstream.path("include"), "", .{}); const root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, // Zig 0.16 moved libc linkage onto the module; `Compile.linkLibC` is gone. .link_libc = true, }); root_module.addImport("httpz", httpz.module("httpz")); root_module.addImport("zeit", zeit.module("zeit")); root_module.addImport("srf", srf.module("srf")); root_module.addAnonymousImport("airports.dat", .{ .root_source_file = openflights.path("data/airports.dat"), }); root_module.addOptions("build_options", build_options); root_module.addIncludePath(maxminddb_upstream.path("include")); root_module.addIncludePath(b.path("libs/phoon_14Aug2014")); root_module.addIncludePath(b.path("libs/sunriset")); root_module.addConfigHeader(maxminddb_config); const libs: []const *std.Build.Step.Compile = &.{ maxminddb, phoon, sunriset }; const exe = b.addExecutable(.{ .name = "wttr", .root_module = root_module, }); configureCompilationUnit(exe, libs); b.installArtifact(exe); const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); if (b.args) |args| { run_cmd.addArgs(args); } const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); const tests = b.addTest(.{ .root_module = root_module }); configureCompilationUnit(tests, libs); const run_tests = b.addRunArtifact(tests); const test_step = b.step("test", "Run tests"); test_step.dependOn(&run_tests.step); // Coverage step const cov_step = Coverage.addCoverageStep(b, root_module, exe.name); configureCompilationUnit(cov_step.test_exe, libs); } fn configureCompilationUnit(compile: *std.Build.Step.Compile, libs: []const *std.Build.Step.Compile) void { // Zig 0.16: linkLibrary moved to Module, and libc linkage is a module // option set where `root_module` is created. for (libs) |lib| compile.root_module.linkLibrary(lib); } /// Resolves the runtime cache directory, mirroring `Config.load`'s precedence /// (`WTTR_CACHE_DIR`, else `${XDG_CACHE_HOME:-$HOME/.cache}/wttr`). /// /// Tests need this because Zig 0.16 removed process-environment access outside /// of `main`; build scripts kept it. Without this, `Config.loadForTest` would /// fall back to `HOME = "/tmp"` and write test artifacts -- including the /// GeoLite2 database -- into `/tmp`. fn resolveCacheDir(b: *std.Build) []const u8 { const env = &b.graph.environ_map; if (env.get("WTTR_CACHE_DIR")) |dir| return b.dupe(dir); const xdg_cache = if (env.get("XDG_CACHE_HOME")) |x| b.dupe(x) else if (env.get("HOME")) |home| b.pathJoin(&.{ home, ".cache" }) else // No home directory to work from; keep artifacts inside the build // cache rather than falling back to a world-writable location. b.pathFromRoot(".zig-cache"); return b.pathJoin(&.{ xdg_cache, "wttr" }); }