Skip to content

Commit

Permalink
chore: revert d421123
Browse files Browse the repository at this point in the history
* Switch gpa to builders arena allocator

Signed-off-by: Shinyzenith <[email protected]>
  • Loading branch information
Shinyzenith committed Oct 26, 2023
1 parent bfc94ad commit 3ca5c3d
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 43 deletions.
41 changes: 16 additions & 25 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@
//
// 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;

const version = "0.1.0-dev";

pub fn build(builder: *std.Build) !void {
defer _ = gpa.deinit();
const target = builder.standardTargetOptions(.{});
const optimize = builder.standardOptimizeOption(.{});

Expand Down Expand Up @@ -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.
Expand Down
76 changes: 68 additions & 8 deletions nextctl.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
14 changes: 4 additions & 10 deletions scdoc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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, .{
Expand All @@ -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);
Expand Down

0 comments on commit 3ca5c3d

Please sign in to comment.