blob: 2a8743fdee0a2e26bc60eef968e30b4b7ea74f46 [file] [log] [blame]
David Lord5c6fcad2020-05-23 20:08:57 -07001import os
David Lord337a79f2020-01-29 20:13:47 -08002import platform
David Lordb41c96e2018-11-03 15:26:05 -07003import sys
4from distutils.errors import CCompilerError
5from distutils.errors import DistutilsExecError
6from distutils.errors import DistutilsPlatformError
7
8from setuptools import Extension
David Lordb41c96e2018-11-03 15:26:05 -07009from setuptools import setup
David Lordfe626812018-05-02 13:47:27 -070010from setuptools.command.build_ext import build_ext
Armin Ronacher115ba372010-06-22 19:21:32 +020011
David Lordb41c96e2018-11-03 15:26:05 -070012ext_modules = [Extension("markupsafe._speedups", ["src/markupsafe/_speedups.c"])]
Armin Ronacher5bda5222010-06-22 19:33:18 +020013
14
15class BuildFailed(Exception):
16 pass
17
18
19class ve_build_ext(build_ext):
20 """This class allows C extension building to fail."""
21
22 def run(self):
23 try:
24 build_ext.run(self)
David Lordfe626812018-05-02 13:47:27 -070025 except DistutilsPlatformError:
Armin Ronacher5bda5222010-06-22 19:33:18 +020026 raise BuildFailed()
27
28 def build_extension(self, ext):
29 try:
30 build_ext.build_extension(self, ext)
David Lordfe626812018-05-02 13:47:27 -070031 except (CCompilerError, DistutilsExecError, DistutilsPlatformError):
Armin Ronacher5bda5222010-06-22 19:33:18 +020032 raise BuildFailed()
Christoph Zwerschke796b2ea2011-06-05 13:02:02 +020033 except ValueError:
34 # this can happen on Windows 64 bit, see Python issue 7511
David Lordb41c96e2018-11-03 15:26:05 -070035 if "'path'" in str(sys.exc_info()[1]): # works with Python 2 and 3
Christoph Zwerschke4964e772011-06-04 19:28:56 +020036 raise BuildFailed()
37 raise
Armin Ronacher5bda5222010-06-22 19:33:18 +020038
39
40def run_setup(with_binary):
Armin Ronacher5bda5222010-06-22 19:33:18 +020041 setup(
David Lordb41c96e2018-11-03 15:26:05 -070042 name="MarkupSafe",
David Lordb41c96e2018-11-03 15:26:05 -070043 cmdclass={"build_ext": ve_build_ext},
David Lordfe626812018-05-02 13:47:27 -070044 ext_modules=ext_modules if with_binary else [],
Armin Ronacher5bda5222010-06-22 19:33:18 +020045 )
46
47
David Lordfe626812018-05-02 13:47:27 -070048def show_message(*lines):
David Lordb41c96e2018-11-03 15:26:05 -070049 print("=" * 74)
David Lordfe626812018-05-02 13:47:27 -070050 for line in lines:
51 print(line)
David Lordb41c96e2018-11-03 15:26:05 -070052 print("=" * 74)
Armin Ronacher515ec272011-07-20 09:51:43 +020053
54
David Lord5c6fcad2020-05-23 20:08:57 -070055if os.environ.get("CIBUILDWHEEL", "0") == "1":
56 run_setup(True)
57elif platform.python_implementation() not in {"PyPy", "Jython"}:
David Lordfe626812018-05-02 13:47:27 -070058 try:
59 run_setup(True)
60 except BuildFailed:
61 show_message(
David Lordb41c96e2018-11-03 15:26:05 -070062 "WARNING: The C extension could not be compiled, speedups"
63 " are not enabled.",
64 "Failure information, if any, is above.",
65 "Retrying the build without the C extension now.",
David Lordfe626812018-05-02 13:47:27 -070066 )
67 run_setup(False)
68 show_message(
David Lordb41c96e2018-11-03 15:26:05 -070069 "WARNING: The C extension could not be compiled, speedups"
70 " are not enabled.",
71 "Plain-Python build succeeded.",
David Lordfe626812018-05-02 13:47:27 -070072 )
Armin Ronacher515ec272011-07-20 09:51:43 +020073else:
Armin Ronacher98caea12011-07-20 10:57:46 +020074 run_setup(False)
David Lordfe626812018-05-02 13:47:27 -070075 show_message(
David Lordb41c96e2018-11-03 15:26:05 -070076 "WARNING: C extensions are not supported on this Python"
77 " platform, speedups are not enabled.",
78 "Plain-Python build succeeded.",
David Lordfe626812018-05-02 13:47:27 -070079 )