-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
143 lines (118 loc) · 4.52 KB
/
build.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const std = @import("std");
pub fn build(b: *std.Build) void {
var target: std.zig.CrossTarget = .{
.cpu_arch = .x86_64,
.os_tag = .freestanding,
.abi = .none,
};
// Those CPU features require additional initialization, so we disable them
const Features = std.Target.x86.Feature;
target.cpu_features_sub.addFeature(@intFromEnum(Features.mmx));
target.cpu_features_sub.addFeature(@intFromEnum(Features.sse));
target.cpu_features_sub.addFeature(@intFromEnum(Features.sse2));
target.cpu_features_sub.addFeature(@intFromEnum(Features.avx));
target.cpu_features_sub.addFeature(@intFromEnum(Features.avx2));
target.cpu_features_add.addFeature(@intFromEnum(Features.soft_float));
const optimize = b.standardOptimizeOption(.{});
const limine = b.dependency("limine", .{});
const exe = b.addExecutable(.{
.name = "Starfire",
.root_source_file = b.path("src/main.zig"),
.target = b.resolveTargetQuery(target),
.optimize = optimize,
.code_model = .kernel,
.pic = true,
});
exe.root_module.addImport("limine", limine.module("limine"));
exe.setLinkerScriptPath(b.path("linker.ld"));
exe.addAssemblyFile(b.path("src/x86/asm/idt.S"));
// Disable LTO. This prevents issues with limine requests
exe.want_lto = false;
b.installArtifact(exe);
const make_iso_step = b.step("make-iso", "Create a bootable ISO image");
make_iso_step.makeFn = makeIso;
make_iso_step.dependOn(b.getInstallStep());
const run_qemu_step = b.step("run-qemu", "Run the operating system under QEMU");
run_qemu_step.makeFn = runQemu;
run_qemu_step.dependOn(make_iso_step);
const run_bochs_step = b.step("run-bochs", "Run the operating system under Bochs");
run_bochs_step.makeFn = runBochs;
run_bochs_step.dependOn(make_iso_step);
}
fn makeIso(self: *std.Build.Step, progress: std.Progress.Node) !void {
_ = progress;
const current_dir = std.fs.cwd();
current_dir.makePath("iso_root/boot/limine") catch {};
current_dir.makePath("iso_root/EFI/BOOT") catch {};
var isoEfiBootDirectory = try current_dir.openDir("iso_root/EFI/BOOT", .{});
defer isoEfiBootDirectory.close();
var isoBootDirectory = try current_dir.openDir("iso_root/boot", .{});
defer isoBootDirectory.close();
var isoLimineDirectory = try current_dir.openDir("iso_root/boot/limine", .{});
defer isoLimineDirectory.close();
try current_dir.copyFile("limine/BOOTX64.EFI", isoEfiBootDirectory, "BOOTX64.EFI", .{});
try current_dir.copyFile("zig-out/bin/Starfire", isoBootDirectory, "Starfire", .{});
try current_dir.copyFile("limine/limine-uefi-cd.bin", isoLimineDirectory, "limine-uefi-cd.bin", .{});
try current_dir.copyFile("limine/limine-bios-cd.bin", isoLimineDirectory, "limine-bios-cd.bin", .{});
try current_dir.copyFile("limine/limine-bios.sys", isoLimineDirectory, "limine-bios.sys", .{});
try current_dir.copyFile("limine/limine.cfg", isoLimineDirectory, "limine.cfg", .{});
const xorriso_argv = [_][]const u8{
"xorriso",
"-as",
"mkisofs",
"-b",
"boot/limine/limine-bios-cd.bin",
"-no-emul-boot",
"-boot-load-size",
"4",
"-boot-info-table",
"--efi-boot",
"boot/limine/limine-uefi-cd.bin",
"-efi-boot-part",
"--efi-boot-image",
"--protective-msdos-label",
"iso_root",
"-o",
"Starfire.iso",
};
_ = try self.evalChildProcess(&xorriso_argv);
const limine_bios_deploy_argv = [_][]const u8{
"limine/limine",
"bios-install",
"Starfire.iso",
};
_ = try self.evalChildProcess(&limine_bios_deploy_argv);
}
fn runQemu(self: *std.Build.Step, progress: std.Progress.Node) !void {
_ = progress;
// Connect to the VM using netcat, e.g. "nc 127.0.0.1 4444"
const qemu_argv = [_][]const u8{
"qemu-system-x86_64",
"-enable-kvm",
"-M",
"q35",
"-cpu",
"host",
"-smp",
"dies=1,sockets=1,cores=2,threads=1",
"-display",
"none",
"-serial",
"tcp:127.0.0.1:4444,server=on,wait=on",
"-m",
"2G",
"-cdrom",
"Starfire.iso",
};
_ = try self.evalChildProcess(&qemu_argv);
}
fn runBochs(self: *std.Build.Step, progress: std.Progress.Node) !void {
_ = progress;
const bochs_argv = [_][]const u8{
"bochs",
"-q",
"-f",
"./bochsrc",
};
_ = try self.evalChildProcess(&bochs_argv);
}