Restrict the target to .freestanding/.none/supported cpu

This commit is contained in:
Eugene Rossokha
2025-03-17 20:05:24 +02:00
parent aee04d2510
commit caec288157
+14 -33
View File
@@ -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) {