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