1 | import platform |
---|
2 | import sys |
---|
3 | import os |
---|
4 | import glob |
---|
5 | import subprocess |
---|
6 | #========================================================================================== |
---|
7 | def is_exe(fpath): |
---|
8 | return os.path.exists(fpath) and os.access(fpath, os.X_OK) |
---|
9 | def which_path(program): |
---|
10 | "emulates Unix which: finds a program in path, but returns the path" |
---|
11 | import os, sys |
---|
12 | if sys.platform == "win32" and os.path.splitext(program)[1].lower() != '.exe': |
---|
13 | program = program + '.exe' |
---|
14 | fpath, fname = os.path.split(program) |
---|
15 | if fpath: |
---|
16 | if is_exe(program): |
---|
17 | return fpath |
---|
18 | else: |
---|
19 | for path in os.environ["PATH"].split(os.pathsep): |
---|
20 | exe_file = os.path.join(path, program) |
---|
21 | if is_exe(exe_file): |
---|
22 | return path |
---|
23 | return "" |
---|
24 | #========================================================================================== |
---|
25 | # misc initializations |
---|
26 | # need command-line options for fortran command and fortran options |
---|
27 | F2PYflags = '' # compiler options for f2py command |
---|
28 | F2PYpath = which_path('f2py') # default path to f2py |
---|
29 | GFORTpath = which_path('gfortran') # path to compiler |
---|
30 | FCompiler='gfortran' |
---|
31 | G77path = which_path('g77') # path to compiler |
---|
32 | FORTpath = "" |
---|
33 | FORTflags = "" |
---|
34 | liblist = [] |
---|
35 | LDFLAGS = '' |
---|
36 | #========================================================================================== |
---|
37 | # configure platform dependent options here: |
---|
38 | if sys.platform == "win32": |
---|
39 | F2PYsuffix = '.pyd' |
---|
40 | FCompiler='g77' |
---|
41 | elif sys.platform == "darwin": |
---|
42 | LDFLAGS = '-undefined dynamic_lookup -bundle -static-libgfortran -static-libgcc' |
---|
43 | #F2PYpath = '/Library/Frameworks/Python.framework/Versions/6.2/bin/' # path to f2py (python 6.2) |
---|
44 | #F2PYpath = '/Library/Frameworks/Python.framework/Versions/current/bin/' # path to f2py |
---|
45 | #FORTpath = '/sw/bin' # path to compilier |
---|
46 | F2PYsuffix = '.so' |
---|
47 | elif sys.platform == "linux2": |
---|
48 | #LDFLAGS = '-undefined dynamic_lookup -bundle -static-libgfortran -static-libgcc' |
---|
49 | #F2PYflags = '--fcompiler=gnu95 --f77exec=gfortran --f77flags="-fno-range-check"' |
---|
50 | F2PYsuffix = '.so' |
---|
51 | else: |
---|
52 | print "Sorry, parameters for platform "+sys.platform+" are not yet defined" |
---|
53 | sys.exit() |
---|
54 | #========================================================================================== |
---|
55 | # help |
---|
56 | if 'help' in COMMAND_LINE_TARGETS: |
---|
57 | print """Building Fortran routines for use with GSAS-II |
---|
58 | ---------------------------------------------- |
---|
59 | |
---|
60 | To build the compiled modules files needed to run GSAS-II, invoke this script: |
---|
61 | scons [options] |
---|
62 | where the following options are defined (all are optional): |
---|
63 | |
---|
64 | -Q -- produces less output from scons |
---|
65 | |
---|
66 | -n -- causes scons to show but not execute the commands it will perform |
---|
67 | |
---|
68 | -c -- clean: causes scons to delete previously created files (don't use |
---|
69 | with install) |
---|
70 | |
---|
71 | help -- causes this message to be displayed (no compiling is done) |
---|
72 | |
---|
73 | install -- causes the module files to be placed in an installation directory |
---|
74 | (../bin<X>NNv.v) rather than ../bin (<X> is mac, win or linux; NN is 64 |
---|
75 | or blank; v.v is the python version (2.6 or 2.7,...). Normally used only |
---|
76 | by Brian or Bob for distribution of compiled software. |
---|
77 | |
---|
78 | The following options override defaults set in the scons script: |
---|
79 | |
---|
80 | FCompiler=<path> -- define the name of the fortran compiler; default is to use |
---|
81 | g77 on Windows and gfortran elsewhere. If you use something other than g77 or |
---|
82 | gfortran, you must also specify F2PYflags |
---|
83 | |
---|
84 | FORTpath=<path> -- define a path to the fortran program; default is to use |
---|
85 | first gfortran (g77 for Windows) found in path |
---|
86 | |
---|
87 | FORTflags='string' -- string of options to be used for Fortran |
---|
88 | during library build step |
---|
89 | |
---|
90 | F2PYpath=<path> -- define a path to the f2py program; default is to use |
---|
91 | first f2py found in path |
---|
92 | |
---|
93 | F2PYflags='string' -- defines optional flags to be supplied to f2py: |
---|
94 | Typically these option define which fortran compiler to use. |
---|
95 | |
---|
96 | F2PYsuffix='.xxx' -- extension for output module files (default: win: '.pyd', |
---|
97 | mac/linux: '.so') |
---|
98 | |
---|
99 | LDFLAGS='string' -- string of options to be used for f2py during link step |
---|
100 | |
---|
101 | Note that at present, only 32-bit python is supported and python 3.x is |
---|
102 | not supported. |
---|
103 | |
---|
104 | examples: |
---|
105 | scons -Q |
---|
106 | (builds into ../bin for current platform using default options) |
---|
107 | scons -Q install |
---|
108 | (builds into ../bin<platform> for module distribution) |
---|
109 | scons -Q install F2PYpath=/Library/Frameworks/Python.framework/Versions/6.2/bin/ |
---|
110 | (builds with a non-default [e.g. older] version of python) |
---|
111 | """ |
---|
112 | sys.exit() |
---|
113 | #========================================================================================== |
---|
114 | # override from command-line options |
---|
115 | for var in ['F2PYflags','F2PYpath','F2PYsuffix','FCompiler','FORTpath','FORTflags','LDFLAGS']: |
---|
116 | if ARGUMENTS.get(var, None) is not None: |
---|
117 | print 'Setting',var,'to',ARGUMENTS.get(var),'based on command line' |
---|
118 | exec(var + "= ARGUMENTS.get('" + var +"')") |
---|
119 | #========================================================================================== |
---|
120 | # use the compiler choice to set compiler options, but don't change anything |
---|
121 | # specified on the command line |
---|
122 | if FCompiler == 'gfortran': |
---|
123 | if FORTpath == "": FORTpath = GFORTpath |
---|
124 | if F2PYflags == "": F2PYflags = '--fcompiler=gnu95 --f77exec=gfortran --f77flags="-fno-range-check"' |
---|
125 | elif FCompiler == 'g77': |
---|
126 | if FORTpath == "": FORTpath = G77path |
---|
127 | if F2PYflags == "": F2PYflags = '--fcompiler=gnu --f77exec=g77 --f77flags="-fno-range-check"' |
---|
128 | if sys.platform == "win32": |
---|
129 | if FORTflags == "": FORTflags = ' -w -O2 -fno-automatic -finit-local-zero -malign-double -mwindows' |
---|
130 | else: |
---|
131 | if FORTpath == "": print 'Likely error, FORTpath is not specified' |
---|
132 | if F2PYflags == "": |
---|
133 | print 'Error: specify a F2PYflags value' |
---|
134 | sys.exit() |
---|
135 | |
---|
136 | #========================================================================================== |
---|
137 | # get the python version number from the python image in the f2py directory |
---|
138 | # find the python location. Note that on Windows it may be in the parent of the f2py location |
---|
139 | # then run it to get info about the verision and the number of bits |
---|
140 | for program in ['python','../python']: |
---|
141 | if sys.platform == "win32" and os.path.splitext(program)[1].lower() != '.exe': |
---|
142 | program = program + '.exe' |
---|
143 | pythonprogram = os.path.normpath(os.path.join(F2PYpath,program)) |
---|
144 | if is_exe(pythonprogram): break |
---|
145 | else: |
---|
146 | print 'python not found' |
---|
147 | sys.exit() |
---|
148 | p = subprocess.Popen(pythonprogram, stdout=subprocess.PIPE, stdin=subprocess.PIPE) |
---|
149 | p.stdin.write("import sys,platform;print str(sys.version_info[0]);print str(sys.version_info[1]);print platform.architecture()[0];sys.exit()") |
---|
150 | p.stdin.close() |
---|
151 | p.wait() |
---|
152 | versions = p.stdout.readlines() |
---|
153 | version = str(int(versions[0])) + '.' + str(int(versions[1])) |
---|
154 | PlatformBits = versions[2][:-1] |
---|
155 | # |
---|
156 | |
---|
157 | # Install location |
---|
158 | InstallLoc = '../bin' |
---|
159 | if len(COMMAND_LINE_TARGETS) == 0: |
---|
160 | Default(InstallLoc) |
---|
161 | elif 'install' in COMMAND_LINE_TARGETS: |
---|
162 | if PlatformBits == '64bit': |
---|
163 | bits = '64' |
---|
164 | else: |
---|
165 | bits = '' |
---|
166 | if sys.platform == "win32": |
---|
167 | prefix = 'binwin' |
---|
168 | elif sys.platform == "darwin": |
---|
169 | prefix = 'binmac' |
---|
170 | elif sys.platform == "linux2": |
---|
171 | prefix = 'binlinux' |
---|
172 | InstallLoc = '../' + prefix + bits + version |
---|
173 | Alias('install',InstallLoc) |
---|
174 | |
---|
175 | # Setup build Environment |
---|
176 | env = Environment() |
---|
177 | # Define a builder to run f2py |
---|
178 | def generate_f2py(source, target, env, for_signature): |
---|
179 | module = os.path.splitext(str(source[0]))[0] |
---|
180 | if len(liblist) > 0: |
---|
181 | for lib in liblist: |
---|
182 | module = module + ' ' + str(lib) |
---|
183 | return os.path.join(F2PYpath,'f2py') + ' -c $SOURCE -m ' + module + ' ' + F2PYflags |
---|
184 | f2py = Builder(generator = generate_f2py) |
---|
185 | env.Append(BUILDERS = {'f2py' : f2py},) |
---|
186 | |
---|
187 | if ARGUMENTS.get('FCompiler', None) is not None or env.get('FORTRAN') is None: |
---|
188 | # create a builder for the fortran compiler for library compilation, if needed |
---|
189 | def generate_obj(source, target, env, for_signature): |
---|
190 | dir = os.path.split(str(source[0]))[0] |
---|
191 | obj = os.path.splitext(str(source[0]))[0]+'.o' |
---|
192 | return os.path.join(FORTpath,FCompiler) + ' -c $SOURCE ' + FORTflags + ' -I' + dir + ' -o' + obj |
---|
193 | fort = Builder(generator = generate_obj, suffix = '.o', |
---|
194 | src_suffix = '.for') |
---|
195 | env.Append(BUILDERS = {'fort' : fort},) |
---|
196 | FortranCompile = env.fort |
---|
197 | print 'Defining a FORTRAN builder' |
---|
198 | else: |
---|
199 | FortranCompile = Object |
---|
200 | env['FORTRANFLAGS'] = FORTflags |
---|
201 | |
---|
202 | # Setup build Environment |
---|
203 | if FORTpath != "": env.PrependENVPath('PATH', FORTpath) |
---|
204 | var = 'LDFLAGS' |
---|
205 | env['ENV'][var] = eval(var) |
---|
206 | |
---|
207 | # find libraries to build (subdirectories named *subs) |
---|
208 | for sub in glob.glob('*subs'): |
---|
209 | filelist = [] |
---|
210 | for file in glob.glob(os.path.join(sub,'*.for')): |
---|
211 | #target = os.path.splitext(file)[0]+'.o' |
---|
212 | target = FortranCompile(file) # connect .o files to .for files |
---|
213 | #print 'Compile: ',file, target |
---|
214 | filelist.append(target) |
---|
215 | #lib = Library(sub, Glob(os.path.join(sub,'*.for'))) # register library to be created |
---|
216 | lib = Library(sub, filelist) # register library to be created |
---|
217 | liblist.append(lib[0].name) |
---|
218 | filename = str(lib[0]) |
---|
219 | #Install(InstallLoc,filename) |
---|
220 | # find modules that need to be built |
---|
221 | modlist = [] |
---|
222 | for src in glob.glob('*.for'): |
---|
223 | target = os.path.splitext(src)[0] + F2PYsuffix |
---|
224 | out = env.f2py(target,src) |
---|
225 | Depends(target, liblist) # make sure libraries are rebuilt if old |
---|
226 | modlist.append(out[0].name) |
---|
227 | env.Install(InstallLoc, out[0].name) |
---|
228 | break # bail out early for testing |
---|
229 | print 80*'=' |
---|
230 | for var in ['FCompiler','FORTpath','FORTflags','F2PYflags','F2PYpath','F2PYsuffix','LDFLAGS']: |
---|
231 | print 'Variable ',var,'is',eval(var) |
---|
232 | print 'Required fortran libraries:', |
---|
233 | for lib in liblist: print " " + lib, |
---|
234 | print "" |
---|
235 | print 'f2py modules:', |
---|
236 | for mod in modlist: print " " + mod, |
---|
237 | print "" |
---|
238 | print 'Using python at', pythonprogram |
---|
239 | print 'Python/f2py version =',version,PlatformBits |
---|
240 | print 'Install directory is',InstallLoc |
---|
241 | print 80*'=' |
---|
242 | #print env.Dump() |
---|