# $Id$
#
# Makefile for low-level crypto library

CC=@CC@
CPP=@CPP@
RANLIB=@RANLIB@
AR=ar

# Reset VPATH
SRCDIR=@srcdir@
VPATH=$(SRCDIR):$(EXTRA_VPATH)

# The flags to generate a shared library
CPPFLAGS=$(PREFLAGS) $(DEFS) $(EXTRA_CPPFLAGS)
LDFLAGS=@LDFLAGS@

default: algorithms.a

### Magic Makefile for descore

# Interesting defines are sparc, mc68000, vax and i386
# Rely on gcc defining them appropriately.
# CPPFLAGS=	-Dsparc		# use 6+8 general regs
# CPPFLAGS=	-Dmc68000	# use 3+4 addr (1+4 live), and 3+3 data regs
# CPPFLAGS=	-Dvax		# use 6+0 general regs
# CPPFLAGS=	-Di386		# use 3+0 regs, and 3+0 normal variables

CODEGEN.c=	$(CC) $(CFLAGS) $(CPPFLAGS) -S
COMPILE.c=	$(CC) $(CFLAGS) $(CPPFLAGS) -c
LINK.c=		$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)

# hand-entered files that go into the library
SC=		desKerb.c desUtil.c desQuick.c
SO=		desKerb.o desUtil.o desQuick.o

# special generated files
GH=		parity.h rotors.h keymap.h
GC=		desSmallFips.c desSmallCore.c desQuickFips.c desQuickCore.c
GI=		desSmallFips.i desSmallCore.i desQuickFips.i desQuickCore.i
GO=		desSmallFips.o desSmallCore.o desQuickFips.o desQuickCore.o

O=		$(SO) $(GO)

# prefer compilation from .i if .i exists
#.SUFFIXES:
#.SUFFIXES:	.i .o .c $(SUFFIXES)

desTest:	desCore.a desTest.o
		$(LINK.c) -o $@ desTest.o desCore.a
		./desTest

# test all performance enhancement flags
sure:
		make clean ; make desTest 'CPPFLAGS=-Di386 -Umc68000 -Usparc'
		make clean ; make desTest 'CPPFLAGS=-Dvax -Umc68000 -Usparc'
		make clean ; make desTest 'CPPFLAGS=-Dmc68000 -Usparc'
		make clean ; make desTest 'CPPFLAGS=-Dsparc   -Umc68000'
		make clean ; make desTest

clean:
	-rm -f *.o *.i *.x *.a ./desTest
	-rm -f $(GC) $(GH) desdata

depend:

desCore.a:	$O
		$(AR) cru $@ $O
		$(RANLIB) $@

desdata.o:	desinfo.h $(SRCDIR)/../include/des.h
desUtil.o:	$(GH)
$(SO):		$(SRCDIR)/../include/des.h
$(GI):		desCode.h $(SRCDIR)/../include/des.h Makefile
desTest.o:	$(SRCDIR)/../include/des.h

$(GH):		desdata
		./desdata $@ > $@

desdata:	desdata.o
		$(LINK.c) -o $@ desdata.o

# new rules  (note: tr|sed|tr is NOT necessary,  just there so .i is readable)
.c.i:
	$(CPP) $(CFLAGS) $(CPPFLAGS) $< > $*.x
	@tr ';'\\012 \\012';' < $*.x |			\
	 sed	-e 's/[ 	][ 	]*/ /g'		\
		-e 's/^ //'				\
		-e 's/ $$//'				\
		-e '/^$$/d'				\
		-e '/^[^;]/s/^/;/'			\
		-e 's/#[^;]*;//g'			\
		-e 's/\([){]\) *\(register\)/\1;\2/g'	\
		-e 's/\([[(]\) /\1/g'			\
		-e 's/ \([])]\)/\1/g'			\
		-e 's/\([^]+0123 ]\) =/\1  =/g'		\
		-e 's/}/};;/g'				\
		-e 's/ *; */;/g'			\
		-e 's/;;;*/;;/g'			\
		-e '1s/^;*//' |				\
	 tr ';'\\012 \\012';' > $@
	@echo ""  >> $@
#	@echo "}" >> $@			# last definition must be a procedure

#		-e 's/\(;[kmxyz][0-9]*\)\([^;]*=\)/\1  \2/g'

.i.o:
		$(CODEGEN.c) $<
		$(COMPILE.c) $*.s

# slowest to quickest
desSmallFips.c:
		@echo '#include "desCode.h"' > $@
		@echo \
'ENCRYPT(DesSmallFipsEncrypt,TEMPSMALL,LOADFIPS,KEYMAPSMALL,SAVEFIPS)' >> $@
		@echo \
'DECRYPT(DesSmallFipsDecrypt,TEMPSMALL,LOADFIPS,KEYMAPSMALL,SAVEFIPS)' >> $@
desSmallCore.c:
		@echo '#include "desCode.h"' > $@
		@echo \
'ENCRYPT(DesSmallCoreEncrypt,TEMPSMALL,LOADCORE,KEYMAPSMALL,SAVECORE)' >> $@
		@echo \
'DECRYPT(DesSmallCoreDecrypt,TEMPSMALL,LOADCORE,KEYMAPSMALL,SAVECORE)' >> $@
desQuickFips.c:
		@echo '#include "desCode.h"' > $@
		@echo \
'ENCRYPT(DesQuickFipsEncrypt,TEMPQUICK,LOADFIPS,KEYMAPQUICK,SAVEFIPS)' >> $@
		@echo \
'DECRYPT(DesQuickFipsDecrypt,TEMPQUICK,LOADFIPS,KEYMAPQUICK,SAVEFIPS)' >> $@
desQuickCore.c:
		@echo '#include "desCode.h"' > $@
		@echo \
'ENCRYPT(DesQuickCoreEncrypt,TEMPQUICK,LOADCORE,KEYMAPQUICK,SAVECORE)' >> $@
		@echo \
'DECRYPT(DesQuickCoreDecrypt,TEMPQUICK,LOADCORE,KEYMAPQUICK,SAVECORE)' >> $@

### End of rules for desCore

SRCS = sha.c md5.c idea.c rc4.c cast.c $(SC) $(GC)
OBJS = $(SRCS:.c=.o)

algorithms.a: $(OBJS)
	rm -f algorithms.a
	$(AR) cru algorithms.a $(OBJS)
	$(RANLIB) algorithms.a

#### Remaking the Makefile and configure scripts. ####

#${srcdir}/configure: configure.in aclocal.m4
${srcdir}/configure: configure.in
	cd ${srcdir} && autoconf

# autoheader might not change config.h.in, so touch a stamp file.
${srcdir}/config.h.in: stamp-h.in

#${srcdir}/stamp-h.in: configure.in aclocal.m4 acconfig.h \
#    config.h.top config.h.bot

${srcdir}/stamp-h.in: configure.in 
	cd ${srcdir} && autoheader
	echo timestamp > ${srcdir}/stamp-h.in

config.h: stamp-h

stamp-h: config.h.in config.status
	./config.status

Makefile: Makefile.in config.status
	./config.status

config.status: configure
	./config.status --recheck

%.d: %.c
	$(SHELL) -ec '$(CC) -MM -MG $(CPPFLAGS) $(DEFS) $< \
		| sed '\''s/\($*\)\.o:/\1\.o $@ : /g'\'' > $@'

include $(SRCS:.c=.d)
