xref: /OK3568_Linux_fs/yocto/poky/meta/classes/python_pep517.bbclass (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun# Common infrastructure for Python packages that use PEP-517 compliant packaging.
2*4882a593Smuzhiyun# https://www.python.org/dev/peps/pep-0517/
3*4882a593Smuzhiyun#
4*4882a593Smuzhiyun# This class will build a wheel in do_compile, and use pypa/installer to install
5*4882a593Smuzhiyun# it in do_install.
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunDEPENDS:append = " python3-installer-native"
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun# Where to execute the build process from
10*4882a593SmuzhiyunPEP517_SOURCE_PATH ?= "${S}"
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun# The PEP517 build API entry point
13*4882a593SmuzhiyunPEP517_BUILD_API ?= "unset"
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun# The directory where wheels will be written
16*4882a593SmuzhiyunPEP517_WHEEL_PATH ?= "${WORKDIR}/dist"
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun# The interpreter to use for installed scripts
19*4882a593SmuzhiyunPEP517_INSTALL_PYTHON = "python3"
20*4882a593SmuzhiyunPEP517_INSTALL_PYTHON:class-native = "nativepython3"
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun# pypa/installer option to control the bytecode compilation
23*4882a593SmuzhiyunINSTALL_WHEEL_COMPILE_BYTECODE ?= "--compile-bytecode=0"
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun# PEP517 doesn't have a specific configure step, so set an empty do_configure to avoid
26*4882a593Smuzhiyun# running base_do_configure.
27*4882a593Smuzhiyunpython_pep517_do_configure () {
28*4882a593Smuzhiyun    :
29*4882a593Smuzhiyun}
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun# When we have Python 3.11 we can parse pyproject.toml to determine the build
32*4882a593Smuzhiyun# API entry point directly
33*4882a593Smuzhiyunpython_pep517_do_compile () {
34*4882a593Smuzhiyun    cd ${PEP517_SOURCE_PATH}
35*4882a593Smuzhiyun    nativepython3 -c "import ${PEP517_BUILD_API} as api; api.build_wheel('${PEP517_WHEEL_PATH}')"
36*4882a593Smuzhiyun}
37*4882a593Smuzhiyundo_compile[cleandirs] += "${PEP517_WHEEL_PATH}"
38*4882a593Smuzhiyun
39*4882a593Smuzhiyunpython_pep517_do_install () {
40*4882a593Smuzhiyun    COUNT=$(find ${PEP517_WHEEL_PATH} -name '*.whl' | wc -l)
41*4882a593Smuzhiyun    if test $COUNT -eq 0; then
42*4882a593Smuzhiyun        bbfatal No wheels found in ${PEP517_WHEEL_PATH}
43*4882a593Smuzhiyun    elif test $COUNT -gt 1; then
44*4882a593Smuzhiyun        bbfatal More than one wheel found in ${PEP517_WHEEL_PATH}, this should not happen
45*4882a593Smuzhiyun    fi
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun    nativepython3 -m installer ${INSTALL_WHEEL_COMPILE_BYTECODE} --interpreter "${USRBINPATH}/env ${PEP517_INSTALL_PYTHON}" --destdir=${D} ${PEP517_WHEEL_PATH}/*.whl
48*4882a593Smuzhiyun}
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun# A manual do_install that just uses unzip for bootstrapping purposes. Callers should DEPEND on unzip-native.
51*4882a593Smuzhiyunpython_pep517_do_bootstrap_install () {
52*4882a593Smuzhiyun    install -d ${D}${PYTHON_SITEPACKAGES_DIR}
53*4882a593Smuzhiyun    unzip -d ${D}${PYTHON_SITEPACKAGES_DIR} ${PEP517_WHEEL_PATH}/*.whl
54*4882a593Smuzhiyun}
55*4882a593Smuzhiyun
56*4882a593SmuzhiyunEXPORT_FUNCTIONS do_configure do_compile do_install
57