CC=gcc # Cross-compilation (only used on Linux) MINGW_CC=x86_64-w64-mingw32-gcc #Base Directories SRCDIR=src BINDIR=bin BINDIR_WIN=bin/win INCDIR=include #Common flags CFLAGS_BASE=-I$(INCDIR) -O2 -flto CFLAGS_NATIVE=$(CFLAGS_BASE) -march=native #-ffast-math CFLAGS_PORTABLE=$(CFLAGS_BASE) #-ffast-math (no -march=native for portability) # OS Detection ifeq ($(OS),Windows_NT) # Windows host EXE_EXT=.exe MKDIR=mkdir RM=del /Q LDFLAGS= TREE_CMD=powershell -Command "(Get-Item .).Name | Set-Content structure.txt; (tree /a /f | Select-Object -Skip 3) | Add-Content structure.txt" # On Windows: compile Windows binaries without -march=native for portability NATIVE_CC=$(CC) NATIVE_CFLAGS=$(CFLAGS_PORTABLE) NATIVE_LDFLAGS=$(LDFLAGS) NATIVE_EXT=.exe else # Linux host EXE_EXT= MKDIR=mkdir -p RM=rm -f LDFLAGS=-lm TREE_CMD=basename $(PWD) > structure.txt; tree -a -F >> structure.txt 2>/dev/null || (find . -type f -name "*.c" -o -name "*.h" -o -name "Makefile" | sort >> structure.txt) # On Linux: compile Linux binaries with -march=native for performance NATIVE_CC=$(CC) NATIVE_CFLAGS=$(CFLAGS_NATIVE) NATIVE_LDFLAGS=$(LDFLAGS) NATIVE_EXT= endif #tolutils TOL_SRCS = $(wildcard $(SRCDIR)/*.c) TOL_EXES_NATIVE = $(patsubst $(SRCDIR)/%.c, $(BINDIR)/%$(NATIVE_EXT), $(TOL_SRCS)) TOL_EXES_WIN = $(patsubst $(SRCDIR)/%.c, $(BINDIR_WIN)/%.exe, $(TOL_SRCS)) # Setup target setup: $(MKDIR) $(BINDIR) $(INCDIR) setup-cross: $(MKDIR) $(BINDIR) $(BINDIR_WIN) $(INCDIR) # Default: build for current platform all: setup tolsac structure # Build for current platform (native) tolsac: $(TOL_EXES_NATIVE) # Native compilation rule $(BINDIR)/%$(NATIVE_EXT): $(SRCDIR)/%.c $(NATIVE_CC) $< $(NATIVE_CFLAGS) -o $@ $(NATIVE_LDFLAGS) # Cross-compilation targets (Linux host only) ifneq ($(OS),Windows_NT) tolsac-win: setup-cross $(TOL_EXES_WIN) tolsac-all: tolsac tolsac-win rebuild-win: clean tolsac-win rebuild-all: clean tolsac-all $(BINDIR_WIN)/%.exe: $(SRCDIR)/%.c $(MINGW_CC) $< $(CFLAGS_PORTABLE) -o $@ else # On Windows, provide helpful messages for cross-compilation targets tolsac-win: @echo "Cross-compilation to Windows not needed on Windows host. Use 'make tolsac' instead." tolsac-all: tolsac @echo "On Windows host: built Windows binaries (cross-compilation not applicable)." rebuild-win: rebuild @echo "On Windows host: rebuilt Windows binaries." rebuild-all: rebuild @echo "On Windows host: rebuilt Windows binaries." endif clean: $(RM) $(BINDIR)/* ifneq ($(OS),Windows_NT) $(RM) $(BINDIR_WIN)/* endif $(RM) structure.txt rebuild: clean all # Generate project structure structure: @$(TREE_CMD) .PHONY: all setup setup-cross tolsac tolsac-win tolsac-all clean rebuild rebuild-win rebuild-all structure