diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-11-20 17:23:44 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-11-20 17:23:44 -0700 |
| commit | 4e5a88b28882eda156a46ecc7f70887a7bc0b49b (patch) | |
| tree | 1ccdc54fdb432ff72692e9f10f4f0640ef6869cd /src/Compilation.zig | |
| parent | a699d678b2c81dcf333a4f4bb84676836849a618 (diff) | |
| download | zig-4e5a88b28882eda156a46ecc7f70887a7bc0b49b.tar.gz zig-4e5a88b28882eda156a46ecc7f70887a7bc0b49b.zip | |
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
Diffstat (limited to 'src/Compilation.zig')
| -rw-r--r-- | src/Compilation.zig | 19 |
1 files changed, 11 insertions, 8 deletions
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, .{ |
