From f263a8fa7121a9632c61cba8251b6272751fcbbd Mon Sep 17 00:00:00 2001 From: Matheus Catarino Date: Wed, 1 Jan 2025 09:55:20 -0300 Subject: [PATCH] zig-build: xpack fetch - optional hash --- .github/workflows/build.yml | 2 +- build.zig | 66 ++++++++++++++++++++++++++++++++++++- build.zig.zon | 4 --- 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ca441e7..10be922 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -93,7 +93,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' diff --git a/build.zig b/build.zig index 917f91b..3f445a2 100644 --- a/build.zig +++ b/build.zig @@ -1125,6 +1125,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| { @@ -1141,7 +1153,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; @@ -1152,6 +1163,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 { diff --git a/build.zig.zon b/build.zig.zon index 6f1eeb3..1b14b1d 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -26,9 +26,5 @@ .hash = "122007ff9e979a63058e3af1de1d8b98957baa741a8b779d50f9b251dca4acf60b41", .lazy = true, }, - .xpack = .{ - .url = "https://github.com/opendlang/opend/releases/download/CI/opend-latest-xpack-emscripten.tar.xz", - .hash = "1220132c994d2f9754174c5fe934413f3fc79603d1a180429bb231aaed6110429cc6", - }, }, }