Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Shinichiro Hamaji | ee8b33c | 2015-04-13 20:08:38 +0900 | [diff] [blame] | 4 | "bytes" |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 5 | "fmt" |
Shinichiro Hamaji | 110af33 | 2015-04-13 19:04:50 +0900 | [diff] [blame] | 6 | "io" |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 7 | "os" |
| 8 | "os/exec" |
Fumitoshi Ukai | 8edcb79 | 2015-04-02 11:23:23 +0900 | [diff] [blame] | 9 | "path/filepath" |
Fumitoshi Ukai | 3e5161e | 2015-04-01 22:42:10 +0900 | [diff] [blame] | 10 | "strings" |
Fumitoshi Ukai | 772fe1a | 2015-04-01 23:28:26 +0900 | [diff] [blame] | 11 | "syscall" |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 12 | "time" |
| 13 | ) |
| 14 | |
| 15 | type Executor struct { |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 16 | rules map[string]*Rule |
| 17 | implicitRules []*Rule |
Shinichiro Hamaji | 20a4376 | 2015-04-02 02:42:25 +0900 | [diff] [blame] | 18 | suffixRules map[string][]*Rule |
Shinichiro Hamaji | 26fe957 | 2015-04-02 02:31:32 +0900 | [diff] [blame] | 19 | firstRule *Rule |
Shinichiro Hamaji | 89551c9 | 2015-04-11 19:33:03 +0900 | [diff] [blame] | 20 | shell string |
Shinichiro Hamaji | de4d3e4 | 2015-04-12 23:14:35 +0900 | [diff] [blame] | 21 | vars Vars |
Shinichiro Hamaji | 940bb6c | 2015-04-13 00:35:59 +0900 | [diff] [blame] | 22 | // target -> timestamp, a negative timestamp means the target is |
| 23 | // currently being processed. |
Fumitoshi Ukai | 4b82f19 | 2015-04-07 11:36:42 +0900 | [diff] [blame] | 24 | done map[string]int64 |
Shinichiro Hamaji | 110af33 | 2015-04-13 19:04:50 +0900 | [diff] [blame] | 25 | |
| 26 | currentOutput string |
| 27 | currentInputs []string |
| 28 | currentStem string |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 29 | |
| 30 | trace []string |
| 31 | buildCnt int |
| 32 | alreadyDoneCnt int |
| 33 | noRuleCnt int |
| 34 | upToDateCnt int |
| 35 | runCommandCnt int |
| 36 | pickExplicitRuleCnt int |
| 37 | pickImplicitRuleCnt int |
| 38 | pickSuffixRuleCnt int |
| 39 | pickExplicitRuleWithoutCmdCnt int |
Shinichiro Hamaji | 110af33 | 2015-04-13 19:04:50 +0900 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | type AutoVar struct{ ex *Executor } |
| 43 | |
| 44 | func (v AutoVar) Flavor() string { return "undefined" } |
| 45 | func (v AutoVar) Origin() string { return "automatic" } |
| 46 | func (v AutoVar) IsDefined() bool { panic("not implemented") } |
| 47 | func (v AutoVar) String() string { panic("not implemented") } |
Shinichiro Hamaji | ea170b1 | 2015-04-15 10:02:33 +0900 | [diff] [blame^] | 48 | func (v AutoVar) Append(*Evaluator, string) Var { |
| 49 | panic("must not be called") |
| 50 | } |
| 51 | func (v AutoVar) AppendVar(*Evaluator, Var) Var { |
Shinichiro Hamaji | 110af33 | 2015-04-13 19:04:50 +0900 | [diff] [blame] | 52 | panic("must not be called") |
| 53 | } |
| 54 | |
| 55 | type AutoAtVar struct{ AutoVar } |
| 56 | |
| 57 | func (v AutoAtVar) Eval(w io.Writer, ev *Evaluator) { |
| 58 | fmt.Fprint(w, v.ex.currentOutput) |
| 59 | } |
| 60 | |
| 61 | type AutoLessVar struct{ AutoVar } |
| 62 | |
| 63 | func (v AutoLessVar) Eval(w io.Writer, ev *Evaluator) { |
| 64 | if len(v.ex.currentInputs) > 0 { |
| 65 | fmt.Fprint(w, v.ex.currentInputs[0]) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | type AutoHatVar struct{ AutoVar } |
| 70 | |
| 71 | func (v AutoHatVar) Eval(w io.Writer, ev *Evaluator) { |
Shinichiro Hamaji | a622c5e | 2015-04-13 19:09:30 +0900 | [diff] [blame] | 72 | var uniqueInputs []string |
| 73 | seen := make(map[string]bool) |
| 74 | for _, input := range v.ex.currentInputs { |
| 75 | if !seen[input] { |
| 76 | seen[input] = true |
| 77 | uniqueInputs = append(uniqueInputs, input) |
| 78 | } |
| 79 | } |
| 80 | fmt.Fprint(w, strings.Join(uniqueInputs, " ")) |
| 81 | } |
| 82 | |
| 83 | type AutoPlusVar struct{ AutoVar } |
| 84 | |
| 85 | func (v AutoPlusVar) Eval(w io.Writer, ev *Evaluator) { |
Shinichiro Hamaji | 110af33 | 2015-04-13 19:04:50 +0900 | [diff] [blame] | 86 | fmt.Fprint(w, strings.Join(v.ex.currentInputs, " ")) |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 87 | } |
| 88 | |
Shinichiro Hamaji | bb9772d | 2015-04-13 21:41:22 +0900 | [diff] [blame] | 89 | type AutoStarVar struct{ AutoVar } |
| 90 | |
| 91 | func (v AutoStarVar) Eval(w io.Writer, ev *Evaluator) { |
| 92 | // TODO: Use currentStem. See auto_stem_var.mk |
| 93 | fmt.Fprint(w, stripExt(v.ex.currentOutput)) |
| 94 | } |
| 95 | |
Shinichiro Hamaji | ee8b33c | 2015-04-13 20:08:38 +0900 | [diff] [blame] | 96 | type AutoSuffixDVar struct { |
| 97 | AutoVar |
| 98 | v Var |
| 99 | } |
| 100 | |
| 101 | func (v AutoSuffixDVar) Eval(w io.Writer, ev *Evaluator) { |
| 102 | var buf bytes.Buffer |
| 103 | v.v.Eval(&buf, ev) |
| 104 | for i, tok := range splitSpaces(buf.String()) { |
| 105 | if i > 0 { |
| 106 | w.Write([]byte{' '}) |
| 107 | } |
| 108 | fmt.Fprint(w, filepath.Dir(tok)) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | type AutoSuffixFVar struct { |
| 113 | AutoVar |
| 114 | v Var |
| 115 | } |
| 116 | |
| 117 | func (v AutoSuffixFVar) Eval(w io.Writer, ev *Evaluator) { |
| 118 | var buf bytes.Buffer |
| 119 | v.v.Eval(&buf, ev) |
| 120 | for i, tok := range splitSpaces(buf.String()) { |
| 121 | if i > 0 { |
| 122 | w.Write([]byte{' '}) |
| 123 | } |
| 124 | fmt.Fprint(w, filepath.Base(tok)) |
| 125 | } |
| 126 | } |
| 127 | |
Shinichiro Hamaji | de4d3e4 | 2015-04-12 23:14:35 +0900 | [diff] [blame] | 128 | func newExecutor(vars Vars) *Executor { |
Shinichiro Hamaji | 110af33 | 2015-04-13 19:04:50 +0900 | [diff] [blame] | 129 | ex := &Executor{ |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 130 | rules: make(map[string]*Rule), |
Shinichiro Hamaji | 20a4376 | 2015-04-02 02:42:25 +0900 | [diff] [blame] | 131 | suffixRules: make(map[string][]*Rule), |
Fumitoshi Ukai | 4b82f19 | 2015-04-07 11:36:42 +0900 | [diff] [blame] | 132 | done: make(map[string]int64), |
Shinichiro Hamaji | de4d3e4 | 2015-04-12 23:14:35 +0900 | [diff] [blame] | 133 | vars: vars, |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 134 | } |
Shinichiro Hamaji | 110af33 | 2015-04-13 19:04:50 +0900 | [diff] [blame] | 135 | |
Shinichiro Hamaji | ee8b33c | 2015-04-13 20:08:38 +0900 | [diff] [blame] | 136 | for k, v := range map[string]Var{ |
| 137 | "@": AutoAtVar{AutoVar: AutoVar{ex: ex}}, |
| 138 | "<": AutoLessVar{AutoVar: AutoVar{ex: ex}}, |
| 139 | "^": AutoHatVar{AutoVar: AutoVar{ex: ex}}, |
| 140 | "+": AutoPlusVar{AutoVar: AutoVar{ex: ex}}, |
Shinichiro Hamaji | bb9772d | 2015-04-13 21:41:22 +0900 | [diff] [blame] | 141 | "*": AutoStarVar{AutoVar: AutoVar{ex: ex}}, |
Shinichiro Hamaji | ee8b33c | 2015-04-13 20:08:38 +0900 | [diff] [blame] | 142 | } { |
| 143 | ex.vars[k] = v |
| 144 | ex.vars[k+"D"] = AutoSuffixDVar{v: v} |
| 145 | ex.vars[k+"F"] = AutoSuffixFVar{v: v} |
| 146 | } |
Shinichiro Hamaji | 110af33 | 2015-04-13 19:04:50 +0900 | [diff] [blame] | 147 | |
| 148 | return ex |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 149 | } |
| 150 | |
Fumitoshi Ukai | cf2b038 | 2015-03-30 17:48:54 +0900 | [diff] [blame] | 151 | // TODO(ukai): use time.Time? |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 152 | func getTimestamp(filename string) int64 { |
| 153 | st, err := os.Stat(filename) |
| 154 | if err != nil { |
| 155 | return -2 |
| 156 | } |
| 157 | return st.ModTime().Unix() |
| 158 | } |
| 159 | |
Fumitoshi Ukai | 6b37883 | 2015-04-06 16:54:37 +0900 | [diff] [blame] | 160 | func (ex *Executor) exists(target string) bool { |
Fumitoshi Ukai | f04610d | 2015-04-07 00:03:03 +0900 | [diff] [blame] | 161 | _, present := ex.rules[target] |
| 162 | if present { |
| 163 | return true |
| 164 | } |
Fumitoshi Ukai | 6b37883 | 2015-04-06 16:54:37 +0900 | [diff] [blame] | 165 | rule, present := ex.rules[".PHONY"] |
| 166 | if present { |
| 167 | for _, input := range rule.inputs { |
| 168 | if target == input { |
| 169 | return true |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | return exists(target) |
| 174 | } |
| 175 | |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 176 | type runner struct { |
| 177 | output string |
| 178 | cmd string |
| 179 | echo bool |
| 180 | dryRun bool |
| 181 | ignoreError bool |
Shinichiro Hamaji | 89551c9 | 2015-04-11 19:33:03 +0900 | [diff] [blame] | 182 | shell string |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 183 | } |
| 184 | |
Shinichiro Hamaji | 973c69f | 2015-04-11 21:52:38 +0900 | [diff] [blame] | 185 | func evalCmd(ev *Evaluator, r runner, s string) []runner { |
| 186 | r = newRunner(r, s) |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 187 | if strings.IndexByte(r.cmd, '$') < 0 { |
| 188 | // fast path |
| 189 | return []runner{r} |
| 190 | } |
| 191 | cmds := ev.evalExpr(r.cmd) |
| 192 | var runners []runner |
| 193 | for _, cmd := range strings.Split(cmds, "\n") { |
Shinichiro Hamaji | 7735319 | 2015-04-13 16:04:15 +0900 | [diff] [blame] | 194 | if len(runners) > 0 && strings.HasSuffix(runners[0].cmd, "\\") { |
| 195 | runners[0].cmd += "\n" |
| 196 | runners[0].cmd += cmd |
| 197 | } else { |
| 198 | runners = append(runners, newRunner(r, cmd)) |
| 199 | } |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 200 | } |
| 201 | return runners |
| 202 | } |
| 203 | |
Shinichiro Hamaji | 973c69f | 2015-04-11 21:52:38 +0900 | [diff] [blame] | 204 | func newRunner(r runner, s string) runner { |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 205 | for { |
Shinichiro Hamaji | c88618f | 2015-04-11 19:58:06 +0900 | [diff] [blame] | 206 | s = trimLeftSpace(s) |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 207 | if s == "" { |
| 208 | return runner{} |
Fumitoshi Ukai | 772fe1a | 2015-04-01 23:28:26 +0900 | [diff] [blame] | 209 | } |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 210 | switch s[0] { |
| 211 | case '@': |
| 212 | if !r.dryRun { |
| 213 | r.echo = false |
| 214 | } |
| 215 | s = s[1:] |
| 216 | continue |
| 217 | case '-': |
| 218 | r.ignoreError = true |
| 219 | s = s[1:] |
Shinichiro Hamaji | 07e146b | 2015-04-02 16:17:24 +0900 | [diff] [blame] | 220 | continue |
| 221 | } |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 222 | break |
| 223 | } |
| 224 | r.cmd = s |
| 225 | return r |
| 226 | } |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 227 | |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 228 | func (r runner) run() error { |
| 229 | if r.echo { |
| 230 | fmt.Printf("%s\n", r.cmd) |
| 231 | } |
| 232 | if r.dryRun { |
| 233 | return nil |
| 234 | } |
Shinichiro Hamaji | 89551c9 | 2015-04-11 19:33:03 +0900 | [diff] [blame] | 235 | args := []string{r.shell, "-c", r.cmd} |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 236 | cmd := exec.Cmd{ |
| 237 | Path: args[0], |
| 238 | Args: args, |
| 239 | } |
| 240 | out, err := cmd.CombinedOutput() |
| 241 | fmt.Printf("%s", out) |
| 242 | exit := exitStatus(err) |
| 243 | if r.ignoreError && exit != 0 { |
| 244 | fmt.Printf("[%s] Error %d (ignored)\n", r.output, exit) |
| 245 | err = nil |
| 246 | } |
| 247 | return err |
| 248 | } |
| 249 | |
| 250 | func exitStatus(err error) int { |
| 251 | if err == nil { |
| 252 | return 0 |
| 253 | } |
| 254 | exit := 1 |
| 255 | if err, ok := err.(*exec.ExitError); ok { |
| 256 | if w, ok := err.ProcessState.Sys().(syscall.WaitStatus); ok { |
| 257 | return w.ExitStatus() |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 258 | } |
| 259 | } |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 260 | return exit |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 261 | } |
| 262 | |
Shinichiro Hamaji | 20a4376 | 2015-04-02 02:42:25 +0900 | [diff] [blame] | 263 | func replaceSuffix(s string, newsuf string) string { |
| 264 | // TODO: Factor out the logic around suffix rules and use |
| 265 | // it from substitution references. |
| 266 | // http://www.gnu.org/software/make/manual/make.html#Substitution-Refs |
Shinichiro Hamaji | 5e2c3c7 | 2015-04-03 15:04:54 +0900 | [diff] [blame] | 267 | return fmt.Sprintf("%s.%s", stripExt(s), newsuf) |
Shinichiro Hamaji | 20a4376 | 2015-04-02 02:42:25 +0900 | [diff] [blame] | 268 | } |
| 269 | |
Shinichiro Hamaji | 0d0af5b | 2015-04-02 05:17:23 +0900 | [diff] [blame] | 270 | func (ex *Executor) canPickImplicitRule(rule *Rule, output string) bool { |
| 271 | outputPattern := rule.outputPatterns[0] |
| 272 | if !matchPattern(outputPattern, output) { |
| 273 | return false |
| 274 | } |
| 275 | for _, input := range rule.inputs { |
| 276 | input = substPattern(outputPattern, input, output) |
Fumitoshi Ukai | 6b37883 | 2015-04-06 16:54:37 +0900 | [diff] [blame] | 277 | if !ex.exists(input) { |
Shinichiro Hamaji | 0d0af5b | 2015-04-02 05:17:23 +0900 | [diff] [blame] | 278 | return false |
| 279 | } |
| 280 | } |
| 281 | return true |
| 282 | } |
| 283 | |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 284 | func (ex *Executor) pickRule(output string) (*Rule, bool) { |
| 285 | rule, present := ex.rules[output] |
| 286 | if present { |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 287 | ex.pickExplicitRuleCnt++ |
Fumitoshi Ukai | d92c487 | 2015-04-06 16:17:31 +0900 | [diff] [blame] | 288 | if len(rule.cmds) > 0 { |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 289 | return rule, true |
| 290 | } |
Fumitoshi Ukai | d92c487 | 2015-04-06 16:17:31 +0900 | [diff] [blame] | 291 | // If none of the explicit rules for a target has commands, |
| 292 | // then `make' searches for an applicable implicit rule to |
| 293 | // find some commands. |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 294 | ex.pickExplicitRuleWithoutCmdCnt++ |
Fumitoshi Ukai | d92c487 | 2015-04-06 16:17:31 +0900 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | for _, irule := range ex.implicitRules { |
| 298 | if !ex.canPickImplicitRule(irule, output) { |
| 299 | continue |
| 300 | } |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 301 | ex.pickImplicitRuleCnt++ |
Fumitoshi Ukai | d92c487 | 2015-04-06 16:17:31 +0900 | [diff] [blame] | 302 | if rule != nil { |
| 303 | r := &Rule{} |
| 304 | *r = *rule |
| 305 | r.outputPatterns = irule.outputPatterns |
| 306 | // implicit rule's prerequisites will be used for $< |
| 307 | r.inputs = append(irule.inputs, r.inputs...) |
| 308 | if irule.vars != nil { |
Shinichiro Hamaji | ea170b1 | 2015-04-15 10:02:33 +0900 | [diff] [blame^] | 309 | r.vars = append(r.vars, rule.vars...) |
| 310 | r.vars = append(r.vars, irule.vars...) |
Fumitoshi Ukai | d92c487 | 2015-04-06 16:17:31 +0900 | [diff] [blame] | 311 | } |
| 312 | r.cmds = irule.cmds |
| 313 | // TODO(ukai): filename, lineno? |
| 314 | r.cmdLineno = irule.cmdLineno |
| 315 | return r, true |
| 316 | } |
| 317 | // TODO(ukai): check len(irule.cmd) ? |
| 318 | return irule, true |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 319 | } |
| 320 | |
Shinichiro Hamaji | abc80d2 | 2015-04-02 01:56:32 +0900 | [diff] [blame] | 321 | outputSuffix := filepath.Ext(output) |
Fumitoshi Ukai | d92c487 | 2015-04-06 16:17:31 +0900 | [diff] [blame] | 322 | if !strings.HasPrefix(outputSuffix, ".") { |
| 323 | return rule, rule != nil |
Shinichiro Hamaji | abc80d2 | 2015-04-02 01:56:32 +0900 | [diff] [blame] | 324 | } |
Fumitoshi Ukai | d92c487 | 2015-04-06 16:17:31 +0900 | [diff] [blame] | 325 | rules, present := ex.suffixRules[outputSuffix[1:]] |
| 326 | if !present { |
| 327 | return rule, rule != nil |
| 328 | } |
| 329 | for _, irule := range rules { |
| 330 | if len(irule.inputs) != 1 { |
| 331 | panic(fmt.Sprintf("unexpected number of input for a suffix rule (%d)", len(irule.inputs))) |
| 332 | } |
Fumitoshi Ukai | 6b37883 | 2015-04-06 16:54:37 +0900 | [diff] [blame] | 333 | if !ex.exists(replaceSuffix(output, irule.inputs[0])) { |
Fumitoshi Ukai | d92c487 | 2015-04-06 16:17:31 +0900 | [diff] [blame] | 334 | continue |
| 335 | } |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 336 | ex.pickSuffixRuleCnt++ |
Fumitoshi Ukai | d92c487 | 2015-04-06 16:17:31 +0900 | [diff] [blame] | 337 | if rule != nil { |
| 338 | r := &Rule{} |
| 339 | *r = *rule |
| 340 | // TODO(ukai): input order is correct? |
| 341 | r.inputs = append([]string{replaceSuffix(output, irule.inputs[0])}, r.inputs...) |
Shinichiro Hamaji | ea170b1 | 2015-04-15 10:02:33 +0900 | [diff] [blame^] | 342 | r.vars = append(r.vars, rule.vars...) |
| 343 | r.vars = append(r.vars, irule.vars...) |
Fumitoshi Ukai | d92c487 | 2015-04-06 16:17:31 +0900 | [diff] [blame] | 344 | r.cmds = irule.cmds |
| 345 | // TODO(ukai): filename, lineno? |
| 346 | r.cmdLineno = irule.cmdLineno |
| 347 | return r, true |
| 348 | } |
| 349 | // TODO(ukai): check len(irule.cmd) ? |
| 350 | return irule, true |
| 351 | } |
| 352 | return rule, rule != nil |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 353 | } |
| 354 | |
Shinichiro Hamaji | de4d3e4 | 2015-04-12 23:14:35 +0900 | [diff] [blame] | 355 | func (ex *Executor) build(output string, neededBy string) (int64, error) { |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 356 | Log("Building: %s", output) |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 357 | ex.buildCnt++ |
| 358 | if ex.buildCnt%100 == 0 { |
| 359 | ex.reportStats() |
| 360 | } |
| 361 | |
Fumitoshi Ukai | 4b82f19 | 2015-04-07 11:36:42 +0900 | [diff] [blame] | 362 | outputTs, ok := ex.done[output] |
| 363 | if ok { |
Shinichiro Hamaji | 940bb6c | 2015-04-13 00:35:59 +0900 | [diff] [blame] | 364 | if outputTs < 0 { |
| 365 | fmt.Printf("Circular %s <- %s dependency dropped.\n", neededBy, output) |
| 366 | } |
Fumitoshi Ukai | bac3ccf | 2015-04-08 13:41:22 +0900 | [diff] [blame] | 367 | Log("Building: %s already done: %d", output, outputTs) |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 368 | ex.alreadyDoneCnt++ |
Fumitoshi Ukai | 4b82f19 | 2015-04-07 11:36:42 +0900 | [diff] [blame] | 369 | return outputTs, nil |
| 370 | } |
Shinichiro Hamaji | 940bb6c | 2015-04-13 00:35:59 +0900 | [diff] [blame] | 371 | ex.done[output] = -1 |
Fumitoshi Ukai | 4b82f19 | 2015-04-07 11:36:42 +0900 | [diff] [blame] | 372 | outputTs = getTimestamp(output) |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 373 | |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 374 | rule, present := ex.pickRule(output) |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 375 | if !present { |
Fumitoshi Ukai | cf2b038 | 2015-03-30 17:48:54 +0900 | [diff] [blame] | 376 | if outputTs >= 0 { |
Shinichiro Hamaji | 61748ac | 2015-04-13 01:08:43 +0900 | [diff] [blame] | 377 | ex.done[output] = outputTs |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 378 | ex.noRuleCnt++ |
Fumitoshi Ukai | f8efa0a | 2015-03-30 18:10:11 +0900 | [diff] [blame] | 379 | return outputTs, nil |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 380 | } |
Shinichiro Hamaji | eb4da7f | 2015-04-11 20:28:54 +0900 | [diff] [blame] | 381 | if neededBy == "" { |
| 382 | ErrorNoLocation("*** No rule to make target %q.", output) |
| 383 | } else { |
| 384 | ErrorNoLocation("*** No rule to make target %q, needed by %q.", output, neededBy) |
| 385 | } |
Fumitoshi Ukai | f8efa0a | 2015-03-30 18:10:11 +0900 | [diff] [blame] | 386 | return outputTs, fmt.Errorf("no rule to make target %q", output) |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 387 | } |
Shinichiro Hamaji | 4e9ab1a | 2015-04-12 01:31:58 +0900 | [diff] [blame] | 388 | |
| 389 | var olds []oldVar |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 390 | if rule.vars != nil { |
Shinichiro Hamaji | ea170b1 | 2015-04-15 10:02:33 +0900 | [diff] [blame^] | 391 | for _, v := range rule.vars { |
| 392 | olds = append(olds, newOldVar(ex.vars, v.name)) |
| 393 | switch v.op { |
| 394 | case ":=", "=": |
| 395 | ex.vars[v.name] = v.v |
| 396 | case "+=": |
| 397 | oldVar := ex.vars[v.name] |
| 398 | // Not sure why make does not append a |
| 399 | // whitespace... See |
| 400 | // target_specific_var_append. |
| 401 | if oldVar.String() == "" { |
| 402 | ex.vars[v.name] = v.v |
| 403 | } else { |
| 404 | ex.vars[v.name] = oldVar.AppendVar(newEvaluator(ex.vars), v.v) |
| 405 | } |
| 406 | case "?=": |
| 407 | if _, present := ex.vars[v.name]; !present { |
| 408 | ex.vars[v.name] = v.v |
| 409 | } |
| 410 | } |
Shinichiro Hamaji | 4e9ab1a | 2015-04-12 01:31:58 +0900 | [diff] [blame] | 411 | } |
| 412 | defer func() { |
| 413 | for _, old := range olds { |
Shinichiro Hamaji | de4d3e4 | 2015-04-12 23:14:35 +0900 | [diff] [blame] | 414 | old.restore(ex.vars) |
Shinichiro Hamaji | 4e9ab1a | 2015-04-12 01:31:58 +0900 | [diff] [blame] | 415 | } |
| 416 | }() |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 417 | } |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 418 | |
| 419 | latest := int64(-1) |
Shinichiro Hamaji | 57aff98 | 2015-04-02 02:15:38 +0900 | [diff] [blame] | 420 | var actualInputs []string |
Fumitoshi Ukai | d92c487 | 2015-04-06 16:17:31 +0900 | [diff] [blame] | 421 | Log("Building: %s inputs:%q", output, rule.inputs) |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 422 | for _, input := range rule.inputs { |
Shinichiro Hamaji | 7b203b2 | 2015-04-01 03:44:57 +0900 | [diff] [blame] | 423 | if len(rule.outputPatterns) > 0 { |
| 424 | if len(rule.outputPatterns) > 1 { |
| 425 | panic("TODO: multiple output pattern is not supported yet") |
| 426 | } |
| 427 | input = substPattern(rule.outputPatterns[0], input, output) |
Shinichiro Hamaji | abc80d2 | 2015-04-02 01:56:32 +0900 | [diff] [blame] | 428 | } else if rule.isSuffixRule { |
Shinichiro Hamaji | 20a4376 | 2015-04-02 02:42:25 +0900 | [diff] [blame] | 429 | input = replaceSuffix(output, input) |
Shinichiro Hamaji | 7b203b2 | 2015-04-01 03:44:57 +0900 | [diff] [blame] | 430 | } |
Shinichiro Hamaji | 57aff98 | 2015-04-02 02:15:38 +0900 | [diff] [blame] | 431 | actualInputs = append(actualInputs, input) |
Shinichiro Hamaji | 7b203b2 | 2015-04-01 03:44:57 +0900 | [diff] [blame] | 432 | |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 433 | ex.trace = append(ex.trace, input) |
Shinichiro Hamaji | de4d3e4 | 2015-04-12 23:14:35 +0900 | [diff] [blame] | 434 | ts, err := ex.build(input, output) |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 435 | ex.trace = ex.trace[0 : len(ex.trace)-1] |
Fumitoshi Ukai | f8efa0a | 2015-03-30 18:10:11 +0900 | [diff] [blame] | 436 | if err != nil { |
| 437 | return outputTs, err |
| 438 | } |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 439 | if latest < ts { |
| 440 | latest = ts |
| 441 | } |
| 442 | } |
| 443 | |
Shinichiro Hamaji | 5c53b57 | 2015-04-02 05:36:42 +0900 | [diff] [blame] | 444 | for _, input := range rule.orderOnlyInputs { |
Fumitoshi Ukai | f04610d | 2015-04-07 00:03:03 +0900 | [diff] [blame] | 445 | if exists(input) { |
Shinichiro Hamaji | 5c53b57 | 2015-04-02 05:36:42 +0900 | [diff] [blame] | 446 | continue |
| 447 | } |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 448 | |
| 449 | ex.trace = append(ex.trace, input) |
Shinichiro Hamaji | de4d3e4 | 2015-04-12 23:14:35 +0900 | [diff] [blame] | 450 | ts, err := ex.build(input, output) |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 451 | ex.trace = ex.trace[0 : len(ex.trace)-1] |
Shinichiro Hamaji | 5c53b57 | 2015-04-02 05:36:42 +0900 | [diff] [blame] | 452 | if err != nil { |
| 453 | return outputTs, err |
| 454 | } |
| 455 | if latest < ts { |
| 456 | latest = ts |
| 457 | } |
| 458 | } |
| 459 | |
Fumitoshi Ukai | cf2b038 | 2015-03-30 17:48:54 +0900 | [diff] [blame] | 460 | if outputTs >= latest { |
Shinichiro Hamaji | 61748ac | 2015-04-13 01:08:43 +0900 | [diff] [blame] | 461 | ex.done[output] = outputTs |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 462 | ex.upToDateCnt++ |
Fumitoshi Ukai | f8efa0a | 2015-03-30 18:10:11 +0900 | [diff] [blame] | 463 | return outputTs, nil |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 464 | } |
| 465 | |
Shinichiro Hamaji | 110af33 | 2015-04-13 19:04:50 +0900 | [diff] [blame] | 466 | // For automatic variables. |
| 467 | ex.currentOutput = output |
| 468 | ex.currentInputs = actualInputs |
| 469 | |
Shinichiro Hamaji | de4d3e4 | 2015-04-12 23:14:35 +0900 | [diff] [blame] | 470 | ev := newEvaluator(ex.vars) |
Shinichiro Hamaji | e708a9d | 2015-04-03 14:34:35 +0900 | [diff] [blame] | 471 | ev.filename = rule.filename |
| 472 | ev.lineno = rule.cmdLineno |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 473 | var runners []runner |
Fumitoshi Ukai | f04610d | 2015-04-07 00:03:03 +0900 | [diff] [blame] | 474 | Log("Building: %s cmds:%q", output, rule.cmds) |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 475 | r := runner{ |
| 476 | output: output, |
| 477 | echo: true, |
| 478 | dryRun: dryRunFlag, |
Shinichiro Hamaji | 7735319 | 2015-04-13 16:04:15 +0900 | [diff] [blame] | 479 | shell: ex.shell, |
Fumitoshi Ukai | 3e5161e | 2015-04-01 22:42:10 +0900 | [diff] [blame] | 480 | } |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 481 | for _, cmd := range rule.cmds { |
Shinichiro Hamaji | 973c69f | 2015-04-11 21:52:38 +0900 | [diff] [blame] | 482 | for _, r := range evalCmd(ev, r, cmd) { |
| 483 | if len(r.cmd) != 0 { |
| 484 | runners = append(runners, r) |
| 485 | } |
| 486 | } |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 487 | } |
| 488 | for _, r := range runners { |
| 489 | err := r.run() |
| 490 | if err != nil { |
| 491 | exit := exitStatus(err) |
| 492 | fmt.Printf("[%s] Error %d: %v\n", r.output, exit, err) |
| 493 | return outputTs, err |
| 494 | } |
Fumitoshi Ukai | f8efa0a | 2015-03-30 18:10:11 +0900 | [diff] [blame] | 495 | } |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 496 | |
Fumitoshi Ukai | cf2b038 | 2015-03-30 17:48:54 +0900 | [diff] [blame] | 497 | outputTs = getTimestamp(output) |
| 498 | if outputTs < 0 { |
| 499 | outputTs = time.Now().Unix() |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 500 | } |
Fumitoshi Ukai | 4b82f19 | 2015-04-07 11:36:42 +0900 | [diff] [blame] | 501 | ex.done[output] = outputTs |
| 502 | Log("Building: %s done %d", output, outputTs) |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 503 | ex.runCommandCnt++ |
Fumitoshi Ukai | f8efa0a | 2015-03-30 18:10:11 +0900 | [diff] [blame] | 504 | return outputTs, nil |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 505 | } |
| 506 | |
Shinichiro Hamaji | 26fe957 | 2015-04-02 02:31:32 +0900 | [diff] [blame] | 507 | func (ex *Executor) populateSuffixRule(rule *Rule, output string) bool { |
Shinichiro Hamaji | abc80d2 | 2015-04-02 01:56:32 +0900 | [diff] [blame] | 508 | if len(output) == 0 || output[0] != '.' { |
Shinichiro Hamaji | 26fe957 | 2015-04-02 02:31:32 +0900 | [diff] [blame] | 509 | return false |
Shinichiro Hamaji | abc80d2 | 2015-04-02 01:56:32 +0900 | [diff] [blame] | 510 | } |
| 511 | rest := output[1:] |
| 512 | dotIndex := strings.IndexByte(rest, '.') |
| 513 | // If there is only a single dot or the third dot, this is not a |
| 514 | // suffix rule. |
| 515 | if dotIndex < 0 || strings.IndexByte(rest[dotIndex+1:], '.') >= 0 { |
Shinichiro Hamaji | 26fe957 | 2015-04-02 02:31:32 +0900 | [diff] [blame] | 516 | return false |
Shinichiro Hamaji | abc80d2 | 2015-04-02 01:56:32 +0900 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | // This is a suffix rule. |
| 520 | inputSuffix := rest[:dotIndex] |
| 521 | outputSuffix := rest[dotIndex+1:] |
| 522 | r := &Rule{} |
| 523 | *r = *rule |
| 524 | r.inputs = []string{inputSuffix} |
| 525 | r.isSuffixRule = true |
Shinichiro Hamaji | 20a4376 | 2015-04-02 02:42:25 +0900 | [diff] [blame] | 526 | ex.suffixRules[outputSuffix] = append([]*Rule{r}, ex.suffixRules[outputSuffix]...) |
Shinichiro Hamaji | 26fe957 | 2015-04-02 02:31:32 +0900 | [diff] [blame] | 527 | return true |
Shinichiro Hamaji | abc80d2 | 2015-04-02 01:56:32 +0900 | [diff] [blame] | 528 | } |
| 529 | |
Shinichiro Hamaji | d1bd331 | 2015-04-13 18:21:08 +0900 | [diff] [blame] | 530 | func mergeRules(oldRule, rule *Rule, output string, isSuffixRule bool) *Rule { |
Shinichiro Hamaji | ea170b1 | 2015-04-15 10:02:33 +0900 | [diff] [blame^] | 531 | if oldRule == rule { |
| 532 | panic("Merging a same rule") |
| 533 | } |
Shinichiro Hamaji | faeba68 | 2015-04-13 18:11:27 +0900 | [diff] [blame] | 534 | if oldRule.vars != nil || rule.vars != nil { |
| 535 | oldRule.isDoubleColon = rule.isDoubleColon |
| 536 | switch { |
| 537 | case rule.vars == nil && oldRule.vars != nil: |
| 538 | rule.vars = oldRule.vars |
| 539 | case rule.vars != nil && oldRule.vars == nil: |
| 540 | case rule.vars != nil && oldRule.vars != nil: |
| 541 | // parent would be the same vars? |
Shinichiro Hamaji | ea170b1 | 2015-04-15 10:02:33 +0900 | [diff] [blame^] | 542 | rule.vars = append(oldRule.vars, rule.vars...) |
Shinichiro Hamaji | faeba68 | 2015-04-13 18:11:27 +0900 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
Shinichiro Hamaji | d1bd331 | 2015-04-13 18:21:08 +0900 | [diff] [blame] | 546 | if oldRule.isDoubleColon != rule.isDoubleColon { |
| 547 | Error(rule.filename, rule.lineno, "*** target file %q has both : and :: entries.", output) |
| 548 | } |
| 549 | if len(oldRule.cmds) > 0 && len(rule.cmds) > 0 && !isSuffixRule && !rule.isDoubleColon { |
| 550 | Warn(rule.filename, rule.cmdLineno, "overriding commands for target %q", output) |
| 551 | Warn(oldRule.filename, oldRule.cmdLineno, "ignoring old commands for target %q", output) |
| 552 | } |
| 553 | |
Shinichiro Hamaji | faeba68 | 2015-04-13 18:11:27 +0900 | [diff] [blame] | 554 | r := &Rule{} |
| 555 | *r = *rule |
| 556 | if rule.isDoubleColon { |
| 557 | r.cmds = append(oldRule.cmds, r.cmds...) |
| 558 | } else if len(oldRule.cmds) > 0 && len(rule.cmds) == 0 { |
| 559 | r.cmds = oldRule.cmds |
| 560 | } |
| 561 | // If the latter rule has a command (regardless of the |
| 562 | // commands in oldRule), inputs in the latter rule has a |
| 563 | // priority. |
| 564 | if len(rule.cmds) > 0 { |
| 565 | r.inputs = append(r.inputs, oldRule.inputs...) |
| 566 | r.orderOnlyInputs = append(r.orderOnlyInputs, oldRule.orderOnlyInputs...) |
| 567 | } else { |
| 568 | r.inputs = append(oldRule.inputs, r.inputs...) |
| 569 | r.orderOnlyInputs = append(oldRule.orderOnlyInputs, r.orderOnlyInputs...) |
| 570 | } |
| 571 | r.outputPatterns = append(r.outputPatterns, oldRule.outputPatterns...) |
| 572 | return r |
| 573 | } |
| 574 | |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 575 | func (ex *Executor) populateExplicitRule(rule *Rule) { |
Shinichiro Hamaji | ea170b1 | 2015-04-15 10:02:33 +0900 | [diff] [blame^] | 576 | fmt.Printf("populateExplicitRule\n") |
Shinichiro Hamaji | 0b93c86 | 2015-04-07 06:15:15 +0900 | [diff] [blame] | 577 | // It seems rules with no outputs are siliently ignored. |
| 578 | if len(rule.outputs) == 0 { |
| 579 | return |
| 580 | } |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 581 | for _, output := range rule.outputs { |
Fumitoshi Ukai | e1e3444 | 2015-04-08 13:10:05 +0900 | [diff] [blame] | 582 | output = filepath.Clean(output) |
Shinichiro Hamaji | 0b93c86 | 2015-04-07 06:15:15 +0900 | [diff] [blame] | 583 | |
Shinichiro Hamaji | 26fe957 | 2015-04-02 02:31:32 +0900 | [diff] [blame] | 584 | isSuffixRule := ex.populateSuffixRule(rule, output) |
Shinichiro Hamaji | abc80d2 | 2015-04-02 01:56:32 +0900 | [diff] [blame] | 585 | |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 586 | if oldRule, present := ex.rules[output]; present { |
Shinichiro Hamaji | ea170b1 | 2015-04-15 10:02:33 +0900 | [diff] [blame^] | 587 | fmt.Printf("merge\n") |
Shinichiro Hamaji | d1bd331 | 2015-04-13 18:21:08 +0900 | [diff] [blame] | 588 | r := mergeRules(oldRule, rule, output, isSuffixRule) |
Shinichiro Hamaji | ea170b1 | 2015-04-15 10:02:33 +0900 | [diff] [blame^] | 589 | fmt.Printf("merge done\n") |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 590 | ex.rules[output] = r |
| 591 | } else { |
| 592 | ex.rules[output] = rule |
Shinichiro Hamaji | 4a1cfc2 | 2015-04-11 13:54:46 +0900 | [diff] [blame] | 593 | if ex.firstRule == nil && !strings.HasPrefix(output, ".") { |
Shinichiro Hamaji | 26fe957 | 2015-04-02 02:31:32 +0900 | [diff] [blame] | 594 | ex.firstRule = rule |
| 595 | } |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 596 | } |
| 597 | } |
Shinichiro Hamaji | ea170b1 | 2015-04-15 10:02:33 +0900 | [diff] [blame^] | 598 | fmt.Printf("populateExplicitRule done\n") |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | func (ex *Executor) populateImplicitRule(rule *Rule) { |
| 602 | for _, outputPattern := range rule.outputPatterns { |
| 603 | r := &Rule{} |
| 604 | *r = *rule |
| 605 | r.outputPatterns = []string{outputPattern} |
| 606 | ex.implicitRules = append(ex.implicitRules, r) |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | func (ex *Executor) populateRules(er *EvalResult) { |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 611 | for _, rule := range er.rules { |
Fumitoshi Ukai | bac3ccf | 2015-04-08 13:41:22 +0900 | [diff] [blame] | 612 | for i, input := range rule.inputs { |
| 613 | rule.inputs[i] = filepath.Clean(input) |
| 614 | } |
| 615 | for i, orderOnlyInput := range rule.orderOnlyInputs { |
| 616 | rule.orderOnlyInputs[i] = filepath.Clean(orderOnlyInput) |
| 617 | } |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 618 | ex.populateExplicitRule(rule) |
| 619 | |
| 620 | if len(rule.outputs) == 0 { |
| 621 | ex.populateImplicitRule(rule) |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 622 | } |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 623 | } |
| 624 | |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 625 | // Reverse the implicit rule for easier lookup. |
| 626 | for i, r := range ex.implicitRules { |
Fumitoshi Ukai | 8edcb79 | 2015-04-02 11:23:23 +0900 | [diff] [blame] | 627 | if i >= len(ex.implicitRules)/2 { |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 628 | break |
| 629 | } |
Fumitoshi Ukai | 8edcb79 | 2015-04-02 11:23:23 +0900 | [diff] [blame] | 630 | j := len(ex.implicitRules) - i - 1 |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 631 | ex.implicitRules[i] = ex.implicitRules[j] |
| 632 | ex.implicitRules[j] = r |
| 633 | } |
| 634 | } |
| 635 | |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 636 | func (ex *Executor) reportStats() { |
| 637 | if !katiLogFlag && !katiStatsFlag { |
| 638 | return |
| 639 | } |
| 640 | |
| 641 | LogStats("build=%d alreadyDone=%d noRule=%d, upToDate=%d runCommand=%d", |
| 642 | ex.buildCnt, ex.alreadyDoneCnt, ex.noRuleCnt, ex.upToDateCnt, ex.runCommandCnt) |
| 643 | LogStats("explicit=%d implicit=%d suffix=%d explicitWOCmd=%d", |
| 644 | ex.pickExplicitRuleCnt, ex.pickImplicitRuleCnt, ex.pickSuffixRuleCnt, ex.pickExplicitRuleWithoutCmdCnt) |
| 645 | if len(ex.trace) > 1 { |
| 646 | LogStats("trace=%q", ex.trace) |
| 647 | } |
| 648 | } |
| 649 | |
Shinichiro Hamaji | 8843a05 | 2015-04-13 16:46:56 +0900 | [diff] [blame] | 650 | func NewExecutor(er *EvalResult, vars Vars) *Executor { |
| 651 | ex := newExecutor(vars) |
Shinichiro Hamaji | 89551c9 | 2015-04-11 19:33:03 +0900 | [diff] [blame] | 652 | // TODO: We should move this to somewhere around evalCmd so that |
| 653 | // we can handle SHELL in target specific variables. |
Shinichiro Hamaji | de4d3e4 | 2015-04-12 23:14:35 +0900 | [diff] [blame] | 654 | shellVar := ex.vars.Lookup("SHELL") |
Shinichiro Hamaji | 89551c9 | 2015-04-11 19:33:03 +0900 | [diff] [blame] | 655 | ex.shell = shellVar.String() |
| 656 | |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 657 | ex.populateRules(er) |
Shinichiro Hamaji | 8843a05 | 2015-04-13 16:46:56 +0900 | [diff] [blame] | 658 | return ex |
| 659 | } |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 660 | |
Shinichiro Hamaji | 8843a05 | 2015-04-13 16:46:56 +0900 | [diff] [blame] | 661 | func (ex *Executor) Exec(targets []string) error { |
Shinichiro Hamaji | b13f3d5 | 2015-03-30 19:29:44 +0900 | [diff] [blame] | 662 | if len(targets) == 0 { |
Shinichiro Hamaji | 26fe957 | 2015-04-02 02:31:32 +0900 | [diff] [blame] | 663 | if ex.firstRule == nil { |
| 664 | ErrorNoLocation("*** No targets.") |
| 665 | } |
| 666 | targets = append(targets, ex.firstRule.outputs[0]) |
Shinichiro Hamaji | b13f3d5 | 2015-03-30 19:29:44 +0900 | [diff] [blame] | 667 | } |
| 668 | |
Shinichiro Hamaji | de4d3e4 | 2015-04-12 23:14:35 +0900 | [diff] [blame] | 669 | LogStats("%d variables", len(ex.vars)) |
Shinichiro Hamaji | 750988e | 2015-04-12 10:14:32 +0900 | [diff] [blame] | 670 | LogStats("%d explicit rules", len(ex.rules)) |
| 671 | LogStats("%d implicit rules", len(ex.implicitRules)) |
| 672 | LogStats("%d suffix rules", len(ex.suffixRules)) |
| 673 | |
Shinichiro Hamaji | b13f3d5 | 2015-03-30 19:29:44 +0900 | [diff] [blame] | 674 | for _, target := range targets { |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 675 | ex.trace = []string{target} |
Shinichiro Hamaji | de4d3e4 | 2015-04-12 23:14:35 +0900 | [diff] [blame] | 676 | _, err := ex.build(target, "") |
Shinichiro Hamaji | b13f3d5 | 2015-03-30 19:29:44 +0900 | [diff] [blame] | 677 | if err != nil { |
| 678 | return err |
| 679 | } |
| 680 | } |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 681 | ex.reportStats() |
Shinichiro Hamaji | b13f3d5 | 2015-03-30 19:29:44 +0900 | [diff] [blame] | 682 | return nil |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 683 | } |