From f2e672f84d8cd45971c415c67d69237d09a8033a Mon Sep 17 00:00:00 2001 From: admin Date: Sat, 24 May 2025 19:17:27 +0200 Subject: changed to sdl3 and added linux/cross-compilation --- Makefile | 294 +++++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 248 insertions(+), 46 deletions(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index 904806c..69b9f44 100644 --- a/Makefile +++ b/Makefile @@ -1,67 +1,269 @@ +# Simple Makefile for glamac - SDL3 version +# Supports: Arch Linux native, Windows cross-compilation CC=gcc -# Base Directories +MINGW_CC=x86_64-w64-mingw32-gcc + +# Directories SRCDIR=src BINDIR=bin +BINDIR_WIN=bin/win INCDIR=include -RESDIR=res + +# SDL3 paths for cross-compilation +CROSS_WIN_SDL3_PREFIX ?= /usr/x86_64-w64-mingw32 +CROSS_WIN_SDL3_INCLUDE ?= $(CROSS_WIN_SDL3_PREFIX)/include +CROSS_WIN_SDL3_LIB ?= $(CROSS_WIN_SDL3_PREFIX)/lib + +# DLL cache directory +DLL_CACHE_DIR = $(HOME)/.cache/glamac-dlls # Common flags -CFLAGS=-I$(INCDIR) -O2 -march=native -flto #-ffast-math +CFLAGS_BASE=-I$(INCDIR) -O2 -flto +CFLAGS_NATIVE=$(CFLAGS_BASE) -march=native +CFLAGS_CROSS=$(CFLAGS_BASE) -I$(CROSS_WIN_SDL3_INCLUDE) -# Resource compiler for Windows -WINDRES=windres +# OS Detection +ifeq ($(OS),Windows_NT) + # Windows host + EXE_EXT=.exe + MKDIR=mkdir + RM=del /Q + RMDIR=rmdir /s /q + SDL3_LDFLAGS=-lSDL3 -lSDL3_ttf -mwindows + NATIVE_CC=$(CC) + NATIVE_CFLAGS=$(CFLAGS_BASE) + NATIVE_LDFLAGS=$(SDL3_LDFLAGS) + NATIVE_EXT=.exe +else + # Linux host (Arch) + EXE_EXT= + MKDIR=mkdir -p + RM=rm -f + RMDIR=rm -rf + SDL3_LDFLAGS=$(shell pkg-config --cflags --libs sdl3 SDL3_ttf 2>/dev/null || echo -lSDL3 -lSDL3_ttf) -lm + NATIVE_CC=$(CC) + NATIVE_CFLAGS=$(CFLAGS_NATIVE) + NATIVE_LDFLAGS=$(SDL3_LDFLAGS) + NATIVE_EXT= + # Cross-compilation flags + CROSS_LDFLAGS=-L$(CROSS_WIN_SDL3_LIB) -lmingw32 -lSDL3 -lSDL3_ttf -mwindows -static-libgcc +endif -# Resource file definitions -RESOURCE_FILE=glamac.rc # Your resource script file with icon definitions -RESOURCE_OBJ=$(BINDIR)/glamac_res.o # The compiled resource object file +# Source files +GLAMAC_SRC_LIST=$(SRCDIR)/glamac/glamac.c \ + $(SRCDIR)/glamac/glamac_view.c \ + $(SRCDIR)/glamac/glamac_render.c \ + $(SRCDIR)/glamac/glamac_events.c \ + $(SRCDIR)/glamac/glass_data.c + +GLAMAC_SRC=$(wildcard $(GLAMAC_SRC_LIST)) +EXPECTED_GLAMAC_FILES=5 +FOUND_GLAMAC_FILES=$(words $(GLAMAC_SRC)) -# glautils GLA_SRCS=$(wildcard $(SRCDIR)/glautils/*.c) -GLA_EXES=$(patsubst $(SRCDIR)/glautils/%.c, $(BINDIR)/%.exe, $(GLA_SRCS)) - -# glamac -GLAMAC_SRC=$(SRCDIR)/glamac/glamac.c \ - $(SRCDIR)/glamac/glamac_view.c \ - $(SRCDIR)/glamac/glamac_render.c \ - $(SRCDIR)/glamac/glamac_events.c \ - $(SRCDIR)/glamac/glass_data.c - -# SDL2 Flags -SDL2_CFLAGS=-I$(USERPROFILE)/scoop/apps/sdl2/current/include/SDl2 -I$(USERPROFILE)/scoop/apps/sdl2-ttf/current/include/SDL2_ttf -O2 -march=native -flto -s #-ffast-math -SDL2_LDFLAGS=-L$(USERPROFILE)/scoop/apps/sdl2/current/lib -L$(USERPROFILE)/scoop/apps/sdl2-ttf/current/lib -lSDL2main -lSDL2 -lSDL2_ttf -mwindows -flto -SDL2_DLL_SRC=$(USERPROFILE)/scoop/apps/sdl2/current/lib/SDL2.dll -SDL2_tff_DLL_SRC=$(USERPROFILE)/scoop/apps/sdl2-ttf/current/lib/SDL2_ttf.dll -SDL2_DLL_DEST=$(BINDIR) - -# Setup target +GLA_EXES_NATIVE=$(patsubst $(SRCDIR)/glautils/%.c, $(BINDIR)/%$(NATIVE_EXT), $(GLA_SRCS)) +GLA_EXES_WIN=$(patsubst $(SRCDIR)/glautils/%.c, $(BINDIR_WIN)/%.exe, $(GLA_SRCS)) + +# Setup targets setup: - mkdir -p $(BINDIR) + $(MKDIR) $(BINDIR) + +setup-cross: + $(MKDIR) $(BINDIR) $(BINDIR_WIN) + +# Check source files +check-files: + @echo "Checking for glamac source files..." + @echo "Expected $(EXPECTED_GLAMAC_FILES) files, found $(FOUND_GLAMAC_FILES)" + @if [ $(FOUND_GLAMAC_FILES) -ne $(EXPECTED_GLAMAC_FILES) ]; then \ + echo "ERROR: Missing glamac source files!"; \ + exit 1; \ + fi + @echo "All glamac source files found." + +# Default: build for current platform +all: check-files setup glamac glautils + +# Native compilation +glamac: check-files $(BINDIR)/glamac$(NATIVE_EXT) +glautils: $(GLA_EXES_NATIVE) + +$(BINDIR)/glamac$(NATIVE_EXT): $(GLAMAC_SRC) + @echo "Compiling glamac..." + $(NATIVE_CC) $^ $(NATIVE_CFLAGS) $(NATIVE_LDFLAGS) -o $@ + +$(BINDIR)/%$(NATIVE_EXT): $(SRCDIR)/glautils/%.c + $(NATIVE_CC) $< $(NATIVE_CFLAGS) -o $@ -all: glautils glamac sdl2 structure +# Windows cross-compilation (Linux only) +ifeq ($(OS),Windows_NT) +# Windows host - no cross-compilation needed +glamac-win all-win: + @echo "Cross-compilation not needed on Windows. Use 'make all' instead." -glautils: $(GLA_EXES) -$(BINDIR)/%.exe: $(SRCDIR)/glautils/%.c - $(CC) $< $(CFLAGS) -o $@ +install-sdl3-cross download-dlls install-dlls copy-dlls-win: + @echo "Cross-compilation operations not available on Windows host." -# Compile resource file -$(RESOURCE_OBJ): $(RESOURCE_FILE) - $(WINDRES) -i $< -o $@ +else +# Linux host - cross-compilation available -# Link glamac with resources -glamac: $(BINDIR)/glamac.exe -$(BINDIR)/glamac.exe: $(GLAMAC_SRC) $(RESOURCE_OBJ) - $(CC) $^ $(CFLAGS) $(SDL2_CFLAGS) $(SDL2_LDFLAGS) -o $@ +# Check cross-compilation dependencies +check-cross-deps: + @echo "Checking cross-compilation dependencies..." + @which $(MINGW_CC) >/dev/null 2>&1 || (echo "ERROR: $(MINGW_CC) not found. Install with: sudo pacman -S mingw-w64-gcc" && exit 1) + @test -f $(CROSS_WIN_SDL3_INCLUDE)/SDL3/SDL.h || (echo "ERROR: SDL3 headers not found. Run 'make install-sdl3-cross'" && exit 1) + @test -f $(CROSS_WIN_SDL3_LIB)/libSDL3.dll.a || (echo "ERROR: SDL3 libraries not found. Run 'make install-sdl3-cross'" && exit 1) + @echo "Cross-compilation dependencies OK" -sdl2: - cp $(SDL2_DLL_SRC) $(SDL2_DLL_DEST) - cp $(SDL2_tff_DLL_SRC) $(SDL2_DLL_DEST) +# Windows targets +glamac-win: check-files check-cross-deps setup-cross $(BINDIR_WIN)/glamac.exe copy-dlls-win +all-win: glamac-win $(GLA_EXES_WIN) +$(BINDIR_WIN)/glamac.exe: $(GLAMAC_SRC) + @echo "Cross-compiling glamac for Windows..." + $(MINGW_CC) $^ $(CFLAGS_CROSS) $(CROSS_LDFLAGS) -o $@ + +$(BINDIR_WIN)/%.exe: $(SRCDIR)/glautils/%.c + $(MINGW_CC) $< $(CFLAGS_CROSS) -static-libgcc -o $@ + +# DLL Management + +# Download DLLs to cache (only once) +download-dlls: + @echo "Downloading SDL3 DLLs to cache..." + @$(MKDIR) $(DLL_CACHE_DIR) + + @if [ ! -f "$(DLL_CACHE_DIR)/SDL3.dll" ]; then \ + echo "Downloading SDL3.dll..."; \ + cd $(DLL_CACHE_DIR) && \ + wget -q https://github.com/libsdl-org/SDL/releases/download/release-3.2.10/SDL3-3.2.10-win32-x64.zip && \ + unzip -j SDL3-3.2.10-win32-x64.zip SDL3.dll && \ + rm SDL3-3.2.10-win32-x64.zip; \ + else \ + echo "SDL3.dll already cached"; \ + fi + + @if [ ! -f "$(DLL_CACHE_DIR)/SDL3_ttf.dll" ]; then \ + echo "Downloading SDL3_ttf.dll..."; \ + cd $(DLL_CACHE_DIR) && \ + wget -q https://github.com/libsdl-org/SDL_ttf/releases/download/release-3.2.2/SDL3_ttf-3.2.2-win32-x64.zip && \ + unzip -j SDL3_ttf-3.2.2-win32-x64.zip SDL3_ttf.dll && \ + rm SDL3_ttf-3.2.2-win32-x64.zip; \ + else \ + echo "SDL3_ttf.dll already cached"; \ + fi + + @echo "DLLs cached in $(DLL_CACHE_DIR)/" + +# Install DLLs to system location (optional, for system-wide access) +install-dlls: download-dlls + @echo "Installing DLLs to system location..." + sudo cp $(DLL_CACHE_DIR)/SDL3.dll $(CROSS_WIN_SDL3_PREFIX)/bin/ 2>/dev/null || \ + (sudo mkdir -p $(CROSS_WIN_SDL3_PREFIX)/bin && sudo cp $(DLL_CACHE_DIR)/SDL3.dll $(CROSS_WIN_SDL3_PREFIX)/bin/) + sudo cp $(DLL_CACHE_DIR)/SDL3_ttf.dll $(CROSS_WIN_SDL3_PREFIX)/bin/ + @echo "DLLs installed to $(CROSS_WIN_SDL3_PREFIX)/bin/" + +# Copy DLLs to Windows build directory +copy-dlls-win: download-dlls + @echo "Copying DLLs for Windows distribution..." + @$(MKDIR) $(BINDIR_WIN) + cp $(DLL_CACHE_DIR)/SDL3.dll $(BINDIR_WIN)/ + cp $(DLL_CACHE_DIR)/SDL3_ttf.dll $(BINDIR_WIN)/ + @echo "" + @echo "Windows distribution ready in $(BINDIR_WIN)/" + @ls -la $(BINDIR_WIN)/ + @echo "" + @echo "Copy the entire $(BINDIR_WIN)/ directory to your Windows machine." + +# Install SDL3 for cross-compilation +install-sdl3-cross: + @echo "Installing SDL3 development libraries for MinGW cross-compilation..." + @echo "Latest versions: SDL3 3.2.10, SDL3_ttf 3.2.2" + @read -p "Continue? [y/N] " confirm && [ "$$confirm" = "y" ] || exit 1 + + @which wget >/dev/null 2>&1 || (echo "ERROR: wget not found. Install with: sudo pacman -S wget" && exit 1) + @which $(MINGW_CC) >/dev/null 2>&1 || (echo "ERROR: MinGW not found. Install with: sudo pacman -S mingw-w64-gcc" && exit 1) + + sudo mkdir -p $(CROSS_WIN_SDL3_PREFIX)/include $(CROSS_WIN_SDL3_PREFIX)/lib + + @echo "Downloading SDL3 development libraries..." + cd /tmp && \ + wget -q https://github.com/libsdl-org/SDL/releases/download/release-3.2.10/SDL3-devel-3.2.10-mingw.tar.gz && \ + wget -q https://github.com/libsdl-org/SDL_ttf/releases/download/release-3.2.2/SDL3_ttf-devel-3.2.2-mingw.tar.gz && \ + tar -xzf SDL3-devel-3.2.10-mingw.tar.gz && \ + tar -xzf SDL3_ttf-devel-3.2.2-mingw.tar.gz && \ + sudo cp -r SDL3-3.2.10/x86_64-w64-mingw32/include/* $(CROSS_WIN_SDL3_PREFIX)/include/ && \ + sudo cp -r SDL3-3.2.10/x86_64-w64-mingw32/lib/* $(CROSS_WIN_SDL3_PREFIX)/lib/ && \ + sudo cp -r SDL3_ttf-3.2.2/x86_64-w64-mingw32/include/* $(CROSS_WIN_SDL3_PREFIX)/include/ && \ + sudo cp -r SDL3_ttf-3.2.2/x86_64-w64-mingw32/lib/* $(CROSS_WIN_SDL3_PREFIX)/lib/ && \ + rm -rf SDL3-3.2.10* SDL3_ttf-3.2.2* + + @echo "SDL3 cross-compilation libraries installed successfully!" + +endif + +# Arch Linux SDL3 installation +install-sdl3-arch: + @echo "Installing SDL3 on Arch Linux..." + sudo pacman -S sdl3 + @echo "Installing SDL3_ttf from AUR (using yay)..." + yay -S sdl3_ttf + @echo "SDL3 installation complete!" + +# Clean targets clean: - rm -f $(BINDIR)/* - rm -f structure.txt +ifeq ($(OS),Windows_NT) + @if exist $(BINDIR) $(RMDIR) $(BINDIR) +else + $(RMDIR) $(BINDIR) 2>/dev/null || true +endif + +clean-dlls: +ifneq ($(OS),Windows_NT) + $(RMDIR) $(DLL_CACHE_DIR) 2>/dev/null || true + @echo "DLL cache cleared" +else + @echo "DLL cache operations not available on Windows" +endif rebuild: clean all -# Generate project structure -structure: - @powershell -Command "(Get-Item .).Name | Set-Content structure.txt; (tree /a /f | Select-Object -Skip 3) | Add-Content structure.txt" +# Help +help: + @echo "GlaMaC Build System - SDL3" + @echo "" + @echo "Native targets (any platform):" + @echo " all - Build for current platform" + @echo " glamac - Build glamac only" + @echo " glautils - Build utilities only" + @echo " clean - Clean build artifacts" + @echo " rebuild - Clean and rebuild" + @echo "" +ifeq ($(OS),Windows_NT) + @echo "Windows host - use native targets above" +else + @echo "Cross-compilation targets (Arch Linux → Windows):" + @echo " glamac-win - Build glamac for Windows (includes DLLs)" + @echo " all-win - Build everything for Windows" + @echo "" + @echo "Setup and maintenance:" + @echo " install-sdl3-arch - Install SDL3 on Arch Linux" + @echo " install-sdl3-cross - Install SDL3 for cross-compilation" + @echo " download-dlls - Download Windows DLLs to cache" + @echo " install-dlls - Install DLLs to system location" + @echo " copy-dlls-win - Copy DLLs to Windows build directory" + @echo " clean-dlls - Clear DLL cache" + @echo " check-cross-deps - Check cross-compilation setup" +endif + @echo "" + @echo "Quick start:" +ifeq ($(OS),Windows_NT) + @echo " 1. Install SDL3: (install SDL3 development libraries)" + @echo " 2. make all" +else + @echo " 1. sudo pacman -S mingw-w64-gcc # Install cross-compiler" + @echo " 2. make install-sdl3-cross # Install SDL3 for Windows" + @echo " 3. make glamac-win # Build for Windows (auto-downloads DLLs)" +endif + +.PHONY: all setup setup-cross glamac glautils glamac-win all-win check-files check-cross-deps download-dlls install-dlls copy-dlls-win install-sdl3-cross install-sdl3-arch clean clean-dlls rebuild help -- cgit v1.2.3