-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
164 lines (140 loc) · 4.17 KB
/
build.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
const std = @import("std");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const use_ssl = b.option(bool, "use-ssl", "Enable TLS support (requires OpenSSL)") orelse false;
const enable_examples = b.option(bool, "enable-examples", "Build examples") orelse false;
const enable_tests = b.option(bool, "enable-tests", "Build test suite") orelse false;
const enable_async_tests = b.option(bool, "enable-async-tests", "Enable asynchronous tests") orelse false;
const hiredict_dep = b.dependency("hiredict", .{});
const hiredict_path = hiredict_dep.path(".");
const lib = b.addStaticLibrary(.{
.name = "hiredict",
.target = target,
.optimize = optimize,
.link_libc = true,
});
lib.addCSourceFiles(.{
.root = hiredict_path,
.files = &source_files,
.flags = &CFLAGS,
});
const platform_libs = switch (target.result.os.tag) {
.windows => &[_][]const u8{ "ws2_32", "crypt32" },
.freebsd => &[_][]const u8{"m"},
.solaris => &[_][]const u8{"socket"},
else => &[_][]const u8{},
};
for (platform_libs) |libname| {
lib.linkSystemLibrary(libname);
}
lib.installHeadersDirectory(
hiredict_path,
"",
.{ .include_extensions = &header_files },
);
lib.addIncludePath(hiredict_path);
b.installArtifact(lib);
const ssl_lib = b.addStaticLibrary(.{
.name = "hiredict_ssl",
.target = target,
.optimize = optimize,
.link_libc = true,
});
if (use_ssl) {
ssl_lib.addCSourceFile(.{
.file = hiredict_dep.path("ssl.c"),
.flags = &CFLAGS,
});
ssl_lib.linkSystemLibrary("ssl");
ssl_lib.linkSystemLibrary("crypto");
ssl_lib.installHeadersDirectory(hiredict_path, "", .{
.include_extensions = &.{"hiredict_ssl.h"},
});
b.installArtifact(ssl_lib);
}
if (enable_examples) {
const example = try getHiredictExecutable(
b,
target,
optimize,
hiredict_path,
hiredict_dep.path("examples/example.c"),
"hiredict-example",
);
example.linkLibrary(lib);
b.installArtifact(example);
if (use_ssl) {
const example_ssl = try getHiredictExecutable(
b,
target,
optimize,
hiredict_path,
hiredict_dep.path("examples/example-ssl.c"),
"hiredict-example-ssl",
);
example_ssl.linkLibrary(lib);
example_ssl.linkLibrary(ssl_lib);
b.installArtifact(example_ssl);
}
}
if (enable_tests) {
const test_suite = try getHiredictExecutable(
b,
target,
optimize,
hiredict_path,
hiredict_dep.path("test.c"),
"hiredict-test",
);
if (use_ssl) {
test_suite.linkLibrary(ssl_lib);
test_suite.defineCMacro("HIREDICT_TEST_SSL", "1");
}
if (enable_async_tests) {
test_suite.linkSystemLibrary("event");
test_suite.defineCMacro("HIREDICT_TEST_ASYNC", "1");
}
test_suite.linkLibrary(lib);
b.installArtifact(test_suite);
}
}
fn getHiredictExecutable(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
hiredict_path: std.Build.LazyPath,
sub_path: std.Build.LazyPath,
name: []const u8,
) !*std.Build.Step.Compile {
const exe = b.addExecutable(.{
.name = name,
.target = target,
.optimize = optimize,
.link_libc = true,
});
exe.addCSourceFile(.{
.file = sub_path,
});
exe.addIncludePath(hiredict_path);
return exe;
}
const CFLAGS = .{"-std=c99"};
const source_files = .{
"alloc.c",
"async.c",
"hiredict.c",
"net.c",
"read.c",
"sds.c",
"sockcompat.c",
};
const header_files = .{
"alloc.h",
"async.h",
"hiredict.h",
"net.h",
"read.h",
"sds.h",
"sockcompat.h",
};