Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup Zig
uses: mlugg/setup-zig@v1
with:
version: 0.14.0
version: 0.15.2

- name: Build Demo
run: |
Expand Down
79 changes: 34 additions & 45 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const link_libc = !(b.option(bool, "no-libc", "Prevents linking of libc by default") orelse false);
const link_libc = b.option(bool, "libc", "Forces the linking of libc");

const config = blk: {
var config = Config{};
Expand Down Expand Up @@ -91,11 +91,11 @@ pub fn build(b: *std.Build) void {
}

if (maybe_volume_names) |volume_names| {
var volumes = std.ArrayList([]const u8).init(b.allocator);
var volumes: std.ArrayList([]const u8) = .empty;

var iter = std.mem.splitScalar(u8, volume_names, ',');
while (iter.next()) |name| {
volumes.append(name) catch @panic("out of memory");
volumes.append(b.allocator, name) catch @panic("out of memory");
}
config.volumes = .{ .named = volumes.items };
}
Expand All @@ -109,20 +109,20 @@ pub fn build(b: *std.Build) void {
config.sector_size = .{
.dynamic = .{
.minimum = std.meta.stringToEnum(SectorOption, min) orelse bad_config(
"Invalid value for -Dsector-size: '{}'",
.{std.zig.fmtEscapes(sector_config)},
"Invalid value for -Dsector-size: '{f}'",
.{std.zig.fmtString(sector_config)},
),
.maximum = std.meta.stringToEnum(SectorOption, max) orelse bad_config(
"Invalid value for -Dsector-size: '{}'",
.{std.zig.fmtEscapes(sector_config)},
"Invalid value for -Dsector-size: '{f}'",
.{std.zig.fmtString(sector_config)},
),
},
};
} else {
config.sector_size = .{
.static = std.meta.stringToEnum(SectorOption, sector_config) orelse bad_config(
"Invalid value for -Dsector-size: '{}'",
.{std.zig.fmtEscapes(sector_config)},
"Invalid value for -Dsector-size: '{f}'",
.{std.zig.fmtString(sector_config)},
),
};
}
Expand All @@ -136,7 +136,7 @@ pub fn build(b: *std.Build) void {
.style = .blank,
.include_path = "ffconf.h",
}, .{
.FFCONF_DEF = 5380,
.FFCONF_DEF = 5385,
});

switch (config.volumes) {
Expand All @@ -147,13 +147,13 @@ pub fn build(b: *std.Build) void {
});
},
.named => |strings| {
var list = std.ArrayList(u8).init(b.allocator);
var list: std.ArrayList(u8) = .empty;
for (strings) |name| {
if (list.items.len > 0) {
list.appendSlice(", ") catch @panic("out of memory");
list.appendSlice(b.allocator, ", ") catch @panic("out of memory");
}
list.writer().print("\"{}\"", .{
std.fmt.fmtSliceHexUpper(name),
list.writer(b.allocator).print("\"{X}\"", .{
name,
}) catch @panic("out of memory");
}
config_header.addValues(.{
Expand Down Expand Up @@ -198,62 +198,51 @@ pub fn build(b: *std.Build) void {
}

// module:
const upstream = b.dependency("fatfs", .{});

const mod_options = b.addOptions();
mod_options.addOption(bool, "has_rtc", (config.rtc != .static));

// create copy of the upstream without ffconf.h
const upstream_copy = b.addWriteFiles();
_ = upstream_copy.addCopyFile(upstream.path("source/ff.c"), "ff.c");
_ = upstream_copy.addCopyFile(upstream.path("source/ff.h"), "ff.h");
_ = upstream_copy.addCopyFile(upstream.path("source/diskio.h"), "diskio.h");
_ = upstream_copy.addCopyFile(upstream.path("source/ffunicode.c"), "ffunicode.c");
_ = upstream_copy.addCopyFile(upstream.path("source/ffsystem.c"), "ffsystem.c");
_ = upstream_copy.addCopyFile(b.path("vendor/fatfs/source/ff.c"), "ff.c");
_ = upstream_copy.addCopyFile(b.path("vendor/fatfs/source/ff.h"), "ff.h");
_ = upstream_copy.addCopyFile(b.path("vendor/fatfs/source/diskio.h"), "diskio.h");
_ = upstream_copy.addCopyFile(b.path("vendor/fatfs/source/ffunicode.c"), "ffunicode.c");
_ = upstream_copy.addCopyFile(b.path("vendor/fatfs/source/ffsystem.c"), "ffsystem.c");
const upstream_copy_dir = upstream_copy.getDirectory();

const zfat_lib_mod = b.createModule(.{
const zfat_mod = b.addModule("zfat", .{
.root_source_file = b.path("src/fatfs.zig"),
.target = target,
.optimize = optimize,
.link_libc = link_libc,
});
zfat_lib_mod.addCSourceFiles(.{
zfat_mod.addCSourceFiles(.{
.root = upstream_copy_dir,

.files = &.{
"ff.c",
"ffunicode.c",
"ffsystem.c",
},
.flags = &.{"-std=c99"},
});
zfat_lib_mod.addConfigHeader(config_header);

const zfat = b.addLibrary(.{
.linkage = .static,
.name = "zfat",
.root_module = zfat_lib_mod,
});
zfat.installHeader(upstream_copy_dir.path(b, "ff.h"), "ff.h");
zfat.installHeader(upstream_copy_dir.path(b, "diskio.h"), "diskio.h");
zfat.installConfigHeader(config_header);

const zfat_mod = b.addModule("zfat", .{
.root_source_file = b.path("src/fatfs.zig"),
.target = target,
.optimize = optimize,
});
zfat_mod.linkLibrary(zfat);
zfat_mod.addIncludePath(upstream_copy_dir.path(b, "."));
zfat_mod.addConfigHeader(config_header);
zfat_mod.addOptions("config", mod_options);

// usage demo:
const exe = b.addExecutable(.{
.name = "zfat-demo",
.root_source_file = b.path("demo/main.zig"),
.target = target,
.optimize = optimize,
.root_module = b.createModule(.{
.root_source_file = b.path("demo/main.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
.imports = &.{
.{ .name = "zfat", .module = zfat_mod },
},
}),
});
exe.root_module.addImport("zfat", zfat_mod);

const demo_exe = b.addInstallArtifact(exe, .{});
demo_step.dependOn(&demo_exe.step);
Expand Down Expand Up @@ -285,7 +274,7 @@ fn add_config_field(config_header: *std.Build.Step.ConfigHeader, config: Config,
}

fn add_config_option(b: *std.Build, config: *Config, comptime field: @TypeOf(.tag), desc: []const u8) void {
const T = std.meta.FieldType(Config, field);
const T = @FieldType(Config, @tagName(field));
if (b.option(T, @tagName(field), desc)) |value|
@field(config, @tagName(field)) = value;
}
Expand Down
7 changes: 1 addition & 6 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
.name = .zfat,
.fingerprint = 0x4212c7b5f54ad348,
.version = "0.15.0",
.dependencies = .{
.fatfs = .{
.url = "http://elm-chan.org/fsw/ff/arc/ff15a.zip",
.hash = "N-V-__8AAFQITQCnpmdR7PARImvk-cgb-9lZmjKolexSWkUL",
},
},
.dependencies = .{},
.paths = .{
"build.zig",
"build.zig.zon",
Expand Down
28 changes: 27 additions & 1 deletion demo/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,33 @@ pub fn main() !u8 {
var file = try fatfs.File.create("0:/firmware.uf2");
defer file.close();

try file.writer().writeAll("Hello, World!\r\n");
var buffer: [64]u8 = undefined;

const test_string = "Hello, World!\r\n";

var writer = file.writer(&buffer);
try writer.writer.writeAll(test_string);
try writer.writer.flush();

try std.testing.expectEqual(true, file.endOfFile());
try std.testing.expectEqual(test_string.len, file.size());

try file.seekTo(test_string.len / 2);
try std.testing.expectEqual(false, file.endOfFile());
try std.testing.expectEqual(test_string.len / 2, file.tell());
try std.testing.expectEqual(test_string.len, file.size());

try std.testing.expectEqual(false, file.hasError());

try file.rewind();
try std.testing.expectEqual(0, file.tell());

var reader = file.reader(&buffer);
const written_string = try reader.reader.take(test_string.len);
try std.testing.expectEqualStrings(test_string, written_string);

_ = try file.sync();
_ = try file.truncate();
}

return 0;
Expand Down
147 changes: 0 additions & 147 deletions flake.lock

This file was deleted.

Loading
Loading