Skip to content
4 changes: 3 additions & 1 deletion build-internals/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const Build = std.Build;
const LazyPath = Build.LazyPath;
const Module = Build.Module;

const anyverz = @import("anyverz");

const regz = @import("regz");
pub const Patch = regz.patch.Patch;
const uf2 = @import("uf2");
Expand Down Expand Up @@ -103,7 +105,7 @@ pub const Target = struct {
const ret = from.dep.builder.allocator.create(Target) catch @panic("out of memory");
ret.* = from.*;

inline for (@typeInfo(DeriveOptions).@"struct".field_names) |field_name| {
inline for (comptime anyverz.fieldNames(DeriveOptions)) |field_name| {
const value = @field(options, field_name);
if (value) |val| @field(ret, field_name) = val;
}
Expand Down
1 change: 1 addition & 0 deletions build-internals/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
.uf2 = .{ .path = "../tools/uf2" },
.@"esp-image" = .{ .path = "../tools/esp-image" },
.dfu = .{ .path = "../tools/dfu" },
.anyverz = .{ .path = "../modules/anyverz" },
},
.paths = .{
"build.zig",
Expand Down
13 changes: 7 additions & 6 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const Build = std.Build;
const LazyPath = Build.LazyPath;
const assert = std.debug.assert;

const anyverz = @import("anyverz");

const internals = @import("build-internals");
pub const Target = internals.Target;
pub const Cpu = internals.Cpu;
Expand Down Expand Up @@ -74,17 +76,16 @@ pub const PortSelect = struct {

pub const all: PortSelect = blk: {
var ret: PortSelect = undefined;
for (@typeInfo(PortSelect).@"struct".field_names) |field_name| {
for (anyverz.fieldNames(PortSelect)) |field_name| {
@field(ret, field_name) = true;
}

break :blk ret;
};

comptime {
const info = @typeInfo(PortSelect).@"struct";
// assumes fields are in the same order as the port list
for (port_list, info.field_names, info.field_attrs) |port_entry, field_name, field_attr| {
for (port_list, anyverz.fieldNames(PortSelect), anyverz.fieldAttrs(PortSelect)) |port_entry, field_name, field_attr| {
assert(std.mem.eql(u8, port_entry.name, field_name));
const default_value_ptr: *const bool = @ptrCast(field_attr.default_value_ptr);
assert(false == default_value_ptr.*);
Expand Down Expand Up @@ -144,7 +145,7 @@ pub fn MicroBuild(port_select: PortSelect) type {

var field_names: [count][]const u8 = undefined;
var field_types: [count]type = undefined;
var field_attrs: [count]std.builtin.Type.Struct.FieldAttributes = undefined;
var field_attrs: [count]anyverz.StructFieldAttributes = undefined;

if (count > 0) {
var i: usize = 0;
Expand Down Expand Up @@ -776,7 +777,7 @@ pub inline fn custom_lazy_import(
const deps = build_runner.dependencies;
const pkg_hash = custom_find_import_pkg_hash_or_fatal(dep_name);

inline for (@typeInfo(deps.packages).@"struct".decl_names) |decl_name| {
inline for (comptime anyverz.declNames(deps.packages)) |decl_name| {
if (comptime std.mem.eql(u8, decl_name, pkg_hash)) {
const pkg = @field(deps.packages, decl_name);
const available = !@hasDecl(pkg, "available") or pkg.available;
Expand All @@ -798,7 +799,7 @@ inline fn custom_find_import_pkg_hash_or_fatal(comptime dep_name: []const u8) []
const build_runner = @import("root");
const deps = build_runner.dependencies;

const pkg_deps = comptime for (@typeInfo(deps.packages).@"struct".decl_names) |decl_name| {
const pkg_deps = comptime for (anyverz.declNames(deps.packages)) |decl_name| {
const pkg_hash = decl_name;
const pkg = @field(deps.packages, pkg_hash);
if (@hasDecl(pkg, "build_zig") and pkg.build_zig == @This()) break pkg.deps;
Expand Down
1 change: 1 addition & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
.@"modules/network" = .{ .path = "modules/network" },
.@"modules/riscv32-common" = .{ .path = "modules/riscv32-common" },
.@"modules/rtt" = .{ .path = "modules/rtt" },
.anyverz = .{ .path = "modules/anyverz" },

// simulators
.@"sim/aviron" = .{ .path = "sim/aviron", .lazy = true },
Expand Down
119 changes: 119 additions & 0 deletions modules/anyverz/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
const builtin = @import("builtin");
const std = @import("std");

const zigVersionNotSupported =
@compileError("Zig version " ++
builtin.zig_version_string ++
" is not supported by anyverz");

pub const ZigVersion = enum { @"0.16", @"0.17" };

pub const zig_version: ZigVersion = switch (builtin.zig_version.minor) {
16 => .@"0.16",
17 => .@"0.17",
else => zigVersionNotSupported,
};

comptime {
std.testing.expectEqual(0, builtin.zig_version.major) catch zigVersionNotSupported;
_ = zig_version;
}

// fields

pub const StructFieldAttributes = switch (zig_version) {
.@"0.16" => std.builtin.Type.StructField.Attributes,
.@"0.17" => std.builtin.Type.Struct.FieldAttributes,
};

pub fn fieldsLen(T: type) comptime_int {
@setEvalBranchQuota(10000);
return std.meta.fieldNames(T).len;
}

pub const fieldNames = std.meta.fieldNames;

pub fn fieldTypes(T: type) [fieldsLen(T)]type {
@setEvalBranchQuota(10000);
comptime switch (zig_version) {
.@"0.16" => {
const fieldInfos = std.meta.fields(T);
var types: [fieldInfos.len]type = undefined;
for (fieldInfos, 0..) |field, i| types[i] = field.type;
return types;
},
.@"0.17" => return std.meta.fieldTypes(T)[0..fieldsLen(T)].*,
};
}

pub fn fieldAttrs(T: type) [fieldsLen(T)]StructFieldAttributes {
@setEvalBranchQuota(10000);
comptime switch (zig_version) {
.@"0.16" => {
const fieldInfos = std.meta.fields(T);
var attrs: [fieldInfos.len]StructFieldAttributes = undefined;
for (fieldInfos, 0..) |field, i| attrs[i] = .{
.@"comptime" = field.is_comptime,
.@"align" = field.alignment,
.default_value_ptr = field.default_value_ptr,
};
return attrs;
},
.@"0.17" => switch (@typeInfo(T)) {
inline .@"struct", .@"union" => |info| return info.field_attrs[0..fieldsLen(T)].*,
else => @compileError("Expected struct or union type, found '" ++ @typeName(T) ++ "'"),
},
};
}

// declarations

pub fn declsLen(T: type) usize {
@setEvalBranchQuota(10000);
return comptime std.meta.declarations(T).len;
}

pub fn declNames(comptime T: type) [declsLen(T)][:0]const u8 {
@setEvalBranchQuota(10000);
comptime switch (zig_version) {
.@"0.16" => {
const declInfos = std.meta.declarations(T);
var names: [declInfos.len][:0]const u8 = undefined;
for (declInfos, 0..) |decl, i| names[i] = decl.name;
return names;
},
.@"0.17" => return std.meta.declarations(T)[0..declsLen(T)].*,
};
}

// build utilities

pub fn addPassthruArgs(b: *std.Build, run_step: *std.Build.Step.Run) void {
switch (zig_version) {
.@"0.16" => if (b.args) |args| run_step.addArgs(args),
.@"0.17" => run_step.addPassthruArgs(),
}
}

pub fn buildRoot(b: *std.Build) std.Io.Dir {
return switch (zig_version) {
.@"0.16" => b.build_root.handle,
.@"0.17" => b.root.root_dir.handle,
};
}

pub fn findProgramLazy(b: *std.Build, names: []const [:0]const u8) std.Build.LazyPath {
return switch (zig_version) {
.@"0.16" => .{ .cwd_relative = b.findProgram(names, &.{}) catch
std.debug.panic("Could not find program {s}", .{names[0]}) },
.@"0.17" => b.findProgramLazy(.{ .names = names }),
};
}

pub fn build(b: *std.Build) void {
_ = b.addModule("anyverz", .{
.root_source_file = b.path("build.zig"),
.target = b.standardTargetOptions(.{}),
.optimize = b.standardOptimizeOption(.{}),
});
}
10 changes: 10 additions & 0 deletions modules/anyverz/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.{
.name = .anyverz,
.version = "0.0.1",
.minimum_zig_version = "0.16.0",
.paths = .{
"build.zig",
"build.zig.zon",
},
.fingerprint = 0xdb00016624a5ee02,
}
6 changes: 5 additions & 1 deletion port/raspberrypi/rp2xxx/build.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const std = @import("std");
const microzig = @import("microzig/build-internals");
const anyverz = @import("anyverz");

const Self = @This();

Expand Down Expand Up @@ -365,7 +366,10 @@ fn get_bootrom(b: *std.Build, target: *const microzig.Target, rom: BootROM) std.

const rom_objcopy = b.addObjCopy(rom_exe.getEmittedBin(), .{
.basename = b.fmt("{s}.bin", .{@tagName(rom)}),
.format = .binary,
.format = switch (anyverz.zig_version) {
.@"0.17" => .binary,
.@"0.16" => .bin,
},
});

return rom_objcopy.getOutput();
Expand Down
1 change: 1 addition & 0 deletions port/raspberrypi/rp2xxx/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
.lazy = true,
},
.@"bounded-array" = .{ .path = "../../../modules/bounded-array" },
.anyverz = .{ .path = "../../../modules/anyverz" },
},
.paths = .{
"README.md",
Expand Down
16 changes: 8 additions & 8 deletions sim/aviron/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const Build = std.Build;
const LazyPath = Build.LazyPath;
const ResolvedTarget = Build.ResolvedTarget;

const anyverz = @import("anyverz");

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

const samples = [_][]const u8{
Expand Down Expand Up @@ -73,7 +75,7 @@ pub fn build(b: *Build) !void {

const run_cmd = b.addRunArtifact(aviron_exe);
run_cmd.step.dependOn(b.getInstallStep());
run_cmd.addPassthruArgs();
anyverz.addPassthruArgs(b, run_cmd);
run_step.dependOn(&run_cmd.step);

const avr_target = b.resolveTargetQuery(avr_target_query);
Expand Down Expand Up @@ -156,7 +158,7 @@ fn add_test_suite(
// Scan the testsuite directory for files. Based on the extension, either load or compile them.
// Files in testsuite.avr-gcc will be compiled with avr-gcc and have the output copied to
// this directory.
var walkdir = try b.root.root_dir.handle.openDir(io, "testsuite", .{ .iterate = true });
var walkdir = try anyverz.buildRoot(b).openDir(io, "testsuite", .{ .iterate = true });
defer walkdir.close(io);

var walker = try walkdir.walk(b.allocator);
Expand Down Expand Up @@ -192,7 +194,7 @@ fn add_test_suite(
};

const ext = std.fs.path.extension(entry.basename);
const action: FileAction = inline for (@typeInfo(@TypeOf(extension_to_action)).@"struct".field_names) |field_name| {
const action: FileAction = inline for (comptime anyverz.fieldNames(@TypeOf(extension_to_action))) |field_name| {
const action: FileAction = @field(extension_to_action, field_name);

if (std.mem.eql(u8, ext, "." ++ field_name))
Expand Down Expand Up @@ -331,12 +333,10 @@ fn add_test_suite_update(
b: *Build,
invoke_step: *Build.Step,
) !void {
const avr_gcc = b.findProgramLazy(.{
.names = &.{"avr-gcc"},
});
const avr_gcc = anyverz.findProgramLazy(b, &.{"avr-gcc"});

const io = b.graph.io;
var walkdir = try b.root.root_dir.handle.openDir(io, "testsuite.avr-gcc", .{ .iterate = true });
var walkdir = try anyverz.buildRoot(b).openDir(io, "testsuite.avr-gcc", .{ .iterate = true });
defer walkdir.close(io);

var walker = try walkdir.walk(b.allocator);
Expand Down Expand Up @@ -364,7 +364,7 @@ fn add_test_suite_update(
};

const ext = std.fs.path.extension(entry.basename);
const action: FileAction = inline for (@typeInfo(@TypeOf(extension_to_action)).@"struct".field_names) |field_name| {
const action: FileAction = inline for (comptime anyverz.fieldNames(@TypeOf(extension_to_action))) |field_name| {
const action: FileAction = @field(extension_to_action, field_name);
if (std.mem.eql(u8, ext, "." ++ field_name))
break action;
Expand Down
1 change: 1 addition & 0 deletions sim/aviron/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.fingerprint = 0xfc536f957aeaeb54,
.dependencies = .{
.@"bounded-array" = .{ .path = "../../modules/bounded-array" },
.anyverz = .{ .path = "../../modules/anyverz" },
.flags = .{ .path = "../../tools/flags" },
.ihex = .{
.url = "git+https://github.com/mattnite/zig-ihex#9deb291104ecd7dcaab0db27bf05252764600947",
Expand Down
33 changes: 14 additions & 19 deletions tools/regz/build.zig
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
const std = @import("std");
const Build = std.Build;

const anyverz = @import("anyverz");

pub const patch = @import("src/patch.zig");

pub fn build(b: *Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const anyverz_dep = b.dependency("anyverz", .{
.target = target,
.optimize = optimize,
});

const libxml2_dep = b.dependency("libxml2", .{
.target = target,
.optimize = .ReleaseSafe,
Expand All @@ -17,8 +24,6 @@ pub fn build(b: *Build) !void {
.optimize = optimize,
});

const zqlite = zqlite_dep.module("zqlite");

const xml_module = b.createModule(.{
.root_source_file = b.path("src/xml.zig"),
.target = target,
Expand All @@ -33,14 +38,9 @@ pub fn build(b: *Build) !void {
.target = target,
.optimize = optimize,
.imports = &.{
.{
.name = "zqlite",
.module = zqlite,
},
.{
.name = "xml",
.module = xml_module,
},
.{ .name = "zqlite", .module = zqlite_dep.module("zqlite") },
.{ .name = "xml", .module = xml_module },
.{ .name = "anyverz", .module = anyverz_dep.module("anyverz") },
},
});
regz_module.linkLibrary(libxml2_dep.artifact("xml"));
Expand All @@ -52,22 +52,17 @@ pub fn build(b: *Build) !void {
.target = target,
.optimize = optimize,
.imports = &.{
.{
.name = "regz",
.module = regz_module,
},
.{
.name = "xml",
.module = xml_module,
},
.{ .name = "regz", .module = regz_module },
.{ .name = "xml", .module = xml_module },
.{ .name = "anyverz", .module = anyverz_dep.module("anyverz") },
},
}),
.use_llvm = true,
});
b.installArtifact(regz);

const run_cmd = b.addRunArtifact(regz);
run_cmd.addPassthruArgs();
anyverz.addPassthruArgs(b, run_cmd);
run_cmd.step.dependOn(b.getInstallStep());

const run_step = b.step("run", "Run the app");
Expand Down
Loading