From caec2881570d61f47f7b648b65ad1c800ec9a874 Mon Sep 17 00:00:00 2001 From: Eugene Rossokha Date: Mon, 17 Mar 2025 20:05:24 +0200 Subject: [PATCH] Restrict the target to .freestanding/.none/supported cpu --- build.zig | 47 ++++++++++++++--------------------------------- 1 file changed, 14 insertions(+), 33 deletions(-) diff --git a/build.zig b/build.zig index 00b2460..c5cd3d5 100644 --- a/build.zig +++ b/build.zig @@ -1,33 +1,20 @@ const std = @import("std"); -const DEFAULT_ARCH = "riscv64"; +const DEFAULT_ARCH = SupportedArch.riscv64; const SupportedArch = enum { aarch64, riscv64, - fn fromStr(s: []const u8) ?SupportedArch { - return if (std.mem.eql(u8, s, "riscv64")) // - .riscv64 - else if (std.mem.eql(u8, s, "aarch64")) // - .aarch64 - else // - null; - } - fn makeTarget(self: SupportedArch, b: *std.Build) std.Build.ResolvedTarget { - return switch (self) { - .riscv64 => b.standardTargetOptions(.{ .default_target = .{ - .cpu_arch = .riscv64, - .os_tag = .freestanding, - .abi = .none, - } }), - .aarch64 => b.standardTargetOptions(.{ .default_target = .{ - .cpu_arch = .aarch64, - .os_tag = .freestanding, - .abi = .none, - } }), - }; + return b.resolveTargetQuery(.{ + .cpu_arch = switch (self) { + .riscv64 => .riscv64, + .aarch64 => .aarch64, + }, + .os_tag = .freestanding, + .abi = .none, + }); } fn addTargetSpecific(self: SupportedArch, b: *std.Build, kernel: *std.Build.Step.Compile) anyerror!*std.Build.Step { @@ -132,19 +119,13 @@ fn build_riscv64(b: *std.Build) anyerror!void { } pub fn build(b: *std.Build) anyerror!void { - const maybeArchOption = b.option([]const u8, "arch", "Architecture to use"); - - const archOption = maybeArchOption orelse DEFAULT_ARCH; - - const sTarget = SupportedArch.fromStr(archOption) orelse { - std.debug.print("Unsupported target arch: '{s}'", .{archOption}); - return error.InvalidArgument; - }; + const maybeArchOption = b.option(SupportedArch, "arch", "Architecture to use"); + const arch = maybeArchOption orelse DEFAULT_ARCH; + const target = arch.makeTarget(b); const optimize = b.standardOptimizeOption(.{ .preferred_optimize_mode = .ReleaseFast }); - const target = sTarget.makeTarget(b); - const codeModel: std.builtin.CodeModel = switch (sTarget) { + const codeModel: std.builtin.CodeModel = switch (arch) { .riscv64 => .medium, .aarch64 => .small, }; @@ -163,7 +144,7 @@ pub fn build(b: *std.Build) anyerror!void { }); kernel.pie = true; - const kernelStep = try sTarget.addTargetSpecific(b, kernel); + const kernelStep = try arch.addTargetSpecific(b, kernel); // TODO QEMU binary override const qemu_info = switch (target.result.cpu.arch) {