# Building
# ========
# 
# To build this you need bmake and mk files from
# http://www.crufty.net/ftp/pub/sjg/
# 
# On some platforms you may need to set ``PICFLAG=-fPIC``
# Otherwise building is simply a matter of::
#
# 	export MAKEOBJDIRPREFIX=/tmp
# 	bmake -m ~/share/mk obj
# 	bmake -m ~/share/mk install
#
# Note: if ``swig`` or ``cython`` are available they will be used, otherwise
# pre-generated files in ``./gen`` will be used.
#
# By default the Cython based module will be built appropriate to 
# PYTHON_VERSION (default is 2.7).
#
# If you want to build the swig based module (does not support Python
# 3.x) use::
#
# 	bmake -m ~/share/mk install-signer
#  
# You can also build a standalone signed (mainly suitable for testing)
# with::
#
# 	bmake -m ~/share/mk signer
# 
#

# RCSid:
#	$Id: Makefile,v 1.15 2013/12/15 07:39:19 sjg Exp $
#
#	@(#) Copyright (c) 2012 Simon J. Gerraty
#
#	This file is provided in the hope that it will
#	be of use.  There is absolutely NO WARRANTY.
#	Permission to copy, redistribute or otherwise
#	use this file is hereby granted provided that 
#	the above copyright notice and this notice are
#	left intact. 
#      
#	Please send copies of changes and bug-fixes to:
#	sjg@crufty.net

.MAIN: all

SRCS= signer.c

.-include "Makefile.local.inc"

PYTHON_VERSION?= 2.7
.if ${PYTHON_VERSION:M2*} != ""
CYTHON_PYVERSION=2
.else
CYTHON_PYVERSION=3
.endif

.if make(signer)
PROG= signer

CFLAGS+= -DMAIN

.elif make(*signer*)
# swig does not handle python3
all: _signer.so

SRCS+= signer_wrap.c

SWIG?= swig

.if ${type ${SWIG} 2> /dev/null || echo:L:sh:M/*} != ""
signer_wrap.c: signer.i
	${SWIG} -python -o $@ ${.ALLSRC}

signer.py: signer_wrap.c
.else
.PATH: ${.CURDIR}/gen
.endif

.else
all: ossl${CYTHON_PYVERSION}.so

SRCS+= ossl${CYTHON_PYVERSION}.c

CYTHON?= cython

.if ${type ${CYTHON} 2> /dev/null || echo:L:sh:M/*} != ""
ossl2.pyx ossl3.pyx:	ossl.pyx
	ln -sf ${.ALLSRC:M*pyx} ${.TARGET}

ossl3.c:	ossl3.pyx
	${CYTHON} ${CYTHON_FLAGS} -3 -o ${.TARGET} ${.ALLSRC:M*pyx}

ossl2.c:	ossl2.pyx
	${CYTHON} ${CYTHON_FLAGS} -2 -o ${.TARGET} ${.ALLSRC:M*pyx}

.else
.PATH: ${.CURDIR}/gen
.endif

install: install-ossl
.endif

.if exists(/usr/pkg/include)
prefix?= /usr/pkg
.endif
prefix?= /usr/local

PYTHON_H?= ${prefix}/include/python${PYTHON_VERSION}/Python.h

CFLAGS+= -I${PYTHON_H:H}

CFLAGS_MD=
CFLAGS_MF=

COMPILE.c?= ${CC} -c ${CFLAGS}

.SUFFIXES: .So .c

.c.So:
	${COMPILE.c} ${PICFLAG} ${CC_PIC} ${.IMPSRC} -o ${.TARGET}

_signer.so:	${SRCS:S,.c,.So,}
	${CC} -shared -o $@ ${.ALLSRC:M*.So} ${LDADD}

ossl${CYTHON_PYVERSION}.so:	${SRCS:S,.c,.So,}
	${CC} -shared -o $@ ${.ALLSRC:M*.So} ${LDADD}

# conf.host_target() is limited to uname -m rather than uname -p
_HOST_MACHINE!= uname -m
.if ${HOST_TARGET:M*${_HOST_MACHINE}} == ""
PY_HOST_TARGET:= ${HOST_TARGET:S,${_HOST_ARCH:U${uname -p:L:sh}}$,${_HOST_MACHINE},}
.endif
BINDIR=${.CURDIR:H}/${PY_HOST_TARGET:U${HOST_TARGET}}

install-signer:	_signer.so signer.py
	test -d ${DESTDIR}${BINDIR} || \
	${INSTALL} -d ${DESTDIR}${BINDIR}
	${INSTALL} -m 755 ${.ALLSRC:M*signer*} ${DESTDIR}${BINDIR}

savegen-signer:	signer_wrap.c signer.py
	cp ${.ALLSRC} ${.CURDIR}/gen


install-ossl:	ossl${CYTHON_PYVERSION}.so
	test -d ${DESTDIR}${BINDIR} || \
	${INSTALL} -d ${DESTDIR}${BINDIR}
	${INSTALL} -m 755 ${.ALLSRC:M*.so} ${DESTDIR}${BINDIR}

savegen-ossl:	ossl2.c ossl3.c
	cp ${.ALLSRC} ${.CURDIR}/gen

LDADD+= -lcrypto

CLEANFILES+= *.So

NOMAN=
NO_MAN=
NO_AUTODEP=

.include <prog.mk>

#CFLAGS+= -g -O0
#LDFLAGS+= -static
