forked from webui-dev/webui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_13.zig
167 lines (132 loc) · 5.7 KB
/
build_13.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const std = @import("std");
const builtin = @import("builtin");
const Build = std.Build;
const OptimizeMode = std.builtin.OptimizeMode;
const CrossTarget = std.zig.CrossTarget;
const Compile = Build.Step.Compile;
const Module = Build.Module;
const log = std.log.scoped(.WebUI);
const default_isStatic = true;
const default_enableTLS = false;
pub fn build_13(b: *Build) void {
const isStatic = b.option(bool, "is_static", "whether lib is static") orelse default_isStatic;
const enableTLS = b.option(bool, "enable_tls", "whether lib enable tls") orelse default_enableTLS;
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
if (enableTLS) {
std.log.info("enable TLS support", .{});
if (!target.query.isNative()) {
std.log.info("when enable tls, not support cross compile", .{});
std.posix.exit(1);
}
}
const webui = build_webui_12(b, optimize, target, isStatic, enableTLS);
webui.installHeader(b.path("include/webui.h"), "webui.h");
build_examples_12(b, optimize, target, webui);
b.installArtifact(webui);
}
fn build_webui_12(b: *Build, optimize: OptimizeMode, target: Build.ResolvedTarget, is_static: bool, enable_tls: bool) *Compile {
const name = "webui";
const webui = if (is_static) b.addStaticLibrary(.{ .name = name, .target = target, .optimize = optimize }) else b.addSharedLibrary(.{ .name = name, .target = target, .optimize = optimize });
const extra_flags = if (target.query.os_tag == .windows or (target.query.os_tag == null and builtin.os.tag == .windows))
"-DMUST_IMPLEMENT_CLOCK_GETTIME"
else
"";
const cflags = if (enable_tls)
[_][]const u8{ "-DNDEBUG", "-DNO_CACHING", "-DNO_CGI", "-DUSE_WEBSOCKET", "-DWEBUI_TLS", "-DNO_SSL_DL", "-DOPENSSL_API_1_1", extra_flags }
else
[_][]const u8{ "-DNDEBUG", "-DNO_CACHING", "-DNO_CGI", "-DUSE_WEBSOCKET", "-DNO_SSL", extra_flags, "", "" };
webui.addCSourceFile(.{
.file = .{ .path = "src/webui.c" },
.flags = if (enable_tls)
&[_][]const u8{ "-DNO_SSL", "-DWEBUI_TLS", "-DNO_SSL_DL", "-DOPENSSL_API_1_1" }
else
&[_][]const u8{"-DNO_SSL"},
});
webui.addCSourceFile(.{
.file = .{ .path = "src/civetweb/civetweb.c" },
.flags = &cflags,
});
webui.linkLibC();
webui.addIncludePath(.{ .path = "include" });
if (target.query.os_tag == .windows or (target.query.os_tag == null and builtin.os.tag == .windows)) {
webui.linkSystemLibrary("ws2_32");
if (enable_tls) {
webui.linkSystemLibrary("bcrypt");
}
}
if (enable_tls) {
webui.linkSystemLibrary("ssl");
webui.linkSystemLibrary("crypto");
}
if (target.query.abi == .msvc) {
webui.linkSystemLibrary("shell32");
webui.linkSystemLibrary("Advapi32");
webui.linkSystemLibrary("user32");
}
return webui;
}
fn build_examples_12(b: *Build, optimize: OptimizeMode, target: Build.ResolvedTarget, webui_lib: *Compile) void {
var lazy_path = Build.LazyPath{
.path = "examples/C",
};
const build_all_step = b.step("build_all", "build all examples");
const examples_path = lazy_path.getPath(b);
var iter_dir =
std.fs.openDirAbsolute(examples_path, .{ .iterate = true }) catch |err| {
log.err("open examples_path failed, err is {}", .{err});
std.posix.exit(1);
};
defer iter_dir.close();
var itera = iter_dir.iterate();
while (itera.next()) |val| {
if (val) |entry| {
if (entry.kind == .directory) {
const example_name = entry.name;
const path = std.fmt.allocPrint(b.allocator, "examples/C/{s}/main.c", .{example_name}) catch |err| {
log.err("fmt path for examples failed, err is {}", .{err});
std.posix.exit(1);
};
const exe = b.addExecutable(.{
.name = example_name,
.target = target,
.optimize = optimize,
});
exe.addCSourceFile(.{
.file = .{
.path = path,
},
.flags = &.{},
});
exe.subsystem = .Windows;
exe.linkLibrary(webui_lib);
const exe_install = b.addInstallArtifact(exe, .{});
build_all_step.dependOn(&exe_install.step);
const exe_run = b.addRunArtifact(exe);
exe_run.step.dependOn(&exe_install.step);
const cwd = std.fmt.allocPrint(b.allocator, "examples/C/{s}", .{example_name}) catch |err| {
log.err("fmt path for examples failed, err is {}", .{err});
std.posix.exit(1);
};
exe_run.setCwd(.{
.path = cwd,
});
const step_name = std.fmt.allocPrint(b.allocator, "run_{s}", .{example_name}) catch |err| {
log.err("fmt step_name for examples failed, err is {}", .{err});
std.posix.exit(1);
};
const step_desc = std.fmt.allocPrint(b.allocator, "run example {s}", .{example_name}) catch |err| {
log.err("fmt step_desc for examples failed, err is {}", .{err});
std.posix.exit(1);
};
const exe_run_step = b.step(step_name, step_desc);
exe_run_step.dependOn(&exe_run.step);
}
} else {
break;
}
} else |err| {
log.err("iterate examples_path failed, err is {}", .{err});
std.posix.exit(1);
}
}