comparison setup.py @ 586:d68e8204f7c3

Allow to build a distribution without C-extensions by using --only-pure
author Franz Glasner <fzglas.hg@dom66.de>
date Sun, 09 Jan 2022 13:46:57 +0100
parents 233bd8bbda28
children eac8e2d3933c
comparison
equal deleted inserted replaced
585:233bd8bbda28 586:d68e8204f7c3
60 wcp_idx = sys.argv.index("--windows-cross-pack") 60 wcp_idx = sys.argv.index("--windows-cross-pack")
61 except ValueError: 61 except ValueError:
62 wcp_idx = None 62 wcp_idx = None
63 else: 63 else:
64 del sys.argv[wcp_idx] 64 del sys.argv[wcp_idx]
65 try:
66 pure_only_idx = sys.argv.index("--pure-only")
67 except ValueError:
68 pure_only_idx = None
69 else:
70 del sys.argv[pure_only_idx]
65 71
66 72
67 # 73 #
68 # Otherwise some cached package_data would be used. 74 # Otherwise some cached package_data would be used.
69 # But our package data differs between "standard" builds and 75 # But our package data differs between "standard" builds and
71 # 77 #
72 if os.path.isdir(PROJECT_NAME + ".egg-info"): 78 if os.path.isdir(PROJECT_NAME + ".egg-info"):
73 print("removing `%s.egg-info'" % (PROJECT_NAME,)) 79 print("removing `%s.egg-info'" % (PROJECT_NAME,))
74 shutil.rmtree(PROJECT_NAME + ".egg-info") 80 shutil.rmtree(PROJECT_NAME + ".egg-info")
75 81
76 if wcp_idx is None: 82 if pure_only_idx is None:
77 # 83 if wcp_idx is None:
78 # Handle the optinal C-extension for Python3.7+ and CPython only. 84 #
79 # PyPy does not need this. 85 # Handle the optinal C-extension for Python3.7+ and CPython only.
80 # 86 # PyPy does not need this.
81 87 #
82 # The C-extension uses multi-phase module initialization (PEP 489, PY 3.5+) 88
83 if (platform.python_implementation() == "CPython" 89 # The C-extension uses multi-phase module initialization (PEP 489, PY 3.5+)
84 and (sys.version_info[0] > 3 90 if (platform.python_implementation() == "CPython"
85 or (sys.version_info[0] == 3 and sys.version_info[1] >= 5))): 91 and (sys.version_info[0] > 3
86 92 or (sys.version_info[0] == 3 and sys.version_info[1] >= 5))):
87 # The stable API for Python 3.7+ is used 93
88 if sys.version_info[0] == 3 and sys.version_info[1] < 7: 94 # The stable API for Python 3.7+ is used
89 py_limited_api = False 95 if sys.version_info[0] == 3 and sys.version_info[1] < 7:
90 else: 96 py_limited_api = False
91 py_limited_api = True 97 else:
92 98 py_limited_api = True
93 if py_limited_api: 99
94 define_macros = [("Py_LIMITED_API", "0x03070000")] 100 if py_limited_api:
95 else: 101 define_macros = [("Py_LIMITED_API", "0x03070000")]
96 define_macros = [] 102 else:
97 103 define_macros = []
98 try: 104
99 from setuptools import Extension 105 try:
100 except ImportError: 106 from setuptools import Extension
101 from distutils.core import Extension 107 except ImportError:
102 108 from distutils.core import Extension
103 ext_modules = [ 109
104 Extension( 110 ext_modules = [
105 name="configmix._speedups", 111 Extension(
106 sources=["configmix/_speedups.c"], 112 name="configmix._speedups",
107 define_macros=define_macros, 113 sources=["configmix/_speedups.c"],
108 py_limited_api=py_limited_api, 114 define_macros=define_macros,
109 optional=True 115 py_limited_api=py_limited_api,
110 ), 116 optional=True
111 ] 117 ),
118 ]
119
120 if py_limited_api:
121 #
122 # Build a wheel that is properly named using the stable API
123 #
124 try:
125 import wheel.bdist_wheel
126 except ImportError:
127 pass
128 else:
129 class BDistWheel(wheel.bdist_wheel.bdist_wheel):
130 def finalize_options(self):
131 # Synchronize this with Py_LIMITED_API
132 self.py_limited_api = 'cp37'
133 super().finalize_options()
134
135 cmdclass["bdist_wheel"] = BDistWheel
136 else:
137
138 if not os.path.isfile("configmix/_speedups.pyd"):
139 raise RuntimeError("no _speedups.pyd found")
140
141 setup_extra_kwds["package_data"] = {
142 "configmix": ["*.pyd"]
143 }
144
145 ext_modules = []
146
147 py_limited_api = True
112 148
113 if py_limited_api: 149 if py_limited_api:
114 # 150 #
115 # Build a wheel that is properly named using the stable API 151 # Build a wheel that is properly named using the stable API
116 # 152 #
119 except ImportError: 155 except ImportError:
120 pass 156 pass
121 else: 157 else:
122 class BDistWheel(wheel.bdist_wheel.bdist_wheel): 158 class BDistWheel(wheel.bdist_wheel.bdist_wheel):
123 def finalize_options(self): 159 def finalize_options(self):
124 # Synchronize this with Py_LIMITED_API 160 #
161 # Synchronize this with Py_LIMITED_API and with the
162 # external build of _speedups.pyd.
163 # Also use the --plat-name (-p) for tagging the Wheel
164 # properly (e.g. -p win_amd64).
165 #
125 self.py_limited_api = 'cp37' 166 self.py_limited_api = 'cp37'
126 super().finalize_options() 167 super().finalize_options()
127 168
128 cmdclass["bdist_wheel"] = BDistWheel 169 cmdclass["bdist_wheel"] = BDistWheel
129 else: 170
130 171 from setuptools.dist import Distribution
131 if not os.path.isfile("configmix/_speedups.pyd"): 172
132 raise RuntimeError("no _speedups.pyd found") 173 #
133 174 # Force a binary package. An empty ext_modules does not do this always.
134 setup_extra_kwds["package_data"] = { 175 # Tested with wheel v0.29.0
135 "configmix": ["*.pyd"] 176 #
136 } 177 class BinaryDistribution(Distribution):
137 178 """Distribution which always forces a binary package with
138 ext_modules = [] 179
139 180 platform name
140 py_limited_api = True 181
141 182 """
142 if py_limited_api: 183 def has_ext_modules(foo):
143 # 184 return True
144 # Build a wheel that is properly named using the stable API 185
145 # 186 setup_extra_kwds["distclass"] = BinaryDistribution
146 try: 187 else:
147 import wheel.bdist_wheel 188 #
148 except ImportError: 189 # pure
149 pass 190 #
150 else: 191 pass
151 class BDistWheel(wheel.bdist_wheel.bdist_wheel):
152 def finalize_options(self):
153 #
154 # Synchronize this with Py_LIMITED_API and with the
155 # external build of _speedups.pyd.
156 # Also use the --plat-name (-p) for tagging the Wheel
157 # properly (e.g. -p win_amd64).
158 #
159 self.py_limited_api = 'cp37'
160 super().finalize_options()
161
162 cmdclass["bdist_wheel"] = BDistWheel
163
164 from setuptools.dist import Distribution
165
166 #
167 # Force a binary package. An empty ext_modules does not do this always.
168 # Tested with wheel v0.29.0
169 #
170 class BinaryDistribution(Distribution):
171 """Distribution which always forces a binary package with
172
173 platform name
174
175 """
176 def has_ext_modules(foo):
177 return True
178
179 setup_extra_kwds["distclass"] = BinaryDistribution
180 192
181 193
182 if ext_modules is not None: 194 if ext_modules is not None:
183 setup_extra_kwds["ext_modules"] = ext_modules 195 setup_extra_kwds["ext_modules"] = ext_modules
184 setup_extra_kwds["zip_safe"] = False 196 setup_extra_kwds["zip_safe"] = False