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.
This commit is contained in:
Yamagi 2020-04-21 13:54:58 +02:00
parent 09f9cf8f61
commit 73fd162dbe
2 changed files with 107 additions and 74 deletions

125
Makefile
View file

@ -19,33 +19,33 @@
# Detect the OS # Detect the OS
ifdef SystemRoot ifdef SystemRoot
OSTYPE := Windows YQ2_OSTYPE ?= Windows
else else
OSTYPE := $(shell uname -s) YQ2_OSTYPE ?= $(shell uname -s)
endif endif
# Special case for MinGW # Special case for MinGW
ifneq (,$(findstring MINGW,$(OSTYPE))) ifneq (,$(findstring MINGW,$(YQ2_OSTYPE)))
OSTYPE := Windows YQ2_OSTYPE := Windows
endif
# On Windows / MinGW $(CC) is undefined by default.
ifeq ($(OSTYPE),Windows)
CC := gcc
endif endif
# Detect the architecture # Detect the architecture
ifeq ($(OSTYPE), Windows) ifeq ($(YQ2_OSTYPE), Windows)
ifdef PROCESSOR_ARCHITEW6432 ifdef PROCESSOR_ARCHITEW6432
# 64 bit Windows # 64 bit Windows
ARCH := $(PROCESSOR_ARCHITEW6432) YQ2_ARCH ?= $(PROCESSOR_ARCHITEW6432)
else else
# 32 bit Windows # 32 bit Windows
ARCH := $(PROCESSOR_ARCHITECTURE) YQ2_ARCH ?= $(PROCESSOR_ARCHITECTURE)
endif endif
else else
# Normalize some abiguous ARCH strings # Normalize some abiguous YQ2_ARCH strings
ARCH := $(shell uname -m | sed -e 's/i.86/i386/' -e 's/amd64/x86_64/' -e 's/^arm.*/arm/') 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 ($(YQ2_OSTYPE),Windows)
CC ?= gcc
endif endif
# Detect the compiler # Detect the compiler
@ -61,30 +61,32 @@ endif
# ---------- # ----------
# Base CFLAGS. # Base CFLAGS. These may be overridden by the environment.
# # Highest supported optimizations are -O2, higher levels
# -O2 are enough optimizations. # will likely break this crappy code.
# ifdef DEBUG
CFLAGS ?= -O0 -g -Wall -pipe
else
CFLAGS ?= -O2 -Wall -pipe -fomit-frame-pointer
endif
# Always needed are:
# -fno-strict-aliasing since the source doesn't comply # -fno-strict-aliasing since the source doesn't comply
# with strict aliasing rules and it's next to impossible # with strict aliasing rules and it's next to impossible
# to get it there... # to get it there...
# # -fwrapv for defined integer wrapping. MSVC6 did this
# -fomit-frame-pointer since the framepointer is mostly # and the game code requires it.
# useless for debugging Quake II and slows things down. override CFLAGS += -std=gnu99 -fno-strict-aliasing -fwrapv
#
# -g to build allways with debug symbols. Please do not # -MMD to generate header dependencies. Unsupported by
# change this, since it's our only chance to debug this # the Clang shipped with OS X.
# crap when random crashes happen! ifneq ($(YQ2_OSTYPE), Darwin)
# override CFLAGS += -MMD
# -fPIC for position independend code. endif
#
# -MMD to generate header dependencies. # OS X architecture.
ifeq ($(OSTYPE), Darwin) ifeq ($(YQ2_OSTYPE), Darwin)
CFLAGS := -O2 -fno-strict-aliasing -fomit-frame-pointer \ override CFLAGS += -arch $(YQ2_ARCH)
-Wall -pipe -g -fwrapv -arch i386 -arch x86_64
else
CFLAGS := -std=gnu99 -O2 -fno-strict-aliasing -fomit-frame-pointer \
-Wall -pipe -g -MMD -fwrapv
endif endif
# ---------- # ----------
@ -106,17 +108,48 @@ endif
# ---------- # ----------
# Defines the operating system and architecture # 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. # Base LDFLAGS.
ifeq ($(OSTYPE), Darwin) LDFLAGS ?=
LDFLAGS := -shared -arch i386 -arch x86_64
else ifeq ($(OSTYPE), Windows) # It's a shared library.
LDFLAGS := -shared -static-libgcc override LDFLAGS += -shared
# Required libaries
ifeq ($(YQ2_OSTYPE), Darwin)
override LDFLAGS += -arch $(YQ2_ARCH)
else ifeq ($(YQ2_OSTYPE), Windows)
override LDFLAGS += -static-libgcc
else else
LDFLAGS := -shared -lm override LDFLAGS += -lm
endif endif
# ---------- # ----------
@ -150,12 +183,12 @@ clean:
# ---------- # ----------
# The rogue game # The rogue game
ifeq ($(OSTYPE), Windows) ifeq ($(YQ2_OSTYPE), Windows)
rogue: rogue:
@echo "===> Building game.dll" @echo "===> Building game.dll"
${Q}mkdir -p release ${Q}mkdir -p release
$(MAKE) release/game.dll $(MAKE) release/game.dll
else ifeq ($(OSTYPE), Darwin) else ifeq ($(YQ2_OSTYPE), Darwin)
rogue: rogue:
@echo "===> Building game.dylib" @echo "===> Building game.dylib"
${Q}mkdir -p release ${Q}mkdir -p release
@ -260,15 +293,15 @@ ROGUE_DEPS= $(ROGUE_OBJS:.o=.d)
ifeq ($(OSTYPE), Windows) ifeq ($(OSTYPE), Windows)
release/game.dll : $(ROGUE_OBJS) release/game.dll : $(ROGUE_OBJS)
@echo "===> LD $@" @echo "===> LD $@"
${Q}$(CC) $(LDFLAGS) -o $@ $(ROGUE_OBJS) ${Q}$(CC) -o $@ $(ROGUE_OBJS) $(LDFLAGS)
else ifeq ($(OSTYPE), Darwin) else ifeq ($(OSTYPE), Darwin)
release/game.dylib : $(ROGUE_OBJS) release/game.dylib : $(ROGUE_OBJS)
@echo "===> LD $@" @echo "===> LD $@"
${Q}$(CC) $(LDFLAGS) -o $@ $(ROGUE_OBJS) ${Q}$(CC) -o $@ $(ROGUE_OBJS) $(LDFLAGS)
else else
release/game.so : $(ROGUE_OBJS) release/game.so : $(ROGUE_OBJS)
@echo "===> LD $@" @echo "===> LD $@"
${Q}$(CC) $(LDFLAGS) -o $@ $(ROGUE_OBJS) ${Q}$(CC) -o $@ $(ROGUE_OBJS) $(LDFLAGS)
endif endif
# ---------- # ----------

View file

@ -58,12 +58,12 @@
* created on other systems or architectures. This will * created on other systems or architectures. This will
* crash q2 in spectacular ways * crash q2 in spectacular ways
*/ */
#ifndef OSTYPE #ifndef YQ2OSTYPE
#error OSTYPE should be defined by the build system #error YQ2OSTYPE should be defined by the build system
#endif #endif
#ifndef ARCH #ifndef YQ2ARCH
#error ARCH should be defined by the build system #error YQ2ARCH should be defined by the build system
#endif #endif
/* /*
@ -71,29 +71,29 @@
* macros, implemented by savegame version YQ2-2. * macros, implemented by savegame version YQ2-2.
*/ */
#if defined(__APPLE__) #if defined(__APPLE__)
#define OSTYPE_1 "MacOS X" #define YQ2OSTYPE_1 "MacOS X"
#elif defined(__FreeBSD__) #elif defined(__FreeBSD__)
#define OSTYPE_1 "FreeBSD" #define YQ2OSTYPE_1 "FreeBSD"
#elif defined(__OpenBSD__) #elif defined(__OpenBSD__)
#define OSTYPE_1 "OpenBSD" #define YQ2OSTYPE_1 "OpenBSD"
#elif defined(__linux__) #elif defined(__linux__)
#define OSTYPE_1 "Linux" #define YQ2OSTYPE_1 "Linux"
#elif defined(_WIN32) #elif defined(_WIN32)
#define OSTYPE_1 "Windows" #define YQ2OSTYPE_1 "Windows"
#else #else
#define OSTYPE_1 "Unknown" #define YQ2OSTYPE_1 "Unknown"
#endif #endif
#if defined(__i386__) #if defined(__i386__)
#define ARCH_1 "i386" #define YQ2ARCH_1 "i386"
#elif defined(__x86_64__) #elif defined(__x86_64__)
#define ARCH_1 "amd64" #define YQ2ARCH_1 "amd64"
#elif defined(__sparc__) #elif defined(__sparc__)
#define ARCH_1 "sparc64" #define YQ2ARCH_1 "sparc64"
#elif defined(__ia64__) #elif defined(__ia64__)
#define ARCH_1 "ia64" #define YQ2ARCH_1 "ia64"
#else #else
#define ARCH_1 "unknown" #define YQ2ARCH_1 "unknown"
#endif #endif
/* /*
@ -791,8 +791,8 @@ WriteGame(const char *filename, qboolean autosave)
strncpy(str_ver, SAVEGAMEVER, sizeof(str_ver) - 1); strncpy(str_ver, SAVEGAMEVER, sizeof(str_ver) - 1);
strncpy(str_game, GAMEVERSION, sizeof(str_game) - 1); strncpy(str_game, GAMEVERSION, sizeof(str_game) - 1);
strncpy(str_os, OSTYPE, sizeof(str_os) - 1); strncpy(str_os, YQ2OSTYPE, sizeof(str_os) - 1);
strncpy(str_arch, ARCH, sizeof(str_arch) - 1); strncpy(str_arch, YQ2ARCH, sizeof(str_arch) - 1);
fwrite(str_ver, sizeof(str_ver), 1, f); fwrite(str_ver, sizeof(str_ver), 1, f);
fwrite(str_game, sizeof(str_game), 1, f); fwrite(str_game, sizeof(str_game), 1, f);
@ -851,12 +851,12 @@ ReadGame(const char *filename)
fclose(f); fclose(f);
gi.error("Savegame from an other game.so.\n"); gi.error("Savegame from an other game.so.\n");
} }
else if (strcmp(str_os, OSTYPE)) else if (strcmp(str_os, YQ2OSTYPE))
{ {
fclose(f); fclose(f);
gi.error("Savegame from an other os.\n"); gi.error("Savegame from an other os.\n");
} }
else if (strcmp(str_arch, ARCH)) else if (strcmp(str_arch, YQ2ARCH))
{ {
fclose(f); fclose(f);
gi.error("Savegame from an other architecure.\n"); gi.error("Savegame from an other architecure.\n");
@ -871,12 +871,12 @@ ReadGame(const char *filename)
fclose(f); fclose(f);
gi.error("Savegame from an other game.so.\n"); gi.error("Savegame from an other game.so.\n");
} }
else if (strcmp(str_os, OSTYPE)) else if (strcmp(str_os, YQ2OSTYPE))
{ {
fclose(f); fclose(f);
gi.error("Savegame from an other os.\n"); gi.error("Savegame from an other os.\n");
} }
else if (strcmp(str_arch, ARCH)) else if (strcmp(str_arch, YQ2ARCH))
{ {
fclose(f); fclose(f);
gi.error("Savegame from an other architecure.\n"); gi.error("Savegame from an other architecure.\n");
@ -891,7 +891,7 @@ ReadGame(const char *filename)
fclose(f); fclose(f);
gi.error("Savegame from an other game.so.\n"); 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); fclose(f);
gi.error("Savegame from an other os.\n"); gi.error("Savegame from an other os.\n");
@ -908,7 +908,7 @@ ReadGame(const char *filename)
} }
else else
{ {
if (strcmp(str_arch, ARCH_1)) if (strcmp(str_arch, YQ2ARCH_1))
{ {
fclose(f); fclose(f);
gi.error("Savegame from an other architecure.\n"); gi.error("Savegame from an other architecure.\n");