|
| 1 | +import os |
| 2 | +import sys |
| 3 | + |
| 4 | + |
| 5 | +from distutils.core import Extension |
| 6 | + |
| 7 | +from distutils.errors import (CCompilerError, DistutilsExecError, |
| 8 | + DistutilsPlatformError) |
| 9 | +from distutils.command.build_ext import build_ext |
| 10 | + |
| 11 | + |
| 12 | +# C Extensions |
| 13 | +with_extensions = os.getenv('PENDULUM_EXTENSIONS', None) |
| 14 | + |
| 15 | +if with_extensions == '1' or with_extensions is None: |
| 16 | + with_extensions = True |
| 17 | + |
| 18 | +if with_extensions == '0' or hasattr(sys, 'pypy_version_info'): |
| 19 | + with_extensions = False |
| 20 | + |
| 21 | +extensions = [] |
| 22 | +if with_extensions: |
| 23 | + extensions = [ |
| 24 | + Extension('pendulum._extensions._helpers', |
| 25 | + ['pendulum/_extensions/_helpers.c']), |
| 26 | + ] |
| 27 | + |
| 28 | + |
| 29 | +class BuildFailed(Exception): |
| 30 | + |
| 31 | + pass |
| 32 | + |
| 33 | + |
| 34 | +class ExtBuilder(build_ext): |
| 35 | + # This class allows C extension building to fail. |
| 36 | + |
| 37 | + def run(self): |
| 38 | + try: |
| 39 | + build_ext.run(self) |
| 40 | + except (DistutilsPlatformError, FileNotFoundError): |
| 41 | + raise BuildFailed() |
| 42 | + |
| 43 | + def build_extension(self, ext): |
| 44 | + try: |
| 45 | + build_ext.build_extension(self, ext) |
| 46 | + except (CCompilerError, DistutilsExecError, |
| 47 | + DistutilsPlatformError, ValueError): |
| 48 | + raise BuildFailed() |
| 49 | + |
| 50 | + |
| 51 | +def build(setup_kwargs): |
| 52 | + """ |
| 53 | + This function is mandatory in order to build the extensions. |
| 54 | + """ |
| 55 | + setup_kwargs.update({ |
| 56 | + 'ext_modules': extensions, |
| 57 | + 'cmdclass': { |
| 58 | + 'build_ext': ExtBuilder |
| 59 | + } |
| 60 | + }) |
0 commit comments