BUG: fix caching bug for msvc_setup_once.
[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
38 import os
39 import platform
40
41 import SCons.Warnings
42
43 import common
44
45 debug = common.debug
46
47 class VisualCException(Exception):
48     pass
49
50 class UnsupportedVersion(VisualCException):
51     pass
52
53 class MissingConfiguration(VisualCException):
54     pass
55
56 class NoVersionFound(VisualCException):
57     pass
58
59 class BatchFileExecutionError(VisualCException):
60     pass
61
62 # Dict to 'canonalize' the arch
63 _ARCH_TO_CANONICAL = {
64     "x86": "x86",
65     "amd64": "amd64",
66     "i386": "x86",
67     "emt64": "amd64",
68     "x86_64": "amd64",
69     "itanium": "ia64",
70     "ia64": "ia64",
71 }
72
73 # Given a (host, target) tuple, return the argument for the bat file. Both host
74 # and targets should be canonalized.
75 _HOST_TARGET_ARCH_TO_BAT_ARCH = {
76     ("x86", "x86"): "x86",
77     ("x86", "amd64"): "x86_amd64",
78     ("amd64", "amd64"): "amd64",
79     ("amd64", "x86"): "x86",
80     ("x86", "ia64"): "x86_ia64"
81 }
82
83 def get_host_target(env):
84     host_platform = env.get('HOST_ARCH')
85     if not host_platform:
86         host_platform = platform.machine()
87     target_platform = env.get('TARGET_ARCH')
88     if not target_platform:
89         target_platform = host_platform
90
91     try:
92         host = _ARCH_TO_CANONICAL[host_platform]
93     except KeyError, e:
94         raise ValueError("Unrecognized host architecture %s" % host_platform)
95
96     try:
97         target = _ARCH_TO_CANONICAL[target_platform]
98     except KeyError, e:
99         raise ValueError("Unrecognized target architecture %s" % target_platform)
100
101     return (host, target)
102
103 _VCVER = ["10.0", "9.0", "8.0", "7.1", "7.0", "6.0"]
104
105 _VCVER_TO_PRODUCT_DIR = {
106         '10.0': [
107             r'Microsoft\VisualStudio\10.0\Setup\VC\ProductDir'],
108         '9.0': [
109             r'Microsoft\VisualStudio\9.0\Setup\VC\ProductDir',
110             r'Microsoft\VCExpress\9.0\Setup\VC\ProductDir'],
111         '8.0': [
112             r'Microsoft\VisualStudio\8.0\Setup\VC\ProductDir',
113             r'Microsoft\VCExpress\8.0\Setup\VC\ProductDir'],
114         '7.1': [
115             r'Microsoft\VisualStudio\7.1\Setup\VC\ProductDir'],
116         '7.0': [
117             r'Microsoft\VisualStudio\7.0\Setup\VC\ProductDir'],
118         '6.0': [
119             r'Microsoft\VisualStudio\6.0\Setup\Microsoft Visual C++\ProductDir']
120 }
121
122 def find_vc_pdir(msvc_version):
123     """Try to find the product directory for the given
124     version.
125
126     Note
127     ----
128     If for some reason the requested version could not be found, an
129     exception which inherits from VisualCException will be raised."""
130     root = 'Software\\'
131     if common.is_win64():
132         root = root + 'Wow6432Node\\'
133     try:
134         hkeys = _VCVER_TO_PRODUCT_DIR[msvc_version]
135     except KeyError:
136         debug("Unknown version of MSVC: %s" % msvc_version)
137         raise UnsupportedVersion("Unknown version %s" % msvc_version)
138
139     for key in hkeys:
140         key = root + key
141         try:
142             comps = common.read_reg(key)
143         except WindowsError, e:
144             debug('find_vc_dir(): no VC registry key %s' % repr(key))
145         else:
146             debug('find_vc_dir(): found VC in registry: %s' % comps)
147             if os.path.exists(comps):
148                 return comps
149             else:
150                 debug('find_vc_dir(): reg says dir is %s, but it does not exist. (ignoring)'\
151                           % comps)
152                 raise MissingConfiguration("registry dir %s not found on the filesystem" % comps)
153     return None
154
155 def find_batch_file(msvc_version):
156     pdir = find_vc_pdir(msvc_version)
157     if pdir is None:
158         raise NoVersionFound("No version of Visual Studio found")
159
160     vernum = float(msvc_version)
161     if 7 <= vernum < 8:
162         pdir = os.path.join(pdir, os.pardir, "Common7", "Tools")
163         batfilename = os.path.join(pdir, "vsvars32.bat")
164     elif vernum < 7:
165         pdir = os.path.join(pdir, "Bin")
166         batfilename = os.path.join(pdir, "vcvars32.bat")
167     else: # >= 8
168         batfilename = os.path.join(pdir, "vcvarsall.bat")
169
170     if os.path.exists(batfilename):
171         return batfilename
172     else:
173         debug("Not found: %s" % batfilename)
174         return None
175
176
177 def get_installed_vcs():
178     installed_versions = []
179     for ver in _VCVER:
180         debug('trying to find VC %s' % ver)
181         try:
182             if find_vc_pdir(ver):
183                 debug('found VC %s' % ver)
184                 installed_versions.append(ver)
185             else:
186                 debug('find_vc_pdir return None for ver %s' % ver)
187         except VisualCException, e:
188             debug('did not find VC %s: caught exception %s' % (ver, str(e)))
189     return installed_versions
190
191 def script_env(script, args=None):
192     stdout = common.get_output(script, args)
193     # Stupid batch files do not set return code: we take a look at the
194     # beginning of the output for an error message instead
195     olines = stdout.splitlines()
196     if olines[0].startswith("The specified configuration type is missing"):
197         raise BatchFileExecutionError("\n".join(olines[:2]))
198
199     return common.parse_output(stdout)
200
201 def get_default_version(env):
202     debug('get_default_version()')
203
204     msvc_version = env.get('MSVC_VERSION')
205     msvs_version = env.get('MSVS_VERSION')
206
207     if msvs_version and not msvc_version:
208         SCons.Warnings.warn(
209                 SCons.Warnings.DeprecatedWarning,
210                 "MSVS_VERSION is deprecated: please use MSVC_VERSION instead ")
211         return msvs_version
212     elif msvc_version and msvs_version:
213         if not msvc_version == msvs_version:
214             SCons.Warnings.warn(
215                     SCons.Warnings.VisualVersionMismatch,
216                     "Requested msvc version (%s) and msvs version (%s) do " \
217                     "not match: please use MSVC_VERSION only to request a " \
218                     "visual studio version, MSVS_VERSION is deprecated" \
219                     % (msvc_version, msvs_version))
220         return msvs_version
221     if not msvc_version:
222         installed_vcs = get_installed_vcs()
223         debug('installed_vcs:%s' % installed_vcs)
224         if not installed_vcs:
225             msg = 'No installed VCs'
226             debug('msv %s\n' % repr(msg))
227             SCons.Warnings.warn(SCons.Warnings.VisualCMissingWarning, msg)
228             return None
229         msvc_version = installed_vcs[0]
230         debug('msvc_setup_env: using default installed MSVC version %s\n' % repr(msvc_version))
231
232     return msvc_version
233
234 def msvc_setup_env_once(env):
235     try:
236         has_run  = env["MSVC_SETUP_RUN"]
237     except KeyError:
238         has_run = False
239
240     if not has_run:
241         msvc_setup_env(env)
242         env["MSVC_SETUP_RUN"] = True
243
244 def msvc_setup_env(env):
245     debug('msvc_setup_env()')
246
247     version = get_default_version(env)
248     if version is None:
249         warn_msg = "No version of Visual Studio compiler found - C/C++ " \
250                    "compilers most likely not set correctly"
251         SCons.Warnings.warn(SCons.Warnings.VisualCMissingWarning, warn_msg)
252         return None
253
254     host_platform, target_platform = get_host_target(env)
255     debug('msvc_setup_env: using specified MSVC version %s\n' % repr(version))
256
257     # XXX: we set-up both MSVS version for backward
258     # compatibility with the msvs tool
259     env['MSVC_VERSION'] = version
260     env['MSVS_VERSION'] = version
261     env['MSVS'] = {}
262
263     try:
264         script = find_batch_file(version)
265     except VisualCException, e:
266         msg = str(e)
267         debug('Caught exception while looking for batch file (%s)' % msg)
268         warn_msg = "VC version %s not installed - C/C++ compilers most " \
269                    "likely not set correctly" % version
270         warn_msg += " \n Install versions are: %s" % get_installed_vcs()
271         SCons.Warnings.warn(SCons.Warnings.VisualCMissingWarning, warn_msg)
272         return None
273
274     use_script = env.get('MSVC_USE_SCRIPT', True)
275     if SCons.Util.is_String(use_script):
276         debug('use_script 1 %s\n' % repr(use_script))
277         d = script_env(use_script)
278     elif use_script:
279         host_target = (host_platform, target_platform)
280         arg = _HOST_TARGET_ARCH_TO_BAT_ARCH[host_target]
281         debug('use_script 2 %s, args:%s\n' % (repr(script), arg))
282         try:
283             d = script_env(script, args=arg)
284         except BatchFileExecutionError, e:
285             msg = "MSVC error while executing %s with args %s (error was %s)" % \
286                   (script, arg, str(e))
287             raise SCons.Errors.UserError(msg)
288     else:
289         debug('MSVC_USE_SCRIPT set to False')
290         warn_msg = "MSVC_USE_SCRIPT set to False, assuming environment " \
291                    "set correctly."
292         SCons.Warnings.warn(SCons.Warnings.VisualCMissingWarning, warn_msg)
293         return None
294
295     for k, v in d.items():
296         env.PrependENVPath(k, v, delete_existing=True)
297
298 def msvc_exists(version=None):
299     vcs = get_installed_vcs()
300     if version is None:
301         return len(vcs) > 0
302     return version in vcs
303