forked from metaopt/torchopt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
144 lines (117 loc) · 4.38 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
import pathlib
import platform
import re
import shutil
import sys
import sysconfig
from setuptools import setup
try:
from pybind11.setup_helpers import Pybind11Extension as Extension
from pybind11.setup_helpers import build_ext
except ImportError:
from setuptools import Extension
from setuptools.command.build_ext import build_ext
HERE = pathlib.Path(__file__).absolute().parent
VERSION_FILE = HERE / 'torchopt' / 'version.py'
sys.path.insert(0, str(VERSION_FILE.parent))
import version # noqa
class CMakeExtension(Extension):
def __init__(self, name, source_dir='.', target=None, **kwargs):
super().__init__(name, sources=[], **kwargs)
self.source_dir = os.path.abspath(source_dir)
self.target = target if target is not None else name.rpartition('.')[-1]
class cmake_build_ext(build_ext):
def build_extension(self, ext):
if not isinstance(ext, CMakeExtension):
super().build_extension(ext)
return
from torch.utils import cpp_extension
cmake = shutil.which('cmake')
if cmake is None:
raise RuntimeError('Cannot find CMake executable.')
ext_path = pathlib.Path(self.get_ext_fullpath(ext.name)).absolute()
build_temp = pathlib.Path(self.build_temp).absolute()
build_temp.mkdir(parents=True, exist_ok=True)
config = 'Debug' if self.debug else 'Release'
cmake_args = [
f'-DCMAKE_BUILD_TYPE={config}',
f'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{config.upper()}={ext_path.parent}',
f'-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_{config.upper()}={build_temp}',
f'-DPYTHON_EXECUTABLE={sys.executable}',
f'-DPYTHON_INCLUDE_DIR={sysconfig.get_path("platinclude")}',
f'-DTORCH_INCLUDE_PATH={";".join(cpp_extension.include_paths())}',
f'-DTORCH_LIBRARY_PATH={";".join(cpp_extension.library_paths())}',
]
if platform.system() == 'Darwin':
# Cross-compile support for macOS - respect ARCHFLAGS if set
archs = re.findall(r'-arch (\S+)', os.environ.get('ARCHFLAGS', ''))
if archs:
cmake_args.append(f'-DCMAKE_OSX_ARCHITECTURES={";".join(archs)}')
try:
import pybind11
cmake_args.append(f'-DPYBIND11_CMAKE_DIR={pybind11.get_cmake_dir()}')
except ImportError:
pass
build_args = ['--config', config]
if (
'CMAKE_BUILD_PARALLEL_LEVEL' not in os.environ
and hasattr(self, 'parallel')
and self.parallel
):
build_args.extend(['--parallel', str(self.parallel)])
else:
build_args.append('--parallel')
build_args.extend(['--target', ext.target, '--'])
try:
os.chdir(build_temp)
self.spawn([cmake, ext.source_dir, *cmake_args])
if not self.dry_run:
self.spawn([cmake, '--build', '.', *build_args])
finally:
os.chdir(HERE)
CIBUILDWHEEL = os.getenv('CIBUILDWHEEL', '0') == '1'
LINUX = platform.system() == 'Linux'
MACOS = platform.system() == 'Darwin'
WINDOWS = platform.system() == 'Windows'
ext_kwargs = {
'cmdclass': {'build_ext': cmake_build_ext},
'ext_modules': [
CMakeExtension(
'torchopt._C',
source_dir=HERE,
optional=not (LINUX and CIBUILDWHEEL),
)
],
}
TORCHOPT_NO_EXTENSIONS = (
bool(os.getenv('TORCHOPT_NO_EXTENSIONS', '')) or WINDOWS or (MACOS and CIBUILDWHEEL)
)
if TORCHOPT_NO_EXTENSIONS:
ext_kwargs.clear()
VERSION_CONTENT = None
try:
if not version.__release__:
try:
VERSION_CONTENT = VERSION_FILE.read_text(encoding='UTF-8')
VERSION_FILE.write_text(
data=re.sub(
r"""__version__\s*=\s*('[^']+'|"[^"]+")""",
f'__version__ = {version.__version__!r}',
string=VERSION_CONTENT,
),
encoding='UTF-8',
)
except OSError:
VERSION_CONTENT = None
setup(
name='torchopt',
version=version.__version__,
package_data={'sharedlib': ['*.so', '*.pyd']},
include_package_data=True,
**ext_kwargs,
)
finally:
if VERSION_CONTENT is not None:
with VERSION_FILE.open(mode='wt', encoding='UTF-8', newline='') as file:
file.write(VERSION_CONTENT)