Skip to content

Commit

Permalink
Fixes for latest master Zig version
Browse files Browse the repository at this point in the history
There are some minor breaking API changes to the Zig build system. It
is good to get ahead of these so we don't have to deal with all of them
at the next release of Zig.

Hopefully soon we can pin the examples that use Zig to an official
release rather than the master version.
  • Loading branch information
Ivan-Velickovic committed Jan 13, 2024
1 parent 59bfb8d commit 0aaef4d
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 42 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ jobs:
run: sudo apt update && sudo apt install -y make clang lld llvm qemu-system-arm device-tree-compiler expect
- name: Install Zig
run: |
wget https://ziglang.org/builds/zig-linux-x86_64-0.12.0-dev.1594+7048e9366.tar.xz
tar xf zig-linux-x86_64-0.12.0-dev.1594+7048e9366.tar.xz
echo "${PWD}/zig-linux-x86_64-0.12.0-dev.1594+7048e9366/:$PATH" >> $GITHUB_PATH
wget https://ziglang.org/builds/zig-linux-x86_64-0.12.0-dev.2150+63de8a598.tar.xz
tar xf zig-linux-x86_64-0.12.0-dev.2150+63de8a598.tar.xz
echo "${PWD}/zig-linux-x86_64-0.12.0-dev.2150+63de8a598/:$PATH" >> $GITHUB_PATH
- name: Install Rust
run: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
- name: Build and run examples
Expand Down Expand Up @@ -76,9 +76,9 @@ jobs:
echo "/usr/local/opt/llvm/bin:$PATH" >> $GITHUB_PATH
- name: Install Zig
run: |
wget https://ziglang.org/builds/zig-macos-x86_64-0.12.0-dev.1594+7048e9366.tar.xz
tar xf zig-macos-x86_64-0.12.0-dev.1594+7048e9366.tar.xz
echo "${PWD}/zig-macos-x86_64-0.12.0-dev.1594+7048e9366/:$PATH" >> $GITHUB_PATH
wget https://ziglang.org/builds/zig-macos-x86_64-0.12.0-dev.2150+63de8a598.tar.xz
tar xf zig-macos-x86_64-0.12.0-dev.2150+63de8a598.tar.xz
echo "${PWD}/zig-macos-x86_64-0.12.0-dev.2150+63de8a598/:$PATH" >> $GITHUB_PATH
- name: Install Rust
run: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
- name: Build and run examples
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ For educational purposes, you can also build and run this example using the

At the moment, Zig still under heavy development and hence this example depends
on the 'master' version of Zig for now. This example has been built using
`0.12.0-dev.1533+b2ed2c4d4`, so anything equal to or above that version should work.
`0.12.0-dev.2150+63de8a598`, so anything equal to or above that version should work.

You can download Zig [here](https://ziglang.org/download/).

Expand Down
18 changes: 9 additions & 9 deletions examples/simple/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn build(b: *std.Build) void {
std.log.err("Missing -Dboard=<BOARD> argument being passed\n", .{});
std.os.exit(1);
}
const target = findTarget(microkit_board_option.?);
const target = b.resolveTargetQuery(findTarget(microkit_board_option.?));
const microkit_board = @tagName(microkit_board_option.?);

// Since we are relying on Zig to produce the final ELF, it needs to do the
Expand Down Expand Up @@ -135,17 +135,18 @@ pub fn build(b: *std.Build) void {
.name = "vmm.elf",
.target = target,
.optimize = optimize,
// Microkit expects and requires the symbol table to exist in the ELF,
// this means that even when building for release mode, we want to tell
// Zig not to strip symbols from the binary.
.strip = false,
});
// Microkit expects and requires the symbol table to exist in the ELF,
// this means that even when building for release mode, we want to tell
// Zig not to strip symbols from the binary.
exe.strip = false;

// For actually compiling the DTS into a DTB
const dts_path = fmtPrint("board/{s}/linux.dts", .{ microkit_board });
const dtc_cmd = b.addSystemCommand(&[_][]const u8{
"dtc", "-q", "-I", "dts", "-O", "dtb", dts_path
"dtc", "-q", "-I", "dts", "-O", "dtb"
});
dtc_cmd.addFileArg(.{ .path = dts_path });
const dtb = dtc_cmd.captureStdOut();

// Add microkit.h to be used by the API wrapper.
Expand Down Expand Up @@ -214,14 +215,13 @@ pub fn build(b: *std.Build) void {
"-r",
b.getInstallPath(.prefix, "./report.txt")
});
// Running the Microkit tool depends on
microkit_tool_cmd.step.dependOn(b.getInstallStep());
// Add the "microkit" step, and make it the default step when we execute `zig build`>
const microkit_step = b.step("microkit", "Compile and build the final bootable image");
microkit_step.dependOn(&microkit_tool_cmd.step);
b.default_step = microkit_step;

// This is setting up a `qemu` command for running the system via QEMU,
// This is setting up a `qemu` command for running the system using QEMU,
// which we only want to do when we have a board that we can actually simulate.
const loader_arg = fmtPrint("loader,file={s},addr=0x70000000,cpu-num=0", .{ final_image_dest });
if (std.mem.eql(u8, microkit_board, "qemu_arm_virt")) {
Expand All @@ -240,7 +240,7 @@ pub fn build(b: *std.Build) void {
"-nographic",
});
qemu_cmd.step.dependOn(b.default_step);
const simulate_step = b.step("qemu", "Simulate the image via QEMU");
const simulate_step = b.step("qemu", "Simulate the image using QEMU");
simulate_step.dependOn(&qemu_cmd.step);
}
}
2 changes: 1 addition & 1 deletion examples/zig/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ the Zig C compiler. Building the example is done via the Zig build system.

At the moment, Zig still under heavy development and hence this example depends
on the 'master' version of Zig for now. This example has been built using
`0.12.0-dev.1533+b2ed2c4d4`, so anything equal to or above that version should work.
`0.12.0-dev.2150+63de8a598`, so anything equal to or above that version should work.

You can download Zig [here](https://ziglang.org/download/).

Expand Down
42 changes: 20 additions & 22 deletions examples/zig/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,21 @@ fn fmtPrint(comptime fmt: []const u8, args: anytype) []const u8 {
return std.fmt.allocPrint(gpa, fmt, args) catch "Could not format print!";
}

// For this example we hard-code the target to AArch64 and the platform to QEMU ARM virt
// since the main point of this example is to show off using libvmm in another
// systems programming language.
const example_target = std.zig.CrossTarget{
.cpu_arch = .aarch64,
.cpu_model = .{ .explicit = &std.Target.arm.cpu.cortex_a53 },
.os_tag = .freestanding,
.abi = .none,
};

const ConfigOptions = enum {
debug,
release,
};

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{ .default_target = example_target });
// For this example we hard-code the target to AArch64 and the platform to QEMU ARM virt
// since the main point of this example is to show off using libvmm in another
// systems programming language.
const target = b.resolveTargetQuery(.{
.cpu_arch = .aarch64,
.cpu_model = .{ .explicit = &std.Target.arm.cpu.cortex_a53 },
.os_tag = .freestanding,
.abi = .none,
});
const optimize = b.standardOptimizeOption(.{});

// Getting the path to the Microkit SDK before doing anything else
Expand All @@ -48,10 +46,10 @@ pub fn build(b: *std.Build) void {

const zig_libmicrokit = b.addObject(.{
.name = "zig_libmicrokit",
.root_source_file = .{ .path = "src/libmicrokit.c" },
.target = target,
.optimize = optimize,
});
zig_libmicrokit.addCSourceFile(.{ .file = .{ .path = "src/libmicrokit.c" }, .flags = &.{} });
zig_libmicrokit.addIncludePath(.{ .path = "src/" });
zig_libmicrokit.addIncludePath(.{ .path = sdk_board_include_dir });

Expand Down Expand Up @@ -103,24 +101,24 @@ pub fn build(b: *std.Build) void {
.root_source_file = .{ .path = "src/vmm.zig" },
.target = target,
.optimize = optimize,
// Microkit expects and requires the symbol table to exist in the ELF,
// this means that even when building for release mode, we want to tell
// Zig not to strip symbols from the binary.
.strip = false,
});
// Microkit expects and requires the symbol table to exist in the ELF,
// this means that even when building for release mode, we want to tell
// Zig not to strip symbols from the binary.
exe.strip = false;

// For actually compiling the DTS into a DTB
const dts_path = "images/linux.dts";
const dtc_cmd = b.addSystemCommand(&[_][]const u8{
"dtc", "-q", "-I", "dts", "-O", "dtb", dts_path
"dtc", "-q", "-I", "dts", "-O", "dtb"
});
dtc_cmd.addFileArg(.{ .path = "images/linux.dts" });
const dtb = dtc_cmd.captureStdOut();
// When we embed these artifacts into our VMM code, we use @embedFile provided by
// the Zig compiler. However, we can't just include any path outside of the 'src/'
// directory and so we add each file as a "module".
exe.addAnonymousModule("linux.dtb", .{ .source_file = dtb });
exe.addAnonymousModule("linux", .{ .source_file = .{ .path = "images/linux" } });
exe.addAnonymousModule("rootfs.cpio.gz", .{ .source_file = .{ .path = "images/rootfs.cpio.gz" } });
exe.root_module.addAnonymousImport("dtb", .{ .root_source_file = dtb });
exe.root_module.addAnonymousImport("linux", .{ .root_source_file = .{ .path = "images/linux" } });
exe.root_module.addAnonymousImport("rootfs", .{ .root_source_file = .{ .path = "images/rootfs.cpio.gz" } });

// Add microkit.h to be used by the API wrapper.
exe.addIncludePath(.{ .path = sdk_board_include_dir });
Expand Down Expand Up @@ -161,7 +159,7 @@ pub fn build(b: *std.Build) void {
microkit_step.dependOn(&microkit_tool_cmd.step);
b.default_step = microkit_step;

// This is setting up a `qemu` command for running the system via QEMU.
// This is setting up a `qemu` command for running the system using QEMU.
const loader_arg = fmtPrint("loader,file={s},addr=0x70000000,cpu-num=0", .{ final_image_dest });
const qemu_cmd = b.addSystemCommand(&[_][]const u8{
"qemu-system-aarch64",
Expand Down
2 changes: 1 addition & 1 deletion examples/zig/src/libmicrokit.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "libmicrokit.h";
#include "libmicrokit.h"

void zig_arm_sys_send(seL4_Word sys, seL4_Word dest, seL4_Word info_arg, seL4_Word mr0, seL4_Word mr1,
seL4_Word mr2, seL4_Word mr3)
Expand Down
4 changes: 2 additions & 2 deletions examples/zig/src/vmm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ const guest_kernel_image = blk: {
break :blk &arr;
};
// Data for the device tree to be passed to the kernel.
const guest_dtb_image = @embedFile("linux.dtb");
const guest_dtb_image = @embedFile("dtb");
// Data for the initial RAM disk to be passed to the kernel.
const guest_initrd_image = @embedFile("rootfs.cpio.gz");
const guest_initrd_image = @embedFile("rootfs");

// In Zig the standard library comes with printf-like functionality with the
// ability to provide your own function to ouput the characters. This is
Expand Down

0 comments on commit 0aaef4d

Please sign in to comment.