Skip to content

Commit daa4b93

Browse files
authored
Merge pull request #488 from sdispater/improve-c-extensions-build
Improve C extensions building
2 parents 81a7a8b + a2267c0 commit daa4b93

File tree

4 files changed

+323
-276
lines changed

4 files changed

+323
-276
lines changed

.github/workflows/tests.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
- name: Install dependencies
5252
run: |
5353
source $HOME/.poetry/env
54-
poetry install
54+
poetry install -vvv
5555
- name: Test Pure Python
5656
run: |
5757
source $HOME/.poetry/env
@@ -67,7 +67,7 @@ jobs:
6767
runs-on: macos-latest
6868
strategy:
6969
matrix:
70-
python-version: [2.7, 3.5, 3.6, 3.7, 3.8, pypy2, pypy3]
70+
python-version: [2.7, 3.5, 3.6, 3.7, 3.8, pypy3]
7171

7272
steps:
7373
- uses: actions/checkout@v2
@@ -97,7 +97,7 @@ jobs:
9797
- name: Install dependencies
9898
run: |
9999
source $HOME/.poetry/env
100-
poetry install
100+
poetry install -vvv
101101
- name: Test Pure Python
102102
run: |
103103
source $HOME/.poetry/env
@@ -142,7 +142,7 @@ jobs:
142142
- name: Install dependencies
143143
run: |
144144
$env:Path += ";$env:Userprofile\.poetry\bin"
145-
poetry install
145+
poetry install -vvv
146146
- name: Test Pure Python
147147
run: |
148148
$env:Path += ";$env:Userprofile\.poetry\bin"

build.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
2+
import shutil
23
import sys
34

45
from distutils.command.build_ext import build_ext
6+
from distutils.core import Distribution
57
from distutils.core import Extension
68
from distutils.errors import CCompilerError
79
from distutils.errors import DistutilsExecError
@@ -33,27 +35,53 @@ class BuildFailed(Exception):
3335
class ExtBuilder(build_ext):
3436
# This class allows C extension building to fail.
3537

38+
built_extensions = []
39+
3640
def run(self):
3741
try:
3842
build_ext.run(self)
3943
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+
)
4348

4449
def build_extension(self, ext):
4550
try:
4651
build_ext.build_extension(self, ext)
4752
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+
)
5159

5260

5361
def build(setup_kwargs):
5462
"""
5563
This function is mandatory in order to build the extensions.
5664
"""
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

Comments
 (0)