Skip to content

Commit

Permalink
FileSource to LazyPath
Browse files Browse the repository at this point in the history
  • Loading branch information
mattnite committed Aug 4, 2023
1 parent 9392fe0 commit 1a422ab
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 97 deletions.
117 changes: 37 additions & 80 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}

Expand All @@ -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).?;
Expand All @@ -79,108 +80,64 @@ 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 },
},
})) 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 },
Expand All @@ -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 },
Expand All @@ -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,
Expand All @@ -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 });
}

Expand All @@ -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(.{});

Expand Down
11 changes: 0 additions & 11 deletions docs/tricks.adoc
Original file line number Diff line number Diff line change
@@ -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;
}
----
2 changes: 1 addition & 1 deletion src/modules/Board.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
8 changes: 4 additions & 4 deletions src/modules/Chip.zig
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const std = @import("std");
const FileSource = std.build.FileSource;
const LazyPath = std.build.LazyPath;

const MemoryRegion = @import("MemoryRegion.zig");
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 {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Cpu.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const std = @import("std");

name: []const u8,
source: std.build.FileSource,
source: std.build.LazyPath,
target: std.zig.CrossTarget,

0 comments on commit 1a422ab

Please sign in to comment.