# Last modified: 2009.09.03
#
# Author: Ivan Gagis
#         igagis@gmail.com

#==============================================================================
#==============================================================================
#==============================================================================
#                        Project configuration part
#             change these strings to configure project building

#default platform. It may be overriden by specifying platform=xxx in command line when running make
#Known platforms are:
#    maemo_linux
#    win32
#    linux
#    win32cross
platform := linux

ifeq ($(platform), linux)
    name := therver
endif
ifeq ($(platform), maemo_linux)
    name := therver
endif
ifeq ($(platform), win32)
    name := therver.exe
endif
ifeq ($(platform), win32cross)
    name := therver.exe
endif

#Sources
srcs:= src/main.cpp \
  src/Therver.cpp \
  src/Client.cpp \
  src/Cast.cpp \
  src/SQLite.cpp \
  src/DatabaseThread.cpp \
  ../third_party/cliser/srv/Client.cpp \
  ../third_party/cliser/srv/Server.cpp \
  ../third_party/cliser/srv/TCPAcceptorThread.cpp \
  ../third_party/cliser/srv/TCPClientsHandlerThread.cpp \
  ../third_party/cliser/srv/ThreadsKillerThread.cpp \
  ../third_party/cliser/util/util.cpp

defines := -DDEBUG #-DM_NO_AUDIO

ifeq ($(platform), linux)
    defines += -DM_NON_MAEMO_LINUX
endif

compiler_flags := -Wall -Wno-comment -funsigned-char
                #-P -E#-Wnon-virtual-dtor -Wreorder #-Wall #turn on all warnings
                #-O3 -funroll-loops -fomit-frame-pointer

linker_flags := -s

ifeq ($(platform), linux)
#    compiler_flags += `pkg-config gtkmm-2.4 --cflags`
#    linker_flags += `pkg-config gtkmm-2.4 --libs`
endif
ifeq ($(platform), maemo_linux)
#    compiler_flags += `pkg-config hildonmm hildon-fmmm --cflags`
#    linker_flags += `pkg-config hildonmm hildon-fmmm --libs`
endif


#=======================
#  Include directories
#=======================
ifeq ($(platform), maemo_linux)
    include_dirs :=
endif
ifeq ($(platform), linux)
    include_dirs := -I../third_party
endif
ifeq ($(platform), win32)
    include_dirs := -I../third_party
endif
ifeq ($(platform), win32cross)
    include_dirs := -I../third_party
endif


#============================
#  Libs and lib directories
#============================
ifeq ($(platform), maemo_linux)
    libs:= 
endif
ifeq ($(platform), linux)
    libs:= -lpthread -lrt -lsqlite3
endif
ifeq ($(platform), win32)
    libs:= -lws2_32
endif
ifeq ($(platform), win32cross)
    libs:=
endif


remove_on_clean := #debian/tmp

#                     __
#                    /  \__/  end of configuration part
#==============================================================================

obj_dir_root := obj
obj_dir := $(obj_dir_root)/1

ifeq ($(platform),win32)
    remove := del /Q /F 
else
    remove := rm -rf
endif

compiler := g++

include_dirs += -I./src

#==============================================================================
#=============================TARGETS==========================================
#==============================================================================

#==========================
#=project (default) target=
proj: create-obj-dir $(name)

create-obj-dir:
ifeq ($(platform), win32)
	@if not exist $(subst /,\,$(obj_dir)) mkdir $(subst /,\,$(obj_dir))
else
	@mkdir -p $(obj_dir)
endif

#find all .cpp files and get an .o file name for it to get dependancies for this target
$(name): $(addprefix $(obj_dir)/,$(patsubst %.cpp,%.o,$(srcs)))
	@echo Linking $@...
	@$(compiler) $^ -o "$@" $(libs) $(linker_flags)

#======================
#=compile .cpp sources=
$(obj_dir)/%.o:%.cpp
	@echo Compiling $<...
ifeq ($(platform), win32)
	@if not exist $(subst /,\,$(dir $@)) mkdir $(subst /,\,$(dir $@))
else
	@mkdir -p $(dir $@)
endif
# -MF option specifies dependency output file name
	@$(compiler) -c -MF "$(patsubst %.o,%.d,$@)" -MD -o "$@" $(compiler_flags) $(defines) $(include_dirs) $<
#workaround for gcc 4.2 compiler (it behaves differently than gcc 4.1 for some reason)
#ifeq ($(platform), linux)
#	@echo -n $(obj_dir)/ > $(patsubst %.o,%.d_,$@)
#	@cat $(patsubst %.o,%.d,$@) >> $(patsubst %.o,%.d_,$@)
#	@mv $(patsubst %.o,%.d_,$@) $(patsubst %.o,%.d,$@)
#endif

include $(wildcard $(addsuffix /*.d,$(dir $(addprefix $(obj_dir)/,$(srcs)))))

#==================
#=build all target=
all: clean proj

#==============
#=clean target=
#delete all objects and executables

#it is an abstract target (not a file), declare as .PHONY
.PHONY: clean
clean:
ifeq ($(platform),win32)
	@del /Q /F $(name)
	@rmdir /Q /S $(obj_dir_root)
else
	@rm -rf $(name)
	@rm -rf $(obj_dir_root)
	@rm -rf $(remove_on_clean)
endif
