Skip to content

Commit

Permalink
zig-build: xpack fetch - optional hash
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Jan 19, 2025
1 parent 5ca192d commit fcddf3f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ jobs:
sudo apt-get install libglu1-mesa-dev mesa-common-dev xorg-dev libasound-dev
- name: (Zig) Build Shaders
run: zig build shaders
run: zig build -Dshaders

- name: (Zig) Running Test
if: runner.os != 'Windows'
Expand Down
66 changes: 65 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,18 @@ pub fn emLinkStep(b: *Build, options: EmLinkOptions) !*Build.Step.InstallDir {
emcc.addArg(arg);
}

if (options.use_drt) {
const libdruntime = fetch(b, .{
.url = "https://github.com/opendlang/opend/releases/download/CI/opend-latest-xpack-emscripten.tar.xz",
.file_name = "lib/libdruntime-ldc.a",
});
const libphobos2 = fetch(b, .{
.url = "https://github.com/opendlang/opend/releases/download/CI/opend-latest-xpack-emscripten.tar.xz",
.file_name = "lib/libphobos2-ldc.a",
});
emcc.addFileArg(libdruntime);
emcc.addFileArg(libphobos2);
}
// add the main lib, and then scan for library dependencies and add those too
emcc.addArtifactArg(options.lib_main);
for (options.lib_main.getCompileDependencies(false)) |item| {
Expand All @@ -1084,7 +1096,6 @@ pub fn emLinkStep(b: *Build, options: EmLinkOptions) !*Build.Step.InstallDir {
.install_subdir = "web",
});
install.step.dependOn(&emcc.step);

// get the emcc step to run on 'zig build'
b.getInstallStep().dependOn(&install.step);
return install;
Expand All @@ -1095,6 +1106,59 @@ pub fn emLinkStep(b: *Build, options: EmLinkOptions) !*Build.Step.InstallDir {
});
}

// Use 'zig fetch' to download and unpack the specified URL, optionally verifying the checksum.
fn fetch(b: *std.Build, options: struct {
url: []const u8,
file_name: []const u8,
hash: ?[]const u8 = null,
}) std.Build.LazyPath {
const copy_from_cache = b.addRunArtifact(b.addExecutable(.{
.name = "copy-from-cache",
.root_source_file = b.addWriteFiles().add("main.zig",
\\const std = @import("std");
\\const assert = std.debug.assert;
\\pub fn main() !void {
\\ var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
\\ const allocator = arena.allocator();
\\ const args = try std.process.argsAlloc(allocator);
\\ assert(args.len == 5 or args.len == 6);
\\
\\ const hash_and_newline = try std.fs.cwd().readFileAlloc(allocator, args[2], 128);
\\ assert(hash_and_newline[hash_and_newline.len - 1] == '\n');
\\ const hash = hash_and_newline[0..hash_and_newline.len - 1];
\\ if (args.len == 6 and !std.mem.eql(u8, args[5], hash)) {
\\ std.debug.panic(
\\ \\bad hash
\\ \\specified: {s}
\\ \\downloaded: {s}
\\ \\
\\ , .{args[5], hash, });
\\ }
\\ const source_path = try std.fs.path.join(allocator, &.{args[1], hash, args[3]});
\\ try std.fs.cwd().copyFile(
\\ source_path,
\\ std.fs.cwd(),
\\ args[4],
\\ .{},
\\ );
\\}
),
.target = b.graph.host,
}));
copy_from_cache.addArg(
b.graph.global_cache_root.join(b.allocator, &.{"p"}) catch @panic("OOM"),
);
copy_from_cache.addFileArg(
b.addSystemCommand(&.{ b.graph.zig_exe, "fetch", options.url }).captureStdOut(),
);
copy_from_cache.addArg(options.file_name);
const result = copy_from_cache.addOutputFileArg(options.file_name);
if (options.hash) |hash| {
copy_from_cache.addArg(hash);
}
return result;
}

// build a run step which uses the emsdk emrun command to run a build target in the browser
// NOTE: ideally this would go into a separate emsdk-zig package
pub const EmRunOptions = struct {
Expand Down
4 changes: 0 additions & 4 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,5 @@
.hash = "1220f2ae16952843dd25b6bc86b888a3fd8c3f25d34adddfd8d1d2b9f801429e80e4",
.lazy = true,
},
.xpack = .{
.url = "https://github.com/opendlang/opend/releases/download/CI/opend-latest-xpack-emscripten.tar.xz",
.hash = "1220132c994d2f9754174c5fe934413f3fc79603d1a180429bb231aaed6110429cc6",
},
},
}

0 comments on commit fcddf3f

Please sign in to comment.