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