diff --git a/build.zig b/build.zig index 2d08152..4f4287e 100644 --- a/build.zig +++ b/build.zig @@ -2,14 +2,12 @@ // // build.zig // -// Created by: Aakash Sen Sharma, May 2022 +// Created by: Aakash Sen Sharma, May 2023 // Copyright: (C) 2022, Aakash Sen Sharma & Contributors const std = @import("std"); -var gpa = std.heap.GeneralPurposeAllocator(.{}){}; -pub const allocator = gpa.allocator(); - +const NextctlStep = @import("nextctl.zig"); const Scdoc = @import("scdoc.zig"); const Nextctl = @import("nextctl.zig"); const Scanner = @import("deps/zig-wayland/build.zig").Scanner; @@ -17,7 +15,6 @@ const Scanner = @import("deps/zig-wayland/build.zig").Scanner; const version = "0.1.0-dev"; pub fn build(builder: *std.Build) !void { - defer _ = gpa.deinit(); const target = builder.standardTargetOptions(.{}); const optimize = builder.standardOptimizeOption(.{}); @@ -116,26 +113,20 @@ pub fn build(builder: *std.Build) !void { // Nextctl Installation { - // Abandoned nextctl step as zig build steps felt confusing and were very racy.. - // There should be an option to disable parallelized builds :( - if (nextctl_rs and nextctl_go) { - @panic("Please choose only 1 Nextctl Implementation."); - } else if (nextctl_rs) { - try Nextctl.syncVersion("version = ", "nextctl-rs/Cargo.toml", version); - _ = builder.exec(&.{ "make", "-C", "nextctl-rs" }); - - builder.installFile("./nextctl-rs/target/release/nextctl", "bin/nextctl"); - } else if (nextctl_go) { - try Nextctl.syncVersion("const VERSION = ", "nextctl-go/cmd/nextctl/nextctl.go", version); - _ = builder.exec(&.{ "make", "-C", "nextctl-go" }); - - builder.installFile("./nextctl-go/nextctl", "bin/nextctl"); - } else { - try Nextctl.syncVersion("#define VERSION ", "nextctl/include/nextctl.h", version); - _ = builder.exec(&.{ "make", "-C", "nextctl" }); - - builder.installFile("./nextctl/zig-out/bin/nextctl", "bin/nextctl"); - } + const build_type: NextctlStep.BuildType = blk: { + if (nextctl_rs and nextctl_go) { + @panic("Please choose only 1 Nextctl Implementation."); + } else if (nextctl_rs) { + break :blk .rust; + } else if (nextctl_go) { + break :blk .go; + } else { + break :blk .c; + } + }; + + const nextctl = try NextctlStep.init(builder, build_type, version); + try nextctl.install(); } // Pkgconfig installation. diff --git a/nextctl.zig b/nextctl.zig index a0eaa8a..fb47e3c 100644 --- a/nextctl.zig +++ b/nextctl.zig @@ -5,29 +5,89 @@ // Created by: Aakash Sen Sharma, October 2023 // Copyright: (C) 2022, Aakash Sen Sharma & Contributors +const Self = @This(); const std = @import("std"); -const allocator = @import("build.zig").allocator; -pub fn syncVersion(needle: []const u8, file_name: []const u8, new_version: []const u8) !void { +pub const BuildType = enum { + c, + go, + rust, +}; + +step: std.build.Step, +build_type: BuildType, +version: []const u8, + +pub fn init(builder: *std.Build, build_type: BuildType, version: []const u8) !*Self { + const self = try builder.allocator.create(Self); + self.* = .{ + .step = std.build.Step.init(.{ + .id = .custom, + .name = "Build nextctl", + .makeFn = &make, + .owner = builder, + }), + .build_type = build_type, + .version = version, + }; + + return self; +} + +fn make(step: *std.build.Step, _: *std.Progress.Node) anyerror!void { + const self = @fieldParentPtr(Self, "step", step); + const builder = self.step.owner; + + switch (self.build_type) { + .c => { + try syncVersion(builder.allocator, "#define VERSION ", "nextctl/include/nextctl.h", self.version); + _ = builder.exec(&.{ "make", "-C", "nextctl" }); + }, + .rust => { + try syncVersion(builder.allocator, "version = ", "nextctl-rs/Cargo.toml", self.version); + _ = builder.exec(&.{ "make", "-C", "nextctl-rs" }); + }, + .go => { + try syncVersion(builder.allocator, "const VERSION = ", "nextctl-go/cmd/nextctl/nextctl.go", self.version); + _ = builder.exec(&.{ "make", "-C", "nextctl-go" }); + }, + } +} + +pub fn install(self: *Self) !void { + const builder = self.step.owner; + const install_nextctl = blk: { + switch (self.build_type) { + .c => { + break :blk builder.addInstallFile(.{ .path = "./nextctl/zig-out/bin/nextctl" }, "bin/nextctl"); + }, + .rust => { + break :blk builder.addInstallFile(.{ .path = "./nextctl-rs/target/release/nextctl" }, "bin/nextctl"); + }, + .go => { + break :blk builder.addInstallFile(.{ .path = "./nextctl-go/nextctl" }, "bin/nextctl"); + }, + } + }; + + install_nextctl.step.dependOn(&self.step); + builder.getInstallStep().dependOn(&install_nextctl.step); +} + +fn syncVersion(allocator: std.mem.Allocator, needle: []const u8, file_name: []const u8, new_version: []const u8) !void { const file = try std.fs.cwd().openFile(file_name, .{}); defer file.close(); const file_size = (try file.stat()).size; const file_buffer = try file.readToEndAlloc(allocator, file_size); - defer allocator.free(file_buffer); const start_index = std.mem.indexOfPos(u8, file_buffer, 0, needle).? + needle.len; const end_index = std.mem.indexOfPos(u8, file_buffer, start_index + 1, "\"").? + 1; const old_version = file_buffer[start_index..end_index]; const old_version_str = try std.fmt.allocPrint(allocator, "{s}{s}\n", .{ needle, old_version }); - defer allocator.free(old_version_str); - const new_version_str = try std.fmt.allocPrint(allocator, "{s}\"{s}\"\n", .{ needle, new_version }); - defer allocator.free(new_version_str); - const replaced_str = try std.mem.replaceOwned(u8, allocator, file_buffer, old_version_str, new_version_str); - defer allocator.free(replaced_str); try std.fs.cwd().writeFile(file_name, replaced_str); } diff --git a/scdoc.zig b/scdoc.zig index 2580c9a..86f591e 100644 --- a/scdoc.zig +++ b/scdoc.zig @@ -6,7 +6,6 @@ // Copyright: (C) 2022, Aakash Sen Sharma & Contributors const std = @import("std"); -const allocator = @import("build.zig").allocator; pub fn build(builder: *std.build.Builder, docs_dir: []const u8) !void { var dir = try std.fs.cwd().openIterableDir(docs_dir, .{ @@ -21,28 +20,23 @@ pub fn build(builder: *std.build.Builder, docs_dir: []const u8) !void { if (entry.kind == .file) { if (std.mem.lastIndexOfScalar(u8, entry.name, '.')) |idx| { if (std.mem.eql(u8, entry.name[idx..], ".scd")) { - const p = try std.fmt.allocPrint(allocator, "{s}{s}", .{ docs_dir, entry.name }); - defer allocator.free(p); - - const path = try std.fmt.allocPrint(allocator, "{s}.gz", .{p[0..(p.len - 4)]}); - defer allocator.free(path); + const p = try std.fmt.allocPrint(builder.allocator, "{s}{s}", .{ docs_dir, entry.name }); + const path = try std.fmt.allocPrint(builder.allocator, "{s}.gz", .{p[0..(p.len - 4)]}); const path_no_ext = path[0..(path.len - 3)]; const section = path_no_ext[(path_no_ext.len - 1)..]; const output = try std.fmt.allocPrint( - allocator, + builder.allocator, "share/man/man{s}/{s}", .{ section, std.fs.path.basename(path) }, ); - defer allocator.free(output); const cmd = try std.fmt.allocPrint( - allocator, + builder.allocator, "scdoc < {s} > {s}", .{ p, path }, ); - defer allocator.free(cmd); _ = builder.exec(&.{ "sh", "-c", cmd }); builder.installFile(path, output);