#! gnumake -f
# Makefile for the CIPE kernel module and driver.

#   Copyright 1996 Olaf Titz <olaf@bigred.inka.de>
#
#   This program is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public License
#   as published by the Free Software Foundation; either version
#   2 of the License, or (at your option) any later version.

# $Id: Makefile,v 1.19.2.2 1998/11/14 20:36:39 olaf Exp $

# Targets: all install dvi clean

### Config: which crypto algorithm and version

# Select one of these algorithms
CRYPTO=	Blowfish
#CRYPTO=IDEA
# Don't use any older version except for compatibility
VERS=	3
# Which assembler module(s) to use
ASM=	i386
#ASM=
# Options
DEFS=	-DDEBUG

### The usual compiler, etc. definitions

CC=	gcc -pipe
# The kernel part needs -O due to inline code
OPT=	-O3
INCS=
# Where the _running_ kernel's includes are - no -I here
KINC=	/usr/src/linux/include
KDEFS=	-D__KERNEL__ -DMODULE -I$(KINC)
LDFLAGS=-s
LDRFLAGS=-r -S

# Define SMP or MODVERSIONS if and only if your kernel is also compiled
# with these options
#SMP=1
MODVERSIONS=1

BINDIR=	/usr/local/sbin
MODDIR=	/lib/modules/`uname -r`/misc
INFODIR=/usr/local/info

#TEX=	tex
TEX=	texinfo

### No user-serviceable parts below here

VERSION=1.0.1
CFG=-DProtocolVersion=$(VERS) -DCrypto_$(CRYPTO) -DVERSION=\"$(VERSION)\"
CFLAGS=	$(OPT) $(DEFS) $(ADEFS) $(CFG) -Wall -Wstrict-prototypes \
	-fomit-frame-pointer -finline-functions -funroll-loops

ifdef ASM
ifeq "$(strip $(CRYPTO))" "IDEA"
ADEFS=-DASM_Idea_Crypt
AOBJS=idea-$(strip $(ASM)).o
endif
ifeq "$(strip $(CRYPTO))" "Blowfish"
ADEFS=-DASM_BF_Crypt
AOBJS=bf-$(strip $(ASM)).o
endif
endif

ifeq "$(strip $(CRYPTO))" "IDEA"
CIPEM=	cip$(strip $(VERS))i.o
endif
ifeq "$(strip $(CRYPTO))" "Blowfish"
CIPEM=	cip$(strip $(VERS))b.o
endif

ifdef SMP
KDEFS+=-D__SMP__
endif
ifdef MODVERSIONS
KDEFS+=-DMODVERSIONS -include $(KINC)/linux/modversions.h
endif

all:	$(CIPEM) ciped

install: all
	-mkdir -p $(MODDIR) $(BINDIR) $(INFODIR)
	install -m 644 $(CIPEM) $(MODDIR)
	install -m 755 ciped $(BINDIR)
	install -m 644 cipe.info* $(INFODIR)
	-depmod -a

KOBJS=	device.o sock.o output.o encaps.o
ifeq "$(strip $(CRYPTO))" "IDEA"
KOBJS+=idea0.o
endif
ifeq "$(strip $(VERS))" "3"
COBJS=	crc32.o
else
COBJS=	crc.o
endif
OBJS=	ciped.o ioctl.o socks5.o

SRCS=	cipe.h crypto.h \
	ciped.c ciped.h ioctl.c ioctl.h socks5.c \
	device.c sock.c output.c encaps.c \
	idea0.h idea0.c bf.h crc.c crc32.c \
	idea-i386.S bf-i386.S \
	thruput.c Makefile cipe.lsm
SMPL=	samples/README samples/options samples/ip-up samples/ip-down
DIST=	README $(SRCS) $(SMPL) cipe.info tcpdump.patch COPYING

TEXAUX=cipe.cp cipe.fn cipe.ky cipe.pg cipe.tp cipe.vr

$(CIPEM): $(KOBJS) $(COBJS) $(AOBJS)
	ld $(LDRFLAGS) -o $(CIPEM) $(KOBJS) $(COBJS) $(AOBJS)

ciped:	$(OBJS) $(COBJS)
	$(CC) $(LDFLAGS) -o ciped $(OBJS) $(COBJS)

$(AOBJS): %.o: %.S
	$(CC) $(DEFS) $(ADEFS) $(KDEFS) -o $@ -c $<

$(KOBJS): %.o: %.c
	$(CC) $(CFLAGS) $(KINCS) $(KDEFS) -o $@ -c $<

$(OBJS) $(COBJS): %.o: %.c
	$(CC) $(CFLAGS) $(INCS) -o $@ -c $<

$(KOBJS:.o=.s) $(COBJS:.o=.s): %.s: %.c
	$(CC) $(CFLAGS) $(KINCS) $(KDEFS) -fverbose-asm \
	-o $@ -S $<

dvi: cipe.dvi

$(TEXAUX): cipe.texinfo
	$(TEX) cipe.texinfo

cipe.dvi: cipe.cp
	texindex $(TEXAUX)
	$(TEX) cipe.texinfo

thruput: thruput.c
	gcc -Wall -O3 -s -o thruput thruput.c

Checksums: $(DIST)
	makechecksums $(DIST) > Checksums

tgz:	$(DIST) Checksums
	cd .. && ln -sf cipe-linux cipe-$(VERSION) && \
		tar czf cipe-$(VERSION).tar.gz \
		$(DIST:%=cipe-$(VERSION)/%) cipe-$(VERSION)/Checksums

clean:
	rm -f core *.out *.tmp *.o *.s ciped thruput

realclean: clean
	rm -f *~ *.bak *.dvi *.aux *.log *.toc *.html $(TEXAUX) $(TEXAUX:%=%s)

# make depend does not account for everything included by crypto.h
depend: clean
	sed -e '/^\#\#\# Dependencies/q' Makefile > Make.tmp
	gcc -E -MM $(KINCS) $(KDEFS) $(CFG) $(KOBJS:.o=.c) >> Make.tmp
	gcc -E -MM $(INCS) $(CFG) $(OBJS:.o=.c) $(COBJS:.o=.c) >> Make.tmp
	mv -f Makefile Makefile~~
	mv -f Make.tmp Makefile

### Dependencies
device.o: device.c cipe.h crypto.h idea0.h bf.h
sock.o: sock.c cipe.h crypto.h idea0.h bf.h
output.o: output.c cipe.h crypto.h idea0.h bf.h
encaps.o: encaps.c cipe.h crypto.h idea0.h bf.h
ciped.o: ciped.c ciped.h cipe.h crypto.h idea0.h bf.h ioctl.h
ioctl.o: ioctl.c cipe.h crypto.h idea0.h bf.h ioctl.h
socks5.o: socks5.c ciped.h cipe.h crypto.h idea0.h bf.h
crc.o: crc.c
crc32.o: crc32.c
