##############################################################################
#
#    The MIT License (MIT)
#
#    Copyright (c) 2014 Vivante Corporation
#
#    Permission is hereby granted, free of charge, to any person obtaining a
#    copy of this software and associated documentation files (the "Software"),
#    to deal in the Software without restriction, including without limitation
#    the rights to use, copy, modify, merge, publish, distribute, sublicense,
#    and/or sell copies of the Software, and to permit persons to whom the
#    Software is furnished to do so, subject to the following conditions:
#
#    The above copyright notice and this permission notice shall be included in
#    all copies or substantial portions of the Software.
#
#    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#    DEALINGS IN THE SOFTWARE.
#
##############################################################################


################################################################################
# Macros.

################################################################################
DEVELOP=1


AQROOT          ?= $(PWD)
AQARCH          ?= $(AQROOT)/arch/XAQ2
AQVGARCH          ?= $(AQROOT)/arch/GC350

export AQROOT AQARCH

################################################################################
# Include common definitions.

include $(AQROOT)/makefile.linux.def

################################################################################
# Components of the project.

# app
CHIPINFO        := $(CHIPINFO_DIR)
HAL_TEST        := $(HAL_TEST_DIR)
HAL_UNIT_TEST   := $(AQROOT)/test/hal/common/UnitTest
GFX_TEST        := $(GFX_TEST_DIR)
VVLAUNCHER      := $(VVLAUNCHER_DIR)
ifneq ($(VIVANTE_NO_VG), 1)
TIGER           := $(AQROOT)/test/vg11/tiger
endif

ifeq ($(USE_VDK), 0)
TUTORIAL        := $(TUTORIAL_DIR)
else
VDK_TEST        := $(VDK_TEST_DIR)
endif
CL11_TEST       := $(CL11_TEST_DIR)
CL11_UNIT       := $(CL11_UNIT_DIR)


ifeq ($(STATIC_LINK), 1)
APP_HAL_LIST    := $(HAL_TEST)
ifneq ($(VIVANTE_NO_VG), 1)
APP_OVG11_LIST  := $(TIGER)
endif
APP_GFX_LIST    :=
else
APP_HAL_LIST    := $(HAL_TEST) $(HAL_UNIT_TEST) $(CHIPINFO)
APP_GFX_LIST    := $(GFX_TEST)
ifneq ($(VIVANTE_NO_VG), 1)
APP_OVG11_LIST  := $(TIGER)
endif
endif


ifeq ($(USE_VDK), 0)
APP_OES11_LIST  += $(TUTORIAL)
endif

APP_OCL11_LIST  := $(CL11_TEST)
APP_VDK_LIST    := $(VDK_TEST) $(TIGER) $(VVLAUNCHER)

APP_LIST        := $(APP_HAL_LIST)
ifneq ($(STATIC_LINK), 1)
APP_LIST        += $(APP_GFX_LIST)
endif

APP_LIST        += $(LIB_VIVANTE)
ifeq ($(USE_VDK), 1)
APP_LIST        += $(APP_VDK_LIST)
else
APP_LIST        += $(APP_OES11_LIST)
APP_LIST        += $(APP_OES2X_LIST)
ifneq ($(VIVANTE_NO_VG), 1)
APP_LIST        += $(APP_OVG11_LIST)
endif
endif
ifeq ($(USE_OPENCL), 1)
APP_LIST        += $(APP_OCL11_LIST)
endif

################################################################################
# Define the macros used in the common makefile.

MODULES     :=  $(APP_LIST)
MAIN_MODULE :=  $(APP_LIST)

OBJ_DIR     := $(SDK_DIR)

################################################################################
# Include the common makefile.

include $(AQROOT)/common.node

################################################################################
# Extra operations.

install: extra_install

clean: extra_clean

.PHONY: extra_install extra_clean
extra_install:
	@mkdir -p $(SDK_DIR)
	@-cp -f $(AQROOT)/release/SW/ReadMe_Linux_SDK.txt $(SDK_DIR)/ReadMe.txt

extra_clean:
	@rm -rf $(SDK_DIR)

################################################################################
# Define the dependencies.
ifneq ($(DEVELOP), 1)

$(APP_OES11_LIST): $(GAL_CORE) $(LIB_EGL) $(LIB_OES11_LIST)
$(APP_OES2X_LIST): $(GAL_CORE) $(LIB_EGL) $(LIB_OES2X_LIST)
ifneq ($(VIVANTE_NO_VG), 1)
$(APP_OVG11_LIST): $(GAL_CORE) $(LIB_EGL) $(LIB_OES2X_LIST) $(LIB_OVG11_LIST)
endif
$(APP_HAL_LIST): $(GAL_CORE) $(LIB_GAL)
$(APP_GFX_LIST): $(GAL_CORE) $(LIB_GAL) $(LIB_GFX)
ifeq ($(USE_VDK), 1)
$(APP_VDK_LIST): $(GAL_CORE)  $(LIB_EGL) $(LIB_VDK) $(LIB_OES11_LIST) $(LIB_OES2X_LIST) $(LIB_OVG11_LIST)
endif
endif
################################################################################
# Special targets.

.PHONY: $(GFX_TEST)
$(GFX_TEST):
ifeq (,$(DFB_DIR))
	@echo "DFB_DIR variable was not exported; skipped gfx test building"
endif

################################################################################
# Supported targets.

gal_core:   $(GAL_CORE)
hal_user:   $(LIB_GAL)

gfx:        $(LIB_GFX)
gfx_test:   $(GFX_TEST)
hal_drv:    $(LIB_GAL) $(GAL_CORE)
hal_test:   $(HAL_TEST)
hal_unit:   $(HAL_UNIT_TEST)
hal_test_all:   $(HAL_TEST) $(HAL_UNIT_TEST)
chipinfo:   $(CHIPINFO)

oes11_drv:  $(LIB_OES11_LIST) $(GAL_CORE)
oes2x_drv:  $(LIB_OES2X_LIST) $(GAL_CORE)
gles11:     $(LIB_GLES11)
gles2x:     $(LIB_GLES2X)
libvivante: $(LIB_VIVANTE)

ifeq ($(USE_VDK), 0)
tutorial:   $(TUTORIAL)
else
vdk:        $(LIB_VDK)
vdktest:    $(VDK_TEST)
vv_launcher: $(VVLAUNCHER)
endif

egl:        $(LIB_EGL)

ifeq ($(USE_OPENCL), 1)
cl11_test:  $(CL11_TEST)
cl11_unit:  $(CL11_UNIT)
endif
ifneq ($(VIVANTE_NO_VG), 1)
vg11:       $(LIB_VG11)
ovg11_drv:  $(LIB_OVG11_LIST) $(GAL_CORE)
ovg11_tst:  $(APP_OVG11_LIST)

tiger:      $(TIGER)
endif

egl_only:
	@test ! -d $(EGL_DIR) || $(MAKE) --directory $(EGL_DIR) $(V_TARGET) STATIC_LINK=$(STATIC_LINK)

es11_only:
	@test ! -d $(GLES11_DIR) || $(MAKE) --directory $(GLES11_DIR) $(V_TARGET) STATIC_LINK=$(STATIC_LINK)

es20_only:
	@test ! -d $(GLES2X_DIR) || $(MAKE) --directory $(GLES2X_DIR) $(V_TARGET) STATIC_LINK=$(STATIC_LINK)

ifneq ($(VIVANTE_NO_VG), 1)
vg11_only:
	@test ! -d $(VG11_DIR) || $(MAKE) --directory $(VG11_DIR) $(V_TARGET) STATIC_LINK=$(STATIC_LINK)
endif

