Freebsd target resolution does not allow to compile

Hi All! so far I came across this misunderstanding regarding building system and how I can tune it. The problem arises when I try cross-compile my console app. for several platforms looping them in build.zig.

When it comes to compiling for freebsd (even at freebsd host), strange error happens. It can be reproduced both with stocked 0.13 zig as well as master’s 0.14 versions.
The simplest way to reproduce is replace

    const target = b.standardTargetOptions(.{});

with

    const target = b.resolveTargetQuery(.{ .abi = .gnu, .os_tag = .freebsd, .cpu_arch = .x86_64 });

right after initialization of blank project with zig init.
The target triplet (as I expect) true for native host compilation target @ freebsd host.

As as sidenote, I see that zig targets strangely resolves freebsd’s triplet as

 ...
 "native": {
  "triple": "x86_64-freebsd.15.0.23...15.0.23-gnu",
 ...

Are there any rakes around I have missed?

UPD: same error about missing libc even if I specify min/max versions of OS, which seems must sutisy my particular case:

    const target = b.resolveTargetQuery(.{ .abi = .gnu, .os_tag = .freebsd, .cpu_arch = .x86_64, .os_version_min = .{ .semver = .{ .major = 15, .minor = 0, .patch = 23 } }, .os_version_max = .{ .semver = .{ .major = 15, .minor = 0, .patch = 23 } } });

error:

error: libc not available
    note: run 'zig libc -h' to learn about libc installations
    note: run 'zig targets' to see the targets for which zig can always provide libc
error: the following command failed with 22 compilation errors:
/usr/local/bin/zig build-exe -ODebug -target x86_64-freebsd.15.0.23...15.0.23-gnu -mcpu baseline -Mroot=/home/ws/p/fbsd-zig/src/main.zig --cache-dir /home/ws/p/fbsd-zig/.zig-cache --global-cache-dir /home/ws/.cache/zig --name fbsd-zig --listen=- 
Build Summary: 2/5 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
+- install fbsd-zig transitive failure
   +- zig build-exe fbsd-zig Debug x86_64-freebsd.15.0.23...15.0.23-gnu 22 errors

Running zig targets | jq -r .native.triple prints the native triple.
In my case it is: x86_64-linux.6.1...6.1-gnu.2.36
Note that my triple have a specific version for the gnu abi 2.36 and a version range for the linux os 6.1 ... 6.1.
In your case, the triple have os freebsd 15.0.23 ... 15.0.23.
Since native means the machine that runs the executable, both are correct.


Running zig build-exe --show-builtin -target x86_64-freebsd displays the builtin zig module contents for the specific target (you can add more flags for release etc.)

pub const abi = std.Target.Abi.gnu;
pub const cpu: std.Target.Cpu = .{
    .arch = .x86_64,
    .model = &std.Target.x86.cpu.x86_64,
    .features = std.Target.x86.featureSet(&[_]std.Target.x86.Feature{
        .@"64bit",
        .cmov,
        .cx8,
        .fxsr,
        .idivq_to_divl,
        .macrofusion,
        .mmx,
        .nopl,
        .slow_3ops_lea,
        .slow_incdec,
        .sse,
        .sse2,
        .vzeroupper,
        .x87,
    }),
};
pub const os = std.Target.Os{
    .tag = .freebsd,
    .version_range = .{ .semver = .{
        .min = .{
            .major = 12,
            .minor = 0,
            .patch = 0,
        },
        .max = .{
            .major = 14,
            .minor = 0,
            .patch = 0,
        },
    }},
};
pub const target: std.Target = .{
    .cpu = cpu,
    .os = os,
    .abi = abi,
    .ofmt = object_format,
    .dynamic_linker = std.Target.DynamicLinker.init("/libexec/ld-elf.so.1"),
};

In this case (baseline) freebsd os version range is 12.0.0 ... 14.0.0


You cannot cross compile to freebsd because freebsd libc is not included in zig.
The relevant issues are:

Currently you cannot cross compile to FreeBSD and you can target only the native libc of the freebsd host.

1 Like