authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-11-27 17:49:55-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-11-27 17:49:55-07:00
logc3b0182f31c874eb9f1bb21397debdfdbf6161d3
tree45a3a65f31c53e6839e27aa831f8cd49d9e8b9a4
parentdd522c0c977cb2ce4d1bb1d059e1755d7c94cef5

restore sonames by default when using zig cli

Before this commit, the branch regressed this case: zig build-lib foo.zig -dynamic readelf -d libfoo.so x000000000000000e (SONAME) Library soname: [libfoo.so] Now it works again, but it preserves the property that using `zig cc` has the SONAME off by default to match C compilers.

2 files changed, 34 insertions(+), 7 deletions(-)

src/link/Elf.zig+2-4
......@@ -1313,10 +1313,8 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
13131313 man.hash.addOptionalBytes(self.base.options.dynamic_linker);
13141314 }
13151315 }
1316 if (is_dyn_lib) {
1317 man.hash.addOptionalBytes(self.base.options.soname);
1318 man.hash.addOptional(self.base.options.version);
1319 }
1316 man.hash.addOptionalBytes(self.base.options.soname);
1317 man.hash.addOptional(self.base.options.version);
13201318 man.hash.addStringSet(self.base.options.system_libs);
13211319 man.hash.add(allow_shlib_undefined);
13221320 man.hash.add(self.base.options.bind_global_refs_locally);
src/main.zig+32-3
......@@ -306,6 +306,8 @@ const usage_build_generic =
306306 \\ --version-script [path] Provide a version .map file
307307 \\ --dynamic-linker [path] Set the dynamic interpreter path (usually ld.so)
308308 \\ --version [ver] Dynamic library semver
309 \\ -fsoname[=name] (linux) Override the default SONAME value
310 \\ -fno-soname (linux) Disable emitting a SONAME
309311 \\ -rdynamic Add all symbols to the dynamic symbol table
310312 \\ -rpath [path] Add directory to the runtime library search path
311313 \\ -feach-lib-rpath Ensure adding rpath for each used dynamic library
......@@ -350,6 +352,12 @@ const repl_help =
350352 \\
351353;
352354
355const SOName = union(enum) {
356 no,
357 yes_default_value,
358 yes: []const u8,
359};
360
353361const Emit = union(enum) {
354362 no,
355363 yes_default_path,
......@@ -452,6 +460,7 @@ fn buildOutputType(
452460 var target_ofmt: ?[]const u8 = null;
453461 var output_mode: std.builtin.OutputMode = undefined;
454462 var emit_h: Emit = undefined;
463 var soname: SOName = undefined;
455464 var ensure_libc_on_non_freestanding = false;
456465 var ensure_libcpp_on_non_freestanding = false;
457466 var link_libc = false;
......@@ -467,7 +476,6 @@ fn buildOutputType(
467476 var linker_script: ?[]const u8 = null;
468477 var version_script: ?[]const u8 = null;
469478 var disable_c_depfile = false;
470 var soname: ?[]const u8 = null;
471479 var linker_gc_sections: ?bool = null;
472480 var linker_allow_shlib_undefined: ?bool = null;
473481 var linker_bind_global_refs_locally: ?bool = null;
......@@ -564,6 +572,8 @@ fn buildOutputType(
564572 // .translate_c, .zig_test, .run => emit_h = .no,
565573 // else => unreachable,
566574 //}
575
576 soname = .yes_default_value;
567577 const args = all_args[2..];
568578 var i: usize = 0;
569579 args_loop: while (i < args.len) : (i += 1) {
......@@ -828,6 +838,12 @@ fn buildOutputType(
828838 use_clang = false;
829839 } else if (mem.eql(u8, arg, "-rdynamic")) {
830840 rdynamic = true;
841 } else if (mem.eql(u8, arg, "-fsoname")) {
842 soname = .yes_default_value;
843 } else if (mem.startsWith(u8, arg, "-fsoname=")) {
844 soname = .{ .yes = arg["-fsoname=".len..] };
845 } else if (mem.eql(u8, arg, "-fno-soname")) {
846 soname = .no;
831847 } else if (mem.eql(u8, arg, "-femit-bin")) {
832848 emit_bin = .yes_default_path;
833849 } else if (mem.startsWith(u8, arg, "-femit-bin=")) {
......@@ -955,6 +971,7 @@ fn buildOutputType(
955971 },
956972 .cc, .cpp => {
957973 emit_h = .no;
974 soname = .no;
958975 strip = true;
959976 ensure_libc_on_non_freestanding = true;
960977 ensure_libcpp_on_non_freestanding = arg_mode == .cpp;
......@@ -1109,7 +1126,7 @@ fn buildOutputType(
11091126 fatal("expected linker arg after '{}'", .{arg});
11101127 }
11111128 const name = linker_args.items[i];
1112 soname = name;
1129 soname = .{ .yes = name };
11131130 // Use it as --name.
11141131 // Example: libsoundio.so.2
11151132 var prefix: usize = 0;
......@@ -1433,6 +1450,18 @@ fn buildOutputType(
14331450 const have_enable_cache = enable_cache orelse false;
14341451 const optional_version = if (have_version) version else null;
14351452
1453 const resolved_soname: ?[]const u8 = switch (soname) {
1454 .yes => |explicit| explicit,
1455 .no => null,
1456 .yes_default_value => switch (object_format) {
1457 .elf => if (have_version)
1458 try std.fmt.allocPrint(arena, "lib{s}.so.{d}", .{ root_name, version.major })
1459 else
1460 try std.fmt.allocPrint(arena, "lib{s}.so", .{root_name}),
1461 else => null,
1462 },
1463 };
1464
14361465 const emit_bin_loc: ?Compilation.EmitLoc = switch (emit_bin) {
14371466 .no => null,
14381467 .yes_default_path => Compilation.EmitLoc{
......@@ -1660,7 +1689,7 @@ fn buildOutputType(
16601689 .linker_script = linker_script,
16611690 .version_script = version_script,
16621691 .disable_c_depfile = disable_c_depfile,
1663 .soname = soname,
1692 .soname = resolved_soname,
16641693 .linker_gc_sections = linker_gc_sections,
16651694 .linker_allow_shlib_undefined = linker_allow_shlib_undefined,
16661695 .linker_bind_global_refs_locally = linker_bind_global_refs_locally,