Add some documentation to the SCons-version-switching hack
[senf.git] / tools / scons-1.2.0 / engine / SCons / Tool / PharLapCommon.py
1 """SCons.Tool.PharLapCommon
2
3 This module contains common code used by all Tools for the
4 Phar Lap ETS tool chain.  Right now, this is linkloc and
5 386asm.
6
7 """
8
9 #
10 # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 The SCons Foundation
11 #
12 # Permission is hereby granted, free of charge, to any person obtaining
13 # a copy of this software and associated documentation files (the
14 # "Software"), to deal in the Software without restriction, including
15 # without limitation the rights to use, copy, modify, merge, publish,
16 # distribute, sublicense, and/or sell copies of the Software, and to
17 # permit persons to whom the Software is furnished to do so, subject to
18 # the following conditions:
19 #
20 # The above copyright notice and this permission notice shall be included
21 # in all copies or substantial portions of the Software.
22 #
23 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
24 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
25 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #
31
32 __revision__ = "src/engine/SCons/Tool/PharLapCommon.py 3842 2008/12/20 22:59:52 scons"
33
34 import os
35 import os.path
36 import SCons.Errors
37 import SCons.Util
38 import re
39 import string
40
41 def getPharLapPath():
42     """Reads the registry to find the installed path of the Phar Lap ETS
43     development kit.
44
45     Raises UserError if no installed version of Phar Lap can
46     be found."""
47
48     if not SCons.Util.can_read_reg:
49         raise SCons.Errors.InternalError, "No Windows registry module was found"
50     try:
51         k=SCons.Util.RegOpenKeyEx(SCons.Util.HKEY_LOCAL_MACHINE,
52                                   'SOFTWARE\\Pharlap\\ETS')
53         val, type = SCons.Util.RegQueryValueEx(k, 'BaseDir')
54
55         # The following is a hack...there is (not surprisingly)
56         # an odd issue in the Phar Lap plug in that inserts
57         # a bunch of junk data after the phar lap path in the
58         # registry.  We must trim it.
59         idx=val.find('\0')
60         if idx >= 0:
61             val = val[:idx]
62                     
63         return os.path.normpath(val)
64     except SCons.Util.RegError:
65         raise SCons.Errors.UserError, "Cannot find Phar Lap ETS path in the registry.  Is it installed properly?"
66
67 REGEX_ETS_VER = re.compile(r'#define\s+ETS_VER\s+([0-9]+)')
68
69 def getPharLapVersion():
70     """Returns the version of the installed ETS Tool Suite as a
71     decimal number.  This version comes from the ETS_VER #define in
72     the embkern.h header.  For example, '#define ETS_VER 1010' (which
73     is what Phar Lap 10.1 defines) would cause this method to return
74     1010. Phar Lap 9.1 does not have such a #define, but this method
75     will return 910 as a default.
76
77     Raises UserError if no installed version of Phar Lap can
78     be found."""
79
80     include_path = os.path.join(getPharLapPath(), os.path.normpath("include/embkern.h"))
81     if not os.path.exists(include_path):
82         raise SCons.Errors.UserError, "Cannot find embkern.h in ETS include directory.\nIs Phar Lap ETS installed properly?"
83     mo = REGEX_ETS_VER.search(open(include_path, 'r').read())
84     if mo:
85         return int(mo.group(1))
86     # Default return for Phar Lap 9.1
87     return 910
88
89 def addPathIfNotExists(env_dict, key, path, sep=os.pathsep):
90     """This function will take 'key' out of the dictionary
91     'env_dict', then add the path 'path' to that key if it is not
92     already there.  This treats the value of env_dict[key] as if it
93     has a similar format to the PATH variable...a list of paths
94     separated by tokens.  The 'path' will get added to the list if it
95     is not already there."""
96     try:
97         is_list = 1
98         paths = env_dict[key]
99         if not SCons.Util.is_List(env_dict[key]):
100             paths = string.split(paths, sep)
101             is_list = 0
102         if not os.path.normcase(path) in map(os.path.normcase, paths):
103             paths = [ path ] + paths
104         if is_list:
105             env_dict[key] = paths
106         else:
107             env_dict[key] = string.join(paths, sep)
108     except KeyError:
109         env_dict[key] = path
110
111 def addPharLapPaths(env):
112     """This function adds the path to the Phar Lap binaries, includes,
113     and libraries, if they are not already there."""
114     ph_path = getPharLapPath()
115
116     try:
117         env_dict = env['ENV']
118     except KeyError:
119         env_dict = {}
120         env['ENV'] = env_dict
121     addPathIfNotExists(env_dict, 'PATH',
122                        os.path.join(ph_path, 'bin'))
123     addPathIfNotExists(env_dict, 'INCLUDE',
124                        os.path.join(ph_path, 'include'))
125     addPathIfNotExists(env_dict, 'LIB',
126                        os.path.join(ph_path, 'lib'))
127     addPathIfNotExists(env_dict, 'LIB',
128                        os.path.join(ph_path, os.path.normpath('lib/vclib')))
129     
130     env['PHARLAP_PATH'] = getPharLapPath()
131     env['PHARLAP_VERSION'] = str(getPharLapVersion())
132