forked from fmtlib/fmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
233 lines (222 loc) · 6.44 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//! fmtlib for Zig Package Manager (MVP)
//! Download [Zig v0.11 or higher](https://ziglang.org/download)
const std = @import("std");
const Path = std.Build.LazyPath;
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Options
const shared = b.option(bool, "Shared", "Build the Shared Library [default: false]") orelse false;
const tests = b.option(bool, "Tests", "Build tests [default: false]") orelse false;
const lib = if (shared) b.addSharedLibrary(.{
.name = "fmt",
.target = target,
.optimize = optimize,
.version = .{
.major = 11,
.minor = 0,
.patch = 0,
},
}) else b.addStaticLibrary(.{
.name = "fmt",
.target = target,
.optimize = optimize,
});
lib.addCSourceFiles(.{
.files = src,
.flags = &.{
"-Wall",
"-Wextra",
},
});
lib.addIncludePath(b.path("include"));
if (optimize == .Debug or optimize == .ReleaseSafe)
lib.bundle_compiler_rt = true
else
lib.root_module.strip = true;
if (lib.linkage == .static)
lib.pie = true;
if (lib.rootModuleTarget().abi != .msvc)
lib.linkLibCpp() // static-linking LLVM-libcxx (all platforms)
else
lib.linkLibC();
lib.installHeadersDirectory(b.path("include"), "", .{});
b.installArtifact(lib);
if (tests) {
buildTest(b, .{
.optimize = optimize,
.target = target,
.lib = lib,
.path = "test/args-test.cc",
});
buildTest(b, .{
.optimize = optimize,
.target = target,
.lib = lib,
.path = "test/base-test.cc",
});
buildTest(b, .{
.optimize = optimize,
.target = target,
.lib = lib,
.path = "test/unicode-test.cc",
});
buildTest(b, .{
.optimize = optimize,
.target = target,
.lib = lib,
.path = "test/assert-test.cc",
});
buildTest(b, .{
.optimize = optimize,
.target = target,
.lib = lib,
.path = "test/std-test.cc",
});
buildTest(b, .{
.optimize = optimize,
.target = target,
.lib = lib,
.path = "test/xchar-test.cc",
});
buildTest(b, .{
.optimize = optimize,
.target = target,
.lib = lib,
.path = "test/ostream-test.cc",
});
buildTest(b, .{
.optimize = optimize,
.target = target,
.lib = lib,
.path = "test/printf-test.cc",
});
// buildTest(b, .{
// .optimize = optimize,
// .target = target,
// .lib = lib,
// .path = "test/scan-test.cc",
// });
buildTest(b, .{
.optimize = optimize,
.target = target,
.lib = lib,
.path = "test/ranges-test.cc",
});
buildTest(b, .{
.optimize = optimize,
.target = target,
.lib = lib,
.path = "test/color-test.cc",
});
buildTest(b, .{
.optimize = optimize,
.target = target,
.lib = lib,
.path = "test/chrono-test.cc",
});
buildTest(b, .{
.optimize = optimize,
.target = target,
.lib = lib,
.path = "test/compile-test.cc",
});
buildTest(b, .{
.optimize = optimize,
.target = target,
.lib = lib,
.path = "test/compile-fp-test.cc",
});
buildTest(b, .{
.optimize = optimize,
.target = target,
.lib = lib,
.path = "test/format-test.cc",
});
buildTest(b, .{
.optimize = optimize,
.target = target,
.lib = lib,
.path = "test/os-test.cc",
});
buildTest(b, .{
.optimize = optimize,
.target = target,
.lib = lib,
.path = "test/noexception-test.cc",
});
buildTest(b, .{
.optimize = optimize,
.target = target,
.lib = lib,
.path = "test/posix-mock-test.cc",
});
// don't work
// buildTest(b, .{
// .optimize = optimize,
// .target = target,
// .lib = lib,
// .path = "test/module-test.cc",
// });
}
}
fn buildTest(b: *std.Build, info: BuildInfo) void {
const test_exe = b.addExecutable(.{
.name = info.filename(),
.optimize = info.optimize,
.target = info.target,
});
test_exe.addIncludePath(b.path("include"));
test_exe.addIncludePath(b.path("test"));
test_exe.addIncludePath(b.path("test/gtest"));
test_exe.addCSourceFile(.{
.file = b.path(info.path),
.flags = &.{},
});
test_exe.addCSourceFiles(.{
.files = test_src,
.flags = &.{
"-Wall",
"-Wextra",
"-Wno-deprecated-declarations",
},
});
test_exe.defineCMacro("_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING", "1");
test_exe.defineCMacro("GTEST_HAS_PTHREAD", "0");
test_exe.linkLibrary(info.lib);
if (test_exe.rootModuleTarget().abi != .msvc)
test_exe.linkLibCpp()
else
test_exe.linkLibC();
b.installArtifact(test_exe);
const run_cmd = b.addRunArtifact(test_exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step(
b.fmt("{s}", .{info.filename()}),
b.fmt("Run the {s} test", .{info.filename()}),
);
run_step.dependOn(&run_cmd.step);
}
const src: []const []const u8 = &.{
"src/format.cc",
"src/os.cc",
};
const test_src: []const []const u8 = &.{
"test/gtest/gmock-gtest-all.cc",
"test/gtest-extra.cc",
"test/enforce-checks-test.cc",
"test/util.cc",
};
const BuildInfo = struct {
lib: *std.Build.Step.Compile,
optimize: std.builtin.OptimizeMode,
target: std.Build.ResolvedTarget,
path: []const u8,
fn filename(self: BuildInfo) []const u8 {
var split = std.mem.splitSequence(u8, std.fs.path.basename(self.path), ".");
return split.first();
}
};