#
## Makefile for the SAR Library
#

DEST_MACHINE = $(shell uname -m)
DEST_KERNEL = $(shell uname -r)
KERNEL_PATH = /lib/modules/$(DEST_KERNEL)/build
CONFIG_PATH = /usr/src/linux
CONFIG_NAME = .config

ifneq ($(MAKECMDGOALS), clean)
        include $(CONFIG_PATH)/$(CONFIG_NAME)
endif

# you can add -DCRCASM to get better performance
#CFLAGS := -O2 -Wstrict-prototypes  -fomit-frame-pointer -fno-strict-aliasing -pipe -fno-strength-reduce -Wall -D__KERNEL__ -DLINUX
CFLAGS := -O2 -Wstrict-prototypes  -fomit-frame-pointer -fno-strict-aliasing -pipe -fno-strength-reduce -march=$(DEST_MACHINE) -Wall -D__KERNEL__ -DLINUX

#INCLUDES := -I/usr/src/linux/include/ -I/usr/include/
INCLUDES = -I$(KERNEL_PATH)/include

VERSION = $(shell cat VERSION)
SHORTVERSION = $(shell cat VERSION | cut -d '.' -f 1-2)
# parse kernel options

ifdef CONFIG_SMP
	CFLAGS += -D__SMP__
endif

ifeq ($(shell grep -q tx_inuse $(KERNEL_PATH)/include/linux/atmdev.h && echo "yes"), yes)
	CFLAGS += -DATM_VCC_OLD
endif

ifeq ($(shell grep -q alloc_tx $(KERNEL_PATH)/include/linux/atmdev.h && echo "yes"), yes)
	CFLAGS += -DATM_VCC_ALLOC_TX
endif

all: normal debug

normal: sarlib.o 

debug: sarlibdbg.o

%.o : %.c %.h Makefile
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

%dbg.o : %.c %.h Makefile
	$(CC) $(CFLAGS) $(INCLUDES) -DDEBUG -c $< -o $@

package: clean
	(cd ..; rm sarlib-$(VERSION); ln -s sarlib-$(SHORTVERSION) sarlib-$(VERSION); tar zcvfh sarlib-$(VERSION).tgz sarlib-$(VERSION)/);

clean:
	/bin/rm -f *.o
