# Change these for your platform
CLANG?=/usr/local/opt/llvm/bin/clang
CLANGCMD=${CLANG} -emit-llvm -DFULL_UNROLL
LLVM_LINK?=/usr/local/opt/llvm/bin/llvm-link
SAW?=../../bin/saw
DEPS?=../../deps
CRYPTOL?=${DEPS}/cryptol
CRYPTOLPATH?=${CRYPTOL}/examples

OPENSSL_VERSION?=1.1.0g
SRCDIR=openssl-${OPENSSL_VERSION}
TARBALL=${SRCDIR}.tar.gz

CFLAGS=-I${SRCDIR}/include \
       -I${SRCDIR}/crypto/aes

all: aes.log

${TARBALL}:
	wget http://openssl.org/source/${TARBALL}

${SRCDIR}/Configure: ${TARBALL}
	tar -xzvf ${TARBALL}

# Notes on "Configure":
#   * The "darwin64" choice is arbitrary. Other platforms should work.
#   * The "debug" portion avoids optimization. This is critical because
#     we currently don't support some instructions generated by the
#     optimizing build.
#   * The "no-asm" bit is crucial because we can't currently analyze asm.
${SRCDIR}/Makefile: ${SRCDIR}/Configure
	(cd ${SRCDIR} && CC="${CLANGCMD}" ./Configure debug-darwin64-x86_64-cc no-asm)

${SRCDIR}/crypto/aes/aes_core.o: ${SRCDIR}/Makefile
	(cd ${SRCDIR} && make CC="${CLANGCMD}" include/openssl/opensslconf.h crypto/aes/aes_core.o)

aes_wrap.bc: aes_wrap.c ${SRCDIR}/crypto/aes/aes_core.o
	${CLANGCMD} -c ${CFLAGS} -o $@ $<

aes_all.bc: aes_wrap.bc ${SRCDIR}/crypto/aes/aes_core.o
	${LLVM_LINK} -o $@ $^

export CRYPTOLPATH := ${CRYPTOLPATH}
aes.log: aes_all.bc aes.saw
	${SAW} aes.saw 2>&1 | tee $@

clean:
	rm -rf ${SRCDIR}
	rm -f aes_wrap.bc aes_all.bc
