Commit 3c907e51d1

Andrew Kelley <andrew@ziglang.org>
2020-12-01 01:46:10
fix regression on wasm targets
The previous commit broke wasm targets because the linking step would look for the compiler-rt lib in the wrong place. Fixed in this commit.
1 parent 791e38d
Changed files (2)
src/link/Wasm.zig
@@ -282,6 +282,11 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
         break :blk full_obj_path;
     } else null;
 
+    const compiler_rt_path: ?[]const u8 = if (self.base.options.include_compiler_rt)
+        comp.compiler_rt_static_lib.?.full_object_path
+    else
+        null;
+
     const target = self.base.options.target;
 
     const id_symlink_basename = "lld.id";
@@ -302,6 +307,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
             _ = try man.addFile(entry.key.status.success.object_path, null);
         }
         try man.addOptionalFile(module_obj_path);
+        try man.addOptionalFile(compiler_rt_path);
         man.hash.addOptional(self.base.options.stack_size_override);
         man.hash.addListOfBytes(self.base.options.extra_lld_args);
 
@@ -378,11 +384,15 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
         try argv.append(p);
     }
 
-    if (self.base.options.output_mode != .Obj and !self.base.options.is_compiler_rt_or_libc) {
-        if (!self.base.options.link_libc) {
-            try argv.append(comp.libc_static_lib.?.full_object_path);
-        }
-        try argv.append(comp.compiler_rt_static_lib.?.full_object_path);
+    if (self.base.options.output_mode != .Obj and
+        !self.base.options.is_compiler_rt_or_libc and
+        !self.base.options.link_libc)
+    {
+        try argv.append(comp.libc_static_lib.?.full_object_path);
+    }
+
+    if (compiler_rt_path) |p| {
+        try argv.append(p);
     }
 
     if (self.base.options.verbose_link) {
src/Compilation.zig
@@ -961,16 +961,20 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
         // Once it is capable this condition should be removed.
         if (build_options.is_stage1) {
             if (comp.bin_file.options.include_compiler_rt) {
-                if (is_exe_or_dyn_lib) {
+                if (is_exe_or_dyn_lib or comp.getTarget().isWasm()) {
                     try comp.work_queue.writeItem(.{ .compiler_rt_lib = {} });
                 } else {
                     try comp.work_queue.writeItem(.{ .compiler_rt_obj = {} });
-                    if (comp.bin_file.options.object_format != .elf) {
+                    if (comp.bin_file.options.object_format != .elf and
+                        comp.bin_file.options.output_mode == .Obj)
+                    {
                         // For ELF we can rely on using -r to link multiple objects together into one,
                         // but to truly support `build-obj -fcompiler-rt` will require virtually
                         // injecting `_ = @import("compiler_rt.zig")` into the root source file of
                         // the compilation.
-                        fatal("Embedding compiler-rt into non-ELF objects is not yet implemented.", .{});
+                        fatal("Embedding compiler-rt into {s} objects is not yet implemented.", .{
+                            @tagName(comp.bin_file.options.object_format),
+                        });
                     }
                 }
             }