Skip to content

Commit 3b08f77

Browse files
meteorcloudykchodorow
authored andcommitted
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 bazelbuild#2120 Change-Id: I61e65ace82163acd456bf82f2b108c5fe8d8a8ce PiperOrigin-RevId: 155850886
1 parent f1631c2 commit 3b08f77

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

tools/cpp/CROSSTOOL.tpl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,50 @@ toolchain {
712712
}
713713
}
714714

715+
feature {
716+
name: 'link_crt_library'
717+
flag_set {
718+
action: 'c-compile'
719+
action: 'c++-compile'
720+
flag_group {
721+
# The flag is filled by cc_configure.
722+
# The default option is /MT, set USE_DYNAMIC_CRT=1 to change it to /MD
723+
flag: "%{crt_option}"
724+
}
725+
}
726+
flag_set {
727+
action: 'c++-link-executable'
728+
action: 'c++-link-dynamic-library'
729+
flag_group {
730+
# The flag is filled by cc_configure.
731+
# The default value is libcmt.lib, set USE_DYNAMIC_CRT=1 to change it to msvcrt.lib
732+
flag: "/DEFAULTLIB:%{crt_library}"
733+
}
734+
}
735+
}
736+
737+
feature {
738+
name: 'link_crt_debug_library'
739+
flag_set {
740+
action: 'c-compile'
741+
action: 'c++-compile'
742+
flag_group {
743+
# The flag is filled by cc_configure.
744+
# The default option is /MTd, set USE_DYNAMIC_CRT=1 to change it to /MDd
745+
flag: "%{crt_debug_option}"
746+
}
747+
}
748+
flag_set {
749+
action: 'c++-link-executable'
750+
action: 'c++-link-dynamic-library'
751+
flag_group {
752+
# The flag is filled by cc_configure.
753+
# The default value is libcmtd.lib, set USE_DYNAMIC_CRT=1 to change it to msvcrtd.lib
754+
flag: "/DEFAULTLIB:%{crt_debug_library}"
755+
}
756+
}
757+
}
758+
715759
feature {
716760
name: 'dbg'
717761
flag_set {
@@ -734,6 +778,7 @@ toolchain {
734778
flag: "/INCREMENTAL:NO"
735779
}
736780
}
781+
implies: 'link_crt_debug_library'
737782
implies: 'generate_pdb_file'
738783
}
739784

@@ -755,6 +800,7 @@ toolchain {
755800
flag: "/INCREMENTAL:NO"
756801
}
757802
}
803+
implies: 'link_crt_library'
758804
implies: 'generate_pdb_file'
759805
}
760806

@@ -767,6 +813,7 @@ toolchain {
767813
flag: "/O2"
768814
}
769815
}
816+
implies: 'link_crt_library'
770817
}
771818

772819
compilation_mode_flags {

tools/cpp/cc_configure.bzl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,28 @@ def _is_support_whole_archive(repository_ctx, vc_path):
607607
result = _execute(repository_ctx, [linker], expect_failure = True)
608608
return result.find("/WHOLEARCHIVE") != -1
609609

610+
def _is_using_dynamic_crt(repository_ctx):
611+
"""Returns True if USE_DYNAMIC_CRT is set to 1."""
612+
env = repository_ctx.os.environ
613+
return "USE_DYNAMIC_CRT" in env and env["USE_DYNAMIC_CRT"] == "1"
614+
615+
def _get_crt_option(repository_ctx, debug = False):
616+
"""Get the CRT option, default is /MT and /MTd."""
617+
crt_option = "/MT"
618+
if _is_using_dynamic_crt(repository_ctx):
619+
crt_option = "/MD"
620+
if debug:
621+
crt_option += "d"
622+
return crt_option
623+
624+
def _get_crt_library(repository_ctx, debug = False):
625+
"""Get the CRT library to link, default is libcmt.lib and libcmtd.lib."""
626+
crt_library = "libcmt"
627+
if _is_using_dynamic_crt(repository_ctx):
628+
crt_library = "msvcrt"
629+
if debug:
630+
crt_library += "d"
631+
return crt_library + ".lib"
610632

611633
def _escaped_cuda_compute_capabilities(repository_ctx):
612634
"""Returns a %-escaped list of strings representing cuda compute capabilities."""
@@ -768,6 +790,10 @@ def _impl(repository_ctx):
768790
"%{msvc_env_include}": escaped_include_paths,
769791
"%{msvc_env_lib}": escaped_lib_paths,
770792
"%{content}": _get_escaped_windows_msys_crosstool_content(repository_ctx),
793+
"%{crt_option}": _get_crt_option(repository_ctx),
794+
"%{crt_debug_option}": _get_crt_option(repository_ctx, debug=True),
795+
"%{crt_library}": _get_crt_library(repository_ctx),
796+
"%{crt_debug_library}": _get_crt_library(repository_ctx, debug=True),
771797
"%{opt_content}": "",
772798
"%{dbg_content}": "",
773799
"%{cxx_builtin_include_directory}": "\n".join(escaped_cxx_include_directories),
@@ -841,6 +867,10 @@ def _impl(repository_ctx):
841867
"%{msvc_env_path}": "",
842868
"%{msvc_env_include}": "",
843869
"%{msvc_env_lib}": "",
870+
"%{crt_option}": "",
871+
"%{crt_debug_option}": "",
872+
"%{crt_library}": "",
873+
"%{crt_debug_library}": "",
844874
})
845875

846876
cc_autoconf = repository_rule(
@@ -864,6 +894,7 @@ cc_autoconf = repository_rule(
864894
"CUDA_PATH",
865895
"HOMEBREW_RUBY_PATH",
866896
"NO_WHOLE_ARCHIVE_OPTION",
897+
"USE_DYNAMIC_CRT",
867898
"SYSTEMROOT",
868899
"VS90COMNTOOLS",
869900
"VS100COMNTOOLS",

0 commit comments

Comments
 (0)