scons fix for lucid
[senf.git] / site_scons / site_init.py
1 import os.path, SCons
2 import inspect
3
4 # SCons is at     #/tools/scons-<v>/engine/SCons/__init__.py
5 # site_init is at #/site_scons/site_init.py
6
7 sconsbase = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(
8     inspect.currentframe().f_code.co_filename))),"tools")
9 sconsbase = os.path.join(
10     sconsbase,sorted((f for f in os.listdir(sconsbase) if f.startswith("scons-")))[-1])
11 sconsengine = os.path.join(sconsbase, 'engine')
12 sconsscript = os.path.join(sconsbase, 'script', 'scons')
13
14 if os.path.dirname(os.path.dirname(os.path.abspath(SCons.__file__))) != sconsengine:
15     import os, sys, SCons.Util
16     SCons.Util.display("scons: Switching version")
17
18     # BEGIN HACK
19     # This is really ugly. SCons has already switched into the
20     # top-level directory when this code is executed. To make 'scons
21     # -u <target>' work, we need to switch back into the orgiginal
22     # startup dir. This directory however is only available in a local
23     # variable called 'target_top' in SCons.Script.Main._main.
24     #
25     # We walk the stack to find a local variable called
26     # 'target_top'. If we can't find it (could really only happen if
27     # the SCons.Main code is changed considerably), we output a
28     # warning, that '-u' will probably fail (we re-start scons from
29     # the top-level directory, relative targets therefore will not
30     # work)
31     frame = sys._getframe()
32     target_top = marker = []
33     while frame and target_top is marker:
34         frame = frame.f_back
35         if frame : target_top = frame.f_locals.get('target_top',marker)
36     if target_top is marker:
37         SCons.Util.display("scons: WARNING: scons -u <target> will probably fail")
38     if not target_top:
39         target_top = '.'
40     if frame:
41         cdir = frame.f_locals.get('cdir', '.')
42     # END HACK
43     
44     os.chdir(target_top)
45     cdir = os.path.normpath(cdir)
46     if cdir.startswith('../'):
47         SCons.Util.display("scons: WARNING: failed to undo -C option")
48     elif cdir != '.' and not cdir.startswith('/'):
49         os.chdir('/'.join(('..' for _ in cdir.split('/'))))
50     os.environ['SCONS_LIB_DIR'] = sconsengine
51     os.execv(sconsscript, sys.argv)