Add some documentation to the SCons-version-switching hack
[senf.git] / scons / scons-1.2.0 / engine / SCons / Tool / mwcc.py
1 """SCons.Tool.mwcc
2
3 Tool-specific initialization for the Metrowerks CodeWarrior compiler.
4
5 There normally shouldn't be any need to import this module directly.
6 It will usually be imported through the generic SCons.Tool.Tool()
7 selection method.
8 """
9
10 #
11 # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 The SCons Foundation
12 #
13 # Permission is hereby granted, free of charge, to any person obtaining
14 # a copy of this software and associated documentation files (the
15 # "Software"), to deal in the Software without restriction, including
16 # without limitation the rights to use, copy, modify, merge, publish,
17 # distribute, sublicense, and/or sell copies of the Software, and to
18 # permit persons to whom the Software is furnished to do so, subject to
19 # the following conditions:
20 #
21 # The above copyright notice and this permission notice shall be included
22 # in all copies or substantial portions of the Software.
23 #
24 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
25 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
26 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #
32
33 __revision__ = "src/engine/SCons/Tool/mwcc.py 3842 2008/12/20 22:59:52 scons"
34
35 import os
36 import os.path
37 import string
38
39 import SCons.Util
40
41 def set_vars(env):
42     """Set MWCW_VERSION, MWCW_VERSIONS, and some codewarrior environment vars
43
44     MWCW_VERSIONS is set to a list of objects representing installed versions
45
46     MWCW_VERSION  is set to the version object that will be used for building.
47                   MWCW_VERSION can be set to a string during Environment
48                   construction to influence which version is chosen, otherwise
49                   the latest one from MWCW_VERSIONS is used.
50
51     Returns true if at least one version is found, false otherwise
52     """
53     desired = env.get('MWCW_VERSION', '')
54
55     # return right away if the variables are already set
56     if isinstance(desired, MWVersion):
57         return 1
58     elif desired is None:
59         return 0
60
61     versions = find_versions()
62     version = None
63
64     if desired:
65         for v in versions:
66             if str(v) == desired:
67                 version = v
68     elif versions:
69         version = versions[-1]
70
71     env['MWCW_VERSIONS'] = versions
72     env['MWCW_VERSION'] = version
73
74     if version is None:
75       return 0
76
77     env.PrependENVPath('PATH', version.clpath)
78     env.PrependENVPath('PATH', version.dllpath)
79     ENV = env['ENV']
80     ENV['CWFolder'] = version.path
81     ENV['LM_LICENSE_FILE'] = version.license
82     plus = lambda x: '+%s' % x
83     ENV['MWCIncludes'] = string.join(map(plus, version.includes), os.pathsep)
84     ENV['MWLibraries'] = string.join(map(plus, version.libs), os.pathsep)
85     return 1
86
87
88 def find_versions():
89     """Return a list of MWVersion objects representing installed versions"""
90     versions = []
91
92     ### This function finds CodeWarrior by reading from the registry on
93     ### Windows. Some other method needs to be implemented for other
94     ### platforms, maybe something that calls env.WhereIs('mwcc')
95
96     if SCons.Util.can_read_reg:
97         try:
98             HLM = SCons.Util.HKEY_LOCAL_MACHINE
99             product = 'SOFTWARE\\Metrowerks\\CodeWarrior\\Product Versions'
100             product_key = SCons.Util.RegOpenKeyEx(HLM, product)
101
102             i = 0
103             while 1:
104                 name = product + '\\' + SCons.Util.RegEnumKey(product_key, i)
105                 name_key = SCons.Util.RegOpenKeyEx(HLM, name)
106
107                 try:
108                     version = SCons.Util.RegQueryValueEx(name_key, 'VERSION')
109                     path = SCons.Util.RegQueryValueEx(name_key, 'PATH')
110                     mwv = MWVersion(version[0], path[0], 'Win32-X86')
111                     versions.append(mwv)
112                 except SCons.Util.RegError:
113                     pass
114
115                 i = i + 1
116
117         except SCons.Util.RegError:
118             pass
119
120     return versions
121
122
123 class MWVersion:
124     def __init__(self, version, path, platform):
125         self.version = version
126         self.path = path
127         self.platform = platform
128         self.clpath = os.path.join(path, 'Other Metrowerks Tools',
129                                    'Command Line Tools')
130         self.dllpath = os.path.join(path, 'Bin')
131
132         # The Metrowerks tools don't store any configuration data so they
133         # are totally dumb when it comes to locating standard headers,
134         # libraries, and other files, expecting all the information
135         # to be handed to them in environment variables. The members set
136         # below control what information scons injects into the environment
137
138         ### The paths below give a normal build environment in CodeWarrior for
139         ### Windows, other versions of CodeWarrior might need different paths.
140
141         msl = os.path.join(path, 'MSL')
142         support = os.path.join(path, '%s Support' % platform)
143
144         self.license = os.path.join(path, 'license.dat')
145         self.includes = [msl, support]
146         self.libs = [msl, support]
147
148     def __str__(self):
149         return self.version
150
151
152 CSuffixes = ['.c', '.C']
153 CXXSuffixes = ['.cc', '.cpp', '.cxx', '.c++', '.C++']
154
155
156 def generate(env):
157     """Add Builders and construction variables for the mwcc to an Environment."""
158     import SCons.Defaults
159     import SCons.Tool
160
161     set_vars(env)
162
163     static_obj, shared_obj = SCons.Tool.createObjBuilders(env)
164
165     for suffix in CSuffixes:
166         static_obj.add_action(suffix, SCons.Defaults.CAction)
167         shared_obj.add_action(suffix, SCons.Defaults.ShCAction)
168
169     for suffix in CXXSuffixes:
170         static_obj.add_action(suffix, SCons.Defaults.CXXAction)
171         shared_obj.add_action(suffix, SCons.Defaults.ShCXXAction)
172
173     env['CCCOMFLAGS'] = '$CPPFLAGS $_CPPDEFFLAGS $_CPPINCFLAGS -nolink -o $TARGET $SOURCES'
174
175     env['CC']         = 'mwcc'
176     env['CCCOM']      = '$CC $CFLAGS $CCFLAGS $CCCOMFLAGS'
177
178     env['CXX']        = 'mwcc'
179     env['CXXCOM']     = '$CXX $CXXFLAGS $CCCOMFLAGS'
180
181     env['SHCC']       = '$CC'
182     env['SHCCFLAGS']  = '$CCFLAGS'
183     env['SHCFLAGS']   = '$CFLAGS'
184     env['SHCCCOM']    = '$SHCC $SHCFLAGS $SHCCFLAGS $CCCOMFLAGS'
185
186     env['SHCXX']       = '$CXX'
187     env['SHCXXFLAGS']  = '$CXXFLAGS'
188     env['SHCXXCOM']    = '$SHCXX $SHCXXFLAGS $CCCOMFLAGS'
189
190     env['CFILESUFFIX'] = '.c'
191     env['CXXFILESUFFIX'] = '.cpp'
192     env['CPPDEFPREFIX']  = '-D'
193     env['CPPDEFSUFFIX']  = ''
194     env['INCPREFIX']  = '-I'
195     env['INCSUFFIX']  = ''
196
197     #env['PCH'] = ?
198     #env['PCHSTOP'] = ?
199
200
201 def exists(env):
202     return set_vars(env)