From a9589c2e56bb5eb792712d13c98299489f70848a Mon Sep 17 00:00:00 2001 From: Sam Tebbs Date: Tue, 2 Jun 2020 18:54:41 +0100 Subject: [PATCH 1/2] Add architecture option to build.zig --- build.zig | 55 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/build.zig b/build.zig index 1707bb1..1affb53 100644 --- a/build.zig +++ b/build.zig @@ -8,16 +8,19 @@ const CrossTarget = std.zig.CrossTarget; const fs = std.fs; const Mode = builtin.Mode; -pub fn build(b: *Builder) !void { - const target = CrossTarget{ - .cpu_arch = .i386, - .os_tag = .freestanding, - .cpu_model = .{ .explicit = &Target.x86.cpu._i686 }, - }; +const x86_i686 = CrossTarget{ + .cpu_arch = .i386, + .os_tag = .freestanding, + .cpu_model = .{ .explicit = &Target.x86.cpu._i686 }, +}; - const target_str = switch (target.getCpuArch()) { - .i386 => "x86", - else => unreachable, +pub fn build(b: *Builder) !void { + const arch = b.option([]const u8, "arch", "Architecture to build for: x86") orelse "x86"; + const target: CrossTarget = if (std.mem.eql(u8, "x86", arch)) + x86_i686 + else { + std.debug.warn("Unsupported or unknown architecture '{}'\n", .{arch}); + unreachable; }; const fmt_step = b.addFmt(&[_][]const u8{ @@ -28,12 +31,12 @@ pub fn build(b: *Builder) !void { 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" }); + const constants_path = try fs.path.join(b.allocator, &[_][]const u8{ "src/kernel/arch", arch, "constants.zig" }); const build_mode = b.standardReleaseOptions(); const rt_test = b.option(bool, "rt-test", "enable/disable runtime testing") orelse false; - const exec = b.addExecutable("pluto", main_src); + const exec = b.addExecutable("pluto.elf", main_src); exec.addPackagePath("constants", constants_path); exec.setOutputDir(b.cache_root); exec.addBuildOption(bool, "rt_test", rt_test); @@ -46,14 +49,17 @@ pub fn build(b: *Builder) !void { 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 }); + const make_iso = switch (target.getCpuArch()) { + .i386 => b.addSystemCommand(&[_][]const u8{ "./makeiso.sh", boot_path, modules_path, iso_dir_path, exec.getOutputPath(), output_iso }), + else => unreachable, + }; 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 }); + const script = b.addSystemCommand(&[_][]const u8{ "python3", "test/rt-test.py", arch, b.zig_exe }); test_step.dependOn(&script.step); } else { const mock_path = "\"../../test/mock/kernel/\""; @@ -68,7 +74,7 @@ pub fn build(b: *Builder) !void { if (builtin.os.tag != .windows) unit_tests.enable_qemu = true; - unit_tests.setTarget(.{ .cpu_arch = .i386 }); + unit_tests.setTarget(.{ .cpu_arch = target.cpu_arch }); test_step.dependOn(&unit_tests.step); } @@ -82,16 +88,25 @@ pub fn build(b: *Builder) !void { }; const qemu_args = &[_][]const u8{ qemu_bin, - "-cdrom", - output_iso, - "-boot", - "d", "-serial", "stdio", }; + const qemu_machine_args = switch (target.getCpuArch()) { + .i386 => &[_][]const u8{ + "-boot", + "d", + "-cdrom", + output_iso, + }, + else => null, + }; const qemu_cmd = b.addSystemCommand(qemu_args); const qemu_debug_cmd = b.addSystemCommand(qemu_args); qemu_debug_cmd.addArgs(&[_][]const u8{ "-s", "-S" }); + if (qemu_machine_args) |machine_args| { + qemu_cmd.addArgs(machine_args); + qemu_debug_cmd.addArgs(machine_args); + } if (rt_test) { const qemu_rt_test_args = &[_][]const u8{ "-display", "none" }; @@ -108,9 +123,11 @@ pub fn build(b: *Builder) !void { 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() }); const debug_cmd = b.addSystemCommand(&[_][]const u8{ - "gdb", + "gdb-multiarch", "-ex", symbol_file_arg, + "-ex", + "set architecture auto", }); debug_cmd.addArgs(&[_][]const u8{ "-ex", From c2a9622b3d23c486f8252cc024d7d81f2a50de5f Mon Sep 17 00:00:00 2001 From: Sam Tebbs Date: Tue, 2 Jun 2020 21:16:37 +0100 Subject: [PATCH 2/2] Add info to README --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b86f41e..d5d19c5 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ zig build debug-run ## Debug -Launch a gdb instance and connect to qemu. +Launch a gdb-multiarch instance and connect to qemu. ```Shell zig build debug @@ -53,10 +53,13 @@ zig build test * `-Drt-test=`: Boolean (default `false`). * **build**: Build with runtime testing enabled. Makes the kernel bigger and slower but tests important functionality. - * **test**: Run the runtime testing script instead of the unittests. Checks for the expected log statements and fails if any are missing. + * **test**: Run the runtime testing script instead of the unit tests. Checks for the expected log statements and fails if any are missing. * `-D[build-mode]=`: Boolean (default `false`). * **build**: Build a certain build mode (*release-safe*, *release-fast*, *release-small*). Don't set in order to use the *debug* build mode. * **test**: Test a certain build mode (*release-safe*, *release-fast*, *release-small*). Don't set in order to use the *debug* build mode. +* `-Darch=`: String (default `x86`). Currently the only supported value is `x86`. + * **build**: Build for a certain architecture. + * **test**: Test a certain architecture. ## Contribution