# Herramientas utilizadas
TOOLCHAIN = arm-none-eabi-
CC = $(TOOLCHAIN)gcc
AS = $(TOOLCHAIN)gcc -x assembler-with-cpp
LD = $(TOOLCHAIN)gcc
OBJCP = $(TOOLCHAIN)objcopy
AR = $(TOOLCHAIN)ar

# nombre del archivo destino
TARGET = gpio

#nombre del linkerscript
LD_SCRIPT = ../cmsis/MKL46Z4/MKL46Z4.ld


CC_SYMBOLS = -DTOOLCHAIN_GCC_ARM -DNDEBUG

LIBS = -lm -lgcc -lc -lnosys

# directorios donde buscar archivos de cabecera 
INC_DIRS = ../cmsis ../cmsis/MKL46Z4 .
# app headers directories (remove comment and add more files)
#INC_DIRS +=

# directorios donde buscar archivos fuentes 
SRC_DIRS = ../cmsis ../cmsis/MKL46Z4 .
# app source directories (remove comment and add more files)
#SRC_DIRS +=


INC_DIRS_F = -I. $(patsubst %, -I%, $(INC_DIRS))
OBJ_FOLDER = build/

COMPILER_OPTIONS  = -g -ggdb -Os -Wall -fno-strict-aliasing
COMPILER_OPTIONS += -ffunction-sections -fdata-sections -fno-exceptions -fno-delete-null-pointer-checks
COMPILER_OPTIONS += -fmessage-length=0 -fno-builtin -mthumb
COMPILER_OPTIONS += -mcpu=cortex-m0plus -MMD -MP $(CC_SYMBOLS)

DEPEND_OPTS = -MF $(OBJ_FOLDER)$(@F:.o=.d)

# Flags
CFLAGS = $(COMPILER_OPTIONS) $(DEPEND_OPTS) $(INC_DIRS_F) -std=gnu99 -c

ASFLAGS = $(COMPILER_OPTIONS) $(INC_DIRS_F) -c

# Linker options
LD_OPTIONS = -mcpu=cortex-m0plus -mthumb -Os -T $(LD_SCRIPT) $(INC_DIRS_F)
#LD_OPTIONS += -specs=nano.specs
#use this if %f is used, by default it's commented
#LD_OPTIONS += -u _printf_float -u _scanf_float
LD_OPTIONS += -Wl,-Map=$(OBJ_FOLDER)$(TARGET).map,--gc-sections

OBJCPFLAGS = -O ihex

ARFLAGS = cr

USER_OBJS =
C_SRCS =
S_SRCS =
C_OBJS =
S_OBJS =

# All source/object files inside SRC_DIRS
C_SRCS := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.c))
C_OBJS := $(patsubst %.c,$(OBJ_FOLDER)%.o,$(notdir $(C_SRCS)))


S_SRCS := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.s))
S_OBJS := $(patsubst %.s,$(OBJ_FOLDER)%.o,$(notdir $(S_SRCS)))
DEPEND := $(C_OBJS:%.o=%.d)



VPATH := $(SRC_DIRS)

$(OBJ_FOLDER)%.o : %.c
	@echo 'Building file: $(@F)'
	@echo 'Invoking: MCU C Compiler'
	$(CC) $(CFLAGS) $< -o $@
	@echo 'Finished building: $(@F)'
	@echo ' '

$(OBJ_FOLDER)%.o : %.s
	@echo 'Building file: $(@F)'
	@echo 'Invoking: MCU Assembler'
	$(AS) $(ASFLAGS) $< -o $@
	@echo 'Finished building: $(@F)'
	@echo ' '

all: create_outputdir $(OBJ_FOLDER)$(TARGET).elf print_info

create_outputdir:
	$(shell mkdir $(OBJ_FOLDER) 2>/dev/null)

# Tool invocations
$(OBJ_FOLDER)$(TARGET).elf: $(LD_SCRIPT) $(C_OBJS) $(S_OBJS)
	@echo 'Building target: $@'
	@echo 'Invoking: MCU Linker'
	$(LD) $(LD_OPTIONS) $(C_OBJS) $(S_OBJS) $(LIBS) -o $(OBJ_FOLDER)$(TARGET).elf
	@echo 'Finished building target: $@'
	@echo ' '

# Other Targets
clean:
	@echo 'Removing entire out directory'
	rm -rf $(TARGET).elf $(TARGET).bin $(TARGET).map $(OBJ_FOLDER)*.* $(OBJ_FOLDER)
	@echo ' '

print_info:
	@echo 'Printing size'
	arm-none-eabi-size --totals $(OBJ_FOLDER)$(TARGET).elf
	arm-none-eabi-objcopy -O srec $(OBJ_FOLDER)$(TARGET).elf $(OBJ_FOLDER)$(TARGET).s19
	arm-none-eabi-objcopy -O binary -v $(OBJ_FOLDER)$(TARGET).elf $(OBJ_FOLDER)$(TARGET).bin
	arm-none-eabi-objdump -D $(OBJ_FOLDER)$(TARGET).elf > $(OBJ_FOLDER)$(TARGET).lst
	arm-none-eabi-nm $(OBJ_FOLDER)$(TARGET).elf > $(OBJ_FOLDER)$(TARGET)-symbol-table.txt
	@echo ' '

-include $(DEPEND)

.PHONY: all clean print_info
