Skip to content

Commit

Permalink
move is_libcpp_lib_name and is_libc_lib_name to std.Target
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Jan 1, 2024
1 parent b7b110e commit 499c0cd
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 94 deletions.
90 changes: 90 additions & 0 deletions lib/std/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2601,8 +2601,98 @@ pub const Target = struct {
},
);
}

pub fn is_libc_lib_name(target: std.Target, name: []const u8) bool {
const ignore_case = target.os.tag == .macos or target.os.tag == .windows;

if (eqlIgnoreCase(ignore_case, name, "c"))
return true;

if (target.isMinGW()) {
if (eqlIgnoreCase(ignore_case, name, "m"))
return true;
if (eqlIgnoreCase(ignore_case, name, "uuid"))
return true;
if (eqlIgnoreCase(ignore_case, name, "mingw32"))
return true;
if (eqlIgnoreCase(ignore_case, name, "msvcrt-os"))
return true;
if (eqlIgnoreCase(ignore_case, name, "mingwex"))
return true;

return false;
}

if (target.abi.isGnu() or target.abi.isMusl()) {
if (eqlIgnoreCase(ignore_case, name, "m"))
return true;
if (eqlIgnoreCase(ignore_case, name, "rt"))
return true;
if (eqlIgnoreCase(ignore_case, name, "pthread"))
return true;
if (eqlIgnoreCase(ignore_case, name, "util"))
return true;
if (eqlIgnoreCase(ignore_case, name, "xnet"))
return true;
if (eqlIgnoreCase(ignore_case, name, "resolv"))
return true;
if (eqlIgnoreCase(ignore_case, name, "dl"))
return true;
}

if (target.abi.isMusl()) {
if (eqlIgnoreCase(ignore_case, name, "crypt"))
return true;
}

if (target.os.tag.isDarwin()) {
if (eqlIgnoreCase(ignore_case, name, "System"))
return true;
if (eqlIgnoreCase(ignore_case, name, "c"))
return true;
if (eqlIgnoreCase(ignore_case, name, "dbm"))
return true;
if (eqlIgnoreCase(ignore_case, name, "dl"))
return true;
if (eqlIgnoreCase(ignore_case, name, "info"))
return true;
if (eqlIgnoreCase(ignore_case, name, "m"))
return true;
if (eqlIgnoreCase(ignore_case, name, "poll"))
return true;
if (eqlIgnoreCase(ignore_case, name, "proc"))
return true;
if (eqlIgnoreCase(ignore_case, name, "pthread"))
return true;
if (eqlIgnoreCase(ignore_case, name, "rpcsvc"))
return true;
}

if (target.os.isAtLeast(.macos, .{ .major = 10, .minor = 8, .patch = 0 }) orelse false) {
if (eqlIgnoreCase(ignore_case, name, "mx"))
return true;
}

return false;
}

pub fn is_libcpp_lib_name(target: std.Target, name: []const u8) bool {
const ignore_case = target.os.tag.isDarwin() or target.os.tag == .windows;

return eqlIgnoreCase(ignore_case, name, "c++") or
eqlIgnoreCase(ignore_case, name, "stdc++") or
eqlIgnoreCase(ignore_case, name, "c++abi");
}
};

fn eqlIgnoreCase(ignore_case: bool, a: []const u8, b: []const u8) bool {
if (ignore_case) {
return std.ascii.eqlIgnoreCase(a, b);
} else {
return std.mem.eql(u8, a, b);
}
}

test {
std.testing.refAllDecls(Target.Cpu.Arch);
}
4 changes: 2 additions & 2 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9075,7 +9075,7 @@ fn handleExternLibName(
const comp = mod.comp;
const target = mod.getTarget();
log.debug("extern fn symbol expected in lib '{s}'", .{lib_name});
if (target_util.is_libc_lib_name(target, lib_name)) {
if (target.is_libc_lib_name(lib_name)) {
if (!comp.bin_file.options.link_libc) {
return sema.fail(
block,
Expand All @@ -9086,7 +9086,7 @@ fn handleExternLibName(
}
break :blk;
}
if (target_util.is_libcpp_lib_name(target, lib_name)) {
if (target.is_libcpp_lib_name(lib_name)) {
if (!comp.bin_file.options.link_libcpp) {
return sema.fail(
block,
Expand Down
4 changes: 2 additions & 2 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2778,11 +2778,11 @@ fn buildOutputType(
}

for (system_libs.keys(), system_libs.values()) |lib_name, info| {
if (target_util.is_libc_lib_name(target_info.target, lib_name)) {
if (target_info.target.is_libc_lib_name(lib_name)) {
link_libc = true;
continue;
}
if (target_util.is_libcpp_lib_name(target_info.target, lib_name)) {
if (target_info.target.is_libcpp_lib_name(lib_name)) {
link_libcpp = true;
continue;
}
Expand Down
90 changes: 0 additions & 90 deletions src/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -356,96 +356,6 @@ pub fn supportsReturnAddress(target: std.Target) bool {
};
}

fn eqlIgnoreCase(ignore_case: bool, a: []const u8, b: []const u8) bool {
if (ignore_case) {
return std.ascii.eqlIgnoreCase(a, b);
} else {
return std.mem.eql(u8, a, b);
}
}

pub fn is_libc_lib_name(target: std.Target, name: []const u8) bool {
const ignore_case = target.os.tag == .macos or target.os.tag == .windows;

if (eqlIgnoreCase(ignore_case, name, "c"))
return true;

if (target.isMinGW()) {
if (eqlIgnoreCase(ignore_case, name, "m"))
return true;
if (eqlIgnoreCase(ignore_case, name, "uuid"))
return true;
if (eqlIgnoreCase(ignore_case, name, "mingw32"))
return true;
if (eqlIgnoreCase(ignore_case, name, "msvcrt-os"))
return true;
if (eqlIgnoreCase(ignore_case, name, "mingwex"))
return true;

return false;
}

if (target.abi.isGnu() or target.abi.isMusl()) {
if (eqlIgnoreCase(ignore_case, name, "m"))
return true;
if (eqlIgnoreCase(ignore_case, name, "rt"))
return true;
if (eqlIgnoreCase(ignore_case, name, "pthread"))
return true;
if (eqlIgnoreCase(ignore_case, name, "util"))
return true;
if (eqlIgnoreCase(ignore_case, name, "xnet"))
return true;
if (eqlIgnoreCase(ignore_case, name, "resolv"))
return true;
if (eqlIgnoreCase(ignore_case, name, "dl"))
return true;
}

if (target.abi.isMusl()) {
if (eqlIgnoreCase(ignore_case, name, "crypt"))
return true;
}

if (target.os.tag.isDarwin()) {
if (eqlIgnoreCase(ignore_case, name, "System"))
return true;
if (eqlIgnoreCase(ignore_case, name, "c"))
return true;
if (eqlIgnoreCase(ignore_case, name, "dbm"))
return true;
if (eqlIgnoreCase(ignore_case, name, "dl"))
return true;
if (eqlIgnoreCase(ignore_case, name, "info"))
return true;
if (eqlIgnoreCase(ignore_case, name, "m"))
return true;
if (eqlIgnoreCase(ignore_case, name, "poll"))
return true;
if (eqlIgnoreCase(ignore_case, name, "proc"))
return true;
if (eqlIgnoreCase(ignore_case, name, "pthread"))
return true;
if (eqlIgnoreCase(ignore_case, name, "rpcsvc"))
return true;
}

if (target.os.isAtLeast(.macos, .{ .major = 10, .minor = 8, .patch = 0 }) orelse false) {
if (eqlIgnoreCase(ignore_case, name, "mx"))
return true;
}

return false;
}

pub fn is_libcpp_lib_name(target: std.Target, name: []const u8) bool {
const ignore_case = target.os.tag.isDarwin() or target.os.tag == .windows;

return eqlIgnoreCase(ignore_case, name, "c++") or
eqlIgnoreCase(ignore_case, name, "stdc++") or
eqlIgnoreCase(ignore_case, name, "c++abi");
}

pub const CompilerRtClassification = enum { none, only_compiler_rt, only_libunwind, both };

pub fn classifyCompilerRtLibName(target: std.Target, name: []const u8) CompilerRtClassification {
Expand Down

0 comments on commit 499c0cd

Please sign in to comment.