source: trunk/fsource/SConstruct @ 324

Last change on this file since 324 was 324, checked in by vondreele, 12 years ago

updated LDFLAGS for linux -- broken; commented

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