
prefix =          @prefix@
exec_prefix =     @exec_prefix@
datarootdir =     @datarootdir@
datadir =         @datadir@

DATADIR =         $(datadir)/games/liquidwar
GAMEDIR =         $(exec_prefix)/games

HOST_OPT =        @HOST_OPT@
STATIC =          @STATIC@
SNPRINTF =        @SNPRINTF@
ALCFLAGS =        @ALCFLAGS@
ASM =             @ASM@
DEBUG =           @DEBUG@
EFENCE =          @EFENCE@
PTHREAD =         @PTHREAD@
GP2X =            @GP2X@

ifeq ($(HOST_OPT),yes)
	HOST =          @host@
	HOST_CPU =      @host_cpu@
	HOST_CPU_CC =   "-march=@host_cpu@"
else
	HOST =          default
	HOST_CPU =      default
	HOST_CPU_CC =
endif

# -mcpu=x86_64 doesn't work, here's a workarround
ifeq ("@host_cpu@","x86_64")
	HOST_CPU_CC =   "-m64"
endif


ifeq ($(DEBUG),yes)
	DEBUG_FLAGS =   -g3
	DEBUG_OPTIONS = -DDEBUG
	ALLEGRO_LIBRARY = debug
else
	DEBUG_FLAGS =   -O2 -fomit-frame-pointer
	DEBUG_OPTIONS =
	ALLEGRO_LIBRARY = release
endif

ifeq ($(EFENCE),yes)
	EFENCE_LIBRARY = -lefence
else
	EFENCE_LIBRARY =
endif

ifeq ($(GP2X),yes)
	GP2X_OPTIONS = -DGP2X
else
	GP2X_OPTIONS =
endif

ifeq ($(SNPRINTF),yes)
	SNPRINTF_NAMES =
	SNPRINTF_OPTIONS =
	SNPRINTF_MODE = "using system library"
else
	SNPRINTF_NAMES = ../utils/snprintf/snprintf
	SNPRINTF_OPTIONS = -DSNPRINTF
	SNPRINTF_MODE = "using Mark Martinec's implementation"
endif

ifeq ($(ASM),yes)
	ASM_NAMES = glouglou monster spread
	ASM_OPTIONS = -DASM
else
	ASM_NAMES =
	ASM_OPTIONS =
endif

OBJ_NAMES = \
	about \
	advanced \
	alleg2 \
	area \
	army \
	asm \
	autoplay \
	back \
	basicopt \
	bigdata \
	capture \
	chat \
	checksum \
	code \
	config \
	connect \
	controls \
	cursor \
	decal \
	dialog \
	disk \
	disp \
	distor \
	dnsutil \
	error \
	exec2 \
	exit \
	fighter \
	game \
	gfxmode \
	grad \
	graphics \
	help \
	httputil \
	info \
	init \
	internet \
	joystick \
	keyboard \
	keyexch \
	lang \
	langde \
	langdk \
	langen \
	langfr \
	langpl \
	language \
	level \
	logcli \
	lw6 \
	main \
	map \
	maptex \
	menu \
	mesh \
	message \
	mouse \
	move \
	music \
	mutxunix \
	netconf \
	netgame \
	netkey \
	netmap \
	netmess \
	netplay \
	network \
	options \
	palette \
	parser \
	path \
	ping \
	pion \
	play \
	profile \
	protocol \
	random \
	rules \
	score \
	serial \
	sock2cli \
	sockex \
	sockunix \
	sound \
	speeds \
	startinf \
	startup \
	team \
	texture \
	thrdunix \
	ticker \
	time \
	viewport \
	volume \
	watchdog \
	wave \
	wwwcli \
	x11icon \
	$(SNPRINTF_NAMES) \
	$(ASM_NAMES)

OBJ_NAMES_SRV = \
	basicopt \
	chat \
	dnsutil \
	execunix \
	httputil \
	logsrv \
	netconf \
	netkey \
	netmap \
	netmess \
	parser \
	serial \
	server \
	sock2srv \
	sockex \
	sockunix \
	srvchan \
	srvcont \
	srvteam \
	srvtime \
	startinf \
	thrdunix \
	wwwsrv \
	$(SNPRINTF_NAMES)

OBJ_NAMES_MAPGEN = \
	../utils/lwmapgen/big_quad \
	../utils/lwmapgen/boxes \
	../utils/lwmapgen/bubbles \
	../utils/lwmapgen/circles \
	../utils/lwmapgen/circuit \
	../utils/lwmapgen/cut \
	../utils/lwmapgen/func \
	../utils/lwmapgen/hole \
	../utils/lwmapgen/lines \
	../utils/lwmapgen/main \
	../utils/lwmapgen/map \
	../utils/lwmapgen/map_generator \
	../utils/lwmapgen/misc \
	../utils/lwmapgen/rand_box \
	../utils/lwmapgen/rand_func \
	../utils/lwmapgen/rand_poly \
	../utils/lwmapgen/rand_poly_cut \
	../utils/lwmapgen/street \
	../utils/lwmapgen/worms

OBJ_FILES = $(addsuffix .o, $(OBJ_NAMES))
OBJ_FILES_SRV = $(addsuffix .o, $(OBJ_NAMES_SRV))
OBJ_FILES_MAPGEN = $(addsuffix .o, $(OBJ_NAMES_MAPGEN))

EXE_FILES = liquidwar liquidwar-server liquidwar-mapgen

ifeq ($(STATIC),yes)
    LINK_OPTIONS = --static
    EXTERN_LIBS = `allegro-config --static $(ALLEGRO_LIBRARY)` $(EFENCE_LIBRARY)
else
    LINK_OPTIONS =
    EXTERN_LIBS = `allegro-config --libs $(ALLEGRO_LIBRARY)` $(EFENCE_LIBRARY)
endif

CONFIG_OPTIONS = \
	-DCONFIG_UNIX_CFG=\".liquidwarrc\" \
	-DCONFIG_UNIX_DAT=\"$(DATADIR)/liquidwar.dat\" \
	-DCONFIG_UNIX_MAP=\"$(DATADIR)/map/\" \
	-DCONFIG_UNIX_TEX=\"$(DATADIR)/texture/\" \
	-DCONFIG_UNIX_MID=\"$(DATADIR)/music/\" \
	-DCONFIG_UNIX_SRV=\"$(GAMEDIR)/liquidwar-server\" \
	-DCONFIG_UNIX_GEN=\"$(GAMEDIR)/liquidwar-mapgen\"

CMD_CC = $(CC) $(CFLAGS) $(ALCFLAGS) -c $(DEBUG_FLAGS) -Wall -W $(HOST_CPU_CC) $(CONFIG_OPTIONS) $(DEBUG_OPTIONS) $(SPRINTF_OPTIONS) $(ASM_OPTIONS) $(GP2X_OPTIONS) -DUNIX -D_THREAD_SAFE
CMD_AS = $(AS) $(ASFLAGS) --32

all: $(EXE_FILES)

liquidwar: $(OBJ_FILES)
	@echo "Linking $@ (host=$(HOST), debug=$(DEBUG), static=$(STATIC), asm=$(ASM))"
	@$(CC) $(LDFLAGS) $(LINK_OPTIONS) -o $@ $(OBJ_FILES) $(PTHREAD) $(EXTERN_LIBS)

liquidwar-server: $(OBJ_FILES_SRV)
	@echo "Linking $@ (host=$(HOST), debug=$(DEBUG), static=$(STATIC))"
	@$(CC) $(LDFLAGS) $(LINK_OPTIONS) -o $@ $(OBJ_FILES_SRV) $(PTHREAD)

liquidwar-mapgen: $(OBJ_FILES_MAPGEN)
	@echo "Linking $@ (host=$(HOST), debug=$(DEBUG), static=$(STATIC))"
	@$(CC) $(LDFLAGS) $(LINK_OPTIONS) -o $@ $(OBJ_FILES_MAPGEN) -lm $(PTHREAD) $(EXTERN_LIBS)

dep:
	@find . \
	-name '*.c' | \
	grep "\.c" | \
	sed "s/^.\///;" | \
	xargs $(CMD_CC) -M | \
	sed "s/\(.*\.o:\)/\1/g;" > Makefile.dep

clean:
	@rm -f *.o *.obj
	@rm -f $(OBJ_FILES) $(OBJ_FILES_SRV) $(OBJ_FILES_MAPGEN)
	@rm -f $(EXE_FILES)

distclean:
	@rm -f Makefile Makefile.dep

%.o: %.c
	@echo "Compiling $< (cpu=$(HOST_CPU), debug=$(DEBUG), asm=$(ASM))"
	@$(CMD_CC) $< -o $@

%.o: %.s
	@echo "Assembling" $<
	@$(CMD_AS) $< -o $@

config: print_config print_cmd_cc print_cmd_as print_cmd_ld
	@true

check:
	@echo No unit tests, blame the lazy developper.

indent:
	@find . -name "*.[c|h]" -a ! -path "*32*" -exec indent -nut {} \;
	@find . -name "*.[c|h]" -a ! -path "*32*" -exec splint {} \;

print_config:
	@echo "The configuration options to generate binaries are:"
	@echo "Host        :" $(HOST)
	@echo "Debug       :" $(DEBUG)
	@echo "Static link :" $(STATIC)
	@echo "Assembly    :" $(ASM)
	@echo "Snprintf    :" $(SNPRINTF_MODE)

print_cmd_cc:
	@echo "The command line to compile .c files in this directory is:"
	@echo $(CMD_CC)

print_cmd_as:
	@echo "The command line to compile .s files in this directory is:"
	@echo $(CMD_AS)

print_cmd_ld:
	@echo "The command line to link .o files in this directory is:"
	@echo $(CC) $(LDFLAGS) $(LINK_OPTIONS) $(PTHREAD) $(EXTERN_LIBS)

include Makefile.dep
