source: trunk/scons/utils.py

Last change on this file was 2517, checked in by Malte Marquarding, 12 years ago

More updates to get a distutils/scons build going

File size: 4.7 KB
Line 
1import sys
2import os
3import glob
4import re
5import platform
6
7def get_libdir():
8    import distutils.sysconfig
9    return os.path.basename(distutils.sysconfig.get_config_var('LIBDIR'))
10
11LIBDIR = "lib" #get_libdir()
12
13def generate(env):
14
15    def SGlob(pattern, excludedirs=[], recursive=False):
16        # always exclude .svn
17        excludedirs.append(".svn")
18        path = env.GetBuildPath('SConscript').replace('SConscript', '')
19        if recursive:
20            # remove '*' from pattern is accidentally specified
21            pattern=pattern.replace("*", "")
22            out = []
23            for d, ld, fls in os.walk(path):
24                # remove directorys to be excluded
25                for exd in excludedirs:
26                    if exd in ld:
27                        ld.remove(exd)
28                for f in fls:               
29                    if f.endswith(pattern):
30                        drel=d.replace(path,"")
31                        out.append(os.path.join(drel,f))
32            return out
33        else:
34            return [ i.replace(path, '') for i in  glob.glob(path + pattern) ]
35    env.SGlob = SGlob
36
37    def AddCustomPath(path=None):
38        if path is None or not os.path.exists(path):
39            env.Exit(1)
40        env.PrependUnique(CPPPATH = [os.path.join(path, "include")])
41        env.PrependUnique(LIBPATH = [os.path.join(path, LIBDIR)])
42    env.AddCustomPath = AddCustomPath
43
44    def AddCustomPackage(pkgname=None):
45        if pkgname is None:
46            return
47        pkgroot = env.get("%sroot" % pkgname, None)
48        pkgincd = env.get("%sincdir" % pkgname, None)
49        pkglibd = env.get("%slibdir" % pkgname, None)
50        incd = None
51        libd = None
52        if pkgroot is not None:
53            incd = os.path.join(pkgroot, "include")
54            libd = os.path.join(pkgroot, LIBDIR)
55        else:       
56            if pkgincd is not None:
57                incd = pkgincd
58            if pkglibd is not None:
59                libd = pkglibd
60        if incd is not None:
61            if not os.path.exists(incd):
62                print "Custom %s include dir '%s' not found" % (pkgname, incd)
63                env.Exit(1)
64            env.PrependUnique(CPPPATH = [incd])
65        if libd is not None:
66            if not os.path.exists(libd):
67                print "Custom %s lib dir '%s' not found" % (pkgname, libd)
68                env.Exit(1)
69            env.PrependUnique(LIBPATH = [libd])
70
71    env.AddCustomPackage = AddCustomPackage
72
73    def PlatformIdent():
74        p = sys.platform
75        # replace the trailing 2 in linux2
76        p = re.sub(re.compile("2$"), "", p)
77        return p + "_" + platform.machine()
78    env.PlatformIdent = PlatformIdent
79
80    def MergeFlags():
81        def _to_list(xf):
82            if xf.count(","):
83                return xf.split(",")
84            return xf.split()
85
86        xf=env.get("extracppflags", None)
87        if xf:
88            env.AppendUnique(CPPFLAGS=_to_list(xf))
89        xf=env.get("extralinkflags", None)
90        if xf:
91            env.AppendUnique(LINKFLAGS=_to_list(xf))
92            env.AppendUnique(SHLINKFLAGS=_to_list(xf))
93        xf=env.get("extracxxflags", None)
94        if xf:
95            env.AppendUnique(CXXFLAGS=_to_list(xf))
96        xf=env.get("extrafflags", None)
97        if xf:
98            env.AppendUnique(FORTRANFLAGS=_to_list(xf))
99            env.AppendUnique(SHFORTRANFLAGS=_to_list(xf))
100        xf=env.get("extracflags", None)
101        if xf:
102            env.AppendUnique(CCFLAGS=_to_list(xf))
103    # set the extra flags if available
104    MergeFlags()
105       
106    def CheckFortran(conf):
107       
108        def getf2clib(fc):
109            fdict = {'gfortran': 'gfortran', 'g77': 'g2c', 'f77': 'f2c'}
110            return fdict[fc]
111
112       
113        if not conf.env.has_key("FORTRAN"):
114            # auto-detect fortran
115            detect_fortran = conf.env.Detect(['gfortran', 'g77', 'f77'])
116            conf.env["FORTRAN"] = detect_fortran
117        f2clib = conf.env.get("f2clib", getf2clib(conf.env["FORTRAN"]))
118        if not conf.CheckLib(f2clib):
119            conf.env.Exit(1)
120
121        if conf.env["FORTRAN"].startswith("g77"):
122            fflags = ["-Wno-globals", "-fno-second-underscore"]
123            conf.env.AppendUnique(SHFORTRANFLAGS=fflags)
124            conf.env.AppendUnique(FORTRANFLAGS=fflags)
125        conf.env.AppendUnique(SHFORTRANFLAGS=['-fPIC'])
126
127    env.CheckFortran = CheckFortran
128
129    def WalkDirTree(targetroot, sourceroot, sources):
130        ifiles = []
131        ofiles = []
132        for s in sources:
133            if os.path.isdir(os.path.join(sourceroot ,s)):
134                for d,ld,f in os.walk(os.path.join(sourceroot ,s)):
135                    for fl in f:
136                        ifile = os.path.join(d, fl)
137                        ifiles.append(ifile)
138                        ofile = ifile.replace(sourceroot, targetroot)
139                        ofiles.append(ofile)
140        return ofiles, ifiles
141    env.WalkDirTree = WalkDirTree
142
143
144    def null_action(target, source, env): return 0
145
146    def message(target, source, env):
147        return "%s" % target[0]
148    env.MessageAction = env.Action(null_action, message)
149
150def exists(env):
151    try:
152        import os
153        import glob
154    except ImportError:
155        return False
156    else:
157        return True
Note: See TracBrowser for help on using the repository browser.