xref: /freebsd/contrib/bmake/mk/cython.mk (revision 6ae1554a5d9b318f8ad53ccc39fa5a961403da73)
1# RCSid:
2#	$Id: cython.mk,v 1.6 2014/10/15 06:23:51 sjg Exp $
3#
4#	@(#) Copyright (c) 2014, Simon J. Gerraty
5#
6#	This file is provided in the hope that it will
7#	be of use.  There is absolutely NO WARRANTY.
8#	Permission to copy, redistribute or otherwise
9#	use this file is hereby granted provided that
10#	the above copyright notice and this notice are
11#	left intact.
12#
13#	Please send copies of changes and bug-fixes to:
14#	sjg@crufty.net
15#
16
17# this is what we build
18CYTHON_MODULE = ${CYTHON_MODULE_NAME}${CYTHON_PYVERSION}.so
19
20CYTHON_MODULE_NAME?= it
21CYTHON_SRCS?= ${CYTHON_MODULE_NAME}.pyx
22
23# this is where we save generated src
24CYTHON_SAVEGENDIR?= ${.CURDIR}/gen
25
26# pyprefix is where python bits are
27# which may not be where we want to put ours (prefix)
28.if exists(/usr/pkg/include)
29pyprefix?= /usr/pkg
30.endif
31pyprefix?= /usr/local
32
33PYTHON_VERSION?= 2.7
34PYTHON_H?= ${pyprefix}/include/python${PYTHON_VERSION}/Python.h
35PYVERSION:= ${PYTHON_VERSION:C,\..*,,}
36
37# set this empty if you don't want to handle multiple versions
38.if !defined(CYTHON_PYVERSION)
39CYTHON_PYVERSION:= ${PYVERSION}
40.endif
41
42CFLAGS+= -I${PYTHON_H:H}
43
44CYTHON_GENSRCS= ${CYTHON_SRCS:R:S,$,${CYTHON_PYVERSION}.c,}
45SRCS+= ${CYTHON_GENSRCS}
46
47.SUFFIXES: .pyx .c .So
48
49CYTHON?= ${pyprefix}/bin/cython
50
51# if we don't have cython we can use pre-generated srcs
52.if ${type ${CYTHON} 2> /dev/null || echo:L:sh:M/*} == ""
53.PATH: ${CYTHON_SAVEGENDIR}
54.else
55
56.if !empty(CYTHON_PYVERSION)
57.for c in ${CYTHON_SRCS}
58${c:R}${CYTHON_PYVERSION}.${c:E}: $c
59	ln -sf ${.ALLSRC:M*pyx} ${.TARGET}
60.endfor
61.endif
62
63.pyx.c:
64	${CYTHON} ${CYTHON_FLAGS} -${PYVERSION} -o ${.TARGET} ${.IMPSRC}
65
66
67save-gen: ${CYTHON_GENSRCS}
68	mkdir -p ${CYTHON_SAVEGENDIR}
69	cp -p ${.ALLSRC} ${CYTHON_SAVEGENDIR}
70
71.endif
72
73COMPILE.c?= ${CC} -c ${CFLAGS}
74
75.c.So:
76	${COMPILE.c} ${PICFLAG} ${CC_PIC} ${.IMPSRC} -o ${.TARGET}
77
78${CYTHON_MODULE}: ${SRCS:S,.c,.So,}
79	${CC} ${CC_SHARED:U-shared} -o ${.TARGET} ${.ALLSRC:M*.So} ${LDADD}
80
81# conf.host_target() is limited to uname -m rather than uname -p
82_HOST_MACHINE!= uname -m
83.if ${HOST_TARGET:M*${_HOST_MACHINE}} == ""
84PY_HOST_TARGET:= ${HOST_TARGET:S,${_HOST_ARCH:U${uname -p:L:sh}}$,${_HOST_MACHINE},}
85.endif
86
87MODULE_BINDIR?= ${.CURDIR:H}/${PY_HOST_TARGET:U${HOST_TARGET}}
88
89build-cython-module: ${CYTHON_MODULE}
90
91install-cython-module: ${CYTHON_MODULE}
92	test -d ${DESTDIR}${MODULE_BINDIR} || \
93	${INSTALL} -d ${DESTDIR}${MODULE_BINDIR}
94	${INSTALL} -m 755 ${.ALLSRC} ${DESTDIR}${MODULE_BINDIR}
95
96CLEANFILES+= *.So ${CYTHON_MODULE}
97