authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-10-21 15:40:41-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-10-23 16:27:39-07:00
log5d75d8f6fc026948aa02309bd820fc15206288e0
treef51698731964244bdf5f88b61104b5eb949b41f1
parent2d8ea78249c85552225db8f50b45e2e71ba85a41

also find static libc files on the host

and don't look for glibc files on windows

3 files changed, 62 insertions(+), 35 deletions(-)

src/Compilation.zig+3-21
......@@ -1780,32 +1780,14 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
17801780 const paths = try lci.resolveCrtPaths(arena, basenames, target);
17811781
17821782 const fields = @typeInfo(@TypeOf(paths)).@"struct".fields;
1783 try comp.link_task_queue.shared.ensureUnusedCapacity(gpa, fields.len);
1783 try comp.link_task_queue.shared.ensureUnusedCapacity(gpa, fields.len + 1);
17841784 inline for (fields) |field| {
17851785 if (@field(paths, field.name)) |path| {
17861786 comp.link_task_queue.shared.appendAssumeCapacity(.{ .load_object = path });
17871787 }
17881788 }
1789
1790 const flags = target_util.libcFullLinkFlags(target);
1791 try comp.link_task_queue.shared.ensureUnusedCapacity(gpa, flags.len);
1792 for (flags) |flag| {
1793 assert(mem.startsWith(u8, flag, "-l"));
1794 const lib_name = flag["-l".len..];
1795 const suffix = switch (comp.config.link_mode) {
1796 .static => target.staticLibSuffix(),
1797 .dynamic => target.dynamicLibSuffix(),
1798 };
1799 const sep = std.fs.path.sep_str;
1800 const lib_path = try std.fmt.allocPrint(arena, "{s}" ++ sep ++ "lib{s}{s}", .{
1801 lci.crt_dir.?, lib_name, suffix,
1802 });
1803 const resolved_path = Path.initCwd(lib_path);
1804 comp.link_task_queue.shared.appendAssumeCapacity(switch (comp.config.link_mode) {
1805 .static => .{ .load_archive = resolved_path },
1806 .dynamic => .{ .load_dso = resolved_path },
1807 });
1808 }
1789 // Loads the libraries provided by `target_util.libcFullLinkFlags(target)`.
1790 comp.link_task_queue.shared.appendAssumeCapacity(.load_host_libc);
18091791 } else if (target.isMusl() and !target.isWasm()) {
18101792 if (!std.zig.target.canBuildLibC(target)) return error.LibCUnavailable;
18111793
src/link.zig+54-10
......@@ -25,6 +25,7 @@ const lldMain = @import("main.zig").lldMain;
2525const Package = @import("Package.zig");
2626const dev = @import("dev.zig");
2727const ThreadSafeQueue = @import("ThreadSafeQueue.zig").ThreadSafeQueue;
28const target_util = @import("target.zig");
2829
2930pub const LdScript = @import("link/LdScript.zig");
3031
......@@ -1364,6 +1365,10 @@ pub const File = struct {
13641365 /// Loads the objects, shared objects, and archives that are already
13651366 /// known from the command line.
13661367 load_explicitly_provided,
1368 /// Loads the shared objects and archives by resolving
1369 /// `target_util.libcFullLinkFlags()` against the host libc
1370 /// installation.
1371 load_host_libc,
13671372 /// Tells the linker to load an object file by path.
13681373 load_object: Path,
13691374 /// Tells the linker to load a static library by path.
......@@ -1393,6 +1398,45 @@ pub const File = struct {
13931398 };
13941399 }
13951400 },
1401 .load_host_libc => {
1402 const target = comp.root_mod.resolved_target.result;
1403 const flags = target_util.libcFullLinkFlags(target);
1404 const crt_dir = comp.libc_installation.?.crt_dir.?;
1405 const sep = std.fs.path.sep_str;
1406 const diags = &comp.link_diags;
1407 for (flags) |flag| {
1408 assert(mem.startsWith(u8, flag, "-l"));
1409 const lib_name = flag["-l".len..];
1410 switch (comp.config.link_mode) {
1411 .dynamic => d: {
1412 const path = Path.initCwd(
1413 std.fmt.allocPrint(comp.arena, "{s}" ++ sep ++ "{s}{s}{s}", .{
1414 crt_dir, target.libPrefix(), lib_name, target.dynamicLibSuffix(),
1415 }) catch return diags.setAllocFailure(),
1416 );
1417 base.openLoadDso(path, .{
1418 .preferred_mode = .dynamic,
1419 .search_strategy = .paths_first,
1420 }) catch |err| switch (err) {
1421 error.FileNotFound => break :d, // also try static
1422 error.LinkFailure => return, // error reported via diags
1423 else => |e| diags.addParseError(path, "failed to parse shared library: {s}", .{@errorName(e)}),
1424 };
1425 continue;
1426 },
1427 .static => {},
1428 }
1429 const path = Path.initCwd(
1430 std.fmt.allocPrint(comp.arena, "{s}" ++ sep ++ "{s}{s}{s}", .{
1431 crt_dir, target.libPrefix(), lib_name, target.staticLibSuffix(),
1432 }) catch return diags.setAllocFailure(),
1433 );
1434 base.openLoadArchive(path) catch |err| switch (err) {
1435 error.LinkFailure => return, // error reported via diags
1436 else => |e| diags.addParseError(path, "failed to parse archive: {s}", .{@errorName(e)}),
1437 };
1438 }
1439 },
13961440 .load_object => |path| {
13971441 base.openLoadObject(path) catch |err| switch (err) {
13981442 error.LinkFailure => return, // error reported via link_diags
......@@ -1873,7 +1917,7 @@ pub fn resolveInputs(
18731917 }
18741918}
18751919
1876const AccessLibPathResult = enum { ok, no_match };
1920const ResolveLibInputResult = enum { ok, no_match };
18771921const fatal = std.process.fatal;
18781922
18791923fn resolveLibInput(
......@@ -1892,7 +1936,7 @@ fn resolveLibInput(
18921936 target: std.Target,
18931937 link_mode: std.builtin.LinkMode,
18941938 color: std.zig.Color,
1895) Allocator.Error!AccessLibPathResult {
1939) Allocator.Error!ResolveLibInputResult {
18961940 try resolved_inputs.ensureUnusedCapacity(gpa, 1);
18971941
18981942 const lib_name = name_query.name;
......@@ -1909,7 +1953,7 @@ fn resolveLibInput(
19091953 else => |e| fatal("unable to search for tbd library '{}': {s}", .{ test_path, @errorName(e) }),
19101954 };
19111955 errdefer file.close();
1912 return finishAccessLibPath(resolved_inputs, test_path, file, link_mode, name_query.query);
1956 return finishResolveLibInput(resolved_inputs, test_path, file, link_mode, name_query.query);
19131957 }
19141958
19151959 {
......@@ -1947,7 +1991,7 @@ fn resolveLibInput(
19471991 }),
19481992 };
19491993 errdefer file.close();
1950 return finishAccessLibPath(resolved_inputs, test_path, file, link_mode, name_query.query);
1994 return finishResolveLibInput(resolved_inputs, test_path, file, link_mode, name_query.query);
19511995 }
19521996
19531997 // In the case of MinGW, the main check will be .lib but we also need to
......@@ -1963,19 +2007,19 @@ fn resolveLibInput(
19632007 else => |e| fatal("unable to search for static library '{}': {s}", .{ test_path, @errorName(e) }),
19642008 };
19652009 errdefer file.close();
1966 return finishAccessLibPath(resolved_inputs, test_path, file, link_mode, name_query.query);
2010 return finishResolveLibInput(resolved_inputs, test_path, file, link_mode, name_query.query);
19672011 }
19682012
19692013 return .no_match;
19702014}
19712015
1972fn finishAccessLibPath(
2016fn finishResolveLibInput(
19732017 resolved_inputs: *std.ArrayListUnmanaged(Input),
19742018 path: Path,
19752019 file: std.fs.File,
19762020 link_mode: std.builtin.LinkMode,
19772021 query: UnresolvedInput.Query,
1978) AccessLibPathResult {
2022) ResolveLibInputResult {
19792023 switch (link_mode) {
19802024 .static => resolved_inputs.appendAssumeCapacity(.{ .archive = .{
19812025 .path = path,
......@@ -2052,7 +2096,7 @@ fn resolvePathInputLib(
20522096 pq: UnresolvedInput.PathQuery,
20532097 link_mode: std.builtin.LinkMode,
20542098 color: std.zig.Color,
2055) Allocator.Error!AccessLibPathResult {
2099) Allocator.Error!ResolveLibInputResult {
20562100 try resolved_inputs.ensureUnusedCapacity(gpa, 1);
20572101
20582102 const test_path: Path = pq.path;
......@@ -2074,7 +2118,7 @@ fn resolvePathInputLib(
20742118 if (n != ld_script_bytes.items.len) break :elf_file;
20752119 if (!mem.eql(u8, ld_script_bytes.items[0..4], "\x7fELF")) break :elf_file;
20762120 // Appears to be an ELF file.
2077 return finishAccessLibPath(resolved_inputs, test_path, file, link_mode, pq.query);
2121 return finishResolveLibInput(resolved_inputs, test_path, file, link_mode, pq.query);
20782122 }
20792123 const stat = file.stat() catch |err|
20802124 fatal("failed to stat {}: {s}", .{ test_path, @errorName(err) });
......@@ -2140,7 +2184,7 @@ fn resolvePathInputLib(
21402184 }),
21412185 };
21422186 errdefer file.close();
2143 return finishAccessLibPath(resolved_inputs, test_path, file, link_mode, pq.query);
2187 return finishResolveLibInput(resolved_inputs, test_path, file, link_mode, pq.query);
21442188}
21452189
21462190pub fn openObject(path: Path, must_link: bool, hidden: bool) !Input.Object {
src/target.zig+5-4
......@@ -291,10 +291,11 @@ pub fn libcFullLinkFlags(target: std.Target) []const []const u8 {
291291 // Solaris releases after 10 merged the threading libraries into libc.
292292 .solaris, .illumos => &.{ "-lm", "-lsocket", "-lnsl", "-lc" },
293293 .haiku => &.{ "-lm", "-lroot", "-lpthread", "-lc", "-lnetwork" },
294 else => if (target.isAndroid() or target.abi.isOpenHarmony())
295 &.{ "-lm", "-lc", "-ldl" }
296 else
297 &.{ "-lm", "-lpthread", "-lc", "-ldl", "-lrt", "-lutil" },
294 .linux => switch (target.abi) {
295 .android, .androideabi, .ohos, .ohoseabi => &.{ "-lm", "-lc", "-ldl" },
296 else => &.{ "-lm", "-lpthread", "-lc", "-ldl", "-lrt", "-lutil" },
297 },
298 else => &.{},
298299 };
299300 return result;
300301}