-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsetup.py
More file actions
152 lines (115 loc) · 5.23 KB
/
setup.py
File metadata and controls
152 lines (115 loc) · 5.23 KB
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
145
146
147
148
149
150
151
152
from setuptools import setup, Extension, find_packages
from setuptools.command.build_ext import build_ext
from setuptools.command.install_lib import install_lib
from setuptools.command.install_scripts import install_scripts
from distutils.command.install_data import install_data
from wheel.bdist_wheel import bdist_wheel
import pathlib
import os
import subprocess
import shutil
import platform
class CMakeExt(Extension):
def __init__(self, name, sourcedir=''):
# Invoke the extension without any sources
super().__init__(name, sources=[])
# Get the source dir
self.src_dir = os.fspath(pathlib.Path(sourcedir).resolve())
print(f'[CMakeExt] Source dir: {self.src_dir}')
# Set cmake default options for the python release
self.cmake_options = [
'-DCMAKE_BUILD_TYPE=Release',
'-DOPENMP=ON',
'-DLTRACE=OFF',
'-DPPROF=OFF'
]
# Add osx sysroot if we are on macOS
if platform.system() == 'Darwin':
try:
sdk_path = subprocess.check_output(
['xcrun', '--sdk', 'macosx', '--show-sdk-path'],
universal_newlines=True
).strip()
# clang_path = subprocess.check_output(
# ['which', 'clang++'],
# universal_newlines=True
# ).strip()
# Append to cmake options
self.cmake_options.append(f'-DCMAKE_OSX_SYSROOT={sdk_path}')
# self.cmake_options.append(f'-DCMAKE_CXX_COMPILER={clang_path}')
except Exception as e:
print('DBG | Failed to determine macOS SDK path:', e)
class NFInstallData(install_data):
def run(self):
print('DBG | InstallData')
self.outfiles = self.distribution.data_files
class NFInstallExt(install_lib):
def run(self):
print('DBG | InstallLib')
self.skip_build = True
bin_dir = self.distribution.bin_dir # type: ignore
additional_files = [os.path.join(bin_dir, file) for file in os.listdir(bin_dir) if
os.path.isfile(os.path.join(bin_dir, file)) and file.startswith('nested_fit')
]
# HACK: (César) The package name is hardcoded...
pathlib.Path(os.path.join(self.build_dir, 'pynested_fit/')).mkdir(exist_ok=True)
for file in additional_files:
shutil.copy(file, os.path.join(self.build_dir, 'pynested_fit/' + os.path.basename(file)))
self.distribution.data_files = [os.path.join(self.build_dir, 'pynested_fit/' + os.path.basename(file)) for file in additional_files]
print('Dist files: ', self.distribution.data_files)
print('Dist files: ', additional_files)
self.distribution.run_command('install_data')
super().run()
class NFInstallScripts(install_scripts):
def run(self):
print('DBG | InstallScripts')
self.skip_build = True
bin_dir = self.distribution.bin_dir # type: ignore
script_dirs = [os.path.join(bin_dir, dir) for dir in os.listdir(bin_dir) if os.path.isdir(os.path.join(bin_dir, dir))]
print('Script Dirs:', script_dirs)
for sd in script_dirs:
shutil.copy(sd, os.path.join(self.build_dir, os.path.basename(sd)))
self.distribution.scripts = script_dirs
super().run()
# TODO: (César) We need to copy the installed cache. See how to do this
class NFBuildExt(build_ext):
def __init__(self, *args, **kwargs):
# Invoke build ext init
super().__init__(*args, **kwargs)
def run(self):
# Support for editable mode
if self.editable_mode:
print('WARNING: Editable mode will not install nested_fit!')
return
for ext in self.extensions:
self.build_nf(ext)
# NOTE: (César) Do not run the base method, we don't want a .so file out of this
# super().run()
def build_nf(self, ext: CMakeExt):
print('DBG | BuildNF')
cwd = pathlib.Path().absolute()
build_temp = pathlib.Path(self.build_temp)
build_temp.mkdir(parents=True, exist_ok=True)
os.chdir(str(build_temp))
self.spawn(['cmake', str(ext.src_dir)] + ext.cmake_options)
if not self.dry_run: # type: ignore
# subprocess.call(['cmake', '--build', '.', '--target', 'install'])
subprocess.call(['cmake', '--build', '.'], env=os.environ.copy())
os.chdir(str(cwd))
bin_dir = os.path.join(build_temp, '../../bin')
self.distribution.bin_dir = bin_dir # type: ignore
# TODO: (César) We need to install at least two executable files for now (currently only using list's [0])
bin_files = [os.path.join(bin_dir, file) for file in os.listdir(bin_dir) if
os.path.isfile(os.path.join(bin_dir, file)) and file.startswith('nested_fit')
]
print('Bin file(s) found:', bin_files)
setup(
cmdclass={
'build_ext': NFBuildExt,
'install_data': NFInstallData,
'install_lib': NFInstallExt,
'install_scripts': NFInstallScripts
},
ext_modules=[CMakeExt('nested_fit')],
packages=find_packages(exclude=['src/']),
)