Merged revisions 4025-4029 via svnmerge from
[scons.git] / src / engine / SCons / Tool / MSCommon / sdk.py
1 #
2 # __COPYRIGHT__
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish,
8 # distribute, sublicense, and/or sell copies of the Software, and to
9 # permit persons to whom the Software is furnished to do so, subject to
10 # the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
16 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #
23
24 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
25
26 __doc__ = """Module to detect the Platform/Windows SDK
27
28 PSDK 2003 R1 is the earliest version detected.
29 """
30
31 import os
32
33 import SCons.Errors
34 from SCons.Tool.MSCommon.common import debug, read_reg
35 import SCons.Util
36
37 # SDK Checks. This is of course a mess as everything else on MS platforms. Here
38 # is what we do to detect the SDK:
39 #
40 # For Windows SDK >= 6.0: just look into the registry entries:
41 #   HKLM\Software\Microsoft\Microsoft SDKs\Windows
42 # All the keys in there are the available versions.
43 #
44 # For Platform SDK before 6.0 (2003 server R1 and R2, etc...), there does not
45 # seem to be any sane registry key, so the precise location is hardcoded.
46 #
47 # For versions below 2003R1, it seems the PSDK is included with Visual Studio?
48 #
49 # Also, per the following:
50 #     http://benjamin.smedbergs.us/blog/tag/atl/
51 # VC++ Professional comes with the SDK, VC++ Express does not.
52
53 # Location of the SDK (checked for 6.1 only)
54 _CURINSTALLED_SDK_HKEY_ROOT = \
55         r"Software\Microsoft\Microsoft SDKs\Windows\CurrentInstallFolder"
56
57
58 class SDKDefinition:
59     """
60     An abstract base class for trying to find installed SDK directories.
61     """
62     def __init__(self, version, **kw):
63         self.version = version
64         self.__dict__.update(kw)
65
66     def find_sdk_dir(self):
67         """Try to find the MS SDK from the registry.
68
69         Return None if failed or the directory does not exist.
70         """
71         if not SCons.Util.can_read_reg:
72             debug('find_sdk_dir():  can not read registry')
73             return None
74
75         hkey = self.HKEY_FMT % self.hkey_data
76
77         try:
78             sdk_dir = read_reg(hkey)
79         except WindowsError, e:
80             debug('find_sdk_dir(): no registry key %s' % hkey)
81             return None
82
83         if not os.path.exists(sdk_dir):
84             debug('find_sdk_dir():  %s not on file system' % sdk_dir)
85             return None
86
87         ftc = os.path.join(sdk_dir, self.sanity_check_file)
88         if not os.path.exists(ftc):
89             debug("find_sdk_dir():  sanity check %s not found" % ftc)
90             return None
91
92         return sdk_dir
93
94     def get_sdk_dir(self):
95         """Return the MSSSDK given the version string."""
96         try:
97             return self._sdk_dir
98         except AttributeError:
99             sdk_dir = self.find_sdk_dir()
100             self._sdk_dir = sdk_dir
101             return sdk_dir
102
103 class WindowsSDK(SDKDefinition):
104     """
105     A subclass for trying to find installed Windows SDK directories.
106     """
107     HKEY_FMT = r'Software\Microsoft\Microsoft SDKs\Windows\v%s\InstallationFolder'
108     def __init__(self, *args, **kw):
109         apply(SDKDefinition.__init__, (self,)+args, kw)
110         self.hkey_data = self.version
111
112 class PlatformSDK(SDKDefinition):
113     """
114     A subclass for trying to find installed Platform SDK directories.
115     """
116     HKEY_FMT = r'Software\Microsoft\MicrosoftSDK\InstalledSDKS\%s\Install Dir'
117     def __init__(self, *args, **kw):
118         apply(SDKDefinition.__init__, (self,)+args, kw)
119         self.hkey_data = self.uuid
120
121 # The list of support SDKs which we know how to detect.
122 #
123 # The first SDK found in the list is the one used by default if there
124 # are multiple SDKs installed.  Barring good reasons to the contrary,
125 # this means we should list SDKs with from most recent to oldest.
126 #
127 # If you update this list, update the documentation in Tool/mssdk.xml.
128 SupportedSDKList = [
129     WindowsSDK('6.1',
130                 sanity_check_file=r'include\windows.h'),
131
132     WindowsSDK('6.0A',
133                sanity_check_file=r'include\windows.h'),
134
135     WindowsSDK('6.0',
136                sanity_check_file=r'bin\gacutil.exe'),
137
138     PlatformSDK('2003R2',
139                 sanity_check_file=r'SetEnv.Cmd',
140                 uuid="D2FF9F89-8AA2-4373-8A31-C838BF4DBBE1"),
141
142     PlatformSDK('2003R1',
143                 sanity_check_file=r'SetEnv.Cmd',
144                 uuid="8F9E5EF3-A9A5-491B-A889-C58EFFECE8B3"),
145 ]
146
147 SupportedSDKMap = {}
148 for sdk in SupportedSDKList:
149     SupportedSDKMap[sdk.version] = sdk
150
151
152 # Finding installed SDKs isn't cheap, because it goes not only to the
153 # registry but also to the disk to sanity-check that there is, in fact,
154 # an SDK installed there and that the registry entry isn't just stale.
155 # Find this information once, when requested, and cache it.
156
157 InstalledSDKList = None
158 InstalledSDKMap = None
159
160 def get_installed_sdks():
161     global InstalledSDKList
162     global InstalledSDKMap
163     if InstalledSDKList is None:
164         InstalledSDKList = []
165         InstalledSDKMap = {}
166         for sdk in SupportedSDKList:
167             debug('trying to find SDK %s' % sdk.version)
168             if sdk.get_sdk_dir():
169                 debug('found SDK %s' % sdk.version)
170                 InstalledSDKList.append(sdk)
171                 InstalledSDKMap[sdk.version] = sdk
172     return InstalledSDKList
173
174
175 # We may be asked to update multiple construction environments with
176 # SDK information.  When doing this, we check on-disk for whether
177 # the SDK has 'mfc' and 'atl' subdirectories.  Since going to disk
178 # is expensive, cache results by directory.
179
180 SDKEnvironmentUpdates = {}
181
182 def set_sdk_by_directory(env, sdk_dir):
183     global SDKEnvironmentUpdates
184     try:
185         env_tuple_list = SDKEnvironmentUpdates[sdk_dir]
186     except KeyError:
187         env_tuple_list = []
188         SDKEnvironmentUpdates[sdk_dir] = env_tuple_list
189
190         include_path = os.path.join(sdk_dir, 'include')
191         mfc_path = os.path.join(include_path, 'mfc')
192         atl_path = os.path.join(include_path, 'atl')
193
194         if os.path.exists(mfc_path):
195             env_tuple_list.append(('INCLUDE', mfc_path))
196         if os.path.exists(atl_path):
197             env_tuple_list.append(('INCLUDE', atl_path))
198         env_tuple_list.append(('INCLUDE', include_path))
199
200         env_tuple_list.append(('LIB', os.path.join(sdk_dir, 'lib')))
201         env_tuple_list.append(('LIBPATH', os.path.join(sdk_dir, 'lib')))
202         env_tuple_list.append(('PATH', os.path.join(sdk_dir, 'bin')))
203
204     for variable, directory in env_tuple_list:
205         env.PrependENVPath(variable, directory)
206
207
208 # TODO(sgk):  currently unused; remove?
209 def get_cur_sdk_dir_from_reg():
210     """Try to find the platform sdk directory from the registry.
211
212     Return None if failed or the directory does not exist"""
213     if not SCons.Util.can_read_reg:
214         debug('SCons cannot read registry')
215         return None
216
217     try:
218         val = read_reg(_CURINSTALLED_SDK_HKEY_ROOT)
219         debug("Found current sdk dir in registry: %s" % val)
220     except WindowsError, e:
221         debug("Did not find current sdk in registry")
222         return None
223
224     if not os.path.exists(val):
225         debug("Current sdk dir %s not on fs" % val)
226         return None
227
228     return val
229
230
231 def detect_sdk():
232     return (len(get_installed_sdks()) > 0)
233
234 def set_sdk_by_version(env, mssdk):
235     if not SupportedSDKMap.has_key(mssdk):
236         msg = "SDK version %s is not supported" % repr(mssdk)
237         raise SCons.Errors.UserError, msg
238     get_installed_sdks()
239     sdk = InstalledSDKMap.get(mssdk)
240     if not sdk:
241         msg = "SDK version %s is not installed" % repr(mssdk)
242         raise SCons.Errors.UserError, msg
243     set_sdk_by_directory(env, sdk.get_sdk_dir())
244
245 def set_default_sdk(env, msver):
246     """Set up the default Platform/Windows SDK."""
247     # For MSVS < 8, use integrated windows sdk by default
248     if msver >= 8:
249         sdks = get_installed_sdks()
250         if len(sdks) > 0:
251             set_sdk_by_directory(env, sdks[0].get_sdk_dir())
252
253 # Local Variables:
254 # tab-width:4
255 # indent-tabs-mode:nil
256 # End:
257 # vim: set expandtab tabstop=4 shiftwidth=4: