1281760Ssjg# RCSid:
2281760Ssjg#	$Id: cython.mk,v 1.6 2014/10/15 06:23:51 sjg Exp $
3281760Ssjg#
4281760Ssjg#	@(#) Copyright (c) 2014, Simon J. Gerraty
5281760Ssjg#
6281760Ssjg#	This file is provided in the hope that it will
7281760Ssjg#	be of use.  There is absolutely NO WARRANTY.
8281760Ssjg#	Permission to copy, redistribute or otherwise
9281760Ssjg#	use this file is hereby granted provided that 
10281760Ssjg#	the above copyright notice and this notice are
11281760Ssjg#	left intact. 
12281760Ssjg#      
13281760Ssjg#	Please send copies of changes and bug-fixes to:
14281760Ssjg#	sjg@crufty.net
15281760Ssjg#
16281760Ssjg
17281760Ssjg# this is what we build
18281760SsjgCYTHON_MODULE = ${CYTHON_MODULE_NAME}${CYTHON_PYVERSION}.so
19281760Ssjg
20281760SsjgCYTHON_MODULE_NAME?= it
21281760SsjgCYTHON_SRCS?= ${CYTHON_MODULE_NAME}.pyx
22281760Ssjg
23281760Ssjg# this is where we save generated src
24281760SsjgCYTHON_SAVEGENDIR?= ${.CURDIR}/gen
25281760Ssjg
26281760Ssjg# pyprefix is where python bits are
27281760Ssjg# which may not be where we want to put ours (prefix)
28281760Ssjg.if exists(/usr/pkg/include)
29281760Ssjgpyprefix?= /usr/pkg
30281760Ssjg.endif
31281760Ssjgpyprefix?= /usr/local
32281760Ssjg
33281760SsjgPYTHON_VERSION?= 2.7
34281760SsjgPYTHON_H?= ${pyprefix}/include/python${PYTHON_VERSION}/Python.h
35281760SsjgPYVERSION:= ${PYTHON_VERSION:C,\..*,,}
36281760Ssjg
37281760Ssjg# set this empty if you don't want to handle multiple versions
38281760Ssjg.if !defined(CYTHON_PYVERSION)
39281760SsjgCYTHON_PYVERSION:= ${PYVERSION}
40281760Ssjg.endif
41281760Ssjg
42281760SsjgCFLAGS+= -I${PYTHON_H:H}
43281760Ssjg
44281760SsjgCYTHON_GENSRCS= ${CYTHON_SRCS:R:S,$,${CYTHON_PYVERSION}.c,}
45281760SsjgSRCS+= ${CYTHON_GENSRCS}
46281760Ssjg
47281760Ssjg.SUFFIXES: .pyx .c .So
48281760Ssjg
49281760SsjgCYTHON?= ${pyprefix}/bin/cython
50281760Ssjg
51281760Ssjg# if we don't have cython we can use pre-generated srcs
52281760Ssjg.if ${type ${CYTHON} 2> /dev/null || echo:L:sh:M/*} == ""
53281760Ssjg.PATH: ${CYTHON_SAVEGENDIR}
54281760Ssjg.else
55281760Ssjg
56281760Ssjg.if !empty(CYTHON_PYVERSION)
57281760Ssjg.for c in ${CYTHON_SRCS}
58281760Ssjg${c:R}${CYTHON_PYVERSION}.${c:E}: $c
59281760Ssjg	ln -sf ${.ALLSRC:M*pyx} ${.TARGET}
60281760Ssjg.endfor
61281760Ssjg.endif
62281760Ssjg
63281760Ssjg.pyx.c:
64281760Ssjg	${CYTHON} ${CYTHON_FLAGS} -${PYVERSION} -o ${.TARGET} ${.IMPSRC}
65281760Ssjg
66281760Ssjg
67281760Ssjgsave-gen: ${CYTHON_GENSRCS}
68281760Ssjg	mkdir -p ${CYTHON_SAVEGENDIR}
69281760Ssjg	cp -p ${.ALLSRC} ${CYTHON_SAVEGENDIR}
70281760Ssjg
71281760Ssjg.endif
72281760Ssjg
73281760SsjgCOMPILE.c?= ${CC} -c ${CFLAGS}
74281760Ssjg
75281760Ssjg.c.So:
76281760Ssjg	${COMPILE.c} ${PICFLAG} ${CC_PIC} ${.IMPSRC} -o ${.TARGET}
77281760Ssjg
78281760Ssjg${CYTHON_MODULE}: ${SRCS:S,.c,.So,}
79281760Ssjg	${CC} ${CC_SHARED:U-shared} -o ${.TARGET} ${.ALLSRC:M*.So} ${LDADD}
80281760Ssjg
81281760Ssjg# conf.host_target() is limited to uname -m rather than uname -p
82281760Ssjg_HOST_MACHINE!= uname -m
83281760Ssjg.if ${HOST_TARGET:M*${_HOST_MACHINE}} == ""
84281760SsjgPY_HOST_TARGET:= ${HOST_TARGET:S,${_HOST_ARCH:U${uname -p:L:sh}}$,${_HOST_MACHINE},}
85281760Ssjg.endif
86281760Ssjg
87281760SsjgMODULE_BINDIR?= ${.CURDIR:H}/${PY_HOST_TARGET:U${HOST_TARGET}}
88281760Ssjg
89281760Ssjgbuild-cython-module: ${CYTHON_MODULE}
90281760Ssjg
91281760Ssjginstall-cython-module: ${CYTHON_MODULE}
92281760Ssjg	test -d ${DESTDIR}${MODULE_BINDIR} || \
93281760Ssjg	${INSTALL} -d ${DESTDIR}${MODULE_BINDIR}
94281760Ssjg	${INSTALL} -m 755 ${.ALLSRC} ${DESTDIR}${MODULE_BINDIR}
95281760Ssjg
96281760SsjgCLEANFILES+= *.So ${CYTHON_MODULE}
97