forked from lballabio/QuantLib-SWIG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
261 lines (228 loc) · 9.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# -*- coding: iso-8859-1 -*-
"""
Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 StatPro Italia srl
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<[email protected]>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
"""
import os, sys, math
from distutils.cmd import Command
from distutils.command.build_ext import build_ext
from distutils.command.build import build
from distutils.ccompiler import get_default_compiler
try:
from setuptools import setup, Extension
except:
from distutils.core import setup, Extension
from distutils import sysconfig
class test(Command):
# Original version of this class posted
# by Berthold Höllmann to [email protected]
description = "test the distribution prior to install"
user_options = [
('test-dir=', None,
"directory that contains the test definitions"),
]
def initialize_options(self):
self.build_base = 'build'
self.test_dir = 'test'
def finalize_options(self):
build = self.get_finalized_command('build')
self.build_purelib = build.build_purelib
self.build_platlib = build.build_platlib
def run(self):
# Testing depends on the module having been built
self.run_command('build')
# extend sys.path
old_path = sys.path[:]
sys.path.insert(0, self.build_purelib)
sys.path.insert(0, self.build_platlib)
sys.path.insert(0, self.test_dir)
# import and run test-suite
module = __import__('QuantLibTestSuite', globals(), locals(), [''])
module.test()
# restore sys.path
sys.path = old_path[:]
class my_wrap(Command):
description = "generate Python wrappers"
user_options = []
def initialize_options(self): pass
def finalize_options(self): pass
def run(self):
print('Generating Python bindings for QuantLib...')
swig_version = os.popen("swig -version").read().split()[2]
major_swig_version = swig_version[0]
if major_swig_version < '4':
print('Warning: You have SWIG {} installed, but at least SWIG 4.0.1'
' is recommended. \nSome features may not work.'
.format(swig_version))
swig_dir = os.path.join("..","SWIG")
if sys.version_info.major >= 3:
os.system('swig -python -py3 -c++ -modern ' +
'-I%s ' % swig_dir +
'-outdir QuantLib -o QuantLib/quantlib_wrap.cpp ' +
'quantlib.i')
else:
os.system('swig -python -c++ -modern ' +
'-I%s ' % swig_dir +
'-outdir QuantLib -o QuantLib/quantlib_wrap.cpp ' +
'quantlib.i')
class my_build(build):
user_options = build.user_options + [
('static', None,
"link against static CRT libraries on Windows")
]
boolean_options = build.boolean_options + ['static']
def initialize_options(self):
build.initialize_options(self)
self.static = None
def finalize_options(self):
build.finalize_options(self)
class my_build_ext(build_ext):
user_options = build_ext.user_options + [
('static', None,
"link against static CRT libraries on Windows")
]
boolean_options = build.boolean_options + ['static']
def initialize_options(self):
build_ext.initialize_options(self)
self.static = None
def finalize_options(self):
build_ext.finalize_options(self)
self.set_undefined_options('build', ('static','static'))
self.include_dirs = self.include_dirs or []
self.library_dirs = self.library_dirs or []
self.define = self.define or []
self.libraries = self.libraries or []
extra_compile_args = []
extra_link_args = []
compiler = self.compiler or get_default_compiler()
if compiler == 'msvc':
try:
QL_INSTALL_DIR = os.environ['QL_DIR']
self.include_dirs += [QL_INSTALL_DIR]
self.library_dirs += [os.path.join(QL_INSTALL_DIR, 'lib')]
except KeyError:
print('warning: unable to detect QuantLib installation')
if 'INCLUDE' in os.environ:
dirs = [dir for dir in os.environ['INCLUDE'].split(';')]
self.include_dirs += [ d for d in dirs if d.strip() ]
if 'LIB' in os.environ:
dirs = [dir for dir in os.environ['LIB'].split(';')]
self.library_dirs += [ d for d in dirs if d.strip() ]
dbit = round(math.log(sys.maxsize, 2) + 1)
if dbit == 64:
machinetype = '/machine:x64'
else:
machinetype = '/machine:x86'
self.define += [('__WIN32__', None), ('WIN32', None),
('NDEBUG', None), ('_WINDOWS', None),
('NOMINMAX', None)]
extra_compile_args = ['/GR', '/FD', '/Zm250', '/EHsc', '/bigobj' ]
extra_link_args = ['/subsystem:windows', machinetype]
if self.debug:
if self.static:
extra_compile_args.append('/MTd')
else:
extra_compile_args.append('/MDd')
else:
if self.static:
extra_compile_args.append('/MT')
else:
extra_compile_args.append('/MD')
elif compiler == 'unix':
ql_compile_args = \
os.popen('quantlib-config --cflags').read()[:-1].split()
ql_link_args = \
os.popen('quantlib-config --libs').read()[:-1].split()
self.define += [ (arg[2:],None) for arg in ql_compile_args
if arg.startswith('-D') ]
self.define += [('NDEBUG', None)]
self.include_dirs += [ arg[2:] for arg in ql_compile_args
if arg.startswith('-I') ]
self.library_dirs += [ arg[2:] for arg in ql_link_args
if arg.startswith('-L') ]
self.libraries += [ arg[2:] for arg in ql_link_args
if arg.startswith('-l') ]
extra_compile_args = [ arg for arg in ql_compile_args
if not arg.startswith('-D')
if not arg.startswith('-I') ] \
+ [ '-Wno-unused' ]
if 'CXXFLAGS' in os.environ:
extra_compile_args += os.environ['CXXFLAGS'].split()
extra_link_args = [ arg for arg in ql_link_args
if not arg.startswith('-L')
if not arg.startswith('-l') ]
if 'LDFLAGS' in os.environ:
extra_link_args += os.environ['LDFLAGS'].split()
else:
pass
for ext in self.extensions:
ext.extra_compile_args = ext.extra_compile_args or []
ext.extra_compile_args += extra_compile_args
ext.extra_link_args = ext.extra_link_args or []
ext.extra_link_args += extra_link_args
if os.name == 'posix':
# changes the compiler from gcc to g++
save_init_posix = sysconfig._init_posix
def my_init_posix():
save_init_posix()
g = sysconfig._config_vars
if 'CXX' in os.environ:
g['CC'] = os.environ['CXX']
else:
g['CC'] = 'g++'
if sys.platform.startswith("darwin"):
g['LDSHARED'] = g['CC'] + \
' -bundle -flat_namespace -undefined suppress'
else:
g['LDSHARED'] = g['CC'] + ' -shared'
sysconfig._init_posix = my_init_posix
classifiers = [
'Development Status :: 5 - Production/Stable',
'Environment :: Console',
'Intended Audience :: Developers',
'Intended Audience :: Science/Research',
'Intended Audience :: End Users/Desktop',
'License :: OSI Approved :: BSD License',
'Natural Language :: English',
'Programming Language :: C++',
'Programming Language :: Python',
'Topic :: Scientific/Engineering',
'Operating System :: Microsoft :: Windows',
'Operating System :: POSIX',
'Operating System :: Unix',
'Operating System :: MacOS',
]
setup(name = "QuantLib",
version = "1.19-dev",
description = "Python bindings for the QuantLib library",
long_description = """
QuantLib (http://quantlib.org/) is a C++ library for financial quantitative
analysts and developers, aimed at providing a comprehensive software
framework for quantitative finance.
""",
author = "QuantLib Team",
author_email = "[email protected]",
url = "http://quantlib.org",
license = "BSD 3-Clause",
classifiers = classifiers,
py_modules = ['QuantLib.__init__','QuantLib.QuantLib'],
ext_modules = [Extension("QuantLib._QuantLib",
["QuantLib/quantlib_wrap.cpp"])
],
data_files = [('share/doc/quantlib', ['../LICENSE.TXT'])],
cmdclass = {'test': test,
'wrap': my_wrap,
'build': my_build,
'build_ext': my_build_ext
}
)