build ready!

This commit is contained in:
Hamcha 2024-05-10 20:48:01 +02:00
parent 7518fec476
commit d0e69286ac
Signed by: hamcha
GPG key ID: 1669C533B8CF6D89
5 changed files with 90 additions and 2 deletions

View file

@ -8,7 +8,9 @@ Turns your ELFs into DOLphins using the power of Zig (and CPU cycles).
## Usage
`elphin input.elf output.dol`
Binary: `elphin input.elf output.dol`
You can embed elphin as part of a Zig build pipeline by following the example in `example-ci`
## License

View file

@ -46,4 +46,4 @@ pub fn addModule(b: *std.Build) void {
});
}
pub usingnamespace @import("src/lib.zig");
pub const buildlib = @import("src/buildlib.zig");

11
example-ci/build.zig Normal file
View file

@ -0,0 +1,11 @@
const std = @import("std");
const elphin = @import("elphin");
pub fn build(b: *std.Build) void {
const file = b.addInstallBinFile(.{ .path = "../testdata/example.elf" }, "lol.elf");
const convert = elphin.buildlib.convertFile(b, "lol.elf", .{});
b.getInstallStep().dependOn(&file.step);
b.getInstallStep().dependOn(&convert.step);
}

12
example-ci/build.zig.zon Normal file
View file

@ -0,0 +1,12 @@
.{
.name = "example-ci",
.version = "0.0.0",
.dependencies = .{
.elphin = .{ .path = ".." },
},
.paths = .{
"",
},
}

63
src/buildlib.zig Normal file
View file

@ -0,0 +1,63 @@
const std = @import("std");
const lib = @import("lib.zig");
const Self = @This();
const ConvertFileOptions = struct {
installDir: std.Build.InstallDir = .bin,
filename: ?[]const u8 = null,
};
step: std.Build.Step,
path: []const u8,
options: ConvertFileOptions,
pub fn convertFile(b: *std.Build, file: []const u8, options: ConvertFileOptions) *Self {
const convert = b.step("convert", "Converts the compiled .elf file into .dol");
const self = b.allocator.create(Self) catch @panic("OOM");
self.* = .{
.step = std.Build.Step.init(.{
.id = .custom,
.name = b.fmt("Convert to DOL", .{}),
.owner = b,
.makeFn = make,
}),
.path = file,
.options = options,
};
convert.dependOn(&self.step);
return self;
}
fn make(step: *std.Build.Step, _: *std.Progress.Node) !void {
const self: *Self = @fieldParentPtr("step", step);
const b = step.owner;
// Get input path
const inputPath = b.getInstallPath(self.options.installDir, self.path);
// Read input
const input = try std.fs.cwd().openFile(inputPath, .{});
defer input.close();
// Get output path or calculate it from the input
const destination = if (self.options.filename) |name|
b.getInstallPath(self.options.installDir, name)
else
calculateOutputName(b, inputPath);
// Create output file
const output = try std.fs.cwd().createFile(destination, .{});
defer output.close();
try lib.convert(input, output);
}
fn calculateOutputName(b: *std.Build, path: []const u8) []const u8 {
const extIndex = std.mem.lastIndexOfScalar(u8, path, '.');
const filenameWithoutExtension = if (extIndex) |index| path[0..index] else path;
return b.fmt("{s}.dol", .{filenameWithoutExtension});
}