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