Reduce complexity of build script
continuous-integration/drone/push Build is passing Details

This commit is contained in:
kayomn 2024-07-25 22:26:03 +01:00
parent c68753bf84
commit e350d88793
1 changed files with 64 additions and 111 deletions

143
build.zig
View File

@ -12,15 +12,23 @@ const Project = struct {
optimize: std.builtin.OptimizeMode, optimize: std.builtin.OptimizeMode,
imports: ImportList, imports: ImportList,
pub fn find_demos(self: Project, b: *std.Build) !void { pub fn demos_step(self: *const Project, b: *std.Build) *std.Build.Step {
const demos = b.step("demos", "Build demos"); const Demos = struct {
var dir = try std.fs.openDirAbsolute(b.path("demos/").getPath(b), .{.iterate = true}); step: std.Build.Step,
project: *const Project,
const Self = @This();
fn make(step: *std.Build.Step, _: std.Progress.Node) anyerror!void {
const demos: *const Self = @fieldParentPtr("step", step);
const absolute_path = step.owner.path("demos/").getPath(step.owner);
var dir = try std.fs.openDirAbsolute(absolute_path, .{.iterate = true});
defer { defer {
dir.close(); dir.close();
} }
var entries = try dir.walk(b.allocator); var entries = try dir.walk(step.owner.allocator);
defer { defer {
entries.deinit(); entries.deinit();
@ -31,28 +39,43 @@ const Project = struct {
continue; continue;
} }
const source_path = try sub_path(.{"demos", entry.basename}); const source_path = step.owner.pathJoin(&.{"demos", entry.basename});
var path_buffer = [_:0]u8{0} ** 255; var path_buffer = [_:0]u8{0} ** 255;
const demo = b.addExecutable(.{ const demo = step.owner.addExecutable(.{
.name = try std.fmt.bufPrint(&path_buffer, "{s}.out", .{std.fs.path.stem(entry.basename)}), .name = try std.fmt.bufPrint(&path_buffer, "{s}.out", .{std.fs.path.stem(entry.basename)}),
.root_source_file = b.path(source_path.bytes()), .root_source_file = step.owner.path(source_path),
.target = self.target, .target = demos.project.target,
.optimize = self.optimize, .optimize = demos.project.optimize,
}); });
for (self.imports.items) |import| { for (demos.project.imports.items) |import| {
demo.root_module.addImport(import.name, import.module); demo.root_module.addImport(import.name, import.module);
} }
demos.dependOn(&b.addInstallArtifact(demo, .{ step.owner.installArtifact(demo);
.dest_dir = .{
.override = .{
.custom = "../demos/",
},
},
}).step);
} }
step.state = .success;
}
};
const demos = b.allocator.create(Demos) catch {
@panic("OOM");
};
demos.* = .{
.step = std.Build.Step.init(.{
.id = .custom,
.name = "demos",
.makeFn = Demos.make,
.owner = b,
}),
.project = self,
};
return &demos.step;
} }
pub fn find_tests(self: Project, b: *std.Build) !void { pub fn find_tests(self: Project, b: *std.Build) !void {
@ -123,15 +146,13 @@ const Project = struct {
var binary_buffer = [_:0]u8{0} ** 255; var binary_buffer = [_:0]u8{0} ** 255;
const binary_name = try std.fmt.bufPrint(&binary_buffer, "{s}.spv", .{entry.path}); const binary_name = try std.fmt.bufPrint(&binary_buffer, "{s}.spv", .{entry.path});
const full_source_path = try sub_path(.{shaders_path, entry.path});
const full_binary_path = try sub_path(.{shaders_path, binary_name});
const glslang_validator_args = [_][]const u8{ const glslang_validator_args = [_][]const u8{
"glslangValidator", "glslangValidator",
"-V", "-V",
full_source_path.bytes(), b.pathJoin(&.{shaders_path, entry.path}),
"-o", "-o",
full_binary_path.bytes(), b.pathJoin(&.{shaders_path, binary_name}),
}; };
shaders_dir.access(binary_name, .{.mode = .read_only}) catch { shaders_dir.access(binary_name, .{.mode = .read_only}) catch {
@ -155,45 +176,12 @@ const Project = struct {
} }
}; };
const SubPath = struct { pub fn build(b: *std.Build) !void {
buffer: [max]u8 = [_]u8{0} ** max, const project = b.allocator.create(Project) catch {
unused: u8 = max, @panic("OOM");
pub const max = 255;
pub fn append(self: *SubPath, component: []const u8) !void {
const used = max - self.unused;
if (used != 0 and self.buffer[used - 1] != '/') {
if (component.len > self.unused) {
return error.PathTooBig;
}
@memcpy(self.buffer[used .. (used + component.len)], component);
self.unused -= @intCast(component.len);
} else {
const required_len = component.len + 1;
if (required_len > self.unused) {
return error.PathTooBig;
}
@memcpy(self.buffer[used .. (used + component.len)], component);
self.buffer[component.len] = '/';
self.unused -= @intCast(required_len);
}
}
pub fn bytes(self: *const SubPath) [:0]const u8 {
return @ptrCast(self.buffer[0 .. max - self.unused]);
}
}; };
pub fn build(b: *std.Build) !void { project.* = .{
var project = Project{
.imports = ImportList.init(b.allocator), .imports = ImportList.init(b.allocator),
.target = b.standardTargetOptions(.{}), .target = b.standardTargetOptions(.{}),
.optimize = b.standardOptimizeOption(.{}), .optimize = b.standardOptimizeOption(.{}),
@ -286,42 +274,7 @@ pub fn build(b: *std.Build) !void {
gfx_module.link_libc = true; gfx_module.link_libc = true;
b.step("demos", "Build demos").dependOn(project.demos_step(b));
try project.find_tests(b); try project.find_tests(b);
try project.find_demos(b);
}
fn sub_path(components: anytype) !SubPath {
var path = comptime try std.BoundedArray(u8, SubPath.max).init(0);
const Components = @TypeOf(components);
switch (@typeInfo(Components)) {
.Struct => |@"struct"| {
if (!@"struct".is_tuple) {
@compileError("`components` must be a tuple");
}
const last_component_index = components.len - 1;
inline for (components, 0 .. components.len) |component, i| {
path.appendSlice(component) catch {
return error.PathTooBig;
};
if (i < last_component_index and !std.mem.endsWith(u8, component, "/")) {
path.append('/') catch {
return error.PathTooBig;
};
}
}
},
else => {
@compileError("`components` cannot be a " ++ @typeName(Components));
}
}
return .{
.unused = SubPath.max - path.len,
.buffer = path.buffer,
};
} }