authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-01-13 17:57:56+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-01-13 20:02:11+01:00
log5cde5f947fa12440463f684d9417ac00c8b9790a
tree4cc1ba8e54327060e7009a923e2c79faddf5d29e
parenta8564df9ed510f572732b8179844299fc3d3c11b

Introduce LinkObject with must_link field


7 files changed, 74 insertions(+), 43 deletions(-)

src/Compilation.zig+13-6
......@@ -654,6 +654,11 @@ pub const ClangPreprocessorMode = enum {
654654pub const SystemLib = link.SystemLib;
655655pub const CacheMode = link.CacheMode;
656656
657pub const LinkObject = struct {
658 path: []const u8,
659 must_link: bool = false,
660};
661
657662pub const InitOptions = struct {
658663 zig_lib_directory: Directory,
659664 local_cache_directory: Directory,
......@@ -698,8 +703,7 @@ pub const InitOptions = struct {
698703 lib_dirs: []const []const u8 = &[0][]const u8{},
699704 rpath_list: []const []const u8 = &[0][]const u8{},
700705 c_source_files: []const CSourceFile = &[0]CSourceFile{},
701 link_objects: []const []const u8 = &[0][]const u8{},
702 must_link_objects: []const []const u8 = &[0][]const u8{},
706 link_objects: []LinkObject = &[0]LinkObject{},
703707 framework_dirs: []const []const u8 = &[0][]const u8{},
704708 frameworks: []const []const u8 = &[0][]const u8{},
705709 system_lib_names: []const []const u8 = &.{},
......@@ -1057,7 +1061,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
10571061 if (options.system_lib_names.len != 0)
10581062 break :x true;
10591063 for (options.link_objects) |obj| {
1060 switch (classifyFileExt(obj)) {
1064 switch (classifyFileExt(obj.path)) {
10611065 .shared_library => break :x true,
10621066 else => continue,
10631067 }
......@@ -1460,7 +1464,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
14601464 if (options.c_source_files.len >= 1) {
14611465 hash.addBytes(options.c_source_files[0].src_path);
14621466 } else if (options.link_objects.len >= 1) {
1463 hash.addBytes(options.link_objects[0]);
1467 hash.addBytes(options.link_objects[0].path);
14641468 }
14651469
14661470 const digest = hash.final();
......@@ -1536,7 +1540,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
15361540 .link_libcpp = link_libcpp,
15371541 .link_libunwind = link_libunwind,
15381542 .objects = options.link_objects,
1539 .must_link_objects = options.must_link_objects,
15401543 .frameworks = options.frameworks,
15411544 .framework_dirs = options.framework_dirs,
15421545 .system_libs = system_libs,
......@@ -2267,7 +2270,11 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
22672270
22682271 try man.addOptionalFile(comp.bin_file.options.linker_script);
22692272 try man.addOptionalFile(comp.bin_file.options.version_script);
2270 try man.addListOfFiles(comp.bin_file.options.objects);
2273
2274 for (comp.bin_file.options.objects) |obj| {
2275 _ = try man.addFile(obj.path, null);
2276 man.hash.add(obj.must_link);
2277 }
22712278
22722279 for (comp.c_object_table.keys()) |key| {
22732280 _ = try man.addFile(key.src.src_path, null);
src/link.zig+7-5
......@@ -155,8 +155,7 @@ pub const Options = struct {
155155 soname: ?[]const u8,
156156 llvm_cpu_features: ?[*:0]const u8,
157157
158 objects: []const []const u8,
159 must_link_objects: []const []const u8,
158 objects: []Compilation.LinkObject,
160159 framework_dirs: []const []const u8,
161160 frameworks: []const []const u8,
162161 system_libs: std.StringArrayHashMapUnmanaged(SystemLib),
......@@ -756,7 +755,10 @@ pub const File = struct {
756755 // We are about to obtain this lock, so here we give other processes a chance first.
757756 base.releaseLock();
758757
759 try man.addListOfFiles(base.options.objects);
758 for (base.options.objects) |obj| {
759 _ = try man.addFile(obj.path, null);
760 man.hash.add(obj.must_link);
761 }
760762 for (comp.c_object_table.keys()) |key| {
761763 _ = try man.addFile(key.status.success.object_path, null);
762764 }
......@@ -793,8 +795,8 @@ pub const File = struct {
793795 var object_files = try std.ArrayList([*:0]const u8).initCapacity(base.allocator, num_object_files);
794796 defer object_files.deinit();
795797
796 for (base.options.objects) |obj_path| {
797 object_files.appendAssumeCapacity(try arena.dupeZ(u8, obj_path));
798 for (base.options.objects) |obj| {
799 object_files.appendAssumeCapacity(try arena.dupeZ(u8, obj.path));
798800 }
799801 for (comp.c_object_table.keys()) |key| {
800802 object_files.appendAssumeCapacity(try arena.dupeZ(u8, key.status.success.object_path));
src/link/Coff.zig+9-3
......@@ -943,7 +943,10 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
943943
944944 comptime assert(Compilation.link_hash_implementation_version == 1);
945945
946 try man.addListOfFiles(self.base.options.objects);
946 for (self.base.options.objects) |obj| {
947 _ = try man.addFile(obj.path, null);
948 man.hash.add(obj.must_link);
949 }
947950 for (comp.c_object_table.keys()) |key| {
948951 _ = try man.addFile(key.status.success.object_path, null);
949952 }
......@@ -1005,7 +1008,7 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
10051008 // build-obj. See also the corresponding TODO in linkAsArchive.
10061009 const the_object_path = blk: {
10071010 if (self.base.options.objects.len != 0)
1008 break :blk self.base.options.objects[0];
1011 break :blk self.base.options.objects[0].path;
10091012
10101013 if (comp.c_object_table.count() != 0)
10111014 break :blk comp.c_object_table.keys()[0].status.success.object_path;
......@@ -1110,7 +1113,10 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
11101113 try argv.append(try allocPrint(arena, "-LIBPATH:{s}", .{lib_dir}));
11111114 }
11121115
1113 try argv.appendSlice(self.base.options.objects);
1116 try argv.ensureUnusedCapacity(self.base.options.objects.len);
1117 for (self.base.options.objects) |obj| {
1118 argv.appendAssumeCapacity(obj.path);
1119 }
11141120
11151121 for (comp.c_object_table.keys()) |key| {
11161122 try argv.append(key.status.success.object_path);
src/link/Elf.zig+9-3
......@@ -1384,7 +1384,10 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
13841384
13851385 try man.addOptionalFile(self.base.options.linker_script);
13861386 try man.addOptionalFile(self.base.options.version_script);
1387 try man.addListOfFiles(self.base.options.objects);
1387 for (self.base.options.objects) |obj| {
1388 _ = try man.addFile(obj.path, null);
1389 man.hash.add(obj.must_link);
1390 }
13881391 for (comp.c_object_table.keys()) |key| {
13891392 _ = try man.addFile(key.status.success.object_path, null);
13901393 }
......@@ -1469,7 +1472,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
14691472 // build-obj. See also the corresponding TODO in linkAsArchive.
14701473 const the_object_path = blk: {
14711474 if (self.base.options.objects.len != 0)
1472 break :blk self.base.options.objects[0];
1475 break :blk self.base.options.objects[0].path;
14731476
14741477 if (comp.c_object_table.count() != 0)
14751478 break :blk comp.c_object_table.keys()[0].status.success.object_path;
......@@ -1678,7 +1681,10 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
16781681 }
16791682
16801683 // Positional arguments to the linker such as object files.
1681 try argv.appendSlice(self.base.options.objects);
1684 try argv.ensureUnusedCapacity(self.base.options.objects.len);
1685 for (self.base.options.objects) |obj| {
1686 argv.appendAssumeCapacity(obj.path);
1687 }
16821688
16831689 for (comp.c_object_table.keys()) |key| {
16841690 try argv.append(key.status.success.object_path);
src/link/MachO.zig+18-13
......@@ -499,7 +499,10 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
499499
500500 comptime assert(Compilation.link_hash_implementation_version == 1);
501501
502 try man.addListOfFiles(self.base.options.objects);
502 for (self.base.options.objects) |obj| {
503 _ = try man.addFile(obj.path, null);
504 man.hash.add(obj.must_link);
505 }
503506 for (comp.c_object_table.keys()) |key| {
504507 _ = try man.addFile(key.status.success.object_path, null);
505508 }
......@@ -571,8 +574,9 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
571574 // here. TODO: think carefully about how we can avoid this redundant operation when doing
572575 // build-obj. See also the corresponding TODO in linkAsArchive.
573576 const the_object_path = blk: {
574 if (self.base.options.objects.len != 0)
575 break :blk self.base.options.objects[0];
577 if (self.base.options.objects.len != 0) {
578 break :blk self.base.options.objects[0].path;
579 }
576580
577581 if (comp.c_object_table.count() != 0)
578582 break :blk comp.c_object_table.keys()[0].status.success.object_path;
......@@ -679,19 +683,20 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
679683 // the relocatable object files.
680684 self.invalidate_relocs = true;
681685
682 // Unpack must-link archives
683 var must_link_archives = std.StringArrayHashMap(void).init(arena);
684 try must_link_archives.ensureTotalCapacity(@intCast(u32, self.base.options.must_link_objects.len));
685 for (self.base.options.must_link_objects) |lib| {
686 _ = must_link_archives.getOrPutAssumeCapacity(lib);
687 }
688
689686 // Positional arguments to the linker such as object files and static archives.
690687 var positionals = std.ArrayList([]const u8).init(arena);
691688 try positionals.ensureUnusedCapacity(self.base.options.objects.len);
689
690 var must_link_archives = std.StringArrayHashMap(void).init(arena);
691 try must_link_archives.ensureUnusedCapacity(self.base.options.objects.len);
692
692693 for (self.base.options.objects) |obj| {
693 if (must_link_archives.contains(obj)) continue;
694 _ = positionals.appendAssumeCapacity(obj);
694 if (must_link_archives.contains(obj.path)) continue;
695 if (obj.must_link) {
696 _ = must_link_archives.getOrPutAssumeCapacity(obj.path);
697 } else {
698 _ = positionals.appendAssumeCapacity(obj.path);
699 }
695700 }
696701
697702 for (comp.c_object_table.keys()) |key| {
......@@ -899,7 +904,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
899904 try argv.append("dynamic_lookup");
900905 }
901906
902 for (self.base.options.must_link_objects) |lib| {
907 for (must_link_archives.keys()) |lib| {
903908 try argv.append(try std.fmt.allocPrint(arena, "-force_load {s}", .{lib}));
904909 }
905910
src/link/Wasm.zig+9-3
......@@ -1128,7 +1128,10 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
11281128
11291129 comptime assert(Compilation.link_hash_implementation_version == 1);
11301130
1131 try man.addListOfFiles(self.base.options.objects);
1131 for (self.base.options.objects) |obj| {
1132 _ = try man.addFile(obj.path, null);
1133 man.hash.add(obj.must_link);
1134 }
11321135 for (comp.c_object_table.keys()) |key| {
11331136 _ = try man.addFile(key.status.success.object_path, null);
11341137 }
......@@ -1181,7 +1184,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
11811184 // build-obj. See also the corresponding TODO in linkAsArchive.
11821185 const the_object_path = blk: {
11831186 if (self.base.options.objects.len != 0)
1184 break :blk self.base.options.objects[0];
1187 break :blk self.base.options.objects[0].path;
11851188
11861189 if (comp.c_object_table.count() != 0)
11871190 break :blk comp.c_object_table.keys()[0].status.success.object_path;
......@@ -1346,7 +1349,10 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
13461349 }
13471350
13481351 // Positional arguments to the linker such as object files.
1349 try argv.appendSlice(self.base.options.objects);
1352 try argv.ensureUnusedCapacity(self.base.options.objects.len);
1353 for (self.base.options.objects) |obj| {
1354 argv.appendAssumeCapacity(obj.path);
1355 }
13501356
13511357 for (comp.c_object_table.keys()) |key| {
13521358 try argv.append(key.status.success.object_path);
src/main.zig+9-10
......@@ -705,12 +705,9 @@ fn buildOutputType(
705705 var c_source_files = std.ArrayList(Compilation.CSourceFile).init(gpa);
706706 defer c_source_files.deinit();
707707
708 var link_objects = std.ArrayList([]const u8).init(gpa);
708 var link_objects = std.ArrayList(Compilation.LinkObject).init(gpa);
709709 defer link_objects.deinit();
710710
711 var must_link_objects = std.ArrayList([]const u8).init(gpa);
712 defer must_link_objects.deinit();
713
714711 var framework_dirs = std.ArrayList([]const u8).init(gpa);
715712 defer framework_dirs.deinit();
716713
......@@ -1241,7 +1238,7 @@ fn buildOutputType(
12411238 }
12421239 } else switch (Compilation.classifyFileExt(arg)) {
12431240 .object, .static_library, .shared_library => {
1244 try link_objects.append(arg);
1241 try link_objects.append(.{ .path = arg });
12451242 },
12461243 .assembly, .c, .cpp, .h, .ll, .bc, .m, .mm => {
12471244 try c_source_files.append(.{
......@@ -1312,7 +1309,7 @@ fn buildOutputType(
13121309 switch (file_ext) {
13131310 .assembly, .c, .cpp, .ll, .bc, .h, .m, .mm => try c_source_files.append(.{ .src_path = it.only_arg }),
13141311 .unknown, .shared_library, .object, .static_library => {
1315 try link_objects.append(it.only_arg);
1312 try link_objects.append(.{ .path = it.only_arg });
13161313 },
13171314 .zig => {
13181315 if (root_src_file) |other| {
......@@ -1756,7 +1753,10 @@ fn buildOutputType(
17561753 if (i >= linker_args.items.len) {
17571754 fatal("expected linker arg after '{s}'", .{arg});
17581755 }
1759 try must_link_objects.append(linker_args.items[i]);
1756 try link_objects.append(.{
1757 .path = linker_args.items[i],
1758 .must_link = true,
1759 });
17601760 } else {
17611761 warn("unsupported linker arg: {s}", .{arg});
17621762 }
......@@ -1851,7 +1851,7 @@ fn buildOutputType(
18511851 const basename = fs.path.basename(c_source_files.items[0].src_path);
18521852 break :blk basename[0 .. basename.len - fs.path.extension(basename).len];
18531853 } else if (link_objects.items.len >= 1) {
1854 const basename = fs.path.basename(link_objects.items[0]);
1854 const basename = fs.path.basename(link_objects.items[0].path);
18551855 break :blk basename[0 .. basename.len - fs.path.extension(basename).len];
18561856 } else if (emit_bin == .yes) {
18571857 const basename = fs.path.basename(emit_bin.yes);
......@@ -2054,7 +2054,7 @@ fn buildOutputType(
20542054 test_path.items, @errorName(e),
20552055 }),
20562056 };
2057 try link_objects.append(try arena.dupe(u8, test_path.items));
2057 try link_objects.append(.{ .path = try arena.dupe(u8, test_path.items) });
20582058 break;
20592059 } else {
20602060 var search_paths = std.ArrayList(u8).init(arena);
......@@ -2447,7 +2447,6 @@ fn buildOutputType(
24472447 .rpath_list = rpath_list.items,
24482448 .c_source_files = c_source_files.items,
24492449 .link_objects = link_objects.items,
2450 .must_link_objects = must_link_objects.items,
24512450 .framework_dirs = framework_dirs.items,
24522451 .frameworks = frameworks.items,
24532452 .system_lib_names = system_libs.keys(),