@@ -233,27 +233,50 @@ pub fn build(b: *std.Build) void {
233233 zabi_utils .addImport ("zabi-types" , zabi_types );
234234 }
235235
236+ // Runs the tests or coverage steps.
237+ buildTestOrCoverage (b , target , optimize , zabi );
238+
239+ // Runs the benchmark
240+ buildBenchmark (b , target , optimize , zabi );
241+
242+ // Build and generate docs for zabi. Uses the `doc_comments` spread across the codebase.
243+ // Always build in `ReleaseFast`.
244+ buildDocs (b , target );
245+
246+ // Build the wasm file. Always build in `ReleaseSmall` on `wasm32-freestanding`.
247+ buildWasm (b , zabi );
248+ }
249+
250+ // Builds and runs the main tests of zabi or the coverage from kcov.
251+ fn buildTestOrCoverage (
252+ b : * std.Build ,
253+ target : std.Build.ResolvedTarget ,
254+ optimize : std.builtin.OptimizeMode ,
255+ module : * std.Build.Module ,
256+ ) void {
236257 const load_variables = b .option (bool , "load_variables" , "Load enviroment variables from a \" env\" file." ) orelse false ;
237258 const env_file_path = b .option ([]const u8 , "env_file_path" , "Specify the location of a env variables file" ) orelse ".env" ;
238259
239260 // Builds and runs the main tests of zabi.
240- const lib_unit_tests = b .addTest (.{
241- .name = "zabi-tests" ,
242- .root_source_file = b .path ("tests/root.zig" ),
243- .target = target ,
244- .optimize = optimize ,
245- .test_runner = b .path ("build/test_runner.zig" ),
246- });
247- lib_unit_tests .root_module .addImport ("zabi" , zabi );
248- addDependencies (b , & lib_unit_tests .root_module , target , optimize );
261+ {
262+ const lib_unit_tests = b .addTest (.{
263+ .name = "zabi-tests" ,
264+ .root_source_file = b .path ("tests/root.zig" ),
265+ .target = target ,
266+ .optimize = optimize ,
267+ .test_runner = b .path ("build/test_runner.zig" ),
268+ });
269+ lib_unit_tests .root_module .addImport ("zabi" , module );
270+ addDependencies (b , & lib_unit_tests .root_module , target , optimize );
249271
250- var run_lib_unit_tests = b .addRunArtifact (lib_unit_tests );
272+ var run_lib_unit_tests = b .addRunArtifact (lib_unit_tests );
251273
252- const test_step = b .step ("test" , "Run unit tests" );
253- test_step .dependOn (& run_lib_unit_tests .step );
274+ const test_step = b .step ("test" , "Run unit tests" );
275+ test_step .dependOn (& run_lib_unit_tests .step );
254276
255- if (load_variables )
256- loadVariables (b , env_file_path , run_lib_unit_tests );
277+ if (load_variables )
278+ loadVariables (b , env_file_path , run_lib_unit_tests );
279+ }
257280
258281 // Build and run coverage test runner if `zig build coverage` was ran
259282 {
@@ -264,7 +287,7 @@ pub fn build(b: *std.Build) void {
264287 .optimize = optimize ,
265288 .test_runner = b .path ("build/test_runner.zig" ),
266289 });
267- coverage_lib_unit_tests .root_module .addImport ("zabi" , zabi );
290+ coverage_lib_unit_tests .root_module .addImport ("zabi" , module );
268291 const test_step_coverage = b .step ("coverage" , "Run unit tests with kcov coverage" );
269292
270293 const kcov_collect = std .Build .Step .Run .create (b , "collect coverage" );
@@ -289,16 +312,44 @@ pub fn build(b: *std.Build) void {
289312 });
290313 test_step_coverage .dependOn (& install_coverage .step );
291314 }
315+ }
292316
293- // Runs the benchmark
294- buildBenchmark (b , target , optimize , zabi );
317+ /// Build the wasm binary.
318+ fn buildWasm (b : * std.Build , module : * std.Build.Module ) void {
319+ const wasm_crosstarget : std.Target.Query = .{
320+ .cpu_arch = .wasm32 ,
321+ .os_tag = .freestanding ,
322+ .cpu_model = .{ .explicit = & std .Target .wasm .cpu .mvp },
323+ .cpu_features_add = std .Target .wasm .featureSet (&.{
324+ .atomics ,
325+ .bulk_memory ,
326+ .reference_types ,
327+ .sign_ext ,
328+ }),
329+ };
295330
296- // Build and generate docs for zabi. Uses the `doc_comments` spread across the codebase.
297- // Always build in `ReleaseFast`.
298- buildDocs (b , target );
331+ const wasm = b .addExecutable (.{
332+ .name = "zabi_wasm" ,
333+ .root_source_file = b .path ("src/root_wasm.zig" ),
334+ .target = b .resolveTargetQuery (wasm_crosstarget ),
335+ .optimize = .ReleaseSmall ,
336+ });
337+ wasm .root_module .addImport ("zabi" , module );
299338
300- // Build the wasm file. Always build in `ReleaseSmall` on `wasm32-freestanding.
301- buildWasm (b );
339+ // Browser target
340+ wasm .entry = .disabled ;
341+ wasm .rdynamic = true ;
342+
343+ // Memory defaults.
344+ wasm .initial_memory = 65536 * 32 ;
345+ wasm .max_memory = 65536 * 65336 ;
346+
347+ wasm .root_module .stack_protector = true ;
348+
349+ const wasm_install = b .addInstallArtifact (wasm , .{});
350+ const wasm_step = b .step ("wasm" , "Build wasm library" );
351+
352+ wasm_step .dependOn (& wasm_install .step );
302353}
303354
304355/// Adds zabi project dependencies.
@@ -354,49 +405,6 @@ fn buildDocs(b: *std.Build, target: std.Build.ResolvedTarget) void {
354405 const docs_step = b .step ("docs" , "Generate documentation based on the source code." );
355406 docs_step .dependOn (& docs_run .step );
356407}
357- /// Builds for wasm32-freestanding target.
358- fn buildWasm (b : * std.Build ) void {
359- const wasm_crosstarget : std.Target.Query = .{
360- .cpu_arch = .wasm32 ,
361- .os_tag = .freestanding ,
362- .cpu_model = .{ .explicit = & std .Target .wasm .cpu .mvp },
363- .cpu_features_add = std .Target .wasm .featureSet (&.{
364- // We use this to explicitly request shared memory.
365- .atomics ,
366-
367- // Not explicitly used but compiler could use them if they want.
368- .bulk_memory ,
369- .reference_types ,
370- .sign_ext ,
371- }),
372- };
373-
374- const wasm = b .addExecutable (.{
375- .name = "zabi_wasm" ,
376- .root_source_file = b .path ("src/root_wasm.zig" ),
377- .target = b .resolveTargetQuery (wasm_crosstarget ),
378- .optimize = .ReleaseSmall ,
379- .link_libc = true ,
380- });
381-
382- // Browser target
383- wasm .entry = .disabled ;
384- wasm .rdynamic = true ;
385-
386- // Memory defaults.
387- wasm .initial_memory = 65536 * 32 ;
388- wasm .max_memory = 65536 * 65336 ;
389-
390- wasm .root_module .stack_protector = true ;
391-
392- addDependencies (b , & wasm .root_module , b .resolveTargetQuery (wasm_crosstarget ), .ReleaseSmall );
393-
394- const wasm_install = b .addInstallArtifact (wasm , .{});
395- const wasm_step = b .step ("wasm" , "Build wasm library" );
396-
397- wasm_step .dependOn (& wasm_install .step );
398- }
399-
400408/// Loads enviroment variables from a `.env` file in case they aren't already present.
401409fn loadVariables (b : * std.Build , env_path : []const u8 , exe : * std.Build.Step.Run ) void {
402410 var file = std .fs .cwd ().openFile (env_path , .{}) catch | err |
0 commit comments