# Version: 5
# 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:
#    win32
#    linux
#    win32cross
platform:=linux

ifeq ($(platform),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.cpp \
  src/MainWindow.cpp \
  src/OptionsDialog.cpp \
  src/AboutDialog.cpp \
  src/pugixml/pugixml.cpp

defines:= #-DDEBUG #-DM_NO_AUDIO

compiler_flags:=-Wall -Wno-comment -funsigned-char `pkg-config hildonmm hildon-fmmm --cflags`
                #-P -E#-Wnon-virtual-dtor -Wreorder #-Wall #turn on all warnings
                #-O3 -funroll-loops -fomit-frame-pointer

ifeq ($(platform),linux)
    include_dirs:=
endif
ifeq ($(platform),win32)
    include_dirs:= 
endif
ifeq ($(platform),win32cross)
    include_dirs:=
endif



linker_flags:=`pkg-config hildonmm hildon-fmmm --libs` -s

ifeq ($(platform),linux)
    libs:= -lasound
endif

ifeq ($(platform),win32)
    libs:= 
endif

ifeq ($(platform),win32cross)
    libs:=
endif


remove_on_clean:= debian/tmp

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

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

obj_dir:=obj
compiler:=g++

source_dirs:=$(dir $(srcs) )

include_dirs += -I./src

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

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


create-obj-dir:
ifeq ($(platform),linux)
	@mkdir -p $(obj_dir)
endif
ifeq ($(platform),win32)
	@if not exist $(obj_dir) mkdir $(obj_dir)
endif
ifeq ($(platform),win32cross)
	@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)/,$(notdir $(patsubst %.cpp,%.o,$(srcs))))
	@echo Linking $@...
	@$(compiler) $^ -o "$@" $(libs) $(linker_flags)

#======================
#=compile .cpp sources=
VPATH:=$(source_dirs)
$(obj_dir)/%.o:%.cpp
	@echo Compiling $<...
# -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 $(obj_dir)/*.d)

#==================
#=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),linux)
	@$(remove) $(name);\
	$(remove) -rf $(obj_dir); \
	$(remove) -rf $(remove_on_clean);
endif
ifeq ($(platform),win32)
	@$(remove) $(name)
	@$(remove) $(obj_dir)\*
endif
ifeq ($(platform),win32cross)
	@$(remove) $(name);\
	$(remove) -rf $(obj_dir); \
	$(remove) -rf $(remove_on_clean);
endif
