pluto/build.zig

137 lines
4.8 KiB
Zig
Raw Normal View History

const std = @import("std");
const builtin = @import("builtin");
const Builder = std.build.Builder;
const LibExeObjStep = std.build.LibExeObjStep;
const Step = std.build.Step;
const Target = std.build.Target;
const fs = std.fs;
const Mode = builtin.Mode;
pub fn build(b: *Builder) !void {
const target = Target{
.Cross = Target.Cross{
.arch = .i386,
.os = .freestanding,
.abi = .gnu,
2020-02-01 17:34:06 +01:00
.cpu_features = Target.CpuFeatures.initFromCpu(.i386, &builtin.Target.x86.cpu._i686),
},
};
const test_target = Target{
.Cross = Target.Cross{
.arch = .i386,
.os = .linux,
.abi = .gnu,
2020-02-01 17:34:06 +01:00
.cpu_features = Target.CpuFeatures.initFromCpu(.i386, &builtin.Target.x86.cpu._i686),
},
};
const target_str = switch (target.getArch()) {
.i386 => "x86",
else => unreachable,
};
2020-01-15 17:30:30 +01:00
const fmt_step = b.addFmt(&[_][]const u8{
"build.zig",
"src",
"test",
});
b.default_step.dependOn(&fmt_step.step);
const main_src = "src/kernel/kmain.zig";
const constants_path = try fs.path.join(b.allocator, &[_][]const u8{ "src/kernel/arch", target_str, "constants.zig" });
2019-09-17 20:00:33 +02:00
const build_mode = b.standardReleaseOptions();
2019-06-30 00:50:44 +02:00
const rt_test = b.option(bool, "rt-test", "enable/disable runtime testing") orelse false;
const exec = b.addExecutable("pluto", main_src);
exec.addPackagePath("constants", constants_path);
exec.setOutputDir(b.cache_root);
exec.addBuildOption(bool, "rt_test", rt_test);
exec.setBuildMode(build_mode);
exec.setLinkerScriptPath("link.ld");
exec.setTheTarget(target);
const output_iso = try fs.path.join(b.allocator, &[_][]const u8{ b.exe_dir, "pluto.iso" });
2020-01-01 20:12:36 +01:00
const iso_dir_path = try fs.path.join(b.allocator, &[_][]const u8{ b.exe_dir, "iso" });
const boot_path = try fs.path.join(b.allocator, &[_][]const u8{ b.exe_dir, "iso", "boot" });
const modules_path = try fs.path.join(b.allocator, &[_][]const u8{ b.exe_dir, "iso", "modules" });
const make_iso = b.addSystemCommand(&[_][]const u8{ "./makeiso.sh", boot_path, modules_path, iso_dir_path, exec.getOutputPath(), output_iso });
make_iso.step.dependOn(&exec.step);
b.default_step.dependOn(&make_iso.step);
const test_step = b.step("test", "Run tests");
if (rt_test) {
const script = b.addSystemCommand(&[_][]const u8{ "python3", "test/rt-test.py", "x86", b.zig_exe });
test_step.dependOn(&script.step);
} else {
const mock_path = "\"../../test/mock/kernel/\"";
const arch_mock_path = "\"../../../../test/mock/kernel/\"";
const unit_tests = b.addTest(main_src);
unit_tests.setBuildMode(build_mode);
unit_tests.setMainPkgPath(".");
unit_tests.addPackagePath("constants", constants_path);
unit_tests.addBuildOption(bool, "rt_test", rt_test);
unit_tests.addBuildOption([]const u8, "mock_path", mock_path);
unit_tests.addBuildOption([]const u8, "arch_mock_path", arch_mock_path);
const qemu_bin = switch (test_target.getArch()) {
.i386 => "qemu-i386",
else => unreachable,
};
2019-11-02 03:18:02 +01:00
// We need this as the build as the make() doesn't handle it properly
unit_tests.setExecCmd(&[_]?[]const u8{ qemu_bin, null });
unit_tests.setTheTarget(test_target);
2019-11-02 03:18:02 +01:00
test_step.dependOn(&unit_tests.step);
}
const run_step = b.step("run", "Run with qemu");
2019-10-01 18:35:15 +02:00
const run_debug_step = b.step("debug-run", "Run with qemu and wait for a gdb connection");
const qemu_bin = switch (target.getArch()) {
.i386 => "qemu-system-i386",
else => unreachable,
};
2020-01-01 20:12:36 +01:00
const qemu_args = &[_][]const u8{
qemu_bin,
"-cdrom",
output_iso,
"-boot",
"d",
"-serial",
"stdio",
2019-10-01 18:35:15 +02:00
};
const qemu_cmd = b.addSystemCommand(qemu_args);
const qemu_debug_cmd = b.addSystemCommand(qemu_args);
2020-01-01 20:12:36 +01:00
qemu_debug_cmd.addArgs(&[_][]const u8{ "-s", "-S" });
if (rt_test) {
2020-01-01 20:12:36 +01:00
const qemu_rt_test_args = &[_][]const u8{ "-display", "none" };
2019-10-01 18:35:15 +02:00
qemu_cmd.addArgs(qemu_rt_test_args);
qemu_debug_cmd.addArgs(qemu_rt_test_args);
}
qemu_cmd.step.dependOn(&make_iso.step);
qemu_debug_cmd.step.dependOn(&make_iso.step);
2019-10-01 18:35:15 +02:00
run_step.dependOn(&qemu_cmd.step);
run_debug_step.dependOn(&qemu_debug_cmd.step);
2019-10-01 18:35:15 +02:00
const debug_step = b.step("debug", "Debug with gdb and connect to a running qemu instance");
const symbol_file_arg = try std.mem.join(b.allocator, " ", &[_][]const u8{ "symbol-file", exec.getOutputPath() });
2020-01-01 20:12:36 +01:00
const debug_cmd = b.addSystemCommand(&[_][]const u8{
"gdb",
"-ex",
2019-09-12 00:25:34 +02:00
symbol_file_arg,
});
2020-01-01 20:12:36 +01:00
debug_cmd.addArgs(&[_][]const u8{
"-ex",
"target remote localhost:1234",
});
debug_step.dependOn(&debug_cmd.step);
}