# In order to compile your application under cygwin
# you might need to define NDK_USE_CYGPATH=1 before calling the ndk-build

USER_LOCAL_PATH:=$(LOCAL_PATH)

USER_LOCAL_C_INCLUDES:=$(LOCAL_C_INCLUDES)
USER_LOCAL_CFLAGS:=$(LOCAL_CFLAGS)
USER_LOCAL_STATIC_LIBRARIES:=$(LOCAL_STATIC_LIBRARIES)
USER_LOCAL_SHARED_LIBRARIES:=$(LOCAL_SHARED_LIBRARIES)
USER_LOCAL_LDLIBS:=$(LOCAL_LDLIBS)

LOCAL_PATH:=$(subst ?,,$(firstword ?$(subst \, ,$(subst /, ,$(call my-dir)))))

VISP_TARGET_ARCH_ABI:=$(TARGET_ARCH_ABI)
VISP_THIS_DIR:=$(patsubst $(LOCAL_PATH)\\%,%,$(patsubst $(LOCAL_PATH)/%,%,$(call my-dir)))
VISP_MK_DIR:=$(dir $(lastword $(MAKEFILE_LIST)))
VISP_LIBS_DIR:=@VISP_LIBS_DIR_CONFIGCMAKE@
VISP_3RDPARTY_LIBS_DIR:=@VISP_3RDPARTY_LIBS_DIR_CONFIGCMAKE@
VISP_BASEDIR:=@VISP_BASE_INCLUDE_DIR_CONFIGCMAKE@
VISP_LOCAL_C_INCLUDES:=@VISP_INCLUDE_DIRS_CONFIGCMAKE@
VISP_MODULES:=@VISP_MODULES_CONFIGMAKE@
VISP_SUB_MK:=$(call my-dir)/VISP-$(TARGET_ARCH_ABI).mk

ifeq ($(VISP_LIB_TYPE),)
    VISP_LIB_TYPE:=@VISP_LIBTYPE_CONFIGMAKE@
endif

ifeq ($(VISP_LIB_TYPE),SHARED)
    VISP_LIBS:=@VISP_LIBS_CONFIGMAKE@
    VISP_LIB_TYPE:=@VISP_LIBTYPE_CONFIGMAKE@
else
    VISP_LIBS:=$(VISP_MODULES)
    VISP_LIB_TYPE:=@VISP_STATIC_LIBTYPE_CONFIGMAKE@
endif

ifeq ($(VISP_LIB_TYPE),SHARED)
    VISP_3RDPARTY_COMPONENTS:=
    VISP_EXTRA_COMPONENTS:=
else
    include $(VISP_SUB_MK)
endif

ifeq ($(VISP_LIB_TYPE),SHARED)
    VISP_LIB_SUFFIX:=so
else
    VISP_LIB_SUFFIX:=a
    VISP_INSTALL_MODULES:=on
endif

define add_visp_module
    include $(CLEAR_VARS)
    LOCAL_MODULE:=visp_$1
    LOCAL_SRC_FILES:=$(VISP_LIBS_DIR)/libvisp_$1.$(VISP_LIB_SUFFIX)
    include $(PREBUILT_$(VISP_LIB_TYPE)_LIBRARY)
endef

define add_visp_3rdparty_component
    include $(CLEAR_VARS)
    LOCAL_MODULE:=$1
    LOCAL_SRC_FILES:=$(VISP_3RDPARTY_LIBS_DIR)/lib$1.a
    include $(PREBUILT_STATIC_LIBRARY)
endef

ifeq ($(VISP_MK_$(VISP_TARGET_ARCH_ABI)_ALREADY_INCLUDED),)
    ifeq ($(VISP_INSTALL_MODULES),on)
        $(foreach module,$(VISP_LIBS),$(eval $(call add_visp_module,$(module))))
    endif

    $(foreach module,$(VISP_3RDPARTY_COMPONENTS),$(eval $(call add_visp_3rdparty_component,$(module))))

    ifneq ($(VISP_BASEDIR),)
        VISP_LOCAL_C_INCLUDES += $(foreach mod, $(VISP_MODULES), $(VISP_BASEDIR)/modules/$(mod)/include)
    endif

    #turn off module installation to prevent their redefinition
    VISP_MK_$(VISP_TARGET_ARCH_ABI)_ALREADY_INCLUDED:=on
endif

ifeq ($(VISP_LOCAL_CFLAGS),)
    VISP_LOCAL_CFLAGS := -fPIC -DANDROID -fsigned-char
endif

include $(CLEAR_VARS)

LOCAL_C_INCLUDES:=$(USER_LOCAL_C_INCLUDES)
LOCAL_CFLAGS:=$(USER_LOCAL_CFLAGS)
LOCAL_STATIC_LIBRARIES:=$(USER_LOCAL_STATIC_LIBRARIES)
LOCAL_SHARED_LIBRARIES:=$(USER_LOCAL_SHARED_LIBRARIES)
LOCAL_LDLIBS:=$(USER_LOCAL_LDLIBS)

LOCAL_C_INCLUDES += $(VISP_LOCAL_C_INCLUDES)
LOCAL_CFLAGS     += $(VISP_LOCAL_CFLAGS)

ifeq ($(VISP_INSTALL_MODULES),on)
    LOCAL_$(VISP_LIB_TYPE)_LIBRARIES += $(foreach mod, $(VISP_LIBS), visp_$(mod))
else
    $(call __ndk_info,VISP: You should ignore warning about 'non-system libraries in linker flags' and 'visp_java' library.)
    $(call __ndk_info,        'VISP_INSTALL_MODULES:=on' can be used to build APK with included VISP binaries)
    LOCAL_LDLIBS += -L$(call host-path,$(LOCAL_PATH)/$(VISP_LIBS_DIR)) $(foreach lib, $(VISP_LIBS), -lvisp_$(lib))
endif

ifeq ($(VISP_LIB_TYPE),STATIC)
    LOCAL_STATIC_LIBRARIES += $(VISP_3RDPARTY_COMPONENTS)
endif

LOCAL_LDLIBS += $(foreach lib,$(VISP_EXTRA_COMPONENTS), -l$(lib))

#restore the LOCAL_PATH
LOCAL_PATH:=$(USER_LOCAL_PATH)
