#
# Makefile for beelib
#
# Library for NIBObee robot
#


#
# BSD-License:
#
# Copyright (c) 2007 by Nils Springob, nicai-systems, Germany
# Copyright (c) 2010 by Matthias Bunte, Germany
#
# 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 following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright notice,
#     this list of conditions and the following disclaimer in the documentation
#     and/or other materials provided with the distribution.
#   * Neither the names of the authors nor the name nicai-systems nor
#     the names of its contributors may be used to endorse or promote products
#     derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
# OR CONTRIBUTORS 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.
#


#####################################################################
## Device setup                                                    ##
##  ##
#####################################################################
# The NIBObee is originally shipped with an ATmega16. The robot is also
# compatible to the ATmega???4-series which are:
#
# ATmega164P
# ATmega324P
# ATmega644P
# ATmega1284P
#
# Choose here your device:
#DEVICE = atmega16
DEVICE = atmega644
#DEVICE = atmega164p
#DEVICE = atmega324p
#DEVICE = atmega644p
#DEVICE = atmega1284p

# Version of Makefile
VERSION = 00.00.01

F_CPU = 15000000

#####################################################################
## Library files setup                                             ##
#####################################################################

# Source files in subdir
vpath %.c ./src

# Target(s) / library        
TARGETS = libbee-$(DEVICE).a

# Object directory
OBJ_DIR=obj

# Source files
SRC = \
  systemtimer.c \
  feelers.c \
  motorctrl.c \
  motorpid.c \
  odometers.c \

#####################################################################
## AVR compiler flags                                              ##
#####################################################################

SVN_REVISION := $(shell svnversion -n)

CFLAGS =  -Os -ffunction-sections
CFLAGS += -std=c99
CFLAGS += -Wall -g -Wa,-ahls=$(@:.o=.lst),-L
CFLAGS += -I./include/beelib
CFLAGS += -I./include/beelib_hal_$(DEVICE)
CFLAGS += -mmcu=$(DEVICE) -DF_CPU=$(F_CPU)
CFLAGS += -D_NIBOBEE_
CFLAGS += -DVERSION="\"$(VERSION)\"" -DSVN_REVISION="\"$(SVN_REVISION)\""

#####################################################################
## AVR Compiler setup                                              ##
#####################################################################

PREFIX =
CC = $(PREFIX)avr-gcc
AR = $(PREFIX)avr-ar

NO_OBJS =

OBJS = $(SRC:%.c=$(OBJ_DIR)/%.o)
DEPS = $(SRC:%.c=$(OBJ_DIR)/%.d)

#####################################################################
## Building instructions                                           ##
#####################################################################

all: $(TARGETS)

libbee-$(DEVICE).a: $(OBJS)

%.a:
	rm -f $@
	$(AR) -q $@ $^


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

$(OBJ_DIR)/%.d: %.c
	@mkdir -p $(OBJ_DIR)
	@echo "calculating dependencies for $<"
	@set -e; $(CC) -MM $(CFLAGS) $< \
	| sed 's#\($*\)\.o[ :]*#$(OBJ_DIR)/\1.o $@ : #g' > $@ ; \
	[ -s $@ ] || rm -f $@


clean:
	rm -rf $(OBJ_DIR) $(TARGETS)

include $(DEPS)

.PHONY: clean all
.SECONDARY: $(OBJS) $(DEPS)

