## 
## To incorporate your code into the build-process there 
## are only two things you need to do:
##
## 1. 	If you add new classes (*.h AND *.cpp files) you need 
## 		to add one entry in the Object File Section below. 
## 
## 2. 	If you also add subdirectories you need to add a 
## 		target definition in the Source Code Compile Section
## 		which can be found at the end of this file. 
## 
## If you get into trouble editing this file or get into problems 
## when adding your code to the build process we will help you. 
## 
## Have fun! 

# Makefile global settings: 
CPUS				= $(shell cat /proc/cpuinfo  | grep MHz | wc -l)
PARALLELPROC		= $(shell echo ${CPUS}*2 | bc)
PROJECT_ROOT  		= $(shell pwd)
TARGET				= unknown
PROG          		= Framework
DEBUGFLAGS    		= -g
COMMON_INCLUDES		= -I$(PROJECT_ROOT)/src \
					-I$(PROJECT_ROOT)/src/base \
					-I$(PROJECT_ROOT)/src/commander \
					-I$(PROJECT_ROOT)/src/components \
					-I$(PROJECT_ROOT)/src/gl
CFLAGS 				= -Wall -DDEBUG_MODE

# ----- TARGET SPECIFIG SETTINGS (compile and link) -----
# Targets are: 
# 1. Linux x86
# 2. QNX (v. 641) x86
# 3. Linux (v. 632) sh4

TARGETS = LINUX_X86 QNX_X86 QNX_SH4
BINDIR	= $(PROJECT_ROOT)/bin/$(TARGET)

ifeq ($(TARGET), LINUX_X86) 
	CC 			= g++
	LD 			= g++
	LIBS		= -lrt -lpthread -lGL -lglut -lfreetype 
	CFLAGS		+= -DLINUX  $(DEBUGFLAGS)
	LDFLAGS		= $(DEBUGFLAGS)
	INCLUDES	= $(COMMON_INCLUDES) -I/usr/include/freetype2
	TARGETOBJS  = $(BINDIR)/CGraphicsDriverGLUT.o
endif # LINUX_X86

ifeq ($(TARGET), QNX_X86)
	QNX_VERSION	=641
	QNX_ROOT=/opt/qnx$(QNX_VERSION)
	QNXBIN=$(QNXROOT)/host/linux/x86/usr/bin
	QNX_TARGET=$(QNX_ROOT)/target/qnx6
	QNX_HOST=$(QNX_ROOT)/host/linux/x86

	CC 			= $(QNX_HOST)/usr/bin/QCC
	LD 			= $(QNX_HOST)/usr/bin/QCC
	LIBS		= -lgf -lGLES_CM $(PROJECT_ROOT)/lib/qnx641/libfreetype.a -lz 
	CFLAGS		+= -Vgcc_ntox86 -Y_ecpp-ne -DQNX -DEGL $(DEBUGFLAGS)
	LDFLAGS		= $(DEBUGFLAGS) $(BUILDTYPE)
	INCLUDES	= $(COMMON_INCLUDES) -I$(PROJECT_ROOT)/include/qnx641
	TARGETOBJS  = $(BINDIR)/CGraphicsDriverEGL.o
endif # QNX_X86

ifeq ($(TARGET), QNX_SH4) 
	QNX_VERSION=632
	QNX_ROOT=/opt/qnx$(QNX_VERSION)
	QNX_TARGET=$(QNX_ROOT)/target/qnx6
	QNX_HOST=$(QNX_ROOT)/host/linux/x86

	CC 				= $(QNX_HOST)/usr/bin/QCC
	LD 				= $(QNX_HOST)/usr/bin/QCC
	CFLAGS 			+= -V2.95.3,gcc_ntoshle -Y_ecpp-ne -DQNX -DGFX -DTARGET_D1 $(DEBUGFLAGS)
	LDFLAGS 		= -Vgcc_ntoshle $(DEBUGFLAGS)

	LIBDIR			= -L$(PROJECT_ROOT)/lib/qnx632 -L$(QNX_TARGET)/shle/lib/nv 
	LIBS			= -lm -lEGL -lGLES_CM -lgf -lgfx-gf -lned -lfreetype
	INCLUDES		= $(COMMON_INCLUDES) \
					-I$(PROJECT_ROOT)/src \
					-I$(PROJECT_ROOT)/src/base \
					-I$(PROJECT_ROOT)/include/qnx632 \
					-I$(PROJECT_ROOT)/include/qnx632/nvidia
	TARGETOBJS  = $(BINDIR)/CGraphicsDriverGFX.o
endif # QNX_SH4

# ------- OBJECT FILE SECTION ------- 
# For every module you add to the sources you 
# need to add one line with the corresponding 
# name (CMyClass.o) to the OBJS definition below: 
OBJS += \
$(BINDIR)/CAdminComponent.o \
$(BINDIR)/CBinarySemaphore.o \
$(BINDIR)/CCdComponent.o \
$(BINDIR)/CCommQueue.o \
$(BINDIR)/CComponentContext.o \
$(BINDIR)/CContext.o \
$(BINDIR)/CDispatcher.o \
$(BINDIR)/CHmiComponent.o \
$(BINDIR)/CHmiGLThread.o \
$(BINDIR)/AGraphicsDriver.o \
$(BINDIR)/CFTRender.o \
$(BINDIR)/CHMIGUIImage.o \
$(BINDIR)/CHMIGUIImageDesc.o \
$(BINDIR)/CMainDispatcher.o \
$(BINDIR)/CMutex.o \
$(BINDIR)/CNaviComponent.o \
$(BINDIR)/CThread.o \
$(BINDIR)/CTunerComponent.o \
$(BINDIR)/CMiniCommander.o \
$(BINDIR)/CMiniComDriver.o \
$(BINDIR)/CMaxiComReceiver.o \
$(BINDIR)/main.o \
$(TARGETOBJS)


## 
# All Targets
all: 
	@echo 
	@echo "Now perform build for all platforms ..."
	@echo 
	@for i in $(TARGETS); do \
	echo "   ... build target $$i"; \
	(make $$i > /dev/null); done
	@echo
	@echo "Finished build for all platforms"
	
LINUX_%:
	@make -j$(PARALLELPROC) $(PROG) TARGET=$@

QNX_%:
	@make -j$(PARALLELPROC) $(PROG) TARGET=$@

# Linking Objects and Libs
$(PROG): check $(OBJS) 
	@echo 'Building target: $@'
	@echo 'Invoking linker'
	@cd $(BINDIR); $(LD) $(LDFLAGS) $(LIBDIR) -o $(BINDIR)/$(PROG) $(OBJS) $(LIBS)
	@echo 'Finished building target: $@'
	@echo ' '

# ----- SOURCE CODE COMPILE SECTION ----- 
# For every directory you add as subdirectory you 
# need to add one entry with the corresponding 
# dir-path to the target definition below. 
# 
# Uncomment the following template and insert
# the path to your subdir: 
#
#%.o: src/YourSubdirsPathGoesHere%.cpp
#	@echo 'Building file: $<'
#	@echo 'Invoking compiler'
#	$(CC) $(CFLAGS) $(INCLUDES) -c -o"$@" "$<"
#	@echo 'Finished building: $<'
#	@echo ' '

# Compiling Sources in top-level directory
$(BINDIR)/%.o: src/%.cpp
	@echo 'Building file: $<'
	@echo 'Invoking compiler'
	@mkdir -p $(BINDIR)
	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<
	@echo 'Finished building: $<'
	@echo ' '

# Compiling Sources in /src/base directory
$(BINDIR)/%.o: src/base/%.cpp
	@echo 'Building file: $<'
	@echo 'Invoking compiler'
	@mkdir -p $(BINDIR)
	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<
	@echo 'Finished building: $<'
	@echo ' '


# Compiling Sources in /src/commander directory
$(BINDIR)/%.o: src/commander/%.cpp
	@echo 'Building file: $<'
	@echo 'Invoking compiler'
	@mkdir -p $(BINDIR)
	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<
	@echo 'Finished building: $<'
	@echo ' '

# Compiling Sources in /src/components directory
$(BINDIR)/%.o: src/components/%.cpp
	@echo 'Building file: $<'
	@echo 'Invoking compiler'
	@mkdir -p $(BINDIR)
	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<
	@echo 'Finished building: $<'
	@echo ' '

# Compiling Sources in /src/gl directory
$(BINDIR)/%.o: src/gl/%.cpp
	@echo 'Building file: $<'
	@echo 'Invoking compiler'
	@mkdir -p $(BINDIR)
	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<
	@echo 'Finished building: $<'
	@echo ' '

# Printing the environment settings
check: 
	@echo 
	@echo " ---- Environment Settings ------ "
	@echo "      Target             [$(TARGET)]"
	@echo "      Compiler           [$(CC)]"
	@echo "      Linker             [$(LD)]"
	@echo "      Includes           [$(INCLUDES)]"
	@echo "      Cores available    [$(CPUS)]"
	@echo "      Parallel tasks     [$(PARALLELPROC)]"
	@echo "      Binaries           [$(BINDIR)]"	
	@echo "      TARGET             [$(QNX_TARGET)]"
	@echo "      HOST               [$(QNX_HOST)]"
	@echo "      PATH               [$(PATH)]"
	@echo " -------------------------------- " 
	@echo 

# Other Targets
clean:
	rm -rf $(PROJECT_ROOT)/bin/*
	-@echo ' '
	