minor fixes for clang++
[senf.git] / site_scons / site_tools / BuildTypeOptions.py
1 import SCons.Util, SCons.Defaults
2 import re
3
4 class BuildTypeOptions:
5     def __init__(self, var):
6         self._var = var
7
8     def __call__(self, target, source, env, for_signature):
9         type = env['final'] and "final" or env['debug'] and "debug" or "normal"
10         return env[self._var + "_" + type]
11
12 _DOLLAR_RE = re.compile(r'\$([a-zA-Z_][\.\w]*)|\${([^}]*)}')
13
14 def _expandDefines(defs, target, source, env, for_signature):
15     rv = []
16     if SCons.Util.is_Dict(defs):
17         keys = defs.keys()
18         keys.sort()
19         defs = [ (k,defs[k]) for k in keys ]
20     elif not SCons.Util.is_List(defs):
21         defs = [ defs ]
22     for elt in defs:
23         if SCons.Util.is_String(elt):
24             m = _DOLLAR_RE.match(elt)
25             if m:
26                 match = m.group(1) or m.group(2)
27                 try: rv.extend(_expandDefines(eval(match, env.gvars(), env.lvars()),
28                                               target, source, env, for_signature))
29                 except NameError: pass
30                 except IndexError: pass
31             else:
32                 rv.append(env.subst(elt))
33         elif callable(elt):
34             rv.extend(_expandDefines(elt(target, source, env, for_signature),
35                                      target, source, env, for_signature))
36         elif SCons.Util.is_Sequence(elt):
37             if len(elt)<2 or elt[1] is None:
38                 rv.append(env.subst(elt[0]))
39             else:
40                 rv.append(env.subst(elt[0]) + "=" + env.subst(elt[1]))
41         else:
42             rv.append(str(elt))
43     return rv
44
45 def expandDefines(prefix, defs, suffix, env):
46     """Expand defines in <defs> using <env>. Calls SCons.Defaults._concat_ixes
47 to append prefix/suffix before/after each define.
48
49     callable
50         Call the callable and replace it with the call result. If the result
51         is a list, the list is processed recursively. It the result is a
52         dictionary it is converted into a list of typles and processed
53         recursively.
54     '$<variable>' or '${<variable>}'
55         Replace the element with the variable expansion. If the result is a
56         list, the list is processed recursively. It the result is a
57         dictionary it is converted into a list of typles and processed
58         recursively.
59     '<any other string>'
60         Define a symbol with that (expanded!) name
61     iterable
62         The iteratble must have two elments. The first element defines the
63         symbol name, the second the symbol value."""
64
65     defs = _expandDefines(defs, None, None, env, False)
66     return SCons.Defaults._concat_ixes(prefix, defs, suffix, env)
67     
68 def _BuildTypeOptions(env, var) : return BuildTypeOptions(var)
69
70 def generate(env):
71     env.Replace(
72         _defines          = expandDefines,
73         )
74     
75     env.AddMethod(_BuildTypeOptions, 'BuildTypeOptions')
76
77 def exists(env):
78     return True