# -*- mode:makefile; -*-
# Makefile-POSIX : 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 host-side test programs.

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

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

# The assumption is one main .c file per test program.
PROGNAMES  = example-controller kalman-test simulate-kalman lqr-test

PROGS      = $(PROGNAMES:%=$(OBJDIR)/%)   # 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"'

CFLAGS        = -DTARGET_POSIX -g $(VERSIONFLAGS) $(INCLUDE_PATHS) -c -Wall

LDFLAGS       = -g -flat_namespace
LDLIBS        = -L$(OBJDIR) -lLPC2xxx -lstd -lLPC2xxx
LIBS          = $(OBJDIR)/libstd.a $(OBJDIR)/libLPC2xxx.a

# configure the included makefiles
LIBSTD_INCLUDE_UNIX_SOURCE = true
RANLIB= ranlib

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

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

################################################################
$(OBJDIR)/kalman-test.o: kalman-test.c \
			 linear-control.c linear-control-no-gyro.c linear-control-pitch-encoder.c \
			 sensors.c serial-robot-interface.c

$(OBJDIR)/example-controller.o: example-controller.c sensors.c serial-robot-interface.c

$(OBJDIR)/simulate-kalman.o: simulate-kalman.c \
			 linear-control.c linear-control-no-gyro.c linear-control-pitch-encoder.c

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

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

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

$(OBJDIR)/% : $(OBJDIR)/%.o $(LIBS)
	$(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS)

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

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

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

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

include $(DEPENDS)

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