1# Copyright (c) 2003 David Abrahams.
2# Copyright (c) 2005 Vladimir Prus.
3# Copyright (c) 2005 Alexey Pakhunov.
4# Copyright (c) 2006 Bojan Resnik.
5# Copyright (c) 2006 Ilya Sokolov.
6# Copyright (c) 2007 Rene Rivera
7# Copyright (c) 2008 Jurko Gospodnetic
8# Copyright (c) 2011 Juraj Ivancic
9#
10# Use, modification and distribution is subject to the Boost Software
11# License Version 1.0. (See accompanying file LICENSE_1_0.txt or
12# http://www.boost.org/LICENSE_1_0.txt)
13
14################################################################################
15#
16# MSVC Boost Build toolset module.
17# --------------------------------
18#
19# All toolset versions need to have their location either auto-detected or
20# explicitly specified except for the special 'default' version that expects the
21# environment to find the needed tools or report an error.
22#
23################################################################################
24
25from os import environ
26import os.path
27import re
28import _winreg
29
30import bjam
31
32from b2.tools import common, rc, pch, builtin, mc, midl
33from b2.build import feature, type, toolset, generators, property_set
34from b2.build.property import Property
35from b2.util import path
36from b2.manager import get_manager
37from b2.build.generators import Generator
38from b2.build.toolset import flags
39from b2.util.utility import to_seq, on_windows
40from b2.tools.common import Configurations
41
42__debug = None
43
44def debug():
45    global __debug
46    if __debug is None:
47        __debug = "--debug-configuration" in bjam.variable("ARGV")
48    return __debug
49
50
51# It is not yet clear what to do with Cygwin on python port.
52def on_cygwin():
53    return False
54
55
56type.register('MANIFEST', ['manifest'])
57feature.feature('embed-manifest',['on','off'], ['incidental', 'propagated']) ;
58
59type.register('PDB',['pdb'])
60
61################################################################################
62#
63# Public rules.
64#
65################################################################################
66
67# Initialize a specific toolset version configuration. As the result, path to
68# compiler and, possible, program names are set up, and will be used when that
69# version of compiler is requested. For example, you might have:
70#
71#    using msvc : 6.5 : cl.exe ;
72#    using msvc : 7.0 : Y:/foo/bar/cl.exe ;
73#
74# The version parameter may be omitted:
75#
76#    using msvc : : Z:/foo/bar/cl.exe ;
77#
78# The following keywords have special meanings when specified as versions:
79#   - all     - all detected but not yet used versions will be marked as used
80#               with their default options.
81#   - default - this is an equivalent to an empty version.
82#
83# Depending on a supplied version, detected configurations and presence 'cl.exe'
84# in the path different results may be achieved. The following table describes
85# the possible scenarios:
86#
87#                                      Nothing            "x.y"
88# Passed   Nothing       "x.y"         detected,          detected,
89# version  detected      detected      cl.exe in path     cl.exe in path
90#
91# default  Error         Use "x.y"     Create "default"   Use "x.y"
92# all      None          Use all       None               Use all
93# x.y      -             Use "x.y"     -                  Use "x.y"
94# a.b      Error         Error         Create "a.b"       Create "a.b"
95#
96# "x.y" - refers to a detected version;
97# "a.b" - refers to an undetected version.
98#
99# FIXME: Currently the command parameter and the <compiler> property parameter
100# seem to overlap in duties. Remove this duplication. This seems to be related
101# to why someone started preparing to replace init with configure rules.
102
103def init(version = None, command = None, options = None):
104    # When initialized from
105    # using msvc : x.0 ;
106    # we get version as a single element list i.e. ['x.0'],
107    # but when specified from the command line we get a string i.e. 'x.0'.
108    # We want to work with a string, so unpack the list if needed.
109    is_single_element_list = (isinstance(version,list) and len(version) == 1)
110    assert(version==None or isinstance(version,str) or is_single_element_list)
111    if is_single_element_list:
112        version = version[0]
113
114    options = to_seq(options)
115    command = to_seq(command)
116
117    if command:
118        options.extend("<command>"+cmd for cmd in command)
119    configure(version,options)
120
121def configure(version=None, options=None):
122    if version == "all":
123        if options:
124            raise RuntimeError("MSVC toolset configuration: options should be empty when '{}' is specified.".format(version))
125
126        # Configure (i.e. mark as used) all registered versions.
127        all_versions = __versions.all()
128        if not all_versions:
129            if debug():
130                print "notice: [msvc-cfg] Asked to configure all registered" \
131                      "msvc toolset versions when there are none currently" \
132                      "registered." ;
133        else:
134            for v in all_versions:
135                # Note that there is no need to skip already configured
136                # versions here as this will request configure-really rule
137                # to configure the version using default options which will
138                # in turn cause it to simply do nothing in case the version
139                # has already been configured.
140                configure_really(v)
141    elif version == "default":
142        configure_really(None,options)
143    else:
144        configure_really(version, options)
145
146def extend_conditions(conditions,exts):
147    return [ cond + '/' + ext for cond in conditions for ext in exts ]
148
149def configure_version_specific(toolset_arg, version, conditions):
150    # Starting with versions 7.0, the msvc compiler have the /Zc:forScope and
151    # /Zc:wchar_t options that improve C++ standard conformance, but those
152    # options are off by default. If we are sure that the msvc version is at
153    # 7.*, add those options explicitly. We can be sure either if user specified
154    # version 7.* explicitly or if we auto-detected the version ourselves.
155    if not re.search('^6\\.', version):
156        toolset.flags('{}.compile'.format(toolset_arg), 'CFLAGS',conditions, ['/Zc:forScope','/Zc:wchar_t'])
157        toolset.flags('{}.compile.c++'.format(toolset_arg), 'C++FLAGS',conditions, ['/wd4675'])
158
159        # Explicitly disable the 'function is deprecated' warning. Some msvc
160        # versions have a bug, causing them to emit the deprecation warning even
161        # with /W0.
162        toolset.flags('{}.compile'.format(toolset_arg), 'CFLAGS',extend_conditions(conditions,['<warnings>off']), ['/wd4996'])
163        if re.search('^[78]\.', version):
164            # 64-bit compatibility warning deprecated since 9.0, see
165            # http://msdn.microsoft.com/en-us/library/yt4xw8fh.aspx
166            toolset.flags('{}.compile'.format(toolset_arg), 'CFLAGS',extend_conditions(conditions,['<warnings>all']), ['/Wp64'])
167
168    #
169    # Processor-specific optimization.
170    #
171    if re.search('^[67]', version ):
172        # 8.0 deprecates some of the options.
173        toolset.flags('{}.compile'.format(toolset_arg), 'CFLAGS', extend_conditions(conditions,['<optimization>speed','<optimization>space']), ['/Ogiy', '/Gs'])
174        toolset.flags('{}.compile'.format(toolset_arg), 'CFLAGS', extend_conditions(conditions,['<optimization>speed']), ['/Ot'])
175        toolset.flags('{}.compile'.format(toolset_arg), 'CFLAGS', extend_conditions(conditions,['<optimization>space']), ['/Os'])
176
177        cpu_arch_i386_cond = extend_conditions(conditions, __cpu_arch_i386)
178        toolset.flags('{}.compile'.format(toolset_arg), 'CFLAGS', extend_conditions(cpu_arch_i386_cond,['<instruction-set>']),['/GB'])
179        toolset.flags('{}.compile'.format(toolset_arg), 'CFLAGS', extend_conditions(cpu_arch_i386_cond,['<instruction-set>i486']),['/G4'])
180
181        toolset.flags('{}.compile'.format(toolset_arg), 'CFLAGS', extend_conditions(cpu_arch_i386_cond,['<instruction-set>' + t for t in __cpu_type_g5]), ['/G5'])
182        toolset.flags('{}.compile'.format(toolset_arg), 'CFLAGS', extend_conditions(cpu_arch_i386_cond,['<instruction-set>' + t for t in __cpu_type_g6]), ['/G6'])
183        toolset.flags('{}.compile'.format(toolset_arg), 'CFLAGS', extend_conditions(cpu_arch_i386_cond,['<instruction-set>' + t for t in __cpu_type_g7]), ['/G7'])
184
185        # Improve floating-point accuracy. Otherwise, some of C++ Boost's "math"
186        # tests will fail.
187        toolset.flags('{}.compile'.format(toolset_arg), 'CFLAGS', conditions, ['/Op'])
188
189        # 7.1 and below have single-threaded static RTL.
190        toolset.flags('{}.compile'.format(toolset_arg), 'CFLAGS', extend_conditions(conditions,['<runtime-debugging>off/<runtime-link>static/<threading>single']), ['/ML'])
191        toolset.flags('{}.compile'.format(toolset_arg), 'CFLAGS', extend_conditions(conditions,['<runtime-debugging>on/<runtime-link>static/<threading>single']), ['/MLd'])
192    else:
193        # 8.0 and above adds some more options.
194        toolset.flags('{}.compile'.format(toolset_arg), 'CFLAGS', extend_conditions(conditions, [a + '/<instruction-set>' for a in __cpu_arch_amd64]), ['/favor:blend'])
195
196        toolset.flags('{}.compile'.format(toolset_arg), 'CFLAGS', extend_conditions(conditions, [a + '/<instruction-set>' + t for a in __cpu_arch_amd64 for t in __cpu_type_em64t]), ['/favor:EM64T'])
197        toolset.flags('{}.compile'.format(toolset_arg), 'CFLAGS', extend_conditions(conditions, [a + '/<instruction-set>' + t for a in __cpu_arch_amd64 for t in __cpu_type_amd64]), ['/favor:AMD64'])
198
199        # 8.0 and above only has multi-threaded static RTL.
200        toolset.flags('{}.compile'.format(toolset_arg), 'CFLAGS', extend_conditions(conditions,['<runtime-debugging>off/<runtime-link>static/<threading>single']), ['/MT'])
201        toolset.flags('{}.compile'.format(toolset_arg), 'CFLAGS', extend_conditions(conditions,['<runtime-debugging>on/<runtime-link>static/<threading>single']), ['/MTd'])
202
203        # Specify target machine type so the linker will not need to guess.
204        toolset.flags('{}.link'.format(toolset_arg), 'LINKFLAGS', extend_conditions(conditions, __cpu_arch_amd64), ['/MACHINE:X64'])
205        toolset.flags('{}.link'.format(toolset_arg), 'LINKFLAGS', extend_conditions(conditions, __cpu_arch_i386), ['/MACHINE:X86'])
206        toolset.flags('{}.link'.format(toolset_arg), 'LINKFLAGS', extend_conditions(conditions, __cpu_arch_ia64), ['/MACHINE:IA64'])
207
208        # Make sure that manifest will be generated even if there is no
209        # dependencies to put there.
210        toolset.flags('{}.link'.format(toolset_arg), 'LINKFLAGS', conditions, ['/MANIFEST'])
211
212
213# Registers this toolset including all of its flags, features & generators. Does
214# nothing on repeated calls.
215
216def register_toolset():
217     if not 'msvc' in feature.values('toolset'):
218        register_toolset_really()
219
220
221engine = get_manager().engine()
222
223# this rule sets up the pdb file that will be used when generating static
224# libraries and the debug-store option is database, so that the compiler
225# puts all debug info into a single .pdb file named after the library
226#
227# Poking at source targets this way is probably not clean, but it's the
228# easiest approach.
229def archive(targets, sources=None, properties=None):
230    bjam.call('set-target-variable',targets,'PDB_NAME', os.path.splitext(targets[0])[0] + '.pdb')
231
232# Declare action for creating static libraries. If library exists, remove it
233# before adding files. See
234# http://article.gmane.org/gmane.comp.lib.boost.build/4241 for rationale.
235if not on_cygwin():
236    engine.register_action(
237        'msvc.archive',
238        '''if exist "$(<[1])" DEL "$(<[1])"
239        $(.LD) $(AROPTIONS) /out:"$(<[1])" @"@($(<[1]:W).rsp:E=
240"$(>)"
241$(LIBRARIES_MENTIONED_BY_FILE)
242"$(LIBRARY_OPTION)$(FINDLIBS_ST).lib"
243"$(LIBRARY_OPTION)$(FINDLIBS_SA).lib")"''',
244    function=archive)
245else:
246    engine.register_action(
247        'msvc.archive',
248        '''{rm} "$(<[1])"
249           $(.LD) $(AROPTIONS) /out:"$(<[1])" @"@($(<[1]:W).rsp:E=
250"$(>)"
251$(LIBRARIES_MENTIONED_BY_FILE)
252"$(LIBRARY_OPTION)$(FINDLIBS_ST).lib"
253"$(LIBRARY_OPTION)$(FINDLIBS_SA).lib")"'''.format(rm=common.rm_command()),
254        function=archive)
255
256# For the assembler the following options are turned on by default:
257#
258#   -Zp4   align structures to 4 bytes
259#   -Cp    preserve case of user identifiers
260#   -Cx    preserve case in publics, externs
261#
262engine.register_action(
263    'msvc.compile.asm',
264    '$(.ASM) -c -Zp4 -Cp -Cx -D$(DEFINES) $(ASMFLAGS) $(USER_ASMFLAGS) -Fo "$(<:W)" "$(>:W)"' )
265
266
267# Equivalent to [ on $(target) return $(prefix)$(var)$(suffix) ]. Note that $(var) can be a list.
268def expand_target_variable(target,var,prefix=None,suffix=None):
269    list = bjam.call( 'get-target-variable', target, var )
270    return " ".join([ ("" if prefix is None else prefix) + elem + ("" if suffix is None else suffix) for elem in list ])
271
272
273def get_rspline(targets, lang_opt):
274    result = lang_opt + '\n' + \
275        expand_target_variable(targets, 'UNDEFS'  , '\n-U'         ) + \
276        expand_target_variable(targets, 'CFLAGS'  , '\n'           ) + \
277        expand_target_variable(targets, 'C++FLAGS', '\n'           ) + \
278        expand_target_variable(targets, 'OPTIONS' , '\n'           ) + '\n-c' + \
279        expand_target_variable(targets, 'DEFINES' , '\n-D' , '\n'  ) + \
280        expand_target_variable(targets, 'INCLUDES', '\n"-I', '"\n' )
281    bjam.call('set-target-variable', targets, 'CC_RSPLINE', result)
282
283def compile_c(targets, sources = [], properties = None):
284    get_manager().engine().set_target_variable( targets[0], 'C++FLAGS', '' )
285    get_rspline(targets, '-TC')
286    compile_c_cpp(targets,sources)
287
288def compile_c_preprocess(targets, sources = [], properties = None):
289    get_manager().engine().set_target_variable( targets[0], 'C++FLAGS', '' )
290    get_rspline(targets, '-TC')
291    preprocess_c_cpp(targets,sources)
292
293def compile_c_pch(targets, sources = [], properties = []):
294    get_manager().engine().set_target_variable( targets[0], 'C++FLAGS', '' )
295    get_rspline([targets[0]], '-TC')
296    get_rspline([targets[1]], '-TC')
297
298toolset.flags( 'msvc', 'YLOPTION', [], ['-Yl'] )
299
300def compile_cpp(targets,sources=[],properties=None):
301    get_rspline(targets,'-TP')
302    bjam.call('set-target-variable', targets, 'PCH_FILE', sources)
303    compile_c_cpp(targets,sources)
304
305def compile_cpp_preprocess(targets,sources=[],properties=None):
306    get_rspline(targets,'-TP')
307    preprocess_c_cpp(targets,sources)
308
309def compile_cpp_pch(targets,sources=[],properties=None):
310    get_rspline([targets[0]], '-TP')
311    get_rspline([targets[1]], '-TP')
312
313
314# Action for running the C/C++ compiler without using precompiled headers.
315#
316# WARNING: Synchronize any changes this in action with intel-win
317#
318# Notes regarding PDB generation, for when we use <debug-symbols>on/<debug-store>database
319#
320# 1. PDB_CFLAG is only set for <debug-symbols>on/<debug-store>database, ensuring that the /Fd flag is dropped if PDB_CFLAG is empty
321#
322# 2. When compiling executables's source files, PDB_NAME is set on a per-source file basis by rule compile-c-c++.
323#    The linker will pull these into the executable's PDB
324#
325# 3. When compiling library's source files, PDB_NAME is updated to <libname>.pdb for each source file by rule archive,
326#    as in this case the compiler must be used to create a single PDB for our library.
327#
328
329class SetupAction:
330    def __init__(self, setup_func, function):
331        self.setup_func = setup_func
332        self.function = function
333
334    def __call__(self, targets, sources, property_set):
335        assert(callable(self.setup_func))
336        # This can modify sources.
337        action_name = self.setup_func(targets, sources, property_set)
338        # Bjam actions defined from Python have only the command
339        # to execute, and no associated jam procedural code. So
340        # passing 'property_set' to it is not necessary.
341        bjam.call("set-update-action", action_name, targets, sources, [])
342        if self.function:
343            self.function(targets, sources, property_set)
344
345def register_setup_action(action_name,setup_function,function=None):
346    global engine
347    if engine.actions.has_key(action_name):
348        raise "Bjam action %s is already defined" % action_name
349    engine.actions[action_name] = SetupAction(setup_function, function)
350
351
352engine.register_action('compile-c-c++',
353'$(.CC) @"@($(<[1]:W).rsp:E="$(>[1]:W)" -Fo"$(<[1]:W)" $(PDB_CFLAG)"$(PDB_NAME)" -Yu"$(>[3]:D=)" -Fp"$(>[2]:W)" $(CC_RSPLINE))" $(.CC.FILTER)''',
354bound_list=['PDB_NAME'])
355
356def setup_compile_c_cpp_action(targets, sources, properties):
357    sources += bjam.call('get-target-variable',targets,'PCH_FILE')
358    sources += bjam.call('get-target-variable',targets,'PCH_HEADER')
359    return 'compile-c-c++'
360
361
362register_setup_action(
363    'msvc.compile.c',
364    setup_compile_c_cpp_action,
365    function=compile_c)
366
367register_setup_action(
368    'msvc.compile.c++',
369    setup_compile_c_cpp_action,
370    function=compile_cpp)
371
372
373engine.register_action('preprocess-c-c++',
374'$(.CC) @"@($(<[1]:W).rsp:E="$(>[1]:W)" -E $(PDB_CFLAG)"$(PDB_NAME)" -Yu"$(>[3]:D=)" -Fp"$(>[2]:W)" $(CC_RSPLINE))" >"$(<[1]:W)"',
375bound_list=['PDB_NAME'])
376
377def setup_preprocess_c_cpp_action(targets, sources, properties):
378    sources += bjam.call('get-target-variable',targets,'PCH_FILE')
379    sources += bjam.call('get-target-variable',targets,'PCH_HEADER')
380    return 'preprocess-c-c++'
381
382register_setup_action(
383    'msvc.preprocess.c',
384    setup_preprocess_c_cpp_action,
385    function=compile_c_preprocess)
386
387register_setup_action(
388    'msvc.preprocess.c++',
389    setup_preprocess_c_cpp_action,
390    function=compile_cpp_preprocess)
391
392def compile_c_cpp(targets,sources=None):
393    pch_header = bjam.call('get-target-variable',targets[0],'PCH_HEADER')
394    pch_file = bjam.call('get-target-variable',targets[0],'PCH_FILE')
395    if pch_header: get_manager().engine().add_dependency(targets[0],pch_header)
396    if pch_file: get_manager().engine().add_dependency(targets[0],pch_file)
397    bjam.call('set-target-variable',targets,'PDB_NAME', os.path.splitext(targets[0])[0] + '.pdb')
398
399def preprocess_c_cpp(targets,sources=None):
400    #same as above
401    return compile_c_cpp(targets,sources)
402
403# Action for running the C/C++ compiler using precompiled headers. In addition
404# to whatever else it needs to compile, this action also adds a temporary source
405# .cpp file used to compile the precompiled headers themselves.
406
407
408engine.register_action('compile-c-c++-pch',
409'$(.CC) @"@($(<[1]:W).rsp:E="$(>[2]:W)" -Fo"$(<[2]:W)" -Yc"$(>[1]:D=)" $(YLOPTION)"__bjam_pch_symbol_$(>[1]:D=)" -Fp"$(<[1]:W)" $(CC_RSPLINE))" "@($(<[1]:W).cpp:E=#include "$(>[1]:D=)"\n)" $(.CC.FILTER)')
410
411engine.register_action('compile-c-c++-pch-s',
412'$(.CC) @"@($(<[1]:W).rsp:E="$(>[2]:W)" -Fo"$(<[2]:W)" -Yc"$(>[1]:D=)" $(YLOPTION)"__bjam_pch_symbol_$(>[1]:D=)" -Fp"$(<[1]:W)" $(CC_RSPLINE))" $(.CC.FILTER)')
413
414def setup_c_cpp_pch(targets, sources, properties):
415    pch_source = bjam.call('get-target-variable', targets, 'PCH_SOURCE')
416    if pch_source:
417        sources += pch_source
418        get_manager().engine().add_dependency(targets,pch_source)
419        return 'compile-c-c++-pch-s'
420    else:
421        return 'compile-c-c++-pch'
422
423register_setup_action(
424    'msvc.compile.c.pch',
425    setup_c_cpp_pch,
426    function=compile_c_pch)
427
428register_setup_action(
429    'msvc.compile.c++.pch',
430    setup_c_cpp_pch,
431    function=compile_cpp_pch)
432
433
434# See midl.py for details.
435#
436engine.register_action(
437    'msvc.compile.idl',
438    '''$(.IDL) /nologo @"@($(<[1]:W).rsp:E=
439"$(>:W)"
440-D$(DEFINES)
441"-I$(INCLUDES:W)"
442-U$(UNDEFS)
443$(MIDLFLAGS)
444/tlb "$(<[1]:W)"
445/h "$(<[2]:W)"
446/iid "$(<[3]:W)"
447/proxy "$(<[4]:W)"
448/dlldata "$(<[5]:W)")"
449    {touch} "$(<[4]:W)"
450    {touch} "$(<[5]:W)"'''.format(touch=common.file_creation_command()))
451
452engine.register_action(
453    'msvc.compile.mc',
454    '$(.MC) $(MCFLAGS) -h "$(<[1]:DW)" -r "$(<[2]:DW)" "$(>:W)"')
455
456engine.register_action(
457    'msvc.compile.rc',
458    '$(.RC) -l 0x409 -U$(UNDEFS) -D$(DEFINES) -I"$(INCLUDES:W)" -fo "$(<:W)" "$(>:W)"')
459
460def link_dll(targets,sources=None,properties=None):
461    get_manager().engine().add_dependency(targets,bjam.call('get-target-variable',targets,'DEF_FILE'))
462    manifest(targets, sources, properties)
463
464def manifest(targets,sources=None,properties=None):
465    if 'on' in properties.get('<embed-manifest>'):
466        get_manager().engine().set_update_action('msvc.manifest', targets, sources, properties)
467
468
469# Incremental linking a DLL causes no end of problems: if the actual exports do
470# not change, the import .lib file is never updated. Therefore, the .lib is
471# always out-of-date and gets rebuilt every time. I am not sure that incremental
472# linking is such a great idea in general, but in this case I am sure we do not
473# want it.
474
475# Windows manifest is a new way to specify dependencies on managed DotNet
476# assemblies and Windows native DLLs. The manifests are embedded as resources
477# and are useful in any PE target (both DLL and EXE).
478
479if not on_cygwin():
480    engine.register_action(
481        'msvc.link',
482        '''$(.LD) $(LINKFLAGS) /out:"$(<[1]:W)" /LIBPATH:"$(LINKPATH:W)" $(OPTIONS) @"@($(<[1]:W).rsp:E=
483"$(>)"
484$(LIBRARIES_MENTIONED_BY_FILE)
485$(LIBRARIES)
486"$(LIBRARY_OPTION)$(FINDLIBS_ST).lib"
487"$(LIBRARY_OPTION)$(FINDLIBS_SA).lib")"
488if %ERRORLEVEL% NEQ 0 EXIT %ERRORLEVEL%''',
489        function=manifest,
490        bound_list=['PDB_NAME','DEF_FILE','LIBRARIES_MENTIONED_BY_FILE'])
491
492    engine.register_action(
493        'msvc.manifest',
494        '''if exist "$(<[1]).manifest" (
495            $(.MT) -manifest "$(<[1]).manifest" "-outputresource:$(<[1]);1"
496        )''')
497
498    engine.register_action(
499        'msvc.link.dll',
500        '''$(.LD) /DLL $(LINKFLAGS) /out:"$(<[1]:W)" /IMPLIB:"$(<[2]:W)" /LIBPATH:"$(LINKPATH:W)" /def:"$(DEF_FILE)" $(OPTIONS) @"@($(<[1]:W).rsp:E=
501"$(>)"
502$(LIBRARIES_MENTIONED_BY_FILE)
503$(LIBRARIES)
504"$(LIBRARY_OPTION)$(FINDLIBS_ST).lib"
505"$(LIBRARY_OPTION)$(FINDLIBS_SA).lib")"
506if %ERRORLEVEL% NEQ 0 EXIT %ERRORLEVEL%''',
507        function=link_dll,
508        bound_list=['DEF_FILE','LIBRARIES_MENTIONED_BY_FILE'])
509
510    engine.register_action(
511        'msvc.manifest.dll',
512        '''if exist "$(<[1]).manifest" (
513            $(.MT) -manifest "$(<[1]).manifest" "-outputresource:$(<[1]);2"
514        )''')
515else:
516    engine.register_action(
517        'msvc.link',
518        '''$(.LD) $(LINKFLAGS) /out:"$(<[1]:W)" /LIBPATH:"$(LINKPATH:W)" $(OPTIONS) @"@($(<[1]:W).rsp:E=
519"$(>)"
520$(LIBRARIES_MENTIONED_BY_FILE)
521$(LIBRARIES)
522"$(LIBRARY_OPTION)$(FINDLIBS_ST).lib"
523"$(LIBRARY_OPTION)$(FINDLIBS_SA).lib")"''',
524        function=manifest,
525        bound_list=['PDB_NAME','DEF_FILE','LIBRARIES_MENTIONED_BY_FILE'])
526
527    engine.register_action(
528        'msvc.manifest',
529        '''if test -e "$(<[1]).manifest"; then
530            $(.MT) -manifest "$(<[1]).manifest" "-outputresource:$(<[1]);1"
531        fi''')
532
533    engine.register_action(
534        'msvc.link.dll',
535        '''$(.LD) /DLL $(LINKFLAGS) /out:"$(<[1]:W)" /IMPLIB:"$(<[2]:W)" /LIBPATH:"$(LINKPATH:W)" /def:"$(DEF_FILE)" $(OPTIONS) @"@($(<[1]:W).rsp:E=
536"$(>)"
537$(LIBRARIES_MENTIONED_BY_FILE)
538$(LIBRARIES)
539"$(LIBRARY_OPTION)$(FINDLIBS_ST).lib"
540"$(LIBRARY_OPTION)$(FINDLIBS_SA).lib")"''',
541        function=link_dll,
542        bound_list=['DEF_FILE','LIBRARIES_MENTIONED_BY_FILE'])
543
544    engine.register_action(
545        'msvc.manifest.dll',
546        '''if test -e "$(<[1]).manifest"; then
547            $(.MT) -manifest "$(<[1]).manifest" "-outputresource:$(<[1]);2"
548        fi''')
549
550
551################################################################################
552#
553# Classes.
554#
555################################################################################
556
557class MsvcPchGenerator(pch.PchGenerator):
558
559    # Inherit the __init__ method
560    def run_pch(self, project, name, prop_set, sources):
561        # Find the header in sources. Ignore any CPP sources.
562        pch_header = None
563        pch_source = None
564        for s in sources:
565            if type.is_derived(s.type(), 'H'):
566                pch_header = s
567            elif type.is_derived(s.type(), 'CPP') or type.is_derived(s.type(), 'C'):
568                pch_source = s
569
570        if not pch_header:
571            raise RuntimeError( "can not build pch without pch-header" )
572
573        # If we do not have the PCH source - that is fine. We will just create a
574        # temporary .cpp file in the action.
575        properties = prop_set.all()
576        # Passing of <pch-source> is a dirty trick, needed because
577        # non-composing generators with multiple inputs are subtly
578        # broken. For more detailed information see:
579        # https://zigzag.cs.msu.su:7813/boost.build/ticket/111
580        if pch_source:
581            properties.append(Property('pch-source',pch_source))
582        generated = Generator.run(self,project,name,property_set.create(properties),[pch_header])
583        pch_file = None
584        for g in generated:
585            if type.is_derived(g.type(), 'PCH'):
586                pch_file = g
587        result_props = []
588        if pch_header:
589            result_props.append(Property('pch-header', pch_header))
590        if pch_file:
591            result_props.append(Property('pch-file', pch_file))
592
593        return property_set.PropertySet(result_props), generated
594
595
596################################################################################
597#
598# Local rules.
599#
600################################################################################
601
602# Detects versions listed as '_known_versions' by checking registry information,
603# environment variables & default paths. Supports both native Windows and
604# Cygwin.
605def auto_detect_toolset_versions():
606    if on_windows() or on_cygwin():
607        for version in _known_versions:
608            versionVarName = '__version_{}_reg'.format(version.replace('.','_'))
609            if versionVarName in globals():
610                vc_path = None
611                for x64elt in [ '', 'Wow6432Node\\' ]:
612                    try:
613                        with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\\{}Microsoft\\{}'.format(x64elt, globals()[versionVarName])) as reg_key:
614                            vc_path = _winreg.QueryValueEx(reg_key, "ProductDir")[0]
615                    except:
616                        pass
617                if vc_path:
618                    vc_path = os.path.join(vc_path,'bin')
619                    register_configuration(version,os.path.normpath(vc_path))
620
621    for i in _known_versions:
622        if not i in __versions.all():
623            register_configuration(i,default_path(i))
624
625
626# Worker rule for toolset version configuration. Takes an explicit version id or
627# nothing in case it should configure the default toolset version (the first
628# registered one or a new 'default' one in case no toolset versions have been
629# registered yet).
630#
631
632def configure_really(version=None, options=[]):
633    v = version
634    if not v:
635        # Take the first registered (i.e. auto-detected) version.
636        version = __versions.first()
637        v = version
638
639        # Note: 'version' can still be empty at this point if no versions have
640        # been auto-detected.
641        if not version:
642            version = "default"
643
644    # Version alias -> real version number.
645    version = globals().get("__version_alias_{}".format(version), version)
646
647    # Check whether the selected configuration is already in use.
648    if version in __versions.used():
649        # Allow multiple 'toolset.using' calls for the same configuration if the
650        # identical sets of options are used.
651        if options and options != __versions.get(version,'options'):
652            raise RuntimeError("MSVC toolset configuration: Toolset version '$(version)' already configured.".format(version))
653    else:
654        # Register a new configuration.
655        __versions.register(version)
656
657        # Add user-supplied to auto-detected options.
658        version_opts = __versions.get(version, 'options')
659        if (version_opts):
660            options = version_opts + options
661
662        # Mark the configuration as 'used'.
663        __versions.use(version)
664        # Generate conditions and save them.
665        conditions = common.check_init_parameters('msvc', None, ('version', v))
666        __versions.set(version, 'conditions', conditions)
667        command = feature.get_values('<command>', options)
668
669        # If version is specified, we try to search first in default paths, and
670        # only then in PATH.
671        command = common.get_invocation_command('msvc', 'cl.exe', command, default_paths(version))
672        common.handle_options('msvc', conditions, command, options)
673
674        if not version:
675            # Even if version is not explicitly specified, try to detect the
676            # version from the path.
677            # FIXME: We currently detect both Microsoft Visual Studio 9.0 and
678            # 9.0express as 9.0 here.
679            if re.search("Microsoft Visual Studio 14", command):
680                version = '14.0'
681            elif re.search("Microsoft Visual Studio 12", command):
682                version = '12.0'
683            elif re.search("Microsoft Visual Studio 11", command):
684                version = '11.0'
685            elif re.search("Microsoft Visual Studio 10", command):
686                version = '10.0'
687            elif re.search("Microsoft Visual Studio 9", command):
688                version = '9.0'
689            elif re.search("Microsoft Visual Studio 8", command):
690                version = '8.0'
691            elif re.search("NET 2003[\/\\]VC7", command):
692                version = '7.1'
693            elif re.search("Microsoft Visual C\\+\\+ Toolkit 2003", command):
694                version = '7.1toolkit'
695            elif re.search(".NET[\/\\]VC7", command):
696                version = '7.0'
697            else:
698                version = '6.0'
699
700        # Generate and register setup command.
701
702        below_8_0 = re.search("^[67]\\.",version) != None
703
704        if below_8_0:
705            cpu = ['i386']
706        else:
707            cpu = ['i386', 'amd64', 'ia64']
708
709        setup_scripts = {}
710
711        if command:
712            # TODO: Note that if we specify a non-existant toolset version then
713            # this rule may find and use a corresponding compiler executable
714            # belonging to an incorrect toolset version. For example, if you
715            # have only MSVC 7.1 installed, have its executable on the path and
716            # specify you want Boost Build to use MSVC 9.0, then you want Boost
717            # Build to report an error but this may cause it to silently use the
718            # MSVC 7.1 compiler even though it thinks it is using the msvc-9.0
719            # toolset version.
720            command = common.get_absolute_tool_path(command)
721
722        if command:
723            parent = os.path.dirname(os.path.normpath(command))
724            # Setup will be used if the command name has been specified. If
725            # setup is not specified explicitly then a default setup script will
726            # be used instead. Setup scripts may be global or arhitecture/
727            # /platform/cpu specific. Setup options are used only in case of
728            # global setup scripts.
729
730            # Default setup scripts provided with different VC distributions:
731            #
732            #   VC 7.1 had only the vcvars32.bat script specific to 32 bit i386
733            # builds. It was located in the bin folder for the regular version
734            # and in the root folder for the free VC 7.1 tools.
735            #
736            #   Later 8.0 & 9.0 versions introduce separate platform specific
737            # vcvars*.bat scripts (e.g. 32 bit, 64 bit AMD or 64 bit Itanium)
738            # located in or under the bin folder. Most also include a global
739            # vcvarsall.bat helper script located in the root folder which runs
740            # one of the aforementioned vcvars*.bat scripts based on the options
741            # passed to it. So far only the version coming with some PlatformSDK
742            # distributions does not include this top level script but to
743            # support those we need to fall back to using the worker scripts
744            # directly in case the top level script can not be found.
745
746            global_setup = feature.get_values('<setup>',options)
747            if global_setup:
748                global_setup = global_setup[0]
749            else:
750                global_setup = None
751
752            if not below_8_0 and not global_setup:
753                global_setup = locate_default_setup(command,parent,'vcvarsall.bat')
754
755
756            default_setup = {
757                'amd64' : 'vcvarsx86_amd64.bat',
758                'i386' : 'vcvars32.bat',
759                'ia64' : 'vcvarsx86_ia64.bat' }
760
761            # http://msdn2.microsoft.com/en-us/library/x4d2c09s(VS.80).aspx and
762            # http://msdn2.microsoft.com/en-us/library/x4d2c09s(vs.90).aspx
763            # mention an x86_IPF option, that seems to be a documentation bug
764            # and x86_ia64 is the correct option.
765            default_global_setup_options = {
766                'amd64' : 'x86_amd64',
767                'i386' : 'x86',
768                'ia64' : 'x86_ia64' }
769
770            somehow_detect_the_itanium_platform = None
771            # When using 64-bit Windows, and targeting 64-bit, it is possible to
772            # use a native 64-bit compiler, selected by the "amd64" & "ia64"
773            # parameters to vcvarsall.bat. There are two variables we can use --
774            # PROCESSOR_ARCHITECTURE and PROCESSOR_IDENTIFIER. The first is
775            # 'x86' when running 32-bit Windows, no matter which processor is
776            # used, and 'AMD64' on 64-bit windows on x86 (either AMD64 or EM64T)
777            # Windows.
778            #
779            if re.search( 'AMD64', environ[ "PROCESSOR_ARCHITECTURE" ] ) != None:
780                default_global_setup_options[ 'amd64' ] = 'amd64'
781            # TODO: The same 'native compiler usage' should be implemented for
782            # the Itanium platform by using the "ia64" parameter. For this
783            # though we need someone with access to this platform who can find
784            # out how to correctly detect this case.
785            elif somehow_detect_the_itanium_platform:
786                default_global_setup_options[ 'ia64' ] = 'ia64'
787
788            setup_prefix = "call "
789            setup_suffix = """ >nul\n"""
790            if on_cygwin():
791                setup_prefix = "cmd.exe /S /C call "
792                setup_suffix = " \">nul\" \"&&\" "
793
794            for c in cpu:
795                setup_options = None
796                setup_cpu = feature.get_values('<setup-{}>'.format(c),options)
797
798                if not setup_cpu:
799                    if global_setup:
800                        setup_cpu = global_setup
801                        # If needed we can easily add using configuration flags
802                        # here for overriding which options get passed to the
803                        # global setup command for which target platform:
804                        # setup_options = feature.get_values('<setup-options-{}>'.format(c),options)
805                        if not setup_options:
806                            setup_options = default_global_setup_options[ c ]
807                    else:
808                        setup_cpu = locate_default_setup(command, parent, default_setup[ c ])
809
810                # Cygwin to Windows path translation.
811                # setup-$(c) = "\""$(setup-$(c):W)"\"" ;
812
813                # Append setup options to the setup name and add the final setup
814                # prefix & suffix.
815                setup_scripts[ c ] = '{}"{}" {}{}'.format(setup_prefix, setup_cpu, setup_options, setup_suffix)
816
817        # Get tool names (if any) and finish setup.
818        compiler = feature.get_values("<compiler>", options)
819        if not compiler:
820            compiler = "cl"
821
822        linker = feature.get_values("<linker>", options)
823        if not linker:
824            linker = "link"
825
826        resource_compiler = feature.get_values("<resource-compiler>", options)
827        if not resource_compiler:
828            resource_compiler = "rc"
829
830        # Turn on some options for i386 assembler
831        #  -coff  generate COFF format object file (compatible with cl.exe output)
832        default_assembler_amd64 = 'ml64'
833        default_assembler_i386  = 'ml -coff'
834        default_assembler_ia64  = 'ias'
835
836        assembler = feature.get_values('<assembler>',options)
837
838        idl_compiler = feature.get_values('<idl-compiler>',options)
839        if not idl_compiler:
840            idl_compiler = 'midl'
841
842        mc_compiler = feature.get_values('<mc-compiler>',options)
843        if not mc_compiler:
844            mc_compiler = 'mc'
845
846        manifest_tool = feature.get_values('<manifest-tool>',options)
847        if not manifest_tool:
848            manifest_tool = 'mt'
849
850        cc_filter = feature.get_values('<compiler-filter>',options)
851
852        for c in cpu:
853            cpu_conditions = [ condition + '/' + arch for arch in globals()['__cpu_arch_{}'.format(c)] for condition in conditions ]
854
855            setup_script = setup_scripts.get(c, '')
856
857            if debug():
858                for cpu_condition in cpu_conditions:
859                    print "notice: [msvc-cfg] condition: '{}', setup: '{}'".format(cpu_condition,setup_script)
860
861            cpu_assembler = assembler
862            if not cpu_assembler:
863                cpu_assembler = locals()['default_assembler_{}'.format(c)]
864
865            toolset.flags('msvc.compile', '.CC' , cpu_conditions, ['{}{} /Zm800 -nologo'         .format(setup_script, compiler)])
866            toolset.flags('msvc.compile', '.RC' , cpu_conditions, ['{}{}'                        .format(setup_script, resource_compiler)])
867            toolset.flags('msvc.compile', '.ASM', cpu_conditions, ['{}{} -nologo'                .format(setup_script, cpu_assembler)])
868            toolset.flags('msvc.link'   , '.LD' , cpu_conditions, ['{}{} /NOLOGO /INCREMENTAL:NO'.format(setup_script, linker)])
869            toolset.flags('msvc.archive', '.LD' , cpu_conditions, ['{}{} /lib /NOLOGO'           .format(setup_script, linker)])
870            toolset.flags('msvc.compile', '.IDL', cpu_conditions, ['{}{}'                        .format(setup_script, idl_compiler)])
871            toolset.flags('msvc.compile', '.MC' , cpu_conditions, ['{}{}'                        .format(setup_script, mc_compiler)])
872            toolset.flags('msvc.link'   , '.MT' , cpu_conditions, ['{}{} -nologo'                .format(setup_script, manifest_tool)])
873
874            if cc_filter:
875                toolset.flags('msvc', '.CC.FILTER', cpu_conditions, ['"|" {}'.format(cc_filter)])
876
877        # Set version-specific flags.
878        configure_version_specific('msvc', version, conditions)
879
880
881# Returns the default installation path for the given version.
882#
883def default_path(version):
884    # Use auto-detected path if possible.
885    options = __versions.get(version, 'options')
886    tmp_path = None
887    if options:
888        tmp_path = feature.get_values('<command>', options)
889
890    if tmp_path:
891        tmp_path="".join(tmp_path)
892        tmp_path=os.path.dirname(tmp_path)
893    else:
894        env_var_var_name = '__version_{}_env'.format(version.replace('.','_'))
895        vc_path = None
896        if env_var_var_name in globals():
897            env_var_name = globals()[env_var_var_name]
898            if env_var_name in os.environ:
899                vc_path = environ[env_var_name]
900        if vc_path:
901            vc_path = os.path.join(vc_path,globals()['__version_{}_envpath'.format(version.replace('.','_'))])
902            tmp_path = os.path.normpath(vc_path)
903
904    var_name = '__version_{}_path'.format(version.replace('.','_'))
905    if not tmp_path and var_name in globals():
906        tmp_path = os.path.normpath(os.path.join(common.get_program_files_dir(), globals()[var_name]))
907    return tmp_path
908
909
910# Returns either the default installation path (if 'version' is not empty) or
911# list of all known default paths (if no version is given)
912#
913def default_paths(version = None):
914    possible_paths = []
915    if version:
916        path = default_path(version)
917        if path:
918            possible_paths.append(path)
919    else:
920        for i in _known_versions:
921            path = default_path(i)
922            if path:
923                possible_paths.append(path)
924    return possible_paths
925
926
927class MsvcLinkingGenerator(builtin.LinkingGenerator):
928    # Calls the base version.  If necessary, also create a target for the
929    # manifest file.specifying source's name as the name of the created
930    # target. As result, the PCH will be named whatever.hpp.gch, and not
931    # whatever.gch.
932    def generated_targets(self, sources, prop_set, project, name):
933        result = builtin.LinkingGenerator.generated_targets(self, sources, prop_set, project, name)
934        if result:
935            name_main = result[0].name()
936            action = result[0].action()
937
938            if prop_set.get('<debug-symbols>') == 'on':
939                # We force exact name on PDB. The reason is tagging -- the tag rule may
940                # reasonably special case some target types, like SHARED_LIB. The tag rule
941                # will not catch PDB, and it cannot even easily figure if PDB is paired with
942                # SHARED_LIB or EXE or something else. Because PDB always get the
943                # same name as the main target, with .pdb as extension, just force it.
944                target = FileTarget(name_main.split_ext()[0]+'.pdb','PDB',project,action,True)
945                registered_target = virtual_target.register(target)
946                if target != registered_target:
947                    action.replace_targets(target,registered_target)
948                result.append(registered_target)
949            if prop_set.get('<embed-manifest>') == 'off':
950                # Manifest is evil target. It has .manifest appened to the name of
951                # main target, including extension. E.g. a.exe.manifest. We use 'exact'
952                # name because to achieve this effect.
953                target = FileTarget(name_main+'.manifest', 'MANIFEST', project, action, True)
954                registered_target = virtual_target.register(target)
955                if target != registered_target:
956                    action.replace_targets(target,registered_target)
957                result.append(registered_target)
958        return result
959
960
961# Unsafe worker rule for the register-toolset() rule. Must not be called
962# multiple times.
963
964def register_toolset_really():
965    feature.extend('toolset', ['msvc'])
966
967    # Intel and msvc supposedly have link-compatible objects.
968    feature.subfeature( 'toolset', 'msvc', 'vendor', 'intel', ['propagated', 'optional'])
969
970    # Inherit MIDL flags.
971    toolset.inherit_flags('msvc', 'midl')
972
973    # Inherit MC flags.
974    toolset.inherit_flags('msvc','mc')
975
976    # Dynamic runtime comes only in MT flavour.
977    toolset.add_requirements(['<toolset>msvc,<runtime-link>shared:<threading>multi'])
978
979    # Declare msvc toolset specific features.
980    feature.feature('debug-store', ['object', 'database'], ['propagated'])
981    feature.feature('pch-source', [], ['dependency', 'free'])
982
983    # Declare generators.
984
985    # TODO: Is it possible to combine these? Make the generators
986    # non-composing so that they do not convert each source into a separate
987    # .rsp file.
988    generators.register(MsvcLinkingGenerator('msvc.link', True, ['OBJ', 'SEARCHED_LIB', 'STATIC_LIB', 'IMPORT_LIB'], ['EXE'], ['<toolset>msvc']))
989    generators.register(MsvcLinkingGenerator('msvc.link.dll', True, ['OBJ', 'SEARCHED_LIB', 'STATIC_LIB', 'IMPORT_LIB'], ['SHARED_LIB','IMPORT_LIB'], ['<toolset>msvc']))
990
991    builtin.register_archiver('msvc.archive', ['OBJ'], ['STATIC_LIB'], ['<toolset>msvc'])
992    builtin.register_c_compiler('msvc.compile.c++', ['CPP'], ['OBJ'], ['<toolset>msvc'])
993    builtin.register_c_compiler('msvc.compile.c', ['C'], ['OBJ'], ['<toolset>msvc'])
994    builtin.register_c_compiler('msvc.compile.c++.preprocess', ['CPP'], ['PREPROCESSED_CPP'], ['<toolset>msvc'])
995    builtin.register_c_compiler('msvc.compile.c.preprocess', ['C'], ['PREPROCESSED_C'], ['<toolset>msvc'])
996
997    # Using 'register-c-compiler' adds the build directory to INCLUDES.
998    builtin.register_c_compiler('msvc.compile.rc', ['RC'], ['OBJ(%_res)'], ['<toolset>msvc'])
999    generators.override('msvc.compile.rc', 'rc.compile.resource')
1000    generators.register_standard('msvc.compile.asm', ['ASM'], ['OBJ'], ['<toolset>msvc'])
1001
1002    builtin.register_c_compiler('msvc.compile.idl', ['IDL'], ['MSTYPELIB', 'H', 'C(%_i)', 'C(%_proxy)', 'C(%_dlldata)'], ['<toolset>msvc'])
1003    generators.override('msvc.compile.idl', 'midl.compile.idl')
1004
1005    generators.register_standard('msvc.compile.mc', ['MC'], ['H','RC'], ['<toolset>msvc'])
1006    generators.override('msvc.compile.mc', 'mc.compile')
1007
1008    # Note: the 'H' source type will catch both '.h' and '.hpp' headers as
1009    # the latter have their HPP type derived from H. The type of compilation
1010    # is determined entirely by the destination type.
1011    generators.register(MsvcPchGenerator('msvc.compile.c.pch', False, ['H'], ['C_PCH','OBJ'], ['<pch>on', '<toolset>msvc']))
1012    generators.register(MsvcPchGenerator('msvc.compile.c++.pch', False, ['H'], ['CPP_PCH','OBJ'], ['<pch>on', '<toolset>msvc']))
1013
1014    generators.override('msvc.compile.c.pch', 'pch.default-c-pch-generator')
1015    generators.override('msvc.compile.c++.pch', 'pch.default-cpp-pch-generator')
1016
1017    toolset.flags('msvc.compile', 'PCH_FILE'  , ['<pch>on'], ['<pch-file>'  ])
1018    toolset.flags('msvc.compile', 'PCH_SOURCE', ['<pch>on'], ['<pch-source>'])
1019    toolset.flags('msvc.compile', 'PCH_HEADER', ['<pch>on'], ['<pch-header>'])
1020
1021    #
1022    # Declare flags for compilation.
1023    #
1024    toolset.flags('msvc.compile', 'CFLAGS', ['<optimization>speed'], ['/O2'])
1025    toolset.flags('msvc.compile', 'CFLAGS', ['<optimization>space'], ['/O1'])
1026
1027    toolset.flags('msvc.compile', 'CFLAGS',  [ a + '/<instruction-set>' + t for a in __cpu_arch_ia64 for t in __cpu_type_itanium ], ['/G1'])
1028    toolset.flags('msvc.compile', 'CFLAGS',  [ a + '/<instruction-set>' + t for a in __cpu_arch_ia64 for t in __cpu_type_itanium2 ], ['/G2'])
1029
1030    toolset.flags('msvc.compile', 'CFLAGS', ['<debug-symbols>on/<debug-store>object'], ['/Z7'])
1031    toolset.flags('msvc.compile', 'CFLAGS', ['<debug-symbols>on/<debug-store>database'], ['/Zi'])
1032    toolset.flags('msvc.compile', 'CFLAGS', ['<optimization>off'], ['/Od'])
1033    toolset.flags('msvc.compile', 'CFLAGS', ['<inlining>off'], ['/Ob0'])
1034    toolset.flags('msvc.compile', 'CFLAGS', ['<inlining>on'], ['/Ob1'])
1035    toolset.flags('msvc.compile', 'CFLAGS', ['<inlining>full'], ['/Ob2'])
1036
1037    toolset.flags('msvc.compile', 'CFLAGS', ['<warnings>on'], ['/W3'])
1038    toolset.flags('msvc.compile', 'CFLAGS', ['<warnings>off'], ['/W0'])
1039    toolset.flags('msvc.compile', 'CFLAGS', ['<warnings>all'], ['/W4'])
1040    toolset.flags('msvc.compile', 'CFLAGS', ['<warnings-as-errors>on'], ['/WX'])
1041
1042    toolset.flags('msvc.compile', 'C++FLAGS', ['<exception-handling>on/<asynch-exceptions>off/<extern-c-nothrow>off'], ['/EHs'])
1043    toolset.flags('msvc.compile', 'C++FLAGS', ['<exception-handling>on/<asynch-exceptions>off/<extern-c-nothrow>on'], ['/EHsc'])
1044    toolset.flags('msvc.compile', 'C++FLAGS', ['<exception-handling>on/<asynch-exceptions>on/<extern-c-nothrow>off'], ['/EHa'])
1045    toolset.flags('msvc.compile', 'C++FLAGS', ['<exception-handling>on/<asynch-exceptions>on/<extern-c-nothrow>on'], ['/EHac'])
1046
1047    # By default 8.0 enables rtti support while prior versions disabled it. We
1048    # simply enable or disable it explicitly so we do not have to depend on this
1049    # default behaviour.
1050    toolset.flags('msvc.compile', 'CFLAGS', ['<rtti>on'], ['/GR'])
1051    toolset.flags('msvc.compile', 'CFLAGS', ['<rtti>off'], ['/GR-'])
1052    toolset.flags('msvc.compile', 'CFLAGS', ['<runtime-debugging>off/<runtime-link>shared'], ['/MD'])
1053    toolset.flags('msvc.compile', 'CFLAGS', ['<runtime-debugging>on/<runtime-link>shared'], ['/MDd'])
1054
1055    toolset.flags('msvc.compile', 'CFLAGS', ['<runtime-debugging>off/<runtime-link>static/<threading>multi'], ['/MT'])
1056    toolset.flags('msvc.compile', 'CFLAGS', ['<runtime-debugging>on/<runtime-link>static/<threading>multi'], ['/MTd'])
1057
1058    toolset.flags('msvc.compile', 'OPTIONS', [], ['<cflags>'])
1059    toolset.flags('msvc.compile.c++', 'OPTIONS', [], ['<cxxflags>'])
1060
1061    toolset.flags('msvc.compile', 'PDB_CFLAG', ['<debug-symbols>on/<debug-store>database'],['/Fd'])
1062
1063    toolset.flags('msvc.compile', 'DEFINES', [], ['<define>'])
1064    toolset.flags('msvc.compile', 'UNDEFS', [], ['<undef>'])
1065    toolset.flags('msvc.compile', 'INCLUDES', [], ['<include>'])
1066
1067    # Declare flags for the assembler.
1068    toolset.flags('msvc.compile.asm', 'USER_ASMFLAGS', [], ['<asmflags>'])
1069
1070    toolset.flags('msvc.compile.asm', 'ASMFLAGS', ['<debug-symbols>on'], ['/Zi', '/Zd'])
1071
1072    toolset.flags('msvc.compile.asm', 'ASMFLAGS', ['<warnings>on'], ['/W3'])
1073    toolset.flags('msvc.compile.asm', 'ASMFLAGS', ['<warnings>off'], ['/W0'])
1074    toolset.flags('msvc.compile.asm', 'ASMFLAGS', ['<warnings>all'], ['/W4'])
1075    toolset.flags('msvc.compile.asm', 'ASMFLAGS', ['<warnings-as-errors>on'], ['/WX'])
1076
1077    toolset.flags('msvc.compile.asm', 'DEFINES', [], ['<define>'])
1078
1079    # Declare flags for linking.
1080    toolset.flags('msvc.link', 'PDB_LINKFLAG', ['<debug-symbols>on/<debug-store>database'], ['/PDB'])  # not used yet
1081    toolset.flags('msvc.link', 'LINKFLAGS', ['<debug-symbols>on'], ['/DEBUG'])
1082    toolset.flags('msvc.link', 'DEF_FILE', [], ['<def-file>'])
1083
1084    # The linker disables the default optimizations when using /DEBUG so we
1085    # have to enable them manually for release builds with debug symbols.
1086    toolset.flags('msvc', 'LINKFLAGS', ['<debug-symbols>on/<runtime-debugging>off'], ['/OPT:REF,ICF'])
1087
1088    toolset.flags('msvc', 'LINKFLAGS', ['<user-interface>console'], ['/subsystem:console'])
1089    toolset.flags('msvc', 'LINKFLAGS', ['<user-interface>gui'], ['/subsystem:windows'])
1090    toolset.flags('msvc', 'LINKFLAGS', ['<user-interface>wince'], ['/subsystem:windowsce'])
1091    toolset.flags('msvc', 'LINKFLAGS', ['<user-interface>native'], ['/subsystem:native'])
1092    toolset.flags('msvc', 'LINKFLAGS', ['<user-interface>auto'], ['/subsystem:posix'])
1093
1094    toolset.flags('msvc.link', 'OPTIONS', [], ['<linkflags>'])
1095    toolset.flags('msvc.link', 'LINKPATH', [], ['<library-path>'])
1096
1097    toolset.flags('msvc.link', 'FINDLIBS_ST', [], ['<find-static-library>'])
1098    toolset.flags('msvc.link', 'FINDLIBS_SA', [], ['<find-shared-library>'])
1099    toolset.flags('msvc.link', 'LIBRARY_OPTION', ['<toolset>msvc'], [''])
1100    toolset.flags('msvc.link', 'LIBRARIES_MENTIONED_BY_FILE', [], ['<library-file>'])
1101
1102    toolset.flags('msvc.archive', 'AROPTIONS', [], ['<archiveflags>'])
1103
1104
1105# Locates the requested setup script under the given folder and returns its full
1106# path or nothing in case the script can not be found. In case multiple scripts
1107# are found only the first one is returned.
1108#
1109# TODO: There used to exist a code comment for the msvc.init rule stating that
1110# we do not correctly detect the location of the vcvars32.bat setup script for
1111# the free VC7.1 tools in case user explicitly provides a path. This should be
1112# tested or simply remove this whole comment in case this toolset version is no
1113# longer important.
1114#
1115def locate_default_setup(command, parent, setup_name):
1116    for setup in [os.path.join(dir,setup_name) for dir in [command,parent]]:
1117        if os.path.exists(setup):
1118            return setup
1119    return None
1120
1121
1122# Validates given path, registers found configuration and prints debug
1123# information about it.
1124#
1125def register_configuration(version, path=None):
1126    if path:
1127        command = os.path.join(path, 'cl.exe')
1128        if os.path.exists(command):
1129            if debug():
1130                print "notice: [msvc-cfg] msvc-$(version) detected, command: ''".format(version,command)
1131            __versions.register(version)
1132            __versions.set(version,'options',['<command>{}'.format(command)])
1133
1134
1135################################################################################
1136#
1137#   Startup code executed when loading this module.
1138#
1139################################################################################
1140
1141# Similar to Configurations, but remembers the first registered configuration.
1142class MSVCConfigurations(Configurations):
1143    def __init__(self):
1144        Configurations.__init__(self)
1145        self.first_ = None
1146
1147    def register(self, id):
1148        Configurations.register(self,id)
1149        if not self.first_:
1150            self.first_ = id
1151
1152    def first(self):
1153        return self.first_
1154
1155
1156# List of all registered configurations.
1157__versions = MSVCConfigurations()
1158
1159# Supported CPU architectures.
1160__cpu_arch_i386 = [
1161    '<architecture>/<address-model>',
1162    '<architecture>/<address-model>32',
1163    '<architecture>x86/<address-model>',
1164    '<architecture>x86/<address-model>32']
1165
1166__cpu_arch_amd64 = [
1167    '<architecture>/<address-model>64',
1168    '<architecture>x86/<address-model>64']
1169
1170__cpu_arch_ia64 = [
1171    '<architecture>ia64/<address-model>',
1172    '<architecture>ia64/<address-model>64']
1173
1174
1175# Supported CPU types (only Itanium optimization options are supported from
1176# VC++ 2005 on). See
1177# http://msdn2.microsoft.com/en-us/library/h66s5s0e(vs.90).aspx for more
1178# detailed information.
1179__cpu_type_g5       = ['i586', 'pentium', 'pentium-mmx' ]
1180__cpu_type_g6       = ['i686', 'pentiumpro', 'pentium2', 'pentium3', 'pentium3m', 'pentium-m', 'k6',
1181                      'k6-2', 'k6-3', 'winchip-c6', 'winchip2', 'c3', 'c3-2' ]
1182__cpu_type_em64t    = ['prescott', 'nocona', 'core2', 'corei7', 'corei7-avx', 'core-avx-i', 'conroe', 'conroe-xe', 'conroe-l', 'allendale', 'merom',
1183                      'merom-xe', 'kentsfield', 'kentsfield-xe', 'penryn', 'wolfdale',
1184                      'yorksfield', 'nehalem', 'sandy-bridge', 'ivy-bridge', 'haswell' ]
1185__cpu_type_amd64    = ['k8', 'opteron', 'athlon64', 'athlon-fx', 'k8-sse3', 'opteron-sse3', 'athlon64-sse3', 'amdfam10', 'barcelona',
1186                      'bdver1', 'bdver2', 'bdver3', 'btver1', 'btver2' ]
1187__cpu_type_g7       = ['pentium4', 'pentium4m', 'athlon', 'athlon-tbird', 'athlon-4', 'athlon-xp'
1188                      'athlon-mp'] + __cpu_type_em64t + __cpu_type_amd64
1189__cpu_type_itanium  = ['itanium', 'itanium1', 'merced']
1190__cpu_type_itanium2 = ['itanium2', 'mckinley']
1191
1192
1193# Known toolset versions, in order of preference.
1194_known_versions = ['14.0', '12.0', '11.0', '10.0', '10.0express', '9.0', '9.0express', '8.0', '8.0express', '7.1', '7.1toolkit', '7.0', '6.0']
1195
1196# Version aliases.
1197__version_alias_6 = '6.0'
1198__version_alias_6_5 = '6.0'
1199__version_alias_7 = '7.0'
1200__version_alias_8 = '8.0'
1201__version_alias_9 = '9.0'
1202__version_alias_10 = '10.0'
1203__version_alias_11 = '11.0'
1204__version_alias_12 = '12.0'
1205__version_alias_14 = '14.0'
1206
1207# Names of registry keys containing the Visual C++ installation path (relative
1208# to "HKEY_LOCAL_MACHINE\SOFTWARE\\Microsoft").
1209__version_6_0_reg = "VisualStudio\\6.0\\Setup\\Microsoft Visual C++"
1210__version_7_0_reg = "VisualStudio\\7.0\\Setup\\VC"
1211__version_7_1_reg = "VisualStudio\\7.1\\Setup\\VC"
1212__version_8_0_reg = "VisualStudio\\8.0\\Setup\\VC"
1213__version_8_0express_reg = "VCExpress\\8.0\\Setup\\VC"
1214__version_9_0_reg = "VisualStudio\\9.0\\Setup\\VC"
1215__version_9_0express_reg = "VCExpress\\9.0\\Setup\\VC"
1216__version_10_0_reg = "VisualStudio\\10.0\\Setup\\VC"
1217__version_10_0express_reg = "VCExpress\\10.0\\Setup\\VC"
1218__version_11_0_reg = "VisualStudio\\11.0\\Setup\\VC"
1219__version_12_0_reg = "VisualStudio\\12.0\\Setup\\VC"
1220__version_14_0_reg = "VisualStudio\\14.0\\Setup\\VC"
1221
1222# Visual C++ Toolkit 2003 does not store its installation path in the registry.
1223# The environment variable 'VCToolkitInstallDir' and the default installation
1224# path will be checked instead.
1225__version_7_1toolkit_path = 'Microsoft Visual C++ Toolkit 2003\\bin'
1226__version_7_1toolkit_env  = 'VCToolkitInstallDir'
1227
1228# Path to the folder containing "cl.exe" relative to the value of the
1229# corresponding environment variable.
1230__version_7_1toolkit_envpath = 'bin' ;
1231#
1232#
1233# Auto-detect all the available msvc installations on the system.
1234auto_detect_toolset_versions()
1235
1236# And finally trigger the actual Boost Build toolset registration.
1237register_toolset()
1238