From 55c63b29dae6a23e6bfa8058d1cb907a9a4f1da0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Mon, 11 Jul 2022 04:54:55 +0300 Subject: [PATCH] support strip in build.zig --- build.zig | 6 +++++ src/analyze.zig | 61 +++++++++++++++++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/build.zig b/build.zig index a516078..eea0399 100644 --- a/build.zig +++ b/build.zig @@ -6,6 +6,8 @@ pub fn build(b: *zbs.Builder) void { const target = b.standardTargetOptions(.{}); const mode = b.standardReleaseOptions(); + const strip = b.option(bool, "strip", "Omit debug information") orelse false; + const cmph = b.addStaticLibrary("cmph", null); cmph.setTarget(target); cmph.setBuildMode(mode); @@ -39,6 +41,7 @@ pub fn build(b: *zbs.Builder) void { "-Wno-unused-function", //"-DDEBUG", }); + cmph.strip = strip; cmph.omit_frame_pointer = true; cmph.addIncludeDir("deps/cmph/src"); 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"); + exe.strip = strip; exe.setTarget(target); exe.setBuildMode(mode); addCmphDeps(exe, cmph); @@ -70,6 +74,7 @@ pub fn build(b: *zbs.Builder) void { { const exe = b.addExecutable("turbo-analyze", "src/analyze.zig"); + exe.strip = strip; exe.setTarget(target); exe.setBuildMode(mode); exe.install(); @@ -83,6 +88,7 @@ pub fn build(b: *zbs.Builder) void { .patch = 0, }, }); + so.strip = strip; so.linkLibC(); so.linkLibrary(bdz); so.addIncludeDir("deps/cmph/src"); diff --git a/src/analyze.zig b/src/analyze.zig index 1686852..332891a 100644 --- a/src/analyze.zig +++ b/src/analyze.zig @@ -5,9 +5,11 @@ const os = std.os; const fmt = std.fmt; const mem = std.mem; const heap = std.heap; +const math = std.math; const meta = std.meta; const ArrayList = std.ArrayList; const Allocator = std.mem.Allocator; +const BoundedArray = std.BoundedArray; const flags = @import("flags.zig"); @@ -28,7 +30,6 @@ const usage = const Info = struct { fname: []const u8, size_file: []const u8, - bytes_file: os.off_t, version: meta.fieldInfo(Header, .version).field_type, endian: []const u8, ptr_size: meta.fieldInfo(Header, .ptr_size).field_type, @@ -106,16 +107,9 @@ fn execute( defer file.close(); 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{ .fname = db_file, - .size_file = buf, - .bytes_file = file_size_bytes, + .size_file = splitInt(@intCast(u64, file_size_bytes)).constSlice(), .version = db.header.version, .endian = @tagName(db.header.endian), .ptr_size = db.header.ptr_size, @@ -128,7 +122,7 @@ fn execute( const template = \\File: {[fname]s} - \\Size: {[size_file]s} ({[bytes_file]d} bytes) + \\Size: {[size_file]s} bytes \\Version: {[version]d} \\Endian: {[endian]s} \\Pointer size: {[ptr_size]} bytes @@ -138,7 +132,7 @@ fn execute( \\Groups: {[groups]d} \\Shells: {[shells]d} \\Sections: - \\ Name Begin End Size + \\ Name Begin End Size bytes \\ ; stdout.print(template, info) catch {}; @@ -148,20 +142,13 @@ fn execute( inline for (meta.fields(DB.DBNumbers)) |field| { 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 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, start << section_length_bits, end << section_length_bits, - size, + splitInt(length << section_length_bits).constSlice(), }) catch {}; } @@ -185,3 +172,37 @@ test "trivial error: db file" { "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()); + } +}