Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cargo run on Windows #95254

Merged
merged 1 commit into from
Apr 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/bootstrap/build.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
use env::consts::{EXE_EXTENSION, EXE_SUFFIX};
use std::env;
use std::ffi::OsString;
use std::path::PathBuf;

/// Given an executable called `name`, return the filename for the
/// executable for a particular target.
pub fn exe(name: &PathBuf) -> PathBuf {
if EXE_EXTENSION != "" && name.extension() != Some(EXE_EXTENSION.as_ref()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this method is basically name.with_extension(EXE_EXTENSION)? Unless I'm missing something, that's the intent of the code...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difference is this doesn't replace any existing extension. with_extension will change my.file to my.exe.

let mut name: OsString = name.clone().into();
name.push(EXE_SUFFIX);
name.into()
} else {
name.clone()
}
}

fn main() {
let host = env::var("HOST").unwrap();
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-env-changed=RUSTC");
println!("cargo:rustc-env=BUILD_TRIPLE={}", env::var("HOST").unwrap());
println!("cargo:rustc-env=BUILD_TRIPLE={}", host);

// This may not be a canonicalized path.
let mut rustc = PathBuf::from(env::var_os("RUSTC").unwrap());

if rustc.is_relative() {
println!("cargo:rerun-if-env-changed=PATH");
for dir in env::split_paths(&env::var_os("PATH").unwrap_or_default()) {
let absolute = dir.join(&rustc);
let absolute = dir.join(&exe(&rustc));
if absolute.exists() {
rustc = absolute;
break;
Expand Down