refactor coverage so it creates its own test exe built with llvm

This commit is contained in:
Emil Lerch 2026-01-08 11:23:37 -08:00
parent d3c227cf33
commit 0726cc0299
Signed by: lobo
GPG key ID: A7B62D657EF764F8
2 changed files with 43 additions and 54 deletions

View file

@ -101,32 +101,36 @@ pub fn build(b: *std.Build) void {
maxminddb.installHeadersDirectory(maxminddb_upstream.path("include"), "", .{}); maxminddb.installHeadersDirectory(maxminddb_upstream.path("include"), "", .{});
const version = GitVersion.getVersion(b, .{}); 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(); const build_options = b.addOptions();
build_options.addOption([]const u8, "version", version); build_options.addOption([]const u8, "version", version);
build_options.addOption(bool, "download_geoip", download_geoip);
const exe = b.addExecutable(.{ const root_module = b.createModule(.{
.name = "wttr", .root_source_file = b.path("src/main.zig"),
.root_module = b.createModule(.{ .target = target,
.root_source_file = b.path("src/main.zig"), .optimize = optimize,
.target = target,
.optimize = optimize,
}),
}); });
root_module.addImport("httpz", httpz.module("httpz"));
exe.root_module.addImport("httpz", httpz.module("httpz")); root_module.addImport("zeit", zeit.module("zeit"));
exe.root_module.addImport("zeit", zeit.module("zeit")); root_module.addAnonymousImport("airports.dat", .{
exe.root_module.addAnonymousImport("airports.dat", .{
.root_source_file = openflights.path("data/airports.dat"), .root_source_file = openflights.path("data/airports.dat"),
}); });
exe.root_module.addOptions("build_options", build_options); root_module.addOptions("build_options", build_options);
exe.root_module.addIncludePath(maxminddb_upstream.path("include")); root_module.addIncludePath(maxminddb_upstream.path("include"));
exe.root_module.addIncludePath(b.path("libs/phoon_14Aug2014")); root_module.addIncludePath(b.path("libs/phoon_14Aug2014"));
exe.root_module.addIncludePath(b.path("libs/sunriset")); root_module.addIncludePath(b.path("libs/sunriset"));
exe.root_module.addConfigHeader(maxminddb_config); root_module.addConfigHeader(maxminddb_config);
exe.linkLibrary(maxminddb); const libs = &[_]*std.Build.Step.Compile{
exe.linkLibrary(phoon); maxminddb,
exe.linkLibrary(sunriset); phoon,
exe.linkLibC(); sunriset,
};
const exe = b.addExecutable(.{
.name = "wttr",
.root_module = root_module,
});
configureCompilationUnit(exe, libs);
b.installArtifact(exe); b.installArtifact(exe);
@ -139,38 +143,19 @@ pub fn build(b: *std.Build) void {
const run_step = b.step("run", "Run the app"); const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step); run_step.dependOn(&run_cmd.step);
const download_geoip = b.option(bool, "download-geoip", "Download GeoIP database for tests") orelse false; const tests = b.addTest(.{ .root_module = root_module });
configureCompilationUnit(tests, libs);
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 run_tests = b.addRunArtifact(tests); const run_tests = b.addRunArtifact(tests);
const test_step = b.step("test", "Run tests"); const test_step = b.step("test", "Run tests");
test_step.dependOn(&run_tests.step); test_step.dependOn(&run_tests.step);
// Coverage 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();
} }

View file

@ -2,7 +2,7 @@ const builtin = @import("builtin");
const std = @import("std"); const std = @import("std");
const Build = std.Build; 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 //verify host requirements
{ {
const supported = builtin.os.tag == .linux and 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 // Create download and coverage build steps
{ return blk: {
const download_exe = b.addExecutable(.{ const download_exe = b.addExecutable(.{
.name = "download-kcov", .name = "download-kcov",
.root_module = b.createModule(.{ .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" }); 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(b.fmt("--configure=css-file={s}", .{css_file}));
run_coverage.addArg(coverage_dir); 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.addArtifactArg(test_exe);
run_coverage.step.dependOn(&test_exe.step); run_coverage.step.dependOn(&test_exe.step);
run_coverage.step.dependOn(&run_download.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) { if (coverage_threshold > 0) {
const xml_path = b.fmt("{s}/test/cobertura.xml", .{coverage_dir}); 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 { } else {
coverage_step.dependOn(&run_coverage.step); coverage_step.dependOn(&run_coverage.step);
} }
} break :blk test_exe;
};
} }
pub const CheckCoverage = struct { pub const CheckCoverage = struct {