]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/bin/main.rs
Merge commit 'f51aade56f93175dde89177a92e3669ebd8e7592' into clippyup
[rust.git] / src / bootstrap / bin / main.rs
1 //! rustbuild, the Rust build system
2 //!
3 //! This is the entry point for the build system used to compile the `rustc`
4 //! compiler. Lots of documentation can be found in the `README.md` file in the
5 //! parent directory, and otherwise documentation can be found throughout the `build`
6 //! directory in each respective module.
7
8 use std::env;
9
10 use bootstrap::{t, Build, Config, Subcommand, VERSION};
11
12 fn main() {
13     let args = env::args().skip(1).collect::<Vec<_>>();
14     let config = Config::parse(&args);
15
16     let mut build_lock;
17     let _build_lock_guard;
18     if cfg!(any(unix, windows)) {
19         build_lock = fd_lock::RwLock::new(t!(std::fs::File::create(config.out.join("lock"))));
20         _build_lock_guard = match build_lock.try_write() {
21             Ok(lock) => lock,
22             err => {
23                 println!("warning: build directory locked, waiting for lock");
24                 drop(err);
25                 t!(build_lock.write())
26             }
27         };
28     } else {
29         println!("warning: file locking not supported for target, not locking build directory");
30     }
31
32     // check_version warnings are not printed during setup
33     let changelog_suggestion =
34         if matches!(config.cmd, Subcommand::Setup { .. }) { None } else { check_version(&config) };
35
36     // NOTE: Since `./configure` generates a `config.toml`, distro maintainers will see the
37     // changelog warning, not the `x.py setup` message.
38     let suggest_setup = !config.config.exists() && !matches!(config.cmd, Subcommand::Setup { .. });
39     if suggest_setup {
40         println!("warning: you have not made a `config.toml`");
41         println!(
42             "help: consider running `./x.py setup` or copying `config.toml.example` by running \
43             `cp config.toml.example config.toml`"
44         );
45     } else if let Some(suggestion) = &changelog_suggestion {
46         println!("{}", suggestion);
47     }
48
49     let pre_commit = config.src.join(".git").join("hooks").join("pre-commit");
50     Build::new(config).build();
51
52     if suggest_setup {
53         println!("warning: you have not made a `config.toml`");
54         println!(
55             "help: consider running `./x.py setup` or copying `config.toml.example` by running \
56             `cp config.toml.example config.toml`"
57         );
58     } else if let Some(suggestion) = &changelog_suggestion {
59         println!("{}", suggestion);
60     }
61
62     // Give a warning if the pre-commit script is in pre-commit and not pre-push.
63     // HACK: Since the commit script uses hard links, we can't actually tell if it was installed by x.py setup or not.
64     // We could see if it's identical to src/etc/pre-push.sh, but pre-push may have been modified in the meantime.
65     // Instead, look for this comment, which is almost certainly not in any custom hook.
66     if std::fs::read_to_string(pre_commit).map_or(false, |contents| {
67         contents.contains("https://github.com/rust-lang/rust/issues/77620#issuecomment-705144570")
68     }) {
69         println!(
70             "warning: You have the pre-push script installed to .git/hooks/pre-commit. \
71                   Consider moving it to .git/hooks/pre-push instead, which runs less often."
72         );
73     }
74
75     if suggest_setup || changelog_suggestion.is_some() {
76         println!("note: this message was printed twice to make it more likely to be seen");
77     }
78 }
79
80 fn check_version(config: &Config) -> Option<String> {
81     let mut msg = String::new();
82
83     let suggestion = if let Some(seen) = config.changelog_seen {
84         if seen != VERSION {
85             msg.push_str("warning: there have been changes to x.py since you last updated.\n");
86             format!("update `config.toml` to use `changelog-seen = {}` instead", VERSION)
87         } else {
88             return None;
89         }
90     } else {
91         msg.push_str("warning: x.py has made several changes recently you may want to look at\n");
92         format!("add `changelog-seen = {}` at the top of `config.toml`", VERSION)
93     };
94
95     msg.push_str("help: consider looking at the changes in `src/bootstrap/CHANGELOG.md`\n");
96     msg.push_str("note: to silence this warning, ");
97     msg.push_str(&suggestion);
98
99     Some(msg)
100 }