@@ -388,7 +388,7 @@ impl Config {
388
388
/// Gets the path to the `rustdoc` executable.
389
389
pub fn rustdoc ( & self ) -> CargoResult < & Path > {
390
390
self . rustdoc
391
- . try_borrow_with ( || Ok ( self . get_tool ( "rustdoc" , & self . build_config ( ) ?. rustdoc ) ) )
391
+ . try_borrow_with ( || Ok ( self . get_tool ( Tool :: Rustdoc , & self . build_config ( ) ?. rustdoc ) ) )
392
392
. map ( AsRef :: as_ref)
393
393
}
394
394
@@ -406,7 +406,7 @@ impl Config {
406
406
) ;
407
407
408
408
Rustc :: new (
409
- self . get_tool ( "rustc" , & self . build_config ( ) ?. rustc ) ,
409
+ self . get_tool ( Tool :: Rustc , & self . build_config ( ) ?. rustc ) ,
410
410
wrapper,
411
411
rustc_workspace_wrapper,
412
412
& self
@@ -1640,15 +1640,19 @@ impl Config {
1640
1640
}
1641
1641
}
1642
1642
1643
- /// Looks for a path for `tool` in an environment variable or config path, defaulting to `tool`
1644
- /// as a path.
1645
- fn get_tool ( & self , tool : & str , from_config : & Option < ConfigRelativePath > ) -> PathBuf {
1646
- // This function is designed to only work with rustup proxies. This
1647
- // assert is to ensure that if it is ever used for something else in
1648
- // the future that you must ensure that it is a proxy-able tool, or if
1649
- // not then you need to use `maybe_get_tool` instead.
1650
- assert ! ( matches!( tool, "rustc" | "rustdoc" ) ) ;
1651
- self . maybe_get_tool ( tool, from_config)
1643
+ /// Returns the path for the given tool.
1644
+ ///
1645
+ /// This will look for the tool in the following order:
1646
+ ///
1647
+ /// 1. From an environment variable matching the tool name (such as `RUSTC`).
1648
+ /// 2. From the given config value (which is usually something like `build.rustc`).
1649
+ /// 3. Finds the tool in the PATH environment variable.
1650
+ ///
1651
+ /// This is intended for tools that are rustup proxies. If you need to get
1652
+ /// a tool that is not a rustup proxy, use `maybe_get_tool` instead.
1653
+ fn get_tool ( & self , tool : Tool , from_config : & Option < ConfigRelativePath > ) -> PathBuf {
1654
+ let tool_str = tool. as_str ( ) ;
1655
+ self . maybe_get_tool ( tool_str, from_config)
1652
1656
. or_else ( || {
1653
1657
// This is an optimization to circumvent the rustup proxies
1654
1658
// which can have a significant performance hit. The goal here
@@ -1671,7 +1675,7 @@ impl Config {
1671
1675
}
1672
1676
// If the tool on PATH is the same as `rustup` on path, then
1673
1677
// there is pretty good evidence that it will be a proxy.
1674
- let tool_resolved = paths:: resolve_executable ( Path :: new ( tool ) ) . ok ( ) ?;
1678
+ let tool_resolved = paths:: resolve_executable ( Path :: new ( tool_str ) ) . ok ( ) ?;
1675
1679
let rustup_resolved = paths:: resolve_executable ( Path :: new ( "rustup" ) ) . ok ( ) ?;
1676
1680
let tool_meta = tool_resolved. metadata ( ) . ok ( ) ?;
1677
1681
let rustup_meta = rustup_resolved. metadata ( ) . ok ( ) ?;
@@ -1683,7 +1687,7 @@ impl Config {
1683
1687
return None ;
1684
1688
}
1685
1689
// Try to find the tool in rustup's toolchain directory.
1686
- let tool_exe = Path :: new ( tool ) . with_extension ( env:: consts:: EXE_EXTENSION ) ;
1690
+ let tool_exe = Path :: new ( tool_str ) . with_extension ( env:: consts:: EXE_EXTENSION ) ;
1687
1691
let toolchain_exe = home:: rustup_home ( )
1688
1692
. ok ( ) ?
1689
1693
. join ( "toolchains" )
@@ -1696,7 +1700,7 @@ impl Config {
1696
1700
None
1697
1701
}
1698
1702
} )
1699
- . unwrap_or_else ( || PathBuf :: from ( tool ) )
1703
+ . unwrap_or_else ( || PathBuf :: from ( tool_str ) )
1700
1704
}
1701
1705
1702
1706
pub fn jobserver_from_env ( & self ) -> Option < & jobserver:: Client > {
@@ -2697,3 +2701,17 @@ macro_rules! drop_eprint {
2697
2701
$crate:: __shell_print!( $config, err, false , $( $arg) * )
2698
2702
) ;
2699
2703
}
2704
+
2705
+ enum Tool {
2706
+ Rustc ,
2707
+ Rustdoc ,
2708
+ }
2709
+
2710
+ impl Tool {
2711
+ fn as_str ( & self ) -> & str {
2712
+ match self {
2713
+ Tool :: Rustc => "rustc" ,
2714
+ Tool :: Rustdoc => "rustdoc" ,
2715
+ }
2716
+ }
2717
+ }
0 commit comments