-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.zig
294 lines (238 loc) · 10.4 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// SPDX-License-Identifier: 0BSD
const std = @import("std");
// TODO: https://github.com/ziglang/zig/issues/14531
const version = "0.1.0-dev";
pub fn build(b: *std.Build) anyerror!void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const disable_aro = b.option(bool, "disable-aro", "Disable Aro C compiler integration") orelse false;
const disable_ffi = b.option(bool, "disable-ffi", "Disable libffi interpreter integration") orelse false;
// TODO: https://github.com/ziglang/zig/issues/15373
const pandoc_prog = b.findProgram(&.{"pandoc"}, &.{}) catch @panic("Could not locate `pandoc` program.");
const install_tls = b.getInstallStep();
const check_tls = b.step("check", "Run source code and documentation checks");
const fmt_tls = b.step("fmt", "Fix source code formatting");
const test_tls = b.step("test", "Build and run tests");
const vscode_tls = b.step("vscode", "Build VS Code extension");
const install_vscode_tls = b.step("install-vscode", "Install VS Code extension");
const uninstall_vscode_tls = b.step("uninstall-vscode", "Uninstall VS Code extension");
const npm_install_doc_step = b.addSystemCommand(&.{ "npm", "install" });
npm_install_doc_step.setName("npm install");
const npm_exec_markdownlint_cli2_doc_step = b.addSystemCommand(&.{ "npm", "exec", "markdownlint-cli2" });
npm_exec_markdownlint_cli2_doc_step.setName("npm exec markdownlint-cli2");
npm_exec_markdownlint_cli2_doc_step.step.dependOn(&npm_install_doc_step.step);
check_tls.dependOn(&npm_exec_markdownlint_cli2_doc_step.step);
inline for (.{ npm_install_doc_step, npm_exec_markdownlint_cli2_doc_step }) |step| {
step.setCwd(b.path("doc"));
}
const npm_install_vscode_step = b.addSystemCommand(&.{ "npm", "install" });
npm_install_vscode_step.setName("npm install");
const npm_run_build_vscode = b.addSystemCommand(&.{ "npm", "run", "build" });
npm_run_build_vscode.setName("npm run build");
npm_run_build_vscode.step.dependOn(&npm_install_vscode_step.step);
vscode_tls.dependOn(&npm_run_build_vscode.step);
inline for (.{ npm_install_vscode_step, npm_run_build_vscode }) |step| {
step.setCwd(b.path(b.pathJoin(&.{ "sup", "vscode" })));
}
const code_install_extension_step = b.addSystemCommand(
&.{ "code", "--install-extension", b.pathJoin(&.{ "vscode", b.fmt("graf-{s}.vsix", .{version}) }) },
);
code_install_extension_step.step.dependOn(&npm_run_build_vscode.step);
install_vscode_tls.dependOn(&code_install_extension_step.step);
const code_uninstall_extension_step = b.addSystemCommand(&.{ "code", "--uninstall-extension", "vezel.graf" });
uninstall_vscode_tls.dependOn(&code_uninstall_extension_step.step);
inline for (.{
npm_install_doc_step,
npm_exec_markdownlint_cli2_doc_step,
npm_install_vscode_step,
npm_run_build_vscode,
code_install_extension_step,
code_uninstall_extension_step,
}) |step| {
step.expectExitCode(0);
// Setting an expected exit code will cause these steps to be cached; ensure that they always run.
step.has_side_effects = true;
}
const fmt_paths = &[_][]const u8{
"bin",
"chk",
"lib",
"build.zig",
"build.zig.zon",
};
check_tls.dependOn(&b.addFmt(.{
.paths = fmt_paths,
.check = true,
}).step);
fmt_tls.dependOn(&b.addFmt(.{
.paths = fmt_paths,
}).step);
const graf_mod = b.addModule("graf", .{
.root_source_file = b.path(b.pathJoin(&.{ "lib", "graf.zig" })),
.target = target,
.optimize = optimize,
// Avoid adding opinionated build options to the module itself as those will be forced on third-party users.
});
graf_mod.addImport("mecha", b.dependency("mecha", .{
.target = target,
.optimize = optimize,
}).module("mecha"));
if (!disable_aro) {
// TODO: Aro should be a lazy dependency, but this causes HTTP problems on macOS.
const aro_dep = b.dependency("aro", .{
.target = target,
.optimize = optimize,
});
graf_mod.addImport("aro", aro_dep.module("aro"));
// TODO: These headers should also be installed for third-party users somehow.
b.installDirectory(.{
.source_dir = aro_dep.path("include"),
.install_dir = .header,
.install_subdir = b.pathJoin(&.{ "graf", "aro" }),
});
}
const t = target.result;
if (!disable_ffi) {
if (b.systemIntegrationOption("ffi", .{})) {
graf_mod.linkSystemLibrary("ffi", .{});
} else {
// TODO: https://github.com/ziglang/zig/issues/20361
const libffi_works = !t.isDarwin() and switch (t.cpu.arch) {
// libffi only supports MSVC for Windows on Arm.
.aarch64, .aarch64_be => t.os.tag != .windows,
// TODO: https://github.com/ziglang/zig/issues/10411
.arm, .armeb => t.getFloatAbi() != .soft and t.os.tag != .windows,
// TODO: https://github.com/llvm/llvm-project/issues/58377
.mips, .mipsel, .mips64, .mips64el => false,
// TODO: https://github.com/ziglang/zig/issues/20376
.powerpc, .powerpcle => !t.isGnuLibC(),
// TODO: https://github.com/ziglang/zig/issues/19107
.riscv32, .riscv64 => !t.isGnuLibC(),
else => true,
};
if (libffi_works) {
// TODO: libffi should be a lazy dependency, but this causes HTTP problems on macOS.
graf_mod.linkLibrary(b.dependency("ffi", .{
.target = target,
.optimize = optimize,
}).artifact("ffi"));
}
}
}
const stlib_step = b.addStaticLibrary(.{
// Avoid name clash with the DLL import library on Windows.
.name = if (t.os.tag == .windows) "libgraf" else "graf",
.root_source_file = b.path(b.pathJoin(&.{ "lib", "c.zig" })),
.target = target,
.optimize = optimize,
.strip = optimize != .Debug,
});
const shlib_step = b.addSharedLibrary(.{
.name = "graf",
.root_source_file = b.path(b.pathJoin(&.{ "lib", "c.zig" })),
.target = target,
.optimize = optimize,
.strip = optimize != .Debug,
});
// On Linux, undefined symbols are allowed in shared libraries by default; override that.
shlib_step.linker_allow_shlib_undefined = false;
inline for (.{ stlib_step, shlib_step }) |step| {
step.installHeadersDirectory(b.path("inc"), "graf", .{});
b.installArtifact(step);
}
install_tls.dependOn(&b.addInstallLibFile(b.addWriteFiles().add("libgraf.pc", b.fmt(
\\prefix=${{pcfiledir}}/../..
\\exec_prefix=${{prefix}}
\\includedir=${{prefix}}/include/graf
\\libdir=${{prefix}}/lib
\\
\\Name: Graf
\\Description: A graph-oriented intermediate representation, optimization framework, and machine code generator.
\\URL: https://docs.vezel.dev/graf
\\Version: {s}
\\
\\Cflags: -I${{includedir}}
\\Libs: -L${{libdir}} -lgraf
, .{version})), b.pathJoin(&.{ "pkgconfig", "libgraf.pc" })).step);
const clap_mod = b.dependency("clap", .{
.target = target,
.optimize = optimize,
}).module("clap");
inline for (.{
"as",
"cc",
"chk",
"dis",
"dot",
"fmt",
"ld",
"mc",
"opt",
"run",
}) |name| {
if (!disable_aro or !std.mem.eql(u8, name, "cc")) {
const bin_name = b.fmt("gc-{s}", .{name});
const exe_step = b.addExecutable(.{
.name = bin_name,
.root_source_file = b.path(b.pathJoin(&.{ "bin", name, "main.zig" })),
.target = target,
.optimize = optimize,
.strip = optimize != .Debug,
});
// PIE is off by default; enable it for hardening purposes.
exe_step.pie = switch (t.cpu.arch) {
// TODO: https://github.com/ziglang/zig/issues/20305
.mips, .mipsel, .mips64, .mips64el => false,
.powerpc, .powerpcle, .powerpc64, .powerpc64le => false,
// TODO: https://github.com/ziglang/zig/issues/20306
.riscv64 => false,
else => true,
};
const exe_mod = &exe_step.root_module;
exe_mod.addImport("graf", graf_mod);
exe_mod.addImport("clap", clap_mod);
b.installArtifact(exe_step);
const run_exe_step = b.addRunArtifact(exe_step);
if (b.args) |args| {
run_exe_step.addArgs(args);
}
b.step(bin_name, b.fmt("Build and run `{s}` (pass arguments with `-- <args>`)", .{bin_name}))
.dependOn(&run_exe_step.step);
const pandoc_step = b.addSystemCommand(&.{
pandoc_prog,
"--standalone",
"--fail-if-warnings",
"--shift-heading-level-by=-1",
"-M",
b.fmt("title={s}", .{bin_name}),
"-M",
"author=Vezel Contributors",
"-V",
"section=1",
"-V",
"header=Graf",
"-V",
b.fmt("footer={s}", .{version}),
});
pandoc_step.addFileArg(b.path(b.pathJoin(&.{ "doc", "tools", b.fmt("{s}.md", .{name}) })));
const man_basename = b.fmt("{s}.1", .{bin_name});
const man_path = pandoc_step.addPrefixedOutputFileArg("-o", man_basename);
pandoc_step.expectExitCode(0);
install_tls.dependOn(&b.addInstallFile(
man_path,
b.pathJoin(&.{ "share", "man", "man1", man_basename }),
).step);
}
}
const run_test_step = b.addRunArtifact(b.addTest(.{
.name = "graf-test",
.root_source_file = b.path(b.pathJoin(&.{ "lib", "graf.zig" })),
.target = target,
.optimize = optimize,
}));
// Always run tests when requested, even if the binary has not changed.
run_test_step.has_side_effects = true;
test_tls.dependOn(&run_test_step.step);
// TODO: Add tests based on CLI invocation.
// TODO: Add a fuzz step based on AFL++.
}