Fix get_host_target()'s use of platform.machine() on Python versions
[scons.git] / src / engine / SCons / Tool / MSCommon / vc.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 # TODO:
25 #   * supported arch for versions: for old versions of batch file without
26 #     argument, giving bogus argument cannot be detected, so we have to hardcode
27 #     this here
28 #   * print warning when msvc version specified but not found
29 #   * find out why warning do not print
30 #   * test on 64 bits XP +  VS 2005 (and VS 6 if possible)
31 #   * SDK
32 #   * Assembly
33 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
34
35 __doc__ = """Module for Visual C/C++ detection and configuration.
36 """
37 import SCons.compat
38
39 import os
40 import platform
41
42 import SCons.Warnings
43
44 import common
45
46 debug = common.debug
47
48 class VisualCException(Exception):
49     pass
50
51 class UnsupportedVersion(VisualCException):
52     pass
53
54 class UnsupportedArch(VisualCException):
55     pass
56
57 class MissingConfiguration(VisualCException):
58     pass
59
60 class NoVersionFound(VisualCException):
61     pass
62
63 class BatchFileExecutionError(VisualCException):
64     pass
65
66 # Dict to 'canonalize' the arch
67 _ARCH_TO_CANONICAL = {
68     "x86": "x86",
69     "amd64": "amd64",
70     "i386": "x86",
71     "emt64": "amd64",
72     "x86_64": "amd64",
73     "itanium": "ia64",
74     "ia64": "ia64",
75 }
76
77 # Given a (host, target) tuple, return the argument for the bat file. Both host
78 # and targets should be canonalized.
79 _HOST_TARGET_ARCH_TO_BAT_ARCH = {
80     ("x86", "x86"): "x86",
81     ("x86", "amd64"): "x86_amd64",
82     ("amd64", "amd64"): "amd64",
83     ("amd64", "x86"): "x86",
84     ("x86", "ia64"): "x86_ia64"
85 }
86
87 def get_host_target(env):
88     host_platform = env.get('HOST_ARCH')
89     if not host_platform:
90         host_platform = platform.machine()
91         # TODO(2.5):  the native Python platform.machine() function returns
92         # '' on all Python versions before 2.6, after which it also uses
93         # PROCESSOR_ARCHITECTURE.
94         if not host_platform:
95             host_platform = os.environ.get('PROCESSOR_ARCHITECTURE', '')
96     target_platform = env.get('TARGET_ARCH')
97     if not target_platform:
98         target_platform = host_platform
99
100     try:
101         host = _ARCH_TO_CANONICAL[host_platform]
102     except KeyError, e:
103         msg = "Unrecognized host architecture %s"
104         raise ValueError(msg % repr(host_platform))
105
106     try:
107         target = _ARCH_TO_CANONICAL[target_platform]
108     except KeyError, e:
109         raise ValueError("Unrecognized target architecture %s" % target_platform)
110
111     return (host, target)
112
113 _VCVER = ["10.0", "9.0", "8.0", "7.1", "7.0", "6.0"]
114
115 _VCVER_TO_PRODUCT_DIR = {
116         '10.0': [
117             r'Microsoft\VisualStudio\10.0\Setup\VC\ProductDir'],
118         '9.0': [
119             r'Microsoft\VisualStudio\9.0\Setup\VC\ProductDir',
120             r'Microsoft\VCExpress\9.0\Setup\VC\ProductDir'],
121         '8.0': [
122             r'Microsoft\VisualStudio\8.0\Setup\VC\ProductDir',
123             r'Microsoft\VCExpress\8.0\Setup\VC\ProductDir'],
124         '7.1': [
125             r'Microsoft\VisualStudio\7.1\Setup\VC\ProductDir'],
126         '7.0': [
127             r'Microsoft\VisualStudio\7.0\Setup\VC\ProductDir'],
128         '6.0': [
129             r'Microsoft\VisualStudio\6.0\Setup\Microsoft Visual C++\ProductDir']
130 }
131
132 def msvc_version_to_maj_min(msvc_version):
133     t = msvc_version.split(".")
134     if not len(t) == 2:
135         raise ValueError("Unrecognized version %s" % msvc_version)
136     try:
137         maj = int(t[0])
138         min = int(t[1])
139         return maj, min
140     except ValueError, e:
141         raise ValueError("Unrecognized version %s" % msvc_version)
142
143 def is_host_target_supported(host_target, msvc_version):
144     """Return True if the given (host, target) tuple is supported given the
145     msvc version.
146
147     Parameters
148     ----------
149     host_target: tuple
150         tuple of (canonalized) host-target, e.g. ("x86", "amd64") for cross
151         compilation from 32 bits windows to 64 bits.
152     msvc_version: str
153         msvc version (major.minor, e.g. 10.0)
154
155     Note
156     ----
157     This only check whether a given version *may* support the given (host,
158     target), not that the toolchain is actually present on the machine.
159     """
160     # We assume that any Visual Studio version supports x86 as a target
161     if host_target[1] != "x86":
162         maj, min = msvc_version_to_maj_min(msvc_version)
163         if maj < 8:
164             return False
165
166     return True
167
168 def find_vc_pdir(msvc_version):
169     """Try to find the product directory for the given
170     version.
171
172     Note
173     ----
174     If for some reason the requested version could not be found, an
175     exception which inherits from VisualCException will be raised."""
176     root = 'Software\\'
177     if common.is_win64():
178         root = root + 'Wow6432Node\\'
179     try:
180         hkeys = _VCVER_TO_PRODUCT_DIR[msvc_version]
181     except KeyError:
182         debug("Unknown version of MSVC: %s" % msvc_version)
183         raise UnsupportedVersion("Unknown version %s" % msvc_version)
184
185     for key in hkeys:
186         key = root + key
187         try:
188             comps = common.read_reg(key)
189         except WindowsError, e:
190             debug('find_vc_dir(): no VC registry key %s' % repr(key))
191         else:
192             debug('find_vc_dir(): found VC in registry: %s' % comps)
193             if os.path.exists(comps):
194                 return comps
195             else:
196                 debug('find_vc_dir(): reg says dir is %s, but it does not exist. (ignoring)'\
197                           % comps)
198                 raise MissingConfiguration("registry dir %s not found on the filesystem" % comps)
199     return None
200
201 def find_batch_file(msvc_version):
202     pdir = find_vc_pdir(msvc_version)
203     if pdir is None:
204         raise NoVersionFound("No version of Visual Studio found")
205
206     vernum = float(msvc_version)
207     if 7 <= vernum < 8:
208         pdir = os.path.join(pdir, os.pardir, "Common7", "Tools")
209         batfilename = os.path.join(pdir, "vsvars32.bat")
210     elif vernum < 7:
211         pdir = os.path.join(pdir, "Bin")
212         batfilename = os.path.join(pdir, "vcvars32.bat")
213     else: # >= 8
214         batfilename = os.path.join(pdir, "vcvarsall.bat")
215
216     if os.path.exists(batfilename):
217         return batfilename
218     else:
219         debug("Not found: %s" % batfilename)
220         return None
221
222 __INSTALLED_VCS_RUN = None
223
224 def cached_get_installed_vcs():
225     global __INSTALLED_VCS_RUN
226
227     if __INSTALLED_VCS_RUN is None:
228         ret = get_installed_vcs()
229         __INSTALLED_VCS_RUN = ret
230
231     return __INSTALLED_VCS_RUN
232
233 def get_installed_vcs():
234     installed_versions = []
235     for ver in _VCVER:
236         debug('trying to find VC %s' % ver)
237         try:
238             if find_vc_pdir(ver):
239                 debug('found VC %s' % ver)
240                 installed_versions.append(ver)
241             else:
242                 debug('find_vc_pdir return None for ver %s' % ver)
243         except VisualCException, e:
244             debug('did not find VC %s: caught exception %s' % (ver, str(e)))
245     return installed_versions
246
247 def reset_installed_vcs():
248     """Make it try again to find VC.  This is just for the tests."""
249     __INSTALLED_VCS_RUN = None
250
251 def script_env(script, args=None):
252     stdout = common.get_output(script, args)
253     # Stupid batch files do not set return code: we take a look at the
254     # beginning of the output for an error message instead
255     olines = stdout.splitlines()
256     if olines[0].startswith("The specified configuration type is missing"):
257         raise BatchFileExecutionError("\n".join(olines[:2]))
258
259     return common.parse_output(stdout)
260
261 def get_default_version(env):
262     debug('get_default_version()')
263
264     msvc_version = env.get('MSVC_VERSION')
265     msvs_version = env.get('MSVS_VERSION')
266
267     if msvs_version and not msvc_version:
268         SCons.Warnings.warn(
269                 SCons.Warnings.DeprecatedWarning,
270                 "MSVS_VERSION is deprecated: please use MSVC_VERSION instead ")
271         return msvs_version
272     elif msvc_version and msvs_version:
273         if not msvc_version == msvs_version:
274             SCons.Warnings.warn(
275                     SCons.Warnings.VisualVersionMismatch,
276                     "Requested msvc version (%s) and msvs version (%s) do " \
277                     "not match: please use MSVC_VERSION only to request a " \
278                     "visual studio version, MSVS_VERSION is deprecated" \
279                     % (msvc_version, msvs_version))
280         return msvs_version
281     if not msvc_version:
282         installed_vcs = cached_get_installed_vcs()
283         debug('installed_vcs:%s' % installed_vcs)
284         if not installed_vcs:
285             msg = 'No installed VCs'
286             debug('msv %s\n' % repr(msg))
287             SCons.Warnings.warn(SCons.Warnings.VisualCMissingWarning, msg)
288             return None
289         msvc_version = installed_vcs[0]
290         debug('msvc_setup_env: using default installed MSVC version %s\n' % repr(msvc_version))
291
292     return msvc_version
293
294 def msvc_setup_env_once(env):
295     try:
296         has_run  = env["MSVC_SETUP_RUN"]
297     except KeyError:
298         has_run = False
299
300     if not has_run:
301         msvc_setup_env(env)
302         env["MSVC_SETUP_RUN"] = True
303
304 def msvc_setup_env(env):
305     debug('msvc_setup_env()')
306
307     version = get_default_version(env)
308     if version is None:
309         warn_msg = "No version of Visual Studio compiler found - C/C++ " \
310                    "compilers most likely not set correctly"
311         SCons.Warnings.warn(SCons.Warnings.VisualCMissingWarning, warn_msg)
312         return None
313     debug('msvc_setup_env: using specified MSVC version %s\n' % repr(version))
314
315     # XXX: we set-up both MSVS version for backward
316     # compatibility with the msvs tool
317     env['MSVC_VERSION'] = version
318     env['MSVS_VERSION'] = version
319     env['MSVS'] = {}
320
321     try:
322         script = find_batch_file(version)
323     except VisualCException, e:
324         msg = str(e)
325         debug('Caught exception while looking for batch file (%s)' % msg)
326         warn_msg = "VC version %s not installed.  " + \
327                    "C/C++ compilers are most likely not set correctly.\n" + \
328                    " Installed versions are: %s"
329         warn_msg = warn_msg % (version, cached_get_installed_vcs())
330         SCons.Warnings.warn(SCons.Warnings.VisualCMissingWarning, warn_msg)
331         return None
332
333     use_script = env.get('MSVC_USE_SCRIPT', True)
334     if SCons.Util.is_String(use_script):
335         debug('use_script 1 %s\n' % repr(use_script))
336         d = script_env(use_script)
337     elif use_script:
338         host_platform, target_platform = get_host_target(env)
339         host_target = (host_platform, target_platform)
340         if not is_host_target_supported(host_target, version):
341             warn_msg = "host, target = %s not supported for MSVC version %s" % \
342                 (host_target, version)
343             SCons.Warnings.warn(SCons.Warnings.VisualCMissingWarning, warn_msg)
344         arg = _HOST_TARGET_ARCH_TO_BAT_ARCH[host_target]
345         debug('use_script 2 %s, args:%s\n' % (repr(script), arg))
346         try:
347             d = script_env(script, args=arg)
348         except BatchFileExecutionError, e:
349             msg = "MSVC error while executing %s with args %s (error was %s)" % \
350                   (script, arg, str(e))
351             raise SCons.Errors.UserError(msg)
352     else:
353         debug('MSVC_USE_SCRIPT set to False')
354         warn_msg = "MSVC_USE_SCRIPT set to False, assuming environment " \
355                    "set correctly."
356         SCons.Warnings.warn(SCons.Warnings.VisualCMissingWarning, warn_msg)
357         return None
358
359     for k, v in d.items():
360         env.PrependENVPath(k, v, delete_existing=True)
361
362 def msvc_exists(version=None):
363     vcs = cached_get_installed_vcs()
364     if version is None:
365         return len(vcs) > 0
366     return version in vcs
367