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.

Closes #9, closes #10.
This commit is contained in:
Yamagi 2020-04-21 13:31:02 +02:00
parent fad0152c6b
commit 1531bcebb7

147
Makefile
View file

@ -14,24 +14,38 @@
# - Linux #
# - Mac OS X #
# - OpenBSD #
# - Windows #
# - Windows #
# ----------------------------------------------------- #
# 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 ($(YQ2_OSTYPE),Windows)
CC := gcc
CC ?= gcc
endif
# Detect the compiler
@ -47,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 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 := -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
# ----------
@ -91,20 +107,49 @@ endif
# ----------
# Base LDFLAGS.
ifeq ($(OSTYPE), Darwin)
LDFLAGS := -shared -arch x86_64
else ifeq ($(OSTYPE), Windows)
LDFLAGS := -shared -static-libgcc
else
LDFLAGS := -shared -lm
# Defines the operating system and architecture
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
# ----------
# 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')\"
# 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.
LDFLAGS ?=
# It's a shared library.
override LDFLAGS += -shared
# Required libaries
ifeq ($(YQ2_OSTYPE), Darwin)
override LDFLAGS += -arch $(YQ2_ARCH)
else ifeq ($(OSTYPE), Windows)
override LDFLAGS += -static-libgcc
else
override LDFLAGS += -lm
endif
# ----------
@ -113,7 +158,7 @@ endif
all: ctf
# ----------
# When make is invoked by "make VERBOSE=1" print
# the compiler and linker commands.
@ -129,12 +174,12 @@ endif
.PHONY : all clean ctf
# ----------
# Cleanup
clean:
@echo "===> CLEAN"
${Q}rm -Rf build release
# ----------
# The ctf game
@ -156,7 +201,7 @@ ctf:
release/game.so : CFLAGS += -fPIC
endif
build/%.o: %.c
@echo "===> CC $<"
$(Q)mkdir -p $(@D)
@ -190,7 +235,7 @@ CTF_OBJS_ = \
src/player/trail.o \
src/player/view.o \
src/player/weapon.o \
src/shared/shared.o
src/shared/shared.o
# ----------
@ -209,18 +254,18 @@ CTF_DEPS= $(CTF_OBJS:.o=.d)
# ----------
ifeq ($(OSTYPE), Windows)
ifeq ($(YQ2_OSTYPE), Windows)
release/game.dll : $(CTF_OBJS)
@echo "===> LD $@"
$(Q)$(CC) $(LDFLAGS) -o $@ $(CTF_OBJS)
else ifeq ($(OSTYPE), Darwin)
$(Q)$(CC) -o $@ $(CTF_OBJS) $(LDFLAGS)
else ifeq ($(YQ2_OSTYPE), Darwin)
release/game.dylib : $(CTF_OBJS)
@echo "===> LD $@"
${Q}$(CC) $(LDFLAGS) -o $@ $(CTF_OBJS)
${Q}$(CC) -o $@ $(CTF_OBJS) $(LDFLAGS)
else
release/game.so : $(CTF_OBJS)
@echo "===> LD $@"
$(Q)$(CC) $(LDFLAGS) -o $@ $(CTF_OBJS)
$(Q)$(CC) -o $@ $(CTF_OBJS) $(LDFLAGS)
endif
# ----------