Files
python-bash-builtins/build.sh
2025-06-01 05:06:52 -07:00

40 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
# maybe uses non-posix regex and thus may be non-portable. oops.
# the bash regex implementation is actually platform dependent, lol.
shopt -s extglob
PY_VERSION=3.13
PYFLAGS="$(/usr/bin/python${PY_VERSION}-config --cflags --ldflags)"
BASH_INCLUDE_DIR="/usr/include/bash"
function translate_function_names() {
# cython generates garbled names like __pyx_pf_7py_test_2foo for a python function named foo
# this function generates a list of #defines that translate these names to what they are in python
file="${1}"
grep 'static PyObject \*__pyx_pf_' "${file}" | grep -v 'proto' | while read -r line; do
desired_name=$"${line#*$"${file/.c/}"_}"
desired_name="${desired_name/#+([0-9])/}"
real_name="${line#*\*}"
echo "#define ${desired_name%(*} ${real_name%(*}"
done
}
py2c() {
rm "${1/.py/.c}"
cython3 -3 "${1}" -o "${1/.py/.c}"
translate_function_names $(basename ${1/.py/.c}) >> "${1/.py/.c}"
}
compile() {
gcc -I$BASH_INCLUDE_DIR{/include,/builtins,}\
$PYFLAGS $1 -o ${1/.c/} -Wall\
-lpython3.13 \
-fPIC -shared -O3
}
py2c foo.py
compile example_builtin.c