blob: 867a7e35041fc1686ff10d21c2eeb05d1136a87b [file] [log] [blame]
Shinichiro Hamaji1d545aa2015-06-23 15:29:13 +09001// Copyright 2015 Google Inc. All rights reserved
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Fumitoshi Ukai744bb2b2015-06-25 00:10:52 +090015// +build ignore
16
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090017#include "rule.h"
18
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090019#include "expr.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090020#include "log.h"
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +090021#include "parser.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090022#include "stringprintf.h"
23#include "strutil.h"
Shinichiro Hamajie7992752015-06-29 18:38:35 +090024#include "symtab.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090025
Sasha Smundakce812f52018-08-15 14:49:39 -070026Rule::Rule() : is_double_colon(false), is_suffix_rule(false), cmd_lineno(0) {}
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +090027
Dan Willemsenee57a3f2018-11-05 16:18:44 -080028void Rule::ParseInputs(const StringPiece& inputs_str) {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090029 bool is_order_only = false;
Sasha Smundakce812f52018-08-15 14:49:39 -070030 for (auto const& input : WordScanner(inputs_str)) {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090031 if (input == "|") {
32 is_order_only = true;
33 continue;
34 }
Shinichiro Hamajie7992752015-06-29 18:38:35 +090035 Symbol input_sym = Intern(TrimLeadingCurdir(input));
Sasha Smundakce812f52018-08-15 14:49:39 -070036 (is_order_only ? order_only_inputs : inputs).push_back(input_sym);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090037 }
38}
39
Sasha Smundakce812f52018-08-15 14:49:39 -070040void Rule::ParsePrerequisites(const StringPiece& line,
41 size_t separator_pos,
Dan Willemsenee57a3f2018-11-05 16:18:44 -080042 const RuleStmt* rule_stmt) {
Sasha Smundakce812f52018-08-15 14:49:39 -070043 // line is either
44 // prerequisites [ ; command ]
45 // or
46 // target-prerequisites : prereq-patterns [ ; command ]
47 // First, separate command. At this point separator_pos should point to ';'
48 // unless null.
49 StringPiece prereq_string = line;
Dan Willemsenee57a3f2018-11-05 16:18:44 -080050 if (separator_pos != string::npos &&
51 rule_stmt->sep != RuleStmt::SEP_SEMICOLON) {
Sasha Smundakce812f52018-08-15 14:49:39 -070052 CHECK(line[separator_pos] == ';');
Shinichiro Hamaji2928f462015-06-23 20:24:53 +090053 // TODO: Maybe better to avoid Intern here?
Sasha Smundakae1d58c2018-08-22 09:39:42 -070054 cmds.push_back(Value::NewLiteral(
Sasha Smundakce812f52018-08-15 14:49:39 -070055 Intern(TrimLeftSpace(line.substr(separator_pos + 1))).str()));
56 prereq_string = line.substr(0, separator_pos);
Shinichiro Hamaji2928f462015-06-23 20:24:53 +090057 }
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090058
Sasha Smundakce812f52018-08-15 14:49:39 -070059 if ((separator_pos = prereq_string.find(':')) == string::npos) {
60 // Simple prerequisites
61 ParseInputs(prereq_string);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090062 return;
63 }
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +090064
Sasha Smundakce812f52018-08-15 14:49:39 -070065 // Static pattern rule.
66 if (!output_patterns.empty()) {
Dan Willemsene41c7552017-02-22 14:31:16 -080067 ERROR_LOC(loc, "*** mixed implicit and normal rules: deprecated syntax");
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +090068 }
69
Sasha Smundakce812f52018-08-15 14:49:39 -070070 // Empty static patterns should not produce rules, but need to eat the
71 // commands So return a rule with no outputs nor output_patterns
72 if (outputs.empty()) {
Dan Willemseneeee13a2018-06-16 10:23:33 -070073 return;
74 }
75
Sasha Smundakce812f52018-08-15 14:49:39 -070076 StringPiece target_prereq = prereq_string.substr(0, separator_pos);
77 StringPiece prereq_patterns = prereq_string.substr(separator_pos + 1);
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +090078
Sasha Smundakce812f52018-08-15 14:49:39 -070079 for (StringPiece target_pattern : WordScanner(target_prereq)) {
80 target_pattern = TrimLeadingCurdir(target_pattern);
81 for (Symbol target : outputs) {
82 if (!Pattern(target_pattern).Match(target.str())) {
Dan Willemsene41c7552017-02-22 14:31:16 -080083 WARN_LOC(loc, "target `%s' doesn't match the target pattern",
Sasha Smundakce812f52018-08-15 14:49:39 -070084 target.c_str());
Shinichiro Hamaji138c4d82015-09-28 13:53:46 +090085 }
86 }
Sasha Smundakce812f52018-08-15 14:49:39 -070087 output_patterns.push_back(Intern(target_pattern));
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +090088 }
89
Sasha Smundakce812f52018-08-15 14:49:39 -070090 if (output_patterns.empty()) {
Dan Willemsene41c7552017-02-22 14:31:16 -080091 ERROR_LOC(loc, "*** missing target pattern.");
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +090092 }
Sasha Smundakce812f52018-08-15 14:49:39 -070093 if (output_patterns.size() > 1) {
Dan Willemsene41c7552017-02-22 14:31:16 -080094 ERROR_LOC(loc, "*** multiple target patterns.");
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +090095 }
Sasha Smundakce812f52018-08-15 14:49:39 -070096 if (!IsPatternRule(output_patterns[0].str())) {
Dan Willemsene41c7552017-02-22 14:31:16 -080097 ERROR_LOC(loc, "*** target pattern contains no '%%'.");
Shinichiro Hamaji9b16bda2015-06-19 14:25:17 +090098 }
Sasha Smundakce812f52018-08-15 14:49:39 -070099 ParseInputs(prereq_patterns);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900100}
101
102string Rule::DebugString() const {
103 vector<string> v;
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900104 v.push_back(StringPrintf("outputs=[%s]", JoinSymbols(outputs, ",").c_str()));
105 v.push_back(StringPrintf("inputs=[%s]", JoinSymbols(inputs, ",").c_str()));
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900106 if (!order_only_inputs.empty()) {
107 v.push_back(StringPrintf("order_only_inputs=[%s]",
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900108 JoinSymbols(order_only_inputs, ",").c_str()));
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900109 }
Shinichiro Hamaji2e23e4a2015-06-26 07:33:16 +0900110 if (!output_patterns.empty()) {
111 v.push_back(StringPrintf("output_patterns=[%s]",
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900112 JoinSymbols(output_patterns, ",").c_str()));
Shinichiro Hamaji2e23e4a2015-06-26 07:33:16 +0900113 }
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900114 if (is_double_colon)
115 v.push_back("is_double_colon");
116 if (is_suffix_rule)
117 v.push_back("is_suffix_rule");
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900118 if (!cmds.empty()) {
119 v.push_back(StringPrintf("cmds=[%s]", JoinValues(cmds, ",").c_str()));
120 }
121 return JoinStrings(v, " ");
122}