Skip to content

Commit

Permalink
Adding feature for linking C Run-Time library on Windows
Browse files Browse the repository at this point in the history
By default, we use /MT(/MTd for debug mode) and link to
libcmt.lib(libcmtd.lib).

Users can set USE_DYNAMIC_CRT=1 or add --action_env=USE_DYNAMIC_CRT=1 to
switch to /MD and msvcrt.lib (/MDd and msvcrtd.lib for debug mode)

Reference: https://msdn.microsoft.com/en-us/library/abx4dbyh.aspx

Fixed #2120

Change-Id: I61e65ace82163acd456bf82f2b108c5fe8d8a8ce
PiperOrigin-RevId: 155850886
  • Loading branch information
meteorcloudy authored and kchodorow committed May 12, 2017
1 parent f1631c2 commit 3b08f77
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
47 changes: 47 additions & 0 deletions tools/cpp/CROSSTOOL.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,50 @@ toolchain {
}
}

feature {
name: 'link_crt_library'
flag_set {
action: 'c-compile'
action: 'c++-compile'
flag_group {
# The flag is filled by cc_configure.
# The default option is /MT, set USE_DYNAMIC_CRT=1 to change it to /MD
flag: "%{crt_option}"
}
}
flag_set {
action: 'c++-link-executable'
action: 'c++-link-dynamic-library'
flag_group {
# The flag is filled by cc_configure.
# The default value is libcmt.lib, set USE_DYNAMIC_CRT=1 to change it to msvcrt.lib
flag: "/DEFAULTLIB:%{crt_library}"
}
}
}

feature {
name: 'link_crt_debug_library'
flag_set {
action: 'c-compile'
action: 'c++-compile'
flag_group {
# The flag is filled by cc_configure.
# The default option is /MTd, set USE_DYNAMIC_CRT=1 to change it to /MDd
flag: "%{crt_debug_option}"
}
}
flag_set {
action: 'c++-link-executable'
action: 'c++-link-dynamic-library'
flag_group {
# The flag is filled by cc_configure.
# The default value is libcmtd.lib, set USE_DYNAMIC_CRT=1 to change it to msvcrtd.lib
flag: "/DEFAULTLIB:%{crt_debug_library}"
}
}
}

feature {
name: 'dbg'
flag_set {
Expand All @@ -734,6 +778,7 @@ toolchain {
flag: "/INCREMENTAL:NO"
}
}
implies: 'link_crt_debug_library'
implies: 'generate_pdb_file'
}

Expand All @@ -755,6 +800,7 @@ toolchain {
flag: "/INCREMENTAL:NO"
}
}
implies: 'link_crt_library'
implies: 'generate_pdb_file'
}

Expand All @@ -767,6 +813,7 @@ toolchain {
flag: "/O2"
}
}
implies: 'link_crt_library'
}

compilation_mode_flags {
Expand Down
31 changes: 31 additions & 0 deletions tools/cpp/cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,28 @@ def _is_support_whole_archive(repository_ctx, vc_path):
result = _execute(repository_ctx, [linker], expect_failure = True)
return result.find("/WHOLEARCHIVE") != -1

def _is_using_dynamic_crt(repository_ctx):
"""Returns True if USE_DYNAMIC_CRT is set to 1."""
env = repository_ctx.os.environ
return "USE_DYNAMIC_CRT" in env and env["USE_DYNAMIC_CRT"] == "1"

def _get_crt_option(repository_ctx, debug = False):
"""Get the CRT option, default is /MT and /MTd."""
crt_option = "/MT"
if _is_using_dynamic_crt(repository_ctx):
crt_option = "/MD"
if debug:
crt_option += "d"
return crt_option

def _get_crt_library(repository_ctx, debug = False):
"""Get the CRT library to link, default is libcmt.lib and libcmtd.lib."""
crt_library = "libcmt"
if _is_using_dynamic_crt(repository_ctx):
crt_library = "msvcrt"
if debug:
crt_library += "d"
return crt_library + ".lib"

def _escaped_cuda_compute_capabilities(repository_ctx):
"""Returns a %-escaped list of strings representing cuda compute capabilities."""
Expand Down Expand Up @@ -768,6 +790,10 @@ def _impl(repository_ctx):
"%{msvc_env_include}": escaped_include_paths,
"%{msvc_env_lib}": escaped_lib_paths,
"%{content}": _get_escaped_windows_msys_crosstool_content(repository_ctx),
"%{crt_option}": _get_crt_option(repository_ctx),
"%{crt_debug_option}": _get_crt_option(repository_ctx, debug=True),
"%{crt_library}": _get_crt_library(repository_ctx),
"%{crt_debug_library}": _get_crt_library(repository_ctx, debug=True),
"%{opt_content}": "",
"%{dbg_content}": "",
"%{cxx_builtin_include_directory}": "\n".join(escaped_cxx_include_directories),
Expand Down Expand Up @@ -841,6 +867,10 @@ def _impl(repository_ctx):
"%{msvc_env_path}": "",
"%{msvc_env_include}": "",
"%{msvc_env_lib}": "",
"%{crt_option}": "",
"%{crt_debug_option}": "",
"%{crt_library}": "",
"%{crt_debug_library}": "",
})

cc_autoconf = repository_rule(
Expand All @@ -864,6 +894,7 @@ cc_autoconf = repository_rule(
"CUDA_PATH",
"HOMEBREW_RUBY_PATH",
"NO_WHOLE_ARCHIVE_OPTION",
"USE_DYNAMIC_CRT",
"SYSTEMROOT",
"VS90COMNTOOLS",
"VS100COMNTOOLS",
Expand Down

0 comments on commit 3b08f77

Please sign in to comment.