zigself
zigself copied to clipboard
FreeBSD build fails
By default FreeBSD builds fail, complaining about error: unable to find or provide libc for target 'x86_64-freebsd.12.2...12.2-musl'
FreeBSD builds should link to the FreeBSD libc, as that is the system ABI, so I don't think Musl makes sense.
Removing the reference to Musl in build.zig
line 14 fixes the issue - I guess the zig compiler defaults to the correct outcome.
Is that reference to musl necessary fo the other OSs? Is there someway to override it which doesn't require editing build.zig
?
Sorry, not that familiar with zig build system.
The idea is that Musl should be the default libc target, as that allows us to build static binaries. You can specify a custom target with -Dtarget=<target name>
.
I believe this is a bug in the Zig build system, which shouldn't use libc targets that don't exist if they're specified as default in build.zig. I'll ask on their Discord and see whether there's a better way of doing this.
All FreeBSD installations will have a system libc. It’s part of the OS. My understanding is that the FreeBSD kernel ABI is changeable but the libc interface is guaranteed to exist and will not change - so the opposite of Linux.
So aiming for a static build of the vm on FreeBSD isn’t as useful as it is on Linux, at least when is comes to libc.
Yup, I'm aware. That's why I said the Zig build system shouldn't choose musl when it's not available on the arch-os
combo, even if it's defined as the default libc target.
Ah Ok - I understand you now. I’ll wait to see what Discord says 😄
Talked on the Discord and I'm not quite sure what the best way to go about this is. If an ABI is given to std.build.Builder.standardTargetOptions
, then it will override the ABI when no target is specified, even if the ABI is not available for the system. If -Dtarget=
is specified at all, the default target is dropped in favor of it.
@russellallen The diff I've added below should prefer static linking if possible, and avoid it in systems which cannot support it. Can you test it and see whether this fixes the issue?
diff --git a/build.zig b/build.zig
index b8eadbc..96b5f24 100644
--- a/build.zig
+++ b/build.zig
@@ -1,4 +1,6 @@
+const builtin = @import("builtin");
const std = @import("std");
+const Target = std.Target;
const zig_args = std.build.Pkg{
.name = "zig-args",
@@ -11,7 +13,18 @@ pub fn build(b: *std.build.Builder) void {
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{
- .default_target = std.zig.CrossTarget{ .abi = .musl },
+ .default_target = .{
+ // NOTE: Zig currently accepts musl in the default target, even if
+ // that target doesn't actually support musl. If we drop the
+ // default target selection however, Zig will instead look at
+ // the current (zig build) binary's ABI, which will most
+ // likely be gnu on Linux systems, thereby making dynamically
+ // linked binaries by default. Until this is fixed, let's
+ // default to std.Target.abi.default, which selects musl on
+ // systems that support it and the most reasonable choice on
+ // others.
+ .abi = Target.Abi.default(builtin.target.cpu.arch, builtin.target.os),
+ },
});
// Standard release options allow the person running `zig build` to select
Works for me on Linux (I get a static binary). On FreeBSD it still fails witht:
[root@syd ~/zigself]# zig build
error: libc not available
error: run 'zig libc -h' to learn about libc installations
error: run 'zig targets' to see the targets for which zig can always provide libc
self...The following command exited with error code 1:
/root/zig/zig build-exe /root/zigself/src/main.zig -lc --cache-dir /root/zigself/zig-cache --global-cache-dir /root/.cache/zig --name self -target native-native-gnu -mcpu=skylake_avx512-adx-clflushopt-prfchw-rdseed+rtm-xsavec-xsaves --pkg-begin zig-args /root/zigself/vendor/zig-args/args.zig --pkg-end --enable-cache
error: the following build command failed with exit code 1:
/root/zigself/zig-cache/o/a1dcdc45617fa1033d580f1ebb0f9bd6/build /root/zig/zig /root/zigself /root/zigself/zig-cache /root/.cache/zig
I see. I'll get a FreeBSD install myself and see what magic invocation makes this work. Thanks for checking it out.
Sorry for going AFK on this. I'll be reverting the change in build.zig
for now, as I encountered a few more issues when I tried it out on FreeBSD.