@@ -62,33 +62,40 @@ pub fn build(b: *std.Build) void {
6262
6363 // --- Example Setup ---
6464 const examples_path = "examples" ;
65- var examples_dir = fs .cwd ().openDir (examples_path , .{ .iterate = true }) catch @panic ("Can't open 'examples' directory" );
66- defer examples_dir .close ();
67-
68- var dir_iter = examples_dir .iterate ();
69- while (dir_iter .next () catch @panic ("Failed to iterate examples" )) | entry | {
70- if (! std .mem .endsWith (u8 , entry .name , ".zig" )) continue ;
71-
72- const exe_name = fs .path .stem (entry .name );
73- const exe_path = b .fmt ("{s}/{s}" , .{ examples_path , entry .name });
74-
75- const exe_module = b .createModule (.{
76- .root_source_file = b .path (exe_path ),
77- .target = target ,
78- .optimize = optimize ,
79- });
80- exe_module .addImport ("chilli" , lib_module );
81-
82- const exe = b .addExecutable (.{
83- .name = exe_name ,
84- .root_module = exe_module ,
85- });
86- b .installArtifact (exe );
87-
88- const run_cmd = b .addRunArtifact (exe );
89- const run_step_name = b .fmt ("run-{s}" , .{exe_name });
90- const run_step_desc = b .fmt ("Run the {s} example" , .{exe_name });
91- const run_step = b .step (run_step_name , run_step_desc );
92- run_step .dependOn (& run_cmd .step );
65+ examples_blk : {
66+ // If the examples directory isn't present (common when used as a dependency),
67+ // skip setting up example artifacts instead of panicking.
68+ var examples_dir = fs .cwd ().openDir (examples_path , .{ .iterate = true }) catch | err | {
69+ if (err == error .FileNotFound or err == error .NotDir ) break :examples_blk ;
70+ @panic ("Can't open 'examples' directory" );
71+ };
72+ defer examples_dir .close ();
73+
74+ var dir_iter = examples_dir .iterate ();
75+ while (dir_iter .next () catch @panic ("Failed to iterate examples" )) | entry | {
76+ if (! std .mem .endsWith (u8 , entry .name , ".zig" )) continue ;
77+
78+ const exe_name = fs .path .stem (entry .name );
79+ const exe_path = b .fmt ("{s}/{s}" , .{ examples_path , entry .name });
80+
81+ const exe_module = b .createModule (.{
82+ .root_source_file = b .path (exe_path ),
83+ .target = target ,
84+ .optimize = optimize ,
85+ });
86+ exe_module .addImport ("chilli" , lib_module );
87+
88+ const exe = b .addExecutable (.{
89+ .name = exe_name ,
90+ .root_module = exe_module ,
91+ });
92+ b .installArtifact (exe );
93+
94+ const run_cmd = b .addRunArtifact (exe );
95+ const run_step_name = b .fmt ("run-{s}" , .{exe_name });
96+ const run_step_desc = b .fmt ("Run the {s} example" , .{exe_name });
97+ const run_step = b .step (run_step_name , run_step_desc );
98+ run_step .dependOn (& run_cmd .step );
99+ }
93100 }
94101}
0 commit comments