Hello!
I am trying to build an executable and I would like to be able to query its size. In order to do so I was thinking of making a build step called “size” which would invoke the binutils’ size program with zig build size. The problem is that i can’t get the output path of the final installed executable from the LazyPath.
For now, I have the following build.zig:
const std = @import("std");
pub fn build(b: *std.Build) void {
const project_name = "sample";
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const main_mod = b.addModule(project_name, .{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const main_exe = b.addExecutable(.{
.name = project_name ++ ".elf",
.root_module = main_mod,
.linkage = .static,
});
const installed_bin = b.addInstallArtifact(
main_exe,
.{},
);
b.getInstallStep().dependOn(&installed_bin.step);
// Actual size step
const size_step = b.step("size", "Show size of the built executable");
size_step.dependOn(&installed_bin.step);
const size_cmd = b.addSystemCommand(&.{
"size",
installed_bin.emitted_bin.?.basename(b, size_step),
});
size_cmd.step.dependOn(&installed_bin.step);
size_step.dependOn(&size_cmd.step);
}
I guess the problem is on installed_bin.emitted_bin.?.basename(b, size_step) because even though the step depends on the built executable, this line is executed inmediately without having actually built the binary, but I dont know how to go around this to implement the step.
Does anyone know how to achieve what I am looking for?
Thank you very much.