diff --git a/tools/cpp/CROSSTOOL.tpl b/tools/cpp/CROSSTOOL.tpl index f4f0f36834c96b..64234625d9b246 100644 --- a/tools/cpp/CROSSTOOL.tpl +++ b/tools/cpp/CROSSTOOL.tpl @@ -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 { @@ -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 4f93195d26b999..e4427cd77aa5c1 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",