-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
183 lines (159 loc) · 5.85 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'''
setup.py for HiGHS scipy interface
Some CMake files are used to create source lists for compilation
'''
import pathlib
from datetime import datetime
import os
def pre_build_hook(build_ext, ext):
from scipy._build_utils.compiler_helper import get_cxx_std_flag
std_flag = get_cxx_std_flag(build_ext._cxx_compiler)
if std_flag is not None:
ext.extra_compile_args.append(std_flag)
def basiclu_pre_build_hook(build_clib, build_info):
from scipy._build_utils.compiler_helper import get_c_std_flag
c_flag = get_c_std_flag(build_clib.compiler)
if c_flag is not None:
if 'extra_compiler_args' not in build_info:
build_info['extra_compiler_args'] = []
build_info['extra_compiler_args'].append(c_flag)
def _get_sources(CMakeLists, start_token, end_token):
# Read in sources from CMakeLists.txt
CMakeLists = pathlib.Path(__file__).parent / CMakeLists
with open(CMakeLists, 'r', encoding='utf-8') as f:
s = f.read()
# Find block where sources are listed
start_idx = s.find(start_token) + len(start_token)
end_idx = s[start_idx:].find(end_token) + len(s[:start_idx])
sources = s[start_idx:end_idx].split('\n')
sources = [s.strip() for s in sources if s[0] != '#']
# Make relative to setup.py
sources = [str(pathlib.Path('src/' + s)) for s in sources]
return sources
# Grab some more info about HiGHS from root CMakeLists
def _get_version(CMakeLists, start_token, end_token=')'):
CMakeLists = pathlib.Path(__file__).parent / CMakeLists
with open(CMakeLists, 'r', encoding='utf-8') as f:
s = f.read()
start_idx = s.find(start_token) + len(start_token) + 1
end_idx = s[start_idx:].find(end_token) + len(s[:start_idx])
return s[start_idx:end_idx].strip()
def configuration(parent_package='', top_path=None):
from numpy.distutils.misc_util import Configuration
config = Configuration('_highs', parent_package, top_path)
# HiGHS info
_major_dot_minor = _get_version(
'CMakeLists.txt', 'project(HIGHS VERSION', 'LANGUAGES CXX C')
HIGHS_VERSION_MAJOR, HIGHS_VERSION_MINOR = _major_dot_minor.split('.')
HIGHS_VERSION_PATCH = _get_version(
'CMakeLists.txt', 'HIGHS_VERSION_PATCH')
GITHASH = 'n/a'
HIGHS_DIR = str(pathlib.Path(__file__).parent.resolve())
# Here are the pound defines that HConfig.h would usually provide;
# We provide an empty HConfig.h file and do the defs and undefs
# here:
TODAY_DATE = datetime.today().strftime('%Y-%m-%d')
DEFINE_MACROS = [
('CMAKE_BUILD_TYPE', '"Release"'),
('HiGHSRELEASE', None),
('IPX_ON', 'ON'),
('HIGHS_GITHASH', '"%s"' % GITHASH),
('HIGHS_COMPILATION_DATE', '"' + TODAY_DATE + '"'),
('HIGHS_VERSION_MAJOR', HIGHS_VERSION_MAJOR),
('HIGHS_VERSION_MINOR', HIGHS_VERSION_MINOR),
('HIGHS_VERSION_PATCH', HIGHS_VERSION_PATCH),
('HIGHS_DIR', '"' + HIGHS_DIR + '"'),
# ('NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION', None),
]
UNDEF_MACROS = [
'OPENMP', # unconditionally disable openmp
'EXT_PRESOLVE',
'SCIP_DEV',
'HiGHSDEV',
'OSI_FOUND',
]
# Compile BASICLU as a static library to appease clang:
# (won't allow -std=c++11/14 option for C sources)
basiclu_sources = _get_sources('src/CMakeLists.txt',
'set(basiclu_sources\n', ')')
config.add_library(
'basiclu',
sources=basiclu_sources,
include_dirs=[
'src/',
'src/ipm/basiclu/include/',
],
language='c',
macros=DEFINE_MACROS,
_pre_build_hook=basiclu_pre_build_hook,
)
# highs_wrapper:
ipx_sources = _get_sources('src/CMakeLists.txt', 'set(ipx_sources\n', ')')
highs_sources = _get_sources('src/CMakeLists.txt', 'set(sources\n', ')')
ext = config.add_extension(
'_highs_wrapper',
sources=['cython/src/_highs_wrapper.cxx'] + highs_sources + ipx_sources,
include_dirs=[
# highs_wrapper
'cython/src/',
'src/',
'src/lp_data/',
# highs
'src/',
'src/io/',
'src/ipm/ipx/include/',
# IPX
'src/ipm/ipx/include/',
'src/ipm/basiclu/include/',
],
language='c++',
libraries=['basiclu'],
define_macros=DEFINE_MACROS,
undef_macros=UNDEF_MACROS,
)
# Add c++11/14 support:
ext._pre_build_hook = pre_build_hook
# wrapper around HiGHS writeMPS:
ext = config.add_extension(
'_mpswriter',
sources=[
# we should be using using highs shared library;
# next best thing is compiling minimal set of sources
'cython/src/_mpswriter.cxx',
'src/util/HighsUtils.cpp',
'src/io/HighsIO.cpp',
'src/io/HMPSIO.cpp',
'src/lp_data/HighsModelUtils.cpp',
'src/util/stringutil.cpp',
],
include_dirs=[
'cython/src/',
'src/',
'src/io/',
'src/lp_data/',
],
language='c++',
libraries=['basiclu'],
define_macros=DEFINE_MACROS,
undef_macros=UNDEF_MACROS,
)
ext._pre_build_hook = pre_build_hook
# Export constants and enums from HiGHS:
ext = config.add_extension(
'_highs_constants',
sources=['cython/src/_highs_constants.cxx'],
include_dirs=[
'cython/src/',
'src/',
'src/io/',
'src/lp_data/',
'src/simplex/',
],
language='c++',
)
ext._pre_build_hook = pre_build_hook
config.add_data_files(os.path.join('cython', 'src', '*.pxd'))
return config
if __name__ == '__main__':
from numpy.distutils.core import setup
setup(**configuration(top_path='').todict())