Skip to content

Commit

Permalink
Use zig instead of make and add a CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratakor committed Sep 24, 2023
1 parent ba6df82 commit 15e6713
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 91 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: ci

on:
push:
pull_request:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup Zig
uses: goto-bus-stop/[email protected]
with:
version: master

- name: Build
run: zig build -Doptimize=ReleaseFast

lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup Zig
uses: goto-bus-stop/[email protected]
with:
version: master

- name: Lint
run: zig fmt --check kernel/*.zig
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
zig-cache/
zig-out/
limine/
ovmf/
*.iso
*.hdd
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "limine"]
path = limine
url = https://github.com/limine-bootloader/limine.git
branch = v5.x-branch-binary
66 changes: 0 additions & 66 deletions Makefile

This file was deleted.

30 changes: 10 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
# Système 9
x86_64 operating system in zig
an operating system made with zig

# TODO
- use flanterm
- use zig instead of make
- current font is under GPL
- add a CI
- support RISC-V64, aarch64 and x86_64
- replace limine with a custom bootloader
- add a checklist/roadmap

# dependencies
- all: make, zig
- iso: xorriso
- hdd: gptfdisk, mtools
- run: qemu
### clone and build

# makefile targets
git clone https://github.com/ratakor/os --recursive

Running `make all` will compile the kernel (from the `kernel/` directory) and
then generate a bootable ISO image.
### build
Make sure to have `xorriso` and run

Running `make all-hdd` will compile the kernel and then generate a raw image
suitable to be flashed onto a USB stick or hard drive/SSD.
zig build image -Doptimize=ReleaseFast

Running `make run` will build the kernel and a bootable ISO (equivalent to make
all) and then run it using `qemu` (if installed).
### run
Make sure to have `qemu` and run

Running `make run-hdd` will build the kernel and a raw HDD image (equivalent to
make all-hdd) and then run it using `qemu` (if installed).

The `run-uefi` and `run-hdd-uefi` targets are equivalent to their non `-uefi`
counterparts except that they boot `qemu` using a UEFI-compatible firmware.
zig build run
57 changes: 55 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const std = @import("std");

pub fn build(b: *std.Build) !void {
fn buildKernel(b: *std.Build) *std.Build.Step.Compile {
const optimize = b.standardOptimizeOption(.{});
var target: std.zig.CrossTarget = .{
.cpu_arch = .x86_64,
Expand Down Expand Up @@ -33,8 +33,61 @@ pub fn build(b: *std.Build) !void {
.Debug, .ReleaseSafe => false,
.ReleaseFast, .ReleaseSmall => true,
};

return kernel;
}

fn buildImage(b: *std.Build, image_name: []const u8) *std.Build.Step.Run {
const image_dir = b.cache_root.join(b.allocator, &.{"image_root/"}) catch unreachable;

const image_params = &[_][]const u8{
"/bin/sh", "-c",
std.mem.concat(b.allocator, u8, &.{
// "git clone https://github.com/limine-bootloader/limine.git --branch=v5.x-branch-binary --depth=1 ; ",
"make -C limine && ",
"mkdir -p ", image_dir, " && ",
"cp zig-out/bin/kernel.elf limine.cfg limine/limine-bios.sys ",
"limine/limine-bios-cd.bin limine/limine-uefi-cd.bin ",
image_dir, " && ",
"mkdir -p ", image_dir, "EFI/BOOT && ",
"cp limine/BOOTX64.EFI ", image_dir, "EFI/BOOT/ && ",
"cp limine/BOOTIA32.EFI ", image_dir, "EFI/BOOT/ && ",
"xorriso -as mkisofs -b limine-bios-cd.bin ",
"-no-emul-boot -boot-load-size 4 -boot-info-table ",
"--efi-boot limine-uefi-cd.bin ",
"-efi-boot-part --efi-boot-image --protective-msdos-label ",
image_dir, " -o ", image_name, " && ",
"./limine/limine bios-install ", image_name,
}) catch unreachable,
};

return b.addSystemCommand(image_params);
}

pub fn build(b: *std.Build) void {
const kernel = buildKernel(b);
b.installArtifact(kernel);

const image_name = std.mem.concat(b.allocator, u8, &.{
"système-9-", @tagName(kernel.target.cpu_arch.?), ".iso"
}) catch unreachable;
const image_step = b.step("image", "Build the image");
const image_cmd = buildImage(b, image_name);
image_cmd.step.dependOn(b.getInstallStep());
image_step.dependOn(&image_cmd.step);

const run_step = b.step("run", "Run the image with qemu");
const run_cmd = b.addSystemCommand(&.{
"qemu-system-x86_64", "-M", "q35", "-m", "2G", "-cdrom", image_name, "-boot", "d"
});
run_cmd.step.dependOn(image_step);
run_step.dependOn(&run_cmd.step);

const fmt_step = b.step("fmt", "Format all source files");
fmt_step.dependOn(&b.addFmt(.{ .paths = &.{ "build.zig", "kernel" } }).step);
fmt_step.dependOn(&b.addFmt(.{ .paths = &.{ "kernel" } }).step);

const clean_step = b.step("clean", "Delete all artifacts created by zig build");
clean_step.dependOn(&b.addRemoveDirTree("zig-cache").step);
clean_step.dependOn(&b.addRemoveDirTree("zig-out").step);
clean_step.dependOn(&b.addRemoveDirTree(image_name).step);
}

0 comments on commit 15e6713

Please sign in to comment.