From 0726cc0299560b13c231e7aa0226524ac0e05bc7 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Thu, 8 Jan 2026 11:23:37 -0800 Subject: [PATCH] refactor coverage so it creates its own test exe built with llvm --- build.zig | 81 +++++++++++++++++++--------------------------- build/coverage.zig | 16 +++++---- 2 files changed, 43 insertions(+), 54 deletions(-) diff --git a/build.zig b/build.zig index 1fd2751..0975993 100644 --- a/build.zig +++ b/build.zig @@ -101,32 +101,36 @@ pub fn build(b: *std.Build) void { maxminddb.installHeadersDirectory(maxminddb_upstream.path("include"), "", .{}); 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); - const exe = b.addExecutable(.{ - .name = "wttr", - .root_module = b.createModule(.{ - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = optimize, - }), + const root_module = b.createModule(.{ + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, }); - - exe.root_module.addImport("httpz", httpz.module("httpz")); - exe.root_module.addImport("zeit", zeit.module("zeit")); - exe.root_module.addAnonymousImport("airports.dat", .{ + root_module.addImport("httpz", httpz.module("httpz")); + root_module.addImport("zeit", zeit.module("zeit")); + root_module.addAnonymousImport("airports.dat", .{ .root_source_file = openflights.path("data/airports.dat"), }); - exe.root_module.addOptions("build_options", build_options); - exe.root_module.addIncludePath(maxminddb_upstream.path("include")); - exe.root_module.addIncludePath(b.path("libs/phoon_14Aug2014")); - exe.root_module.addIncludePath(b.path("libs/sunriset")); - exe.root_module.addConfigHeader(maxminddb_config); - exe.linkLibrary(maxminddb); - exe.linkLibrary(phoon); - exe.linkLibrary(sunriset); - exe.linkLibC(); + 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 = &[_]*std.Build.Step.Compile{ + maxminddb, + phoon, + sunriset, + }; + const exe = b.addExecutable(.{ + .name = "wttr", + .root_module = root_module, + }); + configureCompilationUnit(exe, libs); b.installArtifact(exe); @@ -139,38 +143,19 @@ pub fn build(b: *std.Build) void { const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); - const download_geoip = b.option(bool, "download-geoip", "Download GeoIP database for tests") orelse false; - - const test_options = b.addOptions(); - test_options.addOption(bool, "download_geoip", download_geoip); - test_options.addOption([]const u8, "version", version); - - const tests = b.addTest(.{ - .root_module = b.createModule(.{ - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = optimize, - }), - }); - tests.root_module.addImport("httpz", httpz.module("httpz")); - tests.root_module.addImport("zeit", zeit.module("zeit")); - tests.root_module.addAnonymousImport("airports.dat", .{ - .root_source_file = openflights.path("data/airports.dat"), - }); - tests.root_module.addOptions("build_options", test_options); - tests.root_module.addIncludePath(maxminddb_upstream.path("include")); - tests.root_module.addIncludePath(b.path("libs/phoon_14Aug2014")); - tests.root_module.addIncludePath(b.path("libs/sunriset")); - tests.root_module.addConfigHeader(maxminddb_config); - tests.linkLibrary(maxminddb); - tests.linkLibrary(phoon); - tests.linkLibrary(sunriset); - tests.linkLibC(); + 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 - coverage.addCoverageStep(b, tests); + const cov_exe = coverage.addCoverageStep(b, root_module); + configureCompilationUnit(cov_exe, libs); +} + +fn configureCompilationUnit(compile: *std.Build.Step.Compile, libs: []const *std.Build.Step.Compile) void { + for (libs) |lib| compile.linkLibrary(lib); + compile.linkLibC(); } diff --git a/build/coverage.zig b/build/coverage.zig index 9524ea4..eb3020a 100644 --- a/build/coverage.zig +++ b/build/coverage.zig @@ -2,7 +2,7 @@ const builtin = @import("builtin"); const std = @import("std"); const Build = std.Build; -pub fn addCoverageStep(b: *Build, test_exe: *Build.Step.Compile) void { +pub fn addCoverageStep(b: *Build, root_module: *Build.Module) *Build.Step.Compile { //verify host requirements { const supported = builtin.os.tag == .linux and @@ -47,7 +47,7 @@ pub fn addCoverageStep(b: *Build, test_exe: *Build.Step.Compile) void { }; // Create download and coverage build steps - { + return blk: { const download_exe = b.addExecutable(.{ .name = "download-kcov", .root_module = b.createModule(.{ @@ -65,12 +65,15 @@ pub fn addCoverageStep(b: *Build, test_exe: *Build.Step.Compile) void { const css_file = b.pathJoin(&.{ b.build_root.path.?, "build", "bcov.css" }); run_coverage.addArg(b.fmt("--configure=css-file={s}", .{css_file})); run_coverage.addArg(coverage_dir); + const test_exe = b.addTest(.{ + .root_module = root_module, + // we need to set the test exe to use llvm as the self hosted backend + // does not support the data kcov needs + .use_llvm = true, + }); run_coverage.addArtifactArg(test_exe); run_coverage.step.dependOn(&test_exe.step); run_coverage.step.dependOn(&run_download.step); - // we need to set the test exe to use llvm as the self hosted backend - // does not support the data kcov needs - test_exe.use_llvm = true; if (coverage_threshold > 0) { const xml_path = b.fmt("{s}/test/cobertura.xml", .{coverage_dir}); @@ -80,7 +83,8 @@ pub fn addCoverageStep(b: *Build, test_exe: *Build.Step.Compile) void { } else { coverage_step.dependOn(&run_coverage.step); } - } + break :blk test_exe; + }; } pub const CheckCoverage = struct {