#############################################################################
#
#	Makefile for building the robostix module
#
#############################################################################

ifeq ($(DEBUG),y)
	CFLAGS += -O -g		# -O is need to expand inlines
else
	CFLAGS += -O2
endif

ifneq ($(KERNELRELEASE),)
# call from kernel build system

obj-m	:= robostix_drv.o

else

PWD       := $(shell pwd)

ifdef ROBOSTIX

# Called from buildroot system
#BUILD_ARM		= $(BUILD_DIR)
#KERNELDIR 		= $(BUILD_ARM)/linux-2.6.11gum
#CROSS_COMPILE		= $(TARGET_CROSS)

GUMSTIX_BUILDROOT	= $(PWD)/../../../gumstix-buildroot
BUILD_ARM		= $(GUMSTIX_BUILDROOT)/build_arm_nofpu
KERNELDIR 		?= $(BUILD_ARM)/linux-2.6.11gum
CROSS_COMPILE		= $(BUILD_ARM)/staging_dir/bin/arm-linux-uclibc-

else

# Called as standalone build
GUMSTIX_BUILDROOT	= $(PWD)/../../gumstix-buildroot
BUILD_ARM		= $(GUMSTIX_BUILDROOT)/build_arm_nofpu
KERNELDIR 		?= $(BUILD_ARM)/linux-2.6.11gum
CROSS_COMPILE		= $(BUILD_ARM)/staging_dir/bin/arm-linux-uclibc-

endif

default: user-app kernel-module

print:
	echo $(ROBOSTIX)
	echo $(ROBOSTIX_SOURCE)
	echo $(BUILD_ARM)
	echo $(KERNELDIR)
	echo $(CROSS_COMPILE)

.PHONY: user-app kernel-module

user-app: robostix sertest

TARGET_ARCH=-Os -march=armv5te -mtune=xscale -Wa,-mcpu=xscale
CC = $(CROSS_COMPILE)gcc

robostix: robostix.c

sertest: LDLIBS = -lpthread
sertest: sertest.c

kernel-module:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) ARCH=arm CROSS_COMPILE=$(CROSS_COMPILE) modules

endif

clean:
	rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions robostix sertest

depend .depend dep:
	$(CC) $(CFLAGS) -M *.c > .depend

ifeq (.depend,$(wildcard .depend))
include .depend
endif
