xref: /freebsd/contrib/bmake/mk/cython.mk (revision 6a7405f5a6b639682cacf01e35d561411ff556aa)
1# SPDX-License-Identifier: BSD-2-Clause
2#
3# RCSid:
4#	$Id: cython.mk,v 1.10 2024/09/20 06:16:41 sjg Exp $
5#
6#	@(#) Copyright (c) 2014-2024, 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 ?= python3
27.if empty(PYTHON_VERSION)
28PYTHON_VERSION != ${PYTHON} --version | sed 's,Python \([2-9]\.[0-9][0-9]*\).*,\1,'
29.export PYTHON_VERSION
30.endif
31PYTHON_H ?= ${pyprefix}/include/python${PYTHON_VERSION}/Python.h
32PYVERSION := ${PYTHON_VERSION:C,\..*,,}
33
34CFLAGS+= -I${PYTHON_H:H}
35
36# conf.host_target() is limited to uname -m rather than uname -p
37# so we cannot assume it will always match HOST_TARGET
38_HOST_MACHINE != uname -m
39.if ${HOST_TARGET:M*${_HOST_MACHINE}} == ""
40PY_HOST_TARGET := ${HOST_TARGET:S,${_HOST_ARCH:U${uname -p:L:sh}}$,${_HOST_MACHINE},}
41.endif
42
43COMPILE.c ?= ${CC} -c ${CFLAGS}
44PICO ?= .pico
45
46.SUFFIXES: ${PICO} .c
47
48.c${PICO}:
49	${COMPILE.c} ${PICFLAG} ${CC_PIC} ${.IMPSRC} -o ${.TARGET}
50
51# this is what we build
52.if !empty(CYTHON_MODULE_NAME)
53CYTHON_MODULE = ${CYTHON_MODULE_NAME}${CYTHON_PYVERSION}.so
54
55CYTHON_SRCS ?= ${CYTHON_MODULE_NAME}.pyx
56
57# this is where we save generated src
58CYTHON_SAVEGENDIR ?= ${.CURDIR}/gen
59
60# set this empty if you don't want to handle multiple versions
61.if !defined(CYTHON_PYVERSION)
62CYTHON_PYVERSION := ${PYVERSION}
63.endif
64
65CYTHON_GENSRCS = ${CYTHON_SRCS:R:S,$,${CYTHON_PYVERSION}.c,}
66SRCS += ${CYTHON_GENSRCS}
67
68.SUFFIXES: .pyx .c .So
69
70CYTHON ?= ${pyprefix}/bin/cython
71
72# if we don't have cython we can use pre-generated srcs
73.if empty(CYTHON) || !exists(${CYTHON})
74.PATH: ${CYTHON_SAVEGENDIR}
75.else
76
77.if !empty(CYTHON_PYVERSION)
78.for c in ${CYTHON_SRCS}
79${c:R}${CYTHON_PYVERSION}.${c:E}: $c
80	ln -sf ${.ALLSRC:M*pyx} ${.TARGET}
81.endfor
82.endif
83
84.pyx.c:
85	${CYTHON} ${CYTHON_FLAGS} -${PYVERSION} -o ${.TARGET} ${.IMPSRC}
86
87
88save-gen: ${CYTHON_GENSRCS}
89	mkdir -p ${CYTHON_SAVEGENDIR}
90	cp -p ${.ALLSRC} ${CYTHON_SAVEGENDIR}
91
92.endif
93
94${CYTHON_MODULE}: ${SRCS:S,.c,${PICO},}
95	${CC} ${CC_SHARED:U-shared} -o ${.TARGET} ${.ALLSRC:M*${PICO}} ${LDADD}
96
97MODULE_BINDIR?= ${.CURDIR:H}/${PY_HOST_TARGET:U${HOST_TARGET}}
98
99build-cython-module: ${CYTHON_MODULE}
100
101install-cython-module: ${CYTHON_MODULE}
102	test -d ${DESTDIR}${MODULE_BINDIR} || \
103	${INSTALL} -d ${DESTDIR}${MODULE_BINDIR}
104	${INSTALL} -m 755 ${.ALLSRC} ${DESTDIR}${MODULE_BINDIR}
105
106CLEANFILES += *${PICO} ${CYTHON_MODULE}
107
108.endif
109