Changeset 1437 for trunk/scons


Ignore:
Timestamp:
08/28/08 13:57:50 (16 years ago)
Author:
Malte Marquarding
Message:

handle fortran in scons >=0.98

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/scons/utils.py

    r1332 r1437  
     1import sys
    12import os
    23import glob
     4import re
     5import platform
    36
    47def generate(env):
    5     def SGlob(pattern):
    6         path = env.GetBuildPath('SConscript').replace('SConscript', '')
    7         return [ i.replace(path, '') for i in glob.glob(path + pattern) ]
     8
     9    def SGlob(pattern, excludedirs=[], recursive=False):
     10        # always exclude .svn
     11        excludedirs.append(".svn")
     12        path = env.GetBuildPath('SConscript').replace('SConscript', '')
     13        if recursive:
     14            # remove '*' from pattern is accidentally specified
     15            pattern=pattern.replace("*", "")
     16            out = []
     17            for d, ld, fls in os.walk(path):
     18                # remove directorys to be excluded
     19                for exd in excludedirs:
     20                    if exd in ld:
     21                        ld.remove(exd)
     22                for f in fls:               
     23                    if f.endswith(pattern):
     24                        drel=d.replace(path,"")
     25                        out.append(os.path.join(drel,f))
     26            return out
     27        else:
     28            return [ i.replace(path, '') for i in  glob.glob(path + pattern) ]
    829    env.SGlob = SGlob
    930
    10     def AddCustomPath(path=""):
    11         if not len(path) or not os.path.exists(path):
    12             return
     31    def AddCustomPath(path=None):
     32        if path is None or not os.path.exists(path):
     33            env.Exit(1)
    1334        env.PrependUnique(CPPPATH = [os.path.join(path, "include")])
    1435        env.PrependUnique(LIBPATH = [os.path.join(path, "lib")])
     
    1738    def AddCustomPackage(pkgname=None):
    1839        if pkgname is None:
    19             return
    20         pkgroot = env.get("%sroot" % pkgname, None)
    21         pkgincd = env.get("%sincdir" % pkgname, None)
    22         pkglibd = env.get("%slibdir" % pkgname, None)
    23         incd = None
    24         libd = None
    25         if pkgroot is not None:
    26             incd = os.path.join(pkgroot, "include")
    27             libd = os.path.join(pkgroot, "lib")
    28         else:      
    29             if pkgincd is not None:
    30                 incd = pkgincd
    31             if pkglibd is not None:
    32                 libd = pkglibd
    33         if incd is not None:
    34             if not os.path.exists(incd):
    35                 print "Custom %s include dir '%s' not found" % (pkgname, incd)
    36                 env.Exit(1)
    37             env.PrependUnique(CPPPATH = [incd])
    38         if libd is not None:
    39             if not os.path.exists(libd):
    40                 print "Custom %s lib dir '%s' not found" % (pkgname, libd)
    41                 env.Exit(1)
    42             env.PrependUnique(LIBPATH = [libd])
     40            return
     41        pkgroot = env.get("%sroot" % pkgname, None)
     42        pkgincd = env.get("%sincdir" % pkgname, None)
     43        pkglibd = env.get("%slibdir" % pkgname, None)
     44        incd = None
     45        libd = None
     46        if pkgroot is not None:
     47            incd = os.path.join(pkgroot, "include")
     48            libd = os.path.join(pkgroot, "lib")
     49        else:      
     50            if pkgincd is not None:
     51                incd = pkgincd
     52            if pkglibd is not None:
     53                libd = pkglibd
     54        if incd is not None:
     55            if not os.path.exists(incd):
     56                print "Custom %s include dir '%s' not found" % (pkgname, incd)
     57                env.Exit(1)
     58            env.PrependUnique(CPPPATH = [incd])
     59        if libd is not None:
     60            if not os.path.exists(libd):
     61                print "Custom %s lib dir '%s' not found" % (pkgname, libd)
     62                env.Exit(1)
     63            env.PrependUnique(LIBPATH = [libd])
    4364
    4465    env.AddCustomPackage = AddCustomPackage
    4566
     67    def PlatformIdent():
     68        p = sys.platform
     69        # replace the trailing 2 in linux2
     70        p = re.sub(re.compile("2$"), "", p)
     71        return p + "_" + platform.machine()
     72    env.PlatformIdent = PlatformIdent
     73
     74    def MergeFlags():
     75        def _to_list(xf):
     76            if xf.count(","):
     77                return xf.split(",")
     78            return xf.split()
     79
     80        xf=env.get("extracppflags", None)
     81        if xf:
     82            env.AppendUnique(CPPFLAGS=_to_list(xf))
     83        xf=env.get("extralinkflags", None)
     84        if xf:
     85            env.AppendUnique(LINKFLAGS=_to_list(xf))
     86            env.AppendUnique(SHLINKFLAGS=_to_list(xf))
     87        xf=env.get("extracxxflags", None)
     88        if xf:
     89            env.AppendUnique(CXXFLAGS=_to_list(xf))
     90        xf=env.get("extrafflags", None)
     91        if xf:
     92            env.AppendUnique(FORTRANFLAGS=_to_list(xf))
     93            env.AppendUnique(SHFORTRANFLAGS=_to_list(xf))
     94        xf=env.get("extracflags", None)
     95        if xf:
     96            env.AppendUnique(CCFLAGS=_to_list(xf))
     97    # set the extra flags if available
     98    MergeFlags()
     99       
    46100    def CheckFortran(conf):
    47            
    48         if not conf.env.has_key("FORTRAN"):
    49             # auto-detect fortran
    50             detect_fortran = conf.env.Detect(['gfortran', 'g77', 'f77'])
    51             if not detect_fortran:
    52                 print "No fortran compiler found. Specify FORTRAN and f2clib."
    53                 conf.env.Exit(1)
    54             conf.env["FORTRAN"] = detect_fortran
     101       
     102        def getf2clib(fc):
    55103            fdict = {'gfortran': 'gfortran', 'g77': 'g2c', 'f77': 'f2c'}
    56             f2clib = conf.env.get("f2clib", fdict[detect_fortran])
    57             if not conf.CheckLib(f2clib):
    58                 env.Exit(1)
    59         else:
    60             if not conf.env.has_key("f2clib"):
    61                 print "A custom fortran compiler also needs f2clib defined"
    62                 env.Exit(1)
    63             else:
    64                 if not conf.CheckLib(env["f2clib"]):
    65                     env.Exit(1)
    66         if conf.env["FORTRAN"].startswith("g77"):
     104            return fdict[fc]
     105
     106       
     107        if not conf.env.has_key("FORTRAN"):
     108            # auto-detect fortran
     109            detect_fortran = conf.env.Detect(['gfortran', 'g77', 'f77'])
     110            conf.env["FORTRAN"] = detect_fortran
     111        f2clib = conf.env.get("f2clib", getf2clib(conf.env["FORTRAN"]))
     112        if not conf.CheckLib(f2clib):
     113            conf.env.Exit(1)
     114
     115        if conf.env["FORTRAN"].startswith("g77"):
    67116            fflags = ["-Wno-globals", "-fno-second-underscore"]
    68             conf.env.Append(SHFORTRANFLAGS=fflags)
    69             conf.env.Append(FORTRANFLAGS=fflags)
     117            conf.env.AppendUnique(SHFORTRANFLAGS=fflags)
     118            conf.env.AppendUnique(FORTRANFLAGS=fflags)
     119        conf.env.AppendUnique(SHFORTRANFLAGS=['-fPIC'])
     120
    70121    env.CheckFortran = CheckFortran
    71 
    72     def WalkDirTree(targetroot, sourceroot, sources):
    73         ifiles = []
    74         ofiles = []
    75         for s in sources:
    76             if os.path.isdir(os.path.join(sourceroot ,s)):
    77                 for d,ld,f in os.walk(os.path.join(sourceroot ,s)):
    78                     for fl in f:
    79                         ifile = os.path.join(d, fl)
    80                         ifiles.append(ifile)
    81                         ofile = ifile.replace(sourceroot, targetroot)
    82                         ofiles.append(ofile)
    83         return ofiles, ifiles
    84     env.WalkDirTree = WalkDirTree
    85122
    86123    def null_action(target, source, env): return 0
Note: See TracChangeset for help on using the changeset viewer.