1 // 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 15 // +build ignore 16 17 #include "exec.h" 18 19 #include <stdio.h> 20 #include <stdlib.h> 21 22 #include <memory> 23 #include <unordered_map> 24 #include <utility> 25 #include <vector> 26 27 #include "command.h" 28 #include "dep.h" 29 #include "eval.h" 30 #include "expr.h" 31 #include "fileutil.h" 32 #include "flags.h" 33 #include "log.h" 34 #include "string_piece.h" 35 #include "strutil.h" 36 #include "symtab.h" 37 #include "var.h" 38 39 namespace { 40 41 const double kNotExist = -2.0; 42 const double kProcessing = -1.0; 43 44 class Executor { 45 public: 46 explicit Executor(Evaluator* ev) 47 : ce_(ev) { 48 shell_ = ev->EvalVar(kShellSym); 49 } 50 51 double ExecNode(DepNode* n, DepNode* needed_by) { 52 auto found = done_.find(n->output); 53 if (found != done_.end()) { 54 if (found->second == kProcessing) { 55 WARN("Circular %s <- %s dependency dropped.", 56 needed_by ? needed_by->output.c_str() : "(null)", 57 n->output.c_str()); 58 } 59 return found->second; 60 } 61 done_[n->output] = kProcessing; 62 double output_ts = GetTimestamp(n->output.c_str()); 63 64 LOG("ExecNode: %s for %s", 65 n->output.c_str(), 66 needed_by ? needed_by->output.c_str() : "(null)"); 67 68 if (!n->has_rule && output_ts == kNotExist && !n->is_phony) { 69 if (needed_by) { 70 ERROR("*** No rule to make target '%s', needed by '%s'.", 71 n->output.c_str(), needed_by->output.c_str()); 72 } else { 73 ERROR("*** No rule to make target '%s'.", n->output.c_str()); 74 } 75 } 76 77 double latest = kProcessing; 78 for (DepNode* d : n->order_onlys) { 79 if (Exists(d->output.str())) { 80 continue; 81 } 82 double ts = ExecNode(d, n); 83 if (latest < ts) 84 latest = ts; 85 } 86 87 for (DepNode* d : n->deps) { 88 double ts = ExecNode(d, n); 89 if (latest < ts) 90 latest = ts; 91 } 92 93 if (output_ts >= latest && !n->is_phony) { 94 done_[n->output] = output_ts; 95 return output_ts; 96 } 97 98 vector<Command*> commands; 99 ce_.Eval(n, &commands); 100 for (Command* command : commands) { 101 num_commands_ += 1; 102 if (command->echo) { 103 printf("%s\n", command->cmd.c_str()); 104 fflush(stdout); 105 } 106 if (!g_flags.is_dry_run) { 107 string out; 108 int result = RunCommand(shell_, command->cmd.c_str(), 109 RedirectStderr::STDOUT, 110 &out); 111 printf("%s", out.c_str()); 112 if (result != 0) { 113 if (command->ignore_error) { 114 fprintf(stderr, "[%s] Error %d (ignored)\n", 115 command->output.c_str(), WEXITSTATUS(result)); 116 } else { 117 fprintf(stderr, "*** [%s] Error %d\n", 118 command->output.c_str(), WEXITSTATUS(result)); 119 exit(1); 120 } 121 } 122 } 123 delete command; 124 } 125 126 done_[n->output] = output_ts; 127 return output_ts; 128 } 129 130 uint64_t Count() { 131 return num_commands_; 132 } 133 134 private: 135 CommandEvaluator ce_; 136 unordered_map<Symbol, double> done_; 137 string shell_; 138 uint64_t num_commands_; 139 }; 140 141 } // namespace 142 143 void Exec(const vector<DepNode*>& roots, Evaluator* ev) { 144 unique_ptr<Executor> executor(new Executor(ev)); 145 for (DepNode* root : roots) { 146 executor->ExecNode(root, NULL); 147 } 148 if (executor->Count() == 0) { 149 for (DepNode* root : roots) { 150 printf("kati: Nothing to be done for `%s'.\n", root->output.c_str()); 151 } 152 } 153 } 154