#-*-makefile-*-
# Copyright (C) 2002 Asfand Yar Qazi.

#  This file is part of XBobble.

#     XBobble is free software; you can redistribute it and/or modify it
#     under the terms of the GNU General Public License as published by
#     the Free Software Foundation; either version 2 of the License, or
#     (at your option) any later version.

#     XBobble is distributed in the hope that it will be useful, but
#     WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#     General Public License for more details.

#     You should have received a copy of the GNU General Public License
#     along with XBobble; if not, write to the Free Software Foundation,
#     Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

# Note: this file is specifically for GCC 3 or more and GNU make 3.79
# or greater

CC=gcc
CXX=g++

# Later -I flags are overridden by previous ones.  So for example if
# an SDL_opengl.h can't be found in the SDL default location, then the
# one in . is used.

override CXXFLAGS:=-g -Wall -pipe `sdl-config --cflags` -I. $(CXXFLAGS)
ifdef PCH
override CXXFLAGS:=$(CXXFLAGS) -include all.hh
endif

MY_SDL_LDFLAGS=`sdl-config --libs` -lSDL_mixer -lSDL_image -lpthread

MY_OPENGL_LDFLAGS=-lGLU -lGL -lXmu -lXi -lXext -lX11 -lm

override LDFLAGS:=$(MY_SDL_LDFLAGS) $(MY_OPENGL_LDFLAGS) -lm $(LDFLAGS)

SOURCES=$(wildcard *.cc)
TEST_SOURCES=$(wildcard test/*.cc)
LIB_SOURCES=$(filter-out main.cc, $(SOURCES))

APP_BIN_NAME=xbobble1-bin

SHELL=/bin/sh

.PHONY: all docs
ifdef PCH
all: all.hh.gch $(APP_BIN_NAME)
else
all: $(APP_BIN_NAME)
endif

$(APP_BIN_NAME): main.o $(LIB_SOURCES:.cc=.o)
	$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

docs:
	${MAKE} -C doc/doxygen docs

.PHONY: tests
tests: $(TEST_SOURCES:.cc=)

all.hh.gch: all.hh $(wildcard *.hh)
	$(CXX) -x c++ $(CXXFLAGS) -c $< -o $<.gch

test/%: test/%.o $(LIB_SOURCES:.cc=.o)
	$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

test/%.o: test/%.cc
	$(CXX) $(CXXFLAGS) -c -o $@ $<

test/testSystem: test/testSystem.o Game_Manager.o System.o Video_Output_Manager.o Sound_Output_Manager.o User_Input_Manager.o Config_Parser.o util.o
	$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

test/testConfig_Parser: test/testConfig_Parser.o Config_Parser.o
	$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

test/test_lod: test/test_lod.o Ball_Trait.o
	$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

%.d: %.cc
	@echo Generating dependancies for $<
	@set -e; $(CXX) -M -MT "$(@:.d=.o) $@" $(CXXFLAGS) $< > $@; \
	[ -s $@ ] || rm -f $@

include $(SOURCES:.cc=.d) $(TEST_SOURCES:.cc=.d)

.PHONY: clean distclean crapclean
crapclean:
	find . \( \
		-name '#*#' -o -name '.#*' -o -name '*~' -o -name '.*~' \
		-o -name core -o -name malloc_trace \
		\) -exec rm -fv {} \;
clean: crapclean
	find . \( \
		-name '*.o' -o -name '*.a' -o -name '*.so' -o -name '.so.*' \
		-o -name $(APP_BIN_NAME) \
		\) -exec rm -fv {} \;
	rm -fv $(TEST_SOURCES:.cc=) all.hh.gch
	${MAKE} -C doc/doxygen clean
distclean: clean
	find . -name '*.d' -exec rm -fv {} \;

