From 7af642612b4aea6a8760e2554d5c5d4a67b169c9 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sun, 5 Apr 2026 17:00:17 +0300 Subject: [PATCH] build: migrate from Make to Zig build system + Just Signed-off-by: NotAShelf Change-Id: I7585121a5ec8e797adc43ba8e30d4ac86a6a6964 --- .editorconfig | 2 ++ .gitignore | 10 +++++-- Justfile | 20 ++++++++++++++ Makefile | 45 ------------------------------ build.zig | 73 +++++++++++++++++++++++++++++++++++++++++++++++++ nix/package.nix | 39 ++++++++++++++++++++++++-- nix/shell.nix | 7 +++-- 7 files changed, 144 insertions(+), 52 deletions(-) create mode 100644 Justfile delete mode 100644 Makefile create mode 100644 build.zig diff --git a/.editorconfig b/.editorconfig index 5b9877a..368d066 100644 --- a/.editorconfig +++ b/.editorconfig @@ -5,6 +5,8 @@ charset = utf-8 end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true +indent_size = 2 +tab_width = 2 [*.c] ident_style = space diff --git a/.gitignore b/.gitignore index a5869bb..a53104b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,12 @@ -# ignore build artifacts -result +# Nix +/.direnv/ +result* + +# Build artifacts build obj roguelike + +# Zig .zig-cache +.zig-out diff --git a/Justfile b/Justfile new file mode 100644 index 0000000..c70d068 --- /dev/null +++ b/Justfile @@ -0,0 +1,20 @@ +# Build the project +build: + zig build + +# Build and run +dev: + zig build run + +# Clean build artifacts +clean: + rm -rf zig-out .zig-cache + +# Format all C source files +fmt: + clang-format -i src/*.c src/*.h + zig fmt **/*.zig + +# Check formatting +fmt-check: + clang-format --dry-run --Werror src/*.c src/*.h diff --git a/Makefile b/Makefile deleted file mode 100644 index 607167e..0000000 --- a/Makefile +++ /dev/null @@ -1,45 +0,0 @@ -# Makefile for Roguelike Game -# Requires raylib, pkg-config - -CC := cc -CFLAGS := -Wall -Wextra -O2 -std=c99 -Isrc -LDFLAGS := -lraylib -lm -lpthread -ldl -lrt - -TARGET := roguelike -SRCDIR := src -OBJDIR := obj - -SOURCES := $(wildcard $(SRCDIR)/*.c) -OBJECTS := $(patsubst $(SRCDIR)/%.c,$(OBJDIR)/%.o,$(SOURCES)) - -.PHONY: all clean format format-check - -all: $(TARGET) - -$(TARGET): $(OBJECTS) - $(CC) $^ -o $@ $(LDFLAGS) - -$(OBJDIR)/%.o: $(SRCDIR)/%.c - @mkdir -p $(dir $@) - $(CC) $(CFLAGS) -c $< -o $@ - -clean: - rm -rf $(OBJDIR) $(TARGET) - -# Alias for development -dev: all - ./$(TARGET) - -# Format all source files with clang-format -fmt: - @command -v clang-format >/dev/null 2>&1 || { echo "Error: clang-format is missing"; exit 1; } - @echo "Formatting source files..." - @clang-format -i $(SOURCES) $(wildcard $(SRCDIR)/*.h) - @echo "Done formatting." - -# Check formatting without modifying files -fmt-check: - @command -v clang-format >/dev/null 2>&1 || { echo "Error: clang-format is missing"; exit 1; } - @echo "Checking formatting..." - @clang-format --dry-run --Werror \ - $(SOURCES) $(wildcard $(SRCDIR)/*.h) && echo "All files properly formatted." || { echo "Formatting issues found. Run 'make fmt' to fix."; exit 1; } diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..eb3ff5d --- /dev/null +++ b/build.zig @@ -0,0 +1,73 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + // Zig combat library + const combat_lib = b.addLibrary(.{ + .name = "combat", + .root_module = b.createModule(.{ + .root_source_file = b.path("libs/combat/combat.zig"), + .target = target, + .optimize = optimize, + .link_libc = true, + }), + }); + combat_lib.addIncludePath(b.path("src")); + + // C sources (everything except combat, which is now Zig) + const c_sources = [_][]const u8{ + "src/audio.c", + "src/enemy.c", + "src/items.c", + "src/main.c", + "src/map.c", + "src/player.c", + "src/render.c", + "src/rng.c", + "src/settings.c", + "src/utils.c", + }; + + const c_flags = [_][]const u8{ + "-std=c99", + "-Wall", + "-Wextra", + "-O2", + }; + + // Main executable + const exe = b.addExecutable(.{ + .name = "roguelike", + .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + .link_libc = true, + }), + }); + + exe.addCSourceFiles(.{ + .files = &c_sources, + .flags = &c_flags, + }); + + exe.addIncludePath(b.path("src")); + exe.linkLibrary(combat_lib); + exe.linkSystemLibrary("raylib"); + exe.linkSystemLibrary("m"); + exe.linkSystemLibrary("pthread"); + exe.linkSystemLibrary("dl"); + exe.linkSystemLibrary("rt"); + + b.installArtifact(exe); + + // Run step + const run_cmd = b.addRunArtifact(exe); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + const run_step = b.step("run", "Build and run the roguelike"); + run_step.dependOn(&run_cmd.step); +} diff --git a/nix/package.nix b/nix/package.nix index a097561..8fb5c18 100644 --- a/nix/package.nix +++ b/nix/package.nix @@ -1,6 +1,12 @@ -{stdenv}: +{ + lib, + stdenv, + zig, + raylib, + pkg-config, +}: stdenv.mkDerivation (finalAttrs: { - pname = "sample-c-cpp"; + pname = "rogged"; version = "0.0.1"; src = builtins.path { @@ -8,5 +14,32 @@ stdenv.mkDerivation (finalAttrs: { name = finalAttrs.pname; }; - makeFlags = ["PREFIX=$(out)"]; + nativeBuildInputs = [ + zig + pkg-config + ]; + + buildInputs = [raylib]; + + dontConfigure = true; + + buildPhase = '' + runHook preBuild + export ZIG_GLOBAL_CACHE_DIR="$TMPDIR/zig-cache" + zig build --release=fast + runHook postBuild + ''; + + installPhase = '' + runHook preInstall + mkdir -p $out/bin + cp zig-out/bin/roguelike $out/bin/ + runHook postInstall + ''; + + meta = { + description = "A turn-based roguelike game"; + license = lib.licenses.mit; + mainProgram = "roguelike"; + }; }) diff --git a/nix/shell.nix b/nix/shell.nix index 23ce860..663f55b 100644 --- a/nix/shell.nix +++ b/nix/shell.nix @@ -2,9 +2,10 @@ mkShell, clang-tools, raylib, - gnumake, pkg-config, + just, zig, + zig-zlint, }: mkShell { strictDeps = true; @@ -14,7 +15,9 @@ mkShell { nativeBuildInputs = [ pkg-config clang-tools - gnumake + just + zig + zig-zlint ]; }