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 {...@@ -508,7 +508,7 @@ pub const ChildProcess = struct {
508 // it, that's the error code returned by the child process.508 // it, that's the error code returned by the child process.
509 _ = std.os.poll(&fd, 0) catch unreachable;509 _ = std.os.poll(&fd, 0) catch unreachable;
510510
511 // According to eventfd(2) the descriptro is readable if the counter511 // According to eventfd(2) the descriptor is readable if the counter
512 // has a value greater than 0512 // has a value greater than 0
513 if ((fd[0].revents & std.os.POLL.IN) != 0) {513 if ((fd[0].revents & std.os.POLL.IN) != 0) {
514 const err_int = try readIntFd(err_pipe[0]);514 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 {...@@ -1238,7 +1238,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1238 options.target,1238 options.target,
1239 options.is_native_abi,1239 options.is_native_abi,
1240 link_libc,1240 link_libc,
1241 options.system_lib_names.len != 0 or options.frameworks.count() != 0,
1242 options.libc_installation,1241 options.libc_installation,
1243 options.native_darwin_sdk != null,1242 options.native_darwin_sdk != null,
1244 );1243 );
...@@ -4522,7 +4521,6 @@ fn detectLibCIncludeDirs(...@@ -4522,7 +4521,6 @@ fn detectLibCIncludeDirs(
4522 target: Target,4521 target: Target,
4523 is_native_abi: bool,4522 is_native_abi: bool,
4524 link_libc: bool,4523 link_libc: bool,
4525 link_system_libs: bool,
4526 libc_installation: ?*const LibCInstallation,4524 libc_installation: ?*const LibCInstallation,
4527 has_macos_sdk: bool,4525 has_macos_sdk: bool,
4528) !LibCDirs {4526) !LibCDirs {
...@@ -4539,7 +4537,7 @@ fn detectLibCIncludeDirs(...@@ -4539,7 +4537,7 @@ fn detectLibCIncludeDirs(
45394537
4540 // If linking system libraries and targeting the native abi, default to4538 // If linking system libraries and targeting the native abi, default to
4541 // using the system libc installation.4539 // 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()) {
4543 if (target.isDarwin()) {4541 if (target.isDarwin()) {
4544 return if (has_macos_sdk)4542 return if (has_macos_sdk)
4545 // For Darwin/macOS, we are all set with getDarwinSDK found earlier.4543 // For Darwin/macOS, we are all set with getDarwinSDK found earlier.
...@@ -4551,74 +4549,29 @@ fn detectLibCIncludeDirs(...@@ -4551,74 +4549,29 @@ fn detectLibCIncludeDirs(
4551 getZigShippedLibCIncludeDirsDarwin(arena, zig_lib_dir, target);4549 getZigShippedLibCIncludeDirsDarwin(arena, zig_lib_dir, target);
4552 }4550 }
4553 const libc = try arena.create(LibCInstallation);4551 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 };
4555 return detectLibCFromLibCInstallation(arena, target, libc);4568 return detectLibCFromLibCInstallation(arena, target, libc);
4556 }4569 }
45574570
4558 // If not linking system libraries, build and provide our own libc by4571 // If not linking system libraries, build and provide our own libc by
4559 // default if possible.4572 // default if possible.
4560 if (target_util.canBuildLibC(target)) {4573 if (target_util.canBuildLibC(target)) {
4561 switch (target.os.tag) {4574 return detectLibCFromBuilding(arena, zig_lib_dir, target, has_macos_sdk);
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 }
4622 }4575 }
46234576
4624 // If zig can't build the libc for the target and we are targeting the4577 // 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...@@ -4677,6 +4630,75 @@ fn detectLibCFromLibCInstallation(arena: Allocator, target: Target, lci: *const
4677 };4630 };
4678}4631}
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
4680pub fn get_libc_crt_file(comp: *Compilation, arena: Allocator, basename: []const u8) ![]const u8 {4702pub fn get_libc_crt_file(comp: *Compilation, arena: Allocator, basename: []const u8) ![]const u8 {
4681 if (comp.wantBuildGLibCFromSource() or4703 if (comp.wantBuildGLibCFromSource() or
4682 comp.wantBuildMuslFromSource() or4704 comp.wantBuildMuslFromSource() or
src/glibc.zig+4-5
...@@ -719,17 +719,16 @@ pub fn buildSharedObjects(comp: *Compilation) !void {...@@ -719,17 +719,16 @@ pub fn buildSharedObjects(comp: *Compilation) !void {
719 .lt => continue,719 .lt => continue,
720 .gt => {720 .gt => {
721 // TODO Expose via compile error mechanism instead of log.721 // 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});
723 return error.InvalidTargetGLibCVersion;723 return error.InvalidTargetGLibCVersion;
724 },724 },
725 }725 }
726 } else {726 } else blk: {
727 const latest_index = metadata.all_versions.len - 1;727 const latest_index = metadata.all_versions.len - 1;
728 // TODO Expose via compile error mechanism instead of log.728 log.warn("zig cannot build new glibc version {}; providing instead {}", .{
729 log.err("zig does not yet provide glibc version {}, the max provided version is {}", .{
730 target_version, metadata.all_versions[latest_index],729 target_version, metadata.all_versions[latest_index],
731 });730 });
732 return error.InvalidTargetGLibCVersion;731 break :blk latest_index;
733 };732 };
734733
735 {734 {