From e9671f388c13cf8f7602d5d7ffdd2fd8ced90279 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Sun, 17 Apr 2022 23:00:12 -0700 Subject: [PATCH] examples: ensure zmath submodule is cloned during zig build Fixes the issue people are running into at e.g.: * https://old.reddit.com/r/Zig/comments/u622dq/mach_engine_webgpu_examples_showcase/i564w4s/ * https://twitter.com/slimsag/status/1515925666230784000?s=20&t=M2KXFUcLZT0-mJAcMy6sPw Signed-off-by: Stephen Gutekanst --- build.zig | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/build.zig b/build.zig index 5fcc3a49..a42d86f9 100644 --- a/build.zig +++ b/build.zig @@ -24,6 +24,9 @@ pub fn build(b: *std.build.Builder) void { const test_step = b.step("test", "Run library tests"); test_step.dependOn(&main_tests.step); + // TODO(build-system): https://github.com/hexops/mach/issues/229#issuecomment-1100958939 + ensureDependencySubmodule(b.allocator, "examples/libs/zmath") catch unreachable; + inline for ([_]ExampleDefinition{ .{ .name = "triangle" }, .{ .name = "boids" }, @@ -60,7 +63,13 @@ const ExampleDefinition = struct { }; const Packages = struct { - const zmath = @import("examples/libs/zmath/build.zig").pkg; + const zmath = zmath_pkg; +}; + +// Declared here because submodule may not be cloned at the time build.zig runs. +const zmath_pkg = std.build.Pkg{ + .name = "zmath", + .path = .{ .path = "examples/libs/zmath/src/zmath.zig" }, }; pub const pkg = std.build.Pkg{ @@ -93,3 +102,15 @@ pub fn link(b: *std.build.Builder, step: *std.build.LibExeObjStep, options: Opti fn thisDir() []const u8 { return std.fs.path.dirname(@src().file) orelse "."; } + +fn ensureDependencySubmodule(allocator: std.mem.Allocator, path: []const u8) !void { + if (std.process.getEnvVarOwned(allocator, "NO_ENSURE_SUBMODULES")) |no_ensure_submodules| { + if (std.mem.eql(u8, no_ensure_submodules, "true")) return; + } else |_| {} + const child = try std.ChildProcess.init(&.{ "git", "submodule", "update", "--init", path }, allocator); + child.cwd = thisDir(); + child.stderr = std.io.getStdErr(); + child.stdout = std.io.getStdOut(); + + _ = try child.spawnAndWait(); +}