From 551b5cb7c0db1434c44f09a9477b3163c70168cf Mon Sep 17 00:00:00 2001 From: Matt Knight Date: Fri, 27 Dec 2024 23:32:54 -0800 Subject: [PATCH] catch up to lastest zig release --- .github/workflows/build.yml | 10 ++-- .gitignore | 3 +- build.zig | 105 ++++++++++++++++++++---------------- build.zig.zon | 17 +++++- samples/math.zig | 2 +- src/main.zig | 10 ++-- tools/generate-tables.zig | 6 +-- 7 files changed, 90 insertions(+), 63 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d946f6a..49d668d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -9,13 +9,13 @@ jobs: matrix: os: [ubuntu-latest, windows-latest, macos-latest] steps: - - uses: actions/checkout@v2 - - uses: goto-bus-stop/setup-zig@v1.3.0 + - 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 diff --git a/.gitignore b/.gitignore index 4c80a22..880cd5d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -zig-* +zig-out +.zig-cache diff --git a/build.zig b/build.zig index a3bc908..6c370b6 100644 --- a/build.zig +++ b/build.zig @@ -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, @@ -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"); @@ -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 }, }, @@ -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); @@ -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})); @@ -95,16 +101,16 @@ 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, }); @@ -112,17 +118,19 @@ fn addTestSuite( 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); @@ -162,7 +170,7 @@ fn addTestSuite( } else .unknown; const ConfigAndExe = struct { - binary: std.Build.LazyPath, + binary: LazyPath, config: TestSuiteConfig, }; @@ -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(), @@ -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, }; }, @@ -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); @@ -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); } @@ -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(); @@ -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); diff --git a/build.zig.zon b/build.zig.zon index 12eebdb..5beb375 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -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", + }, } diff --git a/samples/math.zig b/samples/math.zig index fd5de7e..15116fe 100644 --- a/samples/math.zig +++ b/samples/math.zig @@ -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; } diff --git a/src/main.zig b/src/main.zig index 17550ca..f82c055 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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"), @@ -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], }; } diff --git a/tools/generate-tables.zig b/tools/generate-tables.zig index e6cd550..d7ad0b4 100644 --- a/tools/generate-tables.zig +++ b/tools/generate-tables.zig @@ -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); } @@ -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", .{}); } @@ -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);