Skip to content

Commit d960783

Browse files
chore(build): switch build structure to working on base modules & simplify imports (#41)
1 parent 5bc4794 commit d960783

File tree

4 files changed

+28
-27
lines changed

4 files changed

+28
-27
lines changed

build.zig

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,12 @@ const exe_name = "linuxwave";
66
/// Version.
77
const version = "0.3.0"; // managed by release.sh
88

9-
/// Adds the required packages to the given executable.
9+
/// Adds the required packages to the given module.
1010
///
1111
/// This is used for providing the dependencies for main executable as well as the tests.
12-
fn addPackages(b: *std.Build, exe: *std.Build.Step.Compile) !void {
12+
fn addPackages(b: *std.Build, mod: *std.Build.Module) !void {
1313
const clap = b.dependency("clap", .{}).module("clap");
14-
exe.root_module.addImport("clap", clap);
15-
for ([_][]const u8{ "file", "gen", "wav" }) |package| {
16-
const path = b.fmt("src/{s}.zig", .{package});
17-
exe.root_module.addImport(package, b.createModule(.{
18-
.root_source_file = b.path(path),
19-
}));
20-
}
14+
mod.addImport("clap", clap);
2115
}
2216

2317
pub fn build(b: *std.Build) void {
@@ -31,18 +25,25 @@ pub fn build(b: *std.Build) void {
3125
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
3226
const optimize = b.standardOptimizeOption(.{});
3327

28+
const root = b.createModule(.{
29+
.root_source_file = b.path("src/main.zig"),
30+
.target = target,
31+
.optimize = optimize,
32+
});
33+
3434
// Add custom options.
3535
const pie = b.option(bool, "pie", "Build a Position Independent Executable") orelse true;
3636
const relro = b.option(bool, "relro", "Force all relocations to be read-only after processing") orelse true;
3737
const coverage = b.option(bool, "test-coverage", "Generate test coverage") orelse false;
3838
const documentation = b.option(bool, "docs", "Generate documentation") orelse false;
3939

40+
// Add packages.
41+
try addPackages(b, root);
42+
4043
// Add main executable.
4144
const exe = b.addExecutable(.{
4245
.name = exe_name,
43-
.root_source_file = b.path("src/main.zig"),
44-
.target = target,
45-
.optimize = optimize,
46+
.root_module = root,
4647
});
4748
if (documentation) {
4849
const install_docs = b.addInstallDirectory(.{
@@ -56,12 +57,9 @@ pub fn build(b: *std.Build) void {
5657
exe.link_z_relro = relro;
5758
b.installArtifact(exe);
5859

59-
// Add packages.
60-
try addPackages(b, exe);
61-
6260
// Add executable options.
6361
const exe_options = b.addOptions();
64-
exe.root_module.addOptions("build_options", exe_options);
62+
root.addOptions("build_options", exe_options);
6563
exe_options.addOption([]const u8, "version", version);
6664
exe_options.addOption([]const u8, "exe_name", exe_name);
6765

@@ -80,22 +78,25 @@ pub fn build(b: *std.Build) void {
8078
const test_step = b.step("test", "Run tests");
8179
for ([_][]const u8{ "main", "file", "gen", "wav" }) |module| {
8280
const test_name = b.fmt("{s}-tests", .{module});
83-
const test_module = b.fmt("src/{s}.zig", .{module});
84-
var exe_tests = b.addTest(.{
85-
.name = test_name,
86-
.root_source_file = b.path(test_module),
81+
const test_filepath = b.fmt("src/{s}.zig", .{module});
82+
const test_module = b.createModule(.{
83+
.root_source_file = b.path(test_filepath),
8784
.target = target,
8885
.optimize = optimize,
8986
});
87+
try addPackages(b, test_module);
88+
var exe_tests = b.addTest(.{
89+
.name = test_name,
90+
.root_module = test_module,
91+
});
9092
if (coverage) {
9193
exe_tests.setExecCmd(&[_]?[]const u8{
9294
"kcov",
9395
"kcov-output",
9496
null,
9597
});
9698
}
97-
try addPackages(b, exe_tests);
98-
exe_tests.root_module.addOptions("build_options", exe_options);
99+
test_module.addOptions("build_options", exe_options);
99100
const run_unit_tests = b.addRunArtifact(exe_tests);
100101
test_step.dependOn(&run_unit_tests.step);
101102
}

src/args.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const wav = @import("wav");
1+
const wav = @import("wav.zig");
22
const clap = @import("clap");
33

44
// Banner text.

src/defaults.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const wav = @import("wav");
1+
const wav = @import("wav.zig");
22

33
// Default input file.
44
pub const input = "/dev/urandom";

src/main.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const std = @import("std");
2-
const wav = @import("wav");
3-
const gen = @import("gen");
4-
const file = @import("file");
2+
const wav = @import("wav.zig");
3+
const gen = @import("gen.zig");
4+
const file = @import("file.zig");
55
const args = @import("args.zig");
66
const defaults = @import("defaults.zig");
77
const build_options = @import("build_options");

0 commit comments

Comments
 (0)