#-------------------------------------------------------------------------------
# Host SW Linux Userspace Makefile
#
# This is the top level makefile for the Host SW userspace application. It will
# build the pcoip_agent application and the related user space components. 
# 
# Build options:
# 
# -CONFIG=Release|Debug
# A release build will be created by default. 
#
# Copyright (c) Teradici Corporation (2013)
#-------------------------------------------------------------------------------

include ../common/build/makefile.common 

OUTPUT_NAME = pcoip_agent
OUTPUT_PATH = bin
OUTPUT_OBJ_PATH = obj

# List of directories to ignore
IGNORE_DIRS = daemon

ifeq ($(CONFIG),Release)
LIBS = /usr/local/lib/libwxcode_gtk2u_propgrid-2.8.a
LIBS += $(shell wx-config  --version=2.8 --unicode=yes --debug=no --static=yes --libs)
else
LIBS = /usr/local/lib/libwxcode_gtk2ud_propgrid-2.8.a
LIBS += $(shell wx-config  --version=2.8 --unicode=yes --debug=yes --static=yes --libs)
endif

# Filter out the directories without makefiles
DIRS_MK := $(filter %,$(wildcard ../*/Makefile))

# Filter out the current directory from the list
CURDIR_NAME := $(lastword $(subst /, ,$(CURDIR)))
DIRS_MK := $(filter-out %$(CURDIR_NAME)/Makefile,$(DIRS_MK))

# Filter out the directories on the ignore list
DIRS_MK := $(foreach ignore_DIRS,$(IGNORE_DIRS),$(filter-out %$(ignore_DIRS)/Makefile,$(DIRS_MK)))

# Remove "Makefile" from the path.
DIRS := $(subst Makefile, , $(DIRS_MK))

# For directories with makefiles, retrieve the source names and create
# the object names. Change the source path to the object folder path.
SRCS = $(foreach dir_PATH,$(DIRS),$(wildcard $(dir_PATH)*/*.cpp))
OBJS = $(patsubst %.cpp, %.o, $(SRCS))
OBJS := $(subst /src/,/$(OUTPUT_OBJ_PATH)/$(CONFIG)/,$(OBJS))

# This is a hack. There is a source file inside a "res" folder. However,
# the objects are stored in <module directory>/obj folder. We have 
# change this path here instead of ignoring the directory.
OBJS := $(subst /res/,/$(OUTPUT_OBJ_PATH)/$(CONFIG)/,$(OBJS))

# Create a list of rules for module cleaning. We are appending
# "-clean-release" or "-clean-debug" to the directory paths to avoid
# naming collisions. We have to create these modified directory paths
# so the make rule will properly invoke clean_xxx for all sub modules.
CLEAN_DIRS = $(DIRS:%=%-clean)
CLEAN_RELEASE_DIRS = $(DIRS:%=%-clean-release)
CLEAN_DEBUG_DIRS = $(DIRS:%=%-clean-debug)

# Delete the pre-existing output before we begin.
$(shell rm -f $(OUTPUT_PATH)/$(CONFIG)/$(OUTPUT_NAME))

.PHONY: all $(DIRS) $(OUTPUT_NAME)
.PHONY: clean clean-debug clean-release 
.PHONY: $(CLEAN_RELEASE_DIRS) $(CLEAN_DEBUG_DIRS)

all: $(DIRS) $(OUTPUT_NAME)

$(DIRS):
	$(MAKE) -C $@ CONFIG=$(CONFIG)

$(OUTPUT_NAME):
	mkdir -p $(OUTPUT_PATH)/$(CONFIG)
	$(CXX) $(CPPFLAGS) -o $(OUTPUT_PATH)/$(CONFIG)/$(OUTPUT_NAME) $(OBJS) $(LIBS)	

$(CLEAN_DEBUG_DIRS):
	$(MAKE) -C $(@:%-clean-debug=%) clean-debug

clean-debug: $(CLEAN_DEBUG_DIRS)
	rm -Rf $(OUTPUT_OBJ_PATH)/Debug $(OUTPUT_PATH)/Debug

$(CLEAN_RELEASE_DIRS):
	$(MAKE) -C $(@:%-clean-release=%) clean-release

clean-release: $(CLEAN_RELEASE_DIRS)
	rm -Rf $(OUTPUT_OBJ_PATH)/Release $(OUTPUT_PATH)/Release

$(CLEAN_DIRS):
	$(MAKE) -C $(@:%-clean=%) clean

clean: clean-debug clean-release $(CLEAN_DIRS)
	rm -Rf $(OUTPUT_OBJ_PATH)
	rm -Rf $(OUTPUT_PATH)

