authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-11-30 16:35:00-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-11-30 16:35:00-07:00
logff9798eb265b02d572ecbced675efcd7c763aea9
treea76c48fca7b7458f41c4dca14f9ec9eb30325818
parent2fae28b6afc0e5411c9a9a9def43eeb59fba840f

rework the bundle compiler-rt feature

* it is now -fcompiler-rt and -fno-compiler-rt to override the (quite reasonable) default of bundling compiler-rt only for executables and dynamic libraries. - the build.zig API is still called bundle_compiler_rt however it is now an optional bool instead of a bool. leaving it as `null` means to use the compiler default. * renamed some internal identifiers to make the source more readable * additionally support -fcompiler-rt when doing build-obj for ELF files since that target already supports linking multiple objects into one. - includes an error message when attempting this for non-ELF. in the future this could additionally be supported with a more advanced implementation that does not rely on the linker. * properly populate the linker cache hash

5 files changed, 79 insertions(+), 56 deletions(-)

lib/std/build.zig+7-4
......@@ -1216,7 +1216,7 @@ pub const LibExeObjStep = struct {
12161216 emit_bin: bool = true,
12171217 emit_docs: bool = false,
12181218 emit_h: bool = false,
1219 bundle_compiler_rt: bool,
1219 bundle_compiler_rt: ?bool = null,
12201220 disable_stack_probing: bool,
12211221 disable_sanitize_c: bool,
12221222 rdynamic: bool,
......@@ -1395,7 +1395,6 @@ pub const LibExeObjStep = struct {
13951395 .exec_cmd_args = null,
13961396 .name_prefix = "",
13971397 .filter = null,
1398 .bundle_compiler_rt = false,
13991398 .disable_stack_probing = false,
14001399 .disable_sanitize_c = false,
14011400 .rdynamic = false,
......@@ -2120,8 +2119,12 @@ pub const LibExeObjStep = struct {
21202119 if (self.is_dynamic) {
21212120 try zig_args.append("-dynamic");
21222121 }
2123 if (self.bundle_compiler_rt) {
2124 try zig_args.append("--bundle-compiler-rt");
2122 if (self.bundle_compiler_rt) |x| {
2123 if (x) {
2124 try zig_args.append("-fcompiler-rt");
2125 } else {
2126 try zig_args.append("-fno-compiler-rt");
2127 }
21252128 }
21262129 if (self.disable_stack_probing) {
21272130 try zig_args.append("-fno-stack-check");
src/Compilation.zig+38-36
......@@ -167,9 +167,7 @@ const Job = union(enum) {
167167 libcxx: void,
168168 libcxxabi: void,
169169 libssp: void,
170 /// needed when producing a dynamic library or executable
171 libcompiler_rt: void,
172 /// needed when producing a static library with bundle-compiler-rt
170 compiler_rt_lib: void,
173171 compiler_rt_obj: void,
174172 /// needed when not linking libc and using LLVM for code generation because it generates
175173 /// calls to, for example, memcpy and memset.
......@@ -355,6 +353,7 @@ pub const InitOptions = struct {
355353 want_sanitize_c: ?bool = null,
356354 want_stack_check: ?bool = null,
357355 want_valgrind: ?bool = null,
356 want_compiler_rt: ?bool = null,
358357 use_llvm: ?bool = null,
359358 use_lld: ?bool = null,
360359 use_clang: ?bool = null,
......@@ -392,7 +391,6 @@ pub const InitOptions = struct {
392391 parent_compilation_link_libc: bool = false,
393392 stack_size_override: ?u64 = null,
394393 image_base_override: ?u64 = null,
395 bundle_compiler_rt: bool = false,
396394 self_exe_path: ?[]const u8 = null,
397395 version: ?std.builtin.Version = null,
398396 libc_installation: ?*const LibCInstallation = null,
......@@ -410,15 +408,14 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
410408 .Obj, .Exe => false,
411409 .Lib => (options.link_mode orelse .Static) == .Dynamic,
412410 };
413 const is_static_lib = switch (options.output_mode) {
414 .Obj, .Exe => false,
415 .Lib => (options.link_mode orelse .Static) == .Static,
416 };
417411 const is_exe_or_dyn_lib = switch (options.output_mode) {
418412 .Obj => false,
419413 .Lib => is_dyn_lib,
420414 .Exe => true,
421415 };
416 const needs_c_symbols = !options.is_compiler_rt_or_libc and
417 (is_exe_or_dyn_lib or (options.target.isWasm() and options.output_mode != .Obj));
418
422419 const comp: *Compilation = comp: {
423420 // For allocations that have the same lifetime as Compilation. This arena is used only during this
424421 // initialization and then is freed in deinit().
......@@ -595,6 +592,8 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
595592 break :b options.want_valgrind orelse (options.optimize_mode == .Debug);
596593 };
597594
595 const include_compiler_rt = options.want_compiler_rt orelse needs_c_symbols;
596
598597 const single_threaded = options.single_threaded or target_util.isSingleThreaded(options.target);
599598
600599 const llvm_cpu_features: ?[*:0]const u8 = if (build_options.have_llvm and use_llvm) blk: {
......@@ -831,7 +830,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
831830 .z_defs = options.linker_z_defs,
832831 .stack_size_override = options.stack_size_override,
833832 .image_base_override = options.image_base_override,
834 .bundle_compiler_rt = options.bundle_compiler_rt,
833 .include_compiler_rt = include_compiler_rt,
835834 .linker_script = options.linker_script,
836835 .version_script = options.version_script,
837836 .gc_sections = options.linker_gc_sections,
......@@ -978,24 +977,31 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
978977 try comp.work_queue.writeItem(.libcxxabi);
979978 }
980979
981 const needs_libc = is_exe_or_dyn_lib or
982 (comp.getTarget().isWasm() and comp.bin_file.options.output_mode != .Obj);
983 const needs_compiler_rt = options.bundle_compiler_rt or needs_libc;
984
985 if (needs_compiler_rt and build_options.is_stage1) {
986 if (is_static_lib) {
987 try comp.work_queue.writeItem(.{ .compiler_rt_obj = {} });
988 } else {
989 try comp.work_queue.writeItem(.{ .libcompiler_rt = {} });
990 }
991 }
992 if (needs_libc and build_options.is_stage1) {
993 // MinGW provides no libssp, use our own implementation.
994 if (comp.getTarget().isMinGW()) {
995 try comp.work_queue.writeItem(.{ .libssp = {} });
980 // The `is_stage1` condition is here only because stage2 cannot yet build compiler-rt.
981 // Once it is capable this condition should be removed.
982 if (build_options.is_stage1) {
983 if (comp.bin_file.options.include_compiler_rt) {
984 if (is_exe_or_dyn_lib) {
985 try comp.work_queue.writeItem(.{ .compiler_rt_lib = {} });
986 } else {
987 try comp.work_queue.writeItem(.{ .compiler_rt_obj = {} });
988 if (comp.bin_file.options.object_format != .elf) {
989 // For ELF we can rely on using -r to link multiple objects together into one,
990 // but to truly support `build-obj -fcompiler-rt` will require virtually
991 // injecting `_ = @import("compiler_rt.zig")` into the root source file of
992 // the compilation.
993 fatal("Embedding compiler-rt into non-ELF objects is not yet implemented.", .{});
994 }
995 }
996996 }
997 if (!comp.bin_file.options.link_libc) {
998 try comp.work_queue.writeItem(.{ .zig_libc = {} });
997 if (needs_c_symbols) {
998 // MinGW provides no libssp, use our own implementation.
999 if (comp.getTarget().isMinGW()) {
1000 try comp.work_queue.writeItem(.{ .libssp = {} });
1001 }
1002 if (!comp.bin_file.options.link_libc) {
1003 try comp.work_queue.writeItem(.{ .zig_libc = {} });
1004 }
9991005 }
10001006 }
10011007 }
......@@ -1393,26 +1399,26 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
13931399 fatal("unable to build libcxxabi: {}", .{@errorName(err)});
13941400 };
13951401 },
1396 .libcompiler_rt => {
1397 self.buildStaticLibFromZig("compiler_rt.zig", &self.compiler_rt_static_lib) catch |err| {
1402 .compiler_rt_lib => {
1403 self.buildOutputFromZig("compiler_rt.zig", .Lib, &self.compiler_rt_static_lib) catch |err| {
13981404 // TODO Expose this as a normal compile error rather than crashing here.
1399 fatal("unable to build compiler_rt: {}", .{@errorName(err)});
1405 fatal("unable to build compiler_rt: {s}", .{@errorName(err)});
14001406 };
14011407 },
14021408 .compiler_rt_obj => {
14031409 self.buildOutputFromZig("compiler_rt.zig", .Obj, &self.compiler_rt_obj) catch |err| {
14041410 // TODO Expose this as a normal compile error rather than crashing here.
1405 fatal("unable to build compiler_rt: {}", .{@errorName(err)});
1411 fatal("unable to build compiler_rt: {s}", .{@errorName(err)});
14061412 };
14071413 },
14081414 .libssp => {
1409 self.buildStaticLibFromZig("ssp.zig", &self.libssp_static_lib) catch |err| {
1415 self.buildOutputFromZig("ssp.zig", .Lib, &self.libssp_static_lib) catch |err| {
14101416 // TODO Expose this as a normal compile error rather than crashing here.
14111417 fatal("unable to build libssp: {}", .{@errorName(err)});
14121418 };
14131419 },
14141420 .zig_libc => {
1415 self.buildStaticLibFromZig("c.zig", &self.libc_static_lib) catch |err| {
1421 self.buildOutputFromZig("c.zig", .Lib, &self.libc_static_lib) catch |err| {
14161422 // TODO Expose this as a normal compile error rather than crashing here.
14171423 fatal("unable to build zig's multitarget libc: {}", .{@errorName(err)});
14181424 };
......@@ -2670,10 +2676,6 @@ fn buildOutputFromZig(
26702676 };
26712677}
26722678
2673fn buildStaticLibFromZig(comp: *Compilation, src_basename: []const u8, out: *?CRTFile) !void {
2674 return buildOutputFromZig(comp, src_basename, .Lib, out);
2675}
2676
26772679fn updateStage1Module(comp: *Compilation, main_progress_node: *std.Progress.Node) !void {
26782680 const tracy = trace(@src());
26792681 defer tracy.end();
src/link.zig+10-5
......@@ -46,7 +46,7 @@ pub const Options = struct {
4646 entry_addr: ?u64 = null,
4747 stack_size_override: ?u64,
4848 image_base_override: ?u64,
49 bundle_compiler_rt: bool,
49 include_compiler_rt: bool,
5050 /// Set to `true` to omit debug info.
5151 strip: bool,
5252 /// If this is true then this link code is responsible for outputting an object
......@@ -474,6 +474,11 @@ pub const File = struct {
474474 break :blk full_obj_path;
475475 } else null;
476476
477 const compiler_rt_path: ?[]const u8 = if (base.options.include_compiler_rt)
478 comp.compiler_rt_obj.?.full_object_path
479 else
480 null;
481
477482 // This function follows the same pattern as link.Elf.linkWithLLD so if you want some
478483 // insight as to what's going on here you can read that function body which is more
479484 // well-commented.
......@@ -490,6 +495,7 @@ pub const File = struct {
490495 _ = try ch.addFile(entry.key.status.success.object_path, null);
491496 }
492497 try ch.addOptionalFile(module_obj_path);
498 try ch.addOptionalFile(compiler_rt_path);
493499
494500 // We don't actually care whether it's a cache hit or miss; we just need the digest and the lock.
495501 _ = try ch.hit();
......@@ -519,8 +525,7 @@ pub const File = struct {
519525 var object_files = std.ArrayList([*:0]const u8).init(base.allocator);
520526 defer object_files.deinit();
521527
522 try object_files.ensureCapacity(base.options.objects.len + comp.c_object_table.items().len +
523 1 + @boolToInt(base.options.bundle_compiler_rt));
528 try object_files.ensureCapacity(base.options.objects.len + comp.c_object_table.items().len + 2);
524529 for (base.options.objects) |obj_path| {
525530 object_files.appendAssumeCapacity(try arena.dupeZ(u8, obj_path));
526531 }
......@@ -530,8 +535,8 @@ pub const File = struct {
530535 if (module_obj_path) |p| {
531536 object_files.appendAssumeCapacity(try arena.dupeZ(u8, p));
532537 }
533 if (base.options.bundle_compiler_rt) {
534 object_files.appendAssumeCapacity(try arena.dupeZ(u8, comp.compiler_rt_obj.?.full_object_path));
538 if (compiler_rt_path) |p| {
539 object_files.appendAssumeCapacity(try arena.dupeZ(u8, p));
535540 }
536541
537542 const full_out_path = try directory.join(arena, &[_][]const u8{base.options.emit.?.sub_path});
src/link/Elf.zig+17-6
......@@ -1260,6 +1260,13 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
12601260 const gc_sections = self.base.options.gc_sections orelse !is_obj;
12611261 const stack_size = self.base.options.stack_size_override orelse 16777216;
12621262 const allow_shlib_undefined = self.base.options.allow_shlib_undefined orelse !self.base.options.is_native_os;
1263 const compiler_rt_path: ?[]const u8 = if (self.base.options.include_compiler_rt) blk: {
1264 if (is_exe_or_dyn_lib) {
1265 break :blk comp.compiler_rt_static_lib.?.full_object_path;
1266 } else {
1267 break :blk comp.compiler_rt_obj.?.full_object_path;
1268 }
1269 } else null;
12631270
12641271 // Here we want to determine whether we can save time by not invoking LLD when the
12651272 // output is unchanged. None of the linker options or the object files that are being
......@@ -1289,6 +1296,8 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
12891296 _ = try man.addFile(entry.key.status.success.object_path, null);
12901297 }
12911298 try man.addOptionalFile(module_obj_path);
1299 try man.addOptionalFile(compiler_rt_path);
1300
12921301 // We can skip hashing libc and libc++ components that we are in charge of building from Zig
12931302 // installation sources because they are always a product of the compiler version + target information.
12941303 man.hash.add(stack_size);
......@@ -1531,12 +1540,14 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
15311540 try argv.append(p);
15321541 }
15331542
1534 // compiler-rt and libc
1535 if (is_exe_or_dyn_lib and !self.base.options.is_compiler_rt_or_libc) {
1536 if (!self.base.options.link_libc) {
1537 try argv.append(comp.libc_static_lib.?.full_object_path);
1538 }
1539 try argv.append(comp.compiler_rt_static_lib.?.full_object_path);
1543 // libc
1544 if (is_exe_or_dyn_lib and !self.base.options.is_compiler_rt_or_libc and !self.base.options.link_libc) {
1545 try argv.append(comp.libc_static_lib.?.full_object_path);
1546 }
1547
1548 // compiler-rt
1549 if (compiler_rt_path) |p| {
1550 try argv.append(p);
15401551 }
15411552
15421553 // Shared libraries.
src/main.zig+7-5
......@@ -314,7 +314,7 @@ const usage_build_generic =
314314 \\ -fno-soname (Linux) Disable emitting a SONAME
315315 \\ -fLLD Force using LLD as the linker
316316 \\ -fno-LLD Prevent using LLD as the linker
317 \\ -fcompiler-rt Always including compiler-rt symbols in output
317 \\ -fcompiler-rt Always include compiler-rt symbols in output
318318 \\ -fno-compiler-rt Prevent including compiler-rt symbols in output
319319 \\ -rdynamic Add all symbols to the dynamic symbol table
320320 \\ -rpath [path] Add directory to the runtime library search path
......@@ -480,6 +480,7 @@ fn buildOutputType(
480480 var want_sanitize_c: ?bool = null;
481481 var want_stack_check: ?bool = null;
482482 var want_valgrind: ?bool = null;
483 var want_compiler_rt: ?bool = null;
483484 var rdynamic: bool = false;
484485 var linker_script: ?[]const u8 = null;
485486 var version_script: ?[]const u8 = null;
......@@ -492,7 +493,6 @@ fn buildOutputType(
492493 var test_evented_io = false;
493494 var stack_size_override: ?u64 = null;
494495 var image_base_override: ?u64 = null;
495 var bundle_compiler_rt = false;
496496 var use_llvm: ?bool = null;
497497 var use_lld: ?bool = null;
498498 var use_clang: ?bool = null;
......@@ -797,8 +797,10 @@ fn buildOutputType(
797797 if (i + 1 >= args.len) fatal("expected parameter after {}", .{arg});
798798 i += 1;
799799 override_lib_dir = args[i];
800 } else if (mem.eql(u8, arg, "--bundle-compiler-rt")) {
801 bundle_compiler_rt = true;
800 } else if (mem.eql(u8, arg, "-fcompiler-rt")) {
801 want_compiler_rt = true;
802 } else if (mem.eql(u8, arg, "-fno-compiler-rt")) {
803 want_compiler_rt = false;
802804 } else if (mem.eql(u8, arg, "-feach-lib-rpath")) {
803805 each_lib_rpath = true;
804806 } else if (mem.eql(u8, arg, "-fno-each-lib-rpath")) {
......@@ -1693,6 +1695,7 @@ fn buildOutputType(
16931695 .want_sanitize_c = want_sanitize_c,
16941696 .want_stack_check = want_stack_check,
16951697 .want_valgrind = want_valgrind,
1698 .want_compiler_rt = want_compiler_rt,
16961699 .use_llvm = use_llvm,
16971700 .use_lld = use_lld,
16981701 .use_clang = use_clang,
......@@ -1710,7 +1713,6 @@ fn buildOutputType(
17101713 .link_emit_relocs = link_emit_relocs,
17111714 .stack_size_override = stack_size_override,
17121715 .image_base_override = image_base_override,
1713 .bundle_compiler_rt = bundle_compiler_rt,
17141716 .strip = strip,
17151717 .single_threaded = single_threaded,
17161718 .function_sections = function_sections,