
ROOT =		hotplug
PREFIX =	diet
VERSION =	0.3
INSTALL_DIR =	/usr/local/bin
RELEASE_NAME =	$(PREFIX)$(ROOT)-$(VERSION)

# override this one on the make command line, as it's probably wrong
KERNEL_INCLUDE_DIR = /usr/local/linux

# get the kernel version from the include file so we can determine where
# the map files are
KERNEL_VERSION = $(shell grep UTS_RELEASE $(KERNEL_INCLUDE_DIR)/linux/version.h | cut -f 2 -d \" )


# directory pointing to the dietlibc code that we are going to use
DIET_DIR	:= dietlibc

# code taken from dietlibc Makefile to determine the current arch

MYARCH=$(shell uname -m | sed -e 's/i[4-9]86/i386/' -e 's/armv[3-6][lb]/arm/')
# This extra-ugly cruft is here so make will not run uname and sed each
# time it looks at $(OBJDIR).  This alone sped up running make when
# nothing has to be done from 1 sec to 0.12 sec on a 900 MHz Athlon.
ifeq ($(MYARCH),i386)
ARCH=i386
else
ifeq ($(MYARCH),mips)
ARCH=mips
else
ifeq ($(MYARCH),alpha)
ARCH=alpha
else
ifeq ($(MYARCH),ppc)
ARCH=ppc
else
ifeq ($(MYARCH),arm)
ARCH=arm
else
ifeq ($(MYARCH),sparc)
ARCH=sparc
else
ifeq ($(MYARCH),s390)
ARCH=s390
else
ifeq ($(MYARCH),mipsel)
ARCH=mipsel
else
$(error unknown architecture, please fix Makefile)
endif
endif
endif
endif
endif
endif
endif
endif


all: $(ROOT)

# Comment out this line to build with diet libc
#DIET = diet

CC = gcc


# arch specific objects
ARCH_LIB_OBJS =	\
		$(DIET_DIR)/$(ARCH)/start.o		\
		$(DIET_DIR)/$(ARCH)/dyn_syscalls.o

LIB_OBJS =	\
		$(DIET_DIR)/lib/strncmp.o	\
		$(DIET_DIR)/lib/strtoul.o	\
		$(DIET_DIR)/lib/memcmp.o	\
		$(DIET_DIR)/lib/getenv.o	\
		$(DIET_DIR)/lib/errno_location.o	\
		$(DIET_DIR)/lib/atexit.o		\
		$(DIET_DIR)/lib/execv.o			\
		$(DIET_DIR)/syscalls.s/errno.o		\
		$(DIET_DIR)/syscalls.s/environ.o

OBJS =	hotplug.o	\
	usb.o		\
	pci.o		\
	ieee1394.o	\
	logging.o	\
	util.o

# header files automatically generated
GEN_HEADERS =	usb_modules.h		\
		pci_modules.h		\
		ieee1394_modules.h	\
		hotplug_version.h

# other files we want in the release tarball
OTHERFILES =	Makefile	\
		README		\
		ChangeLog	\
		COPYING


ifeq ($(DEBUG),y)
  # options for development
  DEBUG_FLAGS = -g -DDEBUG
endif

INCLUDE_DIR := $(DIET_DIR)/include
WFLAGS := -Wall -Wstrict-prototypes
CFLAGS := -O2 -D_GNU_SOURCE
CFLAGS += $(DEBUG_FLAGS) $(WFLAGS)
CFLAGS += -I$(INCLUDE_DIR)


# Rules on how to create the generated header files
usb_modules.h:
	perl convert_usb.pl < /lib/modules/$(KERNEL_VERSION)/modules.usbmap > $@

pci_modules.h:
	perl convert_pci.pl < /lib/modules/$(KERNEL_VERSION)/modules.pcimap > $@

ieee1394_modules.h:
	perl convert_ieee1394.pl < /lib/modules/$(KERNEL_VERSION)/modules.ieee1394map > $@

hotplug_version.h:
	@echo \#define HOTPLUG_VERSION \"$(VERSION)\" > $@



$(ROOT): $(GEN_HEADERS) $(OBJS) $(LIB_OBJS) $(ARCH_LIB_OBJS)
#	$(CC) -o $(ROOT) $(OBJS) $(LIB_OBJS)
	$(LD) -o $(ROOT) $(OBJS) $(LIB_OBJS) $(ARCH_LIB_OBJS)

clean:
	-find . \( -not -type d \) -and \( -name '*~' -o -name '*.[oas]' \) -type f -print \
	 | xargs rm -f 
	-rm -f core $(ROOT) $(GEN_HEADERS)

#DISTFILES := $(shell find . \( -not -name '.gz' \) -and \( -not -name 'CVS' \) -a \( -not -name 'Root' \)  -a \( -not -name 'Entries' \) -a \( -not -name 'Repository' \) -a \( -not -name '.' \) -a \( -not -name 'Base' \) -print)
DISTFILES = $(shell find . \( -not -name '.' \) -print | grep -v CVS | grep -v "\.tar\.gz" | grep -v "\/\." | grep -v releases )
distdir := $(RELEASE_NAME)
srcdir = .
release: $(DISTFILES) clean
#	@echo $(DISTFILES)
	@-rm -rf $(distdir)
	@mkdir $(distdir)
	@-chmod 777 $(distdir)
	@for file in $(DISTFILES); do			\
		if test -d $$file; then			\
		  	mkdir $(distdir)/$$file;	\
		else					\
			cp -p $$file $(distdir)/$$file;	\
		fi;					\
	done
	@tar -c $(distdir) | gzip -9 > $(RELEASE_NAME).tar.gz
	@rm -rf $(distdir)
	@echo "Built $(RELEASE_NAME).tar.gz"
