Skip to content
Open
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
68 changes: 42 additions & 26 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,47 +56,59 @@ pub fn build(b: *std.Build) !void {
&.{ "/D _CRT_SECURE_NO_WARNINGS", "/WX" }
else
&.{"-Wall -Wextra -Werror"};
const allocator = std.heap.smp_allocator;
var threaded: std.Io.Threaded = .init(allocator, .{
.environ = std.process.Environ.empty,
});

const io = threaded.io();

const soem_sources = try globFiles(
b.path("soem"),
std.heap.smp_allocator,
allocator,
io,
.{ .extension = "c" },
);
defer std.heap.smp_allocator.free(soem_sources);
defer allocator.free(soem_sources);
const soem_headers = try globFiles(
b.path("soem"),
std.heap.smp_allocator,
allocator,
io,
.{ .extension = "h" },
);
defer std.heap.smp_allocator.free(soem_headers);
defer allocator.free(soem_headers);

const osal_path = b.path("osal").path(b, os_subdir);
const osal_sources = try globFiles(
osal_path,
std.heap.smp_allocator,
allocator,
io,
.{ .extension = "c" },
);
defer std.heap.smp_allocator.free(osal_sources);
defer allocator.free(osal_sources);
const osal_headers = try globFiles(
osal_path,
std.heap.smp_allocator,
allocator,
io,
.{ .extension = "h" },
);
defer std.heap.smp_allocator.free(osal_headers);
defer allocator.free(osal_headers);

const oshw_path = b.path("oshw").path(b, os_subdir);
const oshw_sources = try globFiles(
oshw_path,
std.heap.smp_allocator,
allocator,
io,
.{ .extension = "c" },
);
defer std.heap.smp_allocator.free(oshw_sources);
defer allocator.free(oshw_sources);
const oshw_headers = try globFiles(
oshw_path,
std.heap.smp_allocator,
allocator,
io,
.{ .extension = "h" },
);
defer std.heap.smp_allocator.free(oshw_headers);
defer allocator.free(oshw_headers);

lib_mod.addCSourceFiles(.{
.root = b.path("soem"),
Expand Down Expand Up @@ -179,12 +191,12 @@ const GlobOptions = struct {
recursive: bool = false,
};

fn countFiles(dir: std.fs.Dir, options: GlobOptions) !usize {
fn countFiles(io: std.Io, dir: std.Io.Dir, options: GlobOptions) !usize {
var walker = dir.iterate();
var total_count: usize = 0;

// Walk once to get total file count.
while (try walker.next()) |entry| {
while (try walker.next(io)) |entry| {
if (entry.kind == .file) {
if (options.extension.len > 0) {
const extension =
Expand All @@ -197,17 +209,18 @@ fn countFiles(dir: std.fs.Dir, options: GlobOptions) !usize {
total_count += 1;
}
} else if (entry.kind == .directory and options.recursive) {
var sub_dir = try dir.openDir(entry.name, .{ .iterate = true });
defer sub_dir.close();
total_count += try countFiles(sub_dir, options);
var sub_dir = try dir.openDir(io, entry.name, .{ .iterate = true });
defer sub_dir.close(io);
total_count += try countFiles(io, sub_dir, options);
}
}

return total_count;
}

fn globInner(
dir: std.fs.Dir,
io: std.Io,
dir: std.Io.Dir,
allocator: std.mem.Allocator,
results: [][]u8,
filled_: usize,
Expand All @@ -217,7 +230,7 @@ fn globInner(
var filled = filled_;

// Walk once to get total file count.
while (try walker.next()) |entry| {
while (try walker.next(io)) |entry| {
if (entry.kind == .file) {
if (options.extension.len > 0) {
const extension =
Expand All @@ -232,40 +245,43 @@ fn globInner(
filled += 1;
}
} else if (entry.kind == .directory and options.recursive) {
var sub_dir = try dir.openDir(entry.name, .{ .iterate = true });
defer sub_dir.close();
var sub_dir = try dir.openDir(io, entry.name, .{ .iterate = true });
defer sub_dir.close(io);

try globInner(sub_dir, allocator, results, filled, options);
try globInner(io, sub_dir, allocator, results, filled, options);
}
}
}

fn globFiles(
path: std.Build.LazyPath,
allocator: std.mem.Allocator,
io: std.Io,
options: GlobOptions,
) ![]const []const u8 {
var dir = switch (path) {
.cwd_relative => try std.fs.cwd().openDir(
.cwd_relative => try std.Io.Dir.cwd().openDir(
io,
path.src_path.sub_path,
.{ .iterate = true },
),
.src_path => |sp| try sp.owner.build_root.handle.openDir(
io,
sp.sub_path,
.{ .iterate = true },
),
else => unreachable,
};
defer dir.close();
defer dir.close(io);

const total_count = try countFiles(dir, options);
const total_count = try countFiles(io, dir, options);

const result: [][]u8 = try allocator.alloc([]u8, total_count);
for (result) |*file| {
file.* = &.{};
}

try globInner(dir, allocator, result, 0, options);
try globInner(io, dir, allocator, result, 0, options);

return result;
}
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.{
.name = .soem,
.fingerprint = 0x30ccde7443fac184,
.minimum_zig_version = "0.14.0",
.minimum_zig_version = "0.16.0-dev.2187+e2338edb4",
.version = "1.4.0",
.paths = .{
"soem",
Expand Down