Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame^] | 1 | #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 |
| 11 | StringPiece TrimLeadingCurdir(StringPiece s) { |
| 12 | if (s.substr(0, 2) != "./") |
| 13 | return s; |
| 14 | return s.substr(2); |
| 15 | } |
| 16 | |
| 17 | static 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 | |
| 33 | Rule::Rule() |
| 34 | : is_double_colon(false), |
| 35 | is_suffix_rule(false), |
| 36 | cmd_lineno(0) { |
| 37 | } |
| 38 | |
| 39 | void 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 | |
| 72 | string 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 | } |