Commit 2d2a18831e

Andrew Kelley <andrew@ziglang.org>
2023-12-16 23:49:25
linker: rename intermediary_basname to zcu_object_sub_path
1 parent 9b98d33
Changed files (7)
src/link/Coff/lld.zig
@@ -35,9 +35,9 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
         try self.flushModule(comp, prog_node);
 
         if (fs.path.dirname(full_out_path)) |dirname| {
-            break :blk try fs.path.join(arena, &.{ dirname, self.base.intermediary_basename.? });
+            break :blk try fs.path.join(arena, &.{ dirname, self.base.zcu_object_sub_path.? });
         } else {
-            break :blk self.base.intermediary_basename.?;
+            break :blk self.base.zcu_object_sub_path.?;
         }
     } else null;
 
src/link/MachO/zld.zig
@@ -24,9 +24,9 @@ pub fn linkWithZld(
         try macho_file.flushModule(comp, prog_node);
 
         if (fs.path.dirname(full_out_path)) |dirname| {
-            break :blk try fs.path.join(arena, &.{ dirname, macho_file.base.intermediary_basename.? });
+            break :blk try fs.path.join(arena, &.{ dirname, macho_file.base.zcu_object_sub_path.? });
         } else {
-            break :blk macho_file.base.intermediary_basename.?;
+            break :blk macho_file.base.zcu_object_sub_path.?;
         }
     } else null;
 
src/link/Coff.zig
@@ -3,7 +3,7 @@
 //! LLD for traditional linking (linking relocatable object files).
 //! LLD is also the default linker for LLVM.
 
-/// If this is not null, an object file is created by LLVM and emitted to intermediary_basename.
+/// If this is not null, an object file is created by LLVM and emitted to zcu_object_sub_path.
 llvm_object: ?*LlvmObject = null,
 
 base: link.File,
@@ -261,7 +261,7 @@ pub fn open(
         const o_file_path = try std.fmt.allocPrint(arena, "{s}{s}", .{
             emit.sub_path, target.ofmt.fileExt(target.cpu.arch),
         });
-        self.base.intermediary_basename = o_file_path;
+        self.base.zcu_object_sub_path = o_file_path;
         break :p o_file_path;
     };
 
src/link/Elf.zig
@@ -28,7 +28,7 @@ print_map: bool,
 
 ptr_width: PtrWidth,
 
-/// If this is not null, an object file is created by LLVM and emitted to intermediary_basename.
+/// If this is not null, an object file is created by LLVM and emitted to zcu_object_sub_path.
 llvm_object: ?*LlvmObject = null,
 
 /// A list of all input files.
@@ -259,12 +259,25 @@ pub fn createEmpty(
     else
         elf.VER_NDX_LOCAL;
 
+    // If using LLD to link, this code should produce an object file so that it
+    // can be passed to LLD.
+    // If using LLVM to generate the object file for the zig compilation unit,
+    // we need a place to put the object file so that it can be subsequently
+    // handled.
+    const zcu_object_sub_path = if (!use_lld and !use_llvm) null else p: {
+        const o_file_path = try std.fmt.allocPrint(arena, "{s}{s}", .{
+            emit.sub_path, target.ofmt.fileExt(target.cpu.arch),
+        });
+        break :p o_file_path;
+    };
+
     const self = try arena.create(Elf);
     self.* = .{
         .base = .{
             .tag = .elf,
             .comp = comp,
             .emit = emit,
+            .zcu_object_sub_path = zcu_object_sub_path,
             .gc_sections = options.gc_sections orelse (optimize_mode != .Debug and output_mode != .Obj),
             .print_gc_sections = options.print_gc_sections,
             .stack_size = options.stack_size orelse 16777216,
@@ -325,16 +338,10 @@ pub fn createEmpty(
     const is_obj = output_mode == .Obj;
     const is_obj_or_ar = is_obj or (output_mode == .Lib and link_mode == .Static);
 
-    const sub_path = if (!use_lld) emit.sub_path else p: {
-        // Open a temporary object file, not the final output file because we
-        // want to link with LLD.
-        const o_file_path = try std.fmt.allocPrint(arena, "{s}{s}", .{
-            emit.sub_path, target.ofmt.fileExt(target.cpu.arch),
-        });
-        self.base.intermediary_basename = o_file_path;
-        break :p o_file_path;
-    };
-
+    // What path should this ELF linker code output to?
+    // If using LLD to link, this code should produce an object file so that it
+    // can be passed to LLD.
+    const sub_path = if (use_lld) zcu_object_sub_path.? else emit.sub_path;
     self.base.file = try emit.directory.handle.createFile(sub_path, .{
         .truncate = false,
         .read = true,
@@ -1045,7 +1052,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
     const link_mode = comp.config.link_mode;
     const directory = self.base.emit.directory; // Just an alias to make it shorter to type.
     const full_out_path = try directory.join(arena, &[_][]const u8{self.base.emit.sub_path});
-    const module_obj_path: ?[]const u8 = if (self.base.intermediary_basename) |path| blk: {
+    const module_obj_path: ?[]const u8 = if (self.base.zcu_object_sub_path) |path| blk: {
         if (fs.path.dirname(full_out_path)) |dirname| {
             break :blk try fs.path.join(arena, &.{ dirname, path });
         } else {
@@ -1613,7 +1620,7 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
     const link_mode = self.base.comp.config.link_mode;
     const directory = self.base.emit.directory; // Just an alias to make it shorter to type.
     const full_out_path = try directory.join(arena, &[_][]const u8{self.base.emit.sub_path});
-    const module_obj_path: ?[]const u8 = if (self.base.intermediary_basename) |path| blk: {
+    const module_obj_path: ?[]const u8 = if (self.base.zcu_object_sub_path) |path| blk: {
         if (fs.path.dirname(full_out_path)) |dirname| {
             break :blk try fs.path.join(arena, &.{ dirname, path });
         } else {
@@ -2356,9 +2363,9 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
         try self.flushModule(comp, prog_node);
 
         if (fs.path.dirname(full_out_path)) |dirname| {
-            break :blk try fs.path.join(arena, &.{ dirname, self.base.intermediary_basename.? });
+            break :blk try fs.path.join(arena, &.{ dirname, self.base.zcu_object_sub_path.? });
         } else {
-            break :blk self.base.intermediary_basename.?;
+            break :blk self.base.zcu_object_sub_path.?;
         }
     } else null;
 
src/link/MachO.zig
@@ -1,6 +1,6 @@
 base: File,
 
-/// If this is not null, an object file is created by LLVM and emitted to intermediary_basename.
+/// If this is not null, an object file is created by LLVM and emitted to zcu_object_sub_path.
 llvm_object: ?*LlvmObject = null,
 
 /// Debug symbols bundle (or dSym).
@@ -208,10 +208,10 @@ pub fn open(
     errdefer self.base.destroy();
 
     if (mode == .zld) {
-        // TODO this intermediary_basename isn't enough; in the case of `zig build-exe`,
+        // TODO this zcu_object_sub_path isn't enough; in the case of `zig build-exe`,
         // we also want to put the intermediary object file in the cache while the
         // main emit directory is the cwd.
-        self.base.intermediary_basename = sub_path;
+        self.base.zcu_object_sub_path = sub_path;
         return self;
     }
 
src/link/Wasm.zig
@@ -401,7 +401,7 @@ pub fn open(
         const o_file_path = try std.fmt.allocPrint(arena, "{s}{s}", .{
             emit.sub_path, target.ofmt.fileExt(target.cpu.arch),
         });
-        wasm.base.intermediary_basename = o_file_path;
+        wasm.base.zcu_object_sub_path = o_file_path;
         break :p o_file_path;
     };
 
@@ -3511,9 +3511,9 @@ fn linkWithZld(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) l
         try wasm.flushModule(comp, prog_node);
 
         if (fs.path.dirname(full_out_path)) |dirname| {
-            break :blk try fs.path.join(arena, &.{ dirname, wasm.base.intermediary_basename.? });
+            break :blk try fs.path.join(arena, &.{ dirname, wasm.base.zcu_object_sub_path.? });
         } else {
-            break :blk wasm.base.intermediary_basename.?;
+            break :blk wasm.base.zcu_object_sub_path.?;
         }
     } else null;
 
@@ -4604,9 +4604,9 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !
         try wasm.flushModule(comp, prog_node);
 
         if (fs.path.dirname(full_out_path)) |dirname| {
-            break :blk try fs.path.join(arena, &.{ dirname, wasm.base.intermediary_basename.? });
+            break :blk try fs.path.join(arena, &.{ dirname, wasm.base.zcu_object_sub_path.? });
         } else {
-            break :blk wasm.base.intermediary_basename.?;
+            break :blk wasm.base.zcu_object_sub_path.?;
         }
     } else null;
 
src/link.zig
@@ -58,7 +58,7 @@ pub const File = struct {
     file: ?fs.File,
     /// When linking with LLD, this linker code will output an object file only at
     /// this location, and then this path can be placed on the LLD linker line.
-    intermediary_basename: ?[]const u8 = null,
+    zcu_object_sub_path: ?[]const u8 = null,
     disable_lld_caching: bool,
     gc_sections: bool,
     print_gc_sections: bool,
@@ -289,7 +289,7 @@ pub const File = struct {
         switch (base.tag) {
             .elf => if (base.file) |f| {
                 if (build_options.only_c) unreachable;
-                if (base.intermediary_basename != null and use_lld) {
+                if (base.zcu_object_sub_path != null and use_lld) {
                     // The file we have open is not the final file that we want to
                     // make executable, so we don't have to close it.
                     return;
@@ -308,7 +308,7 @@ pub const File = struct {
             },
             .coff, .macho, .plan9, .wasm => if (base.file) |f| {
                 if (build_options.only_c) unreachable;
-                if (base.intermediary_basename != null) {
+                if (base.zcu_object_sub_path != null) {
                     // The file we have open is not the final file that we want to
                     // make executable, so we don't have to close it.
                     return;
@@ -734,7 +734,7 @@ pub const File = struct {
             try base.flushModule(comp, prog_node);
 
             const dirname = fs.path.dirname(full_out_path_z) orelse ".";
-            break :blk try fs.path.join(arena, &.{ dirname, base.intermediary_basename.? });
+            break :blk try fs.path.join(arena, &.{ dirname, base.zcu_object_sub_path.? });
         } else null;
 
         log.debug("zcu_obj_path={s}", .{if (zcu_obj_path) |s| s else "(null)"});
@@ -1022,7 +1022,7 @@ pub const File = struct {
             .pre_bc_path = comp.verbose_llvm_bc,
             .bin_path = try base.resolveEmitLoc(arena, .{
                 .directory = null,
-                .basename = base.intermediary_basename.?,
+                .basename = base.zcu_object_sub_path.?,
             }),
             .asm_path = try base.resolveEmitLoc(arena, comp.emit_asm),
             .post_ir_path = try base.resolveEmitLoc(arena, comp.emit_llvm_ir),