diff --git a/src/Compilation.zig b/src/Compilation.zig index 1c8edcfde2a894b6af21ccdea552b170481e18b8..81a2bff37bdc7630458deb1d078d57c51a4823d9 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -385,6 +385,7 @@ pub const InitOptions = struct { single_threaded: bool = false, function_sections: bool = false, is_native_os: bool, + is_native_abi: bool, time_report: bool = false, stack_report: bool = false, link_eh_frame_hdr: bool = false, @@ -600,7 +601,19 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { break :dl false; }; - const default_link_mode: std.builtin.LinkMode = if (must_dynamic_link) .Dynamic else .Static; + const default_link_mode: std.builtin.LinkMode = blk: { + if (must_dynamic_link) { + break :blk .Dynamic; + } else if (is_exe_or_dyn_lib and link_libc and + options.is_native_abi and options.target.abi.isMusl()) + { + // If targeting the system's native ABI and the system's + // libc is musl, link dynamically by default. + break :blk .Dynamic; + } else { + break :blk .Static; + } + }; const link_mode: std.builtin.LinkMode = if (options.link_mode) |lm| blk: { if (lm == .Static and must_dynamic_link) { return error.UnableToStaticLink; @@ -910,6 +923,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { .rpath_list = options.rpath_list, .strip = strip, .is_native_os = options.is_native_os, + .is_native_abi = options.is_native_abi, .function_sections = options.function_sections, .allow_shlib_undefined = options.linker_allow_shlib_undefined, .bind_global_refs_locally = options.linker_bind_global_refs_locally orelse false, @@ -2778,6 +2792,7 @@ fn buildOutputFromZig( .emit_h = null, .strip = comp.bin_file.options.strip, .is_native_os = comp.bin_file.options.is_native_os, + .is_native_abi = comp.bin_file.options.is_native_abi, .self_exe_path = comp.self_exe_path, .verbose_cc = comp.verbose_cc, .verbose_link = comp.bin_file.options.verbose_link, @@ -3148,6 +3163,7 @@ pub fn build_crt_file( .emit_h = null, .strip = comp.bin_file.options.strip, .is_native_os = comp.bin_file.options.is_native_os, + .is_native_abi = comp.bin_file.options.is_native_abi, .self_exe_path = comp.self_exe_path, .c_source_files = c_source_files, .verbose_cc = comp.verbose_cc, diff --git a/src/glibc.zig b/src/glibc.zig index 6da397d4f59a82da0045d52da8414eef1f2589bf..4f092efb09d0713899df3afeadea327e5efe0f31 100644 --- a/src/glibc.zig +++ b/src/glibc.zig @@ -945,6 +945,7 @@ fn buildSharedLib( .emit_h = null, .strip = comp.bin_file.options.strip, .is_native_os = false, + .is_native_abi = false, .self_exe_path = comp.self_exe_path, .verbose_cc = comp.verbose_cc, .verbose_link = comp.bin_file.options.verbose_link, diff --git a/src/libcxx.zig b/src/libcxx.zig index 9c5ec937767ae6cbb6b706dc224c9e60ec8ac37b..39ec776659617eb946768ac64defdac5fb79bb3b 100644 --- a/src/libcxx.zig +++ b/src/libcxx.zig @@ -175,6 +175,7 @@ pub fn buildLibCXX(comp: *Compilation) !void { .emit_h = null, .strip = comp.bin_file.options.strip, .is_native_os = comp.bin_file.options.is_native_os, + .is_native_abi = comp.bin_file.options.is_native_abi, .self_exe_path = comp.self_exe_path, .c_source_files = c_source_files.items, .verbose_cc = comp.verbose_cc, @@ -293,6 +294,7 @@ pub fn buildLibCXXABI(comp: *Compilation) !void { .emit_h = null, .strip = comp.bin_file.options.strip, .is_native_os = comp.bin_file.options.is_native_os, + .is_native_abi = comp.bin_file.options.is_native_abi, .self_exe_path = comp.self_exe_path, .c_source_files = &c_source_files, .verbose_cc = comp.verbose_cc, diff --git a/src/libunwind.zig b/src/libunwind.zig index 3607488abae91b3962381424a9d4b0bffef1a703..ec8fe990e9f91daa556e98a6cf741896b0ac9261 100644 --- a/src/libunwind.zig +++ b/src/libunwind.zig @@ -108,6 +108,7 @@ pub fn buildStaticLib(comp: *Compilation) !void { .emit_h = null, .strip = comp.bin_file.options.strip, .is_native_os = comp.bin_file.options.is_native_os, + .is_native_abi = comp.bin_file.options.is_native_abi, .self_exe_path = comp.self_exe_path, .c_source_files = &c_source_files, .verbose_cc = comp.verbose_cc, diff --git a/src/link.zig b/src/link.zig index 40d5ff9a7fa2fb6b605b95e99d14fd599ce4b1a3..c0e78f445fa77ab242076ade7bbfb590d6b0faa5 100644 --- a/src/link.zig +++ b/src/link.zig @@ -71,6 +71,7 @@ pub const Options = struct { z_defs: bool, bind_global_refs_locally: bool, is_native_os: bool, + is_native_abi: bool, pic: bool, pie: bool, valgrind: bool, diff --git a/src/main.zig b/src/main.zig index 346fc11af2977155925121e3af5a75ba343da9fd..9654fc95652a6a6b17fb5cfb9c63cd0ed1cf612f 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1685,6 +1685,7 @@ fn buildOutputType( .root_name = root_name, .target = target_info.target, .is_native_os = cross_target.isNativeOs(), + .is_native_abi = cross_target.isNativeAbi(), .dynamic_linker = target_info.dynamic_linker.get(), .output_mode = output_mode, .root_pkg = root_pkg, @@ -2415,6 +2416,7 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v .root_name = "build", .target = target_info.target, .is_native_os = cross_target.isNativeOs(), + .is_native_abi = cross_target.isNativeAbi(), .dynamic_linker = target_info.dynamic_linker.get(), .output_mode = .Exe, .root_pkg = &root_pkg, diff --git a/src/musl.zig b/src/musl.zig index d69acc650ead876f79fa70ae5200e83e1a9cbc2b..ac39eb7d7453543928144d4d50f82ba7d6d971fc 100644 --- a/src/musl.zig +++ b/src/musl.zig @@ -210,6 +210,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void { .emit_h = null, .strip = comp.bin_file.options.strip, .is_native_os = false, + .is_native_abi = false, .self_exe_path = comp.self_exe_path, .verbose_cc = comp.verbose_cc, .verbose_link = comp.bin_file.options.verbose_link, diff --git a/src/test.zig b/src/test.zig index c72c5207f6145d349ab58b0978db7fd2f0bc0c52..251edc2d3439c333f22e765d5a3dd0f0ba1adfdf 100644 --- a/src/test.zig +++ b/src/test.zig @@ -561,6 +561,7 @@ pub const TestContext = struct { .keep_source_files_loaded = true, .object_format = ofmt, .is_native_os = case.target.isNativeOs(), + .is_native_abi = case.target.isNativeAbi(), }); defer comp.destroy();