Something went wrong. Try again.
Monorepo for PackTools - tooling for Minecraft Modpacks!
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081//! packutils' build: `libpackutils.a`, C++23 compiled with `zig c++` for whatever target the//! caller asks. No third-party code: the C++ standard library, POSIX and Win32 only. Packwand//! consumes the `packutils` artifact through `build.zig.zon`. AI-authored (Claude).
const std = @import("std");
/// packutils' sources, held to the strict warning set. The two `fs_` halves compile to nothing on/// the other platform.const sources = [_][]const u8{ "abi.cpp", "fs_common.cpp", "fs_posix.cpp", "fs_windows.cpp", "json.cpp", "launchers.cpp", "links.cpp", "mirror.cpp", "records.cpp",};
const strict_flags: []const []const u8 = &.{ "-std=c++23", "-Wall", "-Wextra", "-Wpedantic", "-Wconversion", "-Wsign-conversion", "-Wshadow", "-Werror",};
/// Release targets Packwand builds; `check` compiles the library for each.const cross_targets = [_]std.Target.Query{ .{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .gnu }, .{ .cpu_arch = .aarch64, .os_tag = .windows, .abi = .gnu }, .{ .cpu_arch = .aarch64, .os_tag = .macos }, .{ .cpu_arch = .x86_64, .os_tag = .macos }, .{ .cpu_arch = .aarch64, .os_tag = .linux, .abi = .musl },};
pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.option(std.lang.Optimize, "optimize", "Optimization mode (default: debug)") orelse .debug;
const library = addLibrary(b, target, optimize); b.installArtifact(library);
const test_step = b.step("test", "Engine tests against a mock host, and the header compiled as C"); addTests(b, test_step, target, optimize);
const check = b.step("check", "Tests in Debug and ReleaseSafe, and the library for every cross target"); check.dependOn(test_step); if (optimize != .safe) addTests(b, check, target, .safe); for (cross_targets) |query| { check.dependOn(&addLibrary(b, b.resolveTargetQuery(query), .safe).step); }
// The engine on Windows: `zig build test-windows -fwine` runs it under Wine, a smoke test of // junctions and the Win32 file layer (Wine has no byte-range locks; the tests probe for them). const windows = b.step("test-windows", "The engine tests for x86_64-windows-gnu (run with -fwine)"); addTests(b, windows, b.resolveTargetQuery(.{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .gnu }), optimize);}
fn addLibrary(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize) *std.Build.Step.Compile { const module = b.createModule(.{ .target = target, .optimize = optimize, .link_libcpp = true }); module.addIncludePath(b.path("include")); module.addIncludePath(b.path("src")); module.addCSourceFiles(.{ .root = b.path("src"), .files = &sources, .flags = strict_flags }); const library = b.addLibrary(.{ .name = "packutils", .root_module = module, .linkage = .static }); library.installHeader(b.path("include/packutils.h"), "packutils.h"); return library;}
fn addTests(b: *std.Build, step: *std.Build.Step, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize) void { const library = addLibrary(b, target, optimize); const triple = target.result.zigTriple(b.allocator) catch @panic("OOM");
const engine = b.createModule(.{ .target = target, .optimize = optimize, .link_libcpp = true }); engine.addIncludePath(b.path("include")); engine.addIncludePath(b.path("src")); engine.addCSourceFiles(.{ .root = b.path("tests"), .files = &.{"engine_test.cpp"}, .flags = strict_flags }); engine.linkLibrary(library); const engine_exe = b.addExecutable(.{ .name = b.fmt("engine-test-{s}-{t}", .{ triple, optimize }), .root_module = engine }); step.dependOn(&b.addRunArtifact(engine_exe).step);
// The contract is C: the header must compile as strict C11 and link from C. const header = b.createModule(.{ .target = target, .optimize = optimize, .link_libcpp = true }); header.addIncludePath(b.path("include")); header.addCSourceFiles(.{ .root = b.path("tests"), .files = &.{"header_test.c"}, .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Wpedantic", "-Werror" } }); header.linkLibrary(library); const header_exe = b.addExecutable(.{ .name = b.fmt("header-test-{s}-{t}", .{ triple, optimize }), .root_module = header }); step.dependOn(&b.addRunArtifact(header_exe).step);}