Skip to content

Commit

Permalink
Improved support for /MD and /MT in MSVC [bazelbuild#2120]
Browse files Browse the repository at this point in the history
msvc_tools.py now prefers the last /MT / /MD option given; if none,
/MD is preferred.

Additionally, the behaviour of the copt -g has been improved to enforce
the debug version of the user-selected runtime, not necessarily /MTd.

Issue: bazelbuild#2120 

Closes bazelbuild#2141.

--
Reviewed-on: bazelbuild#2141
PiperOrigin-RevId: 141294930
MOS_MIGRATED_REVID=141294930
  • Loading branch information
javidcf authored and laszlocsomor committed Dec 7, 2016
1 parent 227369a commit 09c7221
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion tools/cpp/wrapper/bin/pydir/msvc_cl.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
('-Os', ['/O1']),
('-O2', ['/O2']),
('-g0', []),
('-g', ['/MTd']),
('-g', ['$DEBUG_RT']),
('-fexceptions', ['/U_HAS_EXCEPTIONS', '/D_HAS_EXCEPTIONS=1', '/EHsc']),
('-fomit-frame-pointer', ['/Oy']),
('-fno-rtti', ['/GR-']),
Expand Down
32 changes: 27 additions & 5 deletions tools/cpp/wrapper/bin/pydir/msvc_tools.py.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ class ArgParser(object):
$PATH%d_NO_EXT: Same as $PATH but strips out any file extension.
$TARGET_ARCH : Set self.target_arch to 'x86' or 'x64' for '-m32' and
'-m64', respectively.
$DEBUG_RT : Enforce linkage to debug runtime.
$COMPILE_OUTPUT%d: Sets the output name of a compilation step.
$COMPILATION_MODE: Sets self.compilation_mode from the value of a
'-Xcompilation-mode=' flag.
Expand All @@ -260,6 +261,7 @@ class ArgParser(object):
matched = []
unmatched = []
files = []
enforce_debug_rt = False
while i < len(argv):
num_matched, action, groups = self._MatchOneArg(argv[i:])
arg = argv[i]
Expand Down Expand Up @@ -312,6 +314,10 @@ class ArgParser(object):
self.compilation_mode = mode
continue

if entry == '$DEBUG_RT':
enforce_debug_rt = True
continue

if not groups:
self.options.append(entry)
else:
Expand Down Expand Up @@ -357,11 +363,27 @@ class ArgParser(object):
i += num_matched
self.leftover = unmatched

# Use the proper runtime flag depending on compilation mode. If the
# compilation is happening in debug mode, this flag already exists. If not,
# then we must add it.
if '/MT' not in self.options and '/MTd' not in self.options:
self.AddOpt('/MT')
# Select runtime option
# Find the last runtime option passed
rt = None
rt_idx = -1
for i, opt in enumerate(reversed(self.options)):
if opt in ['/MT', '/MTd', '/MD', '/MDd']:
if opt[-1] == 'd':
enforce_debug_rt = True
rt = opt[:3]
rt_idx = len(self.options) - i - 1
break
rt = rt or '/MD' # Default to dynamic runtime
# Add debug if necessary
if enforce_debug_rt:
rt += 'd'
# Include runtime option
if rt_idx >= 0:
self.options[rt_idx] = rt
else:
self.options.append(rt)

# Add in any parsed files
self.options += files

Expand Down

0 comments on commit 09c7221

Please sign in to comment.