Skip to content

Commit

Permalink
catch up to lastest zig release
Browse files Browse the repository at this point in the history
  • Loading branch information
mattnite committed Dec 28, 2024
1 parent 6f1b3e9 commit 551b5cb
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 63 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- uses: actions/checkout@v2
- uses: goto-bus-stop/[email protected]
- name: Checkout
uses: actions/checkout@v4
- name: Setup Zig
uses: mlugg/setup-zig@v1
with:
version: 0.11.0

version: 0.13.0
- name: Build
run: zig build install

- name: Test Suite
run: zig build test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
zig-*
zig-out
.zig-cache
105 changes: 59 additions & 46 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
const std = @import("std");
const Build = std.Build;
const LazyPath = Build.LazyPath;
const ResolvedTarget = Build.ResolvedTarget;

const TestSuiteConfig = @import("src/testconfig.zig").TestSuiteConfig;

const samples = [_][]const u8{
"math",
};

const avr_target = std.zig.CrossTarget{
const avr_target_query = std.zig.CrossTarget{
.cpu_arch = .avr,
.cpu_model = .{ .explicit = &std.Target.avr.cpu.atmega328p },
.os_tag = .freestanding,
Expand All @@ -15,7 +19,7 @@ const avr_target = std.zig.CrossTarget{
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) !void {
pub fn build(b: *Build) !void {
// Targets
const test_step = b.step("test", "Run test suite");
const run_step = b.step("run", "Run the app");
Expand All @@ -36,17 +40,17 @@ pub fn build(b: *std.Build) !void {
// Modules

const isa_module = b.createModule(.{
.source_file = .{ .path = "src/shared/isa.zig" },
.root_source_file = b.path("src/shared/isa.zig"),
});
const isa_tables_module = b.createModule(.{
.source_file = generateIsaTables(b, isa_module),
.dependencies = &.{
.root_source_file = generateIsaTables(b, isa_module),
.imports = &.{
.{ .name = "isa", .module = isa_module },
},
});
const aviron_module = b.addModule("aviron", .{
.source_file = .{ .path = "src/lib/aviron.zig" },
.dependencies = &.{
.root_source_file = b.path("src/lib/aviron.zig"),
.imports = &.{
.{ .name = "autogen-tables", .module = isa_tables_module },
.{ .name = "isa", .module = isa_module },
},
Expand All @@ -55,12 +59,12 @@ pub fn build(b: *std.Build) !void {
// Main emulator executable
const aviron_exe = b.addExecutable(.{
.name = "aviron",
.root_source_file = .{ .path = "src/main.zig" },
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
aviron_exe.addModule("args", args_module);
aviron_exe.addModule("aviron", aviron_module);
aviron_exe.root_module.addImport("args", args_module);
aviron_exe.root_module.addImport("aviron", aviron_module);
b.installArtifact(aviron_exe);

const run_cmd = b.addRunArtifact(aviron_exe);
Expand All @@ -70,17 +74,19 @@ pub fn build(b: *std.Build) !void {
}
run_step.dependOn(&run_cmd.step);

const avr_target = b.resolveTargetQuery(avr_target_query);

// Samples
for (samples) |sample_name| {
const sample = b.addExecutable(.{
.name = sample_name,
.root_source_file = .{ .path = b.fmt("samples/{s}.zig", .{sample_name}) },
.root_source_file = b.path(b.fmt("samples/{s}.zig", .{sample_name})),
.target = avr_target,
.optimize = .ReleaseSmall,
.strip = false,
});
sample.bundle_compiler_rt = false;
sample.setLinkerScriptPath(std.build.FileSource{ .path = "linker.ld" });
sample.strip = false;
sample.setLinkerScriptPath(b.path("linker.ld"));

// install to the prefix:
const install_elf_sample = b.addInstallFile(sample.getEmittedBin(), b.fmt("samples/{s}.elf", .{sample_name}));
Expand All @@ -95,34 +101,36 @@ pub fn build(b: *std.Build) !void {
}

fn addTestSuite(
b: *std.Build,
test_step: *std.Build.Step,
debug_step: *std.Build.Step,
target: std.zig.CrossTarget,
b: *Build,
test_step: *Build.Step,
debug_step: *Build.Step,
target: ResolvedTarget,
optimize: std.builtin.OptimizeMode,
args_module: *std.build.Module,
aviron_module: *std.build.Module,
args_module: *Build.Module,
aviron_module: *Build.Module,
) !void {
const unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
test_step.dependOn(&b.addRunArtifact(unit_tests).step);

const testrunner_exe = b.addExecutable(.{
.name = "aviron-test-runner",
.root_source_file = .{ .path = "src/testrunner.zig" },
.root_source_file = b.path("src/testrunner.zig"),
.target = target,
.optimize = optimize,
});
testrunner_exe.addModule("args", args_module);
testrunner_exe.addModule("aviron", aviron_module);
testrunner_exe.root_module.addImport("args", args_module);
testrunner_exe.root_module.addImport("aviron", aviron_module);

debug_step.dependOn(&b.addInstallArtifact(testrunner_exe, .{}).step);

{
var walkdir = try b.build_root.handle.openIterableDir("testsuite", .{});
var walkdir = try b.build_root.handle.openDir("testsuite", .{
.iterate = true,
});
defer walkdir.close();

var walker = try walkdir.walk(b.allocator);
Expand Down Expand Up @@ -162,7 +170,7 @@ fn addTestSuite(
} else .unknown;

const ConfigAndExe = struct {
binary: std.Build.LazyPath,
binary: LazyPath,
config: TestSuiteConfig,
};

Expand All @@ -177,26 +185,26 @@ fn addTestSuite(
const config = try parseTestSuiteConfig(b, file);

const custom_target = if (config.cpu) |cpu|
std.zig.CrossTarget.parse(.{
b.resolveTargetQuery(std.zig.CrossTarget.parse(.{
.arch_os_abi = "avr-freestanding-eabi",
.cpu_features = cpu,
}) catch @panic(cpu)
}) catch @panic(cpu))
else
avr_target;
target;

const test_payload = b.addExecutable(.{
.name = std.fs.path.stem(entry.basename),
.root_source_file = .{ .path = b.fmt("testsuite/{s}", .{entry.path}) },
.root_source_file = b.path(b.fmt("testsuite/{s}", .{entry.path})),
.target = custom_target,
.optimize = config.optimize,
.strip = false,
});
test_payload.bundle_compiler_rt = false;
test_payload.addIncludePath(.{ .path = "testsuite" });
test_payload.setLinkerScriptPath(std.build.FileSource{ .path = "linker.ld" });
test_payload.addAnonymousModule("testsuite", .{
.source_file = .{ .path = "src/libtestsuite/lib.zig" },
test_payload.addIncludePath(b.path("testsuite"));
test_payload.setLinkerScriptPath(b.path("linker.ld"));
test_payload.root_module.addAnonymousImport("testsuite", .{
.root_source_file = b.path("src/libtestsuite/lib.zig"),
});
test_payload.strip = false;

debug_step.dependOn(&b.addInstallFile(
test_payload.getEmittedBin(),
Expand All @@ -219,7 +227,7 @@ fn addTestSuite(
} else |_| @panic(config_path);

break :blk ConfigAndExe{
.binary = .{ .path = b.fmt("testsuite/{s}", .{entry.path}) },
.binary = b.path(b.fmt("testsuite/{s}", .{entry.path})),
.config = config,
};
},
Expand All @@ -242,16 +250,20 @@ fn addTestSuite(
}
}

fn addTestSuiteUpdate(b: *std.Build, invoke_step: *std.Build.Step) !void {
const avr_gcc = if (b.findProgram(&.{"avr-gcc"}, &.{})) |path| std.build.LazyPath{
fn addTestSuiteUpdate(
b: *Build,
invoke_step: *Build.Step,
) !void {
const avr_gcc = if (b.findProgram(&.{"avr-gcc"}, &.{})) |path| LazyPath{
.cwd_relative = path,
} else |_| b.addExecutable(.{
.name = "no-avr-gcc",
.root_source_file = .{ .path = "tools/no-avr-gcc.zig" },
.target = b.host,
.root_source_file = b.path("tools/no-avr-gcc.zig"),
}).getEmittedBin();

{
var walkdir = try b.build_root.handle.openIterableDir("testsuite.avr-gcc", .{});
var walkdir = try b.build_root.handle.openDir("testsuite.avr-gcc", .{ .iterate = true });
defer walkdir.close();

var walker = try walkdir.walk(b.allocator);
Expand Down Expand Up @@ -295,11 +307,12 @@ fn addTestSuiteUpdate(b: *std.Build, invoke_step: *std.Build.Step) !void {

const config = try parseTestSuiteConfig(b, file);

const gcc_invocation = std.Build.Step.Run.create(b, "run avr-gcc");
const gcc_invocation = Build.Step.Run.create(b, "run avr-gcc");
gcc_invocation.addFileArg(avr_gcc);
gcc_invocation.addArg("-o");
gcc_invocation.addArg(b.fmt("testsuite/{s}/{s}.elf", .{ std.fs.path.dirname(entry.path).?, std.fs.path.stem(entry.basename) }));
gcc_invocation.addArg(b.fmt("-mmcu={s}", .{config.cpu orelse avr_target.cpu_model.explicit.llvm_name orelse @panic("Unknown MCU!")}));
gcc_invocation.addArg(b.fmt("-mmcu={s}", .{config.cpu orelse @panic("Uknown MCU!")}));
//avr_target.cpu_model.explicit.llvm_name orelse @panic("Unknown MCU!")}));
for (config.gcc_flags) |opt| {
gcc_invocation.addArg(opt);
}
Expand All @@ -321,7 +334,7 @@ fn addTestSuiteUpdate(b: *std.Build, invoke_step: *std.Build.Step) !void {
}
}

fn parseTestSuiteConfig(b: *std.Build, file: std.fs.File) !TestSuiteConfig {
fn parseTestSuiteConfig(b: *Build, file: std.fs.File) !TestSuiteConfig {
var code = std.ArrayList(u8).init(b.allocator);
defer code.deinit();

Expand Down Expand Up @@ -355,14 +368,14 @@ fn parseTestSuiteConfig(b: *std.Build, file: std.fs.File) !TestSuiteConfig {
);
}

fn generateIsaTables(b: *std.Build, isa_mod: *std.Build.Module) std.Build.LazyPath {
fn generateIsaTables(b: *Build, isa_mod: *Build.Module) LazyPath {
const generate_tables_exe = b.addExecutable(.{
.name = "aviron-generate-tables",
.root_source_file = .{ .path = "tools/generate-tables.zig" },
.target = .{},
.root_source_file = b.path("tools/generate-tables.zig"),
.target = b.host,
.optimize = .Debug,
});
generate_tables_exe.addModule("isa", isa_mod);
generate_tables_exe.root_module.addImport("isa", isa_mod);

const run = b.addRunArtifact(generate_tables_exe);

Expand Down
17 changes: 15 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,21 @@
.version = "0.1.0",
.dependencies = .{
.args = .{
.url = "https://github.com/MasterQ32/zig-args/archive/bb2eced8ddf28114b3a1ff761c2d80b90b1a61e2.tar.gz",
.hash = "12208556082c280af264ca2a174fd04fc7a35f865bfe59e2c61f4a977bf7a65063e4",
.url = "git+https://github.com/MasterQ32/zig-args#c5f79a133c1eeab38298493c2f4a2b4bd9a9c791",
.hash = "1220904d2fdcd970dd0d216211d092eb3ef6da01117163cc9393ab845a1d66c029d9",
},
},
.paths = .{
"README.md",
"build.zig",
"build.zig.zon",
"doc",
"linker.ld",
"samples",
"shell.nix",
"src",
"testsuite",
"testsuite.avr-gcc",
"tools",
},
}
2 changes: 1 addition & 1 deletion samples/math.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub export fn _start() callconv(.C) noreturn {
var a: usize = 1 + 2;

for (0..10) |p| {
var k = p;
const k = p;
std.mem.doNotOptimizeAway(k);
a += k;
}
Expand Down
10 changes: 5 additions & 5 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ const IO = struct {
const io: *IO = @ptrCast(@alignCast(ctx.?));
const reg: Register = @enumFromInt(addr);
switch (reg) {
.exit => std.os.exit(value & mask),
.exit => std.process.exit(value & mask),
.stdio => std.io.getStdOut().writer().writeByte(value & mask) catch @panic("i/o failure"),
.stderr => std.io.getStdErr().writer().writeByte(value & mask) catch @panic("i/o failure"),

Expand Down Expand Up @@ -259,16 +259,16 @@ const IO = struct {
fn lobyte(val: *u16) *u8 {
const bits: *[2]u8 = @ptrCast(val);
return switch (comptime builtin.cpu.arch.endian()) {
.Big => return &bits[1],
.Little => return &bits[0],
.big => return &bits[1],
.little => return &bits[0],
};
}

fn hibyte(val: *u16) *u8 {
const bits: *[2]u8 = @ptrCast(val);
return switch (comptime builtin.cpu.arch.endian()) {
.Big => return &bits[0],
.Little => return &bits[1],
.big => return &bits[0],
.little => return &bits[1],
};
}

Expand Down
6 changes: 3 additions & 3 deletions tools/generate-tables.zig
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn main() !void {
'0' => {},
'1' => base_number_bit_set.set(index),
else => {
var gop = try positionals.getPtr(opcode).getOrPut(allocator, r);
const gop = try positionals.getPtr(opcode).getOrPut(allocator, r);
if (!gop.found_existing) {
gop.value_ptr.* = try std.BoundedArray(u8, 16).init(0);
}
Expand Down Expand Up @@ -144,7 +144,7 @@ pub fn main() !void {
try writer.writeAll("pub const lookup = [65536]isa.Opcode {");

for (lut, 0..) |v, i| {
try writer.print(".{s},", .{std.zig.fmtId(@tagName(v))});
try writer.print(".{},", .{std.zig.fmtId(@tagName(v))});
if ((i + 1) % 16 == 0) {
try writer.print("\n", .{});
}
Expand Down Expand Up @@ -180,7 +180,7 @@ pub fn main() !void {

try writer.writeAll("};");

var txt = try buf.toOwnedSliceSentinel(0);
const txt = try buf.toOwnedSliceSentinel(0);
defer allocator.free(txt);

var tree = try std.zig.Ast.parse(allocator, txt, .zig);
Expand Down

0 comments on commit 551b5cb

Please sign in to comment.