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,...@@ -93,6 +93,9 @@ libc_static_lib: ?CRTFile = null,
93/// Populated when we build the libcompiler_rt static library. A Job to build this is placed in the queue93/// Populated when we build the libcompiler_rt static library. A Job to build this is placed in the queue
94/// and resolved before calling linker.flush().94/// and resolved before calling linker.flush().
95compiler_rt_static_lib: ?CRTFile = null,95compiler_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
97glibc_so_files: ?glibc.BuiltSharedObjects = null,100glibc_so_files: ?glibc.BuiltSharedObjects = null,
98101
...@@ -166,6 +169,8 @@ const Job = union(enum) {...@@ -166,6 +169,8 @@ const Job = union(enum) {
166 libssp: void,169 libssp: void,
167 /// needed when producing a dynamic library or executable170 /// needed when producing a dynamic library or executable
168 libcompiler_rt: void,171 libcompiler_rt: void,
172 /// needed when producing a static library with bundle-compiler-rt
173 compiler_rt_obj: void,
169 /// needed when not linking libc and using LLVM for code generation because it generates174 /// needed when not linking libc and using LLVM for code generation because it generates
170 /// calls to, for example, memcpy and memset.175 /// calls to, for example, memcpy and memset.
171 zig_libc: void,176 zig_libc: void,
...@@ -387,6 +392,7 @@ pub const InitOptions = struct {...@@ -387,6 +392,7 @@ pub const InitOptions = struct {
387 parent_compilation_link_libc: bool = false,392 parent_compilation_link_libc: bool = false,
388 stack_size_override: ?u64 = null,393 stack_size_override: ?u64 = null,
389 image_base_override: ?u64 = null,394 image_base_override: ?u64 = null,
395 bundle_compiler_rt: bool = false,
390 self_exe_path: ?[]const u8 = null,396 self_exe_path: ?[]const u8 = null,
391 version: ?std.builtin.Version = null,397 version: ?std.builtin.Version = null,
392 libc_installation: ?*const LibCInstallation = null,398 libc_installation: ?*const LibCInstallation = null,
...@@ -404,6 +410,10 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {...@@ -404,6 +410,10 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
404 .Obj, .Exe => false,410 .Obj, .Exe => false,
405 .Lib => (options.link_mode orelse .Static) == .Dynamic,411 .Lib => (options.link_mode orelse .Static) == .Dynamic,
406 };412 };
413 const is_static_lib = switch (options.output_mode) {
414 .Obj, .Exe => false,
415 .Lib => (options.link_mode orelse .Static) == .Static,
416 };
407 const is_exe_or_dyn_lib = switch (options.output_mode) {417 const is_exe_or_dyn_lib = switch (options.output_mode) {
408 .Obj => false,418 .Obj => false,
409 .Lib => is_dyn_lib,419 .Lib => is_dyn_lib,
...@@ -821,6 +831,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {...@@ -821,6 +831,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
821 .z_defs = options.linker_z_defs,831 .z_defs = options.linker_z_defs,
822 .stack_size_override = options.stack_size_override,832 .stack_size_override = options.stack_size_override,
823 .image_base_override = options.image_base_override,833 .image_base_override = options.image_base_override,
834 .bundle_compiler_rt = options.bundle_compiler_rt,
824 .linker_script = options.linker_script,835 .linker_script = options.linker_script,
825 .version_script = options.version_script,836 .version_script = options.version_script,
826 .gc_sections = options.linker_gc_sections,837 .gc_sections = options.linker_gc_sections,
...@@ -967,10 +978,18 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {...@@ -967,10 +978,18 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
967 try comp.work_queue.writeItem(.libcxxabi);978 try comp.work_queue.writeItem(.libcxxabi);
968 }979 }
969980
970 const needs_compiler_rt_and_c = is_exe_or_dyn_lib or981 const needs_libc = is_exe_or_dyn_lib or
971 (comp.getTarget().isWasm() and comp.bin_file.options.output_mode != .Obj);982 (comp.getTarget().isWasm() and comp.bin_file.options.output_mode != .Obj);
972 if (needs_compiler_rt_and_c and build_options.is_stage1) {983 const needs_compiler_rt = options.bundle_compiler_rt or needs_libc;
973 try comp.work_queue.writeItem(.{ .libcompiler_rt = {} });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) {
974 // MinGW provides no libssp, use our own implementation.993 // MinGW provides no libssp, use our own implementation.
975 if (comp.getTarget().isMinGW()) {994 if (comp.getTarget().isMinGW()) {
976 try comp.work_queue.writeItem(.{ .libssp = {} });995 try comp.work_queue.writeItem(.{ .libssp = {} });
...@@ -1380,6 +1399,12 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor...@@ -1380,6 +1399,12 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
1380 fatal("unable to build compiler_rt: {}", .{@errorName(err)});1399 fatal("unable to build compiler_rt: {}", .{@errorName(err)});
1381 };1400 };
1382 },1401 },
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 },
1383 .libssp => {1408 .libssp => {
1384 self.buildStaticLibFromZig("ssp.zig", &self.libssp_static_lib) catch |err| {1409 self.buildStaticLibFromZig("ssp.zig", &self.libssp_static_lib) catch |err| {
1385 // TODO Expose this as a normal compile error rather than crashing here.1410 // TODO Expose this as a normal compile error rather than crashing here.
...@@ -2551,10 +2576,16 @@ pub fn updateSubCompilation(sub_compilation: *Compilation) !void {...@@ -2551,10 +2576,16 @@ pub fn updateSubCompilation(sub_compilation: *Compilation) !void {
2551 }2576 }
2552}2577}
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 {
2555 const tracy = trace(@src());2585 const tracy = trace(@src());
2556 defer tracy.end();2586 defer tracy.end();
25572587
2588 std.debug.assert(output_mode != .Exe);
2558 const special_sub = "std" ++ std.fs.path.sep_str ++ "special";2589 const special_sub = "std" ++ std.fs.path.sep_str ++ "special";
2559 const special_path = try comp.zig_lib_directory.join(comp.gpa, &[_][]const u8{special_sub});2590 const special_path = try comp.zig_lib_directory.join(comp.gpa, &[_][]const u8{special_sub});
2560 defer comp.gpa.free(special_path);2591 defer comp.gpa.free(special_path);
...@@ -2571,11 +2602,11 @@ fn buildStaticLibFromZig(comp: *Compilation, src_basename: []const u8, out: *?CR...@@ -2571,11 +2602,11 @@ fn buildStaticLibFromZig(comp: *Compilation, src_basename: []const u8, out: *?CR
2571 };2602 };
2572 const root_name = mem.split(src_basename, ".").next().?;2603 const root_name = mem.split(src_basename, ".").next().?;
2573 const target = comp.getTarget();2604 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;
2575 const bin_basename = try std.zig.binNameAlloc(comp.gpa, .{2606 const bin_basename = try std.zig.binNameAlloc(comp.gpa, .{
2576 .root_name = root_name,2607 .root_name = root_name,
2577 .target = target,2608 .target = target,
2578 .output_mode = output_mode,2609 .output_mode = fixed_output_mode,
2579 });2610 });
2580 defer comp.gpa.free(bin_basename);2611 defer comp.gpa.free(bin_basename);
25812612
...@@ -2598,7 +2629,7 @@ fn buildStaticLibFromZig(comp: *Compilation, src_basename: []const u8, out: *?CR...@@ -2598,7 +2629,7 @@ fn buildStaticLibFromZig(comp: *Compilation, src_basename: []const u8, out: *?CR
2598 .target = target,2629 .target = target,
2599 .root_name = root_name,2630 .root_name = root_name,
2600 .root_pkg = &root_pkg,2631 .root_pkg = &root_pkg,
2601 .output_mode = output_mode,2632 .output_mode = fixed_output_mode,
2602 .rand = comp.rand,2633 .rand = comp.rand,
2603 .libc_installation = comp.bin_file.options.libc_installation,2634 .libc_installation = comp.bin_file.options.libc_installation,
2604 .emit_bin = emit_bin,2635 .emit_bin = emit_bin,
...@@ -2639,6 +2670,10 @@ fn buildStaticLibFromZig(comp: *Compilation, src_basename: []const u8, out: *?CR...@@ -2639,6 +2670,10 @@ fn buildStaticLibFromZig(comp: *Compilation, src_basename: []const u8, out: *?CR
2639 };2670 };
2640}2671}
26412672
2673fn buildStaticLibFromZig(comp: *Compilation, src_basename: []const u8, out: *?CRTFile) !void {
2674 return buildOutputFromZig(comp, src_basename, .Lib, out);
2675}
2676
2642fn updateStage1Module(comp: *Compilation, main_progress_node: *std.Progress.Node) !void {2677fn updateStage1Module(comp: *Compilation, main_progress_node: *std.Progress.Node) !void {
2643 const tracy = trace(@src());2678 const tracy = trace(@src());
2644 defer tracy.end();2679 defer tracy.end();
src/link.zig+6-1
...@@ -46,6 +46,7 @@ pub const Options = struct {...@@ -46,6 +46,7 @@ pub const Options = struct {
46 entry_addr: ?u64 = null,46 entry_addr: ?u64 = null,
47 stack_size_override: ?u64,47 stack_size_override: ?u64,
48 image_base_override: ?u64,48 image_base_override: ?u64,
49 bundle_compiler_rt: bool,
49 /// Set to `true` to omit debug info.50 /// Set to `true` to omit debug info.
50 strip: bool,51 strip: bool,
51 /// If this is true then this link code is responsible for outputting an object52 /// If this is true then this link code is responsible for outputting an object
...@@ -518,7 +519,8 @@ pub const File = struct {...@@ -518,7 +519,8 @@ pub const File = struct {
518 var object_files = std.ArrayList([*:0]const u8).init(base.allocator);519 var object_files = std.ArrayList([*:0]const u8).init(base.allocator);
519 defer object_files.deinit();520 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));
522 for (base.options.objects) |obj_path| {524 for (base.options.objects) |obj_path| {
523 object_files.appendAssumeCapacity(try arena.dupeZ(u8, obj_path));525 object_files.appendAssumeCapacity(try arena.dupeZ(u8, obj_path));
524 }526 }
...@@ -528,6 +530,9 @@ pub const File = struct {...@@ -528,6 +530,9 @@ pub const File = struct {
528 if (module_obj_path) |p| {530 if (module_obj_path) |p| {
529 object_files.appendAssumeCapacity(try arena.dupeZ(u8, p));531 object_files.appendAssumeCapacity(try arena.dupeZ(u8, p));
530 }532 }
533 if (base.options.bundle_compiler_rt) {
534 object_files.appendAssumeCapacity(try arena.dupeZ(u8, comp.compiler_rt_obj.?.full_object_path));
535 }
531536
532 const full_out_path = try directory.join(arena, &[_][]const u8{base.options.emit.?.sub_path});537 const full_out_path = try directory.join(arena, &[_][]const u8{base.options.emit.?.sub_path});
533 const full_out_path_z = try arena.dupeZ(u8, full_out_path);538 const full_out_path_z = try arena.dupeZ(u8, full_out_path);
src/main.zig+6
...@@ -314,6 +314,8 @@ const usage_build_generic =...@@ -314,6 +314,8 @@ const usage_build_generic =
314 \\ -fno-soname (Linux) Disable emitting a SONAME314 \\ -fno-soname (Linux) Disable emitting a SONAME
315 \\ -fLLD Force using LLD as the linker315 \\ -fLLD Force using LLD as the linker
316 \\ -fno-LLD Prevent using LLD as the linker316 \\ -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
317 \\ -rdynamic Add all symbols to the dynamic symbol table319 \\ -rdynamic Add all symbols to the dynamic symbol table
318 \\ -rpath [path] Add directory to the runtime library search path320 \\ -rpath [path] Add directory to the runtime library search path
319 \\ -feach-lib-rpath Ensure adding rpath for each used dynamic library321 \\ -feach-lib-rpath Ensure adding rpath for each used dynamic library
...@@ -490,6 +492,7 @@ fn buildOutputType(...@@ -490,6 +492,7 @@ fn buildOutputType(
490 var test_evented_io = false;492 var test_evented_io = false;
491 var stack_size_override: ?u64 = null;493 var stack_size_override: ?u64 = null;
492 var image_base_override: ?u64 = null;494 var image_base_override: ?u64 = null;
495 var bundle_compiler_rt = false;
493 var use_llvm: ?bool = null;496 var use_llvm: ?bool = null;
494 var use_lld: ?bool = null;497 var use_lld: ?bool = null;
495 var use_clang: ?bool = null;498 var use_clang: ?bool = null;
...@@ -794,6 +797,8 @@ fn buildOutputType(...@@ -794,6 +797,8 @@ fn buildOutputType(
794 if (i + 1 >= args.len) fatal("expected parameter after {}", .{arg});797 if (i + 1 >= args.len) fatal("expected parameter after {}", .{arg});
795 i += 1;798 i += 1;
796 override_lib_dir = args[i];799 override_lib_dir = args[i];
800 } else if (mem.eql(u8, arg, "--bundle-compiler-rt")) {
801 bundle_compiler_rt = true;
797 } else if (mem.eql(u8, arg, "-feach-lib-rpath")) {802 } else if (mem.eql(u8, arg, "-feach-lib-rpath")) {
798 each_lib_rpath = true;803 each_lib_rpath = true;
799 } else if (mem.eql(u8, arg, "-fno-each-lib-rpath")) {804 } else if (mem.eql(u8, arg, "-fno-each-lib-rpath")) {
...@@ -1705,6 +1710,7 @@ fn buildOutputType(...@@ -1705,6 +1710,7 @@ fn buildOutputType(
1705 .link_emit_relocs = link_emit_relocs,1710 .link_emit_relocs = link_emit_relocs,
1706 .stack_size_override = stack_size_override,1711 .stack_size_override = stack_size_override,
1707 .image_base_override = image_base_override,1712 .image_base_override = image_base_override,
1713 .bundle_compiler_rt = bundle_compiler_rt,
1708 .strip = strip,1714 .strip = strip,
1709 .single_threaded = single_threaded,1715 .single_threaded = single_threaded,
1710 .function_sections = function_sections,1716 .function_sections = function_sections,