Skip to content
/ zig Public
forked from ziglang/zig

Commit

Permalink
std.Target: Remove functions that just wrap component functions.
Browse files Browse the repository at this point in the history
Functions like isMinGW() and isGnuLibC() have a good reason to exist: They look
at multiple components of the target. But functions like isWasm(), isDarwin(),
isGnu(), etc only exist to save 4-8 characters. I don't think this is a good
enough reason to keep them, especially given that:

* It's not immediately obvious to a reader whether target.isDarwin() means the
  same thing as target.os.tag.isDarwin() precisely because isMinGW() and similar
  functions *do* look at multiple components.
* It's not clear where we would draw the line. The logical conclusion before
  this commit would be to also wrap Arch.isX86(), Os.Tag.isSolarish(),
  Abi.isOpenHarmony(), etc... this quickly gets out of hand.
* It's nice to just have a single correct way of doing something.
  • Loading branch information
alexrp committed Nov 4, 2024
1 parent 06010a2 commit 94d5672
Show file tree
Hide file tree
Showing 74 changed files with 203 additions and 235 deletions.
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ fn addCmakeCfgOptionsToExe(
exe: *std.Build.Step.Compile,
use_zig_libcxx: bool,
) !void {
if (exe.rootModuleTarget().isDarwin()) {
if (exe.rootModuleTarget().os.tag.isDarwin()) {
// useful for package maintainers
exe.headerpad_max_install_names = true;
}
Expand Down
4 changes: 2 additions & 2 deletions doc/langref/test_switch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ test "switch simple" {
}

// Switch expressions can be used outside a function:
const os_msg = switch (builtin.target.os.tag) {
const os_msg = switch (builtin.os.tag) {
.linux => "we found a linux user",
else => "not a linux user",
};

// Inside a function, switch statements implicitly are compile-time
// evaluated if the target expression is compile-time known.
test "switch inside function" {
switch (builtin.target.os.tag) {
switch (builtin.os.tag) {
.fuchsia => {
// On an OS other than fuchsia, block is not even analyzed,
// so this compile error is not triggered.
Expand Down
2 changes: 1 addition & 1 deletion doc/langref/test_variable_alignment.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test "variable alignment" {
const align_of_i32 = @alignOf(@TypeOf(x));
try expect(@TypeOf(&x) == *i32);
try expect(*i32 == *align(align_of_i32) i32);
if (builtin.target.cpu.arch == .x86_64) {
if (builtin.cpu.arch == .x86_64) {
try expect(@typeInfo(*i32).pointer.alignment == 4);
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/compiler/aro/aro/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ fn generateSystemDefines(comp: *Compilation, w: anytype) !void {
),
else => {},
}
if (comp.target.isAndroid()) {
if (comp.target.abi.isAndroid()) {
try w.writeAll("#define __ANDROID__ 1\n");
}

Expand Down Expand Up @@ -734,7 +734,7 @@ pub fn float80Type(comp: *const Compilation) ?Type {

/// Smallest integer type with at least N bits
pub fn intLeastN(comp: *const Compilation, bits: usize, signedness: std.builtin.Signedness) Type {
if (bits == 64 and (comp.target.isDarwin() or comp.target.isWasm())) {
if (bits == 64 and (comp.target.os.tag.isDarwin() or comp.target.cpu.arch.isWasm())) {
// WebAssembly and Darwin use `long long` for `int_least64_t` and `int_fast64_t`.
return .{ .specifier = if (signedness == .signed) .long_long else .ulong_long };
}
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/aro/aro/Driver/GCCDetector.zig
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fn collectLibDirsAndTriples(
// TODO
return;
}
if (target.isAndroid()) {
if (target.abi.isAndroid()) {
const AArch64AndroidTriples: [1][]const u8 = .{"aarch64-linux-android"};
const ARMAndroidTriples: [1][]const u8 = .{"arm-linux-androideabi"};
const MIPSELAndroidTriples: [1][]const u8 = .{"mipsel-linux-android"};
Expand Down
14 changes: 7 additions & 7 deletions lib/compiler/aro/aro/Toolchain.zig
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub fn getLinkerPath(tc: *const Toolchain, buf: []u8) ![]const u8 {
} else {
var linker_name = try std.ArrayList(u8).initCapacity(tc.driver.comp.gpa, 5 + use_linker.len); // "ld64." ++ use_linker
defer linker_name.deinit();
if (tc.getTarget().isDarwin()) {
if (tc.getTarget().os.tag.isDarwin()) {
linker_name.appendSliceAssumeCapacity("ld64.");
} else {
linker_name.appendSliceAssumeCapacity("ld.");
Expand Down Expand Up @@ -343,7 +343,7 @@ pub fn buildLinkerArgs(tc: *Toolchain, argv: *std.ArrayList([]const u8)) !void {
}

fn getDefaultRuntimeLibKind(tc: *const Toolchain) RuntimeLibKind {
if (tc.getTarget().isAndroid()) {
if (tc.getTarget().abi.isAndroid()) {
return .compiler_rt;
}
return .libgcc;
Expand All @@ -369,7 +369,7 @@ pub fn getCompilerRt(tc: *const Toolchain, component: []const u8, file_kind: Fil

fn getLibGCCKind(tc: *const Toolchain) LibGCCKind {
const target = tc.getTarget();
if (tc.driver.static_libgcc or tc.driver.static or tc.driver.static_pie or target.isAndroid()) {
if (tc.driver.static_libgcc or tc.driver.static or tc.driver.static_pie or target.abi.isAndroid()) {
return .static;
}
if (tc.driver.shared_libgcc) {
Expand All @@ -384,7 +384,7 @@ fn getUnwindLibKind(tc: *const Toolchain) !UnwindLibKind {
switch (tc.getRuntimeLibKind()) {
.compiler_rt => {
const target = tc.getTarget();
if (target.isAndroid() or target.os.tag == .aix) {
if (target.abi.isAndroid() or target.os.tag == .aix) {
return .compiler_rt;
} else {
return .none;
Expand Down Expand Up @@ -417,14 +417,14 @@ fn getAsNeededOption(is_solaris: bool, needed: bool) []const u8 {
fn addUnwindLibrary(tc: *const Toolchain, argv: *std.ArrayList([]const u8)) !void {
const unw = try tc.getUnwindLibKind();
const target = tc.getTarget();
if ((target.isAndroid() and unw == .libgcc) or
if ((target.abi.isAndroid() and unw == .libgcc) or
target.os.tag == .elfiamcu or
target.ofmt == .wasm or
target_util.isWindowsMSVCEnvironment(target) or
unw == .none) return;

const lgk = tc.getLibGCCKind();
const as_needed = lgk == .unspecified and !target.isAndroid() and !target_util.isCygwinMinGW(target) and target.os.tag != .aix;
const as_needed = lgk == .unspecified and !target.abi.isAndroid() and !target_util.isCygwinMinGW(target) and target.os.tag != .aix;
if (as_needed) {
try argv.append(getAsNeededOption(target.os.tag == .solaris, true));
}
Expand Down Expand Up @@ -483,7 +483,7 @@ pub fn addRuntimeLibs(tc: *const Toolchain, argv: *std.ArrayList([]const u8)) !v
},
}

if (target.isAndroid() and !tc.driver.static and !tc.driver.static_pie) {
if (target.abi.isAndroid() and !tc.driver.static and !tc.driver.static_pie) {
try argv.append("-ldl");
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/aro/aro/Type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ pub fn alignof(ty: Type, comp: *const Compilation) u29 {
.double => comp.target.cTypeAlignment(.double),
.long_double => comp.target.cTypeAlignment(.longdouble),

.int128, .uint128 => if (comp.target.cpu.arch == .s390x and comp.target.os.tag == .linux and comp.target.isGnu()) 8 else 16,
.int128, .uint128 => if (comp.target.cpu.arch == .s390x and comp.target.os.tag == .linux and comp.target.abi.isGnu()) 8 else 16,
.fp16, .float16 => 2,

.float128 => 16,
Expand Down
18 changes: 9 additions & 9 deletions lib/compiler/aro/aro/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ pub fn int64Type(target: std.Target) Type {

.sparc64 => return intMaxType(target),

.x86, .x86_64 => if (!target.isDarwin()) return intMaxType(target),
.aarch64, .aarch64_be => if (!target.isDarwin() and target.os.tag != .openbsd and target.os.tag != .windows) return .{ .specifier = .long },
.x86, .x86_64 => if (!target.os.tag.isDarwin()) return intMaxType(target),
.aarch64, .aarch64_be => if (!target.os.tag.isDarwin() and target.os.tag != .openbsd and target.os.tag != .windows) return .{ .specifier = .long },
else => {},
}
return .{ .specifier = .long_long };
Expand All @@ -144,7 +144,7 @@ pub fn defaultFunctionAlignment(target: std.Target) u8 {
}

pub fn isTlsSupported(target: std.Target) bool {
if (target.isDarwin()) {
if (target.os.tag.isDarwin()) {
var supported = false;
switch (target.os.tag) {
.macos => supported = !(target.os.isAtLeast(.macos, .{ .major = 10, .minor = 7, .patch = 0 }) orelse false),
Expand Down Expand Up @@ -199,7 +199,7 @@ pub fn minZeroWidthBitfieldAlignment(target: std.Target) ?u29 {
pub fn unnamedFieldAffectsAlignment(target: std.Target) bool {
switch (target.cpu.arch) {
.aarch64 => {
if (target.isDarwin() or target.os.tag == .windows) return false;
if (target.os.tag.isDarwin() or target.os.tag == .windows) return false;
return true;
},
.armeb => {
Expand Down Expand Up @@ -229,7 +229,7 @@ pub fn packAllEnums(target: std.Target) bool {
pub fn defaultAlignment(target: std.Target) u29 {
switch (target.cpu.arch) {
.avr => return 1,
.arm => if (target.isAndroid() or target.os.tag == .ios) return 16 else return 8,
.arm => if (target.abi.isAndroid() or target.os.tag == .ios) return 16 else return 8,
.sparc => if (target.cpu.has(.sparc, .v9)) return 16 else return 8,
.mips, .mipsel => switch (target.abi) {
.none, .gnuabi64 => return 16,
Expand All @@ -242,9 +242,9 @@ pub fn defaultAlignment(target: std.Target) u29 {
pub fn systemCompiler(target: std.Target) LangOpts.Compiler {
// Android is linux but not gcc, so these checks go first
// the rest for documentation as fn returns .clang
if (target.isDarwin() or
target.isAndroid() or
target.isBSD() or
if (target.os.tag.isDarwin() or
target.abi.isAndroid() or
target.os.tag.isBSD() or
target.os.tag == .fuchsia or
target.os.tag == .solaris or
target.os.tag == .haiku or
Expand All @@ -268,7 +268,7 @@ pub fn systemCompiler(target: std.Target) LangOpts.Compiler {

pub fn hasFloat128(target: std.Target) bool {
if (target.cpu.arch.isWasm()) return true;
if (target.isDarwin()) return false;
if (target.os.tag.isDarwin()) return false;
if (target.cpu.arch.isPowerPC()) return target.cpu.has(.powerpc, .float128);
return switch (target.os.tag) {
.dragonfly,
Expand Down
14 changes: 7 additions & 7 deletions lib/compiler/aro/aro/toolchains/Linux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn discover(self: *Linux, tc: *Toolchain) !void {
fn buildExtraOpts(self: *Linux, tc: *const Toolchain) !void {
const gpa = tc.driver.comp.gpa;
const target = tc.getTarget();
const is_android = target.isAndroid();
const is_android = target.abi.isAndroid();
if (self.distro.isAlpine() or is_android) {
try self.extra_opts.ensureUnusedCapacity(gpa, 2);
self.extra_opts.appendAssumeCapacity("-z");
Expand Down Expand Up @@ -113,7 +113,7 @@ fn findPaths(self: *Linux, tc: *Toolchain) !void {
try tc.addPathIfExists(&.{ sysroot, "/lib", multiarch_triple }, .file);
try tc.addPathIfExists(&.{ sysroot, "/lib", "..", os_lib_dir }, .file);

if (target.isAndroid()) {
if (target.abi.isAndroid()) {
// TODO
}
try tc.addPathIfExists(&.{ sysroot, "/usr", "lib", multiarch_triple }, .file);
Expand Down Expand Up @@ -156,7 +156,7 @@ fn getStatic(self: *const Linux, d: *const Driver) bool {

pub fn getDefaultLinker(self: *const Linux, target: std.Target) []const u8 {
_ = self;
if (target.isAndroid()) {
if (target.abi.isAndroid()) {
return "ld.lld";
}
return "ld";
Expand All @@ -169,7 +169,7 @@ pub fn buildLinkerArgs(self: *const Linux, tc: *const Toolchain, argv: *std.Arra
const is_pie = self.getPIE(d);
const is_static_pie = try self.getStaticPIE(d);
const is_static = self.getStatic(d);
const is_android = target.isAndroid();
const is_android = target.abi.isAndroid();
const is_iamcu = target.os.tag == .elfiamcu;
const is_ve = target.cpu.arch == .ve;
const has_crt_begin_end_files = target.abi != .none; // TODO: clang checks for MIPS vendor
Expand Down Expand Up @@ -326,7 +326,7 @@ pub fn buildLinkerArgs(self: *const Linux, tc: *const Toolchain, argv: *std.Arra
}

fn getMultiarchTriple(target: std.Target) ?[]const u8 {
const is_android = target.isAndroid();
const is_android = target.abi.isAndroid();
const is_mips_r6 = target.cpu.has(.mips, .mips32r6);
return switch (target.cpu.arch) {
.arm, .thumb => if (is_android) "arm-linux-androideabi" else if (target.abi == .gnueabihf) "arm-linux-gnueabihf" else "arm-linux-gnueabi",
Expand Down Expand Up @@ -380,7 +380,7 @@ pub fn defineSystemIncludes(self: *const Linux, tc: *const Toolchain) !void {

// musl prefers /usr/include before builtin includes, so musl targets will add builtins
// at the end of this function (unless disabled with nostdlibinc)
if (!tc.driver.nobuiltininc and (!target.isMusl() or tc.driver.nostdlibinc)) {
if (!tc.driver.nobuiltininc and (!target.abi.isMusl() or tc.driver.nostdlibinc)) {
try comp.addBuiltinIncludeDir(tc.driver.aro_name);
}

Expand Down Expand Up @@ -411,7 +411,7 @@ pub fn defineSystemIncludes(self: *const Linux, tc: *const Toolchain) !void {
try comp.addSystemIncludeDir("/usr/include");

std.debug.assert(!tc.driver.nostdlibinc);
if (!tc.driver.nobuiltininc and target.isMusl()) {
if (!tc.driver.nobuiltininc and target.abi.isMusl()) {
try comp.addBuiltinIncludeDir(tc.driver.aro_name);
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/compiler/resinator/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ pub fn main() !void {

fn getIncludePaths(arena: std.mem.Allocator, auto_includes_option: cli.Options.AutoIncludes, zig_lib_dir: []const u8) ![]const []const u8 {
var includes = auto_includes_option;
if (builtin.target.os.tag != .windows) {
if (builtin.os.tag != .windows) {
switch (includes) {
// MSVC can't be found when the host isn't Windows, so short-circuit.
.msvc => return error.MsvcIncludesNotFound,
Expand All @@ -282,7 +282,7 @@ fn getIncludePaths(arena: std.mem.Allocator, auto_includes_option: cli.Options.A
// MSVC is only detectable on Windows targets. This unreachable is to signify
// that .any and .msvc should be dealt with on non-Windows targets before this point,
// since getting MSVC include paths uses Windows-only APIs.
if (builtin.target.os.tag != .windows) unreachable;
if (builtin.os.tag != .windows) unreachable;

const target_query: std.Target.Query = .{
.os_tag = .windows,
Expand Down
6 changes: 3 additions & 3 deletions lib/compiler_rt/common.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ else
/// For WebAssembly this allows the symbol to be resolved to other modules, but will not
/// export it to the host runtime.
pub const visibility: std.builtin.SymbolVisibility =
if (builtin.target.isWasm() and linkage != .internal) .hidden else .default;
if (builtin.cpu.arch.isWasm() and linkage != .internal) .hidden else .default;
pub const want_aeabi = switch (builtin.abi) {
.eabi,
.eabihf,
Expand Down Expand Up @@ -96,15 +96,15 @@ pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, ret_
pub fn F16T(comptime OtherType: type) type {
return switch (builtin.cpu.arch) {
.arm, .armeb, .thumb, .thumbeb => if (builtin.cpu.has(.arm, .has_v8))
switch (builtin.abi.floatAbi()) {
switch (builtin.abi.float()) {
.soft => u16,
.hard => f16,
}
else
u16,
.aarch64, .aarch64_be => f16,
.riscv32, .riscv64 => f16,
.x86, .x86_64 => if (builtin.target.isDarwin()) switch (OtherType) {
.x86, .x86_64 => if (builtin.os.tag.isDarwin()) switch (OtherType) {
// Starting with LLVM 16, Darwin uses different abi for f16
// depending on the type of the other return/argument..???
f32, f64 => u16,
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler_rt/extendf_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ test "extendhfsf2" {
try test__extendhfsf2(0x7f00, 0x7fe00000); // sNaN
// On x86 the NaN becomes quiet because the return is pushed on the x87
// stack due to ABI requirements
if (builtin.target.cpu.arch != .x86 and builtin.target.os.tag == .windows)
if (builtin.cpu.arch != .x86 and builtin.os.tag == .windows)
try test__extendhfsf2(0x7c01, 0x7f802000); // sNaN

try test__extendhfsf2(0, 0); // 0
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler_rt/udivmod.zig
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn divwide_generic(comptime T: type, _u1: T, _u0: T, v_: T, r: *T) T {

fn divwide(comptime T: type, _u1: T, _u0: T, v: T, r: *T) T {
@setRuntimeSafety(is_test);
if (T == u64 and builtin.target.cpu.arch == .x86_64 and builtin.target.os.tag != .windows) {
if (T == u64 and builtin.cpu.arch == .x86_64 and builtin.os.tag != .windows) {
var rem: T = undefined;
const quo = asm (
\\divq %[v]
Expand Down
6 changes: 3 additions & 3 deletions lib/std/Build/Step/Compile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
if (compile.linkage != null and compile.linkage.? == .static) {
compile.out_lib_filename = compile.out_filename;
} else if (compile.version) |version| {
if (target.isDarwin()) {
if (target.os.tag.isDarwin()) {
compile.major_only_filename = owner.fmt("lib{s}.{d}.dylib", .{
compile.name,
version.major,
Expand All @@ -472,7 +472,7 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
compile.out_lib_filename = compile.out_filename;
}
} else {
if (target.isDarwin()) {
if (target.os.tag.isDarwin()) {
compile.out_lib_filename = compile.out_filename;
} else if (target.os.tag == .windows) {
compile.out_lib_filename = owner.fmt("{s}.lib", .{compile.name});
Expand Down Expand Up @@ -1519,7 +1519,7 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
try zig_args.append(b.fmt("{}", .{version}));
}

if (compile.rootModuleTarget().isDarwin()) {
if (compile.rootModuleTarget().os.tag.isDarwin()) {
const install_name = compile.install_name orelse b.fmt("@rpath/{s}{s}{s}", .{
compile.rootModuleTarget().libPrefix(),
compile.name,
Expand Down
Loading

0 comments on commit 94d5672

Please sign in to comment.