Skip to content

Zig #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 13, 2025
Merged

Zig #42

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target/
/.task/
/test_app/target/
.zig-cache/
23 changes: 23 additions & 0 deletions assets/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const target_query = std.zig.CrossTarget{
.cpu_arch = std.Target.Cpu.Arch.wasm32,
.os_tag = std.Target.Os.Tag.freestanding,
};
const target = b.resolveTargetQuery(target_query);
const optimize = std.builtin.OptimizeMode.ReleaseSmall;
const exe = b.addExecutable(.{
.name = "main",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.strip = true,
});
exe.entry = .disabled;
exe.rdynamic = true;
const firefly_package = b.dependency("firefly", .{});
const firefly_module = firefly_package.module("firefly");
exe.root_module.addImport("firefly", firefly_module);
b.installArtifact(exe);
}
11 changes: 11 additions & 0 deletions assets/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.{
.name = "main",
.version = "0.0.0",
.dependencies = .{
.firefly = .{
.url = "https://github.com/firefly-zero/firefly-zig/archive/refs/tags/0.1.0.tar.gz",
.hash = "12208d6832ba8b3a4c2a4569fd1404233d2e9fe1bcdf3a4a82f8cf2a87d40264b53b",
},
},
.paths = .{"src"},
}
14 changes: 14 additions & 0 deletions assets/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const ff = @import("firefly");

pub export fn render() void {
ff.drawTriangle(
ff.Point{ .x = 60, .y = 10 },
ff.Point{ .x = 40, .y = 40 },
ff.Point{ .x = 80, .y = 40 },
ff.Style{
.fill_color = ff.Color.light_gray,
.stroke_color = ff.Color.dark_blue,
.stroke_width = 1,
},
);
}
12 changes: 11 additions & 1 deletion src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn cmd_new(args: &NewArgs) -> Result<()> {
match lang {
Lang::Go => new_go(&args.name).context("new Go project")?,
Lang::Rust => new_rust(&args.name).context("new Rust project")?,
Lang::Zig => todo!("Zig is not supported yet"),
Lang::Zig => new_zig(&args.name).context("new Zig project")?,
Lang::TS => todo!("TypeScript is not supported yet"),
Lang::C => new_c(&args.name).context("new C project")?,
Lang::Cpp => new_cpp(&args.name).context("new C++ project")?,
Expand Down Expand Up @@ -99,6 +99,16 @@ fn new_rust(name: &str) -> Result<()> {
Ok(())
}

/// Create a new Zig project.
fn new_zig(name: &str) -> Result<()> {
let mut c = Commander::default();
c.cd(name)?;
c.copy_asset(&["build.zig"], "build.zig")?;
c.copy_asset(&["build.zig.zon"], "build.zig.zon")?;
c.copy_asset(&["src", "main.zig"], "main.zig")?;
Ok(())
}

/// Create a new Go project.
fn new_go(name: &str) -> Result<()> {
check_installed("Go", "tinygo", "version")?;
Expand Down
44 changes: 42 additions & 2 deletions src/langs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,49 @@ fn find_wasi_sdk() -> anyhow::Result<PathBuf> {
Ok(path)
}

fn build_zig(_config: &Config) -> anyhow::Result<()> {
fn build_zig(config: &Config) -> anyhow::Result<()> {
check_installed("Zig", "zig", "version")?;
todo!("Zig is not supported yet")
let mut cmd_args = vec!["build"];
if let Some(additional_args) = &config.compile_args {
for arg in additional_args {
cmd_args.push(arg.as_str());
}
}
let output = Command::new("zig")
.args(cmd_args)
.current_dir(&config.root_path)
.output()
.context("run zig build")?;
check_output(&output)?;

let from_dir = config.root_path.join("zig-out").join("bin");
let from_path = find_wasm(&from_dir)?;
let out_path = config.rom_path.join(BIN);
std::fs::copy(&from_path, out_path).context("copy wasm binary")?;
std::fs::remove_file(from_path).context("remove wasm file")?;
Ok(())
}

/// Find a wasm binary in the given directory.
fn find_wasm(from_dir: &Path) -> anyhow::Result<PathBuf> {
let from_dir = std::fs::read_dir(from_dir)?;
let mut result = None;
for file_path in from_dir {
let file_path = file_path?;
let file_path = file_path.path();
if let Some(ext) = file_path.extension() {
if ext == "wasm" {
if result.is_some() {
bail!("found more than one wasm binary");
}
result = Some(file_path);
}
}
}
match result {
Some(result) => Ok(result),
None => bail!("cannot find wasm binary"),
}
}

fn build_ts(_config: &Config) -> anyhow::Result<()> {
Expand Down