gpu-dawn: don't leave binary download cache in bad state if error occurs
Fixes hexops/mach#188 Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
d7230d6b5b
commit
add120b2a1
1 changed files with 41 additions and 19 deletions
|
|
@ -254,7 +254,13 @@ pub fn linkFromBinary(b: *Builder, step: *std.build.LibExeObjStep, options: Opti
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ensureBinaryDownloaded(allocator: std.mem.Allocator, zig_triple: []const u8, is_release: bool, is_windows: bool, version: []const u8) void {
|
pub fn ensureBinaryDownloaded(
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
zig_triple: []const u8,
|
||||||
|
is_release: bool,
|
||||||
|
is_windows: bool,
|
||||||
|
version: []const u8,
|
||||||
|
) void {
|
||||||
// If zig-cache/mach/gpu-dawn/<git revision> does not exist:
|
// If zig-cache/mach/gpu-dawn/<git revision> does not exist:
|
||||||
// If on a commit in the main branch => rm -r zig-cache/mach/gpu-dawn/
|
// If on a commit in the main branch => rm -r zig-cache/mach/gpu-dawn/
|
||||||
// else => noop
|
// else => noop
|
||||||
|
|
@ -284,20 +290,36 @@ pub fn ensureBinaryDownloaded(allocator: std.mem.Allocator, zig_triple: []const
|
||||||
if (dirExists(target_cache_dir)) {
|
if (dirExists(target_cache_dir)) {
|
||||||
return; // nothing to do, already have the binary
|
return; // nothing to do, already have the binary
|
||||||
}
|
}
|
||||||
|
downloadBinary(allocator, commit_cache_dir, release_tag, target_cache_dir, zig_triple, is_windows, version) catch |err| {
|
||||||
|
// A download failed, or extraction failed, so wipe out the directory to ensure we correctly
|
||||||
|
// try again next time.
|
||||||
|
std.fs.deleteTreeAbsolute(base_cache_dir) catch {};
|
||||||
|
@panic(@errorName(err));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const download_dir = std.fs.path.join(allocator, &.{ target_cache_dir, "download" }) catch unreachable;
|
fn downloadBinary(
|
||||||
std.fs.cwd().makePath(download_dir) catch unreachable;
|
allocator: std.mem.Allocator,
|
||||||
|
commit_cache_dir: []const u8,
|
||||||
|
release_tag: []const u8,
|
||||||
|
target_cache_dir: []const u8,
|
||||||
|
zig_triple: []const u8,
|
||||||
|
is_windows: bool,
|
||||||
|
version: []const u8,
|
||||||
|
) !void {
|
||||||
|
const download_dir = try std.fs.path.join(allocator, &.{ target_cache_dir, "download" });
|
||||||
|
try std.fs.cwd().makePath(download_dir);
|
||||||
|
|
||||||
// Replace "..." with "---" because GitHub releases has very weird restrictions on file names.
|
// Replace "..." with "---" because GitHub releases has very weird restrictions on file names.
|
||||||
// https://twitter.com/slimsag/status/1498025997987315713
|
// https://twitter.com/slimsag/status/1498025997987315713
|
||||||
const github_triple = std.mem.replaceOwned(u8, allocator, zig_triple, "...", "---") catch unreachable;
|
const github_triple = try std.mem.replaceOwned(u8, allocator, zig_triple, "...", "---");
|
||||||
|
|
||||||
// Compose the download URL, e.g.:
|
// Compose the download URL, e.g.:
|
||||||
// https://github.com/hexops/mach-gpu-dawn/releases/download/release-6b59025/libdawn_x86_64-macos-gnu_debug.a.gz
|
// https://github.com/hexops/mach-gpu-dawn/releases/download/release-6b59025/libdawn_x86_64-macos-gnu_debug.a.gz
|
||||||
const lib_prefix = if (is_windows) "dawn_" else "libdawn_";
|
const lib_prefix = if (is_windows) "dawn_" else "libdawn_";
|
||||||
const lib_ext = if (is_windows) ".lib" else ".a";
|
const lib_ext = if (is_windows) ".lib" else ".a";
|
||||||
const lib_file_name = if (is_windows) "dawn.lib" else "libdawn.a";
|
const lib_file_name = if (is_windows) "dawn.lib" else "libdawn.a";
|
||||||
const download_url = std.mem.concat(allocator, u8, &.{
|
const download_url = try std.mem.concat(allocator, u8, &.{
|
||||||
"https://github.com/hexops/mach-gpu-dawn/releases/download/",
|
"https://github.com/hexops/mach-gpu-dawn/releases/download/",
|
||||||
version,
|
version,
|
||||||
"/",
|
"/",
|
||||||
|
|
@ -307,36 +329,36 @@ pub fn ensureBinaryDownloaded(allocator: std.mem.Allocator, zig_triple: []const
|
||||||
release_tag,
|
release_tag,
|
||||||
lib_ext,
|
lib_ext,
|
||||||
".gz",
|
".gz",
|
||||||
}) catch unreachable;
|
});
|
||||||
|
|
||||||
// Download and decompress libdawn
|
// Download and decompress libdawn
|
||||||
const gz_target_file = std.fs.path.join(allocator, &.{ download_dir, "compressed.gz" }) catch unreachable;
|
const gz_target_file = try std.fs.path.join(allocator, &.{ download_dir, "compressed.gz" });
|
||||||
downloadFile(allocator, gz_target_file, download_url) catch unreachable;
|
try downloadFile(allocator, gz_target_file, download_url);
|
||||||
const target_file = std.fs.path.join(allocator, &.{ target_cache_dir, lib_file_name }) catch unreachable;
|
const target_file = try std.fs.path.join(allocator, &.{ target_cache_dir, lib_file_name });
|
||||||
gzipDecompress(allocator, gz_target_file, target_file) catch unreachable;
|
try gzipDecompress(allocator, gz_target_file, target_file);
|
||||||
|
|
||||||
// If we don't yet have the headers (these are shared across architectures), download them.
|
// If we don't yet have the headers (these are shared across architectures), download them.
|
||||||
const include_dir = std.fs.path.join(allocator, &.{ commit_cache_dir, "include" }) catch unreachable;
|
const include_dir = try std.fs.path.join(allocator, &.{ commit_cache_dir, "include" });
|
||||||
if (!dirExists(include_dir)) {
|
if (!dirExists(include_dir)) {
|
||||||
// Compose the headers download URL, e.g.:
|
// Compose the headers download URL, e.g.:
|
||||||
// https://github.com/hexops/mach-gpu-dawn/releases/download/release-6b59025/headers.json.gz
|
// https://github.com/hexops/mach-gpu-dawn/releases/download/release-6b59025/headers.json.gz
|
||||||
const headers_download_url = std.mem.concat(allocator, u8, &.{
|
const headers_download_url = try std.mem.concat(allocator, u8, &.{
|
||||||
"https://github.com/hexops/mach-gpu-dawn/releases/download/",
|
"https://github.com/hexops/mach-gpu-dawn/releases/download/",
|
||||||
version,
|
version,
|
||||||
"/headers.json.gz",
|
"/headers.json.gz",
|
||||||
}) catch unreachable;
|
});
|
||||||
|
|
||||||
// Download and decompress headers.json.gz
|
// Download and decompress headers.json.gz
|
||||||
const headers_gz_target_file = std.fs.path.join(allocator, &.{ download_dir, "headers.json.gz" }) catch unreachable;
|
const headers_gz_target_file = try std.fs.path.join(allocator, &.{ download_dir, "headers.json.gz" });
|
||||||
downloadFile(allocator, headers_gz_target_file, headers_download_url) catch unreachable;
|
try downloadFile(allocator, headers_gz_target_file, headers_download_url);
|
||||||
const headers_target_file = std.fs.path.join(allocator, &.{ target_cache_dir, "headers.json" }) catch unreachable;
|
const headers_target_file = try std.fs.path.join(allocator, &.{ target_cache_dir, "headers.json" });
|
||||||
gzipDecompress(allocator, headers_gz_target_file, headers_target_file) catch unreachable;
|
try gzipDecompress(allocator, headers_gz_target_file, headers_target_file);
|
||||||
|
|
||||||
// Extract headers JSON archive.
|
// Extract headers JSON archive.
|
||||||
extractHeaders(allocator, headers_target_file, commit_cache_dir) catch unreachable;
|
try extractHeaders(allocator, headers_target_file, commit_cache_dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
std.fs.deleteTreeAbsolute(download_dir) catch unreachable;
|
try std.fs.deleteTreeAbsolute(download_dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extractHeaders(allocator: std.mem.Allocator, json_file: []const u8, out_dir: []const u8) !void {
|
fn extractHeaders(allocator: std.mem.Allocator, json_file: []const u8, out_dir: []const u8) !void {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue