authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-12-16 15:49:25-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-01 17:51:20-07:00
log2d2a18831e9821e22d23b4a15ffd20e30e24b27b
tree4e1ba354fe14990efbab35c6ba3e8a85452fc3b8
parent9b98d33d0b76a8c8b23958c89ab7e952893697be

linker: rename intermediary_basname to zcu_object_sub_path


7 files changed, 41 insertions(+), 34 deletions(-)

src/link.zig+5-5
......@@ -58,7 +58,7 @@ pub const File = struct {
5858 file: ?fs.File,
5959 /// When linking with LLD, this linker code will output an object file only at
6060 /// this location, and then this path can be placed on the LLD linker line.
61 intermediary_basename: ?[]const u8 = null,
61 zcu_object_sub_path: ?[]const u8 = null,
6262 disable_lld_caching: bool,
6363 gc_sections: bool,
6464 print_gc_sections: bool,
......@@ -289,7 +289,7 @@ pub const File = struct {
289289 switch (base.tag) {
290290 .elf => if (base.file) |f| {
291291 if (build_options.only_c) unreachable;
292 if (base.intermediary_basename != null and use_lld) {
292 if (base.zcu_object_sub_path != null and use_lld) {
293293 // The file we have open is not the final file that we want to
294294 // make executable, so we don't have to close it.
295295 return;
......@@ -308,7 +308,7 @@ pub const File = struct {
308308 },
309309 .coff, .macho, .plan9, .wasm => if (base.file) |f| {
310310 if (build_options.only_c) unreachable;
311 if (base.intermediary_basename != null) {
311 if (base.zcu_object_sub_path != null) {
312312 // The file we have open is not the final file that we want to
313313 // make executable, so we don't have to close it.
314314 return;
......@@ -734,7 +734,7 @@ pub const File = struct {
734734 try base.flushModule(comp, prog_node);
735735
736736 const dirname = fs.path.dirname(full_out_path_z) orelse ".";
737 break :blk try fs.path.join(arena, &.{ dirname, base.intermediary_basename.? });
737 break :blk try fs.path.join(arena, &.{ dirname, base.zcu_object_sub_path.? });
738738 } else null;
739739
740740 log.debug("zcu_obj_path={s}", .{if (zcu_obj_path) |s| s else "(null)"});
......@@ -1022,7 +1022,7 @@ pub const File = struct {
10221022 .pre_bc_path = comp.verbose_llvm_bc,
10231023 .bin_path = try base.resolveEmitLoc(arena, .{
10241024 .directory = null,
1025 .basename = base.intermediary_basename.?,
1025 .basename = base.zcu_object_sub_path.?,
10261026 }),
10271027 .asm_path = try base.resolveEmitLoc(arena, comp.emit_asm),
10281028 .post_ir_path = try base.resolveEmitLoc(arena, comp.emit_llvm_ir),
src/link/Coff.zig+2-2
......@@ -3,7 +3,7 @@
33//! LLD for traditional linking (linking relocatable object files).
44//! LLD is also the default linker for LLVM.
55
6/// If this is not null, an object file is created by LLVM and emitted to intermediary_basename.
6/// If this is not null, an object file is created by LLVM and emitted to zcu_object_sub_path.
77llvm_object: ?*LlvmObject = null,
88
99base: link.File,
......@@ -261,7 +261,7 @@ pub fn open(
261261 const o_file_path = try std.fmt.allocPrint(arena, "{s}{s}", .{
262262 emit.sub_path, target.ofmt.fileExt(target.cpu.arch),
263263 });
264 self.base.intermediary_basename = o_file_path;
264 self.base.zcu_object_sub_path = o_file_path;
265265 break :p o_file_path;
266266 };
267267
src/link/Coff/lld.zig+2-2
......@@ -35,9 +35,9 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
3535 try self.flushModule(comp, prog_node);
3636
3737 if (fs.path.dirname(full_out_path)) |dirname| {
38 break :blk try fs.path.join(arena, &.{ dirname, self.base.intermediary_basename.? });
38 break :blk try fs.path.join(arena, &.{ dirname, self.base.zcu_object_sub_path.? });
3939 } else {
40 break :blk self.base.intermediary_basename.?;
40 break :blk self.base.zcu_object_sub_path.?;
4141 }
4242 } else null;
4343
src/link/Elf.zig+22-15
......@@ -28,7 +28,7 @@ print_map: bool,
2828
2929ptr_width: PtrWidth,
3030
31/// If this is not null, an object file is created by LLVM and emitted to intermediary_basename.
31/// If this is not null, an object file is created by LLVM and emitted to zcu_object_sub_path.
3232llvm_object: ?*LlvmObject = null,
3333
3434/// A list of all input files.
......@@ -259,12 +259,25 @@ pub fn createEmpty(
259259 else
260260 elf.VER_NDX_LOCAL;
261261
262 // If using LLD to link, this code should produce an object file so that it
263 // can be passed to LLD.
264 // If using LLVM to generate the object file for the zig compilation unit,
265 // we need a place to put the object file so that it can be subsequently
266 // handled.
267 const zcu_object_sub_path = if (!use_lld and !use_llvm) null else p: {
268 const o_file_path = try std.fmt.allocPrint(arena, "{s}{s}", .{
269 emit.sub_path, target.ofmt.fileExt(target.cpu.arch),
270 });
271 break :p o_file_path;
272 };
273
262274 const self = try arena.create(Elf);
263275 self.* = .{
264276 .base = .{
265277 .tag = .elf,
266278 .comp = comp,
267279 .emit = emit,
280 .zcu_object_sub_path = zcu_object_sub_path,
268281 .gc_sections = options.gc_sections orelse (optimize_mode != .Debug and output_mode != .Obj),
269282 .print_gc_sections = options.print_gc_sections,
270283 .stack_size = options.stack_size orelse 16777216,
......@@ -325,16 +338,10 @@ pub fn createEmpty(
325338 const is_obj = output_mode == .Obj;
326339 const is_obj_or_ar = is_obj or (output_mode == .Lib and link_mode == .Static);
327340
328 const sub_path = if (!use_lld) emit.sub_path else p: {
329 // Open a temporary object file, not the final output file because we
330 // want to link with LLD.
331 const o_file_path = try std.fmt.allocPrint(arena, "{s}{s}", .{
332 emit.sub_path, target.ofmt.fileExt(target.cpu.arch),
333 });
334 self.base.intermediary_basename = o_file_path;
335 break :p o_file_path;
336 };
337
341 // What path should this ELF linker code output to?
342 // If using LLD to link, this code should produce an object file so that it
343 // can be passed to LLD.
344 const sub_path = if (use_lld) zcu_object_sub_path.? else emit.sub_path;
338345 self.base.file = try emit.directory.handle.createFile(sub_path, .{
339346 .truncate = false,
340347 .read = true,
......@@ -1045,7 +1052,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10451052 const link_mode = comp.config.link_mode;
10461053 const directory = self.base.emit.directory; // Just an alias to make it shorter to type.
10471054 const full_out_path = try directory.join(arena, &[_][]const u8{self.base.emit.sub_path});
1048 const module_obj_path: ?[]const u8 = if (self.base.intermediary_basename) |path| blk: {
1055 const module_obj_path: ?[]const u8 = if (self.base.zcu_object_sub_path) |path| blk: {
10491056 if (fs.path.dirname(full_out_path)) |dirname| {
10501057 break :blk try fs.path.join(arena, &.{ dirname, path });
10511058 } else {
......@@ -1613,7 +1620,7 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
16131620 const link_mode = self.base.comp.config.link_mode;
16141621 const directory = self.base.emit.directory; // Just an alias to make it shorter to type.
16151622 const full_out_path = try directory.join(arena, &[_][]const u8{self.base.emit.sub_path});
1616 const module_obj_path: ?[]const u8 = if (self.base.intermediary_basename) |path| blk: {
1623 const module_obj_path: ?[]const u8 = if (self.base.zcu_object_sub_path) |path| blk: {
16171624 if (fs.path.dirname(full_out_path)) |dirname| {
16181625 break :blk try fs.path.join(arena, &.{ dirname, path });
16191626 } else {
......@@ -2356,9 +2363,9 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
23562363 try self.flushModule(comp, prog_node);
23572364
23582365 if (fs.path.dirname(full_out_path)) |dirname| {
2359 break :blk try fs.path.join(arena, &.{ dirname, self.base.intermediary_basename.? });
2366 break :blk try fs.path.join(arena, &.{ dirname, self.base.zcu_object_sub_path.? });
23602367 } else {
2361 break :blk self.base.intermediary_basename.?;
2368 break :blk self.base.zcu_object_sub_path.?;
23622369 }
23632370 } else null;
23642371
src/link/MachO.zig+3-3
......@@ -1,6 +1,6 @@
11base: File,
22
3/// If this is not null, an object file is created by LLVM and emitted to intermediary_basename.
3/// If this is not null, an object file is created by LLVM and emitted to zcu_object_sub_path.
44llvm_object: ?*LlvmObject = null,
55
66/// Debug symbols bundle (or dSym).
......@@ -208,10 +208,10 @@ pub fn open(
208208 errdefer self.base.destroy();
209209
210210 if (mode == .zld) {
211 // TODO this intermediary_basename isn't enough; in the case of `zig build-exe`,
211 // TODO this zcu_object_sub_path isn't enough; in the case of `zig build-exe`,
212212 // we also want to put the intermediary object file in the cache while the
213213 // main emit directory is the cwd.
214 self.base.intermediary_basename = sub_path;
214 self.base.zcu_object_sub_path = sub_path;
215215 return self;
216216 }
217217
src/link/MachO/zld.zig+2-2
......@@ -24,9 +24,9 @@ pub fn linkWithZld(
2424 try macho_file.flushModule(comp, prog_node);
2525
2626 if (fs.path.dirname(full_out_path)) |dirname| {
27 break :blk try fs.path.join(arena, &.{ dirname, macho_file.base.intermediary_basename.? });
27 break :blk try fs.path.join(arena, &.{ dirname, macho_file.base.zcu_object_sub_path.? });
2828 } else {
29 break :blk macho_file.base.intermediary_basename.?;
29 break :blk macho_file.base.zcu_object_sub_path.?;
3030 }
3131 } else null;
3232
src/link/Wasm.zig+5-5
......@@ -401,7 +401,7 @@ pub fn open(
401401 const o_file_path = try std.fmt.allocPrint(arena, "{s}{s}", .{
402402 emit.sub_path, target.ofmt.fileExt(target.cpu.arch),
403403 });
404 wasm.base.intermediary_basename = o_file_path;
404 wasm.base.zcu_object_sub_path = o_file_path;
405405 break :p o_file_path;
406406 };
407407
......@@ -3511,9 +3511,9 @@ fn linkWithZld(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) l
35113511 try wasm.flushModule(comp, prog_node);
35123512
35133513 if (fs.path.dirname(full_out_path)) |dirname| {
3514 break :blk try fs.path.join(arena, &.{ dirname, wasm.base.intermediary_basename.? });
3514 break :blk try fs.path.join(arena, &.{ dirname, wasm.base.zcu_object_sub_path.? });
35153515 } else {
3516 break :blk wasm.base.intermediary_basename.?;
3516 break :blk wasm.base.zcu_object_sub_path.?;
35173517 }
35183518 } else null;
35193519
......@@ -4604,9 +4604,9 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !
46044604 try wasm.flushModule(comp, prog_node);
46054605
46064606 if (fs.path.dirname(full_out_path)) |dirname| {
4607 break :blk try fs.path.join(arena, &.{ dirname, wasm.base.intermediary_basename.? });
4607 break :blk try fs.path.join(arena, &.{ dirname, wasm.base.zcu_object_sub_path.? });
46084608 } else {
4609 break :blk wasm.base.intermediary_basename.?;
4609 break :blk wasm.base.zcu_object_sub_path.?;
46104610 }
46114611 } else null;
46124612