1
0
repetition-testing/build.zig

84 lines
2.7 KiB
Zig

const std = @import("std");
const Builder = std.build.Builder;
fn addLinuxAssembly(b: *Builder, filename: []const u8) !std.Build.LazyPath {
const obj_basename = try std.mem.concat(b.allocator, u8, &.{
std.fs.path.stem(filename),
".o"
});
const obj = b.addSystemCommand(&.{ "nasm", "-g", "-f", "elf64", "-o" });
const output_obj = obj.addOutputFileArg(obj_basename);
obj.addFileArg(.{ .path = filename });
return output_obj;
}
fn addAllLinuxAssmeblies(b: *Builder) !std.ArrayList(std.Build.LazyPath) {
var linux_assemblies = std.ArrayList(std.Build.LazyPath).init(b.allocator);
errdefer linux_assemblies.deinit();
var dir = try std.fs.cwd().openIterableDir("src", .{ });
var it = dir.iterate();
while (try it.next()) |file| {
if (file.kind != .file) continue;
const ext = std.fs.path.extension(file.name);
if (!std.mem.eql(u8, ext, ".asm")) continue;
const assembly_path = try std.mem.concat(b.allocator, u8, &.{ "src/", file.name });
defer b.allocator.free(assembly_path);
try linux_assemblies.append(try addLinuxAssembly(b, assembly_path));
}
return linux_assemblies;
}
pub fn build(b: *Builder) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
var linux_assemblies = try addAllLinuxAssmeblies(b);
defer linux_assemblies.deinit();
var dir = try std.fs.cwd().openIterableDir("src/tests", .{ });
var it = dir.iterate();
while (try it.next()) |file| {
if (file.kind != .file) continue;
const ext = std.fs.path.extension(file.name);
if (!std.mem.eql(u8, ext, ".c")) continue;
const source_file_path = try std.mem.concat(b.allocator, u8, &.{ "src/tests/", file.name });
defer b.allocator.free(source_file_path);
const executable_name = std.fs.path.stem(file.name);
const exe = b.addExecutable(.{
.name = executable_name,
.root_source_file = .{ .path = source_file_path },
.optimize = optimize,
.target = target
});
exe.addIncludePath(.{ .path = "src" });
exe.linkLibC();
for (linux_assemblies.items) |obj| {
exe.addObjectFile(obj);
}
b.installArtifact(exe);
const run_exe = b.addRunArtifact(exe);
if (b.args) |args| {
run_exe.addArgs(args);
}
const step_description = try std.fmt.allocPrint(b.allocator, "Run '{s}' test", .{source_file_path});
defer b.allocator.free(step_description);
const run_step = b.step(executable_name, step_description);
run_step.dependOn(&run_exe.step);
}
}