@@ -6,18 +6,12 @@ const exe_name = "linuxwave";
66/// Version.
77const 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
2317pub 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 }
0 commit comments