# Version: 6
# 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 := maemo_linux

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

#Sources
srcs:= src/main.cpp \
  src/aumiks/aumiks.cpp \
  src/aumiks/synth/synth.cpp \
  src/MainWindow.cpp \
  src/MainMenuView.cpp \
  src/ThereminView.cpp \
  src/ListenView.cpp \
  src/OptionsDialog.cpp \
  src/AboutDialog.cpp \
  src/NetworkSettingsDialog.cpp \
  src/RegisterDialog.cpp \
  src/Preferences.cpp \
  src/MainThread.cpp \
  src/PlaybackThread.cpp \
  src/utils.cpp \
  third_party/cliser/clt/NetworkThread.cpp \
  third_party/cliser/util/util.cpp \
  third_party/pugixml/pugixml.cpp

defines :=
#defines += -DDEBUG
defines += -DM_USE_FIXED_POINT
#defines += -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`
endif
ifeq ($(platform), maemo_linux)
    compiler_flags += `pkg-config hildonmm hildon-fmmm --cflags`
endif


#=======================
#  Include directories
#=======================
ifeq ($(platform), maemo_linux)
    include_dirs := -Ithird_party
endif
ifeq ($(platform), linux)
    include_dirs := -Ithird_party
endif
ifeq ($(platform), win32)
    include_dirs :=
endif
ifeq ($(platform), win32cross)
    include_dirs :=
endif


#============================
#  Libs and lib directories
#============================
ifeq ($(platform), maemo_linux)
    libs:= -lasound `pkg-config libosso hildon-1 hildonmm hildon-fmmm --libs`
endif
ifeq ($(platform), linux)
    libs:= -lasound `pkg-config gtkmm-2.4 --libs`
endif
ifeq ($(platform), win32)
    libs:= 
endif
ifeq ($(platform), win32cross)
    libs:=
endif


remove_on_clean:= debian/tmp

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

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

obj_dir := obj/1
compiler := g++

include_dirs += -I./src

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

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

create-obj-dir:
ifeq ($(platform), win32)
	@if not exist $(obj_dir) mkdir $(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 $<...
	@mkdir -p $(dir $@)
# -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)
	@$(remove) $(name)
	@$(remove) $(obj_dir)\*
else
	@$(remove) $(name)
	@$(remove) -rf $(dir $(obj_dir))
	@$(remove) -rf $(remove_on_clean)
endif
