#
# DJGPP makefile for DOSQuake by Norberto Alfredo Bensa <nbensa@hotmail.com>
# Usage: make -f makefile.djgpp [DEBUG=X] [VERBOSE=X]
#
# Use DEBUG=X for a debug version
# Use VERBOSE=X to see all the tool calls
#
# --------------------------------------------------------------------------
#
# Necessary changes for the code to work with DJGPP:
# 1. Remove the "static" statement for the isDedicated variable in SYS_DOS.C
# 2. Add the following to lines to SYS_DOS.C to fix the memory detection bug
#    #include <crt0.h>
#    int _crt0_startup_flags = _CRT0_FLAG_UNIX_SBRK;
# 3. There are more bugs in DOSQuake, for a list of known Quake bugs visit
#    the Quake Info Pool through http://www.bigfoot.com/~maddes (redirection)
#
# --------------------------------------------------------------------------
#
# Packages needed to build DOSQuake can be downloaded at http://www.delorie.com/djgpp/
# (ftp://ftp.simtelnet.net/pub/simtelnet/gnu/djgpp/)
#
# Go to the ZipPicker page and choose the following for the minimum download:
# - "Build and run programs with DJGPP"
# - state your OS
# - online-documentation: "yes" (not necessary, but helpful)
# - programming language: "C"
# - you don't need RHide or the GNU debugger
# - get the extra documentation formats if you are not used to info
#
# As of 2000-07-27 you will get the following download list (older versions may work too)
# - v2/copying.dj              DJGPP Copyright info            3 kb
# - v2/djdev203.zip            DJGPP Basic Development Kit   1.5 mb
# - v2/faq230b.zip             Frequently Asked Questions    664 kb
# - v2/readme.1st              Installation instructions      20 kb
# - v2gnu/bnu2951b.zip         Basic assembler, linker       2.4 mb
# - v2gnu/gcc2952b.zip         Basic GCC compiler            1.8 mb
# - v2gnu/gdb418b.zip          GNU debugger                  1.1 mb
# - v2gnu/mak379b.zip          Make (processes makefiles)    263 kb
# - v2gnu/txi40b.zip           Info file viewer              632 kb
#
# Additional tools needed:
# - v2gnu/bsh203b.zip          BASH ported to DJGPP          483 kb
# - v2gnu/fil316b.zip          Unix tools ported to DJGPP    979 kb
# - v2gnu/sed302b.zip          SED ported to DJGPP           105 kb
#
#
# NOTE: You can compress the final executable with UPX (http://upx.tsx.org/)
#
# --------------------------------------------------------------------------
#
# Enjoy,
# Norberto Alfredo Bensa, Argentina
#

# 2000-05-02 NVS Network Versioning by Maddes
# added NVS files


#
# General and compiler variables
#

ifndef VERBOSE
	MAKEFLAGS+=-s
endif

SHELL=/bin/sh

CC=gcc
LD=gcc
AS=gcc -x assembler-with-cpp

RM=rm -f

CFLAGS=\
	-DDOSQUAKE \
	-O9 \
	-march=i586 \
	-Wall \
	-fomit-frame-pointer \
	-ffast-math \
	-fno-unroll-loops \
	-fno-inline-functions
LDFLAGS=
CSTUB=
LIBS=-lc -lgcc -lm

ifdef DEBUG
	CFLAGS+=\
		-gstabs+3 \
 		-O0 \
 		-fno-omit-frame-pointer \
 		-fno-fast-math
	OBJDIR=debug_dos
else
	CFLAGS+=-DNDEBUG
	LDFLAGS+=-s
	OBJDIR=release_dos
endif

#
# Filename variables
#

# Executable
BUILD=$(OBJDIR)/Quake.exe

# Source files
NET= \
	net_dgrm.c \
	net_loop.c \
	net_main.c \
	net_vcr.c

RENDER= \
	r_aclip.c \
	r_aclipa.s \
	r_alias.c \
	r_aliasa.s \
	r_bsp.c \
	r_draw.c \
	r_drawa.s \
	r_edge.c \
	r_edgea.s \
	r_efrag.c \
	r_light.c \
	r_main.c \
	r_misc.c \
	r_part.c \
	r_sky.c \
	r_sprite.c \
	r_surf.c \
	r_vars.c \
	r_varsa.s

DRAW= \
	d_copy.s \
	d_draw.s \
	d_draw16.s \
	d_edge.c \
	d_fill.c \
	d_init.c \
	d_modech.c \
	d_part.c \
	d_polyse.c \
	d_polysa.s \
	d_parta.s \
	d_scan.c \
	d_scana.s \
	d_sky.c \
	d_spr8.s \
	d_sprite.c \
	d_surf.c \
	d_vars.c \
	d_varsa.s \
	d_zpoint.c

CLIENT= \
	nvs_client.c \
	cl_demo.c \
	cl_input.c \
	cl_main.c \
	cl_parse.c \
	cl_tent.c

SERVER= \
	nvs_server.c \
	nvs_server_data.c \
	sv_main.c \
	sv_move.c \
 	sv_phys.c \
	sv_user.c

SOUND= \
	snd_dma.c \
	snd_mem.c \
	snd_mix.c \
	snd_mixa.s

PROGRAMS= \
	pr_cmds.c \
	pr_edict.c \
	pr_exec.c

DOS= \
	cd_audio.c \
	dos_v2.c \
	in_dos.c \
	mplib.c \
	mplpc.c \
	net_bw.c \
	net_dos.c \
	net_ipx.c \
	net_mp.c \
	net_ser.c \
	snd_dos.c \
	snd_gus.c \
	sys_dos.c \
	sys_dosa.s \
	vid_vga.c \
	vid_ext.c \
	vid_dos.c \
	vregset.c

SOURCES= \
	$(CLIENT) \
	$(DRAW) \
	$(NET) \
	$(PROGRAMS) \
	$(RENDER) \
	$(SERVER) \
	$(SOUND) \
	$(DOS) \
	chase.c \
	cmd.c \
	nvs_common.c \
	common.c \
	console.c \
	crc.c \
	cvar.c \
	draw.c \
	host.c \
	host_cmd.c \
	keys.c \
	math.s \
	mathlib.c \
	menu.c \
	model_common.c \
	model.c \
	nonintel.c \
	sbar.c \
	screen.c \
	surf8.s \
	surf16.s \
	view.c \
	wad.c \
	world.c \
	worlda.s \
	zone.c

# Compute object files from source files
OBJS=\
	$(addprefix $(OBJDIR)/,\
		$(addsuffix .o,\
			$(basename $(SOURCES))\
		)\
	)

# Compute dependency files from source files
DEPS=\
	$(addprefix $(OBJDIR)/,\
		$(addsuffix .d,\
			$(basename $(SOURCES))\
		)\
	)


#
# Rules
#

# Standard for "Build it, please"
.PHONY : all
all : $(BUILD)
	@echo "Done."

# Compile directory
$(OBJDIR) :
	@echo "Creating compiling directory $(OBJDIR)..."
	mkdir $(OBJDIR)

# Dependencies
SEDDEP=\
	sed '1s|$*.o|$(OBJDIR)/& $@ |' > $@;\
 		[ -s $@ ] || $(RM) $@

$(OBJDIR)/%.d : %.c $(OBJDIR)
	@echo "Generating dependencies for $<..."
	set -e; $(CC) -MM $(CFLAGS) $< | $(SEDDEP)

$(OBJDIR)/%.d : ../asm/%.s $(OBJDIR)
	@echo "Generating dependencies for $<..."
	set -e; $(AS) -MM $(CFLAGS) $< | $(SEDDEP)

# Objects
$(OBJDIR)/%.o : %.c $(OBJDIR)
	@echo "Compiling $<..."
	$(CC) $(CFLAGS) -c -o $@ $<

$(OBJDIR)/%.o : ../asm/%.s $(OBJDIR)
	@echo "Assembling $<..."
	$(AS) $(CFLAGS) -c -o $@ $<

# Include all dependencies
ifeq (,$(filter $(MAKECMDGOALS),\
	clean \
	cleanexe \
	cleanobj \
	cleandep))
-include $(DEPS)
endif

# Executable
$(BUILD) : $(OBJS)
	@echo "Linking $@..."
	$(LD) -o $(BUILD) $(LDFLAGS) $(CSTUB) $(OBJS) $(LIBS)


#
# Cleaning procedures
#
.PHONY : clean cleanexe cleanobj cleandep

clean : cleanexe cleanobj cleandep

cleanexe :
	@echo "Cleaning executable from $(OBJDIR)..."
	$(RM) $(BUILD)

cleanobj :
	@echo "Cleaning objects from $(OBJDIR)..."
	$(RM) $(OBJS)

cleandep :
	@echo "Cleaning dependecies from $(OBJDIR)..."
	$(RM) $(DEPS)
