Hello, World and build.zig
This commit is contained in:
parent
c45514ce4f
commit
b8c7fc5674
4 changed files with 113 additions and 163 deletions
143
build.zig
143
build.zig
|
@ -4,113 +4,60 @@ const std = @import("std");
|
||||||
// declaratively construct a build graph that will be executed by an external
|
// declaratively construct a build graph that will be executed by an external
|
||||||
// runner.
|
// runner.
|
||||||
pub fn build(b: *std.Build) void {
|
pub fn build(b: *std.Build) void {
|
||||||
// Standard target options allows the person running `zig build` to choose
|
const initramfs = addNasmFiles(b, AddNasmFilesOptions{
|
||||||
// what target to build for. Here we do not override the defaults, which
|
.filename = "src/initramfs.asm",
|
||||||
// means any target is allowed, and the default is native. Other options
|
.outputname = "initramfs.bin",
|
||||||
// for restricting supported target set are available.
|
.addAsObjFile = false,
|
||||||
const target = b.standardTargetOptions(.{});
|
.flags = &.{"-f bin"},
|
||||||
|
|
||||||
// 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(.{});
|
|
||||||
|
|
||||||
// This creates a "module", which represents a collection of source files alongside
|
|
||||||
// some compilation options, such as optimization mode and linked system libraries.
|
|
||||||
// Every executable or library we compile will be based on one or more modules.
|
|
||||||
const lib_mod = b.createModule(.{
|
|
||||||
// `root_source_file` is the Zig "entry point" of the module. If a module
|
|
||||||
// only contains e.g. external object files, you can make this `null`.
|
|
||||||
// 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"),
|
|
||||||
.target = target,
|
|
||||||
.optimize = optimize,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// We will also create a module for our other entry point, 'main.zig'.
|
const initramfs_install = b.addInstallBinFile(initramfs.obj, "initramfs.img");
|
||||||
const exe_mod = b.createModule(.{
|
initramfs_install.step.dependOn(&truncate(b, initramfs).step);
|
||||||
// `root_source_file` is the Zig "entry point" of the module. If a module
|
|
||||||
// only contains e.g. external object files, you can make this `null`.
|
|
||||||
// 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/main.zig"),
|
|
||||||
.target = target,
|
|
||||||
.optimize = optimize,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Modules can depend on one another using the `std.Build.Module.addImport` function.
|
b.getInstallStep().dependOn(&initramfs_install.step);
|
||||||
// This is what allows Zig source code to use `@import("foo")` where 'foo' is not a
|
|
||||||
// file path. In this case, we set up `exe_mod` to import `lib_mod`.
|
|
||||||
exe_mod.addImport("zos_lib", lib_mod);
|
|
||||||
|
|
||||||
// Now, we will create a static library based on the module we created above.
|
const run = b.addSystemCommand(&.{ "qemu-system-i386", "-fda" });
|
||||||
// This creates a `std.Build.Step.Compile`, which is the build step responsible
|
run.addFileArg(b.path("zig-out/bin/initramfs.img"));
|
||||||
// for actually invoking the compiler.
|
|
||||||
const lib = b.addLibrary(.{
|
|
||||||
.linkage = .static,
|
|
||||||
.name = "zos",
|
|
||||||
.root_module = lib_mod,
|
|
||||||
});
|
|
||||||
|
|
||||||
// This declares intent for the library to be installed into the standard
|
const run_step = b.step("run", "run the os in qemu");
|
||||||
// location when the user invokes the "install" step (the default step when
|
run_step.dependOn(&run.step);
|
||||||
// running `zig build`).
|
|
||||||
b.installArtifact(lib);
|
|
||||||
|
|
||||||
// This creates another `std.Build.Step.Compile`, but this one builds an executable
|
|
||||||
// rather than a static library.
|
|
||||||
const exe = b.addExecutable(.{
|
|
||||||
.name = "zos",
|
|
||||||
.root_module = exe_mod,
|
|
||||||
});
|
|
||||||
|
|
||||||
// This declares intent for the executable to be installed into the
|
|
||||||
// standard location when the user invokes the "install" step (the default
|
|
||||||
// step when running `zig build`).
|
|
||||||
b.installArtifact(exe);
|
|
||||||
|
|
||||||
// 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.addRunArtifact(exe);
|
|
||||||
|
|
||||||
// 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(b.getInstallStep());
|
|
||||||
|
|
||||||
// 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,
|
const AddNasmFilesOptions = struct {
|
||||||
// and can be selected like this: `zig build run`
|
filename: []const u8,
|
||||||
// This will evaluate the `run` step rather than the default, which is "install".
|
outputname: ?[]const u8 = null,
|
||||||
const run_step = b.step("run", "Run the app");
|
addAsObjFile: bool = true,
|
||||||
run_step.dependOn(&run_cmd.step);
|
flags: []const []const u8 = &.{},
|
||||||
|
};
|
||||||
|
|
||||||
// Creates a step for unit testing. This only builds the test executable
|
const NasmFile = struct {
|
||||||
// but does not run it.
|
run: *std.Build.Step.Run,
|
||||||
const lib_unit_tests = b.addTest(.{
|
obj: std.Build.LazyPath,
|
||||||
.root_module = lib_mod,
|
};
|
||||||
});
|
|
||||||
|
|
||||||
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
|
// adapted from https://codeberg.org/raddari/zig-nasm-lib.git
|
||||||
|
fn addNasmFiles(b: *std.Build, options: AddNasmFilesOptions) NasmFile {
|
||||||
|
std.debug.assert(!std.fs.path.isAbsolute(options.filename));
|
||||||
|
const src_file = b.path(options.filename);
|
||||||
|
const output = options.outputname orelse b.fmt("{s}.o", .{std.mem.sliceTo(options.filename, '.')});
|
||||||
|
|
||||||
const exe_unit_tests = b.addTest(.{
|
const nasm = b.addSystemCommand(&.{"nasm"});
|
||||||
.root_module = exe_mod,
|
nasm.addArgs(options.flags);
|
||||||
});
|
nasm.addPrefixedDirectoryArg("-i", b.path("src"));
|
||||||
|
const obj = nasm.addPrefixedOutputFileArg("-o", output);
|
||||||
|
nasm.addFileArg(src_file);
|
||||||
|
|
||||||
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
|
return .{
|
||||||
|
.run = nasm,
|
||||||
// Similar to creating the run step earlier, this exposes a `test` step to
|
.obj = obj,
|
||||||
// the `zig build --help` menu, providing a way for the user to request
|
};
|
||||||
// running the unit tests.
|
}
|
||||||
const test_step = b.step("test", "Run unit tests");
|
|
||||||
test_step.dependOn(&run_lib_unit_tests.step);
|
fn truncate(b: *std.Build, bin: NasmFile) *std.Build.Step.Run {
|
||||||
test_step.dependOn(&run_exe_unit_tests.step);
|
const exec = b.addSystemCommand(&.{"truncate"});
|
||||||
|
exec.addArgs(&.{ "-s", "1440k" });
|
||||||
|
exec.addFileArg(bin.obj);
|
||||||
|
exec.step.dependOn(&bin.run.step);
|
||||||
|
|
||||||
|
return exec;
|
||||||
}
|
}
|
||||||
|
|
62
src/initramfs.asm
Normal file
62
src/initramfs.asm
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
; vim: ft=nasm
|
||||||
|
org 0x7C00
|
||||||
|
bits 16
|
||||||
|
|
||||||
|
%define ENDL 0x0D, 0x0A
|
||||||
|
|
||||||
|
start:
|
||||||
|
jmp main
|
||||||
|
|
||||||
|
;
|
||||||
|
; echo:
|
||||||
|
; print something to the screen
|
||||||
|
; - ds:si points to string
|
||||||
|
;
|
||||||
|
echo:
|
||||||
|
; save the registers we want to modify
|
||||||
|
push si
|
||||||
|
push ax
|
||||||
|
push bx
|
||||||
|
|
||||||
|
mov ah, 0xe
|
||||||
|
mov bh, 0
|
||||||
|
|
||||||
|
.loop:
|
||||||
|
lodsb ; load byte from ds:si to al
|
||||||
|
or al, al ; check if next char is null
|
||||||
|
jz .done
|
||||||
|
|
||||||
|
int 0x10
|
||||||
|
jmp .loop
|
||||||
|
|
||||||
|
.done:
|
||||||
|
pop bx
|
||||||
|
pop ax
|
||||||
|
pop si
|
||||||
|
ret
|
||||||
|
|
||||||
|
main:
|
||||||
|
|
||||||
|
; setup data segments
|
||||||
|
; use ax as and intermediary as we can't write to es/ds directly
|
||||||
|
mov ax, 0
|
||||||
|
mov ds, ax
|
||||||
|
mov es, ax
|
||||||
|
|
||||||
|
; setup stack
|
||||||
|
mov ss, ax
|
||||||
|
mov sp, 0x7C00 ; stack grows downward from where we are loaded in memory
|
||||||
|
|
||||||
|
; print the hello world
|
||||||
|
mov si, msg_hello
|
||||||
|
call echo
|
||||||
|
|
||||||
|
hlt
|
||||||
|
|
||||||
|
.halt:
|
||||||
|
jmp .halt
|
||||||
|
|
||||||
|
msg_hello: db 'Hello, world!', ENDL, 0
|
||||||
|
|
||||||
|
times 510-($-$$) db 0
|
||||||
|
dw 0AA55h
|
46
src/main.zig
46
src/main.zig
|
@ -1,46 +0,0 @@
|
||||||
//! By convention, main.zig is where your main function lives in the case that
|
|
||||||
//! you are building an executable. If you are making a library, the convention
|
|
||||||
//! is to delete this file and start with root.zig instead.
|
|
||||||
|
|
||||||
pub fn main() !void {
|
|
||||||
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
|
|
||||||
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
|
|
||||||
|
|
||||||
// stdout is for the actual output of your application, for example if you
|
|
||||||
// are implementing gzip, then only the compressed bytes should be sent to
|
|
||||||
// stdout, not any debugging messages.
|
|
||||||
const stdout_file = std.io.getStdOut().writer();
|
|
||||||
var bw = std.io.bufferedWriter(stdout_file);
|
|
||||||
const stdout = bw.writer();
|
|
||||||
|
|
||||||
try stdout.print("Run `zig build test` to run the tests.\n", .{});
|
|
||||||
|
|
||||||
try bw.flush(); // Don't forget to flush!
|
|
||||||
}
|
|
||||||
|
|
||||||
test "simple test" {
|
|
||||||
var list = std.ArrayList(i32).init(std.testing.allocator);
|
|
||||||
defer list.deinit(); // Try commenting this out and see if zig detects the memory leak!
|
|
||||||
try list.append(42);
|
|
||||||
try std.testing.expectEqual(@as(i32, 42), list.pop());
|
|
||||||
}
|
|
||||||
|
|
||||||
test "use other module" {
|
|
||||||
try std.testing.expectEqual(@as(i32, 150), lib.add(100, 50));
|
|
||||||
}
|
|
||||||
|
|
||||||
test "fuzz example" {
|
|
||||||
const Context = struct {
|
|
||||||
fn testOne(context: @This(), input: []const u8) anyerror!void {
|
|
||||||
_ = context;
|
|
||||||
// Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case!
|
|
||||||
try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
try std.testing.fuzz(Context{}, Context.testOne, .{});
|
|
||||||
}
|
|
||||||
|
|
||||||
const std = @import("std");
|
|
||||||
|
|
||||||
/// This imports the separate module containing `root.zig`. Take a look in `build.zig` for details.
|
|
||||||
const lib = @import("zos_lib");
|
|
13
src/root.zig
13
src/root.zig
|
@ -1,13 +0,0 @@
|
||||||
//! By convention, root.zig is the root source file when making a library. If
|
|
||||||
//! you are making an executable, the convention is to delete this file and
|
|
||||||
//! start with main.zig instead.
|
|
||||||
const std = @import("std");
|
|
||||||
const testing = std.testing;
|
|
||||||
|
|
||||||
pub export fn add(a: i32, b: i32) i32 {
|
|
||||||
return a + b;
|
|
||||||
}
|
|
||||||
|
|
||||||
test "basic add functionality" {
|
|
||||||
try testing.expect(add(3, 7) == 10);
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue