Commit 55ac973953

Andrew Kelley <andrew@ziglang.org>
2020-10-05 08:16:46
fix each-lib-rpath functionality
It was regressed in 2 ways from the merge of #6250: * it was not being enabled by default when the target OS is native. * we were testing the libfoo.so file path existence with bogus format string ('{}' instead of '{s}') and so it ended up being something like "libstd.HashMap(K,V,...).Entry.so" instead of "libfoo.so". Using {} rather than {s} is a footgun, be careful! Previous functionality is now restored. closes #6523
1 parent d5d48c6
Changed files (3)
src/link/Elf.zig
@@ -1453,10 +1453,11 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
         var test_path = std.ArrayList(u8).init(self.base.allocator);
         defer test_path.deinit();
         for (self.base.options.lib_dirs) |lib_dir_path| {
-            for (self.base.options.system_libs.items()) |link_lib| {
+            for (self.base.options.system_libs.items()) |entry| {
+                const link_lib = entry.key;
                 test_path.shrinkRetainingCapacity(0);
                 const sep = fs.path.sep_str;
-                try test_path.writer().print("{}" ++ sep ++ "lib{}.so", .{ lib_dir_path, link_lib });
+                try test_path.writer().print("{s}" ++ sep ++ "lib{s}.so", .{ lib_dir_path, link_lib });
                 fs.cwd().access(test_path.items, .{}) catch |err| switch (err) {
                     error.FileNotFound => continue,
                     else => |e| return e,
src/Compilation.zig
@@ -800,7 +800,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
             .llvm_cpu_features = llvm_cpu_features,
             .is_compiler_rt_or_libc = options.is_compiler_rt_or_libc,
             .parent_compilation_link_libc = options.parent_compilation_link_libc,
-            .each_lib_rpath = options.each_lib_rpath orelse false,
+            .each_lib_rpath = options.each_lib_rpath orelse options.is_native_os,
             .disable_lld_caching = options.disable_lld_caching,
             .subsystem = options.subsystem,
             .is_test = options.is_test,
src/main.zig
@@ -268,10 +268,11 @@ const usage_build_generic =
     \\  -T[script], --script [script]  Use a custom linker script
     \\  --version-script [path]        Provide a version .map file
     \\  --dynamic-linker [path]        Set the dynamic interpreter path (usually ld.so)
-    \\  --each-lib-rpath               Add rpath for each used dynamic library
     \\  --version [ver]                Dynamic library semver
     \\  -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
+    \\  -fno-each-lib-rpath            Prevent adding rpath for each used dynamic library
     \\  --eh-frame-hdr                 Enable C++ exception handling by passing --eh-frame-hdr to linker
     \\  --emit-relocs                  Enable output of relocation sections for post build tools
     \\  -dynamic                       Force output to be dynamically linked
@@ -442,7 +443,7 @@ fn buildOutputType(
     var use_clang: ?bool = null;
     var link_eh_frame_hdr = false;
     var link_emit_relocs = false;
-    var each_lib_rpath = false;
+    var each_lib_rpath: ?bool = null;
     var libc_paths_file: ?[]const u8 = null;
     var machine_code_model: std.builtin.CodeModel = .default;
     var runtime_args_start: ?usize = null;
@@ -739,8 +740,10 @@ 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, "--each-lib-rpath")) {
+                    } else if (mem.eql(u8, arg, "-feach-lib-rpath")) {
                         each_lib_rpath = true;
+                    } else if (mem.eql(u8, arg, "-fno-each-lib-rpath")) {
+                        each_lib_rpath = false;
                     } else if (mem.eql(u8, arg, "--enable-cache")) {
                         enable_cache = true;
                     } else if (mem.eql(u8, arg, "--test-cmd-bin")) {