Changeset 3178 for trunk


Ignore:
Timestamp:
Dec 6, 2017 9:03:34 PM (8 years ago)
Author:
toby
Message:

revised Scons input; builds on all platforms

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/fsource/SConstruct

    r3136 r3178  
    22# it compiles the Fortran files needed to be used as Python packages for GSAS-II
    33#
    4 # if the default options need to be overridden for this to work on your system, please let me
     4# if the default options need to be overridden for this to work on your system, please let us
    55# know the details of what OS, compiler and python you are using as well as the command-line
    66# options you need.
     
    1010import glob
    1111import subprocess
     12import numpy as np
    1213#==========================================================================================
    1314def is_exe(fpath):
     
    2829                return path
    2930    return ""
     31
     32def GetBinaryDir():
     33    # format current platform, Python & numpy version
     34    if sys.platform == "win32":
     35        prefix = 'win'
     36    elif sys.platform == "darwin":
     37        prefix = 'mac'
     38    elif sys.platform.startswith("linux"):
     39        prefix = 'linux'
     40    else:
     41        print(u'Unknown platform: '+sys.platform)
     42        raise Exception('Unknown platform')
     43    if platform.architecture()[0] == '64bit':
     44        bits = '64'
     45    else:
     46        bits = '32'
     47    pyver = 'p{}.{}'.format(*sys.version_info[0:2])
     48    npver = 'n' + np.__version__[:np.__version__.find('.',2)]
     49    return '_'.join([prefix,bits,pyver,npver])
    3050#==========================================================================================
    3151# misc initializations
     
    7494FORTpath = ""
    7595FORTflags = ""
    76 liblist = []
    7796LDFLAGS = ''
    7897#==========================================================================================
     
    84103    elif GFORTpath != "":
    85104      FCompiler='gfortran'
    86 #      LDFLAGS = '-static-libgfortran -static-libgcc'
    87105    else:
    88106      print ('No Fortran compiler in path')
    89107      sys.exit()
    90108elif sys.platform == "darwin":
    91     LDFLAGS = '-undefined dynamic_lookup -bundle -static-libgfortran -static-libgcc'
     109    LDFLAGS = '-undefined dynamic_lookup -bundle'
    92110    F2PYsuffix = '.so'
    93 elif sys.platform == "linux2":
     111elif sys.platform.startswith("linux"):
    94112    #LDFLAGS = '-Wall -shared -static-libgfortran -static-libgcc' # does not work with gfortran 4.4.4 20100726 (Red Hat 4.4.4-13)
    95113    F2PYsuffix = '.so'
     
    97115    print ("Sorry, parameters for platform "+sys.platform+" are not yet defined")
    98116    sys.exit()
     117if FCompiler == 'gfortran':
     118    if ARGUMENTS.get('LIBGCC', '').upper().startswith('T'):
     119        LDFLAGS += ' -static-libgcc'
     120        print('LIBGCC')
     121    if ARGUMENTS.get('LIBGFORTRAN', '').upper().startswith('T'):
     122        LDFLAGS += ' -static-libgfortran'
     123        print('LIBGfortran')
     124   
    99125#==========================================================================================
    100126# help
     
    113139-n      -- causes scons to show but not execute the commands it will perform
    114140
    115 -c      -- clean: causes scons to delete previously created files (don't use
    116    with install)
     141-c      -- clean: causes scons to delete previously created files (but not from
     142   install directory)
    117143
    118144help    -- causes this message to be displayed (no compiling is done)
    119145
    120 install -- causes the module files to be placed in an installation directory
    121    (../bin<X>NNv.v) rather than ../bin (<X> is mac, win or linux; NN is 64-
    122    or blank; v.v is the python version (2.6 or 2.7,...). Normally used only
    123    by Brian or Bob for distribution of compiled software.
     146install=T -- causes the module files to be placed in an installation directory
     147   (../AllBinaries/<X>_NN_pv.v_nv.v) rather than ../bin, where:
     148     <X> is mac, win or linux;
     149     NN is 64 or 32;
     150     pv.v is the Python version (p2.7 or p3.6,...) and
     151     nv.v is the Numpy version (n1.13,...).
     152   Normally used only by Brian or Bob for distribution of compiled software.
    124153
    125154The following options override defaults set in the scons script:
     
    144173   mac/linux: '.so')
    145174
     175LIBGCC=T -- adds the option -static-libgcc as an link option with gfortran. This
     176   prevents use of the dynamic libgcc library, which must be then present on each
     177   run-time computer. To use this, gfortran must be installed with the static
     178   libraries.
     179
     180LIBGFORTRAN=T -- adds the option -static-libgfortran as an link option with
     181   gfortran. This prevents use of the dynamic libgcc library, which then must be
     182   present on each run-time computer. To use this, gfortran must be installed
     183   with the static libraries.
     184     
    146185LDFLAGS='string'   -- string of options to be used for f2py during link step
    147186
     
    188227version = str(int(versions[0])) + '.' + str(int(versions[1]))
    189228PlatformBits = '64bits'
    190 if '32' in platform.python_compiler():
     229if '32' in platform.architecture()[0]:
    191230   PlatformBits = '32bits'
    192231#PlatformBits = versions[2][:-1]
    193232#
    194 print (version,PlatformBits)
    195 # Install location
    196 InstallLoc = '../bin'
    197 if len(COMMAND_LINE_TARGETS) == 0:
    198     Default(InstallLoc)
    199 elif 'install' in COMMAND_LINE_TARGETS:
    200     if PlatformBits == '64bit':
    201         bits = '64-'
    202     else:
    203         bits = ''
    204     if sys.platform == "win32":
    205         prefix = 'binwin'
    206     elif sys.platform == "darwin":
    207         prefix = 'binmac'
    208     elif sys.platform == "linux2":
    209         prefix = 'binlinux'
    210     InstallLoc = '../' + prefix + bits + version
    211     Alias('install',InstallLoc)
     233# Set install location
     234if ARGUMENTS.get('install', '').upper().startswith('T'):
     235    InstallLoc = os.path.join('..','AllBinaries', GetBinaryDir())
     236else:
     237    InstallLoc = os.path.join('..','bin')
    212238#==========================================================================================
    213239# use the compiler choice to set compiler options, but don't change anything
     
    215241if FCompiler == 'gfortran':
    216242    if FORTpath == "": FORTpath = GFORTpath
    217     if sys.platform == "linux2" and PlatformBits == '64bit':
     243    if sys.platform.startswith("linux") and "64" in PlatformBits:
    218244        if FORTflags == "": FORTflags = ' -w -O2 -fPIC -m64'
    219245        if F2PYflags == "": F2PYflags = '--fcompiler=gnu95 --f77exec=gfortran --f77flags="-fno-range-check -m64"'# --arch="-arch x86_64"'
    220     elif sys.platform == "linux2":
     246    elif sys.platform.startswith("linux"):
    221247        if FORTflags == "": FORTflags = ' -w -O2 -fPIC -m32'
    222248        if F2PYflags == "": F2PYflags = '--fcompiler=gnu95 --f77exec=gfortran --f77flags="-fno-range-check -m32"'
    223     elif sys.platform == "darwin" and PlatformBits == '64bit':
     249    elif sys.platform == "darwin": # now 64 bit only
     250        #if "64" in PlatformBits:
    224251        LDFLAGS += " -arch x86_64 -m64"
    225252        if FORTflags == "": FORTflags = ' -w -O2 -m64'
    226253        if F2PYflags == "": F2PYflags = '--fcompiler=gnu95 --f77exec=gfortran --f77flags="-fno-range-check -m64"'
    227     elif sys.platform == "darwin":
    228         if F2PYflags == "": F2PYflags = '--fcompiler=gnu95 --f77exec=gfortran --f77flags="-fno-range-check -m32"'
     254    elif sys.platform == "win32" and "64" in PlatformBits:
     255        if FORTflags == "": FORTflags = ' -w -O2 -m64'
     256        if F2PYflags == "":
     257            F2PYflags = '--compiler=mingw32 --fcompiler=gfortran --f77flags="-fno-range-check -m64"'
    229258    elif sys.platform == "win32":
    230          if F2PYflags == "": F2PYflags = '--compiler=mingw32 --fcompiler=gfortran --f77flags="-fno-range-check -m64"'
     259        #if FORTflags == "": FORTflags = ' -w -O2 -m32'
     260        if F2PYflags == "":
     261            F2PYflags = '--compiler=mingw32 --fcompiler=gfortran --f77flags="-fno-range-check"'
    231262elif FCompiler == 'g77':
    232263    if FORTpath == "": FORTpath = G77path
     
    252283            module = module + ' ' + str(lib)
    253284    f2pyprogram = os.path.normpath(os.path.join(F2PYpath,F2PYprog))
    254     if os.path.splitext(F2PYprog)[1] == '.py':
    255         return pythonprogram + ' ' + f2pyprogram + ' -c $SOURCE ' + ' -m ' + module + ' ' + F2PYflags
    256     else:
    257         return f2pyprogram + ' -c $SOURCE' + ' -m ' + module + ' ' + F2PYflags
     285    if os.path.splitext(F2PYprog)[1] == '.py':     # use f2py.py if no f2py[.exe]
     286        f2pycmd = pythonprogram + ' ' + f2pyprogram + ' -c $SOURCE ' + ' -m ' + module + ' ' + F2PYflags
     287    else:
     288        f2pycmd = f2pyprogram + ' -c $SOURCE' + ' -m ' + module + ' ' + F2PYflags
     289    if sys.platform == "win32":
     290        installcmd = "copy " + os.path.splitext(str(source[0]))[0] + '*' + F2PYsuffix + ' ' + InstallLoc
     291    else:
     292        installcmd = "cp " + os.path.splitext(str(source[0]))[0] + '*' + F2PYsuffix + ' ' + InstallLoc
     293    return [f2pycmd, installcmd]
    258294f2py = Builder(generator = generate_f2py)
    259295env.Append(BUILDERS = {'f2py' : f2py},)
     
    282318#   add other needed environment variables
    283319var = 'LDFLAGS'
    284 if eval(var) != "": env['ENV'][var] = eval(var)
     320if eval(var) != "":
     321    env['ENV'][var] = eval(var)
     322    print("Setting environment variable {} to {}".format(var,eval(var)))
     323if 'WINDIR' in os.environ: env['ENV']['WINDIR'] = os.environ['WINDIR']
    285324
    286325#==========================================================================================
    287326# finally ready to build something!
    288327# locate libraries to be built (subdirectories named *subs)
     328liblist = []
    289329for sub in glob.glob('*subs'):
    290330    filelist = []
     
    301341    liblist.append(lib[0].name)
    302342    filename = str(lib[0])
    303     #Install(InstallLoc,filename)
    304343# find modules that need to be built
    305344modlist = []
     
    307346    target = os.path.splitext(src)[0] + F2PYsuffix
    308347    out = env.f2py(target,src)
     348    Clean(out, Glob(os.path.splitext(src)[0] + "*" + F2PYsuffix)) # this picks up old- & new-style .pyd/.so names
    309349    Depends(target, liblist) # make sure libraries are rebuilt if old
    310350    modlist.append(out[0].name)
    311     env.Install(InstallLoc, out[0].name)
    312351    #break # bail out early for testing
    313352#==========================================================================================
     
    328367#print (env.Dump())
    329368if 'help' in COMMAND_LINE_TARGETS: sys.exit()
     369if not os.path.exists(InstallLoc):
     370    print('Creating '+InstallLoc)
     371    os.makedirs(InstallLoc)
Note: See TracChangeset for help on using the changeset viewer.