/[ascend]/branches/python3/ascxx/SConscript
ViewVC logotype

Diff of /branches/python3/ascxx/SConscript

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.3505  
changed lines
  Added in v.3506

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22