blob: 54280a3d1d759b7aaafe4ba9769e5828f0f78b04 [file] [log] [blame]
Shinichiro Hamaji776ca302015-06-06 03:52:48 +09001#include "rule.h"
2
3#include "log.h"
4#include "stringprintf.h"
5#include "strutil.h"
6#include "value.h"
7
8// Strip leading sequences of './' from file names, so that ./file
9// and file are considered to be the same file.
10// From http://www.gnu.org/software/make/manual/make.html#Features
11StringPiece TrimLeadingCurdir(StringPiece s) {
12 if (s.substr(0, 2) != "./")
13 return s;
14 return s.substr(2);
15}
16
17static void ParseInputs(Rule* r, StringPiece s) {
18 bool is_order_only = false;
19 for (StringPiece input : WordScanner(s)) {
20 if (input == "|") {
21 is_order_only = true;
22 continue;
23 }
24 input = Intern(TrimLeadingCurdir(input));
25 if (is_order_only) {
26 r->order_only_inputs.push_back(input);
27 } else {
28 r->inputs.push_back(input);
29 }
30 }
31}
32
33Rule::Rule()
34 : is_double_colon(false),
35 is_suffix_rule(false),
36 cmd_lineno(0) {
37}
38
39void Rule::Parse(StringPiece line) {
40 size_t index = line.find(':');
41 if (index == string::npos) {
42 Error("*** missing separator.");
43 }
44
45 StringPiece first = line.substr(0, index);
46 // TODO: isPattern?
47 if (false) {
48 } else {
49 for (StringPiece tok : WordScanner(first)) {
50 outputs.push_back(Intern(TrimLeadingCurdir(tok)));
51 }
52 }
53
54 index++;
55 if (line.get(index) == ':') {
56 is_double_colon = true;
57 index++;
58 }
59
60 StringPiece rest = line.substr(index);
61
62 // TODO: TSV
63 //if (
64
65 index = rest.find(':');
66 if (index == string::npos) {
67 ParseInputs(this, rest);
68 return;
69 }
70}
71
72string Rule::DebugString() const {
73 vector<string> v;
74 v.push_back(StringPrintf("outputs=[%s]", JoinStrings(outputs, ",").c_str()));
75 v.push_back(StringPrintf("inputs=[%s]", JoinStrings(inputs, ",").c_str()));
76 if (!order_only_inputs.empty()) {
77 v.push_back(StringPrintf("order_only_inputs=[%s]",
78 JoinStrings(order_only_inputs, ",").c_str()));
79 }
80 if (is_double_colon)
81 v.push_back("is_double_colon");
82 if (is_suffix_rule)
83 v.push_back("is_suffix_rule");
84 if (!cmds.empty()) {
85 v.push_back(StringPrintf("cmds=[%s]", JoinValues(cmds, ",").c_str()));
86 }
87 return JoinStrings(v, " ");
88}