]> git.proxmox.com Git - rustc.git/blob - src/tools/tidy/src/cargo.rs
New upstream version 1.15.0+dfsg1
[rustc.git] / src / tools / tidy / src / cargo.rs
1 // Copyright 2016 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 //! Tidy check to ensure that `[dependencies]` and `extern crate` are in sync.
12 //!
13 //! This tidy check ensures that all crates listed in the `[dependencies]`
14 //! section of a `Cargo.toml` are present in the corresponding `lib.rs` as
15 //! `extern crate` declarations. This should help us keep the DAG correctly
16 //! structured through various refactorings to prune out unnecessary edges.
17
18 use std::io::prelude::*;
19 use std::fs::File;
20 use std::path::Path;
21
22 pub fn check(path: &Path, bad: &mut bool) {
23 if path.ends_with("vendor") {
24 return
25 }
26 for entry in t!(path.read_dir(), path).map(|e| t!(e)) {
27 // Look for `Cargo.toml` with a sibling `src/lib.rs` or `lib.rs`
28 if entry.file_name().to_str() == Some("Cargo.toml") {
29 if path.join("src/lib.rs").is_file() {
30 verify(&entry.path(), &path.join("src/lib.rs"), bad)
31 }
32 if path.join("lib.rs").is_file() {
33 verify(&entry.path(), &path.join("lib.rs"), bad)
34 }
35 } else if t!(entry.file_type()).is_dir() {
36 check(&entry.path(), bad);
37 }
38 }
39 }
40
41 // Verify that the dependencies in Cargo.toml at `tomlfile` are sync'd with the
42 // `extern crate` annotations in the lib.rs at `libfile`.
43 fn verify(tomlfile: &Path, libfile: &Path, bad: &mut bool) {
44 let mut toml = String::new();
45 let mut librs = String::new();
46 t!(t!(File::open(tomlfile)).read_to_string(&mut toml));
47 t!(t!(File::open(libfile)).read_to_string(&mut librs));
48
49 if toml.contains("name = \"bootstrap\"") {
50 return
51 }
52
53 // "Poor man's TOML parser", just assume we use one syntax for now
54 //
55 // We just look for:
56 //
57 // [dependencies]
58 // name = ...
59 // name2 = ...
60 // name3 = ...
61 //
62 // If we encounter a line starting with `[` then we assume it's the end of
63 // the dependency section and bail out.
64 let deps = match toml.find("[dependencies]") {
65 Some(i) => &toml[i+1..],
66 None => return,
67 };
68 let mut lines = deps.lines().peekable();
69 while let Some(line) = lines.next() {
70 if line.starts_with("[") {
71 break
72 }
73
74 let mut parts = line.splitn(2, '=');
75 let krate = parts.next().unwrap().trim();
76 if parts.next().is_none() {
77 continue
78 }
79
80 // Don't worry about depending on core/std but not saying `extern crate
81 // core/std`, that's intentional.
82 if krate == "core" || krate == "std" {
83 continue
84 }
85
86 // This is intentional, this dependency just makes the crate available
87 // for others later on. Cover cases
88 let whitelisted = krate == "alloc_jemalloc";
89 let whitelisted = whitelisted || krate.starts_with("panic");
90 if toml.contains("name = \"std\"") && whitelisted {
91 continue
92 }
93
94 // We want the compiler to depend on the proc_macro_plugin crate so
95 // that it is built and included in the end, but we don't want to
96 // actually use it in the compiler.
97 if toml.contains("name = \"rustc_driver\"") &&
98 krate == "proc_macro_plugin" {
99 continue
100 }
101
102 if !librs.contains(&format!("extern crate {}", krate)) {
103 println!("{} doesn't have `extern crate {}`, but Cargo.toml \
104 depends on it", libfile.display(), krate);
105 *bad = true;
106 }
107 }
108 }