1 |
#!/usr/bin/env python |
2 |
Import('env') |
3 |
|
4 |
import platform, sys, subprocess, re |
5 |
import packaging.version as pv |
6 |
|
7 |
srcs = Split(""" |
8 |
library.cpp compiler.cpp type.cpp module.cpp symchar.cpp |
9 |
instance.cpp instanceinterfacedata.cpp |
10 |
matrix.cpp method.cpp name.cpp |
11 |
reporter.cpp simulation.cpp set.cpp units.cpp dimensions.cpp extmethod.cpp |
12 |
variable.cpp registry.cpp relation.cpp |
13 |
solver.cpp curve.cpp plot.cpp |
14 |
solverhooks.cpp |
15 |
solverparameters.cpp solverparameter.cpp solverparameteriterator.cpp |
16 |
solverstatus.cpp solverreporter.cpp |
17 |
value.cpp |
18 |
incidencematrix.cpp |
19 |
integrator.cpp |
20 |
integratorreporter.cpp |
21 |
annotation.cpp |
22 |
""") |
23 |
|
24 |
# Build a static library with all the sources |
25 |
|
26 |
envp = env.Clone() |
27 |
|
28 |
# get compile/link settings for libpython, Python.h |
29 |
print("PYTHON_PKG =",envp['PYTHON_PKG']) |
30 |
envp.ParseConfig('pkg-config $PYTHON_PKG --cflags --libs') |
31 |
|
32 |
configh = envp.Substfile(source='config.h.in') |
33 |
|
34 |
objs = [] |
35 |
for s in srcs: |
36 |
objs += envp.SharedObject(s) |
37 |
|
38 |
#--------------------------------------------- |
39 |
# LITTLE WEE TEST PROGRAMS for debuggin the c++ wrapper |
40 |
# currently out of order because of need for a separate builddir due to ASCXX_WITH_PYTHON flag taking different |
41 |
|
42 |
envx = env.Clone() |
43 |
envx.ParseConfig('pkg-config $PYTHON_PKG_EMBED --cflags --libs') |
44 |
print("envx LIBS =",envx['LIBS']) |
45 |
envx.Append(LIBS=['ascend'],LIBPATH=['#']) |
46 |
print("envx LIBS =",envx['LIBS']) |
47 |
ipopttest = envx.Program('testipopt',['testipopt.cpp'] + objs) |
48 |
conopttest = envx.Program('testconopt',['testconopt.cpp'] + objs) |
49 |
slvreqtest = envx.Program('testslvreq',['testslvreq.cpp'] + objs) |
50 |
|
51 |
#--------------------------------------------- |
52 |
# BUILD THE SWIG WRAPPER |
53 |
|
54 |
# check SWIG version |
55 |
|
56 |
def get_swig_version(env): |
57 |
#print("SWIG='%s'" % env['SWIG']) |
58 |
cmd = [env['SWIG'],'-version'] |
59 |
if sys.version_info[0]==2: |
60 |
output = subprocess.check_output(cmd) |
61 |
else: |
62 |
output = subprocess.check_output(cmd,encoding='utf-8') |
63 |
|
64 |
re1 = r"SWIG\s+Version\s+(?P<ver>[0-9][^\s]*)\s*$" |
65 |
expr = re.compile(re1,re.M); |
66 |
m = expr.search(output); |
67 |
if not m: |
68 |
#print("Got '%s'"%output) |
69 |
return None |
70 |
return pv.parse(m.group('ver')) |
71 |
|
72 |
def check_swig_version(context): |
73 |
try: |
74 |
context.Message("Checking version of SWIG... ") |
75 |
ver = get_swig_version(context.env) |
76 |
context.env['SWIGVERSION']=ver |
77 |
if ver >= pv.parse('3'): |
78 |
context.Result("ok, %s" % (ver)) |
79 |
return True; |
80 |
else: |
81 |
context.Result("too old, %d.%d.%d" % (maj,min,pat)) |
82 |
return False; |
83 |
except Exception as e: |
84 |
context.Result("Failed to detect version, or failed to run SWIG (%s)"% str(e)) |
85 |
return False; |
86 |
|
87 |
conf = envp.Configure(custom_tests={'CheckSwig':check_swig_version}) |
88 |
|
89 |
build_python=True |
90 |
if not conf.CheckCHeader('Python.h'): |
91 |
with_python=False |
92 |
if not conf.CheckSwig(): |
93 |
with_python=False |
94 |
without_python_reason = 'SWIG >= 1.3.24 is required' |
95 |
else: |
96 |
conf.env.Append(SWIGFLAGS=['-python']) |
97 |
|
98 |
envp = conf.Finish() |
99 |
|
100 |
print("SWIGVERSION = ",env['SWIGVERSION']) |
101 |
|
102 |
import SCons.Script |
103 |
SWIGScanner = SCons.Scanner.ClassicCPP( |
104 |
"SWIGScan" |
105 |
, ".i" |
106 |
, "CPPPATH" |
107 |
, '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")' |
108 |
) |
109 |
envp.Append(SCANNERS=[SWIGScanner]) |
110 |
|
111 |
#---------------------------------------------- |
112 |
# SWIG wrapper |
113 |
|
114 |
def get_new_swig_flags(env): |
115 |
flags = [] |
116 |
if pv.parse(env['SWIGVERSION']) >= pv.parse('1.3.28'): |
117 |
flags += ['-O'] |
118 |
|
119 |
#print "FLAGS=%s" % env.get('GRAPHVIZ_CPPPATH') |
120 |
if env.get('WITH_GRAPHVIZ') and env.get('GRAPHVIZ_CPPPATH'): |
121 |
flags += ['-I%s' % i for i in env.get('GRAPHVIZ_CPPPATH')] |
122 |
|
123 |
if env.get('LIBS') and 'msvcr71' in env['LIBS']: |
124 |
flags += ['-DHAVE_MSVCR71'] |
125 |
|
126 |
return flags |
127 |
|
128 |
envs = envp.Clone() |
129 |
envs.Append(LIBS=['ascend'],LIBPATH=['#']) |
130 |
|
131 |
if '-Wall' in envs.get('CCFLAGS'): |
132 |
envs['CCFLAGS'] = envs['CCFLAGS'].remove('-Wall') |
133 |
|
134 |
if platform.system()=="Windows": |
135 |
envs['SHLIBSUFFIX']='.pyd' |
136 |
if env.get('LIBS') and 'msvcr71' in env['LIBS']: |
137 |
envs.Append(CPPDEFINES=['HAVE_MSVCR71']) |
138 |
envs.Append(LINKFLAGS="-static-libgcc ") |
139 |
#TDM-GCC does *not* require or support the following: |
140 |
#swig_env.Append(LINKFLAGS="-static-libstdc++") |
141 |
|
142 |
elif platform.system()=="Darwin": |
143 |
envs['SHLIBSUFFIX']='.so' |
144 |
|
145 |
swigobjs = [] |
146 |
|
147 |
for swigf in Split(""" |
148 |
ascpy.i |
149 |
"""): |
150 |
swigobj = envs.SharedObject(swigf |
151 |
, SWIGFLAGS=['-python','-c++'] + get_new_swig_flags(env) |
152 |
) |
153 |
#swig_env.SideEffect(['ascpy.py','ascpy_wrap.h'],'ascpy$SWIGCXXFILESUFFIX') |
154 |
envs.Depends('ascpy$SWIGCXXFILESUFFIX',['ascpy.i','solver.i','plot.i']) |
155 |
envs.Clean('ascpy_wrap$SWIGCXXFILESUFFIX',swigobj) |
156 |
envs.Clean('ascpy.py','ascpy$SWIGCXXFILESUFFIX') |
157 |
envs.Clean('ascpy_wrap.h','ascpy$SWIGCXXFILESUFFIX') |
158 |
|
159 |
swigobjs.append(swigobj) |
160 |
|
161 |
if envs.get('WITH_DMALLOC'): |
162 |
envs.Append(LIBS = ['dmalloc']) |
163 |
envs.AppendUnique(LIBPATH = [env.get('DMALLOC_LIBPATH')]) |
164 |
|
165 |
if envs.get('WITH_GRAPHVIZ'): |
166 |
envs.Append(LIBS = env['GRAPHVIZ_LIBS']) |
167 |
swig_env.AppendUnique(LIBPATH = [env.get('GRAPHVIZ_LIBPATH')]) |
168 |
|
169 |
swiglib = envs.SharedLibrary("ascpy",objs + swigobjs |
170 |
, SHLIBPREFIX = '_' |
171 |
) |
172 |
|
173 |
|
174 |
|
175 |
if 0: |
176 |
#if platform.system()=='Windows' and env.get('MSVS'): |
177 |
# envp.Append(CCFLAGS=['/EHsc']) # for exceptions (as suggested by a MSVC error msg, dunno if it's right or not -- JP) |
178 |
#swig_has_gccvisibility = True |
179 |
# |
180 |
#if env.get('HAVE_GCC'): |
181 |
#python_env.Append(CPPFLAGS=['-O3']) |
182 |
# if swig_has_gccvisibility and 'HAVE_GCCVISIBILITY' in env: |
183 |
# python_env.Append(CCFLAGS=['-fvisibility=hidden']); |
184 |
|
185 |
#--------------------------------------------- |
186 |
# INSTALLATION |
187 |
|
188 |
# python compile bytecode |
189 |
|
190 |
if env.get('CAN_INSTALL'): |
191 |
env.InstallShared(Dir(env.subst("$INSTALL_ROOT$INSTALL_PYTHON_ASCEND")),swiglib) |
192 |
env.InstallShared(Dir(env.subst("$INSTALL_ROOT$INSTALL_PYTHON_ASCEND")),'ascpy.py') |
193 |
|
194 |
# vim: set syntax=python: |
195 |
|