-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakeRecord.cpp
141 lines (134 loc) · 4.86 KB
/
MakeRecord.cpp
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
#include "MakeRecord.h"
using std::back_inserter;
using std::find;
using std::ifstream;
using std::istream_iterator;
using std::istringstream;
using std::regex;
using std::remove;
using std::smatch;
/* Debug only */
#include <iostream>
using std::cout;
using std::endl;
MakeRecord::MakeRecord() {
smatch ruleMatch;
regex ruleReg("(\\S+)(?:[^\\S\\n]+)?:(?:[^\\S\\n]+)([^\\n]+)", regex::ECMAScript);
vector<Rule> rulesInOrder;
/* Process Makefile in current execution directory */
__forEachLine([&](string line) -> void {
/* Check if current line is a rule declaration */
if (std::regex_match(line, ruleMatch, ruleReg)) {
string target = ruleMatch[1];
string deps = ruleMatch[2];
/* Transfer deps string to vector */
istringstream iss(deps);
vector<string> depsVec = {
istream_iterator<string>{iss},
istream_iterator<string>{}};
Rule r(target, depsVec);
rulesInOrder.push_back(r);
__rules[target] = r;
}
});
/* Second pass for adding recursive dependencies */
vector<string> phonyTargets = (ruleExists(".PHONY"))
? __rules[".PHONY"].deps
: vector<string>();
/* Locate default rule for when none specified */
for (Rule r : rulesInOrder) {
if (find(phonyTargets.begin(), phonyTargets.end(), r.target) != phonyTargets.end())
continue;
defaultTarget = r;
break;
}
for (auto&& [target, rule] : __rules) {
if (target != ".PHONY") {
for (string dep : rule.deps) {
/* Check if dependency is phony */
auto phonySearch = find(phonyTargets.begin(), phonyTargets.end(), dep);
if (phonySearch != phonyTargets.end()) {
rule.deps.erase(
std::remove(rule.deps.begin(), rule.deps.end(), dep),
rule.deps.end());
}
/* If the dependency has it's own rule, add deps */
if (ruleExists(dep)) {
rule.addDeps(__rules[dep].deps);
}
}
}
}
}
void MakeRecord::__forEachLine(function<void(string)> cb) {
string validNames[3] = {"Makefile", "makefile", "GNUmakefile"};
/* Process first match from valid makefile names */
for (string fileName : validNames) {
/* Generate a dictionary for makefile vars */
map<string, string> vars;
/**
* Lambda helper to replace variable names in the
* input string with their expansions as defined in
* the vars map.
*/
function<string(const string&)> substituteVars = [&](const string& input) -> string {
string newline = input;
regex varReg("(\\$\\(\\S+\\))");
for (auto [varName, varDef] : vars) {
while (newline.find(varName) != string::npos) {
/* Replace all instances of var with def: */
size_t begin = newline.find(varName);
size_t len = varName.size();
newline.replace(begin, len, varDef);
}
}
return newline;
};
/**
* Lambda helper to add new makefile variables to
* file dictionary recursively. Returns 0 if the line
* is a variable declaration, and 1 if the line is not.
*/
auto addVarDef = [&](const string& line) -> bool {
/* Matches lines with variable declarations */
regex varDeclReg("(\\S+)(?:\\s+)=(?:\\s)([^\\n]+)");
smatch varMatch;
bool lineDeclaresVar = std::regex_match(line, varMatch, varDeclReg);
if (lineDeclaresVar) {
string varName = varMatch[1], varDef = varMatch[2];
vars["$(" + varName + ")"] = varDef;
return 1;
}
return 0;
};
/* Open and read file */
string line;
ifstream file(fileName);
if (file.is_open()) {
while (file.good()) {
getline(file, line);
addVarDef(line);
cb(substituteVars(line));
}
file.close();
/* Return control after successful read */
return;
}
}
/* Failed to find/open file with given names */
throw "No Makefile in current working directory";
};
bool MakeRecord::ruleExists(const string& rule) {
return __rules.find(rule) != __rules.end();
}
MakeRecord::Rule MakeRecord::getRule(const string& rule) {
auto search = __rules.find(rule);
if (search == __rules.end()) {
throw "Make rule does not exist";
} else {
return search->second;
}
};
MakeRecord::Rule MakeRecord::getRule() {
return defaultTarget;
};