FileSource -> LazyPath (#136)

wch-ch32v003
Matt Knight 1 year ago committed by GitHub
parent 9392fe0f7b
commit c1ac2f4cf3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,46 +0,0 @@
name: Downstream Updates
on:
workflow_dispatch:
push:
branches:
- main
jobs:
update:
runs-on: ubuntu-latest
strategy:
matrix:
repo: [
hardware-support-template,
espressif-esp,
gigadevice-gd32,
microchip-atmega,
nordic-nrf5x,
nxp-lpc,
raspberrypi-rp2040,
stmicro-stm32,
]
steps:
- name: Checkout
uses: actions/checkout@v2
with:
repository: ZigEmbeddedGroup/${{ matrix.repo }}
submodules: recursive
fetch-depth: 0
- name: Update
id: update
run: |
cd deps/microzig
git pull origin main
echo "DESCRIPTION=$(git log -1 --pretty=%B | head -n 1)" >> $GITHUB_OUTPUT
- name: Create Pull Request
uses: peter-evans/create-pull-request@v5.0.1
with:
token: ${{ secrets.PR_TOKEN }}
branch: downstream/update-microzig
commit-message: update microzig
delete-branch: true
title: Update microzig
body: ${{ steps.update.outputs.DESCRIPTION }}

@ -8,6 +8,7 @@ jobs:
strategy: strategy:
matrix: matrix:
os: [ os: [
linux-latest,
windows-latest, windows-latest,
macos-latest, macos-latest,
] ]

@ -5,8 +5,9 @@
const std = @import("std"); const std = @import("std");
const LibExeObjStep = std.build.LibExeObjStep; const LibExeObjStep = std.build.LibExeObjStep;
const Module = std.build.Module; const Module = std.build.Module;
const FileSource = std.build.FileSource; const LazyPath = std.build.LazyPath;
const Builder = std.Build; const OptionsStep = std.build.OptionsStep;
const Build = std.Build;
// alias for packages // alias for packages
pub const LinkerScriptStep = @import("src/modules/LinkerScriptStep.zig"); pub const LinkerScriptStep = @import("src/modules/LinkerScriptStep.zig");
@ -45,7 +46,7 @@ pub const EmbeddedExecutable = struct {
app_module.dependencies.put(name, module) catch @panic("OOM"); app_module.dependencies.put(name, module) catch @panic("OOM");
} }
pub fn installArtifact(exe: *EmbeddedExecutable, b: *Builder) void { pub fn installArtifact(exe: *EmbeddedExecutable, b: *Build) void {
b.installArtifact(exe.inner); b.installArtifact(exe.inner);
} }
@ -61,7 +62,7 @@ pub const EmbeddedExecutable = struct {
exe.inner.addCSourceFile(file, flags); exe.inner.addCSourceFile(file, flags);
} }
pub fn addOptions(exe: *EmbeddedExecutable, module_name: []const u8, options: *std.build.OptionsStep) void { pub fn addOptions(exe: *EmbeddedExecutable, module_name: []const u8, options: *OptionsStep) void {
exe.inner.addOptions(module_name, options); exe.inner.addOptions(module_name, options);
const app_module = exe.inner.modules.get("app").?; const app_module = exe.inner.modules.get("app").?;
const opt_module = exe.inner.modules.get(module_name).?; const opt_module = exe.inner.modules.get(module_name).?;
@ -79,100 +80,56 @@ fn root_dir() []const u8 {
pub const EmbeddedExecutableOptions = struct { pub const EmbeddedExecutableOptions = struct {
name: []const u8, name: []const u8,
source_file: std.build.FileSource, source_file: LazyPath,
backing: Backing, backing: Backing,
optimize: std.builtin.OptimizeMode = .Debug, optimize: std.builtin.OptimizeMode = .Debug,
linkerscript_source_file: ?FileSource = null, linkerscript_source_file: ?LazyPath = null,
}; };
pub fn addEmbeddedExecutable( pub fn addEmbeddedExecutable(b: *Build, opts: EmbeddedExecutableOptions) *EmbeddedExecutable {
builder: *std.build.Builder,
opts: EmbeddedExecutableOptions,
) *EmbeddedExecutable {
const has_board = (opts.backing == .board); const has_board = (opts.backing == .board);
const chip = switch (opts.backing) { const chip = switch (opts.backing) {
.chip => |c| c, .chip => |chip| chip,
.board => |b| b.chip, .board => |board| board.chip,
}; };
const has_hal = chip.hal != null; const has_hal = chip.hal != null;
const config_file_name = blk: {
const hash = hash_blk: {
var hasher = std.hash.SipHash128(1, 2).init("abcdefhijklmnopq");
hasher.update(chip.name);
// TODO: this will likely crash for generated sources, need to
// properly hook this up to the build cache api
hasher.update(chip.source.getPath(builder));
hasher.update(chip.cpu.name);
hasher.update(chip.cpu.source.getPath(builder));
if (opts.backing == .board) {
hasher.update(opts.backing.board.name);
// TODO: see above
hasher.update(opts.backing.board.source.getPath(builder));
}
var mac: [16]u8 = undefined;
hasher.final(&mac);
break :hash_blk mac;
};
const file_prefix = "zig-cache/microzig/config-";
const file_suffix = ".zig";
var ld_file_name: [file_prefix.len + 2 * hash.len + file_suffix.len]u8 = undefined; // TODO: let the user override which ram section to use the stack on,
const filename = std.fmt.bufPrint(&ld_file_name, "{s}{}{s}", .{ // for now just using the first ram section in the memory region list
file_prefix, const first_ram = blk: {
std.fmt.fmtSliceHexLower(&hash), for (chip.memory_regions) |region| {
file_suffix, if (region.kind == .ram)
}) catch unreachable; break :blk region;
} else @panic("no ram memory region found for setting the end-of-stack address");
break :blk builder.dupe(filename);
}; };
{ const config = b.addOptions();
// TODO: let the user override which ram section to use the stack on, config.addOption(bool, "has_hal", has_hal);
// for now just using the first ram section in the memory region list config.addOption(bool, "has_board", has_board);
const first_ram = blk: { if (has_board)
for (chip.memory_regions) |region| { config.addOption([]const u8, "board_name", opts.backing.board.name);
if (region.kind == .ram)
break :blk region;
} else @panic("no ram memory region found for setting the end-of-stack address");
};
std.fs.cwd().makeDir(std.fs.path.dirname(config_file_name).?) catch {};
var config_file = std.fs.cwd().createFile(config_file_name, .{}) catch unreachable;
defer config_file.close();
var writer = config_file.writer();
writer.print("pub const has_hal = {};\n", .{has_hal}) catch unreachable; config.addOption([]const u8, "chip_name", chip.name);
writer.print("pub const has_board = {};\n", .{has_board}) catch unreachable; config.addOption([]const u8, "cpu_name", chip.name);
if (has_board) config.addOption(usize, "end_of_stack", first_ram.offset + first_ram.length);
writer.print("pub const board_name = \"{}\";\n", .{std.fmt.fmtSliceEscapeUpper(opts.backing.board.name)}) catch unreachable;
writer.print("pub const chip_name = \"{}\";\n", .{std.fmt.fmtSliceEscapeUpper(chip.name)}) catch unreachable;
writer.print("pub const cpu_name = \"{}\";\n", .{std.fmt.fmtSliceEscapeUpper(chip.cpu.name)}) catch unreachable;
writer.print("pub const end_of_stack = 0x{X:0>8};\n\n", .{first_ram.offset + first_ram.length}) catch unreachable;
}
const microzig_module = builder.createModule(.{ const microzig_module = b.createModule(.{
.source_file = .{ .path = comptime std.fmt.comptimePrint("{s}/src/microzig.zig", .{root_dir()}) }, .source_file = .{ .path = comptime std.fmt.comptimePrint("{s}/src/microzig.zig", .{root_dir()}) },
}); });
microzig_module.dependencies.put("config", builder.createModule(.{ microzig_module.dependencies.put("config", b.createModule(.{
.source_file = .{ .path = config_file_name }, .source_file = config.getSource(),
})) catch unreachable; })) catch unreachable;
microzig_module.dependencies.put("chip", builder.createModule(.{ microzig_module.dependencies.put("chip", b.createModule(.{
.source_file = chip.source, .source_file = chip.source,
.dependencies = &.{ .dependencies = &.{
.{ .name = "microzig", .module = microzig_module }, .{ .name = "microzig", .module = microzig_module },
}, },
})) catch unreachable; })) catch unreachable;
microzig_module.dependencies.put("cpu", builder.createModule(.{ microzig_module.dependencies.put("cpu", b.createModule(.{
.source_file = chip.cpu.source, .source_file = chip.cpu.source,
.dependencies = &.{ .dependencies = &.{
.{ .name = "microzig", .module = microzig_module }, .{ .name = "microzig", .module = microzig_module },
@ -180,7 +137,7 @@ pub fn addEmbeddedExecutable(
})) catch unreachable; })) catch unreachable;
if (chip.hal) |hal_module_source| { if (chip.hal) |hal_module_source| {
microzig_module.dependencies.put("hal", builder.createModule(.{ microzig_module.dependencies.put("hal", b.createModule(.{
.source_file = hal_module_source, .source_file = hal_module_source,
.dependencies = &.{ .dependencies = &.{
.{ .name = "microzig", .module = microzig_module }, .{ .name = "microzig", .module = microzig_module },
@ -190,7 +147,7 @@ pub fn addEmbeddedExecutable(
switch (opts.backing) { switch (opts.backing) {
.board => |board| { .board => |board| {
microzig_module.dependencies.put("board", builder.createModule(.{ microzig_module.dependencies.put("board", b.createModule(.{
.source_file = board.source, .source_file = board.source,
.dependencies = &.{ .dependencies = &.{
.{ .name = "microzig", .module = microzig_module }, .{ .name = "microzig", .module = microzig_module },
@ -200,16 +157,16 @@ pub fn addEmbeddedExecutable(
else => {}, else => {},
} }
const app_module = builder.createModule(.{ const app_module = b.createModule(.{
.source_file = opts.source_file, .source_file = opts.source_file,
.dependencies = &.{ .dependencies = &.{
.{ .name = "microzig", .module = microzig_module }, .{ .name = "microzig", .module = microzig_module },
}, },
}); });
const exe = builder.allocator.create(EmbeddedExecutable) catch unreachable; const exe = b.allocator.create(EmbeddedExecutable) catch unreachable;
exe.* = EmbeddedExecutable{ exe.* = EmbeddedExecutable{
.inner = builder.addExecutable(.{ .inner = b.addExecutable(.{
.name = opts.name, .name = opts.name,
.root_source_file = .{ .path = comptime std.fmt.comptimePrint("{s}/src/start.zig", .{root_dir()}) }, .root_source_file = .{ .path = comptime std.fmt.comptimePrint("{s}/src/start.zig", .{root_dir()}) },
.target = chip.cpu.target, .target = chip.cpu.target,
@ -228,7 +185,7 @@ pub fn addEmbeddedExecutable(
if (opts.linkerscript_source_file) |linkerscript_source_file| { if (opts.linkerscript_source_file) |linkerscript_source_file| {
exe.inner.setLinkerScriptPath(linkerscript_source_file); exe.inner.setLinkerScriptPath(linkerscript_source_file);
} else { } else {
const linkerscript = LinkerScriptStep.create(builder, chip) catch unreachable; const linkerscript = LinkerScriptStep.create(b, chip) catch unreachable;
exe.inner.setLinkerScriptPath(.{ .generated = &linkerscript.generated_file }); exe.inner.setLinkerScriptPath(.{ .generated = &linkerscript.generated_file });
} }
@ -242,7 +199,7 @@ pub fn addEmbeddedExecutable(
} }
/// This build script validates usage patterns we expect from MicroZig /// This build script validates usage patterns we expect from MicroZig
pub fn build(b: *std.build.Builder) !void { pub fn build(b: *Build) !void {
const backings = @import("test/backings.zig"); const backings = @import("test/backings.zig");
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});

@ -1,14 +1,3 @@
= Tips and Tricks = Tips and Tricks
This document has some `build.zig` tricks for other pages to reference. This document has some `build.zig` tricks for other pages to reference.
== Packaging and Paths
TODO
[source,zig]
----
fn root_dir() []const u8 {
return std.fs.path.dirname(@src().file) orelse unreachable;
}
----

@ -2,5 +2,5 @@ const std = @import("std");
const Chip = @import("Chip.zig"); const Chip = @import("Chip.zig");
name: []const u8, name: []const u8,
source: std.build.FileSource, source: std.build.LazyPath,
chip: Chip, chip: Chip,

@ -1,5 +1,5 @@
const std = @import("std"); const std = @import("std");
const FileSource = std.build.FileSource; const LazyPath = std.build.LazyPath;
const MemoryRegion = @import("MemoryRegion.zig"); const MemoryRegion = @import("MemoryRegion.zig");
const Cpu = @import("Cpu.zig"); const Cpu = @import("Cpu.zig");
@ -7,10 +7,10 @@ const Cpu = @import("Cpu.zig");
const Chip = @This(); const Chip = @This();
name: []const u8, name: []const u8,
source: FileSource, source: LazyPath,
cpu: Cpu, cpu: Cpu,
hal: ?FileSource = null, hal: ?LazyPath = null,
json_register_schema: ?FileSource = null, json_register_schema: ?LazyPath = null,
memory_regions: []const MemoryRegion, memory_regions: []const MemoryRegion,
pub fn from_standard_paths(comptime root_dir: []const u8, args: struct { pub fn from_standard_paths(comptime root_dir: []const u8, args: struct {

@ -1,5 +1,5 @@
const std = @import("std"); const std = @import("std");
name: []const u8, name: []const u8,
source: std.build.FileSource, source: std.build.LazyPath,
target: std.zig.CrossTarget, target: std.zig.CrossTarget,

Loading…
Cancel
Save