#
# Makefile (gunzets: an USB driver for a Gunze touchscreen)
#
# Copyright 1998   rubini@linux.it
# (this Makefile does more than needed. it's my standard Makefile for drivers)
#
##########
#

# Comment/uncomment the following line to enable/disable debugging
#DEBUG = y

# Change it here or specify it on the "make" commandline
ifndef KERNELDIR
  KERNELDIR = /usr/src/linux
endif
ifndef INCLUDEDIR #can be defined for backward compatibility
  INCLUDEDIR = $(KERNELDIR)/include
endif

INSTALL = install -c
INSTALLBIN = $(INSTALL) -m 755
INSTALLLIB = $(INSTALL) -m 644

ifndef prefix
  prefix = $(DESTDIR)/usr/local
endif

TARGET = gunzets

MODDIR = $(DESTDIR)/lib/modules/$(shell uname -r)/kernel/drivers/usb
LIBDIR = $(prefix)/lib
BINDIR = $(prefix)/bin
INCDIR = $(prefix)/include
MANDIR = $(prefix)/share/man
INFODIR = $(prefix)/share/info
MAN1DIR = $(MANDIR)/man1
MAN4DIR = $(MANDIR)/man4

# Don't use *.c in make depend, as xf86Gunze.c can't be compiled unless
# X sources are online
DEPENDABLES = gunzets.c

############## no more editing...

INSTALLDIRS =  $(MODDIR) $(BINDIR) $(LIBDIR) $(INCDIR) \
        $(MANDIR) $(MAN1DIR) $(MAN3DIR) $(MAN4DIR)

CLIENTOBJS = 
ALLCLIENTOBJS = $(CLIENTOBJS)
CLIENTS =  $(CLIENTOBJS:.o=)  
ALLCLIENTS = $(ALLCLIENTOBJS:.o=)
SCRIPTS = gunzets_load gunzets_calib
TOOLS = gunzets_control warp
MANPAGES =

ifeq ($(DEBUG),y)
  DEBFLAGS = -O -g -DPX_DEBUG # "-O" is needed to expand inlines
  LDFLAGS =
else
  DEBFLAGS =  -O3 -fomit-frame-pointer
  LDFLAGS = -s
endif

%: %.o
	$(CC) $(CFLAGS) $^ -o $@ -L. 
#-l$(TARGET)

%.i: %.c
	$(CC) $(CFLAGS) -E $^ -o $@

%.S: %.c
	$(CC) $(CFLAGS) -S $^ -o $@

%: %.c
	$(CC) $(CFLAGS)  $^ $(LDFLAGS) -o $@

CFLAGS = -I/usr/X11R6/include -Wall $(DEBFLAGS)
CFLAGS += -I$(INCLUDEDIR)
LDFLAGS = -L/usr/X11R6/lib -lX11 -lXi

# Extract version number from headers.
VER = $(shell awk -F\" '/REL/ {print $$2}' $(INCLUDEDIR)/linux/version.h)

OBJS = $(TARGET).o

# force make depend
ifneq (.depend,$(wildcard .depend))
all: depend
endif



all: $(TARGET).o $(ALLCLIENTOBJS) $(ALLCLIENTS) $(MANPAGES) $(TOOLS)
	$(MAKE) -C doc

ifndef RANLIB
  RANLIB = ranlib
endif

install: all
	install -d $(DESTDIR)/lib/modules/$(VER)/misc \
		$(BINDIR)
	install -c -m 0644 $(TARGET).o \
		$(DESTDIR)/lib/modules/$(VER)/misc
	@-depmod -a 2> /dev/null
	install -c -m 0755 $(CLIENTS) $(SCRIPTS) $(TOOLS) $(BINDIR) 
	make -C doc install


clean-with-docs:
	rm -f $(TARGET).o $(ALLCLIENTOBJS) $(ALLCLIENTS) $(MANPAGES) $(TOOLS)

clean: clean-with-docs
	make -C doc clean

tar:
	@if [ "x" = "x$(RELEASE)" ]; then \
	    n=`basename \`pwd\``; cd ..; tar cvf - $$n | gzip > $$n.tar.gz; \
	    echo 'you can set a numeric $$(RELEASE) to make a named tar'; \
	else \
	    mkdir ../$(TARGET)-$(RELEASE) || exit 1; \
	    cp -a . ../$(TARGET)-$(RELEASE) && cd .. && \
	      tar --exclude '*/CVS*' \
	  	-cvzf $(TARGET)-$(RELEASE).tar.gz $(TARGET)-$(RELEASE); \
	fi
	grep VERSION *.[ch] doc/doc.*

distrib: all clean-with-docs tar

depend dep:
	$(CC) -I$(INCLUDEDIR) -MM $(DEPENDABLES) > .depend

DEPEND DEP:
	$(CC) -I$(INCLUDEDIR) -M *$(DEPENDABLES) > .depend


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

####################
# The following stuff is a dirty hack, from the log of my own compilation
# of XFree-3.3 and XFree-4.0

ifdef XFREE33SOURCE
    # define some shorthands
    XFREE33SERVER = $(XFREE33SOURCE)/programs/Xserver

    # note that:
    #    I removed the "-ansi" flag and added -Wall
%.so: %.c
	gcc -Wall -c -O2 -fno-strength-reduce -pedantic \
	-I$(XFREE33SERVER)/hw/xfree86 \
	-I$(XFREE33SERVER)/hw/xfree86/common \
	-I$(XFREE33SERVER)/hw/xfree86/os-support \
	-I$(XFREE33SERVER)/mfb \
	-I$(XFREE33SERVER)/mi \
	-I$(XFREE33SERVER)/include \
	-I$(XFREE33SERVER)/os \
	-I$(XFREE33SOURCE)/exports/include/X11 \
	-I$(XFREE33SOURCE)/include \
	-I$(XFREE33SOURCE)/include/extensions  \
	-I$(XFREE33SOURCE)/exports/include  \
	-Dlinux -D__i386__ -D_POSIX_C_SOURCE=199309L -D_POSIX_SOURCE \
	-D_XOPEN_SOURCE=500L -D_BSD_SOURCE -D_SVID_SOURCE -DSHAPE -DXINPUT \
	-DXKB -DLBX -DXAPPGROUP -DXCSECURITY  -DDPMSExtension -DPIXPRIV  \
	-DGCCUSESGAS -DSTATIC_COLOR -DAVOID_GLYPHBLT -DPIXPRIV  -DXFreeXDGA \
	-DNDEBUG   -DFUNCPROTO=15 -DNARROWPROTO    -DPNP_MOUSE \
	-fPIC -DDYNAMIC_MODULE \
	-DDEFAULT_MODULE_PATH=\"/usr/X11R6/lib/modules\"  \
	$*.c
	gcc -o $*.so -shared $*.o
	rm -f $*.o
endif

ifdef XFREE40SOURCE
    # define some shorthands
    XFREE40SERVER = $(XFREE40SOURCE)/programs/Xserver

gunze_drv.o: xf86Gunze.c 
	gcc -O2 -fno-strength-reduce -ansi -pedantic -Wall -Wpointer-arith \
	-I. \
	-I$(XFREE40SERVER)/hw/xfree86/common \
	-I$(XFREE40SERVER)/hw/xfree86/loader \
	-I$(XFREE40SERVER)/hw/xfree86/os-support \
	-I$(XFREE40SERVER)/include \
	-I$(XFREE40SERVER)/mi \
	-I$(XFREE40SOURCE)/exports/include/X11 \
	-I$(XFREE40SOURCE)/include/extensions \
	-I$(XFREE40SOURCE) \
	-I$(XFREE40SOURCE)/exports/include \
	-Dlinux -D__i386__ -D_POSIX_C_SOURCE=199309L -D_POSIX_SOURCE \
	-D_XOPEN_SOURCE -D_BSD_SOURCE -D_SVID_SOURCE \
	-I$(INCLUDEDIR) \
	-D_GNU_SOURCE  -DSHAPE \
	-DXINPUT -DXKB -DLBX -DXAPPGROUP -DXCSECURITY -DTOGCUP  -DXF86BIGFONT \
	-DDPMSExtension  -DPIXPRIV -DPANORAMIX  -DGCCUSESGAS -DAVOID_GLYPHBLT \
	-DPIXPRIV -DSINGLEDEPTH -DXFreeXDGA -DXvExtension -DXFree86LOADER \
	-DXFree86Server -DXF86VIDMODE  -DSMART_SCHEDULE \
	-DX_BYTE_ORDER=X_LITTLE_ENDIAN -DNDEBUG \
	-DFUNCPROTO=15 -DNARROWPROTO  -DIN_MODULE \
	-c $^ -o $@

gunze_drv.o: xf86Gunze.c

endif
