From f9e1e76011236f05d6e2c3b62e2ae2969c64f808 Mon Sep 17 00:00:00 2001 From: admin Date: Sat, 24 May 2025 17:38:59 +0200 Subject: Adjusted files for cross-compilation on Linux --- Makefile | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 83 insertions(+), 10 deletions(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index d192768..dc5f750 100644 --- a/Makefile +++ b/Makefile @@ -1,31 +1,104 @@ 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=-I$(INCDIR) -O2 -march=native -flto #-ffast-math +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 = $(patsubst $(SRCDIR)/%.c, $(BINDIR)/%.exe, $(TOL_SRCS)) +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 -p $(BINDIR) $(INCDIR) + $(MKDIR) $(BINDIR) $(INCDIR) +setup-cross: + $(MKDIR) $(BINDIR) $(BINDIR_WIN) $(INCDIR) + +# Default: build for current platform all: setup tolsac structure -tolsac: $(TOL_EXES) -$(BINDIR)/%.exe: $(SRCDIR)/%.c - $(CC) $< $(CFLAGS) -o $@ +# 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 -f $(BINDIR)/* - rm -f structure.txt + $(RM) $(BINDIR)/* +ifneq ($(OS),Windows_NT) + $(RM) $(BINDIR_WIN)/* +endif + $(RM) structure.txt 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" + @$(TREE_CMD) + +.PHONY: all setup setup-cross tolsac tolsac-win tolsac-all clean rebuild rebuild-win rebuild-all structure -- cgit v1.2.3