From 20aff7b597189196ea5ef9fcc29767130a4f6cfd Mon Sep 17 00:00:00 2001 From: Adam Malczewski Date: Mon, 8 Jun 2026 11:59:24 +0900 Subject: build: add Linux native target with static raylib linking - Default target (make) now builds for Linux native (gcc) - Windows cross-compile preserved as x86_64-w64-mingw32-gcc -std=c99 -O2 -Ibuild -Ideps/raylib/src -Ideps/raylib/src/external/glfw/include -Wall -Wextra -DPLATFORM_DESKTOP -D_GLFW_WIN32 -o build/study-player.exe build/main.o build/libraylib.a -lgdi32 -lwinmm -lcomdlg32 -lole32 - Raylib built from source and statically linked (no .so/.dll) - Font embedded at compile time via xxd (best-effort) - Auto-discovery of src/*.c files via wildcard - Target-specific variables for clean dual-target architecture --- Makefile | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 96 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index 283b081..be802da 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,9 @@ # Study Player Makefile -# Builds raylib as a static library, then compiles the application for Windows. -# Usage: -# make - Build for Windows (cross-compile with MinGW) -# make clean - Remove build artifacts +# Default target: Linux native (gcc) +# make windows - Cross-compile for Windows (MinGW) +# make clean - Remove build artifacts +# +# Builds raylib from source as a static library, then links the application. # Directories SRC_DIR := src @@ -10,18 +11,9 @@ BUILD_DIR := build RAYLIB_SRC := deps/raylib/src RAYLIB_LIB := $(BUILD_DIR)/libraylib.a -# Source files -APP_SRC := $(SRC_DIR)/main.c -FONT_HEADER := $(BUILD_DIR)/font_data.h - -CC := x86_64-w64-mingw32-gcc -APP_OUT := $(BUILD_DIR)/study-player.exe -LDFLAGS := -mwindows -lgdi32 -lwinmm -lcomdlg32 -lole32 -DEFINES := -DPLATFORM_DESKTOP -D_GLFW_WIN32 - -CFLAGS_COMMON := -std=c99 -O2 -I$(RAYLIB_SRC) -I$(RAYLIB_SRC)/external/glfw/include -I$(BUILD_DIR) -CFLAGS := $(CFLAGS_COMMON) -Wall -Wextra -CFLAGS_RAYLIB := $(CFLAGS_COMMON) -w +# Application source files (auto-discovered) +SRCS := $(wildcard $(SRC_DIR)/*.c) +APP_OBJS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/%.o,$(SRCS)) # Raylib source files RAYLIB_SRCS := $(RAYLIB_SRC)/rcore.c $(RAYLIB_SRC)/rshapes.c $(RAYLIB_SRC)/rtextures.c \ @@ -29,27 +21,107 @@ RAYLIB_SRCS := $(RAYLIB_SRC)/rcore.c $(RAYLIB_SRC)/rshapes.c $(RAYLIB_SRC)/rtext $(RAYLIB_SRC)/rglfw.c RAYLIB_OBJS := $(patsubst $(RAYLIB_SRC)/%.c,$(BUILD_DIR)/raylib/%.o,$(RAYLIB_SRCS)) -.PHONY: all clean +# Common include paths +COMMON_INCS := -I$(BUILD_DIR) -I$(RAYLIB_SRC) -I$(RAYLIB_SRC)/external/glfw/include + +# --------------------------------------------------------------------------- +# Linux native configuration (default target) +# --------------------------------------------------------------------------- +CC_LINUX := gcc +APP_OUT := $(BUILD_DIR)/study-player +DEFINES_LINUX := -DPLATFORM_DESKTOP -DPLATFORM_LINUX -D_GLFW_X11 +LDFLAGS_LINUX := -lm -lrt -ldl -lpthread -lX11 + +# --------------------------------------------------------------------------- +# Windows cross-compile configuration +# --------------------------------------------------------------------------- +CC_WIN := x86_64-w64-mingw32-gcc +APP_OUT_WIN := $(BUILD_DIR)/study-player.exe +DEFINES_WIN := -DPLATFORM_DESKTOP -D_GLFW_WIN32 +LDFLAGS_WIN := -lgdi32 -lwinmm -lcomdlg32 -lole32 + +# CFLAGS +CFLAGS_COMMON := -std=c99 -O2 $(COMMON_INCS) +CFLAGS_APP := $(CFLAGS_COMMON) -Wall -Wextra +CFLAGS_RAYLIB := $(CFLAGS_COMMON) -w + +# --------------------------------------------------------------------------- +# Font header +# --------------------------------------------------------------------------- +FONT_HEADER := $(BUILD_DIR)/font_data.h + +.PHONY: all windows clean -all: $(APP_OUT) +# --------------------------------------------------------------------------- +# Default target: Linux native +# --------------------------------------------------------------------------- +all: CC := $(CC_LINUX) +all: AR := ar +all: DEFINES := $(DEFINES_LINUX) +all: LDFLAGS := $(LDFLAGS_LINUX) +all: $(FONT_HEADER) $(APP_OUT) -# Build application (font_data.h is generated by bin/build before invoking make) -$(APP_OUT): $(APP_SRC) $(FONT_HEADER) $(RAYLIB_LIB) | $(BUILD_DIR) - $(CC) $(CFLAGS) $(DEFINES) -o $@ $< -L$(BUILD_DIR) -lraylib $(LDFLAGS) +# --------------------------------------------------------------------------- +# Windows cross-compile +# --------------------------------------------------------------------------- +windows: CC := $(CC_WIN) +windows: AR := x86_64-w64-mingw32-ar +windows: DEFINES := $(DEFINES_WIN) +windows: LDFLAGS := $(LDFLAGS_WIN) +windows: $(FONT_HEADER) $(APP_OUT_WIN) + +# --------------------------------------------------------------------------- +# Link rules +# --------------------------------------------------------------------------- +# Direct static link of raylib .a (no -lraylib to avoid picking up shared lib) +$(APP_OUT) $(APP_OUT_WIN): $(APP_OBJS) $(RAYLIB_LIB) | $(BUILD_DIR) + $(CC) $(CFLAGS_APP) $(DEFINES) -o $@ $(APP_OBJS) $(RAYLIB_LIB) $(LDFLAGS) -# Archive raylib objects into static library $(RAYLIB_LIB): $(RAYLIB_OBJS) | $(BUILD_DIR) - ar rcs $@ $^ + $(AR) rcs $@ $^ + +# --------------------------------------------------------------------------- +# Compile rules +# --------------------------------------------------------------------------- +$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c $(FONT_HEADER) | $(BUILD_DIR) + $(CC) $(CFLAGS_APP) $(DEFINES) -c -o $@ $< -# Compile raylib source files $(BUILD_DIR)/raylib/%.o: $(RAYLIB_SRC)/%.c | $(BUILD_DIR)/raylib $(CC) $(CFLAGS_RAYLIB) $(DEFINES) -c -o $@ $< +# --------------------------------------------------------------------------- +# Font header generation (best-effort) +# --------------------------------------------------------------------------- +# Discovers the first .otf/.ttf in resources/ at shell-level to handle +# spaces in filenames. Uses xxd if available; otherwise creates a stub. +$(FONT_HEADER): | $(BUILD_DIR) + @FONT_FILE="$$(find resources -maxdepth 1 -type f \( -iname '*.otf' -o -iname '*.ttf' \) -print -quit 2>/dev/null)"; \ + if command -v xxd >/dev/null 2>&1 && [ -n "$$FONT_FILE" ]; then \ + echo " XXD $$(basename "$$FONT_FILE") -> $@"; \ + xxd -i "$$FONT_FILE" > "$@.tmp"; \ + VARNAME=$$(grep -oP 'unsigned char \K[a-zA-Z0-9_]+' "$@.tmp" | head -1); \ + sed -i "s/$${VARNAME}/embedded_font_data/g" "$@.tmp"; \ + echo '#define FONT_EMBEDDED 1' >> "$@.tmp"; \ + mv "$@.tmp" "$@"; \ + elif [ -n "$$FONT_FILE" ]; then \ + echo " WARN xxd not available, using stub font header"; \ + printf '/* No font embedded - xxd not available */\nstatic unsigned char embedded_font_data[] = {0};\nstatic unsigned int embedded_font_data_len = 0;\n#define FONT_EMBEDDED 0\n' > "$@"; \ + else \ + echo " INFO No font file found, using default raylib font"; \ + printf '/* No font embedded - no font file */\nstatic unsigned char embedded_font_data[] = {0};\nstatic unsigned int embedded_font_data_len = 0;\n#define FONT_EMBEDDED 0\n' > "$@"; \ + fi + +# --------------------------------------------------------------------------- +# Directory creation +# --------------------------------------------------------------------------- $(BUILD_DIR): mkdir -p $(BUILD_DIR) $(BUILD_DIR)/raylib: mkdir -p $(BUILD_DIR)/raylib +# --------------------------------------------------------------------------- +# Clean +# --------------------------------------------------------------------------- clean: rm -rf $(BUILD_DIR) -- cgit v1.2.3