# Main Makefile for Phantom software for RT Linux
# This makefile was constructed by butchering the linux makefile


# pick your favourite compiler
#CC = egcc
#CC = gcc273

EXAMPLE_DIRS = libphantom tests tunnels
# ljocha
MODULE_DIRS = kernel kernel_tunnel
ALL_DIRS = $(MODULE_DIRS) $(EXAMPLE_DIRS)

TOPDIR = $(PWD)

# root directory of rt-linux instalation
RTL_DIR = /var/src/rtl
# root directory for rtlinux (linux kernel patched with RTL patch)
RTLINUX = /var/src/linux

# find architecture - to apply specific CFLAGS needed to match compiled kernel
ARCH := $(shell uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ -e s/arm.*/arm/ -e s/sa110/arm/)

KERNEL_INCLUDES = -I$(RTL_DIR)/include/posix -I$(RTL_DIR)/include -I$(RTLINUX)/include
PHM_INCLUDES = -I$(TOPDIR)/include -I$(TOPDIR)/libphantom

STANDARD_CFLAGS = -O2 -Wall -fomit-frame-pointer -Wstrict-prototypes

# these extra flags creates better code if you have Pentium processor
EXTRA_CFLAGS = -freg-struct-return -mpentium -ffast-math

# get version of current linux kernel source
KERNELRELEASE := $(shell grep UTS_RELEASE $(RTLINUX)/include/linux/version.h 2>/dev/null | cut -d\" -f2)

MODULE_PATH := /lib/modules/$(KERNELRELEASE)/misc

PACKAGE_NAME := $(shell basename $(PWD))

# standard CFLAGS
# CFLAGS = $(INCLUDE) -I$(RTL)/include/posix -Wall -Wstrict-prototypes -O2

# get all current option for RTLinux config file
include $(RTLINUX)/.config

include Makefile.i386

CFLAGS += $$\(INCLUDES\) $(STANDARD_CFLAGS) $(EXTRA_CFLAGS)

ifdef CONFIG_SMP
CFLAGS += -D__SMP__
endif

CFLAGS += -D__RTL__ -D__KERNEL__ -DMODULE

ifdef CONFIG_MODVERSIONS
CFLAGS += -DMODVERSIONS -include $(RTLINUX)/include/linux/modversions.h
endif

PHMMK = phm.mk

all: $(PHMMK) modules examples
	@[ -d modules ] || mkdir modules
	@cp -a kernel/phantom.o modules
	@cp -a kernel_tunnel/tunnel.o modules
	@echo -e "\n  Make sure you have all modules in the subdirectory 'modules'"
	@echo "  and you should be done"
	@echo ""
	@echo "  Type 'make install' as root to install kernel modules"
	@echo "  Type 'make dep' as root to create necessary PHANToM devices"

$(PHMMK): 
	@echo "#Automatically generated by phm Makefile - DO NOT EDIT!" >$(PHMMK)
	@echo KERNEL_INCLUDES = $(PHM_INCLUDES) >> $(PHMMK)
	@echo INCLUDES = $(PHM_INCLUDES) >> $(PHMMK)
	@echo EXTRA_CFLAGS = $(EXTRA_CFLAGS) >> $(PHMMK)
	@echo CFLAGS = $(STANDARD_CFLAGS) $(PHM_INCLUDES) $$\(EXTRA_CFLAGS\) >>$(PHMMK)
	@echo MODULE_CFLAGS = $(CFLAGS) $(KERNEL_INCLUDES) >>$(PHMMK)
	@echo CC = $(CC) >>$(PHMMK)
	@echo PHM_MODULES = $(MODULE_PATH) >>$(PHMMK)

#@echo CFLAGS = $$\(INCLUDES\) $(STANDARD_CFLAGS) $$\(EXTRA_CFLAGS\) >>$(PHMMK)
# were do you wish to place created module
# usually this should be uname -r
# but we are compiling on different machine
PHM_PLACE = /var/tmp/phantom

modules: dummy
	@[ -d modules ] || mkdir modules
	@set -e;\
	echo "	Bulding PHANToMs' RTL modules" ;\
	for i in $(MODULE_DIRS); do\
		$(MAKE) -C $$i;\
	done

examples: dummy
	@set -e;\
	echo "	Bulding PHANToMs' libraries & examples" ;\
	for i in $(EXAMPLE_DIRS); do\
		$(MAKE) -C  $$i;\
	done
install: dummy
	mkdir -p $(MODULE_PATH)
	install -c -m 664 modules/phantom.o $(MODULE_PATH)
	/sbin/depmod -a 
	/sbin/modprobe -r phantom
#	/sbin/modprobe phantom

arch: clean
	@echo -n "Bulding archiv file... ";\
	ARCHNAME=$(PACKAGE_NAME)`date +%Y%m%d`.tgz;\
	cd ..;\
	tar -cf - $(PACKAGE_NAME) | gzip >$$ARCHNAME;\
	mv $$ARCHNAME $(PACKAGE_NAME);\
	cd $(PACKAGE_NAME);\
	echo "$$ARCHNAME created":

dev: dummy
	@echo -n "Creating PHANToM devices...";\
	if [ -c /dev/phantomPCI0 -o -c /dev/phantomPCI1 -o -c /dev/phantomISA0 -o -c /dev/phantomISA1 ]; then\
		echo "";\
		echo "PHANToM device(s) already exists";\
		echo "  If you want to recreate them please first";\
		echo "  remove all /dev/phantom* devices yourself";\
	else\
		test -c /dev/phantomISA0 || mknod /dev/phantomISA0 c 120 0;\
		test -c /dev/phantomISA1 || mknod /dev/phantomISA1 c 120 2;\
		test -c /dev/phantomPCI0 || mknod /dev/phantomPCI0 c 120 1;\
		test -c /dev/phantomPCI1 || mknod /dev/phantomPCI1 c 120 3;\
		test -L /dev/phantom || ln -s /dev/phantomPCI0 /dev/phantom;\
		if [ `grep -q phantom /etc/group` ]; then\
			echo "Please consider to create group phantom for users";\
			echo "which will have write access to this device";\
		else\
			chown root.phantom /dev/phantom* ;\
			chmod 0664 /dev/phantom* ;\
			echo "done";\
		fi\
	fi


clean: $(PHMMK)
	@set -e; \
	for i in $(ALL_DIRS); do\
		if [ -f $$i/Makefile ]; then\
			echo $$i; $(MAKE) -C $$i clean;\
		fi;\
	done;
	@$(RM) -rf modules
	@$(RM) -f core phm.mk $(PHMMK) $(PACKAGE_NAME)*.tgz `find . -type f \
		-name '*.[aos]' -o -name '*~' -o -name '*.out' -o -name '#*' -o -name '.#*'`

.PHONY: dummy
