Setuptools

Setuptools is a set of tools and modules for building python modules from python and C/C++ source and packaging them to be distributed with pip. This requires a special project layout with a setup.py file at the base of the build directory, defining rules for the build. Setuptools provides a few overridable commands; you can find a list of them in the Python docs.

First, install the tools:

pip install setuptools

Setuptools has many options for configuring builds for cross compilation by passing arguments to its build_ext stage. Here is a template for cross-compiling C/C++ python bindings with setuptools using a combination of environment variables and command line arguments to setup.py:

# CFLAGS, CPUVARDIR, and CPU are all set by recurse.mk. If not building with QNX recursive make as a wrapper, provide your own values for these
export CC=${QNX_HOST}/usr/bin/qcc
export CXX=${QNX_HOST}/usr/bin/q++
export CFLAGS="-Vgcc_nto${CPUVARDIR}"
export CPPFLAGS="-D_QNX_SOURCE -D_POSIX_THREADS "
export CXXFLAGS=${CFLAGS}
export LDSHARED=${QNX_HOST}/usr/bin/qcc
export LDFLAGS="-shared -L${QNX_TARGET}/${CPUVARDIR}/lib:${QNX_TARGET}/${CPUVARDIR}/usr/lib"
export host_alias=nto${CPUVARDIR}
export AR="${QNX_HOST}/usr/bin/nto${CPU}-ar"
export AS="$(QNX_HOST)/usr/bin/nto${CPU}-as"
export RANLIB="${QNX_HOST}/usr/bin/nto${CPU}-ranlib"
export LD_LIBRARY_PATH=${QNX_HOST}/usr/lib:${LD_LIBRARY_PATH}
# This already comes from sourcing qnxsdp-env.sh
export PATH=${QNX_HOST}/usr/bin:${PATH}

# Accessed from setuptools.Extension.include_dirs
BUILD_EXT_FLAGS = -I"${QNX_TARGET}/usr/include:${QNX_TARGET}/usr/include/python3.11:${QNX_TARGET}/${CPUVARDIR}/usr/include:${QNX_TARGET}/${CPUVARDIR}/usr/include/python3.11:${QNX_TARGET}/usr/include/${CPUVARDIR}/python3.11"

# Set the build directory
BUILD_FLAGS =  --build-temp=${PWD}/build/tmp --build-lib=${PWD}/build/lib

mkdir -p build
python ./setup.py build_ext ${BUILD_EXT_FLAGS} build ${BUILD_FLAGS}

Note that any stage in the build can be overridden to use an project specific implementation, and you should inspect the documentation and source code to see if any arguments don't apply to the command or if additional arguments are required.

Page updated: