#
# Copyright (C) 2012-2016 The Paparazzi Team
#
# This file is part of paparazzi.
#
# paparazzi is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# paparazzi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with paparazzi; see the file COPYING.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#

#
# This is the common Makefile for target using chibios
#
# chibios directory
CHIBIOS = $(PAPARAZZI_SRC)/sw/ext/chibios

# directory with board defines for chibios platforms (board specific)
BOARD_DIR ?= $(BOARD)/chibios
CHIBIOS_BOARD_DIR = $(PAPARAZZI_SRC)/sw/airborne/boards/$(BOARD_DIR)

# chibos linker scripts directory
# to keep backwards compatibility with Luftboot (Lia/Lisa M) 
# and possibly other bootloaders (like PX4 bootloader)
# we need to keep a separate linker script with a memory offset
# because it is not possible to pass it dynamicaly to the linker
# hence we have two destinations for linker scripts
CHIBIOS_BOOTLOADER_SCRIPT =
# check if we used 1 to define presence of luftboot
ifeq ($(HAS_LUFTBOOT),1)
  CHIBIOS_BOOTLOADER_SCRIPT = 1
endif
# check if we used TRUE to define presence of luftboot
ifeq ($(HAS_LUFTBOOT),TRUE)
  CHIBIOS_BOOTLOADER_SCRIPT = 1
endif

ifdef CHIBIOS_BOOTLOADER_SCRIPT
# we use the modified linker script
CHIBIOS_LINKER_DIR ?= $(PAPARAZZI_SRC)/sw/airborne/arch/chibios/
else
# we use the original linker script
CHIBIOS_LINKER_DIR ?= $(STARTUPLD)
endif

# Launch with "make Q=''" to get full command display
Q=@

# Debugging options
RTOS_DEBUG ?= 0

ifeq (,$(findstring $(RTOS_DEBUG),0 FALSE))
$(info ChibiOS Debug mode is ON)
$(TARGET).CFLAGS += \
	-DCH_DBG_STATISTICS=TRUE \
	-DCH_DBG_SYSTEM_STATE_CHECK=TRUE \
	-DCH_DBG_ENABLE_CHECKS=TRUE \
	-DCH_DBG_ENABLE_ASSERTS=TRUE \
	-DCH_DBG_ENABLE_TRACE=TRUE \
	-DCH_DBG_ENABLE_STACK_CHECK=TRUE \
	-DCH_DBG_FILL_THREADS=TRUE \
	-DCH_DBG_THREADS_PROFILING=TRUE
else
$(TARGET).CFLAGS += -DCH_DBG_THREADS_PROFILING=TRUE
endif

#
# General rules
#
$(TARGET).srcsnd = $(notdir $($(TARGET).srcs))
$(TARGET).objso	= $($(TARGET).srcs:%.c=$(OBJDIR)/%.o)
$(TARGET).objs	= $($(TARGET).objso:%.S=$(OBJDIR)/%.o)


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

# Paparazzi options here.
ifeq ($(PPRZ_DEFINITION),)
  PPRZ_DEFINITION = -DUSE_CHIBIOS_RTOS -DUSE_ADC_WATCHDOG
endif

# Compiler options here.
ifeq ($(USE_OPT),)
  # ChibiOS already define OPT
ifeq (,$(findstring $(RTOS_DEBUG),0 FALSE))
  CH_OPT ?= 0 -g -ggdb3 -fno-inline
else
  CH_OPT ?= 2 -ggdb
endif
  USE_OPT = -std=gnu99 -O$(CH_OPT) \
	-falign-functions=16 -fomit-frame-pointer \
	-W -Wall \
	$(PPRZ_DEFINITION)
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
else
# Force LTO to 'no' if in debug mode
ifeq (,$(findstring $(RTOS_DEBUG),0 FALSE))
  USE_LTO = no
endif
endif

# If enabled, this option allows to compile the application in THUMB mode.
ifeq ($(USE_THUMB),)
  USE_THUMB = yes
endif

# Enable this if you want to see the full log while compiling.
ifeq ($(USE_VERBOSE_COMPILE),)
  ifeq ($(Q),@)
    USE_VERBOSE_COMPILE = no
  else
    USE_VERBOSE_COMPILE = yes
  endif
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 = no
endif

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

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

# 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 = 0x2000
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 = 0x400
endif

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

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

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

# Project name defined in board makefile

# Imported source files and paths
# Startup files.
include $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC/mk/$(CHIBIOS_BOARD_STARTUP)
# HAL-OSAL files (optional).
include $(CHIBIOS)/os/hal/hal.mk
include $(CHIBIOS)/os/hal/ports/STM32/$(CHIBIOS_BOARD_PLATFORM)
include $(CHIBIOS_BOARD_DIR)/board.mk
include $(CHIBIOS)/os/hal/osal/rt/osal.mk
# RTOS files (optional).
include $(CHIBIOS)/os/rt/rt.mk
include $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC/mk/port_v7m.mk
# Other files (optional).
#
ifeq ($(USE_FATFS), TRUE)
include $(PAPARAZZI_HOME)/conf/chibios/fatfs.mk
endif

# Define linker script file here
LDSCRIPT= $(CHIBIOS_LINKER_DIR)/$(CHIBIOS_BOARD_LINKER)

# C sources that can be compiled in ARM or THUMB mode depending on the global
# setting.
CSRC = $(STARTUPSRC) \
       $(KERNSRC) \
       $(PORTSRC) \
       $(OSALSRC) \
       $(HALSRC) \
       $(PLATFORMSRC) \
       $(BOARDSRC) \
       $(CHIBIOS)/os/various/syscalls.c \
       $(FATFSSRC) \
       $(CHIBIOS_BOARD_MAIN)

ECSRC = $($(TARGET).srcs)

# C++ sources that can be compiled in ARM or THUMB mode depending on the global
# setting.
CPPSRC =

# C sources to be compiled in ARM mode regardless of the global setting.
# NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler
#       option that results in lower performance and larger code size.
ACSRC =

# C++ sources to be compiled in ARM mode regardless of the global setting.
# NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler
#       option that results in lower performance and larger code size.
ACPPSRC =

# C sources to be compiled in THUMB mode regardless of the global setting.
# NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler
#       option that results in lower performance and larger code size.
TCSRC =

# C sources to be compiled in THUMB mode regardless of the global setting.
# NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler
#       option that results in lower performance and larger code size.
TCPPSRC =

# List ASM source files here
ASMXSRC = $(STARTUPASM) $(PORTASM) $(OSALASM)

INCDIR = $(CHIBIOS)/os/license $(STARTUPINC) $(KERNINC) $(PORTINC) $(OSALINC) \
         $(HALINC) $(PLATFORMINC) $(BOARDINC) $(FATFSINC) \
         $(CHIBIOS)/os/various $(CHIBIOS_BOARD_DIR) $(CHIBIOS_PROJECT_DIR)

# Output directory and files
BUILDDIR := $(AIRCRAFT_BUILD_DIR)/$(TARGET)

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

##############################################################################
# Compiler settings
#
MCU  ?= cortex-m4

TRGT = arm-none-eabi-
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 =

# THUMB-specific options here
TOPT = -mthumb -DTHUMB

# Define C warning options here
CWARN = -Wall -Wextra -Wstrict-prototypes

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

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

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

# List all user C define here, like -D_DEBUG=1
UDEFS = $($(TARGET).CFLAGS) $(USER_CFLAGS) $(BOARD_CFLAGS)

# Define ASM defines here
UADEFS = $($(TARGET).CFLAGS) $(USER_CFLAGS) $(BOARD_CFLAGS)

# List all user directories here
# remove -I before include dir because ChibiOS is adding them again
UINCDIR = $(CHIBIOS)/os/license $(patsubst -I%,%,$(INCLUDES))

# List the user directory to look for the libraries here
ULIBDIR =

# List all user libraries here
ULIBS = -lm $(BOARD_LDFLAGS)

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

#
# Include upload rules
##############################################################################
# default: assume the luftboot bootloader is used
# if luftboot is not used define NO_LUFTBOOT to a value != 0
# this is necessary for Makefile.stm32-upload
ifdef NO_LUFTBOOT
ASSUMING_LUFTBOOT = "no"
else
ASSUMING_LUFTBOOT = "yes"
endif


# Settings for GDB
GDB = $(shell which arm-none-eabi-gdb)


# Settings for OOCD
$(TARGET).OOCD_INTERFACE=ftdi/flossjtag
OOCD_INTERFACE=ftdi/flossjtag
OOCD = $(shell which openocd)

ifndef $(TARGET).OOCD_BOARD
OOCD_BOARD = lisa-m
else
OOCD_BOARD =  $($(TARGET).OOCD_BOARD)
endif

###############################################################################

RULESPATH = $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC
#include $(RULESPATH)/rules.mk

EXTRA_RULES_INCLUDE_PATH = $(PAPARAZZI_HOME)/conf/chibios/chibios_extra_rules.mk
include $(PAPARAZZI_HOME)/conf/chibios/chibios_rules.mk

###############################################################################
# Upload makefile
include $(PAPARAZZI_HOME)/conf/Makefile.stm32-upload
