#/* ----------------------------------------------------------------------------
# *         ATMEL Microcontroller Software Support  -  ROUSSET  -
# * ----------------------------------------------------------------------------
# * Copyright (c) 2006, Atmel Corporation
#
# * All rights reserved.
# *
# * Redistribution and use in source and binary forms, with or without
# * modification, are permitted provided that the following conditions are met:
# *
# * - Redistributions of source code must retain the above copyright notice,
# * this list of conditions and the disclaiimer below.
# *
# * - Redistributions in binary form must reproduce the above copyright notice,
# * this list of conditions and the disclaimer below in the documentation and/or
# * other materials provided with the distribution.
# *
# * Atmel's name may not be used to endorse or promote products derived from
# * this software without specific prior written permission.
# *
# * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
# * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
# * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
# * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# * ----------------------------------------------------------------------------
# */
#-------------------------------------------------------------------------------
# content   Topmost Makefile for building AT91SAM7SE512 software
# author    JJo
# version   $Id: Makefile 908 2007-02-05 12:03:27Z jjoannic $
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
#       Options
#-------------------------------------------------------------------------------
# Uncomment this line to enable debug mode
DEBUG = -O0
# Format of debug informations
DEBUG_FORMAT = -gstabs+

#-------------------------------------------------------------------------------
#       Tools
#-------------------------------------------------------------------------------
# Compiler
export CC = arm-elf-gcc
# Assembler
export AS = arm-elf-gcc
# Linker
export LD = arm-elf-ld
# Object copier
export OBJCOPY = arm-elf-objcopy

#-------------------------------------------------------------------------------
#       Flags
#-------------------------------------------------------------------------------
# Flags:
# -mcpu:          specifies the CPU family
# -D__ASSEMBLY__: required for correct interpretation of include files
# -Os:			  size optimization
# $(DEBUG):		  disables optimization (-O0) and generates debug info (-g)
# -Wall:		  enables all warnings
# -nostartfiles:  prevent the compiler from using libgcc start files
#
# C compiler compilation flags
export CCFLAGS = -Wall -mcpu=arm7tdmi -Os $(DEBUG) $(DEBUG_FORMAT)
# Assembler compilation flags
export ASFLAGS = -Wall -mcpu=arm7tdmi -Os $(DEBUG) -D__ASSEMBLY__ -Wa,$(DEBUG_FORMAT)
# Linker flags
export LDFLAGS = -nostartfiles

#-------------------------------------------------------------------------------
#       Directories
#-------------------------------------------------------------------------------
# Topmost directory
export TOP = $(shell pwd)
# Binaries directory
export BIN = $(TOP)\bin
# Objects directory
export OBJ = $(TOP)\obj
# Source directory
export SRC = $(TOP)\src

#-------------------------------------------------------------------------------
#       Input and output files/directories
#-------------------------------------------------------------------------------

# Only directories containing Makefiles are searched
# The first line retrieves the directory names in $(SRC)\
# The second line only retains directory which contain a Makefile
DIRS = $(shell dir $(SRC) /AD /B)
DIRS := $(foreach DIR, $(DIRS), $(if $(shell dir $(SRC)\$(DIR) /A-D /B | find "Makefile" | find /V "Makefile."), $(DIR),))

# Partial objects created by sub-Makefiles
INPUT = $(addprefix $(OBJ)\, $(addsuffix .partial, $(DIRS)))

# Outputs
TARGET = flash flash_remap ram sdram
OUTPUT = $(addprefix $(BIN)\, $(addsuffix .bin, $(TARGET)))

#-------------------------------------------------------------------------------
#       Rules
#-------------------------------------------------------------------------------

.PHONY: all $(TARGET) $(DIRS) clean

all: $(TARGET)

$(TARGET): %: $(DIRS) $(BIN)\\%.elf $(BIN)\\%.bin

$(OUTPUT): %.bin: %.elf
# ELF to BIN conversion
	$(OBJCOPY) --strip-debug --strip-unneeded -O binary $^ $@

$(BIN)\flash.elf: $(INPUT)
# Link program for flash execution (exception vectors in flash)
# .text = 0x100000 and .data = 0x200000
	$(CC) $(LDFLAGS) -T "textvectors.lds" -Ttext 0x100000 -Tdata 0x200000 -o $@ $^
	
$(BIN)\flash_remap.elf: $(INPUT)
# Link program for flash execution (exception vectors in RAM)
# .text = 0x100000 and .data = 0x200000
	$(CC) $(LDFLAGS) -T "datavectors.lds" -Ttext 0x100000 -Tdata 0x200000 -o $@ $^

$(BIN)\ram.elf: $(INPUT)
# Link program for RAM execution
# .text = 0x200000, .data = .text end
	$(CC) $(LDFLAGS) -T "textvectors.lds" -Ttext 0x200000 -Tdata 0x200000 -o $@ $^
	
$(BIN)\sdram.elf: $(INPUT)
# Link program for SDRAM execution
# .text = 0x20000000, .data = .text end
	$(CC) $(LDFLAGS) -T "textvectors.lds" -Ttext 0x20000000 -o $@ $^

$(INPUT): $(DIRS)

$(DIRS):
# Recursively enter each directory
	$(MAKE) -C $(SRC)\$@
	
clean:
	@del /Q $(BIN)\*.*
	@del /Q $(OBJ)\*.*
	