David Lord | 337a79f | 2020-01-29 20:13:47 -0800 | [diff] [blame] | 1 | import platform |
David Lord | b41c96e | 2018-11-03 15:26:05 -0700 | [diff] [blame] | 2 | import sys |
| 3 | from distutils.errors import CCompilerError |
| 4 | from distutils.errors import DistutilsExecError |
| 5 | from distutils.errors import DistutilsPlatformError |
| 6 | |
| 7 | from setuptools import Extension |
David Lord | b41c96e | 2018-11-03 15:26:05 -0700 | [diff] [blame] | 8 | from setuptools import setup |
David Lord | fe62681 | 2018-05-02 13:47:27 -0700 | [diff] [blame] | 9 | from setuptools.command.build_ext import build_ext |
Armin Ronacher | 115ba37 | 2010-06-22 19:21:32 +0200 | [diff] [blame] | 10 | |
David Lord | b41c96e | 2018-11-03 15:26:05 -0700 | [diff] [blame] | 11 | ext_modules = [Extension("markupsafe._speedups", ["src/markupsafe/_speedups.c"])] |
Armin Ronacher | 5bda522 | 2010-06-22 19:33:18 +0200 | [diff] [blame] | 12 | |
| 13 | |
| 14 | class BuildFailed(Exception): |
| 15 | pass |
| 16 | |
| 17 | |
| 18 | class 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 Lord | fe62681 | 2018-05-02 13:47:27 -0700 | [diff] [blame] | 24 | except DistutilsPlatformError: |
Armin Ronacher | 5bda522 | 2010-06-22 19:33:18 +0200 | [diff] [blame] | 25 | raise BuildFailed() |
| 26 | |
| 27 | def build_extension(self, ext): |
| 28 | try: |
| 29 | build_ext.build_extension(self, ext) |
David Lord | fe62681 | 2018-05-02 13:47:27 -0700 | [diff] [blame] | 30 | except (CCompilerError, DistutilsExecError, DistutilsPlatformError): |
Armin Ronacher | 5bda522 | 2010-06-22 19:33:18 +0200 | [diff] [blame] | 31 | raise BuildFailed() |
Christoph Zwerschke | 796b2ea | 2011-06-05 13:02:02 +0200 | [diff] [blame] | 32 | except ValueError: |
| 33 | # this can happen on Windows 64 bit, see Python issue 7511 |
David Lord | b41c96e | 2018-11-03 15:26:05 -0700 | [diff] [blame] | 34 | if "'path'" in str(sys.exc_info()[1]): # works with Python 2 and 3 |
Christoph Zwerschke | 4964e77 | 2011-06-04 19:28:56 +0200 | [diff] [blame] | 35 | raise BuildFailed() |
| 36 | raise |
Armin Ronacher | 5bda522 | 2010-06-22 19:33:18 +0200 | [diff] [blame] | 37 | |
| 38 | |
| 39 | def run_setup(with_binary): |
Armin Ronacher | 5bda522 | 2010-06-22 19:33:18 +0200 | [diff] [blame] | 40 | setup( |
David Lord | b41c96e | 2018-11-03 15:26:05 -0700 | [diff] [blame] | 41 | name="MarkupSafe", |
David Lord | b41c96e | 2018-11-03 15:26:05 -0700 | [diff] [blame] | 42 | cmdclass={"build_ext": ve_build_ext}, |
David Lord | fe62681 | 2018-05-02 13:47:27 -0700 | [diff] [blame] | 43 | ext_modules=ext_modules if with_binary else [], |
Armin Ronacher | 5bda522 | 2010-06-22 19:33:18 +0200 | [diff] [blame] | 44 | ) |
| 45 | |
| 46 | |
David Lord | fe62681 | 2018-05-02 13:47:27 -0700 | [diff] [blame] | 47 | def show_message(*lines): |
David Lord | b41c96e | 2018-11-03 15:26:05 -0700 | [diff] [blame] | 48 | print("=" * 74) |
David Lord | fe62681 | 2018-05-02 13:47:27 -0700 | [diff] [blame] | 49 | for line in lines: |
| 50 | print(line) |
David Lord | b41c96e | 2018-11-03 15:26:05 -0700 | [diff] [blame] | 51 | print("=" * 74) |
Armin Ronacher | 515ec27 | 2011-07-20 09:51:43 +0200 | [diff] [blame] | 52 | |
| 53 | |
David Lord | 337a79f | 2020-01-29 20:13:47 -0800 | [diff] [blame] | 54 | if platform.python_implementation() not in {"PyPy", "Jython"}: |
David Lord | fe62681 | 2018-05-02 13:47:27 -0700 | [diff] [blame] | 55 | try: |
| 56 | run_setup(True) |
| 57 | except BuildFailed: |
| 58 | show_message( |
David Lord | b41c96e | 2018-11-03 15:26:05 -0700 | [diff] [blame] | 59 | "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 Lord | fe62681 | 2018-05-02 13:47:27 -0700 | [diff] [blame] | 63 | ) |
| 64 | run_setup(False) |
| 65 | show_message( |
David Lord | b41c96e | 2018-11-03 15:26:05 -0700 | [diff] [blame] | 66 | "WARNING: The C extension could not be compiled, speedups" |
| 67 | " are not enabled.", |
| 68 | "Plain-Python build succeeded.", |
David Lord | fe62681 | 2018-05-02 13:47:27 -0700 | [diff] [blame] | 69 | ) |
Armin Ronacher | 515ec27 | 2011-07-20 09:51:43 +0200 | [diff] [blame] | 70 | else: |
Armin Ronacher | 98caea1 | 2011-07-20 10:57:46 +0200 | [diff] [blame] | 71 | run_setup(False) |
David Lord | fe62681 | 2018-05-02 13:47:27 -0700 | [diff] [blame] | 72 | show_message( |
David Lord | b41c96e | 2018-11-03 15:26:05 -0700 | [diff] [blame] | 73 | "WARNING: C extensions are not supported on this Python" |
| 74 | " platform, speedups are not enabled.", |
| 75 | "Plain-Python build succeeded.", |
David Lord | fe62681 | 2018-05-02 13:47:27 -0700 | [diff] [blame] | 76 | ) |