add build script so zig build works (sort of) as expected

This commit is contained in:
Emil Lerch 2024-07-08 21:53:02 -07:00
parent e7be641b16
commit b64d535824
Signed by: lobo
GPG Key ID: A7B62D657EF764F8
7 changed files with 207 additions and 12 deletions

View File

@ -1,8 +1,8 @@
#
# Automatically generated file; DO NOT EDIT.
# Unikraft/0.17.0~8838cfd Configuration
# Unikraft/0.17.0~e7be641 Configuration
#
CONFIG_UK_FULLVERSION="0.17.0~8838cfd"
CONFIG_UK_FULLVERSION="0.17.0~e7be641"
CONFIG_UK_CODENAME="Calypso"
CONFIG_UK_ARCH="x86_64"
CONFIG_HOST_ARCH="x86_64"

View File

@ -2,4 +2,5 @@ $(eval $(call addlib,apphelloworld))
APPHELLOWORLD_SRCS-y += $(APPHELLOWORLD_BASE)/helloworld.c
APPHELLOWORLD_SRCS-y += $(APPHELLOWORLD_BASE)/undefined.c
UK_ALIBS-y += $(APPHELLOWORLD_BASE)/libziggy.a
UK_ALIBS-y += ${LIB_ZIGGY}
#UK_ALIBS-y += $(APPHELLOWORLD_BASE)/ziggy/zig-out/lib/libziggy.a

View File

@ -1,14 +1,29 @@
Native Unikraft Microkernel Build for Zig libraries
===================================================
This is an example repository, due to be cleaned up. For now, install Kraftkit
and zig and build it this way:
Building
--------
Everything assumes Linux on x86_64, though some trivial changes should allow
aarch64. Install the following:
* [Zig](https://ziglang.org). Versions 0.12.0 and 0.13.0 should work
* [QEMU](https://www.qemu.org/download/#linux)
* [Kraftkit](https://unikraft.org/docs/cli/install)
Then run `zig build run` and everything will compile and run. The zig source
code is all in the `ziggy` directory
Notes
-----
The build script basically runs these commands:
```sh
(cd ziggy && zig build -Doptimize=ReleaseSafe -Dtarget=x86_64-linux-gnu.2.13 -Dcpu=baseline) && cp ziggy/zig-out/lib/libziggy.a . && kraft build --plat qemu --arch x86_64 --log-level debug --log-type basic && kraft run --plat qemu --arch x86_64
(cd ziggy && zig build)
LIBZIGGY=$(pwd)/ziggy/zig-out/lib/libziggy.a kraft build --plat qemu --arch x86_64 --log-level debug --log-type basic
kraft run --plat qemu --arch x86_64
```
Only works on Linux, with QEMU (install that too!) and Zig 0.13.0 (though probably
works on 0.12.0 as well).
More to follow

137
build.zig Normal file
View File

@ -0,0 +1,137 @@
const std = @import("std");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
const ziggy = b.dependency("ziggy", .{
.target = target,
.optimize = optimize,
});
const install_step = b.getInstallStep();
const build_cmd = b.addSystemCommand(&[_][]const u8{
"kraft",
"build",
"--plat",
"qemu",
"--arch",
"x86_64",
"--log-level",
"debug",
// "--log-type",
// "basic",
});
install_step.dependOn(&build_cmd.step);
const artifact_env_var = LazyPathEnvironmentVariable.create(
b,
"LIB_ZIGGY",
ziggy.artifact("libziggy").getEmittedBin(),
build_cmd,
);
artifact_env_var.step.dependOn(&ziggy.artifact("libziggy").step);
build_cmd.step.dependOn(&artifact_env_var.step);
// This *creates* a Run step in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
const run_cmd = b.addSystemCommand(&[_][]const u8{
"kraft",
"run",
"--plat",
"qemu",
"--arch",
"x86_64",
});
// By making the run step depend on the install step, it will be run from the
// installation directory rather than directly from within the cache directory.
// This is not necessary, however, if the application depends on other installed
// files, this ensures they will be present and in the expected location.
run_cmd.step.dependOn(install_step);
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| {
run_cmd.addArgs(args);
}
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const kraft_clean_cmd = b.addSystemCommand(&[_][]const u8{
"kraft",
"clean",
"--plat",
"qemu",
"--arch",
"x86_64",
});
const clean_step = b.step("clean", "Clean the unikraft build");
clean_step.dependOn(&kraft_clean_cmd.step);
const distclean_cmd = b.addSystemCommand(&[_][]const u8{
"rm",
"-rf",
".unikraft",
});
const distclean_step = b.step("distclean", "Deep clean the unikraft build");
distclean_step.dependOn(clean_step);
distclean_step.dependOn(&distclean_cmd.step);
}
const LazyPathEnvironmentVariable = struct {
const base_id: std.Build.Step.Id = .custom;
step: std.Build.Step,
lazy_path: std.Build.LazyPath,
key: []const u8,
run_step: *std.Build.Step.Run,
pub fn create(
owner: *std.Build,
comptime key: []const u8,
lazy_path: std.Build.LazyPath,
run_step: *std.Build.Step.Run,
) *LazyPathEnvironmentVariable {
const step = owner.allocator.create(LazyPathEnvironmentVariable) catch @panic("OOM");
step.* = .{
.step = std.Build.Step.init(.{
.id = base_id,
.name = "env var " ++ key,
.owner = owner,
.makeFn = make,
}),
.key = key,
.lazy_path = lazy_path,
.run_step = run_step,
};
return step;
}
fn make(step: *std.Build.Step, prog_node: std.Progress.Node) error{ MakeFailed, MakeSkipped }!void {
_ = prog_node;
const b = step.owner;
// const arena = b.allocator;
const this: *LazyPathEnvironmentVariable = @fieldParentPtr("step", step);
this.run_step.setEnvironmentVariable(this.key, this.lazy_path.getPath2(b, &this.step));
}
};

38
build.zig.zon Normal file
View File

@ -0,0 +1,38 @@
.{
// This is the default name used by packages depending on this one. For
// example, when a user runs `zig fetch --save <url>`, this field is used
// as the key in the `dependencies` table. Although the user can choose a
// different name, most users will stick with this provided value.
//
// It is redundant to include "zig" in this name because it is already
// within the Zig package namespace.
.name = "unikraft-zig-native-hello",
// This is a [Semantic Version](https://semver.org/).
// In a future version of Zig it will be used for package deduplication.
.version = "0.0.0",
// This field is optional.
// This is currently advisory only; Zig does not yet do anything
// with this value.
//.minimum_zig_version = "0.11.0",
// This field is optional.
// Each dependency must either provide a `url` and `hash`, or a `path`.
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
// Once all dependencies are fetched, `zig build` no longer requires
// internet connectivity.
.dependencies = .{
.ziggy = .{
.path = "ziggy",
},
},
.paths = .{
"build.zig",
"build.zig.zon",
"ziggy",
// For example...
//"LICENSE",
//"README.md",
},
}

View File

@ -16,7 +16,7 @@ pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});
const lib = b.addStaticLibrary(.{
.name = "ziggy",
.name = "libziggy",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = b.path("src/root.zig"),

View File

@ -9,6 +9,10 @@ export fn add(a: i32, b: i32) i32 {
const out = std.io.getStdErr().writer();
out.print("WARNING: Building debug mode will likely crash in Unikraft environment. Use -Doptimize=ReleaseSafe\n", .{}) catch {};
}
if (builtin.mode != .Debug) {
const out = std.io.getStdOut().writer();
out.print("info: Built with a release build\n", .{}) catch {};
}
const out = std.io.getStdOut().writer();
out.print("Hello from lib\n", .{}) catch {};
std.log.err("logging error", .{});