# *************************************************************************
# * Makefile for building CELENO-DK components.
# *************************************************************************
# *	(C) Copyright 2010, Celeno, Inc.
# *************************************************************************

# Read the configuration file. 
-include ./src/celeno.mk

ifeq ($(PLATFORM),PUMA5)
-include $(TARGET_HOME)/.config
-include $(TARGET_HOME)/TI_Build.make
build_filesystem_path=$(TI_filesystem_path)
endif

ifeq ($(BUILD_MODE),)
BUILD_MODE=operational
endif

#ifeq "$(strip $(TARGET))" ""
#TARGET = no_target
#endif

#--------------------------------------------------------------------------
# CELENO-DK Standard Definitions
#--------------------------------------------------------------------------

# Source directory where all the CELENO-DK source files are located.
CELENO_DK_SRC_DIR = $(build_inic_src_dir)/src

#--------------------------------------------------------------------------
# Select the CELENO-DK Components on the basis of the configuration
#--------------------------------------------------------------------------

CELENO_DK_COMPONENTS-$(CONFIG_CELENO_HOST_DRV) 	    += host_drv
CELENO_DK_COMPONENTS-$(CONFIG_CELENO_LIB_GENERAL)   += lib_generic
CELENO_DK_COMPONENTS-$(CONFIG_CELENO_FDL_UTIL) 	    += fdl
CELENO_DK_COMPONENTS-$(CONFIG_CELENO_CDL_UTIL) 	    += cdl
CELENO_DK_COMPONENTS-$(CONFIG_CELENO_NVRAM_UTILS)   += host_nvram
CELENO_DK_COMPONENTS-$(CONFIG_CELENO_IW_UTILS)      += wireless_tools
CELENO_DK_COMPONENTS-$(CONFIG_CELENO_802_1X_UTIL)   += 802.1x

CELENO_CLEAN_ALL_DK_COMPONENTS = host_drv lib_generic fdl cdl host_nvram 802.1x

ifeq ($(CONFIG_CELENO_IW_UTILS),y)
CELENO_CLEAN_ALL_DK_COMPONENTS += wireless_tools
endif

CELENO_DK_COMPONENTS_CLEAN     = $(foreach T,$(CELENO_CLEAN_ALL_DK_COMPONENTS),$(T)_clean)
CELENO_DK_COMPONENTS_DISTCLEAN = $(foreach T,$(CELENO_CLEAN_ALL_DK_COMPONENTS),$(T)_distclean)

#--------------------------------------------------------------------------
# CELENO-DK Targets that are supported.
#--------------------------------------------------------------------------


all: config build install

check_env:
	@if [ -z $(target_firmware_path) -o ! -d $(target_firmware_path) ] ; then \
		echo "Target FW path defined as: $(target_firmware_path)"; \
		echo "ERROR: you need to define a valid target path in celeno.config/celeno.mk !!!" ; \
		echo ""; \
		exit 1 ; \
	fi
	@if [ -z $(target_firmware) -o ! -f $(target_firmware) ] ; then \
		echo "Target FW binary defined as: $(target_firmware)"; \
		echo "ERROR: you need to define a valid firmware binary in celeno.config/celeno.mk !!!" ; \
		echo ""; \
		exit 1 ; \
	fi
	@if [ -z $(target_bootloader) -o ! -f $(target_bootloader) ] ; then \
		echo "Target FW boot binary defined as: $(target_bootloader)"; \
		echo "ERROR: you need to define a valid boot binary in celeno.config/celeno.mk !!!" ; \
		echo ""; \
		exit 1 ; \
	fi
	@if [ -z $(target_gen_eeprom) -o ! -f $(target_gen_eeprom) ] ; then \
		echo "Target Celeno General FW eeprom binary defined as: $(target_gen_eeprom)"; \
		echo "ERROR: you need to define a valid eeprom binary in celeno.config/celeno.mk !!!" ; \
		echo ""; \
		exit 1 ; \
	fi
ifeq ($(CONFIG_MULTIPLE_EEPROM_FILES_SUPPORT),y)
	@if [ -z $(target_pri_eeprom) -o ! -f $(target_pri_eeprom) ] ; then \
		echo "Target Primary driver FW eeprom binary defined as: $(target_pri_eeprom)"; \
		echo "ERROR: you need to define a valid eeprom binary in celeno.config/celeno.mk !!!" ; \
		echo ""; \
		exit 1 ; \
	fi
ifeq ($(CONFIG_CONCURRENT_INIC_SUPPORT),y)
	@if [ -z $(target_sec_eeprom) -o ! -f $(target_sec_eeprom) ] ; then \
		echo "Target Secondary driver FW eeprom binary defined as: $(target_sec_eeprom)"; \
		echo "ERROR: you need to define a valid eeprom binary in celeno.config/celeno.mk !!!" ; \
		echo ""; \
		exit 1 ; \
	fi
endif
endif
	@if [ -z $(build_inic_src_dir) -o ! -d $(build_inic_src_dir) ] ; then \
		echo "Source path defined as: $(build_inic_src_dir)"; \
		echo "ERROR: you need to define a valid source path in celeno.config/celeno.mk !!!" ; \
		echo ""; \
		exit 1 ; \
	fi
ifneq ($(LINUX_SRC),)
	@if [ ! -d $(LINUX_SRC) ] ; then \
		echo "Linux sources path defined as: $(LINUX_SRC)"; \
		echo "ERROR: you need to define a valid Linux sources path in celeno.config/celeno.mk !!!" ; \
		echo ""; \
		exit 1 ; \
	fi
endif
ifneq ($(CROSS_COMPILE),)
ifneq ($(PLATFORM),PUMA5) # CROSS_COMPILE is overriden
	@if [ ! -x "$(CROSS_COMPILE)gcc" ] ; then \
		echo "Cross-compiler path defined as: $(CROSS_COMPILE)"; \
		echo "ERROR: you need to define a valid cross-compiler path in celeno.config/celeno.mk !!!" ; \
		echo ""; \
		exit 1 ; \
	fi
endif
endif
#ifneq ($(PLATFORM),PC)
#ifneq ($(build_inic_install_dir),)
#	@if [ ! -d $(build_inic_install_dir) ] ; then \
#		echo "Config path defined as: $(build_inic_install_dir)"; \
#		echo "ERROR: you need to define a valid installation path in celeno.config/celeno.mk !!!" ; \
#		echo ""; \
#		exit 1 ; \
#	fi
#endif
#ifneq ($(build_inic_cfg_dir),)
#	@if [ ! -d $(build_inic_cfg_dir) ] ; then \
#		echo "Config path defined as: $(build_inic_cfg_dir)"; \
#		echo "ERROR: you need to define a valid config path in celeno.config/celeno.mk !!!" ; \
#		echo ""; \
#		exit 1 ; \
#	fi
#endif
#endif

config: check_env
	@echo
	@echo "************************************************************"
	@echo "************************************************************"
	@echo  celeno package environment setup
	@echo  ---------------------------------
	@echo  PACKAGE_VERSION=$(CONFIG_HOST_CL_VER)
	@echo  SDK_VERSION=$(CONFIG_SDK_CL_VER)
	@echo  PLATFORM=$(PLATFORM)
	@echo  TARGET=$(TARGET)
	@echo  BUS_TYPE=$(CONFIG_BUS_TYPE)
	@echo  PN=$(CONFIG_PN)
	@echo  TR_SEL=$(CONFIG_TR_SEL)
	@echo  BUILD_MODE=$(BUILD_MODE)
	@echo  CROSS_COMPILE=$(CROSS_COMPILE)
	@echo  LINUX_SRC=$(LINUX_SRC)
	@echo  ARCH=$(ARCH)
	@echo  CONCURRENT=$(CONFIG_CONCURRENT_INIC_SUPPORT)
	@echo  APCLI24G=$(CONFIG_APCLI_2_4_SUPPORT)
	@echo  RADIUS=$(CONFIG_CELENO_802_1X_UTIL)
	@echo  IW_UTILS=$(CONFIG_CELENO_IW_UTILS)
	@echo  DBG=$(CONFIG_ENABLE_DEBUG_PRINTS)
	@echo  DBG_LVL=$(CONFIG_DEBUG_PRINTS_LEVEL)
	@echo  EEPROM_FILE=$(CONFIG_USE_EEPROM_FILE)
	@echo  NVRAM_PLATFORM=$(CONFIG_NVRAM_PLATFORM)
	@echo  build_inic_src_dir=$(build_inic_src_dir)
	@echo  build_inic_install_dir=$(build_inic_install_dir)
	@echo  build_inic_cfg_dir=$(build_inic_cfg_dir)
	@echo  target_linux_binary=$(target_firmware)
	@echo  target_uboot_binary=$(target_bootloader)
	@echo  target_gen_eeprom_binary=$(target_gen_eeprom)
	@echo  MULTIPLE_EEPROM_FILES_SUPPORT=$(CONFIG_MULTIPLE_EEPROM_FILES_SUPPORT)
ifeq ($(CONFIG_MULTIPLE_EEPROM_FILES_SUPPORT),y)
	@echo  target_pri_eeprom_binary=$(target_pri_eeprom)
ifeq ($(CONFIG_CONCURRENT_INIC_SUPPORT),y)
	@echo  target_sec_eeprom_binary=$(target_sec_eeprom)
endif
endif
	@echo "************************************************************"
	@echo "************************************************************"
	@echo

build: check_env tools $(CELENO_DK_COMPONENTS-y)

install:
ifeq ($(PLATFORM),PC)
	@if [ -x /usr/bin/sudo ]; then sudo mkdir -p -m 777 $(build_inic_install_dir); else mkdir -p -m 777 $(build_inic_install_dir);fi;
endif
	@if [ -f $(target_firmware) ]; then ln -sf $(target_firmware) $(CELENO_DK_SRC_DIR)/firmware/iNIC_uImage;else echo "$(target_firmware) is missing";exit 1;fi;  
	@if [ -f $(target_bootloader) ]; then ln -sf $(target_bootloader) $(CELENO_DK_SRC_DIR)/firmware/iNIC_boot.bin;else echo "$(target_bootloader) is missing";exit 1;fi;  
ifeq ($(CONFIG_MULTIPLE_EEPROM_FILES_SUPPORT),y)
	@if [ -f $(target_gen_eeprom) ]; then ln -sf $(target_gen_eeprom) $(CELENO_DK_SRC_DIR)/firmware/iNIC_gen_eeprom.bin;else echo "$(target_gen_eeprom) is missing";exit 1;fi;  
	@if [ -f $(target_pri_eeprom) ]; then ln -sf $(target_pri_eeprom) $(CELENO_DK_SRC_DIR)/firmware/iNIC_pri_eeprom.bin;else echo "$(target_pri_eeprom) is missing";exit 1;fi;  
ifeq ($(CONFIG_CONCURRENT_INIC_SUPPORT),y)
	@if [ -f $(target_sec_eeprom) ]; then ln -sf $(target_sec_eeprom) $(CELENO_DK_SRC_DIR)/firmware/iNIC_sec_eeprom.bin;else echo "$(target_sec_eeprom) is missing";exit 1;fi;  
endif
else
	@if [ -f $(target_gen_eeprom) ]; then ln -sf $(target_gen_eeprom) $(CELENO_DK_SRC_DIR)/firmware/iNIC_eeprom.bin;else echo "$(target_gen_eeprom) is missing";exit 1;fi;  
endif
	@$(CELENO_DK_SRC_DIR)/scripts/pack_inic_image.sh

tools: local_host_nvram

tools_clean: local_host_nvram_distclean

clean: $(CELENO_DK_COMPONENTS_CLEAN)

distclean: $(CELENO_DK_COMPONENTS_DISTCLEAN) tools_clean
	rm -Rf $(build_inic_src_dir)/src/scripts/env.sh
	rm -f $(build_inic_src_dir)/src/scripts/inic_cmdr.tar.gz
	rm -f $(build_inic_src_dir)/src/firmware/iNIC_*
	rm -Rf $(build_inic_src_dir)/src/utils/host_nvram/builddir
	rm -Rf $(build_inic_src_dir)/src/kernel/host_drv/.tmp_versions

package: check_env
	$(MAKE) -C $(build_inic_src_dir) distclean
ifeq ($(PLATFORM),PUMA5)
	$(TARGET_HOME)/tools/scripts/mkpkg
else ifeq ($(PLATFORM),PC)
	$(build_inic_src_dir)/src/scripts/make_celeno_package.sh
endif

release: check_env
	$(MAKE) -C $(build_inic_src_dir) distclean
	$(build_inic_src_dir)/src/scripts/make_celeno_package.sh formal

dummy: 
#--------------------------------------------------------------------------
# Individual Rule for building each of the CELENO-DK Components.
#--------------------------------------------------------------------------
	
# **************************
# CELENO Host Kernel Module
# **************************

host_drv:
	@echo "*--------------------------------------------------"
	@echo "*  Building: $@                                    "
	@echo "*--------------------------------------------------"
	$(MAKE) -C $(CELENO_DK_SRC_DIR)/kernel/host_drv -f Makefile.flat 

host_drv_clean:
	@echo "*--------------------------------------------------"
	@echo "*  Cleaning: $@                                    "
	@echo "*--------------------------------------------------"
	$(MAKE) -C $(CELENO_DK_SRC_DIR)/kernel/host_drv -f Makefile.flat clean

host_drv_distclean: host_drv_clean
	@echo "*--------------------------------------------------"
	@echo "*  Dist Cleaning: $@                               "
	@echo "*--------------------------------------------------"
	rm -Rf $(build_inic_install_dir)/*

## *****************
# CELENO Host Utils
# # ****************

## ------------------------------
# CELENO general library 
## ------------------------------

lib_generic:
	@echo "*--------------------------------------------------"
	@echo "*  Building: $@                                    "
	@echo "*--------------------------------------------------"
	$(MAKE) -C $(CELENO_DK_SRC_DIR)/utils/lib_generic -f Makefile.flat 

lib_generic_clean:
	@echo "*--------------------------------------------------"
	@echo "*  Cleaning: $@                                    "
	@echo "*--------------------------------------------------"
	$(MAKE) -C $(CELENO_DK_SRC_DIR)/utils/lib_generic -f Makefile.flat clean

lib_generic_distclean: lib_generic_clean
	@echo "*--------------------------------------------------"
	@echo "*  Dist Cleaning: $@                               "
	@echo "*--------------------------------------------------"
	rm -Rf $(build_inic_install_dir)/*

## ----------------------------
# CELENO firmware download 
## ----------------------------

fdl:
	@echo "*--------------------------------------------------"
	@echo "*  Building: $@                                    "
	@echo "*--------------------------------------------------"
	$(MAKE) -C $(CELENO_DK_SRC_DIR)/utils/fdl -f Makefile.flat 

fdl_clean:
	@echo "*--------------------------------------------------"
	@echo "*  Cleaning: $@                                    "
	@echo "*--------------------------------------------------"
	$(MAKE) -C $(CELENO_DK_SRC_DIR)/utils/fdl -f Makefile.flat clean

fdl_distclean: fdl_clean
	@echo "*--------------------------------------------------"
	@echo "*  Dist Cleaning: $@                               "
	@echo "*--------------------------------------------------"
	rm -Rf $(build_inic_install_dir)/*

## ------------------------------
# CELENO configuration download 
## ------------------------------

cdl:
	@echo "*--------------------------------------------------"
	@echo "*  Building: $@                                    "
	@echo "*--------------------------------------------------"
	$(MAKE) -C $(CELENO_DK_SRC_DIR)/utils/cdl -f Makefile.flat 

cdl_clean:
	@echo "*--------------------------------------------------"
	@echo "*  Cleaning: $@                                    "
	@echo "*--------------------------------------------------"
	$(MAKE) -C $(CELENO_DK_SRC_DIR)/utils/cdl -f Makefile.flat clean

cdl_distclean: cdl_clean
	@echo "*--------------------------------------------------"
	@echo "*  Dist Cleaning: $@                               "
	@echo "*--------------------------------------------------"
	rm -Rf $(build_inic_install_dir)/*

## -----------------------
# CELENO Host Nvram Utils
## -----------------------

host_nvram:
	@echo "*--------------------------------------------------"
	@echo "*  Building: $@                                    "
	@echo "*--------------------------------------------------"
	$(MAKE) -C $(CELENO_DK_SRC_DIR)/utils/host_nvram -f Makefile.flat 

host_nvram_clean:
	@echo "*--------------------------------------------------"
	@echo "*  Cleaning: $@                                    "
	@echo "*--------------------------------------------------"
	$(MAKE) -C $(CELENO_DK_SRC_DIR)/utils/host_nvram -f Makefile.flat clean

host_nvram_distclean: host_nvram_clean
	@echo "*--------------------------------------------------"
	@echo "*  Dist Cleaning: $@                               "
	@echo "*--------------------------------------------------"
	rm -Rf $(build_inic_install_dir)/*

## -------------------------------------------
# CELENO Host Nvram Utils - With Local Tools
## -------------------------------------------

local_host_nvram:
	@echo "*--------------------------------------------------"
	@echo "*  Building: $@                                    "
	@echo "*--------------------------------------------------"
	$(MAKE) -C $(CELENO_DK_SRC_DIR)/utils/host_nvram -f Makefile.flat USE_LOCAL_TOOLS=y 

local_host_nvram_clean:
	@echo "*--------------------------------------------------"
	@echo "*  Cleaning: $@                                    "
	@echo "*--------------------------------------------------"
	$(MAKE) -C $(CELENO_DK_SRC_DIR)/utils/host_nvram -f Makefile.flat clean USE_LOCAL_TOOLS=y

local_host_nvram_distclean: local_host_nvram_clean
	@echo "*--------------------------------------------------"
	@echo "*  Dist Cleaning: $@                               "
	@echo "*--------------------------------------------------"
	rm -Rf $(build_inic_src_dir)/tools/*


# **************
# Wireless-Tools
# **************

wireless_tools:
	@echo "*--------------------------------------------------"
	@echo "*  Building: $@                                    "
	@echo "*--------------------------------------------------"
	$(MAKE) -C $(CELENO_DK_SRC_DIR)/utils/wireless_tools -f Makefile.flat 

wireless_tools_clean:
	@echo "*--------------------------------------------------"
	@echo "*  Cleaning: $@                                    "
	@echo "*--------------------------------------------------"
	$(MAKE) -C $(CELENO_DK_SRC_DIR)/utils/wireless_tools -f Makefile.flat clean

wireless_tools_distclean: wireless_tools_clean
	@echo "*--------------------------------------------------"
	@echo "*  Dist Cleaning: $@                               "
	@echo "*--------------------------------------------------"
	$(MAKE) -C $(CELENO_DK_SRC_DIR)/utils/wireless_tools -f Makefile.flat distclean

## -----------------------
# Ralink 802.1x authenticator daemon
## -----------------------

802.1x:
	@echo "*--------------------------------------------------"
	@echo "*  Building: $@                                    "
	@echo "*--------------------------------------------------"
	$(MAKE) -C $(CELENO_DK_SRC_DIR)/utils/802.1x

802.1x_clean:
	@echo "*--------------------------------------------------"
	@echo "*  Cleaning: $@                                    "
	@echo "*--------------------------------------------------"
	$(MAKE) -C $(CELENO_DK_SRC_DIR)/utils/802.1x clean

802.1x_distclean: 802.1x_clean
	@echo "*--------------------------------------------------"
	@echo "*  Dist Cleaning: $@                               "
	@echo "*--------------------------------------------------"
	rm -Rf $(build_inic_install_dir)/*

