feat: added loader and appropriate build scripts

This commit is contained in:
Andrew Rioux
2025-01-21 03:11:19 -05:00
parent 489b1d380f
commit 78ac60ce60
9 changed files with 114 additions and 56 deletions

View File

@@ -1,26 +1,55 @@
const std = @import("std");
pub fn build(b: *std.Build) !void {
const beacon = b.option([]const u8, "beacon", "path to beacon to load") orelse std.debug.panic("Expected a path to a beacon to be provided with -Dbeacon", .{});
const beacon_path = std.Build.LazyPath{ .cwd_relative = beacon };
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const tool = b.addExecutable(.{
.name = "beacon-compress",
.root_source_file = b.path("src/gzip.zig"),
.target = b.host,
});
const tool_step = b.addRunArtifact(tool);
tool_step.addFileArg(beacon_path);
const compressed_beacon = tool_step.addOutputFileArg("beacon.gz");
const libloader = b.addObject(.{
.name = "libloader",
.root_source_file = b.path("src/libloader.zig"),
.target = target,
.optimize = optimize,
.strip = true,
.pic = true,
});
libloader.root_module.addAnonymousImport("beacon", .{ .root_source_file = compressed_beacon });
libloader.linkLibC();
libloader.step.dependOn(&tool_step.step);
const lib = b.addSharedLibrary(.{
.name = "unix-loader",
.root_source_file = b.path("src/loader.zig"),
.target = target,
.optimize = optimize,
.strip = true,
});
const exe = b.addExecutable(.{
.name = "unix-loader",
.name = "test-loader",
.root_source_file = b.path("src/test_run.zig"),
.target = target,
.optimize = optimize,
.strip = true,
});
lib.linkLibC();
b.installArtifact(lib);
lib.addObject(libloader);
exe.addObject(libloader);
exe.linkLibC();
b.installArtifact(lib);
b.installArtifact(exe);
}

19
unix-loader/src/gzip.zig Normal file
View File

@@ -0,0 +1,19 @@
const std = @import("std");
pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const alloc = arena.allocator();
const args = try std.process.argsAlloc(alloc);
const input_file_path = args[1];
const output_file_path = args[2];
var input_file = try std.fs.cwd().openFile(input_file_path, .{});
defer input_file.close();
var output_file = try std.fs.cwd().createFile(output_file_path, .{});
defer output_file.close();
try std.compress.gzip.compress(input_file.reader(), output_file.writer(), .{ .level = .level_9 });
}

View File

@@ -0,0 +1,11 @@
const std = @import("std");
const beacon = @embedFile("beacon");
fn use_beacon(message: []const u8) !void {
_ = try std.io.getStdOut().write(message);
}
export fn hash_internals() void {
use_beacon(beacon) catch {};
}

View File

@@ -1,10 +1,5 @@
const std = @import("std");
const testing = std.testing;
extern fn hash_internals() void;
export fn add(a: i32, b: i32) i32 {
return a + b;
}
test "basic add functionality" {
try testing.expect(add(3, 7) == 10);
export fn calculate_hash() void {
hash_internals();
}

View File

@@ -1,25 +1,5 @@
const std = @import("std");
extern fn hash_internals() void;
pub fn main() !void {
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
// stdout is for the actual output of your application, for example if you
// are implementing gzip, then only the compressed bytes should be sent to
// stdout, not any debugging messages.
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();
try stdout.print("Run `zig build test` to run the tests.\n", .{});
try bw.flush(); // don't forget to flush!
}
test "simple test" {
var list = std.ArrayList(i32).init(std.testing.allocator);
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
try list.append(42);
try std.testing.expectEqual(@as(i32, 42), list.pop());
pub fn main() void {
hash_internals();
}