Marcel Telka
2022-11-05 27f6d6d1fa9d5d945af9630fddf5100ee585376e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
 
#
# Copyright 2022 Marcel Telka
#
 
include $(WS_MAKE_RULES)/setup.py.mk
 
PYTHON_BOOTSTRAP ?= no
 
ifeq ($(strip $(PYTHON_BOOTSTRAP)),yes)
#
# Both 'build' and 'installer' Python modules used for packaging regular Python
# projects (see below) are not part of core Python, so we need to bootstrap
# them together with their non-core dependencies.
#
# There are basically two ways how to bootstrap such projects:
#
# 1) defer to default 'setup.py' build style for projects that still supports
# it.  The 'setup.py' build style usually does not require anything outside
# core Python modules.  Also, it is likely that the set of projects that needs
# to be bootstrapped (like setuptools) will support the 'setup.py' build style
# for very long time (maybe forever).  Anyway, the main problem with the
# 'setup.py' build style is that its results are not PEP 376 compliant.
#
# 2) use something else, preferably as simple as possible, to do the build.
# Such tool needs to be PEP 376 and PEP 517 compliant and it also must be able
# to build itself without requiring any other non-core Python modules.  We use
# pyproject_installer to do the job.
#
 
ifeq ($(strip $(COMPONENT_NAME)),pyproject_installer)
# To bootstrap the bootstrapper we need to make sure it finds its own modules
PYTHON_ENV += PYTHONPATH=$(@D)/src
endif
 
# Since pyproject_installer requires Python >= 3.8 we need to defer to default
# setup.py build style for Python 3.7, so we will use pyproject_installer for
# Python 3.9 only.  Once we obsolete Python 3.7 we should set
# COMPONENT_BUILD_CMD, COMPONENT_BUILD_ARGS, COMPONENT_INSTALL_CMD, and
# COMPONENT_INSTALL_ARGS unconditionally below and replace $(PYTHON.3.9) by
# $(PYTHON).
$(BUILD_DIR)/%-3.9/.built:    COMPONENT_BUILD_CMD =        $(PYTHON.3.9) -m pyproject_installer build
$(BUILD_DIR)/%-3.9/.built:    COMPONENT_BUILD_ARGS =
 
$(BUILD_DIR)/%-3.9/.installed:    COMPONENT_INSTALL_CMD =        $(PYTHON.3.9) -m pyproject_installer install
$(BUILD_DIR)/%-3.9/.installed:    COMPONENT_INSTALL_ARGS =
$(BUILD_DIR)/%-3.9/.installed:    COMPONENT_INSTALL_ARGS +=    --destdir $(PROTO_DIR)
 
# pyproject_installer does not bytecompile after the install.  Since we need
# pyc files we need to force that.
#
# Since pyproject_installer requires Python >= 3.8 we need to defer to default
# setup.py build style for Python 3.7, and so we do not need to compile for
# Python 3.7.  Once we obsolete Python 3.7 we should set
# COMPONENT_POST_INSTALL_ACTION unconditionally below.
$(BUILD_DIR)/%-3.9/.installed:    COMPONENT_POST_INSTALL_ACTION +=    $(PYTHON) -m compileall $(PROTO_DIR)/$(PYTHON_LIB) ;
 
# Special transforms needed only until we drop support for Python 3.7
PUBLISH_TRANSFORMS +=    $(WS_TOP)/transforms/python-bootstrap
else
COMPONENT_BUILD_CMD =        $(PYTHON) -m build
COMPONENT_BUILD_ARGS =
COMPONENT_BUILD_ARGS +=        --wheel
COMPONENT_BUILD_ARGS +=        --no-isolation
 
COMPONENT_INSTALL_CMD =        $(PYTHON) -m installer
COMPONENT_INSTALL_ARGS =
COMPONENT_INSTALL_ARGS +=    --destdir $(PROTO_DIR)
COMPONENT_INSTALL_ARGS +=    $(@D)/dist/*.whl
 
PYTHON_USERLAND_REQUIRED_PACKAGES += library/python/build
PYTHON_USERLAND_REQUIRED_PACKAGES += library/python/installer
endif
 
# Move all modules from default site-packages directory to vendor-packages
# directory where we place modules shipped by the OS but not included in the
# core Python distribution.
COMPONENT_POST_INSTALL_ACTION += \
    if [ -d $(PROTO_DIR)/$(PYTHON_DIR)/site-packages ] ; then \
        $(RM) -r $(PROTO_DIR)/$(PYTHON_LIB) ; \
        $(MV) $(PROTO_DIR)/$(PYTHON_DIR)/site-packages $(PROTO_DIR)/$(PYTHON_LIB) ; \
    fi ;
 
# Add build dependencies from project metadata to REQUIRED_PACKAGES
REQUIRED_PACKAGES_RESOLVED += $(BUILD_DIR)/META.depend.res
$(BUILD_DIR)/META.depend.res: $(SOURCE_DIR)/.prep
    $(MKDIR) $(BUILD_DIR)
    # PYTHON_ENV is needed here to have the PYTHONPATH set properly when we
    # bootstrap the pyproject_installer bootstrapper.
    #
    # To make the package names comparable we normalize them here by
    # following the PyPA Core metadata specifications and PEP 503.
    #
    # Once we obsolete Python 3.7 we should change $(PYTHON.3.9) to $(PYTHON) here
    $(PYTHON_ENV) $(PYTHON.3.9) -c ' \
            from pathlib import Path; \
            from pyproject_installer.build_cmd._build import parse_build_system_spec; \
            [print(x) for x in parse_build_system_spec(Path("'$(SOURCE_DIR)'"))["requires"]]' \
        | $(GSED) -e $$'s/^[ \t]*''\([a-zA-Z0-9]\([a-zA-Z0-9._-]*[a-zA-Z0-9]\)\{0,1\}\).*/\1/' \
        | tr [A-Z] [a-z] | $(GSED) -e 's/[.-_]\{1,\}/-/g' \
        | $(GSED) -e 's/.*/depend type=require fmri=pkg:\/library\/python\/&-$$(PYV)/' \
        > $@
 
# We need pyproject_installer for two purposes:
# - to detect build dependencies for all Python projects, and
# - to bootstrap some other Python projects.
# The pyproject_installer is not needed (and cannot be needed) for its own
# build.
ifneq ($(strip $(COMPONENT_NAME)),pyproject_installer)
# Once we obsolete Python 3.7 this should be changed to
# PYTHON_USERLAND_REQUIRED_PACKAGES and '-39' suffix should be removed
USERLAND_REQUIRED_PACKAGES += library/python/pyproject_installer-39
endif