diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-29 14:00:00 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-05-02 16:54:24 +0200 |
| commit | 876ef71d1796fdac7f918762390324b8b7e6ff73 (patch) | |
| tree | 8e48ada3269e5df16757193aae1f06313cf601d7 | |
| parent | 8b9d03450d95e45057eb510a9760175cfc44dd35 (diff) | |
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++.
| -rw-r--r-- | build.zig | 2 | ||||
| -rw-r--r-- | go/build.zig | 39 |
2 files changed, 27 insertions, 14 deletions
@@ -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; +} |
