################################################################################
# Makefile
################################################################################

################################################################################
# flavour specific arguments and macros
################################################################################
-include flavour.mk


################################################################################
# generic part of make file
################################################################################

PROJECT := ../../..

ifeq ($(OS),Windows_NT)
RM := C:\ARC\MetaWare\ide\utils\rm.exe -f
else
RM := rm -f
endif




DEPEND_DIR := depend
METAWARE_PATH := $(PROJECT)/firmware/builds/metaware
COMPILER_FLAGS := -tcf $(METAWARE_PATH)/arc.tcf -fstack-protector-strong -Hno_reorder_vars -Hnolib -Hhostlib= -Hnoxcheck -Hon=wait_for_license -Hnocopyr -Hdepend="$(DEPEND_DIR)" -Humake -Hon=each_function_in_own_section -DTX_INCLUDE_USER_DEFINE_FILE -fno-builtin -g -Hasmcpp -Os1 @includes.txt -Weverything \
    -Wno-cast-align \
    -Wno-cast-qual \
    -Wno-conversion \
    -Wno-date-time \
    -Wno-implicit-cast-widening \
    -Wno-missing-braces \
    -Wno-missing-variable-declarations \
    -Wno-padded \
    -Wno-reserved-id-macro \
    -Wno-switch-enum \
    -Wno-unused-macros

LINKER_FLAGS := -tcf $(METAWARE_PATH)/arc.tcf -Hldopt=-Cpage=0 -Hldopt=-xB -Hldopt=-xcbdlt -Hldopt="-xl 0M" -Hldopt=-q -Hldopt=-Cglobals -Hldopt=-Csections -Hldopt=-Csymbols -Hldopt=-Ctables -Hldopt=-Csize -Hldopt=-zpurgetext -Hnolib -Hhostlib= -Hnoxcheck -Hnocopyr -Hnocrt -Hldopt=-Coutput=$(OUT_NAME).map -o $(OUT_NAME).elf

USER_OBJS := $(METAWARE_PATH)/$(PRODUCT)/mem_def.lcf $(METAWARE_PATH)/$(PRODUCT)/mem_shared.lcf $(METAWARE_PATH)/$(OUT_NAME).lcf

# All of the sources participating in the build are defined here
-include sources.mk

C_OBJS := $(notdir $(C_SRCS))
C_OBJS := $(patsubst %.c,%.o,$(C_OBJS))

C_DEPS := $(notdir $(C_SRCS))
C_DEPS := $(patsubst %.c,$(DEPEND_DIR)/%.u,$(C_DEPS))

S_DEPS := $(notdir $(S_SRCS))
S_DEPS := $(patsubst %.s,$(DEPEND_DIR)/%.u,$(S_DEPS))

S_OBJS := $(notdir $(S_SRCS))
S_OBJS := $(patsubst %.s,%.o,$(S_OBJS))

#OBJS += $(S_OBJS)

-include subdir.mk

ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(CC_DEPS)),)
-include $(CC_DEPS)
endif
ifneq ($(strip $(C++_DEPS)),)
-include $(C++_DEPS)
endif
ifneq ($(strip $(CL_DEPS)),)
-include $(CL_DEPS)
endif
ifneq ($(strip $(CXX_DEPS)),)
-include $(CXX_DEPS)
endif
ifneq ($(strip $(C_UPPER_DEPS)),)
-include $(C_UPPER_DEPS)
endif
ifneq ($(strip $(ASM_DEPS)),)
-include $(ASM_DEPS)
endif
ifneq ($(strip $(S_DEPS)),)
-include $(S_DEPS)
endif
ifneq ($(strip $(MET_DEPS)),)
-include $(MET_DEPS)
endif
ifneq ($(strip $(CPP_DEPS)),)
-include $(CPP_DEPS)
endif
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
endif


# Add inputs and outputs from these tool invocations to the build variables 
MAPFILE := $(OUT_NAME).map

# All Target
all: $(OUT_NAME).elf

# Tool invocations
$(OUT_NAME).elf: $(C_OBJS) $(S_OBJS) $(USER_OBJS)
	@echo 'Building target: $@'
	@echo 'Invoking: MetaWare Linker for ARCompact'
	ccac @./argfile.txt $(LINKER_FLAGS) $(USER_OBJS) $(LIBS)
	@echo 'Finished building target: $@'
	@echo ' '

$(MAPFILE): $(OUT_NAME).elf $(C_OBJS) $(S_OBJS) $(USER_OBJS)

# Other Targets
clean:
	-$(RM) $(C_UPPER_DEPS) $(MAPFILE) $(S_DEPS) $(C_DEPS) 
	-$(RM) $(CC_DEPS) $(C++_DEPS) $(CL_DEPS) $(EXECUTABLES) $(CXX_DEPS) $(C_OBJS) 
	-$(RM) $(S_OBJS) $(ASM_DEPS) $(MET_DEPS) $(CPP_DEPS) 
	-$(RM) $(OUT_NAME).elf $(OUT_NAME).hex $(OUT_NAME).lst $(OUT_NAME).vlod
	-@echo ' '

.PHONY: all clean dependents
.SECONDARY:

