From 3c06f4697aeb3a6a8ff93c6bc6900e34ccacd414 Mon Sep 17 00:00:00 2001 From: Patrick Cleavelin Date: Fri, 23 Jun 2023 22:19:03 -0500 Subject: [PATCH] switch to being a module that can be imported (although it can't be imported gracefully because of its coupling with raylib) --- build.zig | 64 ++++++++++++++++++++++------------------------------- src/lib.zig | 5 +++-- 2 files changed, 29 insertions(+), 40 deletions(-) diff --git a/build.zig b/build.zig index 869b8f5..b3cbaed 100644 --- a/build.zig +++ b/build.zig @@ -16,59 +16,47 @@ 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 exe = b.addExecutable(.{ + const lib = b.addStaticLibrary(.{ .name = "zooy", // In this case the main source file is merely a path, however, in more // complicated build scripts, this could be a generated file. - .root_source_file = .{ .path = "src/main.zig" }, + .root_source_file = .{ .path = "src/lib.zig" }, .target = target, .optimize = optimize, }); - // Add raylib - raylib.addTo(b, exe, target, optimize); + raylib.addTo(b, lib, target, optimize); - // This declares intent for the executable to be installed into the - // standard location when the user invokes the "install" step (the default - // step when running `zig build`). - b.installArtifact(exe); - - // This *creates* a Run step in the build graph, to be executed when another - // step is evaluated that depends on it. The next line below will establish - // such a dependency. - const run_cmd = b.addRunArtifact(exe); - - // By making the run step depend on the install step, it will be run from the - // installation directory rather than directly from within the cache directory. - // This is not necessary, however, if the application depends on other installed - // files, this ensures they will be present and in the expected location. - run_cmd.step.dependOn(b.getInstallStep()); - - // This allows the user to pass arguments to the application in the build - // command itself, like this: `zig build run -- arg1 arg2 etc` - if (b.args) |args| { - run_cmd.addArgs(args); - } - - // This creates a build step. It will be visible in the `zig build --help` menu, - // and can be selected like this: `zig build run` - // This will evaluate the `run` step rather than the default, which is "install". - const run_step = b.step("run", "Run the app"); - run_step.dependOn(&run_cmd.step); + // This declares intent for the library to be installed into the standard + // location when the user invokes the "install" step (the default step when + // running `zig build`). + b.installArtifact(lib); // Creates a step for unit testing. This only builds the test executable // but does not run it. - const unit_tests = b.addTest(.{ + const main_tests = b.addTest(.{ .root_source_file = .{ .path = "src/main.zig" }, .target = target, .optimize = optimize, }); - const run_unit_tests = b.addRunArtifact(unit_tests); + const run_main_tests = b.addRunArtifact(main_tests); - // Similar to creating the run step earlier, this exposes a `test` step to - // the `zig build --help` menu, providing a way for the user to request - // running the unit tests. - const test_step = b.step("test", "Run unit tests"); - test_step.dependOn(&run_unit_tests.step); + // This creates a build step. It will be visible in the `zig build --help` menu, + // and can be selected like this: `zig build test` + // This will evaluate the `test` step rather than the default, which is "install". + const test_step = b.step("test", "Run library tests"); + test_step.dependOn(&run_main_tests.step); +} + +fn current_file() []const u8 { + return @src().file; +} + +const cwd = std.fs.path.dirname(current_file()).?; +const sep = std.fs.path.sep_str; + +pub fn addTo(exe: *std.build.LibExeObjStep) void { + exe.addAnonymousModule("zooy", .{ .source_file = .{ .path = cwd ++ sep ++ "src/lib.zig" } }); + exe.addIncludePath(cwd); } diff --git a/src/lib.zig b/src/lib.zig index b2b5a10..71304bd 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -1,7 +1,8 @@ const std = @import("std"); -// TODO: abstract raylib away to allow for consumers to use whatever they want -const raylib = @import("raylib"); +// TODO: abstract raylib away to allow for consumers to use whatever they want. +// I'm also rexporting here because the zig build system hurts +pub const raylib = @import("raylib"); // TODO: don't just make these public pub var box_allocator: std.mem.Allocator = undefined;