support strip in build.zig

This commit is contained in:
Motiejus Jakštys 2022-07-11 04:54:55 +03:00
parent 292bc4e4e0
commit 55c63b29da
2 changed files with 47 additions and 20 deletions

View File

@ -6,6 +6,8 @@ pub fn build(b: *zbs.Builder) void {
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const mode = b.standardReleaseOptions(); const mode = b.standardReleaseOptions();
const strip = b.option(bool, "strip", "Omit debug information") orelse false;
const cmph = b.addStaticLibrary("cmph", null); const cmph = b.addStaticLibrary("cmph", null);
cmph.setTarget(target); cmph.setTarget(target);
cmph.setBuildMode(mode); cmph.setBuildMode(mode);
@ -39,6 +41,7 @@ pub fn build(b: *zbs.Builder) void {
"-Wno-unused-function", "-Wno-unused-function",
//"-DDEBUG", //"-DDEBUG",
}); });
cmph.strip = strip;
cmph.omit_frame_pointer = true; cmph.omit_frame_pointer = true;
cmph.addIncludeDir("deps/cmph/src"); cmph.addIncludeDir("deps/cmph/src");
cmph.addIncludeDir("include/deps/cmph"); cmph.addIncludeDir("include/deps/cmph");
@ -62,6 +65,7 @@ pub fn build(b: *zbs.Builder) void {
{ {
const exe = b.addExecutable("turbo-unix2db", "src/unix2db.zig"); const exe = b.addExecutable("turbo-unix2db", "src/unix2db.zig");
exe.strip = strip;
exe.setTarget(target); exe.setTarget(target);
exe.setBuildMode(mode); exe.setBuildMode(mode);
addCmphDeps(exe, cmph); addCmphDeps(exe, cmph);
@ -70,6 +74,7 @@ pub fn build(b: *zbs.Builder) void {
{ {
const exe = b.addExecutable("turbo-analyze", "src/analyze.zig"); const exe = b.addExecutable("turbo-analyze", "src/analyze.zig");
exe.strip = strip;
exe.setTarget(target); exe.setTarget(target);
exe.setBuildMode(mode); exe.setBuildMode(mode);
exe.install(); exe.install();
@ -83,6 +88,7 @@ pub fn build(b: *zbs.Builder) void {
.patch = 0, .patch = 0,
}, },
}); });
so.strip = strip;
so.linkLibC(); so.linkLibC();
so.linkLibrary(bdz); so.linkLibrary(bdz);
so.addIncludeDir("deps/cmph/src"); so.addIncludeDir("deps/cmph/src");

View File

@ -5,9 +5,11 @@ const os = std.os;
const fmt = std.fmt; const fmt = std.fmt;
const mem = std.mem; const mem = std.mem;
const heap = std.heap; const heap = std.heap;
const math = std.math;
const meta = std.meta; const meta = std.meta;
const ArrayList = std.ArrayList; const ArrayList = std.ArrayList;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const BoundedArray = std.BoundedArray;
const flags = @import("flags.zig"); const flags = @import("flags.zig");
@ -28,7 +30,6 @@ const usage =
const Info = struct { const Info = struct {
fname: []const u8, fname: []const u8,
size_file: []const u8, size_file: []const u8,
bytes_file: os.off_t,
version: meta.fieldInfo(Header, .version).field_type, version: meta.fieldInfo(Header, .version).field_type,
endian: []const u8, endian: []const u8,
ptr_size: meta.fieldInfo(Header, .ptr_size).field_type, ptr_size: meta.fieldInfo(Header, .ptr_size).field_type,
@ -106,16 +107,9 @@ fn execute(
defer file.close(); defer file.close();
const db = file.db; const db = file.db;
var file_size_scratch: [16]u8 = undefined;
const buf = fmt.bufPrint(file_size_scratch[0..], "{d}", .{file_size_bytes}) catch unreachable;
//const buf = fmt.bufPrint(file_size_scratch[0..], "{:.2}", .{
// fmt.fmtIntSizeBin(@intCast(u64, file_size_bytes)),
//}) catch unreachable;
const info = Info{ const info = Info{
.fname = db_file, .fname = db_file,
.size_file = buf, .size_file = splitInt(@intCast(u64, file_size_bytes)).constSlice(),
.bytes_file = file_size_bytes,
.version = db.header.version, .version = db.header.version,
.endian = @tagName(db.header.endian), .endian = @tagName(db.header.endian),
.ptr_size = db.header.ptr_size, .ptr_size = db.header.ptr_size,
@ -128,7 +122,7 @@ fn execute(
const template = const template =
\\File: {[fname]s} \\File: {[fname]s}
\\Size: {[size_file]s} ({[bytes_file]d} bytes) \\Size: {[size_file]s} bytes
\\Version: {[version]d} \\Version: {[version]d}
\\Endian: {[endian]s} \\Endian: {[endian]s}
\\Pointer size: {[ptr_size]} bytes \\Pointer size: {[ptr_size]} bytes
@ -138,7 +132,7 @@ fn execute(
\\Groups: {[groups]d} \\Groups: {[groups]d}
\\Shells: {[shells]d} \\Shells: {[shells]d}
\\Sections: \\Sections:
\\ Name Begin End Size \\ Name Begin End Size bytes
\\ \\
; ;
stdout.print(template, info) catch {}; stdout.print(template, info) catch {};
@ -148,20 +142,13 @@ fn execute(
inline for (meta.fields(DB.DBNumbers)) |field| { inline for (meta.fields(DB.DBNumbers)) |field| {
const length = @field(lengths, field.name); const length = @field(lengths, field.name);
const size = fmt.bufPrint(file_size_scratch[0..], "{d}", .{
length << section_length_bits,
}) catch unreachable;
//const size = fmt.bufPrint(file_size_scratch[0..], "{:.0}", .{
// fmt.fmtIntSizeBin(@intCast(u64, length << section_length_bits)),
//}) catch unreachable;
const start = @field(offsets, field.name); const start = @field(offsets, field.name);
const end = start + @field(lengths, field.name); const end = start + @field(lengths, field.name);
stdout.print(" {s:<21}{x:0>8} {x:0>8} {s:>10}\n", .{ stdout.print(" {s:<21}{x:0>8} {x:0>8} {s:>14}\n", .{
field.name, field.name,
start << section_length_bits, start << section_length_bits,
end << section_length_bits, end << section_length_bits,
size, splitInt(length << section_length_bits).constSlice(),
}) catch {}; }) catch {};
} }
@ -185,3 +172,37 @@ test "trivial error: db file" {
"ERROR: failed to open 'db.turbo': FileNotFound\n", "ERROR: failed to open 'db.turbo': FileNotFound\n",
); );
} }
const max_len = fmt.count("{d}", .{math.maxInt(u64)}) * 2;
fn splitInt(n: u64) BoundedArray(u8, max_len) {
var result = BoundedArray(u8, max_len).init(0) catch unreachable;
var buf: [max_len]u8 = undefined;
const str = fmt.bufPrint(buf[0..], "{d}", .{n}) catch unreachable;
var remaining: usize = str.len;
for (str) |c| {
result.append(c) catch unreachable;
remaining -= 1;
if (remaining > 0 and remaining % 3 == 0)
result.append(',') catch unreachable;
}
return result;
}
test "separators" {
const tests = [_]struct {
input: u64,
want: []const u8,
}{
.{ .input = 0, .want = "0" },
.{ .input = 999, .want = "999" },
.{ .input = 1000, .want = "1,000" },
.{ .input = 9999, .want = "9,999" },
.{ .input = 19999, .want = "19,999" },
.{ .input = 19999, .want = "19,999" },
.{ .input = 18446744073709551615, .want = "18,446,744,073,709,551,615" },
};
for (tests) |tt| {
const got = splitInt(tt.input);
try testing.expectEqualStrings(tt.want, got.constSlice());
}
}