authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2020-11-07 11:03:13+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-11-30 14:31:41-07:00
log2fae28b6afc0e5411c9a9a9def43eeb59fba840f
treea19b223f9cdb4fecd3b4d0601f9417606c704b3e
parent51d7c14ce1bdadc4405474cfb3945581c198c741

Added bundle-compiler-rt flag


3 files changed, 54 insertions(+), 8 deletions(-)

src/Compilation.zig+42-7
......@@ -93,6 +93,9 @@ libc_static_lib: ?CRTFile = null,
9393/// Populated when we build the libcompiler_rt static library. A Job to build this is placed in the queue
9494/// and resolved before calling linker.flush().
9595compiler_rt_static_lib: ?CRTFile = null,
96/// Populated when we build the compiler_rt_obj object. A Job to build this is placed in the queue
97/// and resolved before calling linker.flush().
98compiler_rt_obj: ?CRTFile = null,
9699
97100glibc_so_files: ?glibc.BuiltSharedObjects = null,
98101
......@@ -166,6 +169,8 @@ const Job = union(enum) {
166169 libssp: void,
167170 /// needed when producing a dynamic library or executable
168171 libcompiler_rt: void,
172 /// needed when producing a static library with bundle-compiler-rt
173 compiler_rt_obj: void,
169174 /// needed when not linking libc and using LLVM for code generation because it generates
170175 /// calls to, for example, memcpy and memset.
171176 zig_libc: void,
......@@ -387,6 +392,7 @@ pub const InitOptions = struct {
387392 parent_compilation_link_libc: bool = false,
388393 stack_size_override: ?u64 = null,
389394 image_base_override: ?u64 = null,
395 bundle_compiler_rt: bool = false,
390396 self_exe_path: ?[]const u8 = null,
391397 version: ?std.builtin.Version = null,
392398 libc_installation: ?*const LibCInstallation = null,
......@@ -404,6 +410,10 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
404410 .Obj, .Exe => false,
405411 .Lib => (options.link_mode orelse .Static) == .Dynamic,
406412 };
413 const is_static_lib = switch (options.output_mode) {
414 .Obj, .Exe => false,
415 .Lib => (options.link_mode orelse .Static) == .Static,
416 };
407417 const is_exe_or_dyn_lib = switch (options.output_mode) {
408418 .Obj => false,
409419 .Lib => is_dyn_lib,
......@@ -821,6 +831,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
821831 .z_defs = options.linker_z_defs,
822832 .stack_size_override = options.stack_size_override,
823833 .image_base_override = options.image_base_override,
834 .bundle_compiler_rt = options.bundle_compiler_rt,
824835 .linker_script = options.linker_script,
825836 .version_script = options.version_script,
826837 .gc_sections = options.linker_gc_sections,
......@@ -967,10 +978,18 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
967978 try comp.work_queue.writeItem(.libcxxabi);
968979 }
969980
970 const needs_compiler_rt_and_c = is_exe_or_dyn_lib or
981 const needs_libc = is_exe_or_dyn_lib or
971982 (comp.getTarget().isWasm() and comp.bin_file.options.output_mode != .Obj);
972 if (needs_compiler_rt_and_c and build_options.is_stage1) {
973 try comp.work_queue.writeItem(.{ .libcompiler_rt = {} });
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) {
974993 // MinGW provides no libssp, use our own implementation.
975994 if (comp.getTarget().isMinGW()) {
976995 try comp.work_queue.writeItem(.{ .libssp = {} });
......@@ -1380,6 +1399,12 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
13801399 fatal("unable to build compiler_rt: {}", .{@errorName(err)});
13811400 };
13821401 },
1402 .compiler_rt_obj => {
1403 self.buildOutputFromZig("compiler_rt.zig", .Obj, &self.compiler_rt_obj) catch |err| {
1404 // TODO Expose this as a normal compile error rather than crashing here.
1405 fatal("unable to build compiler_rt: {}", .{@errorName(err)});
1406 };
1407 },
13831408 .libssp => {
13841409 self.buildStaticLibFromZig("ssp.zig", &self.libssp_static_lib) catch |err| {
13851410 // TODO Expose this as a normal compile error rather than crashing here.
......@@ -2551,10 +2576,16 @@ pub fn updateSubCompilation(sub_compilation: *Compilation) !void {
25512576 }
25522577}
25532578
2554fn buildStaticLibFromZig(comp: *Compilation, src_basename: []const u8, out: *?CRTFile) !void {
2579fn buildOutputFromZig(
2580 comp: *Compilation,
2581 src_basename: []const u8,
2582 output_mode: std.builtin.OutputMode,
2583 out: *?CRTFile,
2584) !void {
25552585 const tracy = trace(@src());
25562586 defer tracy.end();
25572587
2588 std.debug.assert(output_mode != .Exe);
25582589 const special_sub = "std" ++ std.fs.path.sep_str ++ "special";
25592590 const special_path = try comp.zig_lib_directory.join(comp.gpa, &[_][]const u8{special_sub});
25602591 defer comp.gpa.free(special_path);
......@@ -2571,11 +2602,11 @@ fn buildStaticLibFromZig(comp: *Compilation, src_basename: []const u8, out: *?CR
25712602 };
25722603 const root_name = mem.split(src_basename, ".").next().?;
25732604 const target = comp.getTarget();
2574 const output_mode: std.builtin.OutputMode = if (target.cpu.arch.isWasm()) .Obj else .Lib;
2605 const fixed_output_mode = if (target.cpu.arch.isWasm()) .Obj else output_mode;
25752606 const bin_basename = try std.zig.binNameAlloc(comp.gpa, .{
25762607 .root_name = root_name,
25772608 .target = target,
2578 .output_mode = output_mode,
2609 .output_mode = fixed_output_mode,
25792610 });
25802611 defer comp.gpa.free(bin_basename);
25812612
......@@ -2598,7 +2629,7 @@ fn buildStaticLibFromZig(comp: *Compilation, src_basename: []const u8, out: *?CR
25982629 .target = target,
25992630 .root_name = root_name,
26002631 .root_pkg = &root_pkg,
2601 .output_mode = output_mode,
2632 .output_mode = fixed_output_mode,
26022633 .rand = comp.rand,
26032634 .libc_installation = comp.bin_file.options.libc_installation,
26042635 .emit_bin = emit_bin,
......@@ -2639,6 +2670,10 @@ fn buildStaticLibFromZig(comp: *Compilation, src_basename: []const u8, out: *?CR
26392670 };
26402671}
26412672
2673fn buildStaticLibFromZig(comp: *Compilation, src_basename: []const u8, out: *?CRTFile) !void {
2674 return buildOutputFromZig(comp, src_basename, .Lib, out);
2675}
2676
26422677fn updateStage1Module(comp: *Compilation, main_progress_node: *std.Progress.Node) !void {
26432678 const tracy = trace(@src());
26442679 defer tracy.end();
src/link.zig+6-1
......@@ -46,6 +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,
4950 /// Set to `true` to omit debug info.
5051 strip: bool,
5152 /// If this is true then this link code is responsible for outputting an object
......@@ -518,7 +519,8 @@ pub const File = struct {
518519 var object_files = std.ArrayList([*:0]const u8).init(base.allocator);
519520 defer object_files.deinit();
520521
521 try object_files.ensureCapacity(base.options.objects.len + comp.c_object_table.items().len + 1);
522 try object_files.ensureCapacity(base.options.objects.len + comp.c_object_table.items().len +
523 1 + @boolToInt(base.options.bundle_compiler_rt));
522524 for (base.options.objects) |obj_path| {
523525 object_files.appendAssumeCapacity(try arena.dupeZ(u8, obj_path));
524526 }
......@@ -528,6 +530,9 @@ pub const File = struct {
528530 if (module_obj_path) |p| {
529531 object_files.appendAssumeCapacity(try arena.dupeZ(u8, p));
530532 }
533 if (base.options.bundle_compiler_rt) {
534 object_files.appendAssumeCapacity(try arena.dupeZ(u8, comp.compiler_rt_obj.?.full_object_path));
535 }
531536
532537 const full_out_path = try directory.join(arena, &[_][]const u8{base.options.emit.?.sub_path});
533538 const full_out_path_z = try arena.dupeZ(u8, full_out_path);
src/main.zig+6
......@@ -314,6 +314,8 @@ 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
318 \\ -fno-compiler-rt Prevent including compiler-rt symbols in output
317319 \\ -rdynamic Add all symbols to the dynamic symbol table
318320 \\ -rpath [path] Add directory to the runtime library search path
319321 \\ -feach-lib-rpath Ensure adding rpath for each used dynamic library
......@@ -490,6 +492,7 @@ fn buildOutputType(
490492 var test_evented_io = false;
491493 var stack_size_override: ?u64 = null;
492494 var image_base_override: ?u64 = null;
495 var bundle_compiler_rt = false;
493496 var use_llvm: ?bool = null;
494497 var use_lld: ?bool = null;
495498 var use_clang: ?bool = null;
......@@ -794,6 +797,8 @@ fn buildOutputType(
794797 if (i + 1 >= args.len) fatal("expected parameter after {}", .{arg});
795798 i += 1;
796799 override_lib_dir = args[i];
800 } else if (mem.eql(u8, arg, "--bundle-compiler-rt")) {
801 bundle_compiler_rt = true;
797802 } else if (mem.eql(u8, arg, "-feach-lib-rpath")) {
798803 each_lib_rpath = true;
799804 } else if (mem.eql(u8, arg, "-fno-each-lib-rpath")) {
......@@ -1705,6 +1710,7 @@ fn buildOutputType(
17051710 .link_emit_relocs = link_emit_relocs,
17061711 .stack_size_override = stack_size_override,
17071712 .image_base_override = image_base_override,
1713 .bundle_compiler_rt = bundle_compiler_rt,
17081714 .strip = strip,
17091715 .single_threaded = single_threaded,
17101716 .function_sections = function_sections,