authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-09-11 23:12:50-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-09-11 23:12:50-04:00
log349df40d14a3a66c1cbfd7ebacc4adac3ce29f46
treea0b30a16aad0418f999ea5e9d677bb3568d87fba
parent60678f5bafdf22e274229f983d02069e13996426
parent65bea514ae3860a5169d044d22ece7170c445bd3
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #12814 from ziglang/native-libc-integration

stage2: no condition on system libs to link native libc

3 files changed, 92 insertions(+), 71 deletions(-)

lib/std/child_process.zig+1-1
......@@ -508,7 +508,7 @@ pub const ChildProcess = struct {
508508 // it, that's the error code returned by the child process.
509509 _ = std.os.poll(&fd, 0) catch unreachable;
510510
511 // According to eventfd(2) the descriptro is readable if the counter
511 // According to eventfd(2) the descriptor is readable if the counter
512512 // has a value greater than 0
513513 if ((fd[0].revents & std.os.POLL.IN) != 0) {
514514 const err_int = try readIntFd(err_pipe[0]);
src/Compilation.zig+87-65
......@@ -1238,7 +1238,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
12381238 options.target,
12391239 options.is_native_abi,
12401240 link_libc,
1241 options.system_lib_names.len != 0 or options.frameworks.count() != 0,
12421241 options.libc_installation,
12431242 options.native_darwin_sdk != null,
12441243 );
......@@ -4522,7 +4521,6 @@ fn detectLibCIncludeDirs(
45224521 target: Target,
45234522 is_native_abi: bool,
45244523 link_libc: bool,
4525 link_system_libs: bool,
45264524 libc_installation: ?*const LibCInstallation,
45274525 has_macos_sdk: bool,
45284526) !LibCDirs {
......@@ -4539,7 +4537,7 @@ fn detectLibCIncludeDirs(
45394537
45404538 // If linking system libraries and targeting the native abi, default to
45414539 // using the system libc installation.
4542 if (link_system_libs and is_native_abi and !target.isMinGW()) {
4540 if (is_native_abi and !target.isMinGW()) {
45434541 if (target.isDarwin()) {
45444542 return if (has_macos_sdk)
45454543 // For Darwin/macOS, we are all set with getDarwinSDK found earlier.
......@@ -4551,74 +4549,29 @@ fn detectLibCIncludeDirs(
45514549 getZigShippedLibCIncludeDirsDarwin(arena, zig_lib_dir, target);
45524550 }
45534551 const libc = try arena.create(LibCInstallation);
4554 libc.* = try LibCInstallation.findNative(.{ .allocator = arena, .verbose = true });
4552 libc.* = LibCInstallation.findNative(.{ .allocator = arena }) catch |err| switch (err) {
4553 error.CCompilerExitCode,
4554 error.CCompilerCrashed,
4555 error.CCompilerCannotFindHeaders,
4556 error.UnableToSpawnCCompiler,
4557 => |e| {
4558 // We tried to integrate with the native system C compiler,
4559 // however, it is not installed. So we must rely on our bundled
4560 // libc files.
4561 if (target_util.canBuildLibC(target)) {
4562 return detectLibCFromBuilding(arena, zig_lib_dir, target, has_macos_sdk);
4563 }
4564 return e;
4565 },
4566 else => |e| return e,
4567 };
45554568 return detectLibCFromLibCInstallation(arena, target, libc);
45564569 }
45574570
45584571 // If not linking system libraries, build and provide our own libc by
45594572 // default if possible.
45604573 if (target_util.canBuildLibC(target)) {
4561 switch (target.os.tag) {
4562 .macos => return if (has_macos_sdk)
4563 // For Darwin/macOS, we are all set with getDarwinSDK found earlier.
4564 LibCDirs{
4565 .libc_include_dir_list = &[0][]u8{},
4566 .libc_installation = null,
4567 }
4568 else
4569 getZigShippedLibCIncludeDirsDarwin(arena, zig_lib_dir, target),
4570 else => {
4571 const generic_name = target_util.libCGenericName(target);
4572 // Some architectures are handled by the same set of headers.
4573 const arch_name = if (target.abi.isMusl())
4574 musl.archName(target.cpu.arch)
4575 else if (target.cpu.arch.isThumb())
4576 // ARM headers are valid for Thumb too.
4577 switch (target.cpu.arch) {
4578 .thumb => "arm",
4579 .thumbeb => "armeb",
4580 else => unreachable,
4581 }
4582 else
4583 @tagName(target.cpu.arch);
4584 const os_name = @tagName(target.os.tag);
4585 // Musl's headers are ABI-agnostic and so they all have the "musl" ABI name.
4586 const abi_name = if (target.abi.isMusl()) "musl" else @tagName(target.abi);
4587 const s = std.fs.path.sep_str;
4588 const arch_include_dir = try std.fmt.allocPrint(
4589 arena,
4590 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "{s}-{s}-{s}",
4591 .{ zig_lib_dir, arch_name, os_name, abi_name },
4592 );
4593 const generic_include_dir = try std.fmt.allocPrint(
4594 arena,
4595 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "generic-{s}",
4596 .{ zig_lib_dir, generic_name },
4597 );
4598 const generic_arch_name = target_util.osArchName(target);
4599 const arch_os_include_dir = try std.fmt.allocPrint(
4600 arena,
4601 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "{s}-{s}-any",
4602 .{ zig_lib_dir, generic_arch_name, os_name },
4603 );
4604 const generic_os_include_dir = try std.fmt.allocPrint(
4605 arena,
4606 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "any-{s}-any",
4607 .{ zig_lib_dir, os_name },
4608 );
4609
4610 const list = try arena.alloc([]const u8, 4);
4611 list[0] = arch_include_dir;
4612 list[1] = generic_include_dir;
4613 list[2] = arch_os_include_dir;
4614 list[3] = generic_os_include_dir;
4615
4616 return LibCDirs{
4617 .libc_include_dir_list = list,
4618 .libc_installation = null,
4619 };
4620 },
4621 }
4574 return detectLibCFromBuilding(arena, zig_lib_dir, target, has_macos_sdk);
46224575 }
46234576
46244577 // If zig can't build the libc for the target and we are targeting the
......@@ -4677,6 +4630,75 @@ fn detectLibCFromLibCInstallation(arena: Allocator, target: Target, lci: *const
46774630 };
46784631}
46794632
4633fn detectLibCFromBuilding(
4634 arena: Allocator,
4635 zig_lib_dir: []const u8,
4636 target: std.Target,
4637 has_macos_sdk: bool,
4638) !LibCDirs {
4639 switch (target.os.tag) {
4640 .macos => return if (has_macos_sdk)
4641 // For Darwin/macOS, we are all set with getDarwinSDK found earlier.
4642 LibCDirs{
4643 .libc_include_dir_list = &[0][]u8{},
4644 .libc_installation = null,
4645 }
4646 else
4647 getZigShippedLibCIncludeDirsDarwin(arena, zig_lib_dir, target),
4648 else => {
4649 const generic_name = target_util.libCGenericName(target);
4650 // Some architectures are handled by the same set of headers.
4651 const arch_name = if (target.abi.isMusl())
4652 musl.archName(target.cpu.arch)
4653 else if (target.cpu.arch.isThumb())
4654 // ARM headers are valid for Thumb too.
4655 switch (target.cpu.arch) {
4656 .thumb => "arm",
4657 .thumbeb => "armeb",
4658 else => unreachable,
4659 }
4660 else
4661 @tagName(target.cpu.arch);
4662 const os_name = @tagName(target.os.tag);
4663 // Musl's headers are ABI-agnostic and so they all have the "musl" ABI name.
4664 const abi_name = if (target.abi.isMusl()) "musl" else @tagName(target.abi);
4665 const s = std.fs.path.sep_str;
4666 const arch_include_dir = try std.fmt.allocPrint(
4667 arena,
4668 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "{s}-{s}-{s}",
4669 .{ zig_lib_dir, arch_name, os_name, abi_name },
4670 );
4671 const generic_include_dir = try std.fmt.allocPrint(
4672 arena,
4673 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "generic-{s}",
4674 .{ zig_lib_dir, generic_name },
4675 );
4676 const generic_arch_name = target_util.osArchName(target);
4677 const arch_os_include_dir = try std.fmt.allocPrint(
4678 arena,
4679 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "{s}-{s}-any",
4680 .{ zig_lib_dir, generic_arch_name, os_name },
4681 );
4682 const generic_os_include_dir = try std.fmt.allocPrint(
4683 arena,
4684 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "any-{s}-any",
4685 .{ zig_lib_dir, os_name },
4686 );
4687
4688 const list = try arena.alloc([]const u8, 4);
4689 list[0] = arch_include_dir;
4690 list[1] = generic_include_dir;
4691 list[2] = arch_os_include_dir;
4692 list[3] = generic_os_include_dir;
4693
4694 return LibCDirs{
4695 .libc_include_dir_list = list,
4696 .libc_installation = null,
4697 };
4698 },
4699 }
4700}
4701
46804702pub fn get_libc_crt_file(comp: *Compilation, arena: Allocator, basename: []const u8) ![]const u8 {
46814703 if (comp.wantBuildGLibCFromSource() or
46824704 comp.wantBuildMuslFromSource() or
src/glibc.zig+4-5
......@@ -719,17 +719,16 @@ pub fn buildSharedObjects(comp: *Compilation) !void {
719719 .lt => continue,
720720 .gt => {
721721 // TODO Expose via compile error mechanism instead of log.
722 log.err("invalid target glibc version: {}", .{target_version});
722 log.warn("invalid target glibc version: {}", .{target_version});
723723 return error.InvalidTargetGLibCVersion;
724724 },
725725 }
726 } else {
726 } else blk: {
727727 const latest_index = metadata.all_versions.len - 1;
728 // TODO Expose via compile error mechanism instead of log.
729 log.err("zig does not yet provide glibc version {}, the max provided version is {}", .{
728 log.warn("zig cannot build new glibc version {}; providing instead {}", .{
730729 target_version, metadata.all_versions[latest_index],
731730 });
732 return error.InvalidTargetGLibCVersion;
731 break :blk latest_index;
733732 };
734733
735734 {