From 254187024dd6f85e4bfa04417813858a49710694 Mon Sep 17 00:00:00 2001 From: Yamagi Date: Tue, 21 Apr 2020 13:45:23 +0200 Subject: [PATCH] Bring Makefile on par with yquake2: * Make CFLAGS and LDFLAGS overrideable * Correct architecture and operating system detection. * Enforce FPU mode. * Implement DEBUG. * Pass LDFLAGS after the objects. * Rename OSTYPE and ARCH to YQ2OSTYPE and YQ2ARCH to avoid collisions. --- Makefile | 147 ++++++++++++++++++++++++---------------- src/savegame/savegame.c | 46 ++++++------- 2 files changed, 113 insertions(+), 80 deletions(-) diff --git a/Makefile b/Makefile index 4959198..529fc90 100644 --- a/Makefile +++ b/Makefile @@ -19,33 +19,33 @@ # Detect the OS ifdef SystemRoot -OSTYPE := Windows +YQ2_OSTYPE ?= Windows else -OSTYPE := $(shell uname -s) +YQ2_OSTYPE ?= $(shell uname -s) endif - + # Special case for MinGW -ifneq (,$(findstring MINGW,$(OSTYPE))) -OSTYPE := Windows +ifneq (,$(findstring MINGW,$(YQ2_OSTYPE))) +YQ2_OSTYPE := Windows +endif + +# Detect the architecture +ifeq ($(YQ2_OSTYPE), Windows) +ifdef PROCESSOR_ARCHITEW6432 +# 64 bit Windows +YQ2_ARCH ?= $(PROCESSOR_ARCHITEW6432) +else +# 32 bit Windows +YQ2_ARCH ?= $(PROCESSOR_ARCHITECTURE) +endif +else +# Normalize some abiguous YQ2_ARCH strings +YQ2_ARCH ?= $(shell uname -m | sed -e 's/i.86/i386/' -e 's/amd64/x86_64/' -e 's/^arm.*/arm/') endif # On Windows / MinGW $(CC) is undefined by default. -ifeq ($(OSTYPE),Windows) -CC := gcc -endif - -# Detect the architecture -ifeq ($(OSTYPE), Windows) -ifdef PROCESSOR_ARCHITEW6432 -# 64 bit Windows -ARCH := $(PROCESSOR_ARCHITEW6432) -else -# 32 bit Windows -ARCH := $(PROCESSOR_ARCHITECTURE) -endif -else -# Normalize some abiguous ARCH strings -ARCH := $(shell uname -m | sed -e 's/i.86/i386/' -e 's/amd64/x86_64/' -e 's/^arm.*/arm/') +ifeq ($(YQ2_OSTYPE),Windows) +CC ?= gcc endif # Detect the compiler @@ -61,30 +61,32 @@ endif # ---------- -# Base CFLAGS. -# -# -O2 are enough optimizations. -# -# -fno-strict-aliasing since the source doesn't comply -# with strict aliasing rules and it's next to impossible -# to get it there... -# -# -fomit-frame-pointer since the framepointer is mostly -# useless for debugging Quake II and slows things down. -# -# -g to build allways with debug symbols. Please do not -# change this, since it's our only chance to debug this -# crap when random crashes happen! -# -# -fPIC for position independend code. -# -# -MMD to generate header dependencies. -ifeq ($(OSTYPE), Darwin) -CFLAGS := -O2 -fno-strict-aliasing -fomit-frame-pointer \ - -Wall -pipe -g -fwrapv -arch i386 -arch x86_64 +# Base CFLAGS. These may be overridden by the environment. +# Highest supported optimizations are -O2, higher levels +# will likely break this crappy code. +ifdef DEBUG +CFLAGS ?= -O0 -g -Wall -pipe else -CFLAGS := -std=gnu99 -O2 -fno-strict-aliasing -fomit-frame-pointer \ - -Wall -pipe -g -MMD -fwrapv +CFLAGS ?= -O2 -Wall -pipe -fomit-frame-pointer +endif + +# Always needed are: +# -fno-strict-aliasing since the source doesn't comply +# with strict aliasing rules and it's next to impossible +# to get it there... +# -fwrapv for defined integer wrapping. MSVC6 did this +# and the game code requires it. +override CFLAGS += -std=gnu99 -fno-strict-aliasing -fwrapv + +# -MMD to generate header dependencies. Unsupported by +# the Clang shipped with OS X. +ifneq ($(YQ2_OSTYPE), Darwin) +override CFLAGS += -MMD +endif + +# OS X architecture. +ifeq ($(YQ2_OSTYPE), Darwin) +override CFLAGS += -arch $(YQ2_ARCH) endif # ---------- @@ -106,17 +108,48 @@ endif # ---------- # Defines the operating system and architecture -CFLAGS += -DOSTYPE=\"$(OSTYPE)\" -DARCH=\"$(ARCH)\" +override CFLAGS += -DYQ2OSTYPE=\"$(YQ2_OSTYPE)\" -DYQ2ARCH=\"$(YQ2_ARCH)\" + +# ---------- + +# For reproduceable builds, look here for details: +# https://reproducible-builds.org/specs/source-date-epoch/ +ifdef SOURCE_DATE_EPOCH +CFLAGS += -DBUILD_DATE=\"$(shell date --utc --date="@${SOURCE_DATE_EPOCH}" +"%b %_d %Y" | sed -e 's/ /\\ /g')\" +endif + +# ---------- + +# If we're building with gcc for i386 let's define -ffloat-store. +# This helps the old and crappy x87 FPU to produce correct values. +# Would be nice if Clang had something comparable. +ifeq ($(YQ2_ARCH), i386) +ifeq ($(COMPILER), gcc) +override CFLAGS += -ffloat-store +endif +endif + +# Force SSE math on x86_64. All sane compilers should do this +# anyway, just to protect us from broken Linux distros. +ifeq ($(YQ2_ARCH), x86_64) +override CFLAGS += -mfpmath=sse +endif # ---------- # Base LDFLAGS. -ifeq ($(OSTYPE), Darwin) -LDFLAGS := -shared -arch i386 -arch x86_64 -else ifeq ($(OSTYPE), Windows) -LDFLAGS := -shared -static-libgcc +LDFLAGS ?= + +# It's a shared library. +override LDFLAGS += -shared + +# Required libaries +ifeq ($(YQ2_OSTYPE), Darwin) +override LDFLAGS += -arch $(YQ2_ARCH) +else ifeq ($(YQ2_OSTYPE), Windows) +override LDFLAGS += -static-libgcc else -LDFLAGS := -shared -lm +override LDFLAGS += -lm endif # ---------- @@ -150,12 +183,12 @@ clean: # ---------- # The xatrix game -ifeq ($(OSTYPE), Windows) +ifeq ($(YQ2_OSTYPE), Windows) xatrix: @echo "===> Building game.dll" ${Q}mkdir -p release ${MAKE} release/game.dll -else ifeq ($(OSTYPE), Darwin) +else ifeq ($(YQ2_OSTYPE), Darwin) xatrix: @echo "===> Building game.dylib" ${Q}mkdir -p release @@ -247,18 +280,18 @@ XATRIX_DEPS= $(XATRIX_OBJS:.o=.d) # ---------- -ifeq ($(OSTYPE), Windows) +ifeq ($(YQ2_OSTYPE), Windows) release/game.dll : $(XATRIX_OBJS) @echo "===> LD $@" - ${Q}$(CC) $(LDFLAGS) -o $@ $(XATRIX_OBJS) -else ifeq ($(OSTYPE), Darwin) + ${Q}$(CC) -o $@ $(XATRIX_OBJS) $(LDFLAGS) +else ifeq ($(YQ2_OSTYPE), Darwin) release/game.dylib : $(XATRIX_OBJS) @echo "===> LD $@" - ${Q}$(CC) $(LDFLAGS) -o $@ $(XATRIX_OBJS) + ${Q}$(CC) -o $@ $(XATRIX_OBJS) $(LDFLAGS) else release/game.so : $(XATRIX_OBJS) @echo "===> LD $@" - ${Q}$(CC) $(LDFLAGS) -o $@ $(XATRIX_OBJS) + ${Q}$(CC) -o $@ $(XATRIX_OBJS) $(LDFLAGS) endif # ---------- diff --git a/src/savegame/savegame.c b/src/savegame/savegame.c index 3d2e1cb..d0017e9 100644 --- a/src/savegame/savegame.c +++ b/src/savegame/savegame.c @@ -59,12 +59,12 @@ * created on other systems or architectures. This will * crash q2 in spectacular ways */ -#ifndef OSTYPE -#error OSTYPE should be defined by the build system +#ifndef YQ2OSTYPE +#error YQ2OSTYPE should be defined by the build system #endif -#ifndef ARCH -#error ARCH should be defined by the build system +#ifndef YQ2ARCH +#error YQ2ARCH should be defined by the build system #endif /* @@ -72,29 +72,29 @@ * macros, implemented by savegame version YQ2-1. */ #if defined(__APPLE__) -#define OSTYPE_1 "MacOS X" +#define YQ2OSTYPE_1 "MacOS X" #elif defined(__FreeBSD__) -#define OSTYPE_1 "FreeBSD" +#define YQ2OSTYPE_1 "FreeBSD" #elif defined(__OpenBSD__) -#define OSTYPE_1 "OpenBSD" +#define YQ2OSTYPE_1 "OpenBSD" #elif defined(__linux__) - #define OSTYPE_1 "Linux" + #define YQ2OSTYPE_1 "Linux" #elif defined(_WIN32) - #define OSTYPE_1 "Windows" + #define YQ2OSTYPE_1 "Windows" #else - #define OSTYPE_1 "Unknown" + #define YQ2OSTYPE_1 "Unknown" #endif #if defined(__i386__) -#define ARCH_1 "i386" +#define YQ2ARCH_1 "i386" #elif defined(__x86_64__) -#define ARCH_1 "amd64" +#define YQ2ARCH_1 "amd64" #elif defined(__sparc__) -#define ARCH_1 "sparc64" +#define YQ2ARCH_1 "sparc64" #elif defined(__ia64__) - #define ARCH_1 "ia64" + #define YQ2ARCH_1 "ia64" #else - #define ARCH_1 "unknown" + #define YQ2ARCH_1 "unknown" #endif /* @@ -776,8 +776,8 @@ WriteGame(const char *filename, qboolean autosave) strncpy(str_ver, SAVEGAMEVER, sizeof(str_ver) - 1); strncpy(str_game, GAMEVERSION, sizeof(str_game) - 1); - strncpy(str_os, OSTYPE, sizeof(str_os) - 1); - strncpy(str_arch, ARCH, sizeof(str_arch) - 1); + strncpy(str_os, YQ2OSTYPE, sizeof(str_os) - 1); + strncpy(str_arch, YQ2ARCH, sizeof(str_arch) - 1); fwrite(str_ver, sizeof(str_ver), 1, f); fwrite(str_game, sizeof(str_game), 1, f); @@ -836,12 +836,12 @@ ReadGame(const char *filename) fclose(f); gi.error("Savegame from an other game.so.\n"); } - else if (strcmp(str_os, OSTYPE)) + else if (strcmp(str_os, YQ2OSTYPE)) { fclose(f); gi.error("Savegame from an other os.\n"); } - else if (strcmp(str_arch, ARCH)) + else if (strcmp(str_arch, YQ2ARCH)) { fclose(f); gi.error("Savegame from an other architecure.\n"); @@ -856,12 +856,12 @@ ReadGame(const char *filename) fclose(f); gi.error("Savegame from an other game.so.\n"); } - else if (strcmp(str_os, OSTYPE)) + else if (strcmp(str_os, YQ2OSTYPE)) { fclose(f); gi.error("Savegame from an other os.\n"); } - else if (strcmp(str_arch, ARCH)) + else if (strcmp(str_arch, YQ2ARCH)) { fclose(f); gi.error("Savegame from an other architecure.\n"); @@ -876,7 +876,7 @@ ReadGame(const char *filename) fclose(f); gi.error("Savegame from an other game.so.\n"); } - else if (strcmp(str_os, OSTYPE_1)) + else if (strcmp(str_os, YQ2OSTYPE_1)) { fclose(f); gi.error("Savegame from an other os.\n"); @@ -893,7 +893,7 @@ ReadGame(const char *filename) } else { - if (strcmp(str_arch, ARCH_1)) + if (strcmp(str_arch, YQ2ARCH_1)) { fclose(f); gi.error("Savegame from an other architecure.\n");