beginning of haku2: a reimplementation of haku in Zig

the goal is to rewrite haku completely, starting with the VM---because it was the most obvious point of improvement
the reason is because Rust is kinda too verbose for low level stuff like this. compare the line numbers between haku1 and haku2's VM and how verbose those lines are, and it's kind of an insane difference
it also feels like Zig's compilation model can work better for small wasm binary sizes

and of course, I also just wanted an excuse to try out Zig :3
This commit is contained in:
りき萌 2025-06-01 23:13:34 +02:00
parent 598c0348f6
commit 01d4514a65
19 changed files with 1946 additions and 11 deletions

61
crates/haku2/build.rs Normal file
View file

@ -0,0 +1,61 @@
use std::{
env,
error::Error,
path::PathBuf,
process::{Command, Stdio},
};
fn main() -> Result<(), Box<dyn Error>> {
println!("cargo::rerun-if-changed=src");
let out_dir = env::var("OUT_DIR").unwrap();
let out_path = PathBuf::from(&out_dir);
let profile = env::var("PROFILE").unwrap();
let target = env::var("TARGET").unwrap();
// TODO: Use of PROFILE is discouraged, so we should switch this to a more fine-grained system
// reading back from other environment variables.
let optimize = match &profile[..] {
"debug" => "Debug",
"release" => "ReleaseSmall",
_ => panic!("unknown profile"),
};
let color = match env::var("NO_COLOR").is_ok() {
true => "off",
false => "on",
};
let target = match &target[..] {
"x86_64-unknown-linux-gnu" => "x86_64-linux-gnu",
_ => &target,
};
let output = Command::new("zig")
.arg("build")
.arg("install")
// Terminal output
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.arg("--prominent-compile-errors")
.arg("--color")
.arg(color)
// Build output
.arg("--cache-dir")
.arg(out_path.join("zig-cache"))
.arg("--prefix")
.arg(out_path.join("zig-out").join(target))
// Build settings
.arg(format!("-Doptimize={optimize}"))
.arg(format!("-Dtarget={target}"))
.output()?;
if !output.status.success() {
panic!("zig failed to build");
}
println!("cargo::rustc-link-search={out_dir}/zig-out/{target}/lib");
println!("cargo::rustc-link-lib=haku2");
Ok(())
}