Skip to content

Commit 865a218

Browse files
committed
build: remove std_extras.fetch and just use lazyDependency instead
It was a lot of extra code for no reason - we can just utilise Zig's dependency system. This also fixes pluginval not working on macOS due to the App bundle structure not being copied by fetch and so the binary couldn't find the bundle structured it needed.
1 parent 93e8434 commit 865a218

File tree

3 files changed

+66
-119
lines changed

3 files changed

+66
-119
lines changed

build.zig

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2761,33 +2761,23 @@ fn doTarget(
27612761
&.{if (target.os.tag != .windows) "clap-validator" else "clap-validator.exe"},
27622762
&[0][]const u8{},
27632763
) catch null) |program| {
2764-
run.addArg(program);
2764+
run.addArg(program); // Use system-installed clap-validator.
27652765
} else if (target.os.tag == .windows) {
2766-
run.addFileArg(std_extras.fetch(b, .{
2767-
.url = "https://github.com/free-audio/clap-validator/releases/download/0.3.2/clap-validator-0.3.2-windows.zip",
2768-
.file_name = "clap-validator.exe",
2769-
.hash = "N-V-__8AAACYMwAKpkDTKEWrhJhUyBs1LxycLWN8iFpe5p6r",
2770-
}));
2766+
if (b.lazyDependency("clap_validator_windows", .{})) |dep| {
2767+
run.addFileArg(dep.path("clap-validator.exe"));
2768+
}
27712769
} else if (target.os.tag == .macos) {
2772-
// Use downloaded binary for macOS
2773-
const clap_validator_fetch = std_extras.fetch(b, .{
2774-
.url = "https://github.com/free-audio/clap-validator/releases/download/0.3.2/clap-validator-0.3.2-macos-universal.tar.gz",
2775-
.file_name = "clap-validator",
2776-
.hash = "N-V-__8AALwZfgBlaKnVwge3d221LJA9s_vQixy9c6OBvGhQ",
2777-
.executable = true,
2778-
});
2779-
run.addFileArg(clap_validator_fetch);
2770+
if (b.lazyDependency("clap_validator_macos", .{})) |dep| {
2771+
const bin_path = dep.path("clap-validator");
2772+
run.addFileArg(bin_path);
2773+
run.step.dependOn(&chmodExeStep(b, bin_path).step);
2774+
}
27802775
} else if (target.os.tag == .linux) {
2781-
// Linux - use downloaded binary.
2782-
// NOTE: we're using floe-audio repo with a re-uploaded ZIP because we needed to workaround a
2783-
// zig fetch bug with tar.gz files.
2784-
const clap_validator_fetch = std_extras.fetch(b, .{
2785-
.url = "https://github.com/floe-audio/clap-validator/releases/download/v0.3.2/clap-validator-0.3.2-ubuntu-18.04.zip",
2786-
.file_name = "clap-validator",
2787-
.hash = "N-V-__8AAFDvhAD7wsMQHzT9s_hiRLUTXJp4mBwyx_O7gZxZ",
2788-
.executable = true,
2789-
});
2790-
run.addFileArg(clap_validator_fetch);
2776+
if (b.lazyDependency("clap_validator_linux", .{})) |dep| {
2777+
const bin_path = dep.path("clap-validator");
2778+
run.addFileArg(bin_path);
2779+
run.step.dependOn(&chmodExeStep(b, bin_path).step);
2780+
}
27912781
} else {
27922782
@panic("Unsupported OS for clap-validator");
27932783
}
@@ -2873,40 +2863,36 @@ fn addRunScript(
28732863
top_level_step.dependOn(&run_step.step);
28742864
}
28752865

2866+
fn chmodExeStep(b: *std.Build, path: std.Build.LazyPath) *std.Build.Step.Run {
2867+
const mod = b.addSystemCommand(&.{ "chmod", "+x" });
2868+
mod.addFileArg(path);
2869+
return mod;
2870+
}
2871+
28762872
fn addPluginvalCommand(run: *std.Build.Step.Run, target: std.Target) void {
28772873
const b = run.step.owner;
28782874

28792875
if (b.findProgram(
28802876
&.{if (target.os.tag != .windows) "pluginval" else "pluginval.exe"},
28812877
&[0][]const u8{},
28822878
) catch null) |program| {
2883-
// Use system-installed pluginval when explicitly requested
2884-
run.addArg(program);
2879+
run.addArg(program); // We found a system installation.
28852880
} else if (target.os.tag == .windows) {
2886-
// On Windows, we use a downloaded binary.
2887-
run.addFileArg(std_extras.fetch(b, .{
2888-
.url = "https://github.com/Tracktion/pluginval/releases/download/v1.0.3/pluginval_Windows.zip",
2889-
.file_name = "pluginval.exe",
2890-
.hash = "N-V-__8AAABcNACEKUY1SsEfHGFybDSKUo4JGhYN5bgZ146c",
2891-
}));
2881+
if (b.lazyDependency("pluginval_windows", .{})) |dep| {
2882+
run.addFileArg(dep.path("pluginval.exe"));
2883+
}
28922884
} else if (target.os.tag == .macos) {
2893-
// Use downloaded binary for macOS
2894-
const pluginval_fetch = std_extras.fetch(b, .{
2895-
.url = "https://github.com/Tracktion/pluginval/releases/download/v1.0.3/pluginval_macOS.zip",
2896-
.file_name = "Contents/MacOS/pluginval",
2897-
.hash = "N-V-__8AAF8tGQHuEhO2q5y6oj6foKiCHCXCQWbfpY6ehS5e",
2898-
.executable = true,
2899-
});
2900-
run.addFileArg(pluginval_fetch);
2885+
if (b.lazyDependency("pluginval_macos", .{})) |dep| {
2886+
const bin_path = dep.path("Contents/MacOS/pluginval");
2887+
run.addFileArg(bin_path);
2888+
run.step.dependOn(&chmodExeStep(b, bin_path).step);
2889+
}
29012890
} else if (target.os.tag == .linux) {
2902-
// Linux - use downloaded binary
2903-
const pluginval_fetch = std_extras.fetch(b, .{
2904-
.url = "https://github.com/Tracktion/pluginval/releases/download/v1.0.3/pluginval_Linux.zip",
2905-
.file_name = "pluginval",
2906-
.hash = "N-V-__8AAHiZqACvZuwhiWbvPBeJQd-K_5xpafp_Pi_6228J",
2907-
.executable = true,
2908-
});
2909-
run.addFileArg(pluginval_fetch);
2891+
if (b.lazyDependency("pluginval_linux", .{})) |dep| {
2892+
const bin_path = dep.path("pluginval");
2893+
run.addFileArg(bin_path);
2894+
run.step.dependOn(&chmodExeStep(b, bin_path).step);
2895+
}
29102896
} else {
29112897
@panic("Unsupported OS for pluginval");
29122898
}

build.zig.zon

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,38 @@
9090
.hash = "N-V-__8AAHEKFgBFuPa2_DBoERCMfyw108H3h7ubU7jLpXtj",
9191
.lazy = true,
9292
},
93+
.pluginval_macos = .{
94+
.url = "https://github.com/Tracktion/pluginval/releases/download/v1.0.3/pluginval_macOS.zip",
95+
.hash = "N-V-__8AAF8tGQHuEhO2q5y6oj6foKiCHCXCQWbfpY6ehS5e",
96+
.lazy = true,
97+
},
98+
.pluginval_windows = .{
99+
.url = "https://github.com/Tracktion/pluginval/releases/download/v1.0.3/pluginval_Windows.zip",
100+
.hash = "N-V-__8AAABcNACEKUY1SsEfHGFybDSKUo4JGhYN5bgZ146c",
101+
.lazy = true,
102+
},
103+
.pluginval_linux = .{
104+
.url = "https://github.com/Tracktion/pluginval/releases/download/v1.0.3/pluginval_Linux.zip",
105+
.hash = "N-V-__8AAHiZqACvZuwhiWbvPBeJQd-K_5xpafp_Pi_6228J",
106+
.lazy = true,
107+
},
108+
.clap_validator_macos = .{
109+
.url = "https://github.com/free-audio/clap-validator/releases/download/0.3.2/clap-validator-0.3.2-macos-universal.tar.gz",
110+
.hash = "N-V-__8AALwZfgBlaKnVwge3d221LJA9s_vQixy9c6OBvGhQ",
111+
.lazy = true,
112+
},
113+
.clap_validator_windows = .{
114+
.url = "https://github.com/free-audio/clap-validator/releases/download/0.3.2/clap-validator-0.3.2-windows.zip",
115+
.hash = "N-V-__8AAACYMwAKpkDTKEWrhJhUyBs1LxycLWN8iFpe5p6r",
116+
.lazy = true,
117+
},
118+
.clap_validator_linux = .{
119+
// NOTE: we're using floe-audio repo with a re-uploaded ZIP because we needed to workaround a zig fetch bug with
120+
// tar.gz files.
121+
.url = "https://github.com/floe-audio/clap-validator/releases/download/v0.3.2/clap-validator-0.3.2-ubuntu-18.04.zip",
122+
.hash = "N-V-__8AAFDvhAD7wsMQHzT9s_hiRLUTXJp4mBwyx_O7gZxZ",
123+
.lazy = true,
124+
},
93125
},
94126
.paths = .{"src"},
95127
}

src/build/std_extras.zig

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -220,77 +220,6 @@ pub fn createCommandWithStdoutToStderr(
220220
return run;
221221
}
222222

223-
// This function is from from TigerBeetle. Modified slightly to fit our needs.
224-
// Copyright TigerBeetle
225-
// SPDX-License-Identifier: Apache-2.0
226-
// Use 'zig fetch' to download and unpack the specified URL, optionally verifying the checksum.
227-
pub fn fetch(b: *std.Build, options: struct {
228-
url: []const u8,
229-
file_name: []const u8,
230-
hash: ?[]const u8,
231-
executable: bool = false,
232-
}) std.Build.LazyPath {
233-
const copy_from_cache = b.addRunArtifact(b.addExecutable(.{
234-
.name = "copy-from-cache",
235-
.root_module = b.createModule(.{
236-
.root_source_file = b.addWriteFiles().add("main.zig",
237-
\\const builtin = @import("builtin");
238-
\\const std = @import("std");
239-
\\const assert = std.debug.assert;
240-
\\
241-
\\pub fn main() !void {
242-
\\ var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
243-
\\ const allocator = arena.allocator();
244-
\\ const args = try std.process.argsAlloc(allocator);
245-
\\ assert(args.len == 7);
246-
\\
247-
\\ const hash_and_newline = try std.fs.cwd().readFileAlloc(allocator, args[2], 128);
248-
\\ assert(hash_and_newline[hash_and_newline.len - 1] == '\n');
249-
\\ const hash = hash_and_newline[0 .. hash_and_newline.len - 1];
250-
\\ if (!std.mem.eql(u8, args[5], "null") and !std.mem.eql(u8, args[5], hash)) {
251-
\\ std.debug.panic(
252-
\\ \\bad hash
253-
\\ \\specified: {s}
254-
\\ \\downloaded: {s}
255-
\\ \\
256-
\\ , .{ args[5], hash });
257-
\\ }
258-
\\
259-
\\ const source_path = try std.fs.path.join(allocator, &.{ args[1], hash, args[3] });
260-
\\ try std.fs.cwd().copyFile(
261-
\\ source_path,
262-
\\ std.fs.cwd(),
263-
\\ args[4],
264-
\\ .{},
265-
\\ );
266-
\\
267-
\\ if (std.mem.eql(u8, args[6], "executable")) {
268-
\\ if (builtin.os.tag != .windows) {
269-
\\ const file = try std.fs.cwd().openFile(args[4], .{});
270-
\\ defer file.close();
271-
\\ const permissions = std.fs.File.Permissions{
272-
\\ .inner = std.fs.File.PermissionsUnix.unixNew(0o755),
273-
\\ };
274-
\\ try file.setPermissions(permissions);
275-
\\ }
276-
\\ }
277-
\\}
278-
),
279-
.target = b.graph.host,
280-
}),
281-
}));
282-
copy_from_cache.addArg(
283-
b.graph.global_cache_root.join(b.allocator, &.{"p"}) catch @panic("OOM"),
284-
);
285-
copy_from_cache.addFileArg(
286-
b.addSystemCommand(&.{ b.graph.zig_exe, "fetch", options.url }).captureStdOut(),
287-
);
288-
copy_from_cache.addArg(options.file_name);
289-
const result = copy_from_cache.addOutputFileArg(options.file_name);
290-
copy_from_cache.addArg(options.hash orelse "null");
291-
copy_from_cache.addArg(if (options.executable) "executable" else "null");
292-
return result;
293-
}
294223

295224
// A wrapper for turning commands that modify files in-place into commands with input and output files.
296225
// See wrap_inplace_cmd.zig for more information.

0 commit comments

Comments
 (0)