@@ -6,79 +6,116 @@ const print = std.debug.print;
66const heap = std .heap ;
77const process = std .process ;
88
9- fn createExecutable (b : * std.Build ,
10- target : std.Build.ResolvedTarget ,
11- optimize : std.builtin.OptimizeMode ,
12- source : []const u8 ) ! void {
9+ fn find (arr : * std .array_list .Managed ([]const u8 ), elem : []const u8 ) bool {
10+ for (arr .items ) | item | if (std .mem .eql (u8 , item , elem )) return true ;
11+ return false ;
12+ }
13+
14+ fn createExecutable (
15+ b : * std.Build ,
16+ module_shadred : * std.Build.Module ,
17+ target : std.Build.ResolvedTarget ,
18+ optimize : std.builtin.OptimizeMode ,
19+ source : []const u8 ,
20+ ) ! void {
1321 const source_filename = std .fs .path .basename (source );
1422 const index = std .mem .lastIndexOfScalar (u8 , source_filename , '.' ) orelse source_filename .len ;
1523 const exe_name = source_filename [0.. index ];
1624 const exe = b .addExecutable (.{
17- .root_source_file = b .path (source ),
1825 .name = exe_name ,
19- .target = target ,
20- .optimize = optimize ,
26+ .root_module = b .createModule (.{
27+ .optimize = optimize ,
28+ .target = target ,
29+ .root_source_file = b .path (source ),
30+ }),
2131 });
22- b .installArtifact (exe );
32+ exe .root_module .addImport ("shared" , module_shadred );
33+
34+ const path = fs .path .dirname (source ) orelse "" ;
35+ const clean_path = try std .mem .replaceOwned (u8 , b .allocator , path , "./src/examples" , "" );
36+ const out_path = try fs .path .join (b .allocator , &.{ "bin" , clean_path });
37+ const full_exe_path = try std .fs .path .join (b .allocator , &.{ out_path , exe_name });
38+ print ("\n → {s}\n " , .{full_exe_path });
39+ const install_artifact = b .addInstallArtifact (exe , .{
40+ .dest_dir = .{
41+ .override = .{
42+ .custom = out_path ,
43+ },
44+ },
45+ });
46+ b .getInstallStep ().dependOn (& install_artifact .step );
2347
2448 const run_cmd = b .addRunArtifact (exe );
2549 run_cmd .step .dependOn (b .getInstallStep ());
2650 if (b .args ) | buildArgs | {
2751 run_cmd .addArgs (buildArgs );
2852 }
2953
30- const run_step_name = try std .fmt .allocPrint (b .allocator , "{s}" , .{ exe_name });
54+ const run_step_name = try std .fmt .allocPrint (b .allocator , "run- {s}" , .{exe_name });
3155 defer b .allocator .free (run_step_name );
3256 const run_step = b .step (run_step_name , "Run the program" );
3357 run_step .dependOn (& run_cmd .step );
3458}
3559
60+ fn parseDir (
61+ b : * std.Build ,
62+ depth : usize ,
63+ path : []const u8 ,
64+ results : * std .array_list .Managed ([]const u8 ),
65+ ) ! void {
66+ const extension = fs .path .extension (path );
67+ if (! std .mem .eql (u8 , extension , "zig" ) and extension .len > 0 ) return ;
68+ const src_dir = fs .cwd ().openDir (path , .{ .iterate = true }) catch | err | {
69+ print ("\n {any}: {s}" , .{ err , path });
70+ return ;
71+ };
72+ var it = src_dir .iterate ();
73+ var i : u32 = 0 ;
74+ while (try it .next ()) | subpath | : (i += 1 ) {
75+ const child_path = try std .fs .path .join (b .allocator , &.{ path , subpath .name });
76+ if (! std .mem .containsAtLeast (u8 , subpath .name , 1 , ".zig" )) {
77+ try parseDir (b , depth + 1 , child_path , results );
78+ continue ;
79+ }
80+ try results .append (child_path );
81+ }
82+ }
83+
3684pub fn build (b : * std.Build ) ! void {
3785 const target = b .standardTargetOptions (.{});
3886 const optimize = b .standardOptimizeOption (.{});
3987
4088 const args = try process .argsAlloc (b .allocator );
4189 defer process .argsFree (b .allocator , args );
4290
43- var examples = std .ArrayList ([]const u8 ).init (b .allocator );
91+ var examples = std .array_list . Managed ([]const u8 ).init (b .allocator );
4492 defer examples .deinit ();
45-
46- print ("\n\n --- Examples ---\n " , .{});
47- const src_dir = try fs .cwd ().openDir ("./src" , .{ .iterate = true });
48- var it = src_dir .iterate ();
49- var i : u32 = 0 ;
50- while (try it .next ()) | path | : (i += 1 ) {
51- if (! std .mem .containsAtLeast (u8 , path .name , 1 , ".zig" )) continue ;
52- const source = try std .fmt .allocPrint (b .allocator , "./src/{s}" , .{ path .name });
53- print ("- {d} {s}\n " , .{ i , source });
54- try examples .append (source );
55- }
56- print ("----------------\n " , .{});
5793
94+ const module_shared = b .addModule ("shared" , .{
95+ .root_source_file = b .path ("src/shared/root.zig" ),
96+ .optimize = optimize ,
97+ .target = target ,
98+ });
5899
59- const single_example_name = args [args .len - 1 ];
60- var index : ? usize = null ;
61- for (0.. examples .items .len ) | j | {
62- if (std .mem .containsAtLeast (u8 , examples .items [j ], 1 , single_example_name )) {
63- index = j ;
64- break ;
65- }
66- }
100+ // print("\n\n--- Examples ---\n", .{});
101+ try parseDir (b , 0 , "./src/examples" , & examples );
102+ // for (examples.items) |example_src| {
103+ // print("- {s}\n", .{example_src});
104+ // }
105+ // print("----------------\n", .{});
67106
68- if (index ) | j | {
69- const example = examples .items [j ];
70- print ("\n Building single example: {s}...\n\n " , .{ example });
71- try createExecutable (b , target , optimize , example );
72- return ;
73- }
74-
75- for (examples .items ) | source_file | {
76- try createExecutable (b , target , optimize , source_file );
107+ const single_example_path = args [args .len - 1 ];
108+ if (find (& examples , single_example_path )) {
109+ print ("Found example: {s}" , .{single_example_path });
110+ try createExecutable (b , module_shared , target , optimize , single_example_path );
111+ } else {
112+ print ("Couldn't find example: {s}" , .{single_example_path });
77113 }
78- // return;
79114
80- // defer b.allocator.free(source);
81- // print("\n...Building {s} {s}\n\n", .{ example_name, source });
82-
83- // try createExecutable(b, source);
115+ if (std .mem .eql (u8 , single_example_path , "all" )) {
116+ for (examples .items ) | example_src | {
117+ print ("\n Building example: {s}" , .{example_src });
118+ try createExecutable (b , module_shared , target , optimize , example_src );
119+ }
120+ }
84121}
0 commit comments