c720956e73f6e34e84b1978dcd554cfc5f8ce5e2
[scons.git] / src / 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__
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__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
34
35 import os
36 import os.path
37
38 import SCons.Util
39
40 def set_vars(env):
41     """Set MWCW_VERSION, MWCW_VERSIONS, and some codewarrior environment vars
42
43     MWCW_VERSIONS is set to a list of objects representing installed versions
44
45     MWCW_VERSION  is set to the version object that will be used for building.
46                   MWCW_VERSION can be set to a string during Environment
47                   construction to influence which version is chosen, otherwise
48                   the latest one from MWCW_VERSIONS is used.
49
50     Returns true if at least one version is found, false otherwise
51     """
52     desired = env.get('MWCW_VERSION', '')
53
54     # return right away if the variables are already set
55     if isinstance(desired, MWVersion):
56         return 1
57     elif desired is None:
58         return 0
59
60     versions = find_versions()
61     version = None
62
63     if desired:
64         for v in versions:
65             if str(v) == desired:
66                 version = v
67     elif versions:
68         version = versions[-1]
69
70     env['MWCW_VERSIONS'] = versions
71     env['MWCW_VERSION'] = version
72
73     if version is None:
74       return 0
75
76     env.PrependENVPath('PATH', version.clpath)
77     env.PrependENVPath('PATH', version.dllpath)
78     ENV = env['ENV']
79     ENV['CWFolder'] = version.path
80     ENV['LM_LICENSE_FILE'] = version.license
81     plus = lambda x: '+%s' % x
82     ENV['MWCIncludes'] = os.pathsep.join(map(plus, version.includes))
83     ENV['MWLibraries'] = os.pathsep.join(map(plus, version.libs))
84     return 1
85
86
87 def find_versions():
88     """Return a list of MWVersion objects representing installed versions"""
89     versions = []
90
91     ### This function finds CodeWarrior by reading from the registry on
92     ### Windows. Some other method needs to be implemented for other
93     ### platforms, maybe something that calls env.WhereIs('mwcc')
94
95     if SCons.Util.can_read_reg:
96         try:
97             HLM = SCons.Util.HKEY_LOCAL_MACHINE
98             product = 'SOFTWARE\\Metrowerks\\CodeWarrior\\Product Versions'
99             product_key = SCons.Util.RegOpenKeyEx(HLM, product)
100
101             i = 0
102             while 1:
103                 name = product + '\\' + SCons.Util.RegEnumKey(product_key, i)
104                 name_key = SCons.Util.RegOpenKeyEx(HLM, name)
105
106                 try:
107                     version = SCons.Util.RegQueryValueEx(name_key, 'VERSION')
108                     path = SCons.Util.RegQueryValueEx(name_key, 'PATH')
109                     mwv = MWVersion(version[0], path[0], 'Win32-X86')
110                     versions.append(mwv)
111                 except SCons.Util.RegError:
112                     pass
113
114                 i = i + 1
115
116         except SCons.Util.RegError:
117             pass
118
119     return versions
120
121
122 class MWVersion:
123     def __init__(self, version, path, platform):
124         self.version = version
125         self.path = path
126         self.platform = platform
127         self.clpath = os.path.join(path, 'Other Metrowerks Tools',
128                                    'Command Line Tools')
129         self.dllpath = os.path.join(path, 'Bin')
130
131         # The Metrowerks tools don't store any configuration data so they
132         # are totally dumb when it comes to locating standard headers,
133         # libraries, and other files, expecting all the information
134         # to be handed to them in environment variables. The members set
135         # below control what information scons injects into the environment
136
137         ### The paths below give a normal build environment in CodeWarrior for
138         ### Windows, other versions of CodeWarrior might need different paths.
139
140         msl = os.path.join(path, 'MSL')
141         support = os.path.join(path, '%s Support' % platform)
142
143         self.license = os.path.join(path, 'license.dat')
144         self.includes = [msl, support]
145         self.libs = [msl, support]
146
147     def __str__(self):
148         return self.version
149
150
151 CSuffixes = ['.c', '.C']
152 CXXSuffixes = ['.cc', '.cpp', '.cxx', '.c++', '.C++']
153
154
155 def generate(env):
156     """Add Builders and construction variables for the mwcc to an Environment."""
157     import SCons.Defaults
158     import SCons.Tool
159
160     set_vars(env)
161
162     static_obj, shared_obj = SCons.Tool.createObjBuilders(env)
163
164     for suffix in CSuffixes:
165         static_obj.add_action(suffix, SCons.Defaults.CAction)
166         shared_obj.add_action(suffix, SCons.Defaults.ShCAction)
167
168     for suffix in CXXSuffixes:
169         static_obj.add_action(suffix, SCons.Defaults.CXXAction)
170         shared_obj.add_action(suffix, SCons.Defaults.ShCXXAction)
171
172     env['CCCOMFLAGS'] = '$CPPFLAGS $_CPPDEFFLAGS $_CPPINCFLAGS -nolink -o $TARGET $SOURCES'
173
174     env['CC']         = 'mwcc'
175     env['CCCOM']      = '$CC $CFLAGS $CCFLAGS $CCCOMFLAGS'
176
177     env['CXX']        = 'mwcc'
178     env['CXXCOM']     = '$CXX $CXXFLAGS $CCCOMFLAGS'
179
180     env['SHCC']       = '$CC'
181     env['SHCCFLAGS']  = '$CCFLAGS'
182     env['SHCFLAGS']   = '$CFLAGS'
183     env['SHCCCOM']    = '$SHCC $SHCFLAGS $SHCCFLAGS $CCCOMFLAGS'
184
185     env['SHCXX']       = '$CXX'
186     env['SHCXXFLAGS']  = '$CXXFLAGS'
187     env['SHCXXCOM']    = '$SHCXX $SHCXXFLAGS $CCCOMFLAGS'
188
189     env['CFILESUFFIX'] = '.c'
190     env['CXXFILESUFFIX'] = '.cpp'
191     env['CPPDEFPREFIX']  = '-D'
192     env['CPPDEFSUFFIX']  = ''
193     env['INCPREFIX']  = '-I'
194     env['INCSUFFIX']  = ''
195
196     #env['PCH'] = ?
197     #env['PCHSTOP'] = ?
198
199
200 def exists(env):
201     return set_vars(env)
202
203 # Local Variables:
204 # tab-width:4
205 # indent-tabs-mode:nil
206 # End:
207 # vim: set expandtab tabstop=4 shiftwidth=4: