# Copyright 2010, Your File System Inc
# All rights reserved
#
# This software has been released under the terms of the BSD License
# For details, see the LICENSE file in the top level source directory

# API version. Must be incremented whenever something changes.
LIBMAJOR=1
LIBMINOR=1

srcdir=@srcdir@
include @TOP_OBJDIR@/src/config/Makefile.config
include @TOP_OBJDIR@/src/config/Makefile.shared


HEADERS= ${TOP_INCDIR}/hcrypto/aes.h \
	 ${TOP_INCDIR}/hcrypto/des.h \
	 ${TOP_INCDIR}/hcrypto/engine.h \
	 ${TOP_INCDIR}/hcrypto/evp.h \
	 ${TOP_INCDIR}/hcrypto/evp-hcrypto.h \
	 ${TOP_INCDIR}/hcrypto/evp-cc.h \
	 ${TOP_INCDIR}/hcrypto/hmac.h \
	 ${TOP_INCDIR}/hcrypto/md2.h \
	 ${TOP_INCDIR}/hcrypto/md4.h \
	 ${TOP_INCDIR}/hcrypto/md5.h \
	 ${TOP_INCDIR}/hcrypto/rand.h \
	 ${TOP_INCDIR}/hcrypto/rc2.h \
	 ${TOP_INCDIR}/hcrypto/rc4.h \
	 ${TOP_INCDIR}/hcrypto/sha.h \
	 ${TOP_INCDIR}/hcrypto/ui.h

UPSTREAM= ${TOP_SRCDIR}/external/heimdal

MODULE_INCLUDE=-I${srcdir} -I${TOP_INCDIR}/hcrypto -I$(UPSTREAM)/hcrypto 

SHLIBOBJ= libafshcrypto.${SHLIB_SUFFIX}.${LIBMAJOR}.${LIBMINOR}

buildtools: ${HEADERS} ${TOP_LIBDIR}/libafshcrypto.a

all: ${HEADERS} ${TOP_LIBDIR}/${SHLIBOBJ} \
	${TOP_LIBDIR}/libafshcrypto.a \
	${TOP_LIBDIR}/libafshcrypto_lwp.a

install: libafshcrypto.a ${SHLIBOBJ}
	${TOP_OBJDIR}/src/config/shlib-install -d ${DESTDIR}${libdir} \
	        -l libafshcrypto -M ${LIBMAJOR} -m ${LIBMINOR}
	${INSTALL_DATA} libafshcrypto.a ${DESTDIR}${libdir}/libafshcrypto.a

dest: libafshcrypto.a ${SHLIBOBJ}
	${TOP_OBJDIR}/src/config/shlib-install -d ${DEST}/lib \
	        -l libafshcrypto -M ${LIBMAJOR} -m ${LIBMINOR}
	${INSTALL_DATA} libafshcrypto.a ${DEST}/lib/libafshcrypto.a

clean:
	$(RM) -f ${OBJECTS} ${SHLIBOBJ} rand-fortuna_lwp.o \
		libafshcrypto.a libafshcrypto_lwp.a libafshcrypto.exp \
		test_cipher test_cipher.o

COMMON_OBJS= aes.o camellia.o camellia-ntt.o des.o engine.o evp.o \
	     evp-hcrypto.o evp-cc.o hmac.o md2.o md4.o md5.o pkcs5.o \
	     rand-egd.o rand-timer.o rand-unix.o rand.o rc2.o rc4.o \
	     rijndael-alg-fst.o rnd_keys.o sha.o sha256.o sha512.o ui.o \
	     validate.o

OBJECTS = $(COMMON_OBJS) rand-fortuna.o

LWP_OBJS = $(COMMON_OBJS) rand-fortuna_lwp.o

${TOP_LIBDIR}/${SHLIBOBJ}: ${SHLIBOBJ}
	${TOP_OBJDIR}/src/config/shlib-install -d ${TOP_LIBDIR} \
	        -l libafshcrypto -M ${LIBMAJOR} -m ${LIBMINOR}

${SHLIBOBJ}: ${OBJECTS} libafshcrypto.map
	${TOP_OBJDIR}/src/config/shlib-build -d $(srcdir) -l libafshcrypto \
		-M ${LIBMAJOR} -m ${LIBMINOR} -- \
		${OBJECTS} $(LDFLAGS_roken) $(LIB_roken) ${MT_LIBS}

libafshcrypto.a: ${OBJECTS}
	$(RM) -f $@
	$(AR) crv $@ ${OBJECTS}
	$(RANLIB) $@

libafshcrypto_lwp.a: $(LWP_OBJS)
	$(RM) -f $@
	$(AR) crv $@ $(LWP_OBJS)
	$(RANLIB) $@

test_cipher: test_cipher.o libafshcrypto.a
	$(AFS_LDRULE) test_cipher.o libafshcrypto.a $(LIB_roken)

${TOP_INCDIR}/hcrypto/aes.h: ${UPSTREAM}/hcrypto/aes.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/hcrypto/bn.h: ${UPSTREAM}/hcrypto/bn.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/hcrypto/des.h: ${UPSTREAM}/hcrypto/des.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/hcrypto/dh.h: ${UPSTREAM}/hcrypto/dh.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/hcrypto/dsa.h: ${UPSTREAM}/hcrypto/dsa.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/hcrypto/engine.h: engine.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/hcrypto/evp.h: ${UPSTREAM}/hcrypto/evp.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/hcrypto/evp-hcrypto.h: ${UPSTREAM}/hcrypto/evp-hcrypto.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/hcrypto/evp-cc.h: ${UPSTREAM}/hcrypto/evp-cc.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/hcrypto/hmac.h: ${UPSTREAM}/hcrypto/hmac.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/hcrypto/md2.h: ${UPSTREAM}/hcrypto/md2.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/hcrypto/md4.h: ${UPSTREAM}/hcrypto/md4.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/hcrypto/md5.h: ${UPSTREAM}/hcrypto/md5.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/hcrypto/pkcs12.h: ${UPSTREAM}/hcrypto/pkcs12.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/hcrypto/rand.h: ${UPSTREAM}/hcrypto/rand.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/hcrypto/rc2.h: ${UPSTREAM}/hcrypto/rc2.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/hcrypto/rc4.h: ${UPSTREAM}/hcrypto/rc4.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/hcrypto/rsa.h: ${UPSTREAM}/hcrypto/rsa.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/hcrypto/sha.h: ${UPSTREAM}/hcrypto/sha.h
	${INSTALL_DATA} $? $@

${TOP_INCDIR}/hcrypto/ui.h: ${UPSTREAM}/hcrypto/ui.h
	${INSTALL_DATA} $? $@

${TOP_LIBDIR}/libafshcrypto.a: libafshcrypto.a
	${INSTALL_DATA} $? $@

${TOP_LIBDIR}/libafshcrypto_lwp.a: libafshcrypto_lwp.a
	${INSTALL_DATA} $? $@

# Pthread Targets

CFLAGS_test_cipher.o= -I${UPSTREAM}/roken/
CFLAGS_rand-unix.o=@CFLAGS_NOERROR@
CFLAGS_evp.o=-DHAVE_CONFIG_H

aes.o: ${UPSTREAM}/hcrypto/aes.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/aes.c

bn.o: ${UPSTREAM}/hcrypto/bn.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/bn.c

camellia.o: ${UPSTREAM}/hcrypto/camellia.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/camellia.c

camellia-ntt.o: ${UPSTREAM}/hcrypto/camellia-ntt.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/camellia-ntt.c

des.o: ${UPSTREAM}/hcrypto/des.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/des.c
	
evp.o: ${UPSTREAM}/hcrypto/evp.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/evp.c
	
evp-hcrypto.o: ${UPSTREAM}/hcrypto/evp-hcrypto.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/evp-hcrypto.c
	
evp-cc.o: ${UPSTREAM}/hcrypto/evp-cc.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/evp-cc.c

hmac.o: ${UPSTREAM}/hcrypto/hmac.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/hmac.c

md2.o: ${UPSTREAM}/hcrypto/md2.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/md2.c

md4.o: ${UPSTREAM}/hcrypto/md4.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/md4.c

md5.o: ${UPSTREAM}/hcrypto/md5.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/md5.c

pkcs5.o: ${UPSTREAM}/hcrypto/pkcs5.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/pkcs5.c

rand-egd.o: ${UPSTREAM}/hcrypto/rand-egd.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/rand-egd.c

rand-fortuna.o: ${UPSTREAM}/hcrypto/rand-fortuna.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/rand-fortuna.c

rand-timer.o: ${UPSTREAM}/hcrypto/rand-timer.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/rand-timer.c

rand-unix.o: ${UPSTREAM}/hcrypto/rand-unix.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/rand-unix.c

rand.o: ${UPSTREAM}/hcrypto/rand.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/rand.c

rc2.o: ${UPSTREAM}/hcrypto/rc2.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/rc2.c

rc4.o: ${UPSTREAM}/hcrypto/rc4.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/rc4.c

rijndael-alg-fst.o: ${UPSTREAM}/hcrypto/rijndael-alg-fst.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/rijndael-alg-fst.c

rnd_keys.o: ${UPSTREAM}/hcrypto/rnd_keys.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/rnd_keys.c

sha.o: ${UPSTREAM}/hcrypto/sha.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/sha.c

sha256.o: ${UPSTREAM}/hcrypto/sha256.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/sha256.c

sha512.o: ${UPSTREAM}/hcrypto/sha512.c
	${AFS_CCRULE} $(UPSTREAM)/hcrypto/sha512.c

ui.o: ${UPSTREAM}/hcrypto/ui.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/ui.c

validate.o: ${UPSTREAM}/hcrypto/validate.c
	${AFS_CCRULE} ${UPSTREAM}/hcrypto/validate.c

test_cipher.o: ${UPSTREAM}/hcrypto/test_cipher.c
	$(AFS_CCRULE) $(UPSTREAM)/hcrypto/test_cipher.c

# LWP Targets

rand-fortuna_lwp.o: ${UPSTREAM}/hcrypto/rand-fortuna.c
	$(LWP_CCRULE) $(UPSTREAM)/hcrypto/rand-fortuna.c

