aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 191f4fe26b287d44635c7c9423e5e10f417a9128 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#![warn(
    unsafe_code,
    unused_crate_dependencies,
    variant_size_differences,
    clippy::pedantic,
    clippy::cargo,
    clippy::nursery,
    clippy::missing_docs_in_private_items,
    clippy::str_to_string,
    clippy::pattern_type_mismatch,
    clippy::unwrap_used,
    clippy::indexing_slicing,
    clippy::panic_in_result_fn,
    clippy::integer_arithmetic,
    clippy::panic,
    clippy::unwrap_in_result,
    clippy::clone_on_ref_ptr,
    clippy::unreachable,
    clippy::todo
)]
#![allow(clippy::non_ascii_literal)]

use std::env::current_dir;
use std::fs::metadata;
use std::io::stdin;
use std::path::PathBuf;

use anyhow::bail;

mod args;
mod makefile;

use args::Args;
use makefile::Makefile;

fn main() -> anyhow::Result<()> {
    let mut args = Args::from_env_and_args();
    // If no makefile is specified, try some options.
    if args.makefile.is_empty() {
        args.makefile = vec![if metadata("./makefile").is_ok() {
            "./makefile".into()
        } else if metadata("./Makefile").is_ok() {
            "./Makefile".into()
        } else {
            bail!(
                "no makefile specified and neither {} nor {} was found.",
                current_dir().map_or_else(
                    |_| "./makefile".to_owned(),
                    |path| path.join("makefile").display().to_string()
                ),
                current_dir().map_or_else(
                    |_| "./Makefile".to_owned(),
                    |path| path.join("Makefile").display().to_string()
                ),
            );
        }];
    }
    // Read in the makefile(s) specified.
    // TODO dump command-line args into MAKEFLAGS
    // TODO dump command-line macros into environment
    // TODO add SHELL macro
    let mut makefile = Makefile::new(&args);
    for filename in &args.makefile {
        if filename == &PathBuf::from("-") {
            makefile.and_read(stdin().lock())?;
        } else {
            makefile.and_read_file(filename)?;
        };
    }

    if args.print_everything {
        println!("{}", &makefile);
    }

    let targets = if args.targets().count() == 0 {
        let first_target = makefile.first_non_special_target.clone();
        match first_target {
            Some(x) => vec![x],
            None => bail!("no targets given on command line or found in makefile."),
        }
    } else {
        args.targets().cloned().collect()
    };

    for target in targets {
        makefile.update_target(&target)?;
    }

    Ok(())
}