]> git.proxmox.com Git - rustc.git/blob - src/librustc/util/fs.rs
3ae78fa7c19048157619e40d683735edc823ce42
[rustc.git] / src / librustc / util / fs.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::path::{self, Path, PathBuf};
12 use std::ffi::OsString;
13
14 // Unfortunately, on windows, gcc cannot accept paths of the form `\\?\C:\...`
15 // (a verbatim path). This form of path is generally pretty rare, but the
16 // implementation of `fs::canonicalize` currently generates paths of this form,
17 // meaning that we're going to be passing quite a few of these down to gcc.
18 //
19 // For now we just strip the "verbatim prefix" of `\\?\` from the path. This
20 // will probably lose information in some cases, but there's not a whole lot
21 // more we can do with a buggy gcc...
22 pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
23 if !cfg!(windows) {
24 return p.to_path_buf()
25 }
26 let mut components = p.components();
27 let prefix = match components.next() {
28 Some(path::Component::Prefix(p)) => p,
29 _ => return p.to_path_buf(),
30 };
31 let disk = match prefix.kind() {
32 path::Prefix::VerbatimDisk(disk) => disk,
33 _ => return p.to_path_buf(),
34 };
35 let mut base = OsString::from(format!("{}:", disk as char));
36 base.push(components.as_path());
37 PathBuf::from(base)
38 }