1
1
import pathlib
2
2
import re
3
3
import shutil
4
- import setuptools # type: ignore
5
- from setuptools .command import build_ext # type: ignore
6
4
import subprocess
7
5
import sys
8
6
7
+ import setuptools # type: ignore
8
+ from setuptools .command import build_ext # type: ignore
9
+ from setuptools .extern .packaging import version # type: ignore
10
+
9
11
10
12
__version__ = "1.4.2"
11
13
@@ -17,8 +19,6 @@ def __init__(self, name):
17
19
18
20
class CMakeBuild (build_ext .build_ext ):
19
21
def run (self ):
20
- from packaging import version # type: ignore
21
-
22
22
if not self .inplace :
23
23
try :
24
24
out = subprocess .check_output (["cmake" , "--version" ])
@@ -28,9 +28,10 @@ def run(self):
28
28
+ ", " .join (e .name for e in self .extensions )
29
29
)
30
30
31
- cmake_version = version .Version (
32
- re .search (r"version\s*([\d.]+)" , out .decode ()).group (1 )
33
- )
31
+ m = re .search (r"version\s*([\d.]+)" , out .decode ())
32
+ if m is None :
33
+ raise RuntimeError ("Could not find CMake version." )
34
+ cmake_version = version .Version (m .group (1 ))
34
35
if cmake_version < version .Version ("3.13.0" ):
35
36
raise RuntimeError (
36
37
"CMake >= 3.13.0 is required. Install the latest CMake with 'pip install cmake'."
@@ -40,11 +41,19 @@ def run(self):
40
41
self .build_extension (extension )
41
42
42
43
def build_extension (self , extension : setuptools .Extension ):
43
- extension_dir = pathlib .Path (self .get_ext_fullpath (extension .name )).parent
44
- extension_dir .mkdir (parents = True , exist_ok = True )
44
+ extension_dir = pathlib .Path (
45
+ self .get_ext_fullpath (extension .name )
46
+ ).parent .absolute ()
47
+
48
+ # Clean old build.
49
+ for old_build in extension_dir .glob (
50
+ "*.dylib" if sys .platform == "darwin" else "*.so"
51
+ ):
52
+ old_build .unlink ()
45
53
54
+ # Create new build folder.
46
55
if self .inplace :
47
- build_dir = extension_dir / "build"
56
+ build_dir = ( pathlib . Path ( __file__ ). parent / "build" ). absolute ()
48
57
else :
49
58
build_dir = pathlib .Path (self .build_temp )
50
59
build_dir .mkdir (parents = True , exist_ok = True )
@@ -66,6 +75,7 @@ def build_extension(self, extension: setuptools.Extension):
66
75
"-DCMAKE_INSTALL_PREFIX=install" ,
67
76
"-DCMAKE_INSTALL_RPATH=" + rpath_origin ,
68
77
]
78
+ print (* cmake_command )
69
79
self .spawn (cmake_command )
70
80
71
81
# Build and install.
0 commit comments