From 876ef71d1796fdac7f918762390324b8b7e6ff73 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Wed, 29 Apr 2026 14:00:00 +0200 Subject: build(go): cross compile This doesn't work, because there is a bug in Zig 0.15 that produces truncated or malformed archives (fixed in Zig 0.16). There also is an error when setting -target for zig cc and zig c++. --- build.zig | 2 +- go/build.zig | 39 ++++++++++++++++++++++++++------------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/build.zig b/build.zig index a934bbc..5c08440 100644 --- a/build.zig +++ b/build.zig @@ -109,7 +109,7 @@ pub fn build(b: *std.Build) void { const example_run = b.addRunArtifact(example); examples_step.dependOn(&example_run.step); - const check = b.step("check", "Check if foo compiles"); + const check = b.step("check", "Check if it compiles"); check.dependOn(&lib.step); } diff --git a/go/build.zig b/go/build.zig index 6399042..4babb18 100644 --- a/go/build.zig +++ b/go/build.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const builtin = @import("builtin"); pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); @@ -18,27 +19,39 @@ pub fn build(b: *std.Build) !void { //lib.pie = true; const install_lib = b.addInstallArtifact(lib, .{}); - var flags = try std.ArrayList(u8).initCapacity(b.allocator, 2); - try flags.appendSlice(b.allocator, "-linkmode external -extldflags -static"); - if (optimize != .Debug) try flags.appendSlice(b.allocator, " -s"); - const go_build = b.addSystemCommand(&[_][]const u8{ - "go", "build", - "-ldflags", flags.items, - ".", - }); + const go_build = buildGo(b, target, optimize, "build"); go_build.step.dependOn(&install_lib.step); install_step.dependOn(&go_build.step); const test_step = b.step("test", "Run tests"); test_step.dependOn(b.getInstallStep()); const race = b.option(bool, "race", "Run tests with -race") orelse false; - const go_test = b.addSystemCommand(&[_][]const u8{ - "go", "test", - "-ldflags", flags.items, - "-v", - }); + const go_test = buildGo(b, target, optimize, "test"); if (race) go_test.addArg("-race"); go_test.addArg("./..."); test_step.dependOn(install_step); } + +fn buildGo(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, command: []const u8) *std.Build.Step.Run { + var flags = std.ArrayList(u8).initCapacity(b.allocator, 2) catch unreachable; + flags.appendSlice(b.allocator, "-linkmode external -extldflags -static") catch unreachable; + if (optimize != .Debug) flags.appendSlice(b.allocator, " -s") catch unreachable; + //const targetStr = std.fmt.allocPrint(b.allocator, "{s}-{s}-{s}", .{ + // @tagName(target.result.cpu.arch), + // @tagName(target.result.os.tag), + // @tagName(target.result.abi), + //}) catch unreachable; + //const cc = std.fmt.allocPrint(b.allocator, "zig cc -target {s}", .{targetStr}) catch unreachable; + //const cpp = std.fmt.allocPrint(b.allocator, "zig c++ -target {s}", .{targetStr}) catch unreachable; + const run = b.addSystemCommand(&[_][]const u8{ + "go", command, + "-ldflags", flags.items, + ".", + }); + run.setEnvironmentVariable("CC", "zig cc"); + run.setEnvironmentVariable("C++", "zig c++"); + run.setEnvironmentVariable("CGO_ENABLED", "1"); + run.setEnvironmentVariable("GOOS", @tagName(target.result.os.tag)); + return run; +} -- cgit v1.2.3