aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Yun Peng <pcloudy@google.com>2017-05-04 06:17:42 -0400
committerGravatar Kristina Chodorow <kchodorow@google.com>2017-05-12 11:14:00 -0400
commit3b08f774e7938928e3a240a47a0a7554cdc8d50b (patch)
treeff0dd288b3d5ab727a02f077a11e1cb838fce1a8
parentf1631c2c85b6abc3f5b65156a52116e76a920e3f (diff)
Adding feature for linking C Run-Time library on Windows
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 https://github.com/bazelbuild/bazel/issues/2120 Change-Id: I61e65ace82163acd456bf82f2b108c5fe8d8a8ce PiperOrigin-RevId: 155850886
-rw-r--r--tools/cpp/CROSSTOOL.tpl47
-rw-r--r--tools/cpp/cc_configure.bzl31
2 files changed, 78 insertions, 0 deletions
diff --git a/tools/cpp/CROSSTOOL.tpl b/tools/cpp/CROSSTOOL.tpl
index f4f0f36834..64234625d9 100644
--- a/tools/cpp/CROSSTOOL.tpl
+++ b/tools/cpp/CROSSTOOL.tpl
@@ -713,6 +713,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 {
action: 'c-compile'
@@ -734,6 +778,7 @@ toolchain {
flag: "/INCREMENTAL:NO"
}
}
+ implies: 'link_crt_debug_library'
implies: 'generate_pdb_file'
}
@@ -755,6 +800,7 @@ toolchain {
flag: "/INCREMENTAL:NO"
}
}
+ implies: 'link_crt_library'
implies: 'generate_pdb_file'
}
@@ -767,6 +813,7 @@ toolchain {
flag: "/O2"
}
}
+ implies: 'link_crt_library'
}
compilation_mode_flags {
diff --git a/tools/cpp/cc_configure.bzl b/tools/cpp/cc_configure.bzl
index 4f93195d26..e4427cd77a 100644
--- a/tools/cpp/cc_configure.bzl
+++ b/tools/cpp/cc_configure.bzl
@@ -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."""
@@ -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),
@@ -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(
@@ -864,6 +894,7 @@ cc_autoconf = repository_rule(
"CUDA_PATH",
"HOMEBREW_RUBY_PATH",
"NO_WHOLE_ARCHIVE_OPTION",
+ "USE_DYNAMIC_CRT",
"SYSTEMROOT",
"VS90COMNTOOLS",
"VS100COMNTOOLS",