# GNU Makefile for compiling Mac OS X version of QuakeSpasm.
# Usage: "make -f Makefile.darwin"
# To cross-compile on Linux hosts, see the build_cross_osx*.sh scripts.
# "make DEBUG=1" to build a debug client.
# "make SDL_FRAMEWORK_PATH=/path/to/Frameworks" to specify the directory
#    containing SDL.framework and override the locally included versions.
# "make DO_USERDIRS=1" to enable user directories support

# Enable/Disable user directories support
DO_USERDIRS=0

### Enable/Disable SDL2
USE_SDL2=0

### Enable/Disable codecs for streaming music support
USE_CODEC_WAVE=1
USE_CODEC_FLAC=1
USE_CODEC_MP3=1
USE_CODEC_VORBIS=1
USE_CODEC_OPUS=1
# either mikmod or xmp
USE_CODEC_MIKMOD=1
USE_CODEC_XMP=0
USE_CODEC_UMX=1

# which library to use for mp3 decoding: mad or mpg123
MP3LIB=mad
# which library to use for ogg decoding: vorbis or tremor
VORBISLIB=vorbis

# ---------------------------
# Helper functions
# ---------------------------

check_gcc = $(shell if echo | $(CC) $(1) -Werror -S -o /dev/null -xc - > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi;)

# ---------------------------

MACH_TYPE= $(shell sh detect.sh arch)

DEBUG   ?= 0

# ---------------------------
# build variables
# ---------------------------

CC ?= gcc
LINKER = $(CC)
LIPO ?= lipo

STRIP ?= strip

CPUFLAGS=
LDFLAGS =
DFLAGS ?=
CFLAGS ?= -Wall
# @rpath can be used when targeting 10.5+
USE_RPATH=0
# require 10.5 for 64 bit builds
ifeq ($(MACH_TYPE),ppc64)
CFLAGS  +=-mmacosx-version-min=10.5
LDFLAGS +=-mmacosx-version-min=10.5
USE_RPATH=1
endif
ifeq ($(USE_SDL2),1)
# sdl2 needs targetting 10.5+
# as of v2.0.5, sdl2 targets 10.6+
ifeq ($(MACH_TYPE),x86)
CFLAGS  +=-mmacosx-version-min=10.6
LDFLAGS +=-mmacosx-version-min=10.6
USE_RPATH=1
endif
endif
ifeq ($(MACH_TYPE),x86_64)
# require 10.6 for amd64 builds, not 10.5 (SDL's requirement.)
# bundle1.o is needed for dyld_stub_binding_helper
CFLAGS  +=-mmacosx-version-min=10.6
LDFLAGS +=-mmacosx-version-min=10.6 -Wl,-lbundle1.o
USE_RPATH=1
endif
ifeq ($(MACH_TYPE),arm64)
CFLAGS  +=-mmacosx-version-min=11.0
LDFLAGS +=-mmacosx-version-min=11.0
USE_RPATH=1
endif
CFLAGS += $(CPUFLAGS)
ifeq ($(USE_RPATH),1)
LDFLAGS+=-Wl,-rpath,@executable_path/../Frameworks
endif

ifneq ($(DEBUG),0)
DFLAGS += -DDEBUG
CFLAGS += -g
do_strip=
else
DFLAGS += -DNDEBUG
CFLAGS += -O2
CFLAGS += $(call check_gcc,-fweb,)
CFLAGS += $(call check_gcc,-frename-registers,)
cmd_strip=$(STRIP) $(1)
define do_strip
	$(call cmd_strip,$(1));
endef
endif

ifeq ($(DO_USERDIRS),1)
CFLAGS += -DDO_USERDIRS=1
endif

ifeq ($(USE_SDL2),1)
CFLAGS += -DUSE_SDL2
endif
CFLAGS += -DGL_SILENCE_DEPRECATION=1

# not relying on sdl-config command and assuming
# /Library/Frameworks/SDL.framework is available
SDL_CFLAGS =-D_GNU_SOURCE=1 -D_THREAD_SAFE
SDL_CFLAGS+=-DSDL_FRAMEWORK -DNO_SDL_CONFIG
ifeq ($(USE_SDL2),1)
SDL_FRAMEWORK_NAME = SDL2
else
SDL_FRAMEWORK_NAME = SDL
endif
# default to our local SDL[2].framework for build
SDL_FRAMEWORK_PATH ?=../MacOSX
ifneq ($(SDL_FRAMEWORK_PATH),)
SDL_LIBS  +=-F$(SDL_FRAMEWORK_PATH)
SDL_CFLAGS+=-F$(SDL_FRAMEWORK_PATH)
endif
SDL_LIBS  +=-Wl,-framework,$(SDL_FRAMEWORK_NAME) -Wl,-framework,Cocoa

NET_LIBS   :=

ifneq ($(VORBISLIB),vorbis)
ifneq ($(VORBISLIB),tremor)
$(error Invalid VORBISLIB setting)
endif
endif
ifneq ($(MP3LIB),mpg123)
ifneq ($(MP3LIB),mad)
$(error Invalid MP3LIB setting)
endif
endif
ifeq ($(MP3LIB),mad)
mp3_obj=snd_mp3
lib_mp3dec=-lmad
endif
ifeq ($(MP3LIB),mpg123)
mp3_obj=snd_mpg123
lib_mp3dec=-lmpg123
endif
ifeq ($(VORBISLIB),vorbis)
cpp_vorbisdec=
lib_vorbisdec=-lvorbisfile -lvorbis -logg
endif
ifeq ($(VORBISLIB),tremor)
cpp_vorbisdec=-DVORBIS_USE_TREMOR
lib_vorbisdec=-lvorbisidec -logg
endif

CODECLIBS  :=
ifeq ($(USE_CODEC_WAVE),1)
CFLAGS+= -DUSE_CODEC_WAVE
endif
ifeq ($(USE_CODEC_FLAC),1)
CFLAGS+= -DUSE_CODEC_FLAC
CODEC_INC = -I../MacOSX/codecs/include
CODEC_LINK= -L../MacOSX/codecs/lib
CODECLIBS+= -lFLAC
endif
ifeq ($(USE_CODEC_OPUS),1)
CFLAGS+= -DUSE_CODEC_OPUS
CODEC_INC = -I../MacOSX/codecs/include
CODEC_LINK= -L../MacOSX/codecs/lib
CODECLIBS+= -lopusfile -lopus -logg
endif
ifeq ($(USE_CODEC_VORBIS),1)
CFLAGS+= -DUSE_CODEC_VORBIS $(cpp_vorbisdec)
CODEC_INC = -I../MacOSX/codecs/include
CODEC_LINK= -L../MacOSX/codecs/lib
CODECLIBS+= $(lib_vorbisdec)
endif
ifeq ($(USE_CODEC_MP3),1)
CFLAGS+= -DUSE_CODEC_MP3
CODEC_INC = -I../MacOSX/codecs/include
CODEC_LINK= -L../MacOSX/codecs/lib
CODECLIBS+= $(lib_mp3dec)
endif
ifeq ($(USE_CODEC_MIKMOD),1)
CFLAGS+= -DUSE_CODEC_MIKMOD
CODEC_INC = -I../MacOSX/codecs/include
CODEC_LINK= -L../MacOSX/codecs/lib
CODECLIBS+= -lmikmod
endif
ifeq ($(USE_CODEC_XMP),1)
CFLAGS+= -DUSE_CODEC_XMP
CODEC_INC = -I../MacOSX/codecs/include
CODEC_LINK= -L../MacOSX/codecs/lib
CODECLIBS+= -lxmp
endif
ifeq ($(USE_CODEC_UMX),1)
CFLAGS+= -DUSE_CODEC_UMX
endif
CFLAGS+= $(CODEC_INC)

COMMON_LIBS:= -Wl,-framework,IOKit -Wl,-framework,OpenGL

LIBS := $(COMMON_LIBS) $(NET_LIBS) $(CODEC_LINK) $(CODECLIBS)

# ---------------------------
# targets
# ---------------------------

.PHONY:	clean debug release

DEFAULT_TARGET := quakespasm

# ---------------------------
# rules
# ---------------------------

%.o:	%.c
	$(CC) $(DFLAGS) -c $(CFLAGS) $(SDL_CFLAGS) -o $@ $<
%.o:	%.m
	$(CC) $(DFLAGS) -c $(CFLAGS) $(SDL_CFLAGS) -o $@ $<
%.o:	../MacOSX/%.m
	$(CC) $(DFLAGS) -c -I../MacOSX $(CFLAGS) $(SDL_CFLAGS) -o $@ $<

# ----------------------------------------------------------------------------
# objects
# ----------------------------------------------------------------------------

MUSIC_OBJS:= bgmusic.o \
	snd_codec.o \
	snd_flac.o \
	snd_wave.o \
	snd_vorbis.o \
	snd_opus.o \
	$(mp3_obj).o \
	snd_mp3tag.o \
	snd_mikmod.o \
	snd_xmp.o \
	snd_umx.o
COMOBJ_SND := snd_dma.o snd_mix.o snd_mem.o $(MUSIC_OBJS)
SYSOBJ_SND := snd_sdl.o
SYSOBJ_CDA := cd_sdl.o
SYSOBJ_INPUT := in_sdl.o
SYSOBJ_GL_VID:= gl_vidsdl.o
SYSOBJ_NET := net_bsd.o net_udp.o
SYSOBJ_LAUNCHER := AppController.o QuakeArgument.o QuakeArguments.o ScreenInfo.o SDLApplication.o
SYSOBJ_SYS := pl_osx.o sys_sdl_unix.o
SYSOBJ_MAIN:= main_sdl.o SDLMain.o

GLOBJS = \
	gl_refrag.o \
	gl_rlight.o \
	gl_rmain.o \
	gl_fog.o \
	gl_rmisc.o \
	r_part.o \
	r_world.o \
	gl_screen.o \
	gl_sky.o \
	gl_warp.o \
	$(SYSOBJ_GL_VID) \
	gl_draw.o \
	image.o \
	gl_texmgr.o \
	gl_mesh.o \
	r_sprite.o \
	r_alias.o \
	r_brush.o \
	gl_model.o

OBJS := strlcat.o \
	strlcpy.o \
	$(GLOBJS) \
	$(SYSOBJ_INPUT) \
	$(COMOBJ_SND) \
	$(SYSOBJ_SND) \
	$(SYSOBJ_CDA) \
	$(SYSOBJ_NET) \
	net_dgrm.o \
	net_loop.o \
	net_main.o \
	chase.o \
	cl_demo.o \
	cl_input.o \
	cl_main.o \
	cl_parse.o \
	cl_tent.o \
	console.o \
	keys.o \
	menu.o \
	sbar.o \
	view.o \
	wad.o \
	cmd.o \
	common.o \
	miniz.o \
	crc.o \
	cvar.o \
	cfgfile.o \
	host.o \
	host_cmd.o \
	mathlib.o \
	pr_cmds.o \
	pr_edict.o \
	pr_exec.o \
	sv_main.o \
	sv_move.o \
	sv_phys.o \
	sv_user.o \
	world.o \
	zone.o \
	$(SYSOBJ_SYS) $(SYSOBJ_LAUNCHER) $(SYSOBJ_MAIN)

# ------------------------
# darwin build rules
# ------------------------

quakespasm:	$(OBJS)
	$(LINKER) $(OBJS) $(LDFLAGS) $(LIBS) $(SDL_LIBS) -o $@
	$(call do_strip,$@)

image.o: lodepng.c lodepng.h stb_image_write.h

release:	quakespasm
debug:
	$(error Use "make DEBUG=1")

clean:
	rm -f $(shell find . \( -name '*~' -o -name '#*#' -o -name '*.o' -o -name '*.res' -o -name $(DEFAULT_TARGET) \) -print)

