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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
|
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::error::Error as StdError;
use std::fs::File;
use std::io::{BufRead, BufReader, Error as IoError, ErrorKind as IoErrorKind, Lines};
use std::iter::Peekable;
use std::path::Path;
use std::rc::Rc;
use eyre::{bail, eyre, Context, Result};
use lazy_static::lazy_static;
use regex::Regex;
use crate::args::Args;
#[cfg(feature = "full")]
use super::conditional::{Line as ConditionalLine, State as ConditionalState};
#[cfg(feature = "full")]
use super::eval_context::DeferredEvalContext;
use super::parse::{MacroAssignment, MacroAssignmentOutcome};
#[cfg(feature = "full")]
use super::r#macro::ExportConfig;
use super::r#macro::Macro;
use super::target::StaticTargetSet;
use super::{
builtin_targets, CommandLine, InferenceRule, InferenceRuleSet, ItemSource, LookupInternal,
MacroScopeStack, MacroSet, Target, TokenString,
};
enum LineType {
Rule,
#[cfg(feature = "full")]
RuleMacro,
Macro,
Include,
#[cfg(feature = "full")]
Export,
#[cfg(feature = "full")]
Unexport,
Unknown,
}
impl LineType {
fn of(line_tokens: &TokenString) -> Self {
#[cfg(feature = "full")]
if line_tokens.starts_with("define ") {
return Self::Macro;
}
if line_tokens.starts_with("include ") || line_tokens.starts_with("-include ") {
return Self::Include;
}
#[cfg(feature = "full")]
if line_tokens.starts_with("export ") || line_tokens == "export" {
return Self::Export;
}
#[cfg(feature = "full")]
if line_tokens.starts_with("unexport ") || line_tokens == "unexport" {
return Self::Unexport;
}
let colon_idx = line_tokens.find(":");
#[cfg(not(feature = "full"))]
let equals_idx = line_tokens.find("=");
#[cfg(feature = "full")]
let equals_idx = ["=", ":=", "::=", "?=", "+="]
.iter()
.filter_map(|p| line_tokens.find(p))
.min();
match (colon_idx, equals_idx) {
(Some(_), None) => {
return Self::Rule;
}
(Some(c), Some(e)) if c < e => {
#[cfg(feature = "full")]
return Self::RuleMacro;
#[cfg(not(feature = "full"))]
return Self::Rule;
}
(None, Some(_)) => {
return Self::Macro;
}
(Some(c), Some(e)) if e <= c => {
return Self::Macro;
}
_ => {}
}
Self::Unknown
}
}
#[derive(Debug)]
struct InferenceMatch<'a> {
s1: &'a str,
s2: &'a str,
}
fn inference_match<'a>(
targets: &[&'a str],
prerequisites: &[String],
) -> Option<InferenceMatch<'a>> {
lazy_static! {
static ref INFERENCE_RULE: Regex = #[allow(clippy::unwrap_used)]
Regex::new(r"^(?P<s2>(\.[^/.]+)?)(?P<s1>\.[^/.]+)$")
.unwrap();
static ref SPECIAL_TARGET: Regex = #[allow(clippy::unwrap_used)]
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 {
#[allow(clippy::unwrap_used)]
inference_match.map(|x| InferenceMatch {
s1: x.name("s1").unwrap().as_str(),
s2: x.name("s2").unwrap().as_str(),
})
} else {
None
}
}
struct LineNumbers<T, E, Inner>(Inner, usize)
where
E: StdError + Send + Sync + 'static,
Inner: Iterator<Item = Result<T, E>>;
impl<T, E, Inner> LineNumbers<T, E, Inner>
where
E: StdError + Send + Sync + 'static,
Inner: Iterator<Item = Result<T, E>>,
{
const fn new(inner: Inner) -> Self {
Self(inner, 0)
}
}
impl<T, E, Inner> Iterator for LineNumbers<T, E, Inner>
where
E: StdError + Send + Sync + 'static,
Inner: Iterator<Item = Result<T, E>>,
{
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
{
}
#[derive(Clone, Copy)]
struct NextLineSettings {
escaped_newline_replacement: &'static str,
peeking: bool,
strip_comments: bool,
}
impl Default for NextLineSettings {
fn default() -> Self {
Self {
escaped_newline_replacement: " ",
peeking: false,
strip_comments: true,
}
}
}
pub struct MakefileReader<'a, 'parent, R: BufRead> {
file_name: String,
pub inference_rules: InferenceRuleSet,
pub stack: MacroScopeStack<'parent>,
pub macros: MacroSet,
pub targets: StaticTargetSet,
built_in_targets: HashMap<String, Target>,
pub first_non_special_target: Option<String>,
pub failed_includes: Vec<String>,
pub args: &'a Args,
lines_iter: Peekable<LineNumbers<String, IoError, Lines<R>>>,
// join with escaped_newline_replacement to get the actual line
pending_line: Option<(usize, Vec<String>)>,
#[cfg(feature = "full")]
conditional_stack: Vec<ConditionalState>,
pub file_names: Rc<RefCell<Vec<String>>>,
}
impl<'a, 'parent> MakefileReader<'a, 'parent, BufReader<File>> {
pub fn read_file(
args: &'a Args,
stack: MacroScopeStack<'parent>,
path: impl AsRef<Path>,
file_names: Rc<RefCell<Vec<String>>>,
) -> Result<Self> {
let mut macros = MacroSet::new();
#[cfg(feature = "full")]
if let Some(old_makefile_list) = stack.get("MAKEFILE_LIST") {
let mut old_makefile_list = old_makefile_list.into_owned();
old_makefile_list.text.extend(TokenString::text(format!(
" {}",
path.as_ref().to_string_lossy()
)));
macros.set("MAKEFILE_LIST".to_owned(), old_makefile_list);
} else {
macros.set(
"MAKEFILE_LIST".to_owned(),
Macro {
source: ItemSource::Builtin,
text: TokenString::text(path.as_ref().to_string_lossy()),
#[cfg(feature = "full")]
eagerly_expanded: false,
},
);
}
let file_name = path.as_ref().to_string_lossy();
file_names.borrow_mut().push(file_name.to_string());
let file = File::open(path.as_ref());
// TODO handle errors
let file = file.context("couldn't open makefile!")?;
let file_reader = BufReader::new(file);
Self::read(args, stack, macros, file_reader, file_name, file_names)
}
}
impl<'a, 'parent, R: BufRead> MakefileReader<'a, 'parent, R> {
pub fn read(
args: &'a Args,
stack: MacroScopeStack<'parent>,
macros: MacroSet,
source: R,
name: impl Into<String>,
file_names: Rc<RefCell<Vec<String>>>,
) -> Result<Self> {
let name = name.into();
let mut reader = Self {
file_name: name.clone(),
inference_rules: InferenceRuleSet::default(),
stack,
macros,
targets: Default::default(),
built_in_targets: HashMap::new(),
first_non_special_target: None,
failed_includes: Vec::new(),
args,
lines_iter: source.lines().line_numbered().peekable(),
pending_line: None,
#[cfg(feature = "full")]
conditional_stack: Vec::new(),
file_names,
};
// TODO be smart about this instead, please
if !args.no_builtin_rules {
reader.built_in_targets.extend(
builtin_targets()
.into_iter()
.map(|target| (target.name.clone(), target)),
);
}
reader
.read_all()
.wrap_err_with(|| format!("while reading {}", name))?;
Ok(reader)
}
fn read_all(&mut self) -> Result<()> {
let topmost = NextLineSettings {
escaped_newline_replacement: " ",
..Default::default()
};
while let Some((line_number, line)) = self.next_line(topmost) {
let line = line?;
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 matches!(line_type, LineType::Unknown) {
let line_tokens = TokenString::text(
self.expand_macros(&line_tokens)
.wrap_err_with(|| format!("while parsing line {}", line_number))?
.trim(),
);
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).wrap_err_with(|| {
format!(
"while parsing rule definition starting on line {}",
line_number
)
})?;
}
#[cfg(feature = "full")]
LineType::RuleMacro => {
self.read_rule_macro(line_tokens, line_number)
.wrap_err_with(|| {
format!(
"while parsing rule-specific macro definition starting on line {}",
line_number
)
})?;
}
LineType::Macro => {
self.read_macro(line_tokens, line_number)
.wrap_err_with(|| {
format!(
"while parsing macro definition starting on line {}",
line_number
)
})?;
}
LineType::Include => {
self.read_include(line_tokens, line_number)
.wrap_err_with(|| {
format!("while parsing include starting on line {}", line_number)
})?;
}
#[cfg(feature = "full")]
LineType::Export => {
let mut line_tokens = line_tokens;
line_tokens.strip_prefix("export");
if line_tokens.is_empty() {
self.macros.exported = ExportConfig::all_but();
} else {
let exported = if line_tokens.contains_text("=") {
// that's an assignment!
self.read_macro(line_tokens, line_number)?
} else {
self.expand_macros(&line_tokens)?
};
self.macros.exported.add_all(exported.split_whitespace());
}
}
#[cfg(feature = "full")]
LineType::Unexport => {
let mut line_tokens = line_tokens;
line_tokens.strip_prefix("unexport");
if line_tokens.is_empty() {
self.macros.exported = ExportConfig::only();
} else {
let exported = self.expand_macros(&line_tokens)?;
self.macros.exported.remove_all(exported.split_whitespace());
}
}
LineType::Unknown => {
if !line_tokens.is_empty() {
bail!(
"error: line {}: unknown line \"{}\"",
line_number,
line_tokens
);
}
}
}
}
Ok(())
}
fn next_line(&mut self, settings: NextLineSettings) -> Option<(usize, Result<String>)> {
lazy_static! {
static ref COMMENT: Regex = #[allow(clippy::unwrap_used)]
Regex::new(r"(^|[^\\])#.*$").unwrap();
}
let escaped_newline_replacement = settings.escaped_newline_replacement;
if let Some((line_number, line)) = self.pending_line.take() {
if settings.peeking {
self.pending_line = Some((line_number, line.clone()));
}
let line = line.join(escaped_newline_replacement);
let line = if settings.strip_comments {
// TODO only do this if we were in don't-strip-comments mode before
// TODO deduplicate
COMMENT
.replace(&line, "$1")
.replace(r"\#", "#")
.trim_end()
.to_owned()
} else {
line
};
return Some((line_number, Ok(line)));
}
while let Some((line_number, line)) = self.lines_iter.next() {
let line = match line {
Ok(x) => x,
Err(err) => return Some((line_number, Err(err))),
};
let line_without_comments = COMMENT.replace(&line, "$1").replace(r"\#", "#");
let line_without_comments = line_without_comments.trim_end();
// handle comments
let line = if settings.strip_comments {
line_without_comments.to_owned()
} else {
line
};
// handle escaped newlines
let mut line_pieces = vec![line];
while line_pieces.last().map_or(false, |p| p.ends_with('\\')) {
line_pieces.last_mut().map(|x| x.pop());
if let Some((n, x)) = self.lines_iter.next() {
let line = match x {
Ok(x) => x,
Err(err) => return Some((n, Err(err))),
};
let line = if settings.strip_comments {
COMMENT
.replace(&line, "$1")
.replace(r"\#", "#")
.trim_end()
.to_owned()
} else {
line
};
line_pieces.push(line.trim_start().to_owned());
}
}
let line = line_pieces.join(escaped_newline_replacement);
#[cfg(feature = "full")]
{
let cond_line =
ConditionalLine::from(line_without_comments, |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 mut deferred_eval_context = DeferredEvalContext::new(self);
let action = line
.action(
self.conditional_stack.last(),
|name| self.stack.with_scope(&self.macros).is_defined(name),
|t| self.expand_macros_deferred_eval(t, &mut deferred_eval_context),
)
.wrap_err_with(|| {
format!("while applying conditional on line {}", line_number)
});
for child in deferred_eval_context {
self.extend(child);
}
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
.iter()
.any(ConditionalState::skipping)
{
continue;
}
}
if settings.peeking {
self.pending_line = Some((line_number, line_pieces));
}
return Some((line_number, Ok(line)));
}
None
}
/// Only applies the predicate to the next physical line in the file.
/// Doesn't apply the escaped newline replacement unless the predicate passes.
fn next_line_if(
&mut self,
settings: NextLineSettings,
predicate: impl FnOnce(&(usize, Result<String>)) -> bool,
) -> Option<(usize, Result<String>)> {
let peek_settings = NextLineSettings {
peeking: true,
..settings
};
if predicate(&self.next_line(peek_settings)?) {
self.next_line(settings)
} else {
None
}
}
fn special_target_has_prereq(&self, target: &str, name: &str, empty_counts: bool) -> bool {
self.targets
.get(target)
.or_else(|| self.built_in_targets.get(target))
.map_or(false, |target| {
(empty_counts && target.prerequisites.is_empty())
|| target.prerequisites.iter().any(|e| e == name)
})
}
fn read_include(&mut self, mut line_tokens: TokenString, line_number: usize) -> Result<()> {
let suppress_errors = line_tokens.starts_with("-");
line_tokens.strip_prefix("-");
line_tokens.strip_prefix("include ");
// remove extra leading space
line_tokens.trim_start();
let line = self.expand_macros(&line_tokens)?;
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 {
log::trace!("{}:{}: including {}", &self.file_name, line_number, field);
let child_stack = self.stack.with_scope(&self.macros);
let child = MakefileReader::read_file(
self.args,
child_stack,
field,
Rc::clone(&self.file_names),
)
.with_context(|| format!("while including {}", field));
match child {
Ok(child) => {
let child = child.finish();
self.extend(child);
}
Err(err) => {
if !suppress_errors {
match err.downcast_ref::<IoError>() {
Some(err) if err.kind() == IoErrorKind::NotFound => {
log::error!(
"{}:{}: included makefile {} not found",
&self.file_name,
line_number,
field,
);
self.failed_includes.push(field.to_owned());
}
_ => {
return Err(err);
}
}
}
}
}
}
Ok(())
}
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))?;
#[cfg(feature = "full")]
let (static_targets, targets, not_targets) = if not_targets.contains_text(":") {
// ugh, this is probably a Static Pattern Rule
let (pattern, not_targets) = not_targets
.split_once(":")
.ok_or_else(|| eyre!("bro hold the fuck up it literally just had that"))?;
(Some(targets), pattern, not_targets)
} else {
(None, targets, not_targets)
};
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![]),
};
if prerequisites.contains_text("=") {
bail!("handling rule-specific macro as rule");
}
#[cfg(feature = "full")]
let mut deferred_eval_context = DeferredEvalContext::new(self);
let prerequisites = self
.stack
.with_scope(&self.macros)
.with_scope(&LookupInternal::new_partial(&targets))
.expand(
&prerequisites,
#[cfg(feature = "full")]
Some(&mut deferred_eval_context),
)?;
#[cfg(feature = "full")]
for child in deferred_eval_context {
self.extend(child);
}
let prerequisites = prerequisites
.split_whitespace()
.map(|x| x.into())
.collect::<Vec<String>>();
let settings = NextLineSettings {
escaped_newline_replacement: "\\\n",
strip_comments: false,
..Default::default()
};
while let Some((_, x)) = self.next_line_if(settings, |(_, x)| {
x.as_ref()
.ok()
.map_or(false, |line| line.starts_with('\t') || line.is_empty())
}) {
let mut line = x?;
if !line.is_empty() {
assert!(line.starts_with('\t'));
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 (or a GNUish "pattern rule")
let inference_match = inference_match(&targets, &prerequisites);
#[cfg(feature = "full")]
let is_pattern = targets.iter().all(|x| x.contains('%'));
#[cfg(feature = "full")]
if is_pattern {
let new_rule = InferenceRule {
source: ItemSource::File {
name: self.file_name.clone(),
line: line_number,
},
products: targets.into_iter().map(|x| x.to_owned()).collect(),
prerequisites,
commands,
macros: MacroSet::new(),
};
if let Some(static_targets) = static_targets {
let static_targets = self.expand_macros(&static_targets)?;
let static_targets = static_targets.split_whitespace();
for real_target in static_targets {
if new_rule.matches(real_target)? {
let new_target = Target {
name: real_target.to_owned(),
prerequisites: new_rule.prereqs(real_target)?.collect(),
commands: new_rule.commands.clone(),
stem: new_rule
.first_match(real_target)?
.and_then(|x| x.get(1).map(|x| x.as_str().to_owned())),
already_updated: Cell::new(false),
macros: MacroSet::new(),
};
self.targets.put(new_target);
}
}
} else {
log::debug!(
"pattern-based inference rule defined: {:?}",
&new_rule,
);
self.inference_rules.put(new_rule);
}
return Ok(());
}
// don't interpret things like `.tmp: ; mkdir -p $@` as single-suffix rules
let inference_match = inference_match.and_then(|inference| {
if self.special_target_has_prereq(".SUFFIXES", inference.s1, false)
&& (inference.s2.is_empty()
|| self.special_target_has_prereq(".SUFFIXES", inference.s2, false))
{
Some(inference)
} else {
log::info!(
"{}:{}: looks like {:?} is not a suffix rule because .SUFFIXES is {:?}",
&self.file_name,
line_number,
inference,
self.targets
.get(".SUFFIXES")
.or_else(|| self.built_in_targets.get(".SUFFIXES"))
.map(|x| &x.prerequisites)
);
None
}
});
if let Some(inference_match) = inference_match {
let new_rule = InferenceRule::new_suffix(
ItemSource::File {
name: self.file_name.clone(),
line: line_number,
},
inference_match.s1.to_owned(),
inference_match.s2.to_owned(),
commands,
MacroSet::new(),
);
log::debug!(
"suffix-based inference rule defined by {:?} - {:?}",
&inference_match,
&new_rule,
);
self.inference_rules.put(new_rule);
} else {
log::debug!(
"{}:{}: new target {:?} based on {:?}",
&self.file_name,
line_number,
&targets,
&prerequisites
);
for target in targets {
if self.first_non_special_target.is_none() && !target.starts_with('.') {
self.first_non_special_target = Some(target.into());
}
// TODO handle appending to built-in (it's Complicated)
let new_target = Target {
name: target.into(),
prerequisites: prerequisites.clone(),
commands: commands.clone(),
stem: None,
already_updated: Cell::new(false),
macros: MacroSet::new(),
};
self.targets.put(new_target);
}
}
Ok(())
}
#[cfg(feature = "full")]
fn read_rule_macro(&mut self, line_tokens: TokenString, line_number: usize) -> Result<()> {
let (targets, macro_def) = line_tokens
.split_once(":")
.ok_or_else(|| eyre!("read_rule couldn't find a ':' on line {}", line_number))?;
lazy_static! {
// my kingdom for lookahead
static ref NON_EAGER_EXPANSION_ASSIGNMENT_COLON: Regex = #[allow(clippy::unwrap_used)] Regex::new(":[^:=]").unwrap();
}
if macro_def.matches_regex(&NON_EAGER_EXPANSION_ASSIGNMENT_COLON) {
bail!("GNUful static patterns not yet implemented in rule-specific macros");
};
let targets = self.expand_macros(&targets)?;
let targets = targets.split_whitespace().collect::<Vec<_>>();
let (name, value) = macro_def
.split_once("=")
.ok_or_else(|| eyre!("read_macro couldn't find a '=' on line {}", line_number))?;
let macro_assignment = self.parse_macro_assignment(name, value, line_number)?;
if targets.is_empty() {
return Ok(());
}
// we don't know yet if it's a target rule or an inference rule (or a GNUish "pattern rule")
let inference_match = inference_match(&targets, &[]);
let is_pattern = targets.iter().all(|x| x.contains('%'));
// TODO resolve against existing stack
let mut macro_set = MacroSet::new();
if let Some(outcome) = self
.check_macro_assignment_outcome(¯o_assignment, self.stack.with_scope(&self.macros))
{
let (name, value) = self.resolve_macro_value(macro_assignment, outcome, line_number)?;
// TODO trace
macro_set.set(name, value);
}
if is_pattern {
let new_rule = InferenceRule {
source: ItemSource::File {
name: self.file_name.clone(),
line: line_number,
},
products: targets.into_iter().map(|x| x.to_owned()).collect(),
prerequisites: vec![],
commands: vec![],
macros: macro_set,
};
log::error!(
"{}:{}: inference rule specific macros not yet working",
&self.file_name,
line_number
);
self.inference_rules.put(new_rule);
return Ok(());
}
// don't interpret things like `.tmp: ; mkdir -p $@` as single-suffix rules
let inference_match = inference_match.and_then(|inference| {
if self.special_target_has_prereq(".SUFFIXES", inference.s1, false)
&& (inference.s2.is_empty()
|| self.special_target_has_prereq(".SUFFIXES", inference.s2, false))
{
Some(inference)
} else {
log::info!(
"{}:{}: looks like {:?} is not a suffix rule because .SUFFIXES is {:?}",
&self.file_name,
line_number,
inference,
self.targets
.get(".SUFFIXES")
.or_else(|| self.built_in_targets.get(".SUFFIXES"))
.map(|x| &x.prerequisites)
);
None
}
});
if let Some(inference_match) = inference_match {
let new_rule = InferenceRule::new_suffix(
ItemSource::File {
name: self.file_name.clone(),
line: line_number,
},
inference_match.s1.to_owned(),
inference_match.s2.to_owned(),
vec![],
macro_set,
);
log::error!(
"{}:{}: inference rule specific macros not yet working",
&self.file_name,
line_number
);
self.inference_rules.put(new_rule);
} else {
log::trace!(
"{}:{}: target {:?} gets macros {:?}",
&self.file_name,
line_number,
&targets,
¯o_set
);
for target in targets {
if self.first_non_special_target.is_none() && !target.starts_with('.') {
self.first_non_special_target = Some(target.into());
}
// TODO handle appending to built-in (it's Complicated)
let new_target = Target {
name: target.into(),
prerequisites: vec![],
commands: vec![],
stem: None,
already_updated: Cell::new(false),
macros: macro_set.clone(),
};
self.targets.put(new_target);
}
}
Ok(())
}
/// If successful, returns the name of the macro which was read.
fn read_macro(&mut self, mut line_tokens: TokenString, line_number: usize) -> Result<String> {
let (name, 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
let settings = NextLineSettings {
strip_comments: false, // apparently
..Default::default()
};
while let Some((_, line)) = self.next_line(settings) {
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_macro couldn't find a '=' on line {}", line_number))?
};
let macro_assignment = self.parse_macro_assignment(name, value, line_number)?;
let macro_name = macro_assignment.name.clone();
if let Some(outcome) = self
.check_macro_assignment_outcome(¯o_assignment, self.stack.with_scope(&self.macros))
{
let (name, value) = self.resolve_macro_value(macro_assignment, outcome, line_number)?;
log::trace!(
"{}:{}: setting macro {} to {:?}",
&self.file_name,
line_number,
&name,
&value
);
self.macros.set(name, value);
}
Ok(macro_name)
}
fn parse_macro_assignment(
&mut self,
name: TokenString,
mut value: TokenString,
line_number: usize,
) -> Result<MacroAssignment> {
let name = self.expand_macros(&name)?;
#[cfg(feature = "full")]
let mut expand_value = false;
#[cfg(feature = "full")]
let mut skip_if_defined = false;
#[cfg(feature = "full")]
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();
#[cfg(feature = "full")]
let value = if expand_value {
TokenString::text(
self.expand_macros(&value)
.wrap_err_with(|| format!("while defining {} on line {}", name, line_number))?,
)
} else {
value
};
Ok(MacroAssignment {
name: name.to_owned(),
value,
#[cfg(feature = "full")]
expand_value,
#[cfg(feature = "full")]
skip_if_defined,
#[cfg(feature = "full")]
append,
})
}
/// For aliasing reasons, applying a macro assignment is done in three steps:
/// 1. Determine what the assignment will do, and if it will append, fetch and clone the original value. Reads both self and the stack.
/// 2. Resolve the new value of the macro, eagerly expanding if appending to an eagerly expanded macro. May write to self due to eval, and since the stack will include `&self.macros`, must be separate from 1.
/// 3. Actually perform the assignment in a [MacroSet]. Since the [MacroSet] may be `&self.macros`, must be separate from 1 and 2.
fn check_macro_assignment_outcome(
&self,
macro_assignment: &MacroAssignment,
stack: MacroScopeStack,
) -> Option<MacroAssignmentOutcome> {
let skipped = match stack.get(¯o_assignment.name).map(|x| x.source.clone()) {
// We always let command line or MAKEFLAGS macros override macros from the file.
Some(ItemSource::CommandLineOrMakeflags) => true,
// We let environment variables override macros from the file only if the command-line argument to do that was given
Some(ItemSource::Environment) => self.args.environment_overrides,
#[cfg(feature = "full")]
Some(_) => macro_assignment.skip_if_defined,
#[cfg(not(feature = "full"))]
Some(_) => false,
None => false,
};
if skipped {
None
} else {
Some(match stack.get(¯o_assignment.name) {
#[cfg(feature = "full")]
Some(old_value) if macro_assignment.append => {
MacroAssignmentOutcome::AppendedTo(old_value.into_owned())
}
_ => MacroAssignmentOutcome::Set,
})
}
}
fn resolve_macro_value(
&mut self,
macro_assignment: MacroAssignment,
outcome: MacroAssignmentOutcome,
line_number: usize,
) -> Result<(String, Macro)> {
match outcome {
MacroAssignmentOutcome::AppendedTo(mut old_value) => {
let value = macro_assignment.value;
#[cfg(feature = "full")]
let value = if old_value.eagerly_expanded {
TokenString::text(self.expand_macros(&value).wrap_err_with(|| {
format!(
"while defining {} on line {}",
macro_assignment.name, line_number
)
})?)
} else {
value
};
old_value.text.extend(TokenString::text(" "));
old_value.text.extend(value);
Ok((macro_assignment.name, old_value))
}
MacroAssignmentOutcome::Set => Ok((
macro_assignment.name,
Macro {
source: ItemSource::File {
name: self.file_name.clone(),
line: line_number,
},
text: macro_assignment.value,
#[cfg(feature = "full")]
eagerly_expanded: macro_assignment.expand_value,
},
)),
}
}
fn expand_macros(&mut self, text: &TokenString) -> Result<String> {
#[cfg(feature = "full")]
let mut deferred_eval_context = DeferredEvalContext::new(self);
let result = self.expand_macros_deferred_eval(
text,
#[cfg(feature = "full")]
&mut deferred_eval_context,
);
#[cfg(feature = "full")]
for child in deferred_eval_context {
self.extend(child);
}
result
}
fn expand_macros_deferred_eval(
&self,
text: &TokenString,
#[cfg(feature = "full")] deferred_eval_context: &mut DeferredEvalContext<R>,
) -> Result<String> {
self.stack
.with_scope(&self.macros)
.expand(
text,
#[cfg(feature = "full")]
Some(deferred_eval_context),
)
.wrap_err_with(|| format!("while expanding \"{}\"", text))
}
pub fn finish(self) -> FinishedMakefileReader {
FinishedMakefileReader {
inference_rules: self.inference_rules,
macros: self.macros,
targets: self.targets.into(),
first_non_special_target: self.first_non_special_target,
failed_includes: self.failed_includes,
}
}
fn extend(&mut self, new: FinishedMakefileReader) {
self.inference_rules.extend(new.inference_rules);
self.macros.extend(new.macros);
for (_, target) in new.targets {
self.targets.put(target);
}
if self.first_non_special_target.is_none() {
self.first_non_special_target = new.first_non_special_target;
}
self.failed_includes.extend(new.failed_includes);
}
}
pub struct FinishedMakefileReader {
pub inference_rules: InferenceRuleSet,
pub macros: MacroSet,
pub targets: HashMap<String, Target>,
pub first_non_special_target: Option<String>,
pub failed_includes: Vec<String>,
}
#[cfg(test)]
mod test {
use super::*;
use std::io::Cursor;
type R = Result<()>;
#[test]
fn multi_line_dependencies() -> R {
let file = "
unrelated: example
\tswag
x = 3 4 \\
\t\t5
a: $(x) b \\
\t\tc \\
\t\td
\tfoo";
let args = Args::empty();
let makefile = MakefileReader::read(
&args,
MacroScopeStack::default(),
MacroSet::new(),
Cursor::new(file),
"",
Default::default(),
)?
.finish();
assert_eq!(
makefile.targets["a"].prerequisites,
vec!["3", "4", "5", "b", "c", "d"]
);
Ok(())
}
#[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 mut makefile = MakefileReader::read(
&args,
MacroScopeStack::default(),
MacroSet::new(),
Cursor::new(file),
"",
Default::default(),
)?;
assert_eq!(
makefile.expand_macros(&TokenString::r#macro("worked"))?,
"yes"
);
Ok(())
}
#[cfg(feature = "full")]
#[test]
fn condition_in_rule() -> R {
let file = "
a:
ifeq (1,1)
\tfoo
endif
";
let args = Args::empty();
let makefile = MakefileReader::read(
&args,
MacroScopeStack::default(),
MacroSet::new(),
Cursor::new(file),
"",
Default::default(),
)?;
let makefile = makefile.finish();
assert_eq!(makefile.targets["a"].commands.len(), 1);
Ok(())
}
#[cfg(feature = "full")]
#[test]
fn define_syntax() -> R {
let file = "
define foo =
bar
baz
endef
";
let args = Args::empty();
let mut makefile = MakefileReader::read(
&args,
MacroScopeStack::default(),
MacroSet::new(),
Cursor::new(file),
"",
Default::default(),
)?;
assert_eq!(
makefile.expand_macros(&TokenString::r#macro("foo"))?,
"bar\nbaz"
);
Ok(())
}
#[cfg(feature = "full")]
#[test]
fn elseif() -> R {
let file = "
ifdef CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE
KBUILD_CFLAGS += -O2
else ifdef CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE_O3
KBUILD_CFLAGS += -O3
else ifdef CONFIG_CC_OPTIMIZE_FOR_SIZE
KBUILD_CFLAGS += -Os
endif
FOO = bar
";
let args = Args::empty();
let mut makefile = MakefileReader::read(
&args,
MacroScopeStack::default(),
MacroSet::new(),
Cursor::new(file),
"",
Default::default(),
)?;
assert_eq!(makefile.expand_macros(&TokenString::r#macro("FOO"))?, "bar",);
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,
MacroScopeStack::default(),
MacroSet::new(),
Cursor::new(file),
"",
Default::default(),
)?;
let makefile = makefile.finish();
assert!(makefile.targets.contains_key("server"));
Ok(())
}
#[test]
fn comment_bullshit() -> R {
let file = "
foo: bar baz#swag
example: test\\#post
info:
\thello # there
";
let args = Args::empty();
let makefile = MakefileReader::read(
&args,
MacroScopeStack::default(),
MacroSet::new(),
Cursor::new(file),
"",
Default::default(),
)?;
let makefile = makefile.finish();
assert_eq!(
makefile.targets["foo"],
Target {
name: "foo".to_owned(),
prerequisites: vec!["bar".to_owned(), "baz".to_owned()],
commands: vec![],
stem: None,
already_updated: Cell::new(false),
macros: MacroSet::new(),
}
);
assert_eq!(
makefile.targets["example"],
Target {
name: "example".to_owned(),
prerequisites: vec!["test#post".to_owned()],
commands: vec![],
stem: None,
already_updated: Cell::new(false),
macros: MacroSet::new(),
}
);
assert_eq!(
makefile.targets["info"],
Target {
name: "info".to_owned(),
prerequisites: vec![],
commands: vec![CommandLine::from(TokenString::text("hello # there")),],
stem: None,
already_updated: Cell::new(false),
macros: MacroSet::new(),
}
);
Ok(())
}
#[test]
fn sdafjijsafjdoisdf() -> R {
let file = "
cursed:
\techo this uses the bash variable '$$#' and all that \\
\techo yeah its value is $$# and it's really cool
";
let args = Args::empty();
let makefile = MakefileReader::read(
&args,
MacroScopeStack::default(),
MacroSet::new(),
Cursor::new(file),
"",
Default::default(),
)?;
let _makefile = makefile.finish();
Ok(())
}
#[test]
fn double_suffix_rule() -> R {
let file = "
.c.o:
\techo yeet
.SUFFIXES:
.l.a:
\techo hey
.SUFFIXES: .test .post
.post.test:
\techo hiiii
";
let args = Args::empty();
let makefile = MakefileReader::read(
&args,
MacroScopeStack::default(),
MacroSet::new(),
Cursor::new(file),
"",
Default::default(),
)?;
let makefile = makefile.finish();
assert_eq!(makefile.inference_rules.len(), 2);
Ok(())
}
#[test]
fn dependency_prepending_appending() -> R {
let file = "
test: b
test: a
\techo hi
test: c
";
let args = Args::empty();
let makefile = MakefileReader::read(
&args,
MacroScopeStack::default(),
MacroSet::new(),
Cursor::new(file),
"",
Default::default(),
)?;
let makefile = makefile.finish();
assert_eq!(
makefile.targets["test"].prerequisites,
vec!["a".to_owned(), "b".to_owned(), "c".to_owned()]
);
Ok(())
}
#[cfg(feature = "full")]
#[test]
fn export_assign() -> R {
let file = "export x = 3";
let args = Args::empty();
let makefile = MakefileReader::read(
&args,
MacroScopeStack::default(),
MacroSet::new(),
Cursor::new(file),
"",
Default::default(),
)?;
let makefile = makefile.finish();
assert_eq!(
makefile.macros.get_non_recursive("x").map(|x| &x.text),
Some(&TokenString::text("3"))
);
assert!(
matches!(makefile.macros.exported, ExportConfig::Only(exported) if exported.contains("x"))
);
Ok(())
}
}
|