-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.zig
107 lines (85 loc) · 3.87 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
const std = @import("std");
const Builder = @import("std").build.Builder;
const Target = @import("std").Target;
const CrossTarget = @import("std").zig.CrossTarget;
const Feature = @import("std").Target.Cpu.Feature;
const objFiles = [_][]u8{"main"};
pub fn build(b: *Builder) void {
const features = Target.x86.Feature;
var disabled_features = Feature.Set.empty;
var enabled_features = Feature.Set.empty;
disabled_features.addFeature(@intFromEnum(features.mmx));
disabled_features.addFeature(@intFromEnum(features.sse));
disabled_features.addFeature(@intFromEnum(features.sse2));
disabled_features.addFeature(@intFromEnum(features.avx));
disabled_features.addFeature(@intFromEnum(features.avx2));
enabled_features.addFeature(@intFromEnum(features.soft_float));
const target = CrossTarget{ .cpu_arch = Target.Cpu.Arch.x86, .os_tag = Target.Os.Tag.freestanding, .cpu_features_sub = disabled_features, .cpu_features_add = enabled_features };
const optimize = b.standardOptimizeOption(.{});
const build_initcode_step = buildInitcode(b);
// objects for assembly
const main_obj = b.addObject(std.Build.ObjectOptions{
.name = "main",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
const kernel = b.addExecutable(.{
.name = "kernel.elf",
.root_source_file = .{ .path = "src/entry.zig" },
.optimize = optimize,
.target = target,
.linkage = std.build.CompileStep.Linkage.static,
});
kernel.setLinkerScriptPath(.{ .path = "src/kernel.ld" });
kernel.addAssemblyFile(.{ .path = "src/trapasm.S" });
kernel.addAssemblyFile(.{ .path = "src/vector.S" });
kernel.addAssemblyFile(.{ .path = "src/swtch.S" });
kernel.addObject(main_obj);
kernel.addObjectFile(.{ .path = "zig-out/bin/initcode.o" });
kernel.code_model = .kernel;
b.installArtifact(kernel);
const kernel_step = b.step("kernel", "Build the kernel");
kernel_step.dependOn(&kernel.step);
kernel.step.dependOn(build_initcode_step);
const iso_dir = "./zig-cache/iso_root";
const boot_dir = "./zig-cache/iso_root/boot";
const grub_dir = "./zig-cache/iso_root/boot/grub";
const kernel_path = std.fmt.allocPrint(std.heap.page_allocator, "./zig-out/bin/{s}", .{kernel.out_filename}) catch unreachable;
const iso_path = b.fmt("{s}/disk.iso", .{b.exe_dir});
const iso_cmd_str = &[_][]const u8{ "/bin/sh", "-c", std.mem.concat(b.allocator, u8, &[_][]const u8{ "mkdir -p ", grub_dir, " && ", "cp ", kernel_path, " ", boot_dir, " && ", "cp grub.cfg ", grub_dir, " && ", "grub-mkrescue -o ", iso_path, " ", iso_dir }) catch unreachable };
const iso_cmd = b.addSystemCommand(iso_cmd_str);
iso_cmd.step.dependOn(kernel_step);
const iso_step = b.step("iso", "Build an ISO image");
iso_step.dependOn(&iso_cmd.step);
b.default_step.dependOn(iso_step);
const run_cmd_str = [_][]const u8{
"qemu-system-i386",
"-drive",
"file=zig-out/bin/disk.iso,index=0,media=disk,format=raw",
"-m",
"512",
"-smp",
"1",
"-no-shutdown",
"-no-reboot",
"-nographic",
"-gdb",
"tcp::1234",
};
const run_cmd = b.addSystemCommand(&run_cmd_str);
run_cmd.step.dependOn(b.getInstallStep());
const run_step = b.step("run", "Run the kernel");
run_step.dependOn(&run_cmd.step);
const debug_cmd_str = run_cmd_str ++ [_][]const u8{
"-S",
};
const debug_cmd = b.addSystemCommand(&debug_cmd_str);
debug_cmd.step.dependOn(b.getInstallStep());
const debug_step = b.step("debug", "Debug the kernel");
debug_step.dependOn(&debug_cmd.step);
}
fn buildInitcode(b: *Builder) *std.Build.Step {
const build_initcode_command = b.addSystemCommand(&[_][]const u8{"./scripts/build_initcode.sh"});
return &build_initcode_command.step;
}