From 1a422abd433c98bc2ae2cc3c1911ca4f0b301e7c Mon Sep 17 00:00:00 2001 From: Matt Knight Date: Thu, 3 Aug 2023 19:07:02 -0700 Subject: [PATCH] FileSource to LazyPath --- build.zig | 117 +++++++++++++----------------------------- docs/tricks.adoc | 11 ---- src/modules/Board.zig | 2 +- src/modules/Chip.zig | 8 +-- src/modules/Cpu.zig | 2 +- 5 files changed, 43 insertions(+), 97 deletions(-) diff --git a/build.zig b/build.zig index 55e2ac88..275882a8 100644 --- a/build.zig +++ b/build.zig @@ -5,8 +5,9 @@ const std = @import("std"); const LibExeObjStep = std.build.LibExeObjStep; const Module = std.build.Module; -const FileSource = std.build.FileSource; -const Builder = std.Build; +const LazyPath = std.build.LazyPath; +const OptionsStep = std.build.OptionsStep; +const Build = std.Build; // alias for packages 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"); } - pub fn installArtifact(exe: *EmbeddedExecutable, b: *Builder) void { + pub fn installArtifact(exe: *EmbeddedExecutable, b: *Build) void { b.installArtifact(exe.inner); } @@ -61,7 +62,7 @@ pub const EmbeddedExecutable = struct { 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); const app_module = exe.inner.modules.get("app").?; const opt_module = exe.inner.modules.get(module_name).?; @@ -79,100 +80,56 @@ fn root_dir() []const u8 { pub const EmbeddedExecutableOptions = struct { name: []const u8, - source_file: std.build.FileSource, + source_file: LazyPath, backing: Backing, optimize: std.builtin.OptimizeMode = .Debug, - linkerscript_source_file: ?FileSource = null, + linkerscript_source_file: ?LazyPath = null, }; -pub fn addEmbeddedExecutable( - builder: *std.build.Builder, - opts: EmbeddedExecutableOptions, -) *EmbeddedExecutable { +pub fn addEmbeddedExecutable(b: *Build, opts: EmbeddedExecutableOptions) *EmbeddedExecutable { const has_board = (opts.backing == .board); const chip = switch (opts.backing) { - .chip => |c| c, - .board => |b| b.chip, + .chip => |chip| chip, + .board => |board| board.chip, }; 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; - const filename = std.fmt.bufPrint(&ld_file_name, "{s}{}{s}", .{ - file_prefix, - std.fmt.fmtSliceHexLower(&hash), - file_suffix, - }) catch unreachable; - - break :blk builder.dupe(filename); + // TODO: let the user override which ram section to use the stack on, + // for now just using the first ram section in the memory region list + const first_ram = blk: { + for (chip.memory_regions) |region| { + if (region.kind == .ram) + break :blk region; + } else @panic("no ram memory region found for setting the end-of-stack address"); }; - { - // TODO: let the user override which ram section to use the stack on, - // for now just using the first ram section in the memory region list - const first_ram = blk: { - for (chip.memory_regions) |region| { - 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(); + const config = b.addOptions(); + config.addOption(bool, "has_hal", has_hal); + config.addOption(bool, "has_board", has_board); + if (has_board) + config.addOption([]const u8, "board_name", opts.backing.board.name); - writer.print("pub const has_hal = {};\n", .{has_hal}) catch unreachable; - writer.print("pub const has_board = {};\n", .{has_board}) catch unreachable; - if (has_board) - 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; - } + config.addOption([]const u8, "chip_name", chip.name); + config.addOption([]const u8, "cpu_name", chip.name); + config.addOption(usize, "end_of_stack", first_ram.offset + first_ram.length); - const microzig_module = builder.createModule(.{ + const microzig_module = b.createModule(.{ .source_file = .{ .path = comptime std.fmt.comptimePrint("{s}/src/microzig.zig", .{root_dir()}) }, }); - microzig_module.dependencies.put("config", builder.createModule(.{ - .source_file = .{ .path = config_file_name }, + microzig_module.dependencies.put("config", b.createModule(.{ + .source_file = config.getSource(), })) catch unreachable; - microzig_module.dependencies.put("chip", builder.createModule(.{ + microzig_module.dependencies.put("chip", b.createModule(.{ .source_file = chip.source, .dependencies = &.{ .{ .name = "microzig", .module = microzig_module }, }, })) catch unreachable; - microzig_module.dependencies.put("cpu", builder.createModule(.{ + microzig_module.dependencies.put("cpu", b.createModule(.{ .source_file = chip.cpu.source, .dependencies = &.{ .{ .name = "microzig", .module = microzig_module }, @@ -180,7 +137,7 @@ pub fn addEmbeddedExecutable( })) catch unreachable; 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, .dependencies = &.{ .{ .name = "microzig", .module = microzig_module }, @@ -190,7 +147,7 @@ pub fn addEmbeddedExecutable( switch (opts.backing) { .board => |board| { - microzig_module.dependencies.put("board", builder.createModule(.{ + microzig_module.dependencies.put("board", b.createModule(.{ .source_file = board.source, .dependencies = &.{ .{ .name = "microzig", .module = microzig_module }, @@ -200,16 +157,16 @@ pub fn addEmbeddedExecutable( else => {}, } - const app_module = builder.createModule(.{ + const app_module = b.createModule(.{ .source_file = opts.source_file, .dependencies = &.{ .{ .name = "microzig", .module = microzig_module }, }, }); - const exe = builder.allocator.create(EmbeddedExecutable) catch unreachable; + const exe = b.allocator.create(EmbeddedExecutable) catch unreachable; exe.* = EmbeddedExecutable{ - .inner = builder.addExecutable(.{ + .inner = b.addExecutable(.{ .name = opts.name, .root_source_file = .{ .path = comptime std.fmt.comptimePrint("{s}/src/start.zig", .{root_dir()}) }, .target = chip.cpu.target, @@ -228,7 +185,7 @@ pub fn addEmbeddedExecutable( if (opts.linkerscript_source_file) |linkerscript_source_file| { exe.inner.setLinkerScriptPath(linkerscript_source_file); } else { - const linkerscript = LinkerScriptStep.create(builder, chip) catch unreachable; + const linkerscript = LinkerScriptStep.create(b, chip) catch unreachable; exe.inner.setLinkerScriptPath(.{ .generated = &linkerscript.generated_file }); } @@ -242,7 +199,7 @@ pub fn addEmbeddedExecutable( } /// 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 optimize = b.standardOptimizeOption(.{}); diff --git a/docs/tricks.adoc b/docs/tricks.adoc index 11055a95..6eda21f8 100644 --- a/docs/tricks.adoc +++ b/docs/tricks.adoc @@ -1,14 +1,3 @@ = Tips and Tricks 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; -} ----- diff --git a/src/modules/Board.zig b/src/modules/Board.zig index eb114bfe..8e09b4ac 100644 --- a/src/modules/Board.zig +++ b/src/modules/Board.zig @@ -2,5 +2,5 @@ const std = @import("std"); const Chip = @import("Chip.zig"); name: []const u8, -source: std.build.FileSource, +source: std.build.LazyPath, chip: Chip, diff --git a/src/modules/Chip.zig b/src/modules/Chip.zig index b26aecda..3676fc9b 100644 --- a/src/modules/Chip.zig +++ b/src/modules/Chip.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const FileSource = std.build.FileSource; +const LazyPath = std.build.LazyPath; const MemoryRegion = @import("MemoryRegion.zig"); const Cpu = @import("Cpu.zig"); @@ -7,10 +7,10 @@ const Cpu = @import("Cpu.zig"); const Chip = @This(); name: []const u8, -source: FileSource, +source: LazyPath, cpu: Cpu, -hal: ?FileSource = null, -json_register_schema: ?FileSource = null, +hal: ?LazyPath = null, +json_register_schema: ?LazyPath = null, memory_regions: []const MemoryRegion, pub fn from_standard_paths(comptime root_dir: []const u8, args: struct { diff --git a/src/modules/Cpu.zig b/src/modules/Cpu.zig index 4f9bcb9e..e4cd3077 100644 --- a/src/modules/Cpu.zig +++ b/src/modules/Cpu.zig @@ -1,5 +1,5 @@ const std = @import("std"); name: []const u8, -source: std.build.FileSource, +source: std.build.LazyPath, target: std.zig.CrossTarget,