From 6b82dc677ca507062a090379a8d22026f9ca0c75 Mon Sep 17 00:00:00 2001 From: anth64 Date: Sun, 13 Sep 2026 14:32:41 +0200 Subject: [PATCH] build: add make scaffold (gmake/bmake/build.sh/build.bat) --- bmake.mk | 77 +++++++++++++++++++++++++++++++++++ build.bat | 2 + build.sh | 39 ++++++++++++++++++ compile_flags.txt | 6 +++ config.mk | 13 ++++++ gmake.mk | 101 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 238 insertions(+) create mode 100644 bmake.mk create mode 100755 build.bat create mode 100755 build.sh create mode 100644 compile_flags.txt create mode 100644 config.mk create mode 100644 gmake.mk diff --git a/bmake.mk b/bmake.mk new file mode 100644 index 0000000..7ad75e5 --- /dev/null +++ b/bmake.mk @@ -0,0 +1,77 @@ +.include "config.mk" + +PREFIX ?= /usr/local +LIBDIR ?= ${PREFIX}/lib +INCDIR ?= ${PREFIX}/include + +STATIC_LIB = lib${LIB_NAME}.a +CFLAGS_BASE = -Wall -Wpedantic -I${.CURDIR}/${INC_DIR} -std=c89 + +.PHONY: all debug release tools clean test install uninstall + +all: debug tools + +OBJS_DEBUG = ${SRCS:S|^src/|${.CURDIR}/obj/debug/|:S/.c$/.o/} +OBJS_RELEASE = ${SRCS:S|^src/|${.CURDIR}/obj/release/|:S/.c$/.o/} + +TOOL_DEBUG_BINS = ${TOOL_SRCS:S|^tools/|${.CURDIR}/${BIN_DIR}/debug/tools/|:S/.c$//} +TOOL_RELEASE_BINS = ${TOOL_SRCS:S|^tools/|${.CURDIR}/${BIN_DIR}/release/tools/|:S/.c$//} + +debug: ${.CURDIR}/${BIN_DIR}/debug/${STATIC_LIB} +release: ${.CURDIR}/${BIN_DIR}/release/${STATIC_LIB} + +tools: ${TOOL_DEBUG_BINS} + +${.CURDIR}/${BIN_DIR}/debug/${STATIC_LIB}: ${OBJS_DEBUG} + @mkdir -p ${.TARGET:H} + ar rcs ${.TARGET} ${.ALLSRC} + +${.CURDIR}/${BIN_DIR}/release/${STATIC_LIB}: ${OBJS_RELEASE} + @mkdir -p ${.TARGET:H} + ar rcs ${.TARGET} ${.ALLSRC} + +.for _src in ${SRCS} +${.CURDIR}/obj/debug/${_src:S|^src/||:S/.c$/.o/}: ${.CURDIR}/${_src} + @mkdir -p ${.TARGET:H} + ${CC} ${CFLAGS_BASE} -g -O0 -MMD -MP -MT ${.TARGET} -MF ${.TARGET:S/.o$/.d/} -c ${.ALLSRC} -o ${.TARGET} + +${.CURDIR}/obj/release/${_src:S|^src/||:S/.c$/.o/}: ${.CURDIR}/${_src} + @mkdir -p ${.TARGET:H} + ${CC} ${CFLAGS_BASE} -O2 -MMD -MP -MT ${.TARGET} -MF ${.TARGET:S/.o$/.d/} -c ${.ALLSRC} -o ${.TARGET} +.endfor + +.for _src in ${TOOL_SRCS} +${.CURDIR}/obj/debug/tools/${_src:S|^tools/||:S/.c$/.o/}: ${.CURDIR}/${_src} + @mkdir -p ${.TARGET:H} + ${CC} ${CFLAGS_BASE} -g -O0 -MMD -MP -MT ${.TARGET} -MF ${.TARGET:S/.o$/.d/} -c ${.ALLSRC} -o ${.TARGET} + +${.CURDIR}/${BIN_DIR}/debug/tools/${_src:S|^tools/||:S/.c$//}: ${.CURDIR}/obj/debug/tools/${_src:S|^tools/||:S/.c$/.o/} ${.CURDIR}/${BIN_DIR}/debug/${STATIC_LIB} + @mkdir -p ${.TARGET:H} + ${CC} -o ${.TARGET} ${.CURDIR}/obj/debug/tools/${_src:S|^tools/||:S/.c$/.o/} -L${.CURDIR}/${BIN_DIR}/debug -l${LIB_NAME} + +${.CURDIR}/obj/release/tools/${_src:S|^tools/||:S/.c$/.o/}: ${.CURDIR}/${_src} + @mkdir -p ${.TARGET:H} + ${CC} ${CFLAGS_BASE} -O2 -MMD -MP -MT ${.TARGET} -MF ${.TARGET:S/.o$/.d/} -c ${.ALLSRC} -o ${.TARGET} + +${.CURDIR}/${BIN_DIR}/release/tools/${_src:S|^tools/||:S/.c$//}: ${.CURDIR}/obj/release/tools/${_src:S|^tools/||:S/.c$/.o/} ${.CURDIR}/${BIN_DIR}/release/${STATIC_LIB} + @mkdir -p ${.TARGET:H} + ${CC} -O2 -s -o ${.TARGET} ${.CURDIR}/obj/release/tools/${_src:S|^tools/||:S/.c$/.o/} -L${.CURDIR}/${BIN_DIR}/release -l${LIB_NAME} +.endfor + +clean: + rm -rf ${.CURDIR}/${OBJ_DIR} ${.CURDIR}/${BIN_DIR} + +test: debug + @echo "=== Building and running fpk tests ===" + cd ${.CURDIR}/test && ${MAKE} -f bmake.mk + +install: + @test -f ${.CURDIR}/${BIN_DIR}/release/${STATIC_LIB} || { echo "Run 'make -f bmake.mk release' before installing."; exit 1; } + install -d ${LIBDIR} ${INCDIR}/fpk + install -m 644 ${.CURDIR}/${BIN_DIR}/release/${STATIC_LIB} ${LIBDIR}/ + install -m 644 ${.CURDIR}/${INC_DIR}/fpk.h ${INCDIR}/fpk/ + install -m 644 ${.CURDIR}/${INC_DIR}/fpk_version.h ${INCDIR}/fpk/ + +uninstall: + rm -f ${LIBDIR}/${STATIC_LIB} + rm -rf ${INCDIR}/fpk diff --git a/build.bat b/build.bat new file mode 100755 index 0000000..00d0faf --- /dev/null +++ b/build.bat @@ -0,0 +1,2 @@ +@echo off +make -f gmake.mk %* diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..f14e46a --- /dev/null +++ b/build.sh @@ -0,0 +1,39 @@ +#!/bin/sh + +OS=$(uname -s | tr '[:upper:]' '[:lower:]') + +if [ "$OS" = "linux" ]; then + MK_FILE="gmake.mk" +else + MK_FILE="bmake.mk" +fi + +HAS_INSTALL=0 +HAS_UNINSTALL=0 +for arg in "$@"; do + case "$arg" in + install) HAS_INSTALL=1 ;; + uninstall) HAS_UNINSTALL=1 ;; + esac +done + +if [ "$HAS_INSTALL" = "1" ] || [ "$HAS_UNINSTALL" = "1" ]; then + if [ "$HAS_INSTALL" = "1" ]; then + make -f "$MK_FILE" release + fi + + if [ "$(id -u)" = "0" ]; then + PRIV="" + elif command -v doas > /dev/null 2>&1; then + PRIV="doas" + elif command -v sudo > /dev/null 2>&1; then + PRIV="sudo" + else + echo "error: install requires root. neither doas nor sudo found." >&2 + exit 1 + fi + + $PRIV make -f "$MK_FILE" "$@" +else + make -f "$MK_FILE" "$@" +fi diff --git a/compile_flags.txt b/compile_flags.txt new file mode 100644 index 0000000..36c3937 --- /dev/null +++ b/compile_flags.txt @@ -0,0 +1,6 @@ +-xc +-std=c89 +-Wall +-Wpedantic +-Iinclude + diff --git a/config.mk b/config.mk new file mode 100644 index 0000000..b44e631 --- /dev/null +++ b/config.mk @@ -0,0 +1,13 @@ +SRC_DIR = src +INC_DIR = include +OBJ_DIR = obj +BIN_DIR = bin + +LIB_NAME = fpk +STATIC_LIB = lib$(LIB_NAME).a + +SRCS = src/fpk.c + +TOOL_SRCS = \ + tools/fpack.c \ + tools/funpack.c diff --git a/gmake.mk b/gmake.mk new file mode 100644 index 0000000..febf128 --- /dev/null +++ b/gmake.mk @@ -0,0 +1,101 @@ +include config.mk + +ifeq ($(OS),Windows_NT) + SHELL := cmd.exe + STATIC_LIB := lib$(LIB_NAME).a + CFLAGS_PLAT := + MKDIR = if not exist $(subst /,\,$(1)) mkdir $(subst /,\,$(1)) + RMDIR = if exist $(subst /,\,$(1)) rd /s /q $(subst /,\,$(1)) + TOOL_EXT := .exe +else + STATIC_LIB := lib$(LIB_NAME).a + CFLAGS_PLAT := + MKDIR = mkdir -p $(1) + RMDIR = rm -rf $(1) + TOOL_EXT := +endif + +RELEASE_LDFLAGS := -s +CFLAGS_BASE := -Wall -Wpedantic -I$(INC_DIR) -std=c89 $(CFLAGS_PLAT) + +PREFIX ?= /usr +LIBDIR ?= $(PREFIX)/lib +INCDIR ?= $(PREFIX)/include + +TOOL_DEBUG_BINS := $(TOOL_SRCS:tools/%.c=$(BIN_DIR)/debug/tools/%$(TOOL_EXT)) +TOOL_RELEASE_BINS := $(TOOL_SRCS:tools/%.c=$(BIN_DIR)/release/tools/%$(TOOL_EXT)) + +.PHONY: all debug release tools clean test install uninstall + +all: debug tools + +debug: $(BIN_DIR)/debug/$(STATIC_LIB) +release: $(BIN_DIR)/release/$(STATIC_LIB) + +tools: $(TOOL_DEBUG_BINS) + +$(BIN_DIR)/debug/$(STATIC_LIB): $(SRCS:src/%.c=obj/debug/%.o) + @$(call MKDIR,$(@D)) + ar rcs $@ $^ + +obj/debug/%.o: src/%.c + @$(call MKDIR,$(@D)) + $(CC) $(CFLAGS_BASE) -g -O0 -MMD -MP -c $< -o $@ + +$(BIN_DIR)/release/$(STATIC_LIB): $(SRCS:src/%.c=obj/release/%.o) + @$(call MKDIR,$(@D)) + ar rcs $@ $^ + +obj/release/%.o: src/%.c + @$(call MKDIR,$(@D)) + $(CC) $(CFLAGS_BASE) -O2 -MMD -MP -c $< -o $@ + +obj/debug/tools/%.o: tools/%.c + @$(call MKDIR,$(@D)) + $(CC) $(CFLAGS_BASE) -g -O0 -MMD -MP -c $< -o $@ + +$(BIN_DIR)/debug/tools/%$(TOOL_EXT): obj/debug/tools/%.o $(BIN_DIR)/debug/$(STATIC_LIB) + @$(call MKDIR,$(@D)) + $(CC) -o $@ $< -L$(BIN_DIR)/debug -l$(LIB_NAME) + +obj/release/tools/%.o: tools/%.c + @$(call MKDIR,$(@D)) + $(CC) $(CFLAGS_BASE) -O2 -MMD -MP -c $< -o $@ + +$(BIN_DIR)/release/tools/%$(TOOL_EXT): obj/release/tools/%.o $(BIN_DIR)/release/$(STATIC_LIB) + @$(call MKDIR,$(@D)) + $(CC) $(RELEASE_LDFLAGS) -o $@ $< -L$(BIN_DIR)/release -l$(LIB_NAME) + +-include $(wildcard obj/debug/*.d) +-include $(wildcard obj/debug/tools/*.d) +-include $(wildcard obj/release/*.d) +-include $(wildcard obj/release/tools/*.d) + +clean: + @$(call RMDIR,$(OBJ_DIR)) + @$(call RMDIR,$(BIN_DIR)) + +test: debug + @echo "=== Building and running fpk tests ===" + @$(MAKE) -C test -f gmake.mk + +ifneq ($(OS),Windows_NT) +install: + @test -f $(BIN_DIR)/release/$(STATIC_LIB) || { echo "Run 'make -f gmake.mk release' before installing."; exit 1; } + install -d $(LIBDIR) $(INCDIR)/fpk + install -m 644 $(BIN_DIR)/release/$(STATIC_LIB) $(LIBDIR)/ + install -m 644 $(INC_DIR)/fpk.h $(INCDIR)/fpk/ + install -m 644 $(INC_DIR)/fpk_version.h $(INCDIR)/fpk/ + +uninstall: + rm -f $(LIBDIR)/$(STATIC_LIB) + rm -rf $(INCDIR)/fpk +else +install: + @echo "make install is not supported on Windows." + @echo "Copy include/ directory contents to your_project/include/fpk/" + @echo "Copy bin/release/libfpk.a to your project's lib directory." + +uninstall: + @echo "make uninstall is not supported on Windows." +endif