@@ -3,7 +3,7 @@ const builtin = @import("builtin");
33
44const Build = std .Build ;
55
6- const min_zig_string = "0.11 .0" ;
6+ const min_zig_string = "0.12 .0" ;
77const current_zig = builtin .zig_version ;
88
99// NOTE: we should note that when enable tls support we cannot compile with musl
@@ -15,267 +15,140 @@ comptime {
1515 }
1616}
1717
18- pub fn build (b : * Build ) ! void {
19- switch (comptime current_zig .minor ) {
20- 11 = > V0_11 .build (b ),
21- 12 , 13 , 14 = > try V0_12 .build (b ),
22- else = > @compileError ("unknown version!" ),
23- }
24- }
25-
26- /// build for zig 0.12
27- pub const V0_12 = struct {
28- const OptimizeMode = std .builtin .OptimizeMode ;
29- const CrossTarget = std .zig .CrossTarget ;
30- const Compile = Build .Step .Compile ;
31- const Module = Build .Module ;
18+ const log = std .log .scoped (.WebUI );
19+ const OptimizeMode = std .builtin .OptimizeMode ;
20+ const CrossTarget = std .zig .CrossTarget ;
21+ const Compile = Build .Step .Compile ;
22+ const Module = Build .Module ;
3223
33- const log = std .log .scoped (.WebUI );
24+ const default_isStatic = true ;
25+ const default_enableTLS = false ;
3426
35- const default_isStatic = true ;
36- const default_enableTLS = false ;
37-
38- pub fn build (b : * Build ) ! void {
39- const isStatic = b .option (bool , "is_static" , "whether lib is static" ) orelse default_isStatic ;
40- const enableTLS = b .option (bool , "enable_tls" , "whether lib enable tls" ) orelse default_enableTLS ;
41-
42- const target = b .standardTargetOptions (.{});
43- const optimize = b .standardOptimizeOption (.{});
44-
45- if (enableTLS ) {
46- log .info ("enable TLS support" , .{});
47- if (! target .query .isNative ()) {
48- log .info ("when enable tls, not support cross compile" , .{});
49- std .posix .exit (1 );
50- }
51- }
52-
53- // create a options for command paramter
54- const flags_options = b .addOptions ();
55-
56- // add option
57- flags_options .addOption (bool , "enableTLS" , enableTLS );
58-
59- // create a new module for flags options
60- const flags_module = flags_options .createModule ();
27+ pub fn build (b : * Build ) ! void {
28+ const isStatic = b .option (bool , "is_static" , "whether lib is static" ) orelse default_isStatic ;
29+ const enableTLS = b .option (bool , "enable_tls" , "whether lib enable tls" ) orelse default_enableTLS ;
6130
62- const webui = b .dependency ("webui" , .{
63- .target = target ,
64- .optimize = optimize ,
65- .dynamic = ! isStatic ,
66- .@"enable-tls" = enableTLS ,
67- .verbose = .err ,
68- });
31+ const target = b .standardTargetOptions (.{});
32+ const optimize = b .standardOptimizeOption (.{});
6933
70- const webui_module = b .addModule ("webui" , .{
71- .root_source_file = b .path (b .pathJoin (&.{ "src" , "webui.zig" })),
72- .imports = &.{
73- .{
74- .name = "flags" ,
75- .module = flags_module ,
76- },
77- },
78- });
79- webui_module .linkLibrary (webui .artifact ("webui" ));
80- if (! isStatic ) {
81- b .installArtifact (webui .artifact ("webui" ));
34+ if (enableTLS ) {
35+ log .info ("enable TLS support" , .{});
36+ if (! target .query .isNative ()) {
37+ log .info ("when enable tls, not support cross compile" , .{});
38+ std .posix .exit (1 );
8239 }
83-
84- // generate docs
85- generate_docs (b , optimize , target , flags_module );
86-
87- // build examples
88- build_examples_12 (b , optimize , target , webui_module , webui .artifact ("webui" )) catch | err | {
89- log .err ("failed to build examples: {}" , .{err });
90- std .process .exit (1 );
91- };
9240 }
9341
94- fn generate_docs (b : * Build , optimize : OptimizeMode , target : Build.ResolvedTarget , flags_module : * Module ) void {
95- const webui_lib = b .addObject (.{
96- .name = "webui_lib" ,
97- .root_source_file = b .path (b .pathJoin (&.{ "src" , "webui.zig" })),
98- .target = target ,
99- .optimize = optimize ,
100- });
101-
102- webui_lib .root_module .addImport ("flags" , flags_module );
103-
104- const docs_step = b .step ("docs" , "Generate docs" );
105-
106- const docs_install = b .addInstallDirectory (.{
107- .source_dir = webui_lib .getEmittedDocs (),
108- .install_dir = .prefix ,
109- .install_subdir = "docs" ,
110- });
111-
112- docs_step .dependOn (& docs_install .step );
42+ // create a options for command paramter
43+ const flags_options = b .addOptions ();
44+
45+ // add option
46+ flags_options .addOption (bool , "enableTLS" , enableTLS );
47+
48+ // create a new module for flags options
49+ const flags_module = flags_options .createModule ();
50+
51+ const webui = b .dependency ("webui" , .{
52+ .target = target ,
53+ .optimize = optimize ,
54+ .dynamic = ! isStatic ,
55+ .@"enable-tls" = enableTLS ,
56+ .verbose = .err ,
57+ });
58+
59+ const webui_module = b .addModule ("webui" , .{
60+ .root_source_file = b .path (b .pathJoin (&.{ "src" , "webui.zig" })),
61+ .imports = &.{
62+ .{
63+ .name = "flags" ,
64+ .module = flags_module ,
65+ },
66+ },
67+ });
68+ webui_module .linkLibrary (webui .artifact ("webui" ));
69+ if (! isStatic ) {
70+ b .installArtifact (webui .artifact ("webui" ));
11371 }
11472
115- fn build_examples_12 (b : * Build , optimize : OptimizeMode , target : Build.ResolvedTarget , webui_module : * Module , webui_lib : * Compile ) ! void {
116-
117- // we use lazyPath to get absolute path of package
118- var lazy_path = b .path ("examples" );
119-
120- const build_all_step = b .step ("examples" , "build all examples" );
73+ // generate docs
74+ generate_docs (b , optimize , target , flags_module );
12175
122- const examples_path = lazy_path .getPath (b );
123-
124- var iter_dir = try std .fs .openDirAbsolute (examples_path , .{ .iterate = true });
125- defer iter_dir .close ();
126-
127- var itera = iter_dir .iterate ();
128-
129- while (try itera .next ()) | val | {
130- if (val .kind != .directory ) {
131- continue ;
132- }
133-
134- const example_name = val .name ;
135- const path = b .pathJoin (&.{ "examples" , example_name , "main.zig" });
136-
137- const exe = b .addExecutable (.{
138- .name = example_name ,
139- .root_source_file = b .path (path ),
140- .target = target ,
141- .optimize = optimize ,
142- });
143-
144- exe .root_module .addImport ("webui" , webui_module );
145- exe .linkLibrary (webui_lib );
146-
147- const exe_install = b .addInstallArtifact (exe , .{});
148-
149- build_all_step .dependOn (& exe_install .step );
76+ // build examples
77+ build_examples (b , optimize , target , webui_module , webui .artifact ("webui" )) catch | err | {
78+ log .err ("failed to build examples: {}" , .{err });
79+ std .process .exit (1 );
80+ };
81+ }
15082
151- const exe_run = b .addRunArtifact (exe );
152- exe_run .step .dependOn (& exe_install .step );
83+ fn generate_docs (b : * Build , optimize : OptimizeMode , target : Build.ResolvedTarget , flags_module : * Module ) void {
84+ const webui_lib = b .addObject (.{
85+ .name = "webui_lib" ,
86+ .root_source_file = b .path (b .pathJoin (&.{ "src" , "webui.zig" })),
87+ .target = target ,
88+ .optimize = optimize ,
89+ });
15390
154- const cwd = b . path ( b . pathJoin (&.{ "examples " , example_name }) );
91+ webui_lib . root_module . addImport ( "flags " , flags_module );
15592
156- exe_run . setCwd ( cwd );
93+ const docs_step = b . step ( "docs" , "Generate docs" );
15794
158- const step_name = try std .fmt .allocPrint (b .allocator , "run_{s}" , .{example_name });
95+ const docs_install = b .addInstallDirectory (.{
96+ .source_dir = webui_lib .getEmittedDocs (),
97+ .install_dir = .prefix ,
98+ .install_subdir = "docs" ,
99+ });
159100
160- const step_desc = try std .fmt .allocPrint (b .allocator , "run_{s}" , .{example_name });
101+ docs_step .dependOn (& docs_install .step );
102+ }
161103
162- const exe_run_step = b .step (step_name , step_desc );
163- exe_run_step .dependOn (& exe_run .step );
164- }
165- }
166- };
104+ fn build_examples (b : * Build , optimize : OptimizeMode , target : Build.ResolvedTarget , webui_module : * Module , webui_lib : * Compile ) ! void {
167105
168- pub const V0_11 = struct {
169- const OptimizeMode = std .builtin .OptimizeMode ;
170- const CrossTarget = std .zig .CrossTarget ;
171- const Compile = Build .Step .Compile ;
172- const Module = Build .Module ;
106+ // we use lazyPath to get absolute path of package
107+ var lazy_path = b .path ("examples" );
173108
174- const log = std . log . scoped ( .WebUI );
109+ const build_all_step = b . step ( "examples" , "build all examples" );
175110
176- const default_isStatic = true ;
177- const default_enableTLS = false ;
111+ const examples_path = lazy_path .getPath (b );
178112
179- pub fn build (b : * Build ) void {
180- const isStatic = b .option (bool , "is_static" , "whether lib is static" ) orelse default_isStatic ;
181- const enableTLS = b .option (bool , "enable_tls" , "whether lib enable tls" ) orelse default_enableTLS ;
113+ var iter_dir = try std .fs .openDirAbsolute (examples_path , .{ .iterate = true });
114+ defer iter_dir .close ();
182115
183- const target = b .standardTargetOptions (.{});
184- const optimize = b .standardOptimizeOption (.{});
116+ var itera = iter_dir .iterate ();
185117
186- if (enableTLS ) {
187- log .info ("enable TLS support" , .{});
188- if (! target .isNative ()) {
189- log .info ("when enable tls, not support cross compile" , .{});
190- std .os .exit (1 );
191- }
118+ while (try itera .next ()) | val | {
119+ if (val .kind != .directory ) {
120+ continue ;
192121 }
193122
194- // create a options for command paramter
195- const flags_options = b .addOptions ();
196-
197- // add option
198- flags_options .addOption (bool , "enableTLS" , enableTLS );
199-
200- // create a new module for flags options
201- const flags_module = flags_options .createModule ();
202-
203- const webui_module = b .addModule ("webui" , .{
204- .source_file = .{
205- .path = "src/webui.zig" ,
206- },
207- .dependencies = &.{
208- .{
209- .name = "flags" ,
210- .module = flags_module ,
211- },
212- },
213- });
123+ const example_name = val .name ;
124+ const path = b .pathJoin (&.{ "examples" , example_name , "main.zig" });
214125
215- const webui = b .dependency ("webui" , .{
126+ const exe = b .addExecutable (.{
127+ .name = example_name ,
128+ .root_source_file = b .path (path ),
216129 .target = target ,
217130 .optimize = optimize ,
218- .@"enable-tls" = enableTLS ,
219- .dynamic = ! isStatic ,
220- }).artifact ("webui" );
221-
222- b .installArtifact (webui );
223-
224- // build examples
225- build_examples_11 (b , optimize , target , webui_module , webui ) catch | err | {
226- log .err ("failed to build examples: {}" , .{err });
227- std .os .exit (1 );
228- };
229- }
230-
231- fn build_examples_11 (b : * Build , optimize : OptimizeMode , target : CrossTarget , webui_module : * Module , webui_lib : * Compile ) ! void {
232- // we use lazyPath to get absolute path of package
233- var lazy_path = Build.LazyPath {
234- .path = "examples" ,
235- };
236-
237- const build_all_step = b .step ("examples" , "build all examples" );
238-
239- const examples_path = lazy_path .getPath (b );
240- var iter_dir = try std .fs .openIterableDirAbsolute (examples_path , .{});
241- defer iter_dir .close ();
242-
243- var itera = iter_dir .iterate ();
244-
245- while (try itera .next ()) | val | {
246- if (val .kind != .directory ) {
247- continue ;
248- }
249-
250- const example_name = val .name ;
251- const path = try std .fmt .allocPrint (b .allocator , "examples/{s}/main.zig" , .{example_name });
131+ });
252132
253- const exe = b .addExecutable (.{
254- .name = example_name ,
255- .root_source_file = .{ .path = path },
256- .target = target ,
257- .optimize = optimize ,
258- });
133+ exe .root_module .addImport ("webui" , webui_module );
134+ exe .linkLibrary (webui_lib );
259135
260- exe .addModule ("webui" , webui_module );
261- exe .linkLibrary (webui_lib );
136+ const exe_install = b .addInstallArtifact (exe , .{});
262137
263- const exe_install = b . addInstallArtifact ( exe , .{} );
138+ build_all_step . dependOn ( & exe_install . step );
264139
265- build_all_step .dependOn (& exe_install .step );
140+ const exe_run = b .addRunArtifact (exe );
141+ exe_run .step .dependOn (& exe_install .step );
266142
267- const exe_run = b .addRunArtifact (exe );
268- exe_run .step .dependOn (& exe_install .step );
143+ const cwd = b .path (b .pathJoin (&.{ "examples" , example_name }));
269144
270- const cwd = try std .fmt .allocPrint (b .allocator , "{s}/{s}" , .{ examples_path , example_name });
271- exe_run .cwd = cwd ;
145+ exe_run .setCwd (cwd );
272146
273- const step_name = try std .fmt .allocPrint (b .allocator , "run_{s}" , .{example_name });
147+ const step_name = try std .fmt .allocPrint (b .allocator , "run_{s}" , .{example_name });
274148
275- const step_desc = try std .fmt .allocPrint (b .allocator , "run_{s}" , .{example_name });
149+ const step_desc = try std .fmt .allocPrint (b .allocator , "run_{s}" , .{example_name });
276150
277- const exe_run_step = b .step (step_name , step_desc );
278- exe_run_step .dependOn (& exe_run .step );
279- }
151+ const exe_run_step = b .step (step_name , step_desc );
152+ exe_run_step .dependOn (& exe_run .step );
280153 }
281- };
154+ }
0 commit comments