Skip to content
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

Catch up to lastest zig release #14

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
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
133 changes: 85 additions & 48 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 @@ -89,40 +95,43 @@ pub fn build(b: *std.Build) !void {

// Test suite:

try addTestSuite(b, test_step, debug_testsuite_step, target, optimize, args_module, aviron_module);
try addTestSuite(b, test_step, debug_testsuite_step, target, avr_target, optimize, args_module, aviron_module);

try addTestSuiteUpdate(b, update_testsuite_step);
}

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,
host_target: ResolvedTarget,
avr_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" },
.target = target,
.root_source_file = b.path("src/main.zig"),
.target = host_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" },
.target = target,
.root_source_file = b.path("src/testrunner.zig"),
.target = host_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 All @@ -132,6 +141,11 @@ fn addTestSuite(
if (entry.kind != .file)
continue;

if (std.mem.eql(u8, entry.path, "dummy.zig")) {
// This file is not interesting to test.
continue;
}

const FileAction = union(enum) {
compile,
load,
Expand Down Expand Up @@ -162,7 +176,7 @@ fn addTestSuite(
} else .unknown;

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

Expand All @@ -177,26 +191,44 @@ 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;

const is_zig_test = std.mem.eql(u8, std.fs.path.extension(entry.path), ".zig");

const source_file = b.path(b.fmt("testsuite/{s}", .{entry.path}));
const root_file = if (is_zig_test)
source_file
else
b.path("testsuite/dummy.zig");

const test_payload = b.addExecutable(.{
.name = std.fs.path.stem(entry.basename),
.root_source_file = .{ .path = b.fmt("testsuite/{s}", .{entry.path}) },
.target = custom_target,
.optimize = config.optimize,
.strip = false,
.root_source_file = root_file,
.link_libc = false,
});
test_payload.want_lto = false; // AVR has no LTO support!
test_payload.verbose_link = true;
test_payload.verbose_cc = true;
if (!is_zig_test) {
test_payload.addCSourceFile(.{
.file = source_file,
.flags = &.{},
});
}
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 +251,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 +274,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 +331,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 +358,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 +392,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
Loading
Loading