xref: /freebsd/contrib/bmake/mk/cython.mk (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
1# RCSid:
2#	$Id: cython.mk,v 1.8 2020/08/19 17:51:53 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# pyprefix is where python bits are
18# which may not be where we want to put ours (prefix)
19.if exists(/usr/pkg/include)
20pyprefix?= /usr/pkg
21.endif
22pyprefix?= /usr/local
23
24PYTHON_VERSION?= 2.7
25PYTHON_H?= ${pyprefix}/include/python${PYTHON_VERSION}/Python.h
26PYVERSION:= ${PYTHON_VERSION:C,\..*,,}
27
28CFLAGS+= -I${PYTHON_H:H}
29
30# conf.host_target() is limited to uname -m rather than uname -p
31_HOST_MACHINE!= uname -m
32.if ${HOST_TARGET:M*${_HOST_MACHINE}} == ""
33PY_HOST_TARGET:= ${HOST_TARGET:S,${_HOST_ARCH:U${uname -p:L:sh}}$,${_HOST_MACHINE},}
34.endif
35
36COMPILE.c?= ${CC} -c ${CFLAGS}
37PICO?= .pico
38
39.SUFFIXES: ${PICO} .c
40
41.c${PICO}:
42	${COMPILE.c} ${PICFLAG} ${CC_PIC} ${.IMPSRC} -o ${.TARGET}
43
44# this is what we build
45.if !empty(CYTHON_MODULE_NAME)
46CYTHON_MODULE = ${CYTHON_MODULE_NAME}${CYTHON_PYVERSION}.so
47
48CYTHON_SRCS?= ${CYTHON_MODULE_NAME}.pyx
49
50# this is where we save generated src
51CYTHON_SAVEGENDIR?= ${.CURDIR}/gen
52
53# set this empty if you don't want to handle multiple versions
54.if !defined(CYTHON_PYVERSION)
55CYTHON_PYVERSION:= ${PYVERSION}
56.endif
57
58CYTHON_GENSRCS= ${CYTHON_SRCS:R:S,$,${CYTHON_PYVERSION}.c,}
59SRCS+= ${CYTHON_GENSRCS}
60
61.SUFFIXES: .pyx .c .So
62
63CYTHON?= ${pyprefix}/bin/cython
64
65# if we don't have cython we can use pre-generated srcs
66.if ${type ${CYTHON} 2> /dev/null || echo:L:sh:M/*} == ""
67.PATH: ${CYTHON_SAVEGENDIR}
68.else
69
70.if !empty(CYTHON_PYVERSION)
71.for c in ${CYTHON_SRCS}
72${c:R}${CYTHON_PYVERSION}.${c:E}: $c
73	ln -sf ${.ALLSRC:M*pyx} ${.TARGET}
74.endfor
75.endif
76
77.pyx.c:
78	${CYTHON} ${CYTHON_FLAGS} -${PYVERSION} -o ${.TARGET} ${.IMPSRC}
79
80
81save-gen: ${CYTHON_GENSRCS}
82	mkdir -p ${CYTHON_SAVEGENDIR}
83	cp -p ${.ALLSRC} ${CYTHON_SAVEGENDIR}
84
85.endif
86
87${CYTHON_MODULE}: ${SRCS:S,.c,${PICO},}
88	${CC} ${CC_SHARED:U-shared} -o ${.TARGET} ${.ALLSRC:M*${PICO}} ${LDADD}
89
90MODULE_BINDIR?= ${.CURDIR:H}/${PY_HOST_TARGET:U${HOST_TARGET}}
91
92build-cython-module: ${CYTHON_MODULE}
93
94install-cython-module: ${CYTHON_MODULE}
95	test -d ${DESTDIR}${MODULE_BINDIR} || \
96	${INSTALL} -d ${DESTDIR}${MODULE_BINDIR}
97	${INSTALL} -m 755 ${.ALLSRC} ${DESTDIR}${MODULE_BINDIR}
98
99CLEANFILES+= *${PICO} ${CYTHON_MODULE}
100
101.endif
102