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