Skip to content

Commit abb6304

Browse files
authored
Merge pull request #42 from firefly-zero/zig
Zig
2 parents 31c6efe + b665e88 commit abb6304

File tree

6 files changed

+102
-3
lines changed

6 files changed

+102
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/target/
22
/.task/
33
/test_app/target/
4+
.zig-cache/

assets/build.zig

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
const target_query = std.zig.CrossTarget{
5+
.cpu_arch = std.Target.Cpu.Arch.wasm32,
6+
.os_tag = std.Target.Os.Tag.freestanding,
7+
};
8+
const target = b.resolveTargetQuery(target_query);
9+
const optimize = std.builtin.OptimizeMode.ReleaseSmall;
10+
const exe = b.addExecutable(.{
11+
.name = "main",
12+
.root_source_file = b.path("src/main.zig"),
13+
.target = target,
14+
.optimize = optimize,
15+
.strip = true,
16+
});
17+
exe.entry = .disabled;
18+
exe.rdynamic = true;
19+
const firefly_package = b.dependency("firefly", .{});
20+
const firefly_module = firefly_package.module("firefly");
21+
exe.root_module.addImport("firefly", firefly_module);
22+
b.installArtifact(exe);
23+
}

assets/build.zig.zon

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.{
2+
.name = "main",
3+
.version = "0.0.0",
4+
.dependencies = .{
5+
.firefly = .{
6+
.url = "https://github.com/firefly-zero/firefly-zig/archive/refs/tags/0.1.0.tar.gz",
7+
.hash = "12208d6832ba8b3a4c2a4569fd1404233d2e9fe1bcdf3a4a82f8cf2a87d40264b53b",
8+
},
9+
},
10+
.paths = .{"src"},
11+
}

assets/main.zig

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const ff = @import("firefly");
2+
3+
pub export fn render() void {
4+
ff.drawTriangle(
5+
ff.Point{ .x = 60, .y = 10 },
6+
ff.Point{ .x = 40, .y = 40 },
7+
ff.Point{ .x = 80, .y = 40 },
8+
ff.Style{
9+
.fill_color = ff.Color.light_gray,
10+
.stroke_color = ff.Color.dark_blue,
11+
.stroke_width = 1,
12+
},
13+
);
14+
}

src/commands/new.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub fn cmd_new(args: &NewArgs) -> Result<()> {
2424
match lang {
2525
Lang::Go => new_go(&args.name).context("new Go project")?,
2626
Lang::Rust => new_rust(&args.name).context("new Rust project")?,
27-
Lang::Zig => todo!("Zig is not supported yet"),
27+
Lang::Zig => new_zig(&args.name).context("new Zig project")?,
2828
Lang::TS => todo!("TypeScript is not supported yet"),
2929
Lang::C => new_c(&args.name).context("new C project")?,
3030
Lang::Cpp => new_cpp(&args.name).context("new C++ project")?,
@@ -99,6 +99,16 @@ fn new_rust(name: &str) -> Result<()> {
9999
Ok(())
100100
}
101101

102+
/// Create a new Zig project.
103+
fn new_zig(name: &str) -> Result<()> {
104+
let mut c = Commander::default();
105+
c.cd(name)?;
106+
c.copy_asset(&["build.zig"], "build.zig")?;
107+
c.copy_asset(&["build.zig.zon"], "build.zig.zon")?;
108+
c.copy_asset(&["src", "main.zig"], "main.zig")?;
109+
Ok(())
110+
}
111+
102112
/// Create a new Go project.
103113
fn new_go(name: &str) -> Result<()> {
104114
check_installed("Go", "tinygo", "version")?;

src/langs.rs

+42-2
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,49 @@ fn find_wasi_sdk() -> anyhow::Result<PathBuf> {
299299
Ok(path)
300300
}
301301

302-
fn build_zig(_config: &Config) -> anyhow::Result<()> {
302+
fn build_zig(config: &Config) -> anyhow::Result<()> {
303303
check_installed("Zig", "zig", "version")?;
304-
todo!("Zig is not supported yet")
304+
let mut cmd_args = vec!["build"];
305+
if let Some(additional_args) = &config.compile_args {
306+
for arg in additional_args {
307+
cmd_args.push(arg.as_str());
308+
}
309+
}
310+
let output = Command::new("zig")
311+
.args(cmd_args)
312+
.current_dir(&config.root_path)
313+
.output()
314+
.context("run zig build")?;
315+
check_output(&output)?;
316+
317+
let from_dir = config.root_path.join("zig-out").join("bin");
318+
let from_path = find_wasm(&from_dir)?;
319+
let out_path = config.rom_path.join(BIN);
320+
std::fs::copy(&from_path, out_path).context("copy wasm binary")?;
321+
std::fs::remove_file(from_path).context("remove wasm file")?;
322+
Ok(())
323+
}
324+
325+
/// Find a wasm binary in the given directory.
326+
fn find_wasm(from_dir: &Path) -> anyhow::Result<PathBuf> {
327+
let from_dir = std::fs::read_dir(from_dir)?;
328+
let mut result = None;
329+
for file_path in from_dir {
330+
let file_path = file_path?;
331+
let file_path = file_path.path();
332+
if let Some(ext) = file_path.extension() {
333+
if ext == "wasm" {
334+
if result.is_some() {
335+
bail!("found more than one wasm binary");
336+
}
337+
result = Some(file_path);
338+
}
339+
}
340+
}
341+
match result {
342+
Some(result) => Ok(result),
343+
None => bail!("cannot find wasm binary"),
344+
}
305345
}
306346

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

0 commit comments

Comments
 (0)