-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuild.zig
More file actions
96 lines (84 loc) · 3.51 KB
/
build.zig
File metadata and controls
96 lines (84 loc) · 3.51 KB
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
const std = @import("std");
// builds ripgrep (Rust) via `cargo zigbuild`, using Zig as the C
// compiler/linker. The Rust sources live in vendor/ripgrep as a git submodule.
//
// Two build steps are exposed:
// - `wasi` — builds only wasm32-wasip1 (default)
// - `native` — cross-compiles all native release targets
// Outputs land in a top-level `dist/rg-<triple>[.exe|.wasm]`.
pub fn build(b: *std.Build) void {
const native_triples = [_][]const u8{
"aarch64-apple-darwin",
"x86_64-unknown-linux-gnu",
"aarch64-unknown-linux-gnu",
"x86_64-pc-windows-gnu",
};
const wasi_triples = [_][]const u8{
"wasm32-wasip1",
};
const native_step = b.step("native", "Cross-compile ripgrep for all native targets");
const wasi_step = b.step("wasi", "Cross-compile ripgrep for wasm32-wasip1");
for (native_triples) |triple| addTarget(b, native_step, triple);
for (wasi_triples) |triple| addTarget(b, wasi_step, triple);
// Default to wasi.
b.default_step = wasi_step;
}
fn addTarget(b: *std.Build, parent: *std.Build.Step, triple: []const u8) void {
const bld = addRipgrepBuild(b, triple);
const dest = b.fmt("rg-{s}{s}", .{ triple, binExt(triple) });
// `../dist` resolves relative to the install prefix (`zig-out`), so
// binaries land in a top-level `dist/` at the repo root.
const install = b.addInstallFileWithDir(bld.rg_path, .{ .custom = "../dist" }, dest);
install.step.dependOn(&bld.cargo.step);
parent.dependOn(&install.step);
}
const RipgrepBuild = struct {
cargo: *std.Build.Step.Run,
rg_path: std.Build.LazyPath,
};
fn addRipgrepBuild(b: *std.Build, rust_triple: []const u8) RipgrepBuild {
// ripgrep's `release-lto` profile (defined in vendor/ripgrep/Cargo.toml)
// layered with size tuning via `--config` to produce the smallest binary.
const profile = "release-lto";
const manifest_path = b.pathJoin(&.{ "vendor", "ripgrep", "Cargo.toml" });
const cargo = b.addSystemCommand(&.{
"cargo",
"zigbuild",
"--manifest-path",
manifest_path,
"--profile",
profile,
"--target",
rust_triple,
"--bin",
"rg",
"--config",
"profile.release-lto.opt-level=\"z\"",
"--config",
"profile.release-lto.debug=false",
"--config",
"profile.release-lto.strip=\"symbols\"",
});
cargo.setName(b.fmt("cargo zigbuild ripgrep [{s}]", .{rust_triple}));
// Keep each target's cargo target-dir separate so parallel/cross builds
// don't thrash each other's fingerprints.
const cache_root = b.cache_root.path orelse ".zig-cache";
const target_dir = b.pathJoin(&.{ cache_root, "cargo-target", rust_triple });
cargo.setEnvironmentVariable("CARGO_TARGET_DIR", target_dir);
// Enable WASM SIMD for wasm targets — unlocks memchr's simd128 vectorized paths.
if (std.mem.startsWith(u8, rust_triple, "wasm")) {
cargo.setEnvironmentVariable("RUSTFLAGS", "-C target-feature=+simd128");
}
const exe_name = b.fmt("rg{s}", .{binExt(rust_triple)});
const rg_path_str = b.pathJoin(&.{ target_dir, rust_triple, profile, exe_name });
return .{
.cargo = cargo,
.rg_path = .{ .cwd_relative = rg_path_str },
};
}
fn binExt(triple: []const u8) []const u8 {
if (std.mem.indexOf(u8, triple, "windows") != null) return ".exe";
if (std.mem.indexOf(u8, triple, "wasi") != null) return ".wasm";
if (std.mem.startsWith(u8, triple, "wasm")) return ".wasm";
return "";
}