blob: 9ef2acd436f7b0f1ece46953122a6ff7dfdd789b [file] [log] [blame]
David Lord337a79f2020-01-29 20:13:47 -08001import platform
David Lordb41c96e2018-11-03 15:26:05 -07002import sys
3from distutils.errors import CCompilerError
4from distutils.errors import DistutilsExecError
5from distutils.errors import DistutilsPlatformError
6
7from setuptools import Extension
David Lordb41c96e2018-11-03 15:26:05 -07008from setuptools import setup
David Lordfe626812018-05-02 13:47:27 -07009from setuptools.command.build_ext import build_ext
Armin Ronacher115ba372010-06-22 19:21:32 +020010
David Lordb41c96e2018-11-03 15:26:05 -070011ext_modules = [Extension("markupsafe._speedups", ["src/markupsafe/_speedups.c"])]
Armin Ronacher5bda5222010-06-22 19:33:18 +020012
13
14class BuildFailed(Exception):
15 pass
16
17
18class ve_build_ext(build_ext):
19 """This class allows C extension building to fail."""
20
21 def run(self):
22 try:
23 build_ext.run(self)
David Lordfe626812018-05-02 13:47:27 -070024 except DistutilsPlatformError:
Armin Ronacher5bda5222010-06-22 19:33:18 +020025 raise BuildFailed()
26
27 def build_extension(self, ext):
28 try:
29 build_ext.build_extension(self, ext)
David Lordfe626812018-05-02 13:47:27 -070030 except (CCompilerError, DistutilsExecError, DistutilsPlatformError):
Armin Ronacher5bda5222010-06-22 19:33:18 +020031 raise BuildFailed()
Christoph Zwerschke796b2ea2011-06-05 13:02:02 +020032 except ValueError:
33 # this can happen on Windows 64 bit, see Python issue 7511
David Lordb41c96e2018-11-03 15:26:05 -070034 if "'path'" in str(sys.exc_info()[1]): # works with Python 2 and 3
Christoph Zwerschke4964e772011-06-04 19:28:56 +020035 raise BuildFailed()
36 raise
Armin Ronacher5bda5222010-06-22 19:33:18 +020037
38
39def run_setup(with_binary):
Armin Ronacher5bda5222010-06-22 19:33:18 +020040 setup(
David Lordb41c96e2018-11-03 15:26:05 -070041 name="MarkupSafe",
David Lordb41c96e2018-11-03 15:26:05 -070042 cmdclass={"build_ext": ve_build_ext},
David Lordfe626812018-05-02 13:47:27 -070043 ext_modules=ext_modules if with_binary else [],
Armin Ronacher5bda5222010-06-22 19:33:18 +020044 )
45
46
David Lordfe626812018-05-02 13:47:27 -070047def show_message(*lines):
David Lordb41c96e2018-11-03 15:26:05 -070048 print("=" * 74)
David Lordfe626812018-05-02 13:47:27 -070049 for line in lines:
50 print(line)
David Lordb41c96e2018-11-03 15:26:05 -070051 print("=" * 74)
Armin Ronacher515ec272011-07-20 09:51:43 +020052
53
David Lord337a79f2020-01-29 20:13:47 -080054if platform.python_implementation() not in {"PyPy", "Jython"}:
David Lordfe626812018-05-02 13:47:27 -070055 try:
56 run_setup(True)
57 except BuildFailed:
58 show_message(
David Lordb41c96e2018-11-03 15:26:05 -070059 "WARNING: The C extension could not be compiled, speedups"
60 " are not enabled.",
61 "Failure information, if any, is above.",
62 "Retrying the build without the C extension now.",
David Lordfe626812018-05-02 13:47:27 -070063 )
64 run_setup(False)
65 show_message(
David Lordb41c96e2018-11-03 15:26:05 -070066 "WARNING: The C extension could not be compiled, speedups"
67 " are not enabled.",
68 "Plain-Python build succeeded.",
David Lordfe626812018-05-02 13:47:27 -070069 )
Armin Ronacher515ec272011-07-20 09:51:43 +020070else:
Armin Ronacher98caea12011-07-20 10:57:46 +020071 run_setup(False)
David Lordfe626812018-05-02 13:47:27 -070072 show_message(
David Lordb41c96e2018-11-03 15:26:05 -070073 "WARNING: C extensions are not supported on this Python"
74 " platform, speedups are not enabled.",
75 "Plain-Python build succeeded.",
David Lordfe626812018-05-02 13:47:27 -070076 )