# 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 is available it will be used to generate signer_wrap.c
# and signer.py, otherwise pregenerated files in ./gen will be used.
#
# You can also build a standalone signed (mainly suitable for testing)
# with::
#
# 	bmake -m ~/share/mk signer
# 
#

# RCSid:
#	$Id: Makefile,v 1.10 2013/07/18 20:40:25 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

.if make(signer)
PROG= signer

SRCS= signer.c
CFLAGS+= -DMAIN


.else
all: _signer.so

SRCS= signer.c 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

.-include "Makefile.local.inc"

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

PYTHON_H ?= ${prefix}/include/python2.7/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}

# 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.so signer.py
	test -d ${DESTDIR}${BINDIR} || \
	${INSTALL} -d ${DESTDIR}${BINDIR}
	${INSTALL} -m 755 ${.ALLSRC:M*signer*} ${DESTDIR}${BINDIR}

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

.endif

LDADD+= -lcrypto

CLEANFILES+= *.So

NOMAN=
NO_MAN=
NO_AUTODEP=

.include <prog.mk>

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