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 # # - Linux #
# - Mac OS X # # - Mac OS X #
# - OpenBSD # # - OpenBSD #
# - Windows # # - Windows #
# ----------------------------------------------------- # # ----------------------------------------------------- #
# 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
# 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 endif
# On Windows / MinGW $(CC) is undefined by default. # On Windows / MinGW $(CC) is undefined by default.
ifeq ($(YQ2_OSTYPE),Windows) ifeq ($(YQ2_OSTYPE),Windows)
CC := gcc CC ?= gcc
endif endif
# Detect the compiler # Detect the compiler
@ -47,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
# -fno-strict-aliasing since the source doesn't comply CFLAGS ?= -O0 -g -Wall -pipe
# 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
else else
CFLAGS := -O2 -fno-strict-aliasing -fomit-frame-pointer \ CFLAGS ?= -O2 -Wall -pipe -fomit-frame-pointer
-Wall -pipe -g -MMD -fwrapv 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 endif
# ---------- # ----------
@ -91,20 +107,49 @@ endif
# ---------- # ----------
# Base LDFLAGS. # Defines the operating system and architecture
ifeq ($(OSTYPE), Darwin) override CFLAGS += -DYQ2OSTYPE=\"$(YQ2_OSTYPE)\" -DYQ2ARCH=\"$(YQ2_ARCH)\"
LDFLAGS := -shared -arch x86_64
else ifeq ($(OSTYPE), Windows) # ----------
LDFLAGS := -shared -static-libgcc
else # For reproduceable builds, look here for details:
LDFLAGS := -shared -lm # 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 endif
# ---------- # ----------
# https://reproducible-builds.org/specs/source-date-epoch/ # If we're building with gcc for i386 let's define -ffloat-store.
ifdef SOURCE_DATE_EPOCH # This helps the old and crappy x87 FPU to produce correct values.
CFLAGS += -DBUILD_DATE=\"$(shell date --utc --date="@${SOURCE_DATE_EPOCH}" +"%b %_d %Y" | sed -e 's/ /\\ /g')\" # 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 endif
# ---------- # ----------
@ -113,7 +158,7 @@ endif
all: ctf all: ctf
# ---------- # ----------
# When make is invoked by "make VERBOSE=1" print # When make is invoked by "make VERBOSE=1" print
# the compiler and linker commands. # the compiler and linker commands.
@ -129,12 +174,12 @@ endif
.PHONY : all clean ctf .PHONY : all clean ctf
# ---------- # ----------
# Cleanup # Cleanup
clean: clean:
@echo "===> CLEAN" @echo "===> CLEAN"
${Q}rm -Rf build release ${Q}rm -Rf build release
# ---------- # ----------
# The ctf game # The ctf game
@ -156,7 +201,7 @@ ctf:
release/game.so : CFLAGS += -fPIC release/game.so : CFLAGS += -fPIC
endif endif
build/%.o: %.c build/%.o: %.c
@echo "===> CC $<" @echo "===> CC $<"
$(Q)mkdir -p $(@D) $(Q)mkdir -p $(@D)
@ -190,7 +235,7 @@ CTF_OBJS_ = \
src/player/trail.o \ src/player/trail.o \
src/player/view.o \ src/player/view.o \
src/player/weapon.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) release/game.dll : $(CTF_OBJS)
@echo "===> LD $@" @echo "===> LD $@"
$(Q)$(CC) $(LDFLAGS) -o $@ $(CTF_OBJS) $(Q)$(CC) -o $@ $(CTF_OBJS) $(LDFLAGS)
else ifeq ($(OSTYPE), Darwin) else ifeq ($(YQ2_OSTYPE), Darwin)
release/game.dylib : $(CTF_OBJS) release/game.dylib : $(CTF_OBJS)
@echo "===> LD $@" @echo "===> LD $@"
${Q}$(CC) $(LDFLAGS) -o $@ $(CTF_OBJS) ${Q}$(CC) -o $@ $(CTF_OBJS) $(LDFLAGS)
else else
release/game.so : $(CTF_OBJS) release/game.so : $(CTF_OBJS)
@echo "===> LD $@" @echo "===> LD $@"
$(Q)$(CC) $(LDFLAGS) -o $@ $(CTF_OBJS) $(Q)$(CC) -o $@ $(CTF_OBJS) $(LDFLAGS)
endif endif
# ---------- # ----------