##############################################################################
# Build global options
# NOTE: Can be overridden externally.
#

# Compiler options here.
ifeq ($(USE_OPT),)
  USE_OPT = -Os -ggdb -fomit-frame-pointer -falign-functions=4
  #USE_OPT = -Og -ggdb -falign-functions=4
endif

# C specific options here (added to USE_OPT).
ifeq ($(USE_COPT),)
  USE_COPT =
endif

# C++ specific options here (added to USE_OPT).
ifeq ($(USE_CPPOPT),)
  USE_CPPOPT = -fno-rtti
endif

# Enable this if you want the linker to remove unused code and data
ifeq ($(USE_LINK_GC),)
  USE_LINK_GC = yes
endif

# Linker extra options here.
ifeq ($(USE_LDOPT),)
  USE_LDOPT =
endif

# Enable this if you want link time optimizations (LTO)
ifeq ($(USE_LTO),)
  USE_LTO = no
endif

# Enable this if you want to see the full log while compiling.
ifeq ($(USE_VERBOSE_COMPILE),)
  USE_VERBOSE_COMPILE = no
endif
ifeq ($(V),1)
  USE_VERBOSE_COMPILE = yes
endif

# If enabled, this option makes the build process faster by not compiling
# modules not used in the current configuration.
ifeq ($(USE_SMART_BUILD),)
  USE_SMART_BUILD = yes
endif

#
# Build global options
##############################################################################

##############################################################################
# Architecture or project specific options
#

# don't enable EX9 or IFC in sflash version
ifeq ($(USE_EX9),)
  USE_EX9 = no
endif
ifeq ($(USE_IFC),)
  USE_IFC = no
endif

# Stack size to be allocated to the Cortex-M process stack. This stack is
# the stack used by the main() thread.
ifeq ($(USE_PROCESS_STACKSIZE),)
  USE_PROCESS_STACKSIZE = 0x800
endif

# Stack size to the allocated to the Cortex-M main/exceptions stack. This
# stack is used for processing interrupts and exceptions.
ifeq ($(USE_EXCEPTIONS_STACKSIZE),)
  USE_EXCEPTIONS_STACKSIZE = 0x800
endif

# Enables the use of FPU on Cortex-M4 (no, softfp, hard).
ifeq ($(USE_FPU),)
  USE_FPU = no
endif

# ESP8266: wifi
ifeq ($(MCU_NAME),)
  MCU_NAME = ESP8266
endif
mcu_suffix = $(shell echo $(MCU_NAME) | tr A-Z a-z)

# OS type
OS_TYPE = alios

#
# Architecture or project specific options
##############################################################################

##############################################################################
# Project, sources and paths
#

# Define project name here
PROJECT = $(mcu_suffix)_$(OS_TYPE)_ll

# Imported source files and paths
MIE = ../..
include $(MIE)/ext/alios/alios.mk
#include $(MIE)/ext/ESP8266_RTOS_SDK/esp8266.mk
include $(MIE)/porting/port.mk
include $(MIE)/nimble/drivers/bsp.mk
include $(MIE)/nimble/controller/ll.mk
include $(MIE)/nimble/transport/hci.mk
#include $(MIE)/nimble/host/hs.mk
#include $(MIE)/ext/tinycrypt/crypto.mk

# Define linker script file here
LDSCRIPT= $(MIE)/nimble/drivers/esp8266_bsp/ld/esp8266_eagle_v6_app1.ld

# C sources
ifneq ($(LDSCRIPT),)
HAL_SOURCES += $(MIE)/ext/alios/platform/arch/xtensa/lx106/port.c \
               $(rhino_SOURCES) \
               $(AOS_HAL_SOURCES)
endif
CSRC = $(HAL_SOURCES) \
       $(PORT_SOURCES) \
       $(LL_SOURCES) \
       $(HCI_SOURCES) \
       $(HOST_SOURCES) \
       $(MESH_SOURCES) \
       $(CRYPTO_SOURCES) \
       main.c

# C++ sources
CPPSRC =

ifeq ($(LDSCRIPT),)
# Don't compile startup files into .a library.
ASMSRC =
else
# List ASM source files here: compile the .S in aos rather than in bsp.
ASMSRC = $(STARTUPASM)
endif

INCDIR = $(ESP8266_INCLUDES) \
         $(HAL_INCLUDES) \
         $(MIE)/ext/alios/platform/arch/xtensa/lx106 \
         $(MIE)/ext/alios/platform/arch/xtensa/lx106/include \
         $(MIE)/ext/alios/platform/arch/xtensa/lx106/include/frxt \
         $(MIE)/ext/alios/platform/mcu/esp8266/bsp/include \
         $(MIE)/ext/alios/platform/mcu/esp8266/bsp/include/espressif \
         $(MIE)/ext/alios/platform/mcu/esp8266/bsp/include/espressif/esp8266 \
         $(rhino_INCLUDES) \
         $(PORT_INCLUDES) \
         $(LL_INCLUDES) \
         $(HCI_INCLUDES) \
         $(HOST_INCLUDES) \
         $(MESH_INCLUDES) \
         $(CRYPTO_INCLUDES)

#
# Project, sources and paths
##############################################################################

##############################################################################
# Compiler settings
#

MCU  = xtensa

TRGT = xtensa-lx106-elf-
CC   = $(TRGT)gcc
CPPC = $(TRGT)g++
# Enable loading with g++ only if you need C++ runtime support.
# NOTE: You can use C++ even without C++ support if you are careful. C++
#       runtime support makes code size explode.
LD   = $(TRGT)gcc
#LD   = $(TRGT)g++
CP   = $(TRGT)objcopy
AS   = $(TRGT)gcc -x assembler-with-cpp
AR   = $(TRGT)ar
OD   = $(TRGT)objdump
SZ   = $(TRGT)size
HEX  = $(CP) -O ihex
BIN  = $(CP) -O binary

# ARM-specific options here
AOPT =

# Define C warning options here: -std=c99
CWARN = -Wall -Wno-unused-parameter -Wno-unused-function #-Wextra -Wundef -Wstrict-prototypes

# Define C++ warning options here
CPPWARN = -Wall -Wextra -Wundef

#
# Compiler settings
##############################################################################

##############################################################################
# Start of user section
#

# List all user C define here, like -D_DEBUG=1
UDEFS = -DNDEBUG -DXCHAL_HAVE_XEA2 -DESPOS_FOR_ESP8266 -DWITH_LWIP -D$(MCU_NAME) -DNIMBLE_CFG_HOST=0 -DNIMBLE_CFG_CONTROLLER=1 -DEXE_NDA_FEAT_ENC=1 -DMIBLE_CFG_HCIoUART
ifeq ($(LDSCRIPT),)
UDEFS += -DMIBLE_BUILD_LIBRARY
endif

# Define ASM defines here
UADEFS = -D$(MCU_NAME)

# List all user directories here
UINCDIR =

# List the user directory to look for the libraries here
ULIBDIR = $(MIE)/ext/alios/platform/mcu/esp8266/bsp/lib

# List all user libraries here
# core:g_esp_ticks_per_us
# main:_xt_int_exit, user_start
# hal:xthal_set_intset
# pp:wDev_ProcessFiq
ULIBS = -lmain -lcirom -lnet80211 -lpp -lwpa -lphy -lgcc -llwip -lcirom -lcrypto -lnet80211 -lhal

#
# End of user defines
##############################################################################

RULESPATH = $(MIE)/porting/toolchain/gcc
include $(RULESPATH)/rules.mk

##############################################################################
# MISRA check rule, requires PCLint and the setup files, not provided.
#
misra:
	@lint-nt -v -w3 $(DEFS) pclint/co-gcc.lnt pclint/au-misra3.lnt pclint/waivers.lnt $(IINCDIR) $(CSRC) &> misra.txt

link:
	(cd ../../ext/ESP8266_RTOS_SDK; lndir /opt/huntersun/source/ESP8266_RTOS_SDK >& /dev/null)
	(cd ../../ext/alios; ln -sf ../../../../../../work.hs6601c/trunk/AliOS-Things/kernel; ln -sf ../../../../../../work.hs6601c/trunk/AliOS-Things/platform; ln -sf ../../../../../../work.hs6601c/trunk/AliOS-Things/osal; ln -sf ../../../../../../work.hs6601c/trunk/AliOS-Things/utility; ln -sf ../../../../../../work.hs6601c/trunk/AliOS-Things/tools)
