Skip to content

Commit

Permalink
Refactor libvmm zig build to allow building virtio example
Browse files Browse the repository at this point in the history
  • Loading branch information
erichchan999 committed May 22, 2024
1 parent a13651f commit 8fdde2a
Showing 1 changed file with 79 additions and 24 deletions.
103 changes: 79 additions & 24 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
const std = @import("std");
const Build = std.Build;
const LazyPath = std.Build.LazyPath;

const MicrokitBoard = enum {
qemu_arm_virt,
odroidc4
};

const ConfigOptions = enum {
debug,
release,
benchmark
};

const src = [_][]const u8{
"src/guest.c",
Expand All @@ -21,44 +34,53 @@ const src_aarch64 = [_][]const u8{
"src/arch/aarch64/vcpu.c",
};

const uio_blk_driver_src = [_][]const u8{
"tools/linux/uio/blk.c",
"tools/linux/uio/libuio.c",
};

pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

// Getting the path to the Microkit SDK before doing anything else
const microkit_sdk_arg = b.option([]const u8, "sdk", "Path to Microkit SDK");
if (microkit_sdk_arg == null) {
// Microkit SDK option
const microkit_sdk_option_tmp = b.option([]const u8, "sdk", "Path to Microkit SDK");
if (microkit_sdk_option_tmp == null) {
std.log.err("Missing -Dsdk=/path/to/sdk argument being passed\n", .{});
std.posix.exit(1);
}
const microkit_sdk = microkit_sdk_arg.?;
std.fs.cwd().access(microkit_sdk, .{}) catch |err| {
const microkit_sdk_option = microkit_sdk_option_tmp.?;
std.fs.cwd().access(microkit_sdk_option, .{}) catch |err| {
switch (err) {
error.FileNotFound => std.log.err("Path to SDK '{s}' does not exist\n", .{ microkit_sdk }),
else => std.log.err("Could not acccess SDK path '{s}', error is {any}\n", .{ microkit_sdk, err })
error.FileNotFound => std.log.err("Path to SDK '{s}' does not exist\n", .{ microkit_sdk_option }),
else => std.log.err("Could not acccess SDK path '{s}', error is {any}\n", .{ microkit_sdk_option, err })
}
std.posix.exit(1);
};

const microkit_config = b.option([]const u8, "config", "Microkit config to build for") orelse "debug";
const microkit_board_arg = b.option([]const u8, "board", "Microkit board to target");
// Microkit config option
const microkit_config_option = b.option(ConfigOptions, "config", "Microkit config to build for") orelse ConfigOptions.debug;

if (microkit_board_arg == null) {
// Microkit board option
const microkit_board_option_tmp = b.option(MicrokitBoard, "board", "Microkit board to target");
if (microkit_board_option_tmp == null) {
std.log.err("Missing -Dboard=<BOARD> argument being passed\n", .{});
std.posix.exit(1);
}
const microkit_board = microkit_board_arg.?;

const microkit_board_dir = b.fmt("{s}/board/{s}/{s}", .{ microkit_sdk, microkit_board, microkit_config });
const microkit_board_option = microkit_board_option_tmp.?;
const microkit_board_dir = b.fmt("{s}/board/{s}/{s}", .{ microkit_sdk_option, @tagName(microkit_board_option), @tagName(microkit_config_option) });
std.fs.cwd().access(microkit_board_dir, .{}) catch |err| {
switch (err) {
error.FileNotFound => std.log.err("Path to '{s}' does not exist\n", .{ microkit_board_dir }),
else => std.log.err("Could not acccess path '{s}', error is {any}\n", .{ microkit_board_dir, err })
}
std.posix.exit(1);
};

// Libmicrokit
const libmicrokit_include = b.fmt("{s}/include", .{ microkit_board_dir });

// libvmm static ibrary
const libvmm = b.addStaticLibrary(.{
.name = "vmm",
.target = target,
Expand All @@ -80,25 +102,58 @@ pub fn build(b: *std.Build) void {
"-Wno-unused-function",
"-mstrict-align",
"-fno-sanitize=undefined", // @ivanv: ideally we wouldn't have to turn off UBSAN
b.fmt("-DBOARD_{s}", .{ microkit_board }) // @ivanv: shouldn't be needed as the library should not depend on the board
b.fmt("-DBOARD_{s}", .{ @tagName(microkit_board_option) }) // @ivanv: shouldn't be needed as the library should not depend on the board
}
});

// @ivanv: fix all of our libvmm includes! This is a mess!
libvmm.addIncludePath(b.path("src"));
libvmm.addIncludePath(b.path("src/util/"));
libvmm.addIncludePath(b.path("src/util"));
libvmm.addIncludePath(b.path("src/virtio"));
libvmm.addIncludePath(b.path("src/arch/aarch64"));
libvmm.addIncludePath(b.path("src/arch/aarch64/vgic/"));
libvmm.addIncludePath(.{ .path = libmicrokit_include });

libvmm.addIncludePath(.{ .cwd_relative = libmicrokit_include });
const sddf_dep = b.dependency("sddf", .{
.target = target,
.optimize = optimize,
.sdk = microkit_sdk,
.config = @as([]const u8, microkit_config),
.board = @as([]const u8, microkit_board),
.sdk = microkit_sdk_option,
.config = @as([]const u8, @tagName(microkit_config_option)),
.board = @as([]const u8, @tagName(microkit_board_option)),
});
libvmm.addIncludePath(sddf_dep.path("include"));

libvmm.installHeadersDirectory(b.path("src"), "", .{});
libvmm.installHeadersDirectory(b.path("src/util"), "", .{});
libvmm.installHeadersDirectory(b.path("src/virtio"), "", .{});
libvmm.installHeadersDirectory(b.path("src/arch/aarch64"), "", .{});
b.installArtifact(libvmm);

// UIO target
const uio_target_query = switch (target.result.cpu.arch) {
.aarch64 => .{ .cpu_arch = .aarch64, .os_tag = .linux, .abi = .musl, .cpu_model = .{ .explicit = target.result.cpu.model } },
else => {
std.log.err("Unsupported UIO driver architecture given '{s}'", .{ @tagName(target.result.cpu.arch) });
std.posix.exit(1);
}
};
const uio_target = b.resolveTargetQuery(uio_target_query);

// UIO block driver
const uio_blk_driver = b.addExecutable(.{
.name = "uio_blk_driver",
.target = uio_target,
.optimize = optimize,
});
uio_blk_driver.addIncludePath(b.path("include"));
uio_blk_driver.addIncludePath(b.path("tools/linux/include"));
uio_blk_driver.addIncludePath(sddf_dep.path("include"));
uio_blk_driver.linkLibC();
uio_blk_driver.addCSourceFiles(.{
.files = &(uio_blk_driver_src),
.flags = &.{
"-Wall",
"-Werror",
"-Wno-unused-function",
"-mstrict-align",
"-D_GNU_SOURCE", // Needed for opening files in O_DIRECT to do userspace DMA to block device
}
});
b.installArtifact(uio_blk_driver);
}

0 comments on commit 8fdde2a

Please sign in to comment.