@@ -128,6 +128,13 @@ impl EnvGetter for StdEnvGetter {
128128/// return `Some(cmd)` which represents a command that's ready to execute the
129129/// tool with the appropriate environment variables set.
130130///
131+ /// To find MSVC tools, this function will first attempt to detect if we are
132+ /// running in the context of a developer command prompt, and then use the tools
133+ /// as found in the current `PATH`. If that fails, it will attempt to locate
134+ /// the newest MSVC toolset in the newest installed version of Visual Studio.
135+ /// To limit the search to a specific version of the MSVC toolset, set the
136+ /// VCToolsVersion environment variable to the desired version (e.g. "14.44.35207").
137+ ///
131138/// Note that this function always returns `None` for non-MSVC targets (if a
132139/// full target name was specified).
133140pub fn find ( arch_or_target : & str , tool : & str ) -> Option < Command > {
@@ -787,7 +794,7 @@ mod impl_ {
787794 instance_path : & Path ,
788795 env_getter : & dyn EnvGetter ,
789796 ) -> Option < ( PathBuf , PathBuf , PathBuf , PathBuf , Option < PathBuf > , PathBuf ) > {
790- let version = vs15plus_vc_read_version ( instance_path) ?;
797+ let version = vs15plus_vc_read_version ( instance_path, env_getter ) ?;
791798
792799 let hosts = match host_arch ( ) {
793800 X86 => & [ "X86" ] ,
@@ -843,7 +850,13 @@ mod impl_ {
843850 ) )
844851 }
845852
846- fn vs15plus_vc_read_version ( dir : & Path ) -> Option < String > {
853+ fn vs15plus_vc_read_version ( dir : & Path , env_getter : & dyn EnvGetter ) -> Option < String > {
854+ if let Some ( version) = env_getter. get_env ( "VCToolsVersion" ) {
855+ // Restrict the search to a specific msvc version; if it doesn't exist then
856+ // our caller will fail to find the tool for this instance and move on.
857+ return version. to_str ( ) . map ( ToString :: to_string) ;
858+ }
859+
847860 // Try to open the default version file.
848861 let mut version_path: PathBuf =
849862 dir. join ( r"VC\Auxiliary\Build\Microsoft.VCToolsVersion.default.txt" ) ;
@@ -919,6 +932,11 @@ mod impl_ {
919932 target : TargetArch ,
920933 env_getter : & dyn EnvGetter ,
921934 ) -> Option < Tool > {
935+ if env_getter. get_env ( "VCToolsVersion" ) . is_some ( ) {
936+ // VCToolsVersion is not set/supported for MSVC 14
937+ return None ;
938+ }
939+
922940 let vcdir = get_vc_dir ( "14.0" ) ?;
923941 let sdk_info = get_sdks ( target, env_getter) ?;
924942 let mut tool = get_tool ( tool, & vcdir, target, & sdk_info) ?;
0 commit comments