Skip to content

Commit

Permalink
Merge pull request #171 from kubkon/refactor-after-split
Browse files Browse the repository at this point in the history
Refactor after the split
  • Loading branch information
kubkon authored Dec 26, 2024
2 parents eb5c6f9 + 75f387b commit ae6612f
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 209 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ jobs:
version: 0.13.0
- run: zig version
- run: zig fmt --check src
- run: zig build test -Dhas-static -Dhas-zig -Dhas-objc-msgsend-stubs
- run: zig build test -Dhas-zig -Dhas-objc-msgsend-stubs
6 changes: 0 additions & 6 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ pub fn build(b: *std.Build) void {
}
b.installArtifact(exe);

const system_compiler = b.option(tests.SystemCompiler, "system-compiler", "System compiler we are utilizing for tests: gcc, clang");
const has_static = b.option(bool, "has-static", "Whether the system compiler supports '-static' flag") orelse false;
const has_zig = b.option(bool, "has-zig", "Whether the Zig compiler is in path") orelse false;
const is_musl = b.option(bool, "musl", "Whether the tests are linked against musl libc") orelse false;
const has_objc_msgsend_stubs = b.option(bool, "has-objc-msgsend-stubs", "Whether the system compiler supports '-fobjc-msgsend-selector-stubs' flag") orelse false;
const is_nix = b.option(bool, "nix", "Whether the host is Nix-based") orelse false;

Expand All @@ -99,10 +96,7 @@ pub fn build(b: *std.Build) void {
const test_step = b.step("test", "Run tests");
test_step.dependOn(&b.addRunArtifact(unit_tests).step);
test_step.dependOn(tests.addTests(b, exe, .{
.system_compiler = system_compiler,
.has_static = has_static,
.has_zig = has_zig,
.is_musl = is_musl,
.has_objc_msgsend_stubs = has_objc_msgsend_stubs,
.is_nix = is_nix,
}));
Expand Down
5 changes: 3 additions & 2 deletions src/MachO.zig
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
allocator: Allocator,
file: fs.File,
thread_pool: *ThreadPool,
options: Options,

warnings: std.ArrayListUnmanaged(ErrorMsg) = .{},
warnings_mutex: std.Thread.Mutex = .{},
errors: std.ArrayListUnmanaged(ErrorMsg) = .{},
errors_mutex: std.Thread.Mutex = .{},
options: Options,

dyld_info_cmd: macho.dyld_info_command = .{},
symtab_cmd: macho.symtab_command = .{},
Expand Down Expand Up @@ -158,7 +159,7 @@ pub fn deinit(self: *MachO) void {
self.data_in_code.deinit(gpa);
}

pub fn flush(self: *MachO) !void {
pub fn link(self: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();

Expand Down
78 changes: 17 additions & 61 deletions src/Options.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ const Allocator = mem.Allocator;
const CrossTarget = std.zig.CrossTarget;
const MachO = @import("MachO.zig");

pub const SearchStrategy = enum {
paths_first,
dylibs_first,
};

const usage =
\\Usage: bold [files...]
\\
Expand Down Expand Up @@ -489,19 +484,19 @@ const ArgsIterator = struct {
args: []const []const u8,
i: usize = 0,

pub fn next(it: *@This()) ?[]const u8 {
fn next(it: *@This()) ?[]const u8 {
if (it.i >= it.args.len) return null;
defer it.i += 1;
return it.args[it.i];
}

pub fn nextOrFatal(it: *@This(), ctx: anytype) []const u8 {
fn nextOrFatal(it: *@This(), ctx: anytype) []const u8 {
const arg = it.next() orelse
ctx.fatal("Expected parameter after '{s}'", .{it.args[it.i - 1]});
return arg;
}

pub fn peek(it: *@This()) ?[]const u8 {
fn peek(it: *@This()) ?[]const u8 {
const arg = it.next();
defer if (it.i > 0) {
it.i -= 1;
Expand All @@ -513,43 +508,26 @@ const ArgsIterator = struct {
fn ArgParser(comptime Ctx: type) type {
return struct {
arg: []const u8 = undefined,
it: *Options.ArgsIterator,
it: *ArgsIterator,
ctx: Ctx,

pub fn hasMore(p: *Self) bool {
fn hasMore(p: *Self) bool {
p.arg = p.it.next() orelse return false;
return true;
}

pub fn flagAny(p: *Self, comptime pat: []const u8) bool {
fn flagAny(p: *Self, comptime pat: []const u8) bool {
return p.flag2(pat) or p.flag1(pat);
}

pub fn flag2(p: *Self, comptime pat: []const u8) bool {
fn flag2(p: *Self, comptime pat: []const u8) bool {
return p.flagPrefix(pat, "--");
}

pub fn flag1(p: *Self, comptime pat: []const u8) bool {
fn flag1(p: *Self, comptime pat: []const u8) bool {
return p.flagPrefix(pat, "-");
}

pub fn flagZ(p: *Self, comptime pat: []const u8) bool {
const prefix = "-z";
const i = p.it.i;
const actual_flag = blk: {
if (mem.eql(u8, p.arg, prefix)) {
break :blk p.it.nextOrFatal(p.ctx);
}
if (mem.startsWith(u8, p.arg, prefix)) {
break :blk p.arg[prefix.len..];
}
return false;
};
if (mem.eql(u8, actual_flag, pat)) return true;
p.it.i = i;
return false;
}

fn flagPrefix(p: *Self, comptime pat: []const u8, comptime prefix: []const u8) bool {
if (mem.startsWith(u8, p.arg, prefix)) {
const actual_arg = p.arg[prefix.len..];
Expand All @@ -560,47 +538,20 @@ fn ArgParser(comptime Ctx: type) type {
return false;
}

pub fn argAny(p: *Self, comptime pat: []const u8) ?[]const u8 {
fn argAny(p: *Self, comptime pat: []const u8) ?[]const u8 {
if (p.arg2(pat)) |value| return value;
return p.arg1(pat);
}

pub fn arg2(p: *Self, comptime pat: []const u8) ?[]const u8 {
fn arg2(p: *Self, comptime pat: []const u8) ?[]const u8 {
return p.argPrefix(pat, "--");
}

pub fn arg1(p: *Self, comptime pat: []const u8) ?[]const u8 {
fn arg1(p: *Self, comptime pat: []const u8) ?[]const u8 {
return p.argPrefix(pat, "-");
}

pub fn argZ(p: *Self, comptime pat: []const u8) ?[]const u8 {
const prefix = "-z";
const i = p.it.i;
const actual_arg = blk: {
if (mem.eql(u8, p.arg, prefix)) {
if (p.it.peek()) |next| {
if (mem.startsWith(u8, next, "-")) return null;
}
break :blk p.it.nextOrFatal(p.ctx);
}
if (mem.startsWith(u8, p.arg, prefix)) {
break :blk p.arg[prefix.len..];
}
return null;
};
if (mem.startsWith(u8, actual_arg, pat)) {
if (mem.indexOf(u8, actual_arg, "=")) |index| {
if (index == pat.len) {
const value = actual_arg[index + 1 ..];
return value;
}
}
}
p.it.i = i;
return null;
}

pub fn argLLVM(p: *Self) ?[]const u8 {
fn argLLVM(p: *Self) ?[]const u8 {
if (p.flag1("mllvm")) {
return p.it.nextOrFatal(p.ctx);
}
Expand Down Expand Up @@ -689,6 +640,11 @@ pub const Platform = struct {
}
};

pub const SearchStrategy = enum {
paths_first,
dylibs_first,
};

const UndefinedTreatment = enum {
@"error",
warn,
Expand Down
2 changes: 1 addition & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn main() !void {

var ld = try MachO.openPath(gpa, opts, &thread_pool);
defer ld.deinit();
const res = ld.flush();
const res = ld.link();
ld.reportWarnings();
ld.reportErrors();
res catch |err| {
Expand Down
Loading

0 comments on commit ae6612f

Please sign in to comment.