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