Update to latest zig with modules (#102)

wch-ch32v003
Vesim 2 years ago committed by GitHub
parent e63558330b
commit 9ccde9ff37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,11 +3,14 @@
//! This means we need to use addExecutable() instead of using //! This means we need to use addExecutable() instead of using
const std = @import("std"); const std = @import("std");
const microzig = @import("src/main.zig");
const boards = microzig.boards; pub const microzig = @import("src/main.zig");
const chips = microzig.chips;
const Backing = microzig.Backing; // alias for packages
pub const addEmbeddedExecutable = microzig.addEmbeddedExecutable;
pub const boards = microzig.boards;
pub const chips = microzig.chips;
pub const Backing = microzig.Backing;
pub fn build(b: *std.build.Builder) !void { pub fn build(b: *std.build.Builder) !void {
// Standard optimization options allow the person running `zig build -Doptimize=...` to select // Standard optimization options allow the person running `zig build -Doptimize=...` to select
@ -50,13 +53,14 @@ pub fn build(b: *std.build.Builder) !void {
if ((cfg.backing.getTarget().cpu_arch.?) == .avr and tst.on_avr == false) continue; if ((cfg.backing.getTarget().cpu_arch.?) == .avr and tst.on_avr == false) continue;
if (!tst.on_riscv32) continue; if (!tst.on_riscv32) continue;
const exe = microzig.addEmbeddedExecutable( var exe = microzig.addEmbeddedExecutable(
b, b,
b.fmt("test-{s}-{s}.elf", .{ tst.name, cfg.name }), b.fmt("test-{s}-{s}.elf", .{ tst.name, cfg.name }),
tst.source, tst.source,
cfg.backing, cfg.backing,
.{.optimize = optimize }, .{ .optimize = optimize },
); );
exe.addDriver(microzig.drivers.button);
if (filter == null or exe.inner.target.cpu_arch.? == filter.?) { if (filter == null or exe.inner.target.cpu_arch.? == filter.?) {
exe.inner.install(); exe.inner.install();

@ -22,39 +22,46 @@ pub const Backing = union(enum) {
} }
}; };
const Pkg = std.build.Pkg; const Module = std.build.Module;
const root_path = root() ++ "/"; const root_path = root() ++ "/";
fn root() []const u8 { fn root() []const u8 {
return std.fs.path.dirname(@src().file) orelse unreachable; return std.fs.path.dirname(@src().file) orelse unreachable;
} }
pub const BuildOptions = struct { pub const BuildOptions = struct {
// a hal package is a package with ergonomic wrappers for registers for a // a hal module is a module with ergonomic wrappers for registers for a
// given mcu, it's only dependency can be microzig // given mcu, it's only dependency can be microzig
hal_package_path: ?std.build.FileSource = null, hal_module_path: ?std.build.FileSource = null,
optimize: std.builtin.OptimizeMode = std.builtin.OptimizeMode.Debug, optimize: std.builtin.OptimizeMode = std.builtin.OptimizeMode.Debug,
}; };
pub const EmbeddedExecutable = struct { pub const EmbeddedExecutable = struct {
inner: *LibExeObjStep, inner: *LibExeObjStep,
app_packages: std.ArrayList(Pkg),
pub fn addPackage(exe: *EmbeddedExecutable, pkg: Pkg) void { pub fn addModule(exe: *EmbeddedExecutable, name: []const u8, module: *Module) void {
exe.app_packages.append(pkg) catch @panic("failed to append"); exe.inner.addModule(name, module);
for (exe.inner.packages.items) |*entry| {
if (std.mem.eql(u8, "app", entry.name)) {
entry.dependencies = exe.app_packages.items;
break;
}
} else @panic("app package not found");
} }
pub fn addPackagePath(exe: *EmbeddedExecutable, name: []const u8, pkg_index_path: []const u8) void { pub fn addDriver(exe: *EmbeddedExecutable, driver: Driver) void {
exe.addPackage(Pkg{ var dependencies = std.ArrayList(std.Build.ModuleDependency).init(exe.inner.builder.allocator);
.name = exe.inner.builder.allocator.dupe(u8, name) catch unreachable,
.source = .{ .path = exe.inner.builder.allocator.dupe(u8, pkg_index_path) catch unreachable }, for (driver.dependencies) |dep| {
dependencies.append(.{
.name = dep,
.module = exe.inner.builder.modules.get(dep).?,
}) catch @panic("OOM");
}
// TODO: this is not perfect but should work for now
exe.inner.addAnonymousModule(driver.name, .{
.source_file = driver.source_file,
.dependencies = dependencies.toOwnedSlice() catch @panic("OOM"),
}); });
const app_module = exe.inner.modules.get("app").?;
const driver_module = exe.inner.modules.get(driver.name).?;
app_module.dependencies.put(driver.name, driver_module) catch @panic("OOM");
} }
pub fn install(exe: *EmbeddedExecutable) void { pub fn install(exe: *EmbeddedExecutable) void {
@ -77,9 +84,8 @@ pub const EmbeddedExecutable = struct {
exe.inner.addCSourceFile(file, flags); exe.inner.addCSourceFile(file, flags);
} }
pub fn addOptions(exe: *EmbeddedExecutable, package_name: []const u8, options: *std.build.OptionsStep) void { pub fn addOptions(exe: *EmbeddedExecutable, module_name: []const u8, options: *std.build.OptionsStep) void {
exe.inner.addOptions(package_name, options); exe.inner.addOptions(module_name, options);
exe.addPackage(.{ .name = package_name, .source = options.getSource() });
} }
pub fn addObjectFile(exe: *EmbeddedExecutable, source_file: []const u8) void { pub fn addObjectFile(exe: *EmbeddedExecutable, source_file: []const u8) void {
@ -156,26 +162,32 @@ pub fn addEmbeddedExecutable(
writer.print("pub const end_of_stack = 0x{X:0>8};\n\n", .{first_ram.offset + first_ram.length}) catch unreachable; writer.print("pub const end_of_stack = 0x{X:0>8};\n\n", .{first_ram.offset + first_ram.length}) catch unreachable;
} }
const config_pkg = Pkg{ builder.addModule(.{
.name = "microzig-config", .name = "microzig",
.source = .{ .path = config_file_name }, .source_file = .{ .path = root_path ++ "core/import-module.zig" },
}; });
const microzig = builder.modules.get("microzig").?;
const chip_pkg = Pkg{ const config_module = builder.createModule(.{
.name = "chip", .source_file = .{ .path = config_file_name },
.source = .{ .path = chip.path }, });
.dependencies = &.{pkgs.microzig},
};
const cpu_pkg = Pkg{ const chip_module = builder.createModule(.{
.name = "cpu", .source_file = .{ .path = chip.path },
.source = .{ .path = chip.cpu.path }, .dependencies = &.{
.dependencies = &.{pkgs.microzig}, .{ .name = "microzig", .module = microzig },
}; },
});
const cpu_module = builder.createModule(.{
.source_file = .{ .path = chip.cpu.path },
.dependencies = &.{
.{ .name = "microzig", .module = microzig },
},
});
var exe = EmbeddedExecutable{ var exe = EmbeddedExecutable{
.inner = builder.addExecutable(.{ .name = name, .root_source_file = .{ .path = root_path ++ "core/microzig.zig" }, .target = chip.cpu.target, .optimize = options.optimize }), .inner = builder.addExecutable(.{ .name = name, .root_source_file = .{ .path = root_path ++ "core/microzig.zig" }, .target = chip.cpu.target, .optimize = options.optimize }),
.app_packages = std.ArrayList(Pkg).init(builder.allocator),
}; };
exe.inner.strip = false; // we always want debug symbols, stripping brings us no benefit on embedded exe.inner.strip = false; // we always want debug symbols, stripping brings us no benefit on embedded
@ -188,67 +200,64 @@ pub fn addEmbeddedExecutable(
exe.inner.setLinkerScriptPath(.{ .generated = &linkerscript.generated_file }); exe.inner.setLinkerScriptPath(.{ .generated = &linkerscript.generated_file });
// TODO: // TODO:
// - Generate the linker scripts from the "chip" or "board" package instead of using hardcoded ones. // - Generate the linker scripts from the "chip" or "board" module instead of using hardcoded ones.
// - This requires building another tool that runs on the host that compiles those files and emits the linker script. // - This requires building another tool that runs on the host that compiles those files and emits the linker script.
// - src/tools/linkerscript-gen.zig is the source file for this // - src/tools/linkerscript-gen.zig is the source file for this
exe.inner.bundle_compiler_rt = (exe.inner.target.cpu_arch.? != .avr); // don't bundle compiler_rt for AVR as it doesn't compile right now exe.inner.bundle_compiler_rt = (exe.inner.target.cpu_arch.? != .avr); // don't bundle compiler_rt for AVR as it doesn't compile right now
// these packages will be re-exported from core/microzig.zig // these modules will be re-exported from core/microzig.zig
exe.inner.addPackage(config_pkg); exe.inner.addModule("microzig-config", config_module);
exe.inner.addPackage(chip_pkg); exe.inner.addModule("chip", chip_module);
exe.inner.addPackage(cpu_pkg); exe.inner.addModule("cpu", cpu_module);
exe.inner.addPackage(.{ exe.inner.addModule("hal", builder.createModule(.{
.name = "hal", .source_file = if (options.hal_module_path) |hal_module_path|
.source = if (options.hal_package_path) |hal_package_path| hal_module_path
hal_package_path
else .{ .path = root_path ++ "core/empty.zig" }, else .{ .path = root_path ++ "core/empty.zig" },
.dependencies = &.{pkgs.microzig}, .dependencies = &.{
}); .{ .name = "microzig", .module = microzig },
},
}));
switch (backing) { switch (backing) {
.board => |board| { .board => |board| {
exe.inner.addPackage(std.build.Pkg{ exe.inner.addModule("board", builder.createModule(.{
.name = "board", .source_file = .{ .path = board.path },
.source = .{ .path = board.path }, .dependencies = &.{
.dependencies = &.{pkgs.microzig}, .{ .name = "microzig", .module = microzig },
}); },
}));
}, },
else => {}, else => {},
} }
exe.inner.addPackage(.{ exe.inner.addModule("app", builder.createModule(.{
.name = "app", .source_file = .{ .path = source },
.source = .{ .path = source }, .dependencies = &.{
}); .{ .name = "microzig", .module = microzig },
exe.addPackage(pkgs.microzig); },
}));
return exe; return exe;
} }
pub const pkgs = struct { pub const Driver = struct {
const mmio = std.build.Pkg{ name: []const u8,
.name = "microzig-mmio", source_file: std.build.FileSource,
.source = .{ .path = root_path ++ "core/mmio.zig" }, dependencies: []const []const u8,
};
pub const microzig = std.build.Pkg{
.name = "microzig",
.source = .{ .path = root_path ++ "core/import-package.zig" },
};
}; };
/// Generic purpose drivers shipped with microzig // Generic purpose drivers shipped with microzig
pub const drivers = struct { pub const drivers = struct {
pub const quadrature = std.build.Pkg{ pub const quadrature = Driver{
.name = "microzig.quadrature", .name = "microzig.quadrature",
.source = .{ .path = root_path ++ "drivers/quadrature.zig" }, .source_file = .{ .path = root_path ++ "drivers/quadrature.zig" },
.dependencies = &.{pkgs.microzig}, .dependencies = &.{"microzig"},
}; };
pub const button = std.build.Pkg{ pub const button = Driver{
.name = "microzig.button", .name = "microzig.button",
.source = .{ .path = root_path ++ "drivers/button.zig" }, .source_file = .{ .path = root_path ++ "drivers/button.zig" },
.dependencies = &.{pkgs.microzig}, .dependencies = &.{"microzig"},
}; };
}; };

Loading…
Cancel
Save