Added bundle-compiler-rt flag

This commit is contained in:
Alexandros Naskos 2020-11-07 11:03:13 +02:00 committed by Andrew Kelley
parent 51d7c14ce1
commit 2fae28b6af
3 changed files with 54 additions and 8 deletions

View File

@ -93,6 +93,9 @@ libc_static_lib: ?CRTFile = null,
/// Populated when we build the libcompiler_rt static library. A Job to build this is placed in the queue
/// and resolved before calling linker.flush().
compiler_rt_static_lib: ?CRTFile = null,
/// Populated when we build the compiler_rt_obj object. A Job to build this is placed in the queue
/// and resolved before calling linker.flush().
compiler_rt_obj: ?CRTFile = null,
glibc_so_files: ?glibc.BuiltSharedObjects = null,
@ -166,6 +169,8 @@ const Job = union(enum) {
libssp: void,
/// needed when producing a dynamic library or executable
libcompiler_rt: void,
/// needed when producing a static library with bundle-compiler-rt
compiler_rt_obj: void,
/// needed when not linking libc and using LLVM for code generation because it generates
/// calls to, for example, memcpy and memset.
zig_libc: void,
@ -387,6 +392,7 @@ pub const InitOptions = struct {
parent_compilation_link_libc: bool = false,
stack_size_override: ?u64 = null,
image_base_override: ?u64 = null,
bundle_compiler_rt: bool = false,
self_exe_path: ?[]const u8 = null,
version: ?std.builtin.Version = null,
libc_installation: ?*const LibCInstallation = null,
@ -404,6 +410,10 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
.Obj, .Exe => false,
.Lib => (options.link_mode orelse .Static) == .Dynamic,
};
const is_static_lib = switch (options.output_mode) {
.Obj, .Exe => false,
.Lib => (options.link_mode orelse .Static) == .Static,
};
const is_exe_or_dyn_lib = switch (options.output_mode) {
.Obj => false,
.Lib => is_dyn_lib,
@ -821,6 +831,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
.z_defs = options.linker_z_defs,
.stack_size_override = options.stack_size_override,
.image_base_override = options.image_base_override,
.bundle_compiler_rt = options.bundle_compiler_rt,
.linker_script = options.linker_script,
.version_script = options.version_script,
.gc_sections = options.linker_gc_sections,
@ -967,10 +978,18 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
try comp.work_queue.writeItem(.libcxxabi);
}
const needs_compiler_rt_and_c = is_exe_or_dyn_lib or
const needs_libc = is_exe_or_dyn_lib or
(comp.getTarget().isWasm() and comp.bin_file.options.output_mode != .Obj);
if (needs_compiler_rt_and_c and build_options.is_stage1) {
try comp.work_queue.writeItem(.{ .libcompiler_rt = {} });
const needs_compiler_rt = options.bundle_compiler_rt or needs_libc;
if (needs_compiler_rt and build_options.is_stage1) {
if (is_static_lib) {
try comp.work_queue.writeItem(.{ .compiler_rt_obj = {} });
} else {
try comp.work_queue.writeItem(.{ .libcompiler_rt = {} });
}
}
if (needs_libc and build_options.is_stage1) {
// MinGW provides no libssp, use our own implementation.
if (comp.getTarget().isMinGW()) {
try comp.work_queue.writeItem(.{ .libssp = {} });
@ -1380,6 +1399,12 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
fatal("unable to build compiler_rt: {}", .{@errorName(err)});
};
},
.compiler_rt_obj => {
self.buildOutputFromZig("compiler_rt.zig", .Obj, &self.compiler_rt_obj) catch |err| {
// TODO Expose this as a normal compile error rather than crashing here.
fatal("unable to build compiler_rt: {}", .{@errorName(err)});
};
},
.libssp => {
self.buildStaticLibFromZig("ssp.zig", &self.libssp_static_lib) catch |err| {
// TODO Expose this as a normal compile error rather than crashing here.
@ -2551,10 +2576,16 @@ pub fn updateSubCompilation(sub_compilation: *Compilation) !void {
}
}
fn buildStaticLibFromZig(comp: *Compilation, src_basename: []const u8, out: *?CRTFile) !void {
fn buildOutputFromZig(
comp: *Compilation,
src_basename: []const u8,
output_mode: std.builtin.OutputMode,
out: *?CRTFile,
) !void {
const tracy = trace(@src());
defer tracy.end();
std.debug.assert(output_mode != .Exe);
const special_sub = "std" ++ std.fs.path.sep_str ++ "special";
const special_path = try comp.zig_lib_directory.join(comp.gpa, &[_][]const u8{special_sub});
defer comp.gpa.free(special_path);
@ -2571,11 +2602,11 @@ fn buildStaticLibFromZig(comp: *Compilation, src_basename: []const u8, out: *?CR
};
const root_name = mem.split(src_basename, ".").next().?;
const target = comp.getTarget();
const output_mode: std.builtin.OutputMode = if (target.cpu.arch.isWasm()) .Obj else .Lib;
const fixed_output_mode = if (target.cpu.arch.isWasm()) .Obj else output_mode;
const bin_basename = try std.zig.binNameAlloc(comp.gpa, .{
.root_name = root_name,
.target = target,
.output_mode = output_mode,
.output_mode = fixed_output_mode,
});
defer comp.gpa.free(bin_basename);
@ -2598,7 +2629,7 @@ fn buildStaticLibFromZig(comp: *Compilation, src_basename: []const u8, out: *?CR
.target = target,
.root_name = root_name,
.root_pkg = &root_pkg,
.output_mode = output_mode,
.output_mode = fixed_output_mode,
.rand = comp.rand,
.libc_installation = comp.bin_file.options.libc_installation,
.emit_bin = emit_bin,
@ -2639,6 +2670,10 @@ fn buildStaticLibFromZig(comp: *Compilation, src_basename: []const u8, out: *?CR
};
}
fn buildStaticLibFromZig(comp: *Compilation, src_basename: []const u8, out: *?CRTFile) !void {
return buildOutputFromZig(comp, src_basename, .Lib, out);
}
fn updateStage1Module(comp: *Compilation, main_progress_node: *std.Progress.Node) !void {
const tracy = trace(@src());
defer tracy.end();

View File

@ -46,6 +46,7 @@ pub const Options = struct {
entry_addr: ?u64 = null,
stack_size_override: ?u64,
image_base_override: ?u64,
bundle_compiler_rt: bool,
/// Set to `true` to omit debug info.
strip: bool,
/// If this is true then this link code is responsible for outputting an object
@ -518,7 +519,8 @@ pub const File = struct {
var object_files = std.ArrayList([*:0]const u8).init(base.allocator);
defer object_files.deinit();
try object_files.ensureCapacity(base.options.objects.len + comp.c_object_table.items().len + 1);
try object_files.ensureCapacity(base.options.objects.len + comp.c_object_table.items().len +
1 + @boolToInt(base.options.bundle_compiler_rt));
for (base.options.objects) |obj_path| {
object_files.appendAssumeCapacity(try arena.dupeZ(u8, obj_path));
}
@ -528,6 +530,9 @@ pub const File = struct {
if (module_obj_path) |p| {
object_files.appendAssumeCapacity(try arena.dupeZ(u8, p));
}
if (base.options.bundle_compiler_rt) {
object_files.appendAssumeCapacity(try arena.dupeZ(u8, comp.compiler_rt_obj.?.full_object_path));
}
const full_out_path = try directory.join(arena, &[_][]const u8{base.options.emit.?.sub_path});
const full_out_path_z = try arena.dupeZ(u8, full_out_path);

View File

@ -314,6 +314,8 @@ const usage_build_generic =
\\ -fno-soname (Linux) Disable emitting a SONAME
\\ -fLLD Force using LLD as the linker
\\ -fno-LLD Prevent using LLD as the linker
\\ -fcompiler-rt Always including compiler-rt symbols in output
\\ -fno-compiler-rt Prevent including compiler-rt symbols in output
\\ -rdynamic Add all symbols to the dynamic symbol table
\\ -rpath [path] Add directory to the runtime library search path
\\ -feach-lib-rpath Ensure adding rpath for each used dynamic library
@ -490,6 +492,7 @@ fn buildOutputType(
var test_evented_io = false;
var stack_size_override: ?u64 = null;
var image_base_override: ?u64 = null;
var bundle_compiler_rt = false;
var use_llvm: ?bool = null;
var use_lld: ?bool = null;
var use_clang: ?bool = null;
@ -794,6 +797,8 @@ fn buildOutputType(
if (i + 1 >= args.len) fatal("expected parameter after {}", .{arg});
i += 1;
override_lib_dir = args[i];
} else if (mem.eql(u8, arg, "--bundle-compiler-rt")) {
bundle_compiler_rt = true;
} else if (mem.eql(u8, arg, "-feach-lib-rpath")) {
each_lib_rpath = true;
} else if (mem.eql(u8, arg, "-fno-each-lib-rpath")) {
@ -1705,6 +1710,7 @@ fn buildOutputType(
.link_emit_relocs = link_emit_relocs,
.stack_size_override = stack_size_override,
.image_base_override = image_base_override,
.bundle_compiler_rt = bundle_compiler_rt,
.strip = strip,
.single_threaded = single_threaded,
.function_sections = function_sections,