source: trunk/fsource/SConstruct @ 315

Last change on this file since 315 was 315, checked in by toby, 10 years ago

new scons file for compiling (needs linux & win work); update macbin's; changes to texture to work on Mac; minor cleanup to GSASIIpwd

File size: 7.4 KB
Line 
1import platform
2import sys
3import os
4import glob
5import subprocess
6#==========================================================================================
7def which_path(program):
8    "emulates Unix which: finds a program in path, but returns the path"
9    import os, sys
10    def is_exe(fpath):
11        return os.path.exists(fpath) and os.access(fpath, os.X_OK)
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
26F2PYflags = '' # compiler options for f2py command
27F2PYpath = which_path('f2py')       # default path to f2py & python
28FORTpath = which_path('gfortran')   # default path to compiler
29ENVvars = {}
30liblist = []
31#==========================================================================================
32# configure platform dependent options here:
33if sys.platform == "win32":
34    F2PYsuffix = '.pyd'
35    FORTpath = which_path('g77')   # default path to compiler
36if sys.platform == "darwin":
37    ENVvars['LDFLAGS'] = '-undefined dynamic_lookup -bundle -static-libgfortran -static-libgcc'
38    F2PYflags = '--fcompiler=gnu95 --f77exec=gfortran --f77flags="-fno-range-check"'
39    #F2PYpath = '/Library/Frameworks/Python.framework/Versions/6.2/bin/'     # path to f2py (python 6.2)
40    #F2PYpath = '/Library/Frameworks/Python.framework/Versions/current/bin/' # path to f2py
41    #FORTpath = '/sw/bin'                                                    # path to compilier
42    F2PYsuffix = '.so'
43elif sys.platform == "linux2":
44    #ENVvars['LDFLAGS'] = '-undefined dynamic_lookup -bundle -static-libgfortran -static-libgcc'
45    #F2PYflags = '--fcompiler=gnu95 --f77exec=gfortran --f77flags="-fno-range-check"'
46    F2PYsuffix = '.so'
47else:
48    print "Sorry, parameters for platform "+sys.platform+" are not yet defined"
49    sys.exit()
50#==========================================================================================
51# override from command-line options
52for var in ['F2PYflags','F2PYpath','FORTpath','F2PYsuffix']:
53    if ARGUMENTS.get(var, None) is not None:
54        print 'Setting',var,'to',ARGUMENTS.get(var),'based on command line'
55        exec(var + "= ARGUMENTS.get('" + var +"')")
56var = 'LDFLAGS'
57if ARGUMENTS.get(var, None) is not None:
58    print 'Setting LDFLAGS environment vafiable to',ARGUMENTS.get(var),'based on command line'
59    ENVvars[var] = ARGUMENTS.get(var)
60           
61
62#==========================================================================================
63
64# get the python version number from the python image in the f2py directory
65p = subprocess.Popen([os.path.join(F2PYpath,'python')], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
66p.stdin.write("import sys;print str(sys.version_info[0]);print str(sys.version_info[1]);sys.exit()")
67p.stdin.close()
68p.wait()
69versions = p.stdout.readlines()
70version = str(int(versions[0])) + '.' + str(int(versions[1]))
71#
72
73# Install location
74InstallLoc = '../bin'
75if len(COMMAND_LINE_TARGETS) == 0:
76    Default(InstallLoc)
77elif 'install' in COMMAND_LINE_TARGETS:
78    if platform.architecture()[0] == '64bit':
79        bits = '64'
80    else:
81        bits = ''
82    if sys.platform == "win32":
83        prefix = 'binwin'
84    elif sys.platform == "darwin":
85        prefix = 'binmac'
86    elif sys.platform == "linux2":
87        prefix = 'binlinux'
88    InstallLoc = '../' + prefix + bits + version
89    Alias('install',InstallLoc)
90elif 'help' in COMMAND_LINE_TARGETS:
91    print """
92Building Fortran routines for use with GSAS-II
93----------------------------------------------
94
95To build the compiled modules files needed to run GSAS-II, invoke this script:
96    scons [options]
97where the following options are defined (all are optional):
98
99-Q      -- produces less output from scons
100
101-n      -- causes scons to show but not execute the commands it will perform
102
103-c      -- clean: causes scons to delete previously created files (don't use
104   with install)
105
106help    -- causes this message to be displayed (no compiling is done)
107
108install -- causes the module files to be placed in an installation directory
109   (../bin<X>NNv.v) rather than ../bin (<X> is mac, win or linux; NN is 64
110   or blank; v.v is the python version (2.6 or 2.7,...). Normally used only
111   by Brian or Bob for distribution of compiled software.
112
113The following options override defaults set in the scons script:
114
115F2PYflags='string' -- defines optional flags to be supplied to f2py:
116   Typically these option define which fortran compiler to use.
117
118F2PYpath=<path>    -- define a path to the f2py program; default is to use
119   first f2py found in path
120
121FORTpath=<path>    -- define a path to the f2py program; default is to use
122   first gfortran (g77 for Windows) found in path
123
124F2PYsuffix='.xxx'  -- extension for output module files (default: win: '.pyd',
125   mac/linux: '.so')
126
127LDFLAGS='string'   -- string of options to be used for f2py during link step
128
129Note that at present, only 32-bit python is supported and python 3.x is
130not supported.
131
132examples:
133    scons -Q
134       (builds into ../bin for current platform using default options)
135    scons -Q install
136       (builds into ../bin<platform> for module distribution)
137    scons -Q install F2PYpath=/Library/Frameworks/Python.framework/Versions/6.2/bin/
138       (builds with a non-default [e.g. older] version of python)
139    """
140    sys.exit()
141#else:
142#    raise Exception, 'Unknown scons option(s): ' + str(COMMAND_LINE_TARGETS)
143
144# commands needed to run f2py
145def generate_f2py(source, target, env, for_signature):
146    module = os.path.splitext(str(source[0]))[0]
147    if len(liblist) > 0:
148        for lib in liblist:
149            module = module + ' ' + str(lib)
150    return os.path.join(F2PYpath,'f2py')  + ' -c $SOURCE -m ' + module + ' ' + F2PYflags
151f2py = Builder(generator = generate_f2py)
152env = Environment(BUILDERS = {'f2py' : f2py},)
153# Setup build Environment
154if FORTpath != "":  env.PrependENVPath('PATH', FORTpath)
155#for path in PATH:
156#    env.PrependENVPath('PATH', path)
157for envvar in ENVvars:
158    env['ENV'][envvar] = ENVvars[envvar]
159
160#out = env.f2py('fellipse.for')
161#filename = str(out[0])
162#print filename
163#print os.path.join(InstallLoc,filename)
164#Command(os.path.join(InstallLoc,filename),filename,Copy("$TARGET","$SOURCE"))
165
166
167# find libraries to build (subdirectories named *subs)
168for sub in glob.glob('*subs'):
169    lib = Library(sub, Glob(os.path.join(sub,'*.for'))) # register library to be created
170    liblist.append(lib[0].name)
171    filename = str(lib[0])
172    #Install(InstallLoc,filename)
173# find modules that need to be built
174modlist = []
175for src in glob.glob('*.for'):
176    target = os.path.splitext(src)[0] + '.so'
177    out = env.f2py(target,src)
178    Depends(target, liblist) # make sure libraries are rebuilt if old
179    modlist.append(out[0].name)
180    env.Install(InstallLoc, out[0].name)
181print 80*'='
182for var in ['F2PYflags','F2PYpath','FORTpath','F2PYsuffix']:
183    print 'Variable ',var,'is',eval(var)
184print 'Required fortran libraries:',
185for lib in liblist: print " " + lib,
186print ""
187print 'f2py modules:',
188for mod in modlist: print " " + mod,
189print ""
190print 'Python/f2py version =',version
191print 'Install directory is',InstallLoc
192print 80*'='
Note: See TracBrowser for help on using the repository browser.