source: anuga_core/source/pypar-numeric/contrib/compile.py @ 5779

Last change on this file since 5779 was 5779, checked in by steve, 16 years ago

Added the old version of pypar which works with Numeric. Necessary for parallel code until we move anuga to numpy (and then we can use pypar as distribute via sourceforge).

File size: 3.7 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
13def compile(FNs=None, CC=None, LD = None, SFLAG = None, verbose = 1):
14  """compile(FNs=None, CC=None, LD = None, SFLAG = None):
15 
16     Compile FN(s) using compiler CC (e.g. mpicc),
17     Loader LD and shared flag SFLAG.
18     If CC is absent use default compiler dependent on platform
19     if LD is absent CC is used.
20     if SFLAG is absent platform default is used
21     FNs can be either one filename or a list of filenames
22     In the latter case, the first will be used to name so file.
23  """
24  import os, string, sys, types
25 
26  # Input check
27  #
28  assert not FNs is None, "No filename provided"
29
30  if not type(FNs) == types.ListType:
31    FNs = [FNs]
32
33  # Determine platform and compiler
34  #
35  if sys.platform == 'sunos5':  #Solaris
36    if CC:
37      compiler = CC
38    else: 
39      compiler = 'gcc'
40    if LD:
41      loader = LD
42    else: 
43      loader = compiler
44    if SFLAG:
45      sharedflag = SFLAG
46    else: 
47      sharedflag = 'G'
48     
49  elif sys.platform == 'osf1V5':  #Compaq AlphaServer
50    if CC:
51      compiler = CC
52    else: 
53      compiler = 'cc'
54    if LD:
55      loader = LD
56    else: 
57      loader = compiler
58    if SFLAG:
59      sharedflag = SFLAG
60    else: 
61      sharedflag = 'shared'   
62     
63  elif sys.platform == 'linux2':  #Linux
64    if CC:
65      compiler = CC
66    else: 
67      compiler = 'gcc'
68    if LD:
69      loader = LD
70    else: 
71      loader = compiler
72    if SFLAG:
73      sharedflag = SFLAG
74    else: 
75      sharedflag = 'shared'   
76     
77  else:
78    if verbose: print "Unrecognised platform %s - revert to default"\
79                %sys.platform
80   
81    if CC:
82      compiler = CC
83    else: 
84      compiler = 'cc'
85    if LD:
86      loader = LD
87    else: 
88      loader = 'ld'
89    if SFLAG:
90      sharedflag = SFLAG
91    else: 
92      sharedflag = 'G'
93
94           
95  # Find location of include files
96  #
97  include = os.path.join(os.path.join(sys.exec_prefix, 'include'),
98                     'python'+sys.version[:3])
99 
100 
101  # Check existence of Python.h
102  #
103  headerfile = include + '/Python.h'
104  try:
105    open(headerfile, 'r')
106  except:
107    raise """Did not find Python header file %s.
108    Make sure files for Python C-extensions are installed.
109    In debian linux, for example, you need to install a
110    package called something like python2.1-dev""" %headerfile
111 
112 
113  # Check filename(s)
114  #
115  object_files = ''
116  for FN in FNs:       
117    root, ext = os.path.splitext(FN)
118    if ext == '':
119      FN = FN + '.c'
120    elif ext.lower() != '.c':
121      raise Exception, "Unrecognised extension: " + FN
122   
123    try:
124      open(FN,'r')
125    except:   
126      raise Exception, "Could not open: " + FN
127
128    if not object_files: root1 = root  #Remember first filename       
129    object_files += root + '.o ' 
130 
131 
132    # Compile
133    #
134   
135    s = "%s -c %s -I%s" %(compiler, FN, include)
136    if verbose:
137      print s
138    else:
139      s = s + ' 2> /dev/null' #Suppress errors
140 
141    try:
142      os.system(s)
143    except:
144      raise 'Could not compile %s - please try manually' %FN 
145
146 
147  # Make shared library (*.so)
148 
149  s = "%s -%s %s -o %s.so" %(loader, sharedflag, object_files, root1)
150  if verbose:
151    print s
152  else:
153    s = s + ' 2> /dev/null' #Suppress warnings
154 
155  try: 
156    err=os.system(s)
157  except:
158    raise 'Could not link %s - please try manually' %root1
159   
160
161
162
163if __name__ == '__main__':
164
165  import sys
166 
167  # Get file to compile
168  #
169  if len(sys.argv) < 2:
170    print "Usage:"
171    print "  python compile.py <filename>"
172    sys.exit()
173
174  compile(sys.argv[1], verbose=1)
175
Note: See TracBrowser for help on using the repository browser.