Commit 4e6f21e2cb

Jakub Konka <kubkon@jakubkonka.com>
2023-02-14 11:42:49
comp: reinstate -fcompiler-rt when used with build-obj as output
When the following is specified ``` $ zig build-obj -fcompiler-rt example.zig ``` the resulting relocatable object file will have the compiler-rt unconditionally embedded inside. ``` $ nm example.o ... 0000000012345678 W __truncsfhf2 ... ```
1 parent 47e14b7
Changed files (1)
src/Compilation.zig
@@ -1635,10 +1635,25 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
             } else main_pkg;
             errdefer if (options.is_test) root_pkg.destroy(gpa);
 
+            const compiler_rt_pkg = if (include_compiler_rt and options.output_mode == .Obj) compiler_rt_pkg: {
+                break :compiler_rt_pkg try Package.createWithDir(
+                    gpa,
+                    "compiler_rt",
+                    options.zig_lib_directory,
+                    null,
+                    "compiler_rt.zig",
+                );
+            } else null;
+            errdefer if (compiler_rt_pkg) |p| p.destroy(gpa);
+
             try main_pkg.addAndAdopt(gpa, builtin_pkg);
             try main_pkg.add(gpa, root_pkg);
             try main_pkg.addAndAdopt(gpa, std_pkg);
 
+            if (compiler_rt_pkg) |p| {
+                try main_pkg.addAndAdopt(gpa, p);
+            }
+
             const main_pkg_is_std = m: {
                 const std_path = try std.fs.path.resolve(arena, &[_][]const u8{
                     std_pkg.root_src_directory.path orelse ".",
@@ -2355,6 +2370,10 @@ pub fn update(comp: *Compilation) !void {
             _ = try module.importPkg(module.main_pkg);
         }
 
+        if (module.main_pkg.table.get("compiler_rt")) |compiler_rt_pkg| {
+            _ = try module.importPkg(compiler_rt_pkg);
+        }
+
         // Put a work item in for every known source file to detect if
         // it changed, and, if so, re-compute ZIR and then queue the job
         // to update it.
@@ -2379,6 +2398,10 @@ pub fn update(comp: *Compilation) !void {
         if (comp.bin_file.options.is_test) {
             try comp.work_queue.writeItem(.{ .analyze_pkg = module.main_pkg });
         }
+
+        if (module.main_pkg.table.get("compiler_rt")) |compiler_rt_pkg| {
+            try comp.work_queue.writeItem(.{ .analyze_pkg = compiler_rt_pkg });
+        }
     }
 
     // If the terminal is dumb, we dont want to show the user all the output.