#
# Developer makefile to build hostpad and wpa_supplicant for both
# android and linux.
#
# Author: Sreenath S
# Contact: hnd-blr-swapps-sup-list, hnd-software-scm-list
#
# $Copyright Broadcom Corporation$
#
# $Id$
#

SHELL := /bin/bash

# Print usage with error.
define usage_help
	$(info ${\n})
	$(info Linux   : make PLAT=<platform> OPT=<opt>)
	$(info Android : make PLAT=<platform> OPT=<opt> FRAMEWORK_DIR=<android/source/path> FRAMEWORK_TARGET=<lunch_target> FRAMEWORK_PRODUCT=<product_name>)
	$(info ${\n\n})
	$(info Supported PLAT : $1)
	$(info Supported OPT  : $2)
	$(info ${\n\n})
	$(error $3)
endef

# Location of developer makefile.
BASE_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))

# Use temp dir for local builds.
WORK_DIR := $(or $(TMPWORKDIR),$(TMPDIR),/tmp)/$(LOGNAME)_$(PLAT)

# Collect valid platforms and opts by scanning opt makefile folder.
SUPPORTED_PLATFORMS := $(shell ls $(BASE_DIR)/opt_mk | awk -F [._] '{ print $$1 }' | awk '!a[$$0]++')
SUPPORTED_OPTS := $(shell ls $(BASE_DIR)/opt_mk | awk -F [._] '{ print $$2 }' | awk '!a[$$0]++')

# Error if PLAT or OPT are undefined.
ifndef PLAT
    $(call usage_help,$(SUPPORTED_PLATFORMS),$(SUPPORTED_OPTS),Missing argument 'PLAT')
else ifndef OPT
    $(call usage_help,$(SUPPORTED_PLATFORMS),$(SUPPORTED_OPTS),Missing argument 'OPT')
endif

# Error if PLAT or OPT are unsupported ones.
ifeq (,$(filter $(PLAT),$(SUPPORTED_PLATFORMS)))
    $(call usage_help,$(SUPPORTED_PLATFORMS),$(SUPPORTED_OPTS),Unsupported argument 'PLAT=$(PLAT)')
else ifeq (,$(filter $(OPT),$(SUPPORTED_OPTS)))
    $(call usage_help,$(SUPPORTED_PLATFORMS),$(SUPPORTED_OPTS),Unsupported argument 'OPT=$(OPT)')
endif

# Collect platform and opt specific configurations.
include $(BASE_DIR)/opt_mk/$(PLAT)_$(OPT).mk

# Sanity check to make sure that needed variables are present
include $(BASE_DIR)/hostap_check.mk

# Define rest of the variables.
SVN_BASE :=http://iot-svn.aus.cypress.com/svn/wlansvn
TOP_DIR := $(abspath $(BASE_DIR)/../)
HOSTAP_DIR := $(notdir $(TOP_DIR))
BUILD_DIR := $(or $(TMPBUILDDIR),$(WORK_DIR)/build/$(OPT))
FRAMEWORK_OUTDIR := $(FRAMEWORK_DIR)/out/target/product/$(strip $(FRAMEWORK_PRODUCT))
TOOLS_BIN := /projects/hnd/tools/bin

exclude_general := *.o *.d .svn bcmbuild
ifeq ($(PLAT),commercial)
exclude_base := $(exclude_general) .config* wext.config android_*.config .gitlab-ci.yml iapp.* brcm_vendor.h wireless_copy.h
exclude_base := $(exclude_base) supp_wapiap_interface.c wapiap_interface.c Makefile.cy libbcmdhd wapilib_softap wapilib wapi brcm_supp hidl
else
exclude_base := $(exclude_general) wapilib_softap wapilib wapi brcm_supp .gitlab-ci.yml .gitignore .git
endif
exclude_base-wapi := $(exclude_general) brcm_supp
exclude_all := $(exclude_general)

EXCLUDE_FLAGS := $(foreach patt,$(exclude_$(OPT)),--exclude='$(patt)')

mogrify_commercial_base_on  := NONE
mogrify_commercial_base_off := CONFIG_DRIVER_NL80211_IFX

mogrify_linux_base_on := BRCM_CONTRIBUTE_COPYRIGHT CONFIG_DRIVER_NL80211_IFX
mogrify_linux_base_off := ICS CONFIG_BRCM_TBD BRCM_FEATURES_COPYRIGHT WAPI WAPI_AP \
    BRCM_CCX BCM_SDO BCM_MAP_SDCMDS_2_SDOCMDS BCM_GENL \
    CONFIG_P2P_NFC BRCM_DRV_ROAM CONFIG_BRCM_WFDS CONFIG_WIFI_DISPLAY_NFC_BCM \
    BRCM_DEBUG CONFIG_P2P_HACK_POST38 CONFIG_WPS_NFC_BCM \
    CONFIG_BRCM_SD_DYNAMIC_CAPA CONFIG_BRCM_BEST_CHANNEL CONFIG_IP_ALLOC
#CONFIG_BRCM_HS20

mogrify_linux_base-wapi_on  := BRCM_CONTRIBUTE_COPYRIGHT BRCM_FEATURES_COPYRIGHT CONFIG_DRIVER_NL80211_IFX
mogrify_linux_base-wapi_off := ICS CONFIG_BRCM_TBD BRCM_VE BRCM_CCX BRCM_OKC \
    CONFIG_BRCM_WFDS CONFIG_WIFI_DISPLAY_NFC_BCM CONFIG_P2P_HACK_POST38 CONFIG_BRCM_HS20

mogrify_linux_all_on  := BRCM_CONTRIBUTE_COPYRIGHT BRCM_FEATURES_COPYRIGHT CONFIG_DRIVER_NL80211_IFX
mogrify_linux_all_off := ICS CONFIG_BRCM_TBD

mogrify_android_base_on  := BRCM_CONTRIBUTE_COPYRIGHT CONFIG_DRIVER_NL80211_IFX
mogrify_android_base_off := ICS CONFIG_BRCM_TBD BRCM_FEATURES_COPYRIGHT WAPI WAPI_AP BRCM_VE \
    BRCM_CCX BRCM_OKC BCM_SDO BCM_MAP_SDCMDS_2_SDOCMDS BCM_GENL \
    CONFIG_P2P_NFC BRCM_DRV_ROAM CONFIG_BRCM_WFDS CONFIG_WIFI_DISPLAY_NFC_BCM \
    BRCM_DEBUG CONFIG_P2P_HACK_POST38 CONFIG_WPS_NFC_BCM \
    CONFIG_BRCM_SD_DYNAMIC_CAPA CONFIG_BRCM_BEST_CHANNEL CONFIG_IP_ALLOC \
    BCM_LINUX_BUILD
#CONFIG_BRCM_HS20


mogrify_android_base-wapi_on  := BRCM_CONTRIBUTE_COPYRIGHT BRCM_FEATURES_COPYRIGHT CONFIG_DRIVER_NL80211_IFX
mogrify_android_base-wapi_off := ICS CONFIG_BRCM_TBD BCM_LINUX_BUILD BRCM_VE \
    BRCM_CCX BRCM_OKC CONFIG_BRCM_WFDS CONFIG_WIFI_DISPLAY_NFC_BCM CONFIG_P2P_HACK_POST38 \
    CONFIG_BRCM_HS20

mogrify_android_all_on  := BRCM_CONTRIBUTE_COPYRIGHT BRCM_FEATURES_COPYRIGHT CONFIG_DRIVER_NL80211_IFX
mogrify_android_all_off := ICS CONFIG_BRCM_TBD BCM_LINUX_BUILD

MOGRIFY_FLAGS := --define $(strip $(sort $(mogrify_$(PLAT)_$(OPT)_on)))
MOGRIFY_FLAGS += --undefine $(strip $(sort $(mogrify_$(PLAT)_$(OPT)_off)))

ifneq ($(OPT),base)
    WAPILIB_DIR := $(BUILD_DIR)/$(HOSTAP_DIR)/wapilib/src
endif
#ifeq ($(OPT),all)
#    HS20_DIR = $(BUILD_DIR)/$(HOSTAP_DIR)/hs20/client
#    LIBCURL = $(wildcard $(FRAMEWORK_OUTDIR)/system/lib/libcurl.so)
#endif

.PHONY: all
all: build_$(PLAT)

# Dependency based rules for folder and link creation.
$(BUILD_DIR):
	mkdir -p $@

$(BUILD_DIR)/$(HOSTAP_DIR): | $(BUILD_DIR)
	mkdir $@

$(BUILD_DIR)/$(HOSTAP_DIR)/hostapd: | $(BUILD_DIR)/$(HOSTAP_DIR)
	mkdir $@

$(BUILD_DIR)/$(HOSTAP_DIR)/wpa_supplicant: | $(BUILD_DIR)/$(HOSTAP_DIR)
	mkdir $@

$(BUILD_DIR)/$(HOSTAP_DIR)/hostapd/src: | $(BUILD_DIR)/$(HOSTAP_DIR)/hostapd
	ln -sfn -t $(BUILD_DIR)/$(HOSTAP_DIR)/hostapd ../src

$(BUILD_DIR)/$(HOSTAP_DIR)/wpa_supplicant/src: | $(BUILD_DIR)/$(HOSTAP_DIR)/wpa_supplicant
	ln -sfn -t $(BUILD_DIR)/$(HOSTAP_DIR)/wpa_supplicant ../src

ifneq ($(wildcard $(TOOLS_BIN)/xmogrify),)
    XMOGRIFY_SCRIPT := $(TOOLS_BIN)/xmogrify
    $(XMOGRIFY_SCRIPT): | $(BUILD_DIR)
else
    XMOGRIFY_SCRIPT := $(BUILD_DIR)/xmogrify
    XMOGRIFY_URL := $(SVN_BASE)/groups/software/infrastructure/GUB/bin/xmogrify
    # Checkout from svn if xmogrify is not present by default.
    $(XMOGRIFY_SCRIPT): | $(BUILD_DIR); svn export --quiet --force $(XMOGRIFY_URL) $@
endif

PHONY: mogrify
mogrify: | $(XMOGRIFY_SCRIPT)
	rsync -a $(EXCLUDE_FLAGS) $(TOP_DIR) $(BUILD_DIR)/
	cd $(BUILD_DIR)/$(HOSTAP_DIR)/hostapd && rsync -a $(HOSTAPD_OPT_CONFIG) $(HOSTAPD_BUILD_CONFIG)
	cd $(BUILD_DIR)/$(HOSTAP_DIR)/wpa_supplicant && rsync -a $(WPA_SUPPLICANT_OPT_CONFIG) $(WPA_SUPPLICANT_BUILD_CONFIG)
	$(XMOGRIFY_SCRIPT) --mogrifier LATEST -q $(MOGRIFY_FLAGS) -- $(BUILD_DIR)/$(HOSTAP_DIR)

# Forcefully create a new one or overwrite existing symbolic link between
# build folder and android external folder, to make sure that we are
# building the right code.
.PHONY: build_android
build_android: mogrify | $(BUILD_DIR)/$(HOSTAP_DIR)/hostapd/src $(BUILD_DIR)/$(HOSTAP_DIR)/wpa_supplicant/src
	$(strip ln -sfn $(BUILD_DIR)/$(HOSTAP_DIR) $(FRAMEWORK_DIR)/external/$(HOSTAP_DIR) && \
	cd $(FRAMEWORK_DIR) && source build/envsetup.sh && lunch $(FRAMEWORK_TARGET) && \
	mmm external/hostap/libbcmdhd && \
	$(if $(WAPILIB_DIR),mmm external/hostap/wapilib,:) && \
	mmm external/hostap/hostapd && \
	mmm external/hostap/wpa_supplicant)

#Below line has to be added after hs20 has been merged to 150 branch
#	$(if $(HS20_DIR),$(if $(LIBCURL),mmm -B $(FRAMEWORK_DIR)/external/$(HOSTAP_DIR)/hs20/client,:),:) &&
LINUX_BLDDIRS := \
    $(BUILD_DIR)/$(HOSTAP_DIR)/hostapd \
    $(BUILD_DIR)/$(HOSTAP_DIR)/wpa_supplicant \
    $(BUILD_DIR)/$(HOSTAP_DIR)/libbcmdhd

COMMERCIAL_BLDDIRS := \
    $(BUILD_DIR)/$(HOSTAP_DIR)/hostapd \
    $(BUILD_DIR)/$(HOSTAP_DIR)/wpa_supplicant

# Require subdirectories to build in a certain order.
ifeq ($(PLAT),commercial)
    $(BUILD_DIR)/$(HOSTAP_DIR)/wpa_supplicant/_: $(BUILD_DIR)/$(HOSTAP_DIR)/hostapd/_
else
ifeq ($(OPT),base)
    $(BUILD_DIR)/$(HOSTAP_DIR)/hostapd/_: $(BUILD_DIR)/$(HOSTAP_DIR)/libbcmdhd/_
    $(BUILD_DIR)/$(HOSTAP_DIR)/wpa_supplicant/_: $(BUILD_DIR)/$(HOSTAP_DIR)/hostapd/_
else
    LINUX_BLDDIRS += \
        $(BUILD_DIR)/$(HOSTAP_DIR)/wapilib/src

    $(BUILD_DIR)/$(HOSTAP_DIR)/wapilib/src/_: $(BUILD_DIR)/$(HOSTAP_DIR)/libbcmdhd/_
    $(BUILD_DIR)/$(HOSTAP_DIR)/hostapd/_: $(BUILD_DIR)/$(HOSTAP_DIR)/wapilib/src/_
    $(BUILD_DIR)/$(HOSTAP_DIR)/wpa_supplicant/_: $(BUILD_DIR)/$(HOSTAP_DIR)/hostapd/_
endif
endif

ifeq ($(PLAT),commercial)
PHONY_BLDDIRS = $(addsuffix /_,$(COMMERCIAL_BLDDIRS))
else
PHONY_BLDDIRS = $(addsuffix /_,$(LINUX_BLDDIRS))
endif
# Clean build is needed for hostapd and wpa_supplicant linux compilation.
.PHONY: $(PHONY_BLDDIRS)
$(PHONY_BLDDIRS): mogrify
	$(MAKE) -C $(patsubst %/_,%,$@) clean
	$(MAKE) -C $(patsubst %/_,%,$@) all

.PHONY: build_linux
build_linux: $(PHONY_BLDDIRS)

.PHONY: build_commercial
build_commercial: $(PHONY_BLDDIRS)

# Android makefile doesn't have any clean. So all dependencies should be
# cleaned manually.
.PHONY: clean
clean:
ifeq ($(PLAT),android)
	$(RM) -r $(FRAMEWORK_OUTDIR)/obj/EXECUTABLES/wpa_supplicant_intermediates
	$(RM) -r $(FRAMEWORK_OUTDIR)/obj/EXECUTABLES/wpa_cli_intermediates
#	$(RM) -r $(FRAMEWORK_OUTDIR)/obj/EXECUTABLES/hs20-osu-client_intermediates
	$(RM) -r $(FRAMEWORK_OUTDIR)/obj/SHARED_LIBRARIES/libwpa_client_intermediates
	$(RM) -r $(FRAMEWORK_OUTDIR)/obj/STATIC_LIBRARIES/lib_driver_cmd_bcmdhd_supplicant_intermediates
	$(RM) -r $(FRAMEWORK_OUTDIR)/obj/STATIC_LIBRARIES/lib_driver_cmd_bcmdhd_hostap_intermediates
	$(RM) -r $(FRAMEWORK_OUTDIR)/obj/STATIC_LIBRARIES/libwapi_intermediates
	$(RM) $(FRAMEWORK_OUTDIR)/system/bin/wpa_supplicant
	$(RM) $(FRAMEWORK_OUTDIR)/system/bin/wpa_cli
#	$(RM) $(FRAMEWORK_OUTDIR)/system/bin/hs20-osu-client
	$(RM) $(FRAMEWORK_OUTDIR)/system/lib/libwpa_client.so
else ifeq ($(PLAT),linux)
	for dir in $(wildcard $(LINUX_BLDDIRS)); do $(MAKE) -C $$dir -j1 clean; done
else ifeq ($(PLAT),commercial)
	for dir in $(wildcard $(LINUX_BLDDIRS)); do $(MAKE) -C $$dir -j1 clean; done
endif

.PHONY: clobber
clobber: clean
	$(RM) -r $(BUILD_DIR)
