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