# -*- mode:makefile; -*-
# Makefile-OP03 : target makefile for ArtLPC
# Copyright (c) 2005-2007 Garth Zeglin

# This file is part of ArtLPC. 

# ArtLPC 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 of the License, or
# (at your option) any later version.

# ArtLPC 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 Foobar; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

# ---------------------------------------------------------------------

# Makefile for target-side library test programs for the Olimex
# LPC-P2103 Prototyping board.  Ideally everything in this directory
# would always compile.

# selective forms of vpath
vpath %.c ../libstd ../libLPC2xxx
vpath %.h ../libstd ../libLPC2xxx
vpath %.S ../libstd ../libLPC2xxx

# LIBOBJDIR is used by the library makefile fragments
OBJDIR    = OP03
LIBOBJDIR = $(OBJDIR)

# The assumption is one main .c file per test program.
PROGNAMES  = hello test-format test-memalloc

PROGS      = $(PROGNAMES:%=$(OBJDIR)/%.hex) # the executable targets
C_SRCS     = $(PROGNAMES:%=%.c)             # the source files for dependencies
OBJS       = $(C_SRCS:%.c=%.o)

INCLUDE_PATHS= -I. -I../libstd -I../libLPC2xxx

# If Subversion is installed, this can create a symbol with the current code revision.
# VERSIONFLAGS := -D'SVN_REVISION="$(shell svnversion -n .)"'
VERSIONFLAGS=-D'SVN_REVISION="public"'

# -Os 		  optimize for size, similar to -O2
# -O3 	 	  enable automatic inlining, etc.
# -mcpu=arm7tdmi  this should be the compiler default as-configured
# -Wa,-mapcs-32   selects the procedure calling convention (should be default).
CFLAGS  = -DTARGET_OP03 $(VERSIONFLAGS) -g $(INCLUDE_PATHS) -c -Os -Wall

# assembler options (given to gcc):
#   -Wa,-mapcs-32        selects the procedure calling convention (should be default).
#   -Wa,-ahls=main.lst   generates a listing file
AFLAGS  = -c -g

# linker options:
# -u   declare an undefined symbol to trigger linking of otherwise unreferenced code
LDFLAGS  =  -g  -nostartfiles -T ../libLPC2xxx/LPC2103-flash.lnk -nostdlib -u _reset_vector
LDLIBS   = -L$(OBJDIR) -lLPC2xxx -lstd -lLPC2xxx -lgcc
LIBS     = $(OBJDIR)/libstd.a $(OBJDIR)/libLPC2xxx.a

# uncomment the following lines to strip individual functions out of the linked image
LDFLAGS  +=  --gc-sections
CFLAGS   +=  -ffunction-sections -fdata-sections

# configure the included makefiles:
LIBSTD_INCLUDE_UNIX_SOURCE = false
RANLIB  = arm-elf-ranlib

################################################################
default: $(OBJDIR) $(LIBS) $(PROGS)

include ../libstd/libstd-public-makefile
include ../libLPC2xxx/OP03-makefile

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

# the main target

################################################################
.SUFFIXES : .o .c .S

CC      = arm-elf-gcc
LD      = arm-elf-ld -v
AR      = arm-elf-ar
AS      = arm-elf-as
OBJCOPY = arm-elf-objcopy
OBJDUMP = arm-elf-objdump

$(OBJDIR)/%.o : %.c
	$(CC) -o $@ $(CFLAGS) $< 

$(OBJDIR)/%.o : %.S
	$(CC) -o $@ $(AFLAGS) $<

$(OBJDIR)/%.out: $(OBJDIR)/%.o $(LIBS)
	$(LD) $(LDFLAGS) -o $@ -Map $(OBJDIR)/$*.map $^ $(LDLIBS)

$(OBJDIR)/%.hex : $(OBJDIR)/%.out
	$(OBJCOPY) -O ihex $< $@

# create the object directory if needed
$(OBJDIR):
	mkdir $@

################################################################
# disassembly options for testing
$(OBJDIR)/%.disasm : $(OBJDIR)/%.o
	$(OBJDUMP) --disassemble-all $< >$@

$(OBJDIR)/%.disasm : $(OBJDIR)/%.out
	$(OBJDUMP) --disassemble-all $< >$@

# rule to generate assembly, useful for testing
$(OBJDIR)/%.S : %.c
	$(CC) -o $@ $(CFLAGS) -S $<

################################################################
clean:
	-rm $(LIBS) $(PROGS) $(OBJDIR)/*.o $(OBJDIR)/*.out \
	$(OBJDIR)/*.map $(OBJDIR)/*.disasm $(OBJDIR)/*.lst $(OBJDIR)/*.bin \
	$(OBJDIR)/.gdb_history 

################################################################
# The dependencies need a little post-processing so they are kept in a separate file.
DEPENDS = Makefile-OP03-deps

depend:
	makedepend -f$(DEPENDS) -Y $(INCLUDE_PATHS) -p$(OBJDIR)/ -- \
	$(C_SRCS) \
	$(NANOSCHEME_SRCS:%=../nanoscheme/%) \
	$(LIBSTD_SRCS:%=../libstd/%) \
	$(LPC2XXX_ASM_SRCS:%=../libLPC2xxx/%) \
	$(LPC2XXX_C_SRCS:%=../libLPC2xxx/%)
	mv $(DEPENDS) $(DEPENDS)-tmp
	sed <$(DEPENDS)-tmp -e 's|OP03/../nanoscheme/|OP03/|' | \
	sed -e 's|OP03/../libstd/|OP03/|' | \
	sed -e 's|OP03/../libLPC2xxx/|OP03/|' >$(DEPENDS)

include $(DEPENDS)

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