Use std.testing.refAllDeclsRecursive in teest builds to compile (most) unused functions (#1249)

* Use `std.testing.refAllDeclsRecursive` in teest builds to compile (most) unused functions

* Fix formatting
This commit is contained in:
IntegratedQuantum 2025-03-28 17:23:27 +01:00 committed by GitHub
parent b5951f4bac
commit b347be8764
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 49 additions and 20 deletions

View File

@ -1,24 +1,10 @@
const std = @import("std");
pub fn build(b: *std.Build) !void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
fn linkLibraries(b: *std.Build, exe: *std.Build.Step.Compile, useLocalDeps: bool) void {
const target = exe.root_module.resolved_target.?;
const t = target.result;
const optimize = exe.root_module.optimize.?;
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "Cubyzig",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
//.sanitize_thread = true,
});
exe.root_module.addImport("main", exe.root_module);
exe.linkLibC();
exe.linkLibCpp();
@ -34,7 +20,6 @@ pub fn build(b: *std.Build) !void {
};
var depsName: []const u8 = b.fmt("cubyz_deps_{s}_{s}", .{@tagName(t.cpu.arch), @tagName(t.os.tag)});
const useLocalDeps = b.option(bool, "local", "Use local cubyz_deps") orelse false;
if(useLocalDeps) depsName = "local";
const libsDeps = b.lazyDependency(depsName, .{
@ -80,6 +65,32 @@ pub fn build(b: *std.Build) !void {
} else {
std.log.err("Unsupported target: {}\n", .{t.os.tag});
}
}
pub fn build(b: *std.Build) !void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const optimize = b.standardOptimizeOption(.{});
const useLocalDeps = b.option(bool, "local", "Use local cubyz_deps") orelse false;
const exe = b.addExecutable(.{
.name = "Cubyzig",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
//.sanitize_thread = true,
//.use_llvm = false,
});
exe.root_module.addImport("main", exe.root_module);
linkLibraries(b, exe, useLocalDeps);
b.installArtifact(exe);
@ -97,8 +108,7 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = optimize,
});
exe_tests.addIncludePath(headersDeps.path("include"));
exe_tests.linkLibC();
linkLibraries(b, exe_tests, useLocalDeps);
exe_tests.root_module.addImport("main", exe_tests.root_module);
const run_exe_tests = b.addRunArtifact(exe_tests);

View File

@ -719,7 +719,26 @@ pub fn main() void { // MARK: main()
}
}
/// std.testing.refAllDeclsRecursive, but ignores C imports (by name)
pub fn refAllDeclsRecursiveExceptCImports(comptime T: type) void {
if(!@import("builtin").is_test) return;
inline for(comptime std.meta.declarations(T)) |decl| blk: {
if(comptime std.mem.eql(u8, decl.name, "c")) continue;
if(comptime std.mem.eql(u8, decl.name, "hbft")) break :blk;
if(comptime std.mem.eql(u8, decl.name, "stb_image")) break :blk;
if(@TypeOf(@field(T, decl.name)) == type) {
switch(@typeInfo(@field(T, decl.name))) {
.@"struct", .@"enum", .@"union", .@"opaque" => refAllDeclsRecursiveExceptCImports(@field(T, decl.name)),
else => {},
}
}
_ = &@field(T, decl.name);
}
}
test "abc" {
@setEvalBranchQuota(1000000);
refAllDeclsRecursiveExceptCImports(@This());
_ = @import("json.zig");
_ = @import("zon.zig");
}