aboutsummaryrefslogtreecommitdiff
path: root/src/makefile/mod.rs
blob: 75873d2916547005a0367b242954e7ec227d099b (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use std::collections::HashMap;
use std::env;
use std::fs::{File, metadata};
use std::io::{BufRead, BufReader};
use std::path::Path;
use std::time::SystemTime;

use lazy_static::lazy_static;
use regex::Regex;

use crate::args::Args;

mod token;

use token::{tokenize, Token, TokenString};

pub enum RuleType {
    Inference,
    Target,
}

#[derive(PartialEq, Eq, Clone)]
pub struct Rule {
    name: String,
    prerequisites: Vec<String>,
    commands: Vec<CommandLine>,
}

impl Rule {
    pub fn r#type(&self) -> RuleType {
        if self.name.contains(".") && !self.name.contains("/") {
            RuleType::Inference
        } else {
            RuleType::Target
        }
    }

    fn execute_commands(&self, file: &Makefile, target: &Target) {
        for command in &self.commands {
            command.execute(file, target);
        }
    }
}

#[derive(PartialEq, Eq, Clone)]
pub struct Target {
    name: String,
    prerequisites: Vec<String>,
    rule: Option<Rule>,
    already_updated: bool,
}

impl Target {
    fn modified_time(&self) -> Option<SystemTime> {
        metadata(&self.name)
            .and_then(|metadata| metadata.modified())
            .ok()
    }

    fn newer_than(&self, other: &Target) -> Option<bool> {
        Some(match (self.modified_time(), other.modified_time()) {
            (Some(self_mtime), Some(other_mtime)) => self_mtime >= other_mtime,
            // per POSIX: "If the target does not exist after the target has been
            // successfully made up-to-date, the target shall be treated as being
            // newer than any target for which it is a prerequisite."
            (None, _) if self.already_updated && other.prerequisites.contains(&self.name) => true,
            (_, None) if other.already_updated && self.prerequisites.contains(&other.name) => false,
            _ => return None,
        })
    }

    fn is_up_to_date(&self, file: &mut Makefile) -> bool {
        if self.already_updated {
            return true;
        }
        let exists = metadata(&self.name).is_ok();
        if exists && self.rule.is_none() {
            return true;
        }
        let newer_than_all_dependencies = self.prerequisites
            .iter()
            .all(|t| self.newer_than(&file.get_target(t)).unwrap_or(false));
        if exists && newer_than_all_dependencies {
            return true;
        }
        false
    }

    fn update(&mut self, file: &mut Makefile) {
        for prereq in &self.prerequisites {
            file.update_target(prereq);
        }
        if !self.is_up_to_date(file) {
            match &self.rule {
                Some(rule) => rule.execute_commands(file, self),
                None => panic!("target doesn't exist & no rule to make it"), // TODO handle this error well
            }
        }
        self.already_updated = true;
    }
}

#[derive(PartialEq, Eq, Clone)]
pub struct CommandLine {
    /// If the command prefix contains a <hyphen-minus>, or the -i option is present, or
    /// the special target .IGNORE has either the current target as a prerequisite or has
    /// no prerequisites, any error found while executing the command shall be ignored.
    ignore_errors: bool,
    /// If the command prefix contains an at-sign and the make utility command line -n
    /// option is not specified, or the -s option is present, or the special target
    /// .SILENT has either the current target as a prerequisite or has no prerequisites,
    /// the command shall not be written to standard output before it is executed.
    silent: bool,
    /// If the command prefix contains a <plus-sign>, this indicates a makefile command
    /// line that shall be executed even if -n, -q, or -t is specified.
    always_execute: bool,
    execution_line: TokenString,
}

impl CommandLine {
    fn from(mut line: TokenString) -> Self {
        let mut ignore_errors = false;
        let mut silent = false;
        let mut always_execute = false;

        if let Token::Text(text) = line.first_token_mut() {
            let mut text_chars = text.chars().peekable();
            loop {
                match text_chars.peek() {
                    Some('-') | Some('@') | Some('+') => match text_chars.next() {
                        Some('-') => ignore_errors = true,
                        Some('@') => silent = true,
                        Some('+') => always_execute = true,
                        _ => unreachable!()
                    },
                    _ => break,
                }
            }
            *text = text_chars.collect();
        }

        CommandLine {
            ignore_errors,
            silent,
            always_execute,
            execution_line: line,
        }
    }

    fn execute(&self, file: &Makefile, target: &Target) {
        let avoid_execution = file.args.dry_run || file.args.question || file.args.touch;
        if avoid_execution && !self.always_execute {
            return;
        }

        let execution_line = file.expand_macros(&self.execution_line);

        let self_silent = self.silent && !file.args.dry_run;
        let special_target_silent = file.rules.get(".SILENT")
            .map_or(false, |silent_target| {
                silent_target.prerequisites.is_empty() || silent_target.prerequisites.contains(&target.name)
            });
        let silent = self_silent || file.args.silent || special_target_silent;
        if !silent {
            println!("{}", execution_line);
        }

        let special_target_ignore = file.rules.get(".IGNORE")
            .map_or(false, |ignore_target| {
                ignore_target.prerequisites.is_empty() || ignore_target.prerequisites.contains(&target.name)
            });
        let ignore_error = self.ignore_errors || file.args.ignore_errors || special_target_ignore;

        // TODO don't fuck this up
        let execution_line = ::std::ffi::CString::new(execution_line.as_bytes())
            .expect("execution line shouldn't have a null in the middle");
        // TODO pass shell "-e" if errors are not ignored
        let return_value = unsafe { libc::system(execution_line.as_ptr()) };
        if return_value != 0 {
            // apparently there was an error. do we care?
            if !ignore_error {
                // TODO handle this error gracefully
                panic!("error from command execution!");
            }
        }
    }
}

enum MacroSource {
    File,
    CommandLineOrMAKEFLAGS,
    Environment,
    Builtin,
}

pub struct Makefile {
    rules: HashMap<String, Rule>,
    macros: HashMap<String, (MacroSource, TokenString)>,
    targets: HashMap<String, Target>,
    args: Args,
}

impl Makefile {
    pub fn new(args: Args) -> Makefile {
        Makefile {
            rules: HashMap::new(),
            macros: HashMap::new(),
            targets: HashMap::new(),
            args,
        }
    }

    pub fn add_builtins(&mut self) -> &mut Makefile {
        self.rules.extend(BUILTIN_RULES.iter().map(|(name, rule)| (name.to_string(), rule.clone())));
        self
    }

    pub fn add_env(&mut self) -> &mut Makefile {
        self.macros.extend(env::vars()
            .filter_map(|(name, value)| {
                if name == "MAKEFLAGS" || name == "SHELL" {
                    None
                } else {
                    Some((name, (MacroSource::Environment, TokenString::from(vec![Token::Text(value)]))))
                }
            })
        );
        self
    }

    pub fn and_read_file(&mut self, path: impl AsRef<Path>) -> &mut Makefile {
        let file = File::open(path);
        // TODO handle errors
        let file = file.expect("couldn't open makefile!");
        let file_reader = BufReader::new(file);
        self.and_read(file_reader)
    }

    pub fn and_read(&mut self, source: impl BufRead) -> &mut Makefile {
        let mut lines_iter = source.lines().peekable();
        while lines_iter.peek().is_some() {
            let line = match lines_iter.next() {
                Some(x) => x,
                // fancy Rust trick: break-with-an-argument to return a value from a
                // `loop` expression
                None => break,
            };
            // TODO handle I/O errors at all
            let mut line = line.expect("failed to read line of makefile!");

            // handle escaped newlines (TODO exception for command lines)
            while line.ends_with(r"\") {
                let next_line = match lines_iter.next() {
                    Some(x) => x,
                    None => Ok("".into()),
                };
                let next_line = next_line.expect("failed to read line of makefile!");
                let next_line = next_line.trim_start();
                line.push(' ');
                line.push_str(next_line);
            }
            // handle comments
            lazy_static! {
                static ref COMMENT: Regex = Regex::new("#.*$").unwrap();
            }
            let line = COMMENT.replace(&line, "").into_owned();

            // handle include lines
            if let Some(line) = line.strip_prefix("include ") {
                // remove extra leading space
                let line = line.trim_start();
                let line = self.expand_macros(&tokenize(line));
                let fields = line.split_whitespace();
                // POSIX says we only have to handle a single filename, but GNU make
                // handles arbitrarily many filenames, and it's not like that's more work
                // TODO have some way of linting for non-portable constructs
                for field in fields {
                    self.and_read_file(field);
                }
            } else if line.trim().is_empty() {
                // handle blank lines
                continue;
            } else {
                // unfortunately, rules vs macros can't be determined until after
                // macro tokenizing. so that's suboptimal.
                // TODO errors
                let line_tokens: TokenString = line.parse().unwrap();

                enum LineType {
                    Rule,
                    Macro,
                    Unknown,
                }

                fn get_line_type(line_tokens: &TokenString) -> LineType {
                    for token in line_tokens.tokens() {
                        if let Token::Text(text) = token {
                            let colon_idx = text.find(":");
                            let equals_idx = text.find("=");
                            match (colon_idx, equals_idx) {
                                (Some(_), None) => {
                                    return LineType::Rule;
                                }
                                (Some(c), Some(e)) if c < e => {
                                    return LineType::Rule;
                                }
                                (None, Some(_)) => {
                                    return LineType::Macro;
                                }
                                (Some(c), Some(e)) if e < c => {
                                    return LineType::Macro;
                                }
                                _ => {}
                            }
                        }
                    }
                    LineType::Unknown
                }

                let line_type = get_line_type(&line_tokens);

                match line_type {
                    LineType::Rule => {
                        let (targets, not_targets) = line_tokens.split_once(':').unwrap();
                        let targets = self.expand_macros(&targets);
                        let targets = targets.split_whitespace().map(|x| x.into()).collect::<Vec<String>>();
                        let (prerequisites, mut commands) = match not_targets.split_once(';') {
                            Some((prerequisites, commands)) => (prerequisites, vec![commands]),
                            None => (not_targets, vec![]),
                        };
                        let prerequisites = self.expand_macros(&prerequisites);
                        let prerequisites = prerequisites.split_whitespace().map(|x| x.into()).collect::<Vec<String>>();

                        while lines_iter.peek().and_then(|x| x.as_ref().ok()).map_or(false, |line| line.starts_with('\t')) {
                            let line = lines_iter.next().unwrap().unwrap();
                            let line = line.strip_prefix("\t").unwrap();
                            commands.push(line.parse().unwrap());
                        }

                        let commands = commands.into_iter()
                            .map(CommandLine::from)
                            .collect::<Vec<_>>();

                        for target in targets {
                            match self.rules.get_mut(&target) {
                                Some(old_rule) if commands.is_empty() => {
                                    old_rule.prerequisites.extend(prerequisites.clone());
                                }
                                _ => {
                                    self.rules.insert(target.clone(), Rule {
                                        name: target,
                                        prerequisites: prerequisites.clone(),
                                        commands: commands.clone(),
                                    });
                                }
                            }
                        }
                    },
                    LineType::Macro => {
                        let (name, value) = line_tokens.split_once('=').unwrap();
                        let name = self.expand_macros(&name);
                        match self.macros.get(&name) {
                            // We always let command line or MAKEFLAGS macros override macros from the file.
                            Some((MacroSource::CommandLineOrMAKEFLAGS, _)) => continue,
                            // We let environment variables override macros from the file only if the command-line argument to do that was given
                            Some((MacroSource::Environment, _)) if self.args.environment_overrides => continue,
                            _ => {}
                        }
                        self.macros.insert(name, (MacroSource::File, value));
                    }
                    LineType::Unknown => {
                        panic!("Unknown line {:?}", line_tokens);
                    }
                }
            }
        }
        self
    }

    fn get_target(&mut self, name: impl Into<String>) -> &mut Target {
        let name = name.into();
        {
            let rules_get_name = self.rules.get(&name);
            let make_target = || {
                if let Some(target_rule) = rules_get_name {
                    return Target {
                        name: name.clone(),
                        prerequisites: target_rule.prerequisites.clone(),
                        already_updated: false,
                        rule: Some(target_rule.clone())
                    };
                }
                panic!("uhhhhh i don't even know anymore bro");
            };
            self.targets
                .entry(name.clone())
                .or_insert_with(make_target);
        }
        self.targets.get_mut(&name).unwrap()
    }

    fn update_target(&mut self, name: impl Into<String>) {
        // This is the dumbest fucking thing I've ever had to do.
        // We can't leave it in the map, because then we have overlapping mutable borrows of self,
        // so we have to remove it from the map, do the work, and then re-insert it into the map.
        // Fuck this so much.
        // Why the goddamn hell do I even write Rust.
        let name = name.into();
        {
            let _ = self.get_target(name.clone());
        }
        let mut target = self.targets.remove(&name).unwrap();
        target.update(self);
        self.targets.insert(name.clone(), target);
    }

    fn expand_macros(&self, text: &TokenString) -> String {
        let mut result = String::new();
        for token in text.tokens() {
            match token {
                Token::Text(t) => result.push_str(t),
                Token::MacroExpansion { name, replacement } => {
                    let (_, macro_value) = &self.macros[name];
                    let macro_value = self.expand_macros(macro_value);
                    let macro_value = match replacement {
                        Some((subst1, subst2)) => {
                            let subst1 = self.expand_macros(subst1);
                            let subst1_suffix = regex::escape(&subst1);
                            let subst1_suffix = Regex::new(&format!(r"{}\b", subst1_suffix)).unwrap();
                            let subst2 = self.expand_macros(subst2);
                            subst1_suffix.replace_all(&macro_value, subst2).to_string()
                        },
                        None => macro_value,
                    };
                    result.push_str(&macro_value);
                }
            }
        }
        return result;
    }
}

const BUILTIN_RULES: &'static [(&'static str, Rule)] = &[];
const BUILTIN_SUFFIX_LIST: &'static [&'static str] = &[];