source: anuga_core/source/anuga/utilities/compile.py @ 3514

Last change on this file since 3514 was 3514, checked in by duncan, 18 years ago

Hi all,
I'm doing a change in the anuga structure, moving the code to

\anuga_core\source\anuga

After you have done an svn update, the PYTHONPATH has to be changed to;
PYTHONPATH = anuga_core/source/

This is part of changes required to make installation of anuga quicker and reducing the size of our sandpits.

If any imports are broken, try fixing them. With adding anuga. to them for example. If this seems to have really broken things, email/phone me.

Cheers
Duncan

File size: 8.4 KB
Line 
1"""compile.py - compile Python C-extension
2
3   Commandline usage:
4     python compile.py <filename>
5
6   Usage from within Python:
7     import compile
8     compile.compile(<filename>,..)
9
10   Ole Nielsen, Oct 2001     
11"""     
12
13 
14def compile(FNs=None, CC=None, LD = None, SFLAG = None, verbose = 1):
15  """compile(FNs=None, CC=None, LD = None, SFLAG = None):
16 
17     Compile FN(s) using compiler CC (e.g. mpicc),
18     Loader LD and shared flag SFLAG.
19     If CC is absent use default compiler dependent on platform
20     if LD is absent CC is used.
21     if SFLAG is absent platform default is used
22     FNs can be either one filename or a list of filenames
23     In the latter case, the first will be used to name so file.
24  """
25 
26 
27  ###########################jhdsgfjgdjfg
28 
29  import os, string, sys, types
30 
31  # Input check
32  #
33  assert not FNs is None, "No filename provided"
34
35  if not type(FNs) == types.ListType:
36    FNs = [FNs]
37
38
39  libext = 'so' #Default extension (Unix)
40  libs = ''
41  version = sys.version[:3]
42 
43  # Determine platform and compiler
44  #
45  if sys.platform == 'sunos5':  #Solaris
46    if CC:
47      compiler = CC
48    else: 
49      compiler = 'gcc'
50    if LD:
51      loader = LD
52    else: 
53      loader = compiler
54    if SFLAG:
55      sharedflag = SFLAG
56    else: 
57      sharedflag = 'G'
58     
59  elif sys.platform == 'osf1V5':  #Compaq AlphaServer
60    if CC:
61      compiler = CC
62    else: 
63      compiler = 'cc'
64    if LD:
65      loader = LD
66    else: 
67      loader = compiler
68    if SFLAG:
69      sharedflag = SFLAG
70    else: 
71      sharedflag = 'shared'   
72     
73  elif sys.platform == 'linux2':  #Linux
74    if CC:
75      compiler = CC
76    else: 
77      compiler = 'gcc'
78    if LD:
79      loader = LD
80    else: 
81      loader = compiler
82    if SFLAG:
83      sharedflag = SFLAG
84    else: 
85      sharedflag = 'shared'   
86     
87  elif sys.platform == 'darwin':  #Mac OS X:
88    if CC:
89      compiler = CC
90    else: 
91      compiler = 'cc'
92    if LD:
93      loader = LD
94    else: 
95      loader = compiler
96    if SFLAG:
97      sharedflag = SFLAG
98    else: 
99      sharedflag = 'bundle -flat_namespace -undefined suppress'
100
101  elif sys.platform == 'cygwin':  #Cygwin (compilation same as linux)
102    if CC:
103      compiler = CC
104    else: 
105      compiler = 'gcc'
106    if LD:
107      loader = LD
108    else: 
109      loader = compiler
110    if SFLAG:
111      sharedflag = SFLAG
112    else: 
113      sharedflag = 'shared'
114    libext = 'dll'
115    libs = '/lib/python%s/config/libpython%s.dll.a' %(version,version)
116     
117  elif sys.platform == 'win32':  #Windows
118    if CC:
119      compiler = CC
120    else: 
121      compiler = 'gcc'
122    if LD:
123      loader = LD
124    else: 
125      loader = compiler
126    if SFLAG:
127      sharedflag = SFLAG
128    else: 
129      sharedflag = 'shared'
130    libext = 'dll'
131
132    v = version.replace('.','')
133    dllfilename = 'python%s.dll' %(v)
134    libs = os.path.join(sys.exec_prefix,dllfilename)
135     
136     
137  else:
138    if verbose: print "Unrecognised platform %s - revert to default"\
139                %sys.platform
140   
141    if CC:
142      compiler = CC
143    else: 
144      compiler = 'cc'
145    if LD:
146      loader = LD
147    else: 
148      loader = 'ld'
149    if SFLAG:
150      sharedflag = SFLAG
151    else: 
152      sharedflag = 'G'
153
154           
155  # Find location of include files
156  #
157  if sys.platform == 'win32':  #Windows
158    python_include = os.path.join(sys.exec_prefix, 'include')   
159  else: 
160    python_include = os.path.join(os.path.join(sys.exec_prefix, 'include'),
161                                  'python' + version)
162
163  # Check existence of Python.h
164  #
165  headerfile = python_include + os.sep + 'Python.h'
166  try:
167    open(headerfile, 'r')
168  except:
169    raise """Did not find Python header file %s.
170    Make sure files for Python C-extensions are installed.
171    In debian linux, for example, you need to install a
172    package called something like python2.3-dev""" %headerfile
173
174
175
176  #Add Python path + utilities to includelist (see ticket:31)
177  #Assume there is only one 'utilities' dir under path dirs
178 
179  utilities_include_dir = None
180  for pathdir in sys.path:
181
182    utilities_include_dir = pathdir + os.sep + 'utilities'
183    #print pathdir
184    #print utilities_include_dir
185    try:
186      os.stat(utilities_include_dir)
187    except OSError:
188      pass
189    else:
190      #print 'Found %s to be used as include dir' %utilities_include_dir
191      break
192
193  utilities_include_dir = buildroot + os.sep + "source" + os.sep + "anuga" \
194                          + os.sep + 'utilities'
195 
196
197 
198 
199  # Check filename(s)
200  #
201  object_files = ''
202  for FN in FNs:       
203    root, ext = os.path.splitext(FN)
204    if ext == '':
205      FN = FN + '.c'
206    elif ext.lower() != '.c':
207      raise Exception, "Unrecognised extension: " + FN
208   
209    try:
210      open(FN, 'r')
211    except:
212      #print 'CWD:', os.getcwd()     
213      raise Exception, "Could not open: " + FN
214
215    if not object_files: root1 = root  #Remember first filename       
216    object_files += root + '.o ' 
217 
218 
219    # Compile
220    #
221    if utilities_include_dir is None:   
222      s = '%s -c %s -I%s -o %s.o -Wall -O3'\
223          %(compiler, FN, python_include, root)
224    else:
225      s = '%s -c %s -I%s -I%s -o %s.o -Wall -O3'\
226          %(compiler, FN, python_include, utilities_include_dir, root)
227
228    if os.name == 'posix' and os.uname()[4] == 'x86_64':
229      #Extra flags for 64 bit architectures
230      #Second clause will always fail on Win32 because uname is UNIX specific
231      #but won't get past first clause
232
233      #FIXME: Which one?
234      #s += ' -fPIC'
235      s += ' -fPIC -m64' 
236     
237     
238    if verbose:
239      print s
240    else:
241      s = s + ' 2> /dev/null' #Suppress errors
242 
243    try:
244      err = os.system(s)
245      if err != 0:
246          raise 'Attempting to compile %s failed - please try manually' %FN
247    except:
248      raise 'Could not compile %s - please try manually' %FN 
249
250 
251  # Make shared library (*.so or *.dll)
252 
253  s = "%s -%s %s -o %s.%s %s -lm" %(loader, sharedflag, object_files, root1, libext, libs)
254  if verbose:
255    print s
256  else:
257    s = s + ' 2> /dev/null' #Suppress warnings
258 
259  try: 
260    err=os.system(s)
261    if err != 0:       
262        raise 'Atempting to link %s failed - please try manually' %root1     
263  except:
264    raise 'Could not link %s - please try manually' %root1
265   
266
267def can_use_C_extension(filename):
268    """Determine whether specified C-extension
269    can and should be used.
270    """
271
272
273    from os.path import splitext
274
275    root, ext = splitext(filename)
276   
277    C=False
278
279    try:
280        s = 'import %s' %root
281        #print s
282        exec(s)
283    except:
284        try:
285            open(filename)
286        except:
287            msg = 'C extension %s cannot be opened' %filename
288            print msg               
289        else:   
290            print '------- Trying to compile c-extension %s' %filename
291
292            #compile(filename)       
293            try:
294                compile(filename)
295            except:
296                print 'WARNING: Could not compile C-extension %s'\
297                      %filename
298            else:
299                try:
300                    exec('import %s' %root)
301                except:
302                    msg = 'C extension %s seems to compile OK, '
303                    msg += 'but it can still not be imported.'
304                    raise msg
305                else:
306                    C=True
307    else:
308        C=True
309           
310    if not C:
311        pass
312        print 'NOTICE: C-extension %s not used' %filename
313
314    return C
315
316
317
318
319if __name__ == '__main__':
320
321
322  import sys, os
323  from os.path import splitext
324 
325  if len(sys.argv) > 1:
326      files = sys.argv[1:]
327      for filename in files:
328          root, ext = splitext(filename)
329
330          if ext <> '.c':
331              print 'WARNING (compile.py): Skipping %s. I only compile C-files.' %filename
332     
333  else: 
334      #path = os.path.split(sys.argv[0])[0] or os.getcwd()
335      path = '.'
336      files = os.listdir(path)
337     
338     
339
340  for filename in files:
341      root, ext = splitext(filename)
342
343      if ext == '.c':
344          for x in ['.dll', '.so']:
345              try:
346                  os.remove(root + x)
347              except:
348                  pass
349
350          print '--------------------------------------'     
351          print 'Trying to compile c-extension %s in %s'\
352                %(filename, os.getcwd())
353
354          try:
355              compile(filename)
356          except Exception, e:
357              print 'Could not compile C extension %s' %filename
358          else:
359              print 'C extension %s OK' %filename
360          print   
361       
362
Note: See TracBrowser for help on using the repository browser.