From 4e5a88b28882eda156a46ecc7f70887a7bc0b49b Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 20 Nov 2021 17:23:44 -0700 Subject: stage2: default dynamic libraries to be linked as needed After this change, the default for dynamic libraries (`-l` or `--library`) is to only link them if they end up being actually used. With the Zig CLI, the new options `-needed-l` or `--needed-library` can be used to force link against a dynamic library. With `zig cc`, this behavior can be overridden with `-Wl,--no-as-needed` (and restored with `-Wl,--as-needed`). Closes #10164 --- src/Compilation.zig | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'src/Compilation.zig') diff --git a/src/Compilation.zig b/src/Compilation.zig index 7478d27ad4..177746ae90 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -627,6 +627,8 @@ pub const ClangPreprocessorMode = enum { stdout, }; +pub const SystemLib = link.SystemLib; + pub const InitOptions = struct { zig_lib_directory: Directory, local_cache_directory: Directory, @@ -672,7 +674,8 @@ pub const InitOptions = struct { link_objects: []const []const u8 = &[0][]const u8{}, framework_dirs: []const []const u8 = &[0][]const u8{}, frameworks: []const []const u8 = &[0][]const u8{}, - system_libs: []const []const u8 = &[0][]const u8{}, + system_lib_names: []const []const u8 = &.{}, + system_lib_infos: []const SystemLib = &.{}, /// These correspond to the WASI libc emulated subcomponents including: /// * process clocks /// * getpid @@ -935,7 +938,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { if (options.link_objects.len != 0 or options.c_source_files.len != 0 or options.frameworks.len != 0 or - options.system_libs.len != 0 or + options.system_lib_names.len != 0 or options.link_libc or options.link_libcpp or link_eh_frame_hdr or options.link_emit_relocs or @@ -1003,7 +1006,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { break :dl true; } const any_dyn_libs: bool = x: { - if (options.system_libs.len != 0) + if (options.system_lib_names.len != 0) break :x true; for (options.link_objects) |obj| { switch (classifyFileExt(obj)) { @@ -1050,7 +1053,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { options.target, options.is_native_abi, link_libc, - options.system_libs.len != 0 or options.frameworks.len != 0, + options.system_lib_names.len != 0 or options.frameworks.len != 0, options.libc_installation, ); @@ -1372,11 +1375,11 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { }; }; - var system_libs: std.StringArrayHashMapUnmanaged(void) = .{}; + var system_libs: std.StringArrayHashMapUnmanaged(SystemLib) = .{}; errdefer system_libs.deinit(gpa); - try system_libs.ensureTotalCapacity(gpa, options.system_libs.len); - for (options.system_libs) |lib_name| { - system_libs.putAssumeCapacity(lib_name, {}); + try system_libs.ensureTotalCapacity(gpa, options.system_lib_names.len); + for (options.system_lib_names) |lib_name, i| { + system_libs.putAssumeCapacity(lib_name, options.system_lib_infos[i]); } const bin_file = try link.File.openPath(gpa, .{ -- cgit v1.2.3