tazel
A build system for C, C++, Objective-C, Objective-C++ and Zig, with C# project files.
Clonegit clone https://git.progmasoft.com/tazel
Say why zig links nothing for DragonFly BSD, test the BSD targets
- Commit
6f2c070120140816e89166bb16467aab049d86eb- Author
- Leitwolf11 ·
- Parents
48c348f635df
Changes
CHANGELOG.md | 3 + docs/project-files.md | 6 ++ src/Progmasoft.Tazel/Driver/ZigPlanner.cs | 10 ++ tests/Progmasoft.Tazel.Tests/BsdCrossTests.cs | 131 ++++++++++++++++++++++++++ 4 files changed, 150 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 315b6de..6b910cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ ## Unreleased +- Building for DragonFly BSD with zig says that zig brings no libc for it, instead of zig's own error, and what does + build for it. Tests build Zig, C and C++ for FreeBSD and OpenBSD with zig and check that the files are theirs. + - A build reads each file once: a header that every compilation includes is looked at once, not once per compilation. diff --git a/docs/project-files.md b/docs/project-files.md index 692cfc1..a2ae9ae 100644 --- a/docs/project-files.md +++ b/docs/project-files.md @@ -250,3 +250,9 @@ Zig.Toolchain("zig", new() - The platforms are `LinuxAmd64` (`linux-amd64`), `LinuxAarch64`, `WindowsX64`, `WindowsArm64`, `MacOSX86_64` (`macos-x86_64`), `MacOSArm64`, `FreeBSDAmd64`, `FreeBSDAarch64`, `OpenBSDAmd64`, `OpenBSDAarch64` and `DragonFlyBSDAmd64`. +- The BSD platforms are platforms to build for: Tazel runs on Linux, macOS and Windows. This machine's own compilers + build only for this machine, so another platform takes a registered toolchain. zig brings the libc of FreeBSD and + OpenBSD, so a `Zig.Toolchain` whose `Zig` is the zig on PATH builds Zig, C and C++ for them; a `CC.Toolchain` for + them names a compiler that targets them (zig cc does, wrapped in a script that adds `-target aarch64-freebsd`). zig + has no libc for DragonFly BSD: for `DragonFlyBSDAmd64` it builds static libraries, and linking needs a CC toolchain + whose compiler has a DragonFly sysroot. diff --git a/src/Progmasoft.Tazel/Driver/ZigPlanner.cs b/src/Progmasoft.Tazel/Driver/ZigPlanner.cs index d16329c..0af514a 100644 --- a/src/Progmasoft.Tazel/Driver/ZigPlanner.cs +++ b/src/Progmasoft.Tazel/Driver/ZigPlanner.cs @@ -117,6 +117,16 @@ internal sealed partial class Planner return products; } + // zig brings the libc of FreeBSD and OpenBSD, so it links for them on any host, but not DragonFly's: there it + // compiles and archives, and linking fails without a sysroot of DragonFly that zig is not given. + if (Options.Platform == Platform.DragonFlyBSDAmd64 && target.Kind != TargetKind.StaticLibrary) + { + throw new ProjectException( + $"{target.Place}: zig brings no libc for DragonFly BSD, so it cannot link {target.Label} for " + + "dragonfly-bsd-amd64; Zig static libraries build for it, and C and C++ programs build with a registered " + + "CC toolchain whose compiler has a DragonFly sysroot"); + } + var settings = (ZigCompiledSettings)target.Settings; var zig = Toolchains.Zig(); var directory = Layout.PackageOut(target); diff --git a/tests/Progmasoft.Tazel.Tests/BsdCrossTests.cs b/tests/Progmasoft.Tazel.Tests/BsdCrossTests.cs new file mode 100644 index 0000000..439773c --- /dev/null +++ b/tests/Progmasoft.Tazel.Tests/BsdCrossTests.cs @@ -0,0 +1,131 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: MIT + +using System; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Progmasoft.Tazel.Driver; +using Xunit; + +namespace Progmasoft.Tazel.Tests; + +/// <summary>Builds for the BSDs from this machine, with the zig on PATH: what Tazel's BSD platforms are for.</summary> +public sealed class BsdCrossTests +{ + private const string ZigProgram = "const std = @import(\"std\");\npub fn main() void { std.debug.print(\"hi\\n\", .{}); }\n"; + + // The note an ELF file of the system carries: name size 8, description size 4, type 1, then the name. + private static bool IsFor(string file, string system) + { + var note = new byte[] { 8, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0 }.Concat(Encoding.ASCII.GetBytes(system + "\0")).ToArray(); + return File.ReadAllBytes(file).AsSpan().IndexOf(note) >= 0; + } + + // A module whose zig, the one on PATH, is registered for the platform: Tazel takes this machine's tools for this + // machine alone, and a toolchain for every other platform. + private static TemporaryModule ZigModule(string member, string build) + { + var module = new TemporaryModule() + .Write("MODULE.csx", TemporaryModule.ModuleFile + "\ntoolchains = Toolchains.Define(\"//toolchains:zig\");") + .Write("toolchains/BUILD.csx", $$""" + using Progmasoft.Tazel.Dsl.Build.Toolchain.Prelude; + using Progmasoft.Tazel.Dsl.Common; + Zig.Toolchain("zig", new() + { + ExecutionPlatform = Platform.{{Host.Platform}}, + TargetPlatform = Platform.{{member}}, + Tools = new() { Zig = "zig" }, + }); + """) + .Write("BUILD.csx", "using Progmasoft.Tazel.Dsl.Build.Prelude;\n" + build); + return module; + } + + private static string Built(TemporaryModule module, string name) => + Directory.EnumerateFiles(Path.Combine(module.Root, ".tazel", "out"), name, SearchOption.AllDirectories).Single(); + + [Theory] + [InlineData("FreeBSDAmd64", "freebsd-amd64", "FreeBSD")] + [InlineData("FreeBSDAarch64", "freebsd-aarch64", "FreeBSD")] + [InlineData("OpenBSDAmd64", "openbsd-amd64", "OpenBSD")] + [InlineData("OpenBSDAarch64", "openbsd-aarch64", "OpenBSD")] + public async Task BuildsAZigProgramForABsd(string member, string platform, string system) + { + Assert.SkipWhen(Host.Which("zig") is null, "no zig on PATH"); + using var module = ZigModule(member, "Zig.Executable(\"app\", new() { RootSourceFile = \"main.zig\" });\n") + .Write("main.zig", ZigProgram); + var (status, output) = await module.TazelAsync(".", "build", "//:app", "--platform", platform); + Assert.True(status == 0, output); + Assert.True(IsFor(Built(module, "app"), system), $"//:app is not a {system} program"); + } + + [Theory] + [InlineData("FreeBSDAarch64", "freebsd-aarch64", "aarch64-freebsd", "FreeBSD")] + [InlineData("OpenBSDAmd64", "openbsd-amd64", "x86_64-openbsd", "OpenBSD")] + public async Task BuildsACppProgramForABsdWithARegisteredToolchain(string member, string platform, string triple, string system) + { + Assert.SkipWhen(Host.Which("zig") is null, "no zig on PATH"); + Assert.SkipWhen(OperatingSystem.IsWindows(), "the compilers of the toolchain are shell scripts"); + using var module = new TemporaryModule(); + // zig cc for the BSD, as a user would wrap a cross compiler: a toolchain names programs, not arguments. + string Wrapper(string name, string command) + { + var path = Path.Combine(module.Root, "bin", name); + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + File.WriteAllText(path, $"#!/bin/sh\nexec zig {command} \"$@\"\n"); + if (!OperatingSystem.IsWindows()) + { + File.SetUnixFileMode(path, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute); + } + return path; + } + + var cc = Wrapper("cc", $"cc -target {triple}"); + var cxx = Wrapper("c++", $"c++ -target {triple}"); + var ar = Wrapper("ar", "ar"); + module.Write("MODULE.csx", TemporaryModule.ModuleFile + "\ntoolchains = Toolchains.Define(\"//toolchains:bsd\");") + .Write("toolchains/BUILD.csx", $$""" + using Progmasoft.Tazel.Dsl.Build.Toolchain.Prelude; + using Progmasoft.Tazel.Dsl.Common; + CC.Toolchain("bsd", new() + { + ExecutionPlatform = Platform.{{Host.Platform}}, + TargetPlatform = Platform.{{member}}, + Tools = new() { CCompiler = "{{cc}}", CppCompiler = "{{cxx}}", Linker = "{{cxx}}", Archiver = "{{ar}}" }, + }); + """) + .Write("BUILD.csx", """ + using Progmasoft.Tazel.Dsl.Build.Prelude; + CC.StaticLibrary("greeting", new() { Sources = ["greeting.cpp"] }); + CC.Executable("app", new() { Sources = ["main.cpp"], Depends = [":greeting"] }); + """) + .Write("greeting.cpp", "#include <string>\nstd::string greeting() { return \"hi\"; }\n") + .Write("main.cpp", "#include <iostream>\n#include <string>\nstd::string greeting();\nint main() { std::cout << greeting() << '\\n'; }\n"); + var (status, output) = await module.TazelAsync(".", "build", "//:app", "--platform", platform); + Assert.True(status == 0, output); + Assert.True(IsFor(Built(module, "app"), system), $"//:app is not a {system} program"); + } + + [Fact] + public async Task ZigLinksNothingForDragonFlyAndSaysWhy() + { + using var module = ZigModule("DragonFlyBSDAmd64", "Zig.Executable(\"app\", new() { RootSourceFile = \"main.zig\" });\n") + .Write("main.zig", ZigProgram); + var (status, output) = await module.TazelAsync(".", "build", "//:app", "--platform", "dragonfly-bsd-amd64"); + Assert.Equal(1, status); + Assert.Contains("zig brings no libc for DragonFly BSD, so it cannot link //:app for dragonfly-bsd-amd64", output, + StringComparison.Ordinal); + } + + [Fact] + public async Task ZigBuildsAStaticLibraryForDragonFly() + { + Assert.SkipWhen(Host.Which("zig") is null, "no zig on PATH"); + using var module = ZigModule("DragonFlyBSDAmd64", "Zig.StaticLibrary(\"core\", new() { RootSourceFile = \"core.zig\" });\n") + .Write("core.zig", "export fn add(a: i32, b: i32) i32 { return a + b; }\n"); + var (status, output) = await module.TazelAsync(".", "build", "//:core", "--platform", "dragonfly-bsd-amd64"); + Assert.True(status == 0, output); + } +}