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 enable_png = b.option(bool, "enable-png", "Enable PNG image generation (adds zigimg, freetype, and embedded fonts)") 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(bool, "enable_png", enable_png); const httpz = b.dependency("httpz", .{ .target = target, .optimize = optimize, }); const zeit = b.dependency("zeit", .{ .target = target, .optimize = optimize, }); const zigimg = if (enable_png) b.dependency("zigimg", .{ .target = target, .optimize = optimize, }) else null; const freetype = if (enable_png) b.dependency("ghostty", .{ .target = target, .optimize = optimize, }).builder.dependency("freetype", .{ .target = target, .optimize = optimize, }) else null; const jetbrains_mono = if (enable_png) b.dependency("jetbrains_mono", .{}) else null; const nerd_fonts = if (enable_png) b.dependency("nerd_fonts_symbols_only", .{}) else null; 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")); if (zigimg) |dep| root_module.addImport("zigimg", dep.module("zigimg")); if (freetype) |dep| root_module.addImport("freetype", dep.module("freetype")); root_module.addAnonymousImport("airports.dat", .{ .root_source_file = openflights.path("data/airports.dat"), }); if (jetbrains_mono) |dep| root_module.addAnonymousImport("JetBrainsMono-Regular.ttf", .{ .root_source_file = dep.path("fonts/ttf/JetBrainsMono-Regular.ttf"), }); if (enable_png) root_module.addAnonymousImport("LexiGulim.ttf", .{ .root_source_file = b.path("libs/2914-LexiGulim090423.ttf"), }); if (nerd_fonts) |dep| root_module.addAnonymousImport("SymbolsNerdFont-Regular.ttf", .{ .root_source_file = dep.path("SymbolsNerdFont-Regular.ttf"), }); 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); var libs_buf: [4]*std.Build.Step.Compile = undefined; libs_buf[0] = maxminddb; libs_buf[1] = phoon; libs_buf[2] = sunriset; var libs_len: usize = 3; if (freetype) |dep| { libs_buf[libs_len] = dep.artifact("freetype"); libs_len += 1; } const libs = libs_buf[0..libs_len]; 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); }