From a7a5f4cf4d1d975c95f8f1b71bf2d9bd4afa9204 Mon Sep 17 00:00:00 2001 From: binarycraft007 Date: Fri, 1 Mar 2024 09:18:33 +0800 Subject: [PATCH] objcopy: support multiple only sections --- lib/std/Build/Cache.zig | 5 +++++ lib/std/Build/Step/ObjCopy.zig | 14 ++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/std/Build/Cache.zig b/lib/std/Build/Cache.zig index 0bfaf283db..c18a748de2 100644 --- a/lib/std/Build/Cache.zig +++ b/lib/std/Build/Cache.zig @@ -245,6 +245,11 @@ pub const HashHelper = struct { for (list_of_bytes) |bytes| hh.addBytes(bytes); } + pub fn addOptionalListOfBytes(hh: *HashHelper, optional_list_of_bytes: ?[]const []const u8) void { + hh.add(optional_list_of_bytes != null); + hh.addListOfBytes(optional_list_of_bytes orelse return); + } + /// Convert the input value into bytes and record it as a dependency of the process being cached. pub fn add(hh: *HashHelper, x: anytype) void { switch (@TypeOf(x)) { diff --git a/lib/std/Build/Step/ObjCopy.zig b/lib/std/Build/Step/ObjCopy.zig index e10c5ce9a9..5ad123f89f 100644 --- a/lib/std/Build/Step/ObjCopy.zig +++ b/lib/std/Build/Step/ObjCopy.zig @@ -33,7 +33,7 @@ output_file: std.Build.GeneratedFile, output_file_debug: ?std.Build.GeneratedFile, format: ?RawFormat, -only_section: ?[]const u8, +only_sections: ?[]const []const u8, pad_to: ?u64, strip: Strip, compress_debug: bool, @@ -41,7 +41,7 @@ compress_debug: bool, pub const Options = struct { basename: ?[]const u8 = null, format: ?RawFormat = null, - only_section: ?[]const u8 = null, + only_sections: ?[]const []const u8 = null, pad_to: ?u64 = null, compress_debug: bool = false, @@ -71,7 +71,7 @@ pub fn create( .output_file = std.Build.GeneratedFile{ .step = &self.step }, .output_file_debug = if (options.strip != .none and options.extract_to_separate_file) std.Build.GeneratedFile{ .step = &self.step } else null, .format = options.format, - .only_section = options.only_section, + .only_sections = options.only_sections, .pad_to = options.pad_to, .strip = options.strip, .compress_debug = options.compress_debug, @@ -103,7 +103,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { const full_src_path = self.input_file.getPath(b); _ = try man.addFile(full_src_path, null); - man.hash.addOptionalBytes(self.only_section); + man.hash.addOptionalListOfBytes(self.only_sections); man.hash.addOptional(self.pad_to); man.hash.addOptional(self.format); man.hash.add(self.compress_debug); @@ -135,8 +135,10 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { var argv = std.ArrayList([]const u8).init(b.allocator); try argv.appendSlice(&.{ b.graph.zig_exe, "objcopy" }); - if (self.only_section) |only_section| { - try argv.appendSlice(&.{ "-j", only_section }); + if (self.only_sections) |only_sections| { + for (only_sections) |only_section| { + try argv.appendSlice(&.{ "-j", only_section }); + } } switch (self.strip) { .none => {},