|
1 | 1 | import os
|
| 2 | +import shutil |
2 | 3 | import sys
|
3 | 4 |
|
4 | 5 | from distutils.command.build_ext import build_ext
|
| 6 | +from distutils.core import Distribution |
5 | 7 | from distutils.core import Extension
|
6 | 8 | from distutils.errors import CCompilerError
|
7 | 9 | from distutils.errors import DistutilsExecError
|
@@ -33,27 +35,53 @@ class BuildFailed(Exception):
|
33 | 35 | class ExtBuilder(build_ext):
|
34 | 36 | # This class allows C extension building to fail.
|
35 | 37 |
|
| 38 | + built_extensions = [] |
| 39 | + |
36 | 40 | def run(self):
|
37 | 41 | try:
|
38 | 42 | build_ext.run(self)
|
39 | 43 | except (DistutilsPlatformError, FileNotFoundError):
|
40 |
| - print("************************************************************") |
41 |
| - print("Cannot compile C accelerator module, use pure python version") |
42 |
| - print("************************************************************") |
| 44 | + print( |
| 45 | + " Unable to build the C extensions, " |
| 46 | + "Pendulum will use the pure python code instead." |
| 47 | + ) |
43 | 48 |
|
44 | 49 | def build_extension(self, ext):
|
45 | 50 | try:
|
46 | 51 | build_ext.build_extension(self, ext)
|
47 | 52 | except (CCompilerError, DistutilsExecError, DistutilsPlatformError, ValueError):
|
48 |
| - print("************************************************************") |
49 |
| - print("Cannot compile C accelerator module, use pure python version") |
50 |
| - print("************************************************************") |
| 53 | + print( |
| 54 | + ' Unable to build the "{}" C extension, ' |
| 55 | + "Pendulum will use the pure python version of the extension.".format( |
| 56 | + ext.name |
| 57 | + ) |
| 58 | + ) |
51 | 59 |
|
52 | 60 |
|
53 | 61 | def build(setup_kwargs):
|
54 | 62 | """
|
55 | 63 | This function is mandatory in order to build the extensions.
|
56 | 64 | """
|
57 |
| - setup_kwargs.update( |
58 |
| - {"ext_modules": extensions, "cmdclass": {"build_ext": ExtBuilder}} |
59 |
| - ) |
| 65 | + distribution = Distribution({"name": "pendulum", "ext_modules": extensions}) |
| 66 | + distribution.package_dir = "pendulum" |
| 67 | + |
| 68 | + cmd = ExtBuilder(distribution) |
| 69 | + cmd.ensure_finalized() |
| 70 | + cmd.run() |
| 71 | + |
| 72 | + # Copy built extensions back to the project |
| 73 | + for output in cmd.get_outputs(): |
| 74 | + relative_extension = os.path.relpath(output, cmd.build_lib) |
| 75 | + if not os.path.exists(output): |
| 76 | + continue |
| 77 | + |
| 78 | + shutil.copyfile(output, relative_extension) |
| 79 | + mode = os.stat(relative_extension).st_mode |
| 80 | + mode |= (mode & 0o444) >> 2 |
| 81 | + os.chmod(relative_extension, mode) |
| 82 | + |
| 83 | + return setup_kwargs |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + build({}) |
0 commit comments