]> git.lizzy.rs Git - rust.git/commitdiff
rustc: Fix more verbatim paths leaking to gcc
authorAlex Crichton <alex@alexcrichton.com>
Tue, 5 May 2015 22:19:36 +0000 (15:19 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Tue, 5 May 2015 22:21:52 +0000 (15:21 -0700)
Turns out that a verbatim path was leaking through to gcc via the PATH
environment variable (pointing to the bundled gcc provided by the main
distribution) which was wreaking havoc when gcc itself was run. The fix here is
to just stop passing verbatim paths down by adding more liberal uses of
`fix_windows_verbatim_for_gcc`.

Closes #25072

src/librustc/lib.rs
src/librustc/metadata/filesearch.rs
src/librustc/session/mod.rs
src/librustc/util/fs.rs [new file with mode: 0644]
src/librustc_driver/driver.rs
src/librustc_trans/back/link.rs

index 5bd3759a6e04e5386fadf522afb4aa21a5c74077..35abbc77c12b16cf3342401542ac1536fa5ee591 100644 (file)
@@ -147,6 +147,7 @@ pub mod util {
     pub mod nodemap;
     pub mod lev_distance;
     pub mod num;
+    pub mod fs;
 }
 
 pub mod lib {
index 7d8cf5b22a9016c6b9dec3ff8a61b147a6ee6ca5..311ab1cbd0ce02269a5aea17ee01880dab254cb5 100644 (file)
@@ -19,6 +19,7 @@
 use std::path::{Path, PathBuf};
 
 use session::search_paths::{SearchPaths, PathKind};
+use util::fs as rustcfs;
 
 #[derive(Copy, Clone)]
 pub enum FileMatch {
@@ -191,7 +192,10 @@ pub fn get_or_default_sysroot() -> PathBuf {
     fn canonicalize(path: Option<PathBuf>) -> Option<PathBuf> {
         path.and_then(|path| {
             match fs::canonicalize(&path) {
-                Ok(canon) => Some(canon),
+                // See comments on this target function, but the gist is that
+                // gcc chokes on verbatim paths which fs::canonicalize generates
+                // so we try to avoid those kinds of paths.
+                Ok(canon) => Some(rustcfs::fix_windows_verbatim_for_gcc(&canon)),
                 Err(e) => panic!("failed to get realpath: {}", e),
             }
         })
index 14bc19dffd5d0b6f307856c23ccdf8814b74fdf9..7a8ce1bf48e47d283e7e285315987658c5073d4e 100644 (file)
@@ -46,8 +46,9 @@ pub struct Session {
     pub entry_type: Cell<Option<config::EntryFnType>>,
     pub plugin_registrar_fn: Cell<Option<ast::NodeId>>,
     pub default_sysroot: Option<PathBuf>,
-    // The name of the root source file of the crate, in the local file system. The path is always
-    // expected to be absolute. `None` means that there is no source file.
+    // The name of the root source file of the crate, in the local file system.
+    // The path is always expected to be absolute. `None` means that there is no
+    // source file.
     pub local_crate_source_file: Option<PathBuf>,
     pub working_dir: PathBuf,
     pub lint_store: RefCell<lint::LintStore>,
diff --git a/src/librustc/util/fs.rs b/src/librustc/util/fs.rs
new file mode 100644 (file)
index 0000000..3ae78fa
--- /dev/null
@@ -0,0 +1,38 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use std::path::{self, Path, PathBuf};
+use std::ffi::OsString;
+
+// Unfortunately, on windows, gcc cannot accept paths of the form `\\?\C:\...`
+// (a verbatim path). This form of path is generally pretty rare, but the
+// implementation of `fs::canonicalize` currently generates paths of this form,
+// meaning that we're going to be passing quite a few of these down to gcc.
+//
+// For now we just strip the "verbatim prefix" of `\\?\` from the path. This
+// will probably lose information in some cases, but there's not a whole lot
+// more we can do with a buggy gcc...
+pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
+    if !cfg!(windows) {
+        return p.to_path_buf()
+    }
+    let mut components = p.components();
+    let prefix = match components.next() {
+        Some(path::Component::Prefix(p)) => p,
+        _ => return p.to_path_buf(),
+    };
+    let disk = match prefix.kind() {
+        path::Prefix::VerbatimDisk(disk) => disk,
+        _ => return p.to_path_buf(),
+    };
+    let mut base = OsString::from(format!("{}:", disk as char));
+    base.push(components.as_path());
+    PathBuf::from(base)
+}
index 154e0a1f64460482db3c933417945bf3452009a1..45d81ff0f65297f9e7811c3528a9bd0b89345df5 100644 (file)
@@ -479,7 +479,8 @@ pub fn phase_2_configure_and_expand(sess: &Session,
             let mut _old_path = OsString::new();
             if cfg!(windows) {
                 _old_path = env::var_os("PATH").unwrap_or(_old_path);
-                let mut new_path = sess.host_filesearch(PathKind::All).get_dylib_search_paths();
+                let mut new_path = sess.host_filesearch(PathKind::All)
+                                       .get_dylib_search_paths();
                 new_path.extend(env::split_paths(&_old_path));
                 env::set_var("PATH", &env::join_paths(new_path.iter()).unwrap());
             }
index 92c9549b37727adbd9263cc95d52120776ef3efd..8830cd13052a5be8b6d737c6d87e9139ddaa67e3 100644 (file)
 use util::common::time;
 use util::ppaux;
 use util::sha2::{Digest, Sha256};
+use util::fs::fix_windows_verbatim_for_gcc;
 use rustc_back::tempdir::TempDir;
 
 use std::ffi::OsString;
 use std::fs::{self, PathExt};
 use std::io::{self, Read, Write};
 use std::mem;
-use std::path::{self, Path, PathBuf};
+use std::path::{Path, PathBuf};
 use std::process::Command;
 use std::str;
 use flate;
@@ -1333,29 +1334,3 @@ fn add_upstream_native_libraries(cmd: &mut Command, sess: &Session) {
         }
     }
 }
-
-// Unfortunately, on windows, gcc cannot accept paths of the form `\\?\C:\...`
-// (a verbatim path). This form of path is generally pretty rare, but the
-// implementation of `fs::canonicalize` currently generates paths of this form,
-// meaning that we're going to be passing quite a few of these down to gcc.
-//
-// For now we just strip the "verbatim prefix" of `\\?\` from the path. This
-// will probably lose information in some cases, but there's not a whole lot
-// more we can do with a buggy gcc...
-fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
-    if !cfg!(windows) {
-        return p.to_path_buf()
-    }
-    let mut components = p.components();
-    let prefix = match components.next() {
-        Some(path::Component::Prefix(p)) => p,
-        _ => return p.to_path_buf(),
-    };
-    let disk = match prefix.kind() {
-        path::Prefix::VerbatimDisk(disk) => disk,
-        _ => return p.to_path_buf(),
-    };
-    let mut base = OsString::from(format!("{}:", disk as char));
-    base.push(components.as_path());
-    PathBuf::from(base)
-}