diff options
| author | Isaac Freund <ifreund@ifreund.xyz> | 2020-12-11 15:31:23 +0100 |
|---|---|---|
| committer | Isaac Freund <ifreund@ifreund.xyz> | 2020-12-13 00:40:35 +0100 |
| commit | 307d98dc35354c6562bb0c12cc79710f6b8a4c01 (patch) | |
| tree | eeb803f62161d1b94a547cca6df7c4ed9c19344e /src | |
| parent | d569e37cb538d17d009f5632c25acd643d3736cb (diff) | |
| download | zig-307d98dc35354c6562bb0c12cc79710f6b8a4c01.tar.gz zig-307d98dc35354c6562bb0c12cc79710f6b8a4c01.zip | |
stage2: support dynamically linking musl libc
Diffstat (limited to 'src')
| -rw-r--r-- | src/Compilation.zig | 5 | ||||
| -rw-r--r-- | src/link/Elf.zig | 5 | ||||
| -rw-r--r-- | src/musl.zig | 53 |
3 files changed, 61 insertions, 2 deletions
diff --git a/src/Compilation.zig b/src/Compilation.zig index 96bf7b5797..1c8edcfde2 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -1025,7 +1025,10 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { .{ .musl_crt_file = .crt1_o }, .{ .musl_crt_file = .scrt1_o }, .{ .musl_crt_file = .rcrt1_o }, - .{ .musl_crt_file = .libc_a }, + switch (comp.bin_file.options.link_mode) { + .Static => .{ .musl_crt_file = .libc_a }, + .Dynamic => .{ .musl_crt_file = .libc_so }, + }, }); } if (comp.wantBuildMinGWFromSource()) { diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 852b51ece1..03202c90be 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -1598,7 +1598,10 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void { try argv.append(try comp.get_libc_crt_file(arena, "libc_nonshared.a")); } else if (target.isMusl()) { try argv.append(comp.libunwind_static_lib.?.full_object_path); - try argv.append(try comp.get_libc_crt_file(arena, "libc.a")); + try argv.append(try comp.get_libc_crt_file(arena, switch (self.base.options.link_mode) { + .Static => "libc.a", + .Dynamic => "libc.so", + })); } else if (self.base.options.link_libcpp) { try argv.append(comp.libunwind_static_lib.?.full_object_path); } else { diff --git a/src/musl.zig b/src/musl.zig index 9ac4d42f8d..d69acc650e 100644 --- a/src/musl.zig +++ b/src/musl.zig @@ -15,6 +15,7 @@ pub const CRTFile = enum { rcrt1_o, scrt1_o, libc_a, + libc_so, }; pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void { @@ -189,6 +190,58 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void { } return comp.build_crt_file("c", .Lib, c_source_files.items); }, + .libc_so => { + const sub_compilation = try Compilation.create(comp.gpa, .{ + .local_cache_directory = comp.global_cache_directory, + .global_cache_directory = comp.global_cache_directory, + .zig_lib_directory = comp.zig_lib_directory, + .target = comp.getTarget(), + .root_name = "c", + .root_pkg = null, + .output_mode = .Lib, + .link_mode = .Dynamic, + .rand = comp.rand, + .libc_installation = comp.bin_file.options.libc_installation, + .emit_bin = Compilation.EmitLoc{ .directory = null, .basename = "libc.so" }, + .optimize_mode = comp.bin_file.options.optimize_mode, + .want_sanitize_c = false, + .want_stack_check = false, + .want_valgrind = false, + .emit_h = null, + .strip = comp.bin_file.options.strip, + .is_native_os = false, + .self_exe_path = comp.self_exe_path, + .verbose_cc = comp.verbose_cc, + .verbose_link = comp.bin_file.options.verbose_link, + .verbose_tokenize = comp.verbose_tokenize, + .verbose_ast = comp.verbose_ast, + .verbose_ir = comp.verbose_ir, + .verbose_llvm_ir = comp.verbose_llvm_ir, + .verbose_cimport = comp.verbose_cimport, + .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features, + .clang_passthrough_mode = comp.clang_passthrough_mode, + .c_source_files = &[_]Compilation.CSourceFile{ + .{ .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libc", "musl", "libc.s" }) }, + }, + .is_compiler_rt_or_libc = true, + .soname = "libc.so", + }); + defer sub_compilation.destroy(); + + try sub_compilation.updateSubCompilation(); + + try comp.crt_files.ensureCapacity(comp.gpa, comp.crt_files.count() + 1); + + const basename = try comp.gpa.dupe(u8, "libc.so"); + errdefer comp.gpa.free(basename); + + comp.crt_files.putAssumeCapacityNoClobber(basename, .{ + .full_object_path = try sub_compilation.bin_file.options.emit.?.directory.join(comp.gpa, &[_][]const u8{ + sub_compilation.bin_file.options.emit.?.sub_path, + }), + .lock = sub_compilation.bin_file.toOwnedLock(), + }); + }, } } |
