aboutsummaryrefslogtreecommitdiff
path: root/src/makefile/input.rs
blob: aa744c87eea47ea4beea1bb320a9cf8ddf70107e (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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
use std::cell::Cell;
use std::collections::HashMap;
use std::error::Error as StdError;
use std::fs::File;
use std::io::{BufRead, BufReader, Cursor, Error as IoError, Lines};
use std::iter::Peekable;
use std::path::Path;

use eyre::{bail, eyre, Context, Result};
use lazy_static::lazy_static;
use regex::Regex;

use crate::args::Args;

use super::command_line::CommandLine;
#[cfg(feature = "full")]
use super::conditional::{Line as ConditionalLine, State as ConditionalState};
use super::inference_rules::InferenceRule;
use super::r#macro::{Set as MacroSet, Source as MacroSource};
use super::target::Target;
use super::token::{tokenize, Token, TokenString};

enum LineType {
    Rule,
    Macro,
    Unknown,
}

impl LineType {
    fn of(line_tokens: &TokenString) -> Self {
        #[cfg(feature = "full")]
        if line_tokens.starts_with("define ") {
            return Self::Macro;
        }
        for token in line_tokens.tokens() {
            if let Token::Text(text) = token {
                let colon_idx = text.find(':');
                #[cfg(not(feature = "full"))]
                let equals_idx = text.find('=');
                #[cfg(feature = "full")]
                let equals_idx = ["=", ":=", "::=", "?=", "+="]
                    .iter()
                    .filter_map(|p| text.find(p))
                    .min();
                match (colon_idx, equals_idx) {
                    (Some(_), None) => {
                        return Self::Rule;
                    }
                    (Some(c), Some(e)) if c < e => {
                        return Self::Rule;
                    }
                    (None, Some(_)) => {
                        return Self::Macro;
                    }
                    (Some(c), Some(e)) if e <= c => {
                        return Self::Macro;
                    }
                    _ => {}
                }
            }
        }
        Self::Unknown
    }
}

fn inference_match<'a>(
    targets: &[&'a str],
    prerequisites: &[String],
) -> Option<regex::Captures<'a>> {
    lazy_static! {
        static ref INFERENCE_RULE: Regex =
            Regex::new(r"^(?P<s2>(\.[^/.]+)?)(?P<s1>\.[^/.]+)$").unwrap();
        static ref SPECIAL_TARGET: Regex = Regex::new(r"^\.[A-Z]+$").unwrap();
    }

    let inference_match = INFERENCE_RULE.captures(targets[0]);
    let special_target_match = SPECIAL_TARGET.captures(targets[0]);

    let inference_rule = targets.len() == 1
        && prerequisites.is_empty()
        && inference_match.is_some()
        && special_target_match.is_none();
    if inference_rule {
        inference_match
    } else {
        None
    }
}

struct LineNumbers<T, E: StdError + Send + Sync + 'static, Inner: Iterator<Item = Result<T, E>>>(
    Inner,
    usize,
);

impl<T, E: StdError + Send + Sync + 'static, Inner: Iterator<Item = Result<T, E>>>
    LineNumbers<T, E, Inner>
{
    fn new(inner: Inner) -> Self {
        Self(inner, 0)
    }
}

impl<T, E: StdError + Send + Sync + 'static, Inner: Iterator<Item = Result<T, E>>> Iterator
    for LineNumbers<T, E, Inner>
{
    type Item = (usize, Result<T>);

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().map(|x| {
            self.1 = self.1.saturating_add(1);
            (
                self.1,
                x.with_context(|| format!("failed to read line {} of makefile", self.1)),
            )
        })
    }
}

trait IteratorExt<T, E: StdError + Send + Sync + 'static>: Iterator<Item = Result<T, E>> {
    fn line_numbered(self) -> LineNumbers<T, E, Self>
    where
        Self: Sized,
    {
        LineNumbers::new(self)
    }
}
impl<T, E: StdError + Send + Sync + 'static, I: Iterator<Item = Result<T, E>>> IteratorExt<T, E>
    for I
{
}

pub struct MakefileReader<'a, R: BufRead> {
    pub inference_rules: Vec<InferenceRule>,
    pub macros: MacroSet<'static, 'static>,
    pub targets: HashMap<String, Target>,
    pub first_non_special_target: Option<String>,
    args: &'a Args,
    lines_iter: Peekable<LineNumbers<String, IoError, Lines<R>>>,
    pending_line: Option<(usize, Result<String>)>,
    #[cfg(feature = "full")]
    conditional_stack: Vec<ConditionalState>,
}

impl<'a> MakefileReader<'a, BufReader<File>> {
    pub fn read_file(args: &'a Args, path: impl AsRef<Path>) -> Result<Self> {
        let file = File::open(path);
        // TODO handle errors
        let file = file.context("couldn't open makefile!")?;
        let file_reader = BufReader::new(file);
        Self::read(args, file_reader)
    }
}

impl<'a, R: BufRead> MakefileReader<'a, R> {
    pub fn read(args: &'a Args, source: R) -> Result<Self> {
        let mut reader = Self {
            inference_rules: Vec::new(),
            macros: MacroSet::new(),
            targets: HashMap::new(),
            first_non_special_target: None,
            args,
            lines_iter: source.lines().line_numbered().peekable(),
            #[cfg(feature = "full")]
            conditional_stack: Vec::new(),
            pending_line: None,
        };
        reader.read_all()?;
        Ok(reader)
    }

    fn read_all(&mut self) -> Result<()> {
        while let Some((line_number, line)) = self.next_line(" ") {
            let line = line?;

            // 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
                for field in fields {
                    self.extend(
                        MakefileReader::read_file(self.args, field)
                            .with_context(|| format!("while including {}", field))?,
                    );
                }
                continue;
            }

            if line.trim().is_empty() {
                // handle blank lines
                continue;
            }
            // unfortunately, rules vs macros can't be determined until after
            // macro tokenizing. so that's suboptimal.

            // TODO errors
            let line_tokens: TokenString = line
                .parse()
                .with_context(|| format!("failed to parse line {}", line_number))?;

            let line_type = LineType::of(&line_tokens);

            // before we actually test it, see if it's only visible after expanding macros
            let (line_tokens, line_type) = if let LineType::Unknown = line_type {
                let line_tokens = TokenString::text(self.expand_macros(&line_tokens)?.trim());
                // and let's eval whatever bullshit needs evaling
                #[cfg(feature = "full")]
                {
                    let eval = self.macros.to_eval.take();
                    for eval in eval {
                        self.extend(
                            MakefileReader::read(self.args, Cursor::new(eval))
                                .context("while evaling")?,
                        );
                    }
                }
                let line_type = LineType::of(&line_tokens);
                (line_tokens, line_type)
            } else {
                (line_tokens, line_type)
            };

            match line_type {
                LineType::Rule => self.read_rule(&line_tokens, line_number)?,
                LineType::Macro => self.read_macro(line_tokens, line_number)?,
                LineType::Unknown => {
                    if !line_tokens.is_empty() {
                        bail!(
                            "error: line {}: unknown line \"{}\"",
                            line_number,
                            line_tokens
                        );
                    }
                }
            }
        }

        Ok(())
    }

    fn next_line(
        &mut self,
        escaped_newline_replacement: &'static str,
    ) -> Option<(usize, Result<String>)> {
        if let Some(x) = self.pending_line.take() {
            return Some(x);
        }
        while let Some((line_number, line)) = self.lines_iter.next() {
            let mut line = match line {
                Ok(x) => x,
                Err(err) => return Some((line_number, Err(err))),
            };

            // handle escaped newlines
            while line.ends_with('\\') {
                line.pop();
                line.push_str(escaped_newline_replacement);
                if let Some((n, x)) = self.lines_iter.next() {
                    let x = match x {
                        Ok(x) => x,
                        Err(err) => return Some((n, Err(err))),
                    };
                    line.push_str(x.trim_start())
                }
            }

            // handle comments
            lazy_static! {
                static ref COMMENT: Regex = Regex::new("#.*$").unwrap();
            }
            let line = COMMENT.replace(&line, "").into_owned();

            #[cfg(feature = "full")]
            {
                let cond_line = ConditionalLine::from(&line, |t| self.expand_macros(t));
                let cond_line = match cond_line {
                    Ok(x) => x,
                    Err(err) => return Some((line_number, Err(err))),
                };
                if let Some(line) = cond_line {
                    let action = line.action(
                        self.conditional_stack.last(),
                        |name| self.macros.is_defined(name),
                        |t| self.expand_macros(t),
                    );
                    let action = match action {
                        Ok(x) => x,
                        Err(err) => return Some((line_number, Err(err))),
                    };
                    action.apply_to(&mut self.conditional_stack);
                    continue;
                }

                // skip lines if we need to
                if self
                    .conditional_stack
                    .last()
                    .map_or(false, ConditionalState::skipping)
                {
                    continue;
                }
            }

            return Some((line_number, Ok(line)));
        }
        None
    }

    fn next_line_if(
        &mut self,
        escaped_newline_replacement: &'static str,
        predicate: impl FnOnce(&(usize, Result<String>)) -> bool,
    ) -> Option<(usize, Result<String>)> {
        let pending_line = self.next_line(escaped_newline_replacement)?;
        if (predicate)(&pending_line) {
            Some(pending_line)
        } else {
            self.pending_line = Some(pending_line);
            None
        }
    }

    fn read_rule(&mut self, line_tokens: &TokenString, line_number: usize) -> Result<()> {
        let (targets, not_targets) = line_tokens
            .split_once(':')
            .ok_or_else(|| eyre!("read_rule couldn't find a ':' on line {}", line_number))?;
        let targets = self.expand_macros(&targets)?;
        let targets = targets.split_whitespace().collect::<Vec<_>>();
        let (prerequisites, mut commands) = match not_targets.split_once(';') {
            Some((prerequisites, command)) => {
                // TODO make sure escaped newlines get retroactively treated correctly here
                (prerequisites, vec![command])
            }
            None => (not_targets, vec![]),
        };
        let prerequisites = self.expand_macros(&prerequisites)?;
        let prerequisites = prerequisites
            .split_whitespace()
            .map(|x| x.into())
            .collect::<Vec<String>>();

        while let Some((_, x)) = self.next_line_if("\\\n", |(_, x)| {
            x.as_ref()
                .ok()
                .map_or(false, |line| line.starts_with('\t') || line.is_empty())
        }) {
            let mut line = x?;
            if !line.is_empty() {
                line.remove(0);
            }
            if line.is_empty() {
                continue;
            }
            commands.push(
                line.parse()
                    .with_context(|| format!("failed to parse line {}", line_number))?,
            );
        }

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

        if targets.is_empty() {
            return Ok(());
        }

        // we don't know yet if it's a target rule or an inference rule
        let inference_match = inference_match(&targets, &prerequisites);

        if let Some(inference_match) = inference_match {
            let new_rule = InferenceRule {
                product: inference_match.name("s1").unwrap().as_str().to_owned(),
                prereq: inference_match.name("s2").unwrap().as_str().to_owned(),
                commands,
            };

            self.inference_rules.retain(|existing_rule| {
                (&existing_rule.prereq, &existing_rule.product)
                    != (&new_rule.prereq, &new_rule.product)
            });
            self.inference_rules.push(new_rule);
        } else {
            for target in targets {
                if self.first_non_special_target.is_none() && !target.starts_with('.') {
                    self.first_non_special_target = Some(target.into());
                }
                match self.targets.get_mut(target) {
                    Some(old_target)
                        if commands.is_empty()
                            && !(target == ".SUFIXES" && prerequisites.is_empty()) =>
                    {
                        let new_prerequisites = prerequisites
                            .iter()
                            .filter(|x| !old_target.prerequisites.contains(x))
                            .cloned()
                            .collect::<Vec<_>>();
                        old_target.prerequisites.extend(new_prerequisites);
                    }
                    _ => {
                        let new_target = Target {
                            name: target.into(),
                            prerequisites: prerequisites.clone(),
                            commands: commands.clone(),
                            already_updated: Cell::new(false),
                        };
                        self.targets.insert(target.into(), new_target);
                    }
                }
            }
        }

        Ok(())
    }

    fn read_macro(&mut self, mut line_tokens: TokenString, line_number: usize) -> Result<()> {
        let (name, mut value) = if cfg!(feature = "full") && line_tokens.starts_with("define ") {
            line_tokens.strip_prefix("define ");
            if line_tokens.ends_with("=") {
                line_tokens.strip_suffix("=");
                line_tokens.trim_end();
            }
            let mut value = TokenString::empty();
            // TODO what should be done with escaped newlines
            while let Some((_, line)) = self.next_line(" ") {
                let line = line?;
                if line == "endef" {
                    break;
                }
                if !value.is_empty() {
                    value.extend(TokenString::text("\n"));
                }
                value.extend(line.parse()?);
            }
            (line_tokens, value)
        } else {
            line_tokens
                .split_once('=')
                .ok_or_else(|| eyre!("read_rule couldn't find a ':' on line {}", line_number))?
        };
        let name = self.expand_macros(&name)?;
        // GNUisms are annoying, but popular
        let mut expand_value = false;
        let mut skip_if_defined = false;
        let mut append = false;

        #[cfg(feature = "full")]
        let name = if let Some(real_name) = name.strip_suffix("::") {
            expand_value = true;
            real_name
        } else if let Some(real_name) = name.strip_suffix(":") {
            expand_value = true;
            real_name
        } else if let Some(real_name) = name.strip_suffix("?") {
            skip_if_defined = true;
            real_name
        } else if let Some(real_name) = name.strip_suffix("+") {
            append = true;
            real_name
        } else {
            &name
        };

        let name = name.trim();
        value.trim_start();

        let value = if expand_value {
            TokenString::text(self.expand_macros(&value)?)
        } else {
            value
        };

        match self.macros.get(name) {
            // We always let command line or MAKEFLAGS macros override macros from the file.
            Some((MacroSource::CommandLineOrMakeflags, _)) => return Ok(()),
            // 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 => return Ok(()),
            Some(_) if skip_if_defined => return Ok(()),
            _ => {}
        }

        let value = match self.macros.pop(name) {
            Some((_, mut old_value)) if append => {
                // TODO eagerly expand if appending to eagerly-expanded macro
                old_value.extend(TokenString::text(" "));
                old_value.extend(value);
                old_value
            }
            _ => value,
        };
        self.macros.set(name.into(), MacroSource::File, value);

        Ok(())
    }

    fn expand_macros(&self, text: &TokenString) -> Result<String> {
        self.macros.expand(text)
    }

    fn extend<R2: BufRead>(&mut self, new: MakefileReader<R2>) {
        self.inference_rules.extend(new.inference_rules);
        self.macros.extend(new.macros);
        self.targets.extend(new.targets);
        if self.first_non_special_target.is_none() {
            self.first_non_special_target = new.first_non_special_target;
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    type R = Result<()>;

    #[cfg(feature = "full")]
    #[test]
    fn basic_conditionals() -> R {
        let file = "
ifeq (1,1)
worked = yes
else ifeq (2,2)
worked = no
else 
worked = perhaps
endif
        ";
        let args = Args::empty();
        let makefile = MakefileReader::read(&args, Cursor::new(file))?;
        assert_eq!(
            makefile.expand_macros(&TokenString::r#macro("worked"))?,
            "yes"
        );
        Ok(())
    }

    #[cfg(feature = "full")]
    #[test]
    fn define_syntax() -> R {
        let file = "
define foo =
bar
baz
endef
        ";
        let args = Args::empty();
        let makefile = MakefileReader::read(&args, Cursor::new(file))?;
        assert_eq!(
            makefile.expand_macros(&TokenString::r#macro("foo"))?,
            "bar\nbaz"
        );
        Ok(())
    }

    #[test]
    #[cfg(feature = "full")]
    fn eval() -> R {
        // This, for the record, is a terrible misfeature.
        // If you need this, you probably shouldn't be using Make.
        // But a lot of people are using this and still use Make anyway, so here we go,
        // I guess.

        let file = "
PROGRAMS    = server client

server_OBJS = server.o server_priv.o server_access.o
server_LIBS = priv protocol

client_OBJS = client.o client_api.o client_mem.o
client_LIBS = protocol

# Everything after this is generic

.PHONY: all
all: $(PROGRAMS)

define PROGRAM_template =
 $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%)
 ALL_OBJS   += $$($(1)_OBJS)
endef

$(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))

$(PROGRAMS):
\t$(LINK.o) $^ $(LDLIBS) -o $@

clean:
\trm -f $(ALL_OBJS) $(PROGRAMS)
        ";

        let args = Args::empty();
        let makefile = MakefileReader::read(&args, Cursor::new(file))?;
        assert!(makefile.targets.contains_key("server"));
        Ok(())
    }
}