]> git.lizzy.rs Git - rust.git/blob - src/tools/tidy/src/cargo.rs
use top level `fs` functions where appropriate
[rust.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::fs;
19 use std::path::Path;
20
21 pub fn check(path: &Path, bad: &mut bool) {
22     if !super::filter_dirs(path) {
23         return
24     }
25     for entry in t!(path.read_dir(), path).map(|e| t!(e)) {
26         // Look for `Cargo.toml` with a sibling `src/lib.rs` or `lib.rs`
27         if entry.file_name().to_str() == Some("Cargo.toml") {
28             if path.join("src/lib.rs").is_file() {
29                 verify(&entry.path(), &path.join("src/lib.rs"), bad)
30             }
31             if path.join("lib.rs").is_file() {
32                 verify(&entry.path(), &path.join("lib.rs"), bad)
33             }
34         } else if t!(entry.file_type()).is_dir() {
35             check(&entry.path(), bad);
36         }
37     }
38 }
39
40 // Verify that the dependencies in Cargo.toml at `tomlfile` are sync'd with the
41 // `extern crate` annotations in the lib.rs at `libfile`.
42 fn verify(tomlfile: &Path, libfile: &Path, bad: &mut bool) {
43     let toml = t!(fs::read_to_string(&tomlfile));
44     let librs = t!(fs::read_to_string(&libfile));
45
46     if toml.contains("name = \"bootstrap\"") {
47         return
48     }
49
50     // "Poor man's TOML parser", just assume we use one syntax for now
51     //
52     // We just look for:
53     //
54     //      [dependencies]
55     //      name = ...
56     //      name2 = ...
57     //      name3 = ...
58     //
59     // If we encounter a line starting with `[` then we assume it's the end of
60     // the dependency section and bail out.
61     let deps = match toml.find("[dependencies]") {
62         Some(i) => &toml[i+1..],
63         None => return,
64     };
65     for line in deps.lines() {
66         if line.starts_with('[') {
67             break
68         }
69
70         let mut parts = line.splitn(2, '=');
71         let krate = parts.next().unwrap().trim();
72         if parts.next().is_none() {
73             continue
74         }
75
76         // Don't worry about depending on core/std but not saying `extern crate
77         // core/std`, that's intentional.
78         if krate == "core" || krate == "std" {
79             continue
80         }
81
82         // This is intentional, this dependency just makes the crate available
83         // for others later on. Cover cases
84         let whitelisted = krate.starts_with("panic");
85         if toml.contains("name = \"std\"") && whitelisted {
86             continue
87         }
88
89         if !librs.contains(&format!("extern crate {}", krate)) {
90             tidy_error!(bad, "{} doesn't have `extern crate {}`, but Cargo.toml \
91                               depends on it", libfile.display(), krate);
92         }
93     }
94 }