From e87bfb7508799bffa2a3cc0150ad605bfb5b58e9 Mon Sep 17 00:00:00 2001 From: tazend Date: Sun, 14 Apr 2024 13:20:11 +0200 Subject: [PATCH] update build.zig for zig 0.12.0-dev.3635+786876c05 - remove unused variables - Passing "target" as "CrossTarget" to the buildExamples function throws an error expecting a different type, simply pass it as the resolved target. - make the "prettytable" module available for other projects to import as a dependency. Without "b.addModule('prettytable')...", it isn't possible to import it. Also simply pass this module then on to the buildExample function, so the tests can also use it, without needing to call createModule again. --- build.zig | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/build.zig b/build.zig index 26e77c8..de0d016 100644 --- a/build.zig +++ b/build.zig @@ -1,9 +1,7 @@ const std = @import("std"); -const Module = std.build.Module; -const Builder = std.build.Builder; +const Module = std.Build.Module; const Mode = std.builtin.Mode; -const CrossTarget = std.zig.CrossTarget; -const FileSource = std.build.FileSource; +const ResolvedTarget = std.Build.ResolvedTarget; // Although this function looks imperative, note that its job is to // declaratively construct a build graph that will be executed by an external @@ -20,6 +18,10 @@ pub fn build(b: *std.Build) void { // set a preferred release mode, allowing the user to decide how to optimize. const optimize = b.standardOptimizeOption(.{}); + const module = b.addModule("prettytable", .{ + .root_source_file = .{ .path = "src/lib.zig" }, + }); + const lib = b.addStaticLibrary(.{ .name = "prettytable-zig", // In this case the main source file is merely a path, however, in more @@ -99,13 +101,13 @@ pub fn build(b: *std.Build) void { test_step.dependOn(&run_table_tests.step); test_step.dependOn(&run_style_tests.step); - buildExample(b, optimize, target, &.{ "basic", "format", "multiline", "align", "read", "style" }); + buildExample(b, optimize, target, module, &.{ "basic", "format", "multiline", "align", "read", "style" }); } // Although this function looks imperative, note that its job is to // declaratively construct a build graph that will be executed by an external // runner. -pub fn buildExample(b: *std.Build, optimize: Mode, target: CrossTarget, comptime source: []const []const u8) void { +pub fn buildExample(b: *std.Build, optimize: Mode, target: ResolvedTarget, module: *Module, comptime source: []const []const u8) void { inline for (source) |s| { const exe = b.addExecutable(.{ .name = s, @@ -116,9 +118,7 @@ pub fn buildExample(b: *std.Build, optimize: Mode, target: CrossTarget, comptime .optimize = optimize, }); - const pkg_prettytable = b.createModule(.{ .source_file = FileSource{ .path = "src/lib.zig" } }); - - exe.addModule("prettytable", pkg_prettytable); + exe.root_module.addImport("prettytable", module); // This declares intent for the executable to be installed into the // standard location when the user invokes the "install" step (the default