Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "os/exec" |
Fumitoshi Ukai | 8edcb79 | 2015-04-02 11:23:23 +0900 | [diff] [blame] | 7 | "path/filepath" |
Fumitoshi Ukai | 3e5161e | 2015-04-01 22:42:10 +0900 | [diff] [blame] | 8 | "strings" |
Fumitoshi Ukai | 772fe1a | 2015-04-01 23:28:26 +0900 | [diff] [blame] | 9 | "syscall" |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | type Executor struct { |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 14 | rules map[string]*Rule |
| 15 | implicitRules []*Rule |
Shinichiro Hamaji | 20a4376 | 2015-04-02 02:42:25 +0900 | [diff] [blame] | 16 | suffixRules map[string][]*Rule |
Shinichiro Hamaji | 26fe957 | 2015-04-02 02:31:32 +0900 | [diff] [blame] | 17 | firstRule *Rule |
Fumitoshi Ukai | 4b82f19 | 2015-04-07 11:36:42 +0900 | [diff] [blame] | 18 | |
| 19 | // target -> timestamp |
| 20 | done map[string]int64 |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | func newExecutor() *Executor { |
| 24 | return &Executor{ |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 25 | rules: make(map[string]*Rule), |
Shinichiro Hamaji | 20a4376 | 2015-04-02 02:42:25 +0900 | [diff] [blame] | 26 | suffixRules: make(map[string][]*Rule), |
Fumitoshi Ukai | 4b82f19 | 2015-04-07 11:36:42 +0900 | [diff] [blame] | 27 | done: make(map[string]int64), |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 28 | } |
| 29 | } |
| 30 | |
Fumitoshi Ukai | cf2b038 | 2015-03-30 17:48:54 +0900 | [diff] [blame] | 31 | // TODO(ukai): use time.Time? |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 32 | func getTimestamp(filename string) int64 { |
| 33 | st, err := os.Stat(filename) |
| 34 | if err != nil { |
| 35 | return -2 |
| 36 | } |
| 37 | return st.ModTime().Unix() |
| 38 | } |
| 39 | |
Fumitoshi Ukai | 6b37883 | 2015-04-06 16:54:37 +0900 | [diff] [blame] | 40 | func (ex *Executor) exists(target string) bool { |
Fumitoshi Ukai | f04610d | 2015-04-07 00:03:03 +0900 | [diff] [blame] | 41 | _, present := ex.rules[target] |
| 42 | if present { |
| 43 | return true |
| 44 | } |
Fumitoshi Ukai | 6b37883 | 2015-04-06 16:54:37 +0900 | [diff] [blame] | 45 | rule, present := ex.rules[".PHONY"] |
| 46 | if present { |
| 47 | for _, input := range rule.inputs { |
| 48 | if target == input { |
| 49 | return true |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | return exists(target) |
| 54 | } |
| 55 | |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 56 | type runner struct { |
| 57 | output string |
| 58 | cmd string |
| 59 | echo bool |
| 60 | dryRun bool |
| 61 | ignoreError bool |
| 62 | } |
| 63 | |
| 64 | func evalCmd(ev *Evaluator, r runner, s string) []runner { |
| 65 | r = newRunner(r, s) |
| 66 | if strings.IndexByte(r.cmd, '$') < 0 { |
| 67 | // fast path |
| 68 | return []runner{r} |
| 69 | } |
| 70 | cmds := ev.evalExpr(r.cmd) |
| 71 | var runners []runner |
| 72 | for _, cmd := range strings.Split(cmds, "\n") { |
| 73 | runners = append(runners, newRunner(r, cmd)) |
| 74 | } |
| 75 | return runners |
| 76 | } |
| 77 | |
| 78 | func newRunner(r runner, s string) runner { |
| 79 | for { |
| 80 | s = strings.TrimLeft(s, " \t") |
| 81 | if s == "" { |
| 82 | return runner{} |
Fumitoshi Ukai | 772fe1a | 2015-04-01 23:28:26 +0900 | [diff] [blame] | 83 | } |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 84 | switch s[0] { |
| 85 | case '@': |
| 86 | if !r.dryRun { |
| 87 | r.echo = false |
| 88 | } |
| 89 | s = s[1:] |
| 90 | continue |
| 91 | case '-': |
| 92 | r.ignoreError = true |
| 93 | s = s[1:] |
Shinichiro Hamaji | 07e146b | 2015-04-02 16:17:24 +0900 | [diff] [blame] | 94 | continue |
| 95 | } |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 96 | break |
| 97 | } |
| 98 | r.cmd = s |
| 99 | return r |
| 100 | } |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 101 | |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 102 | func (r runner) run() error { |
| 103 | if r.echo { |
| 104 | fmt.Printf("%s\n", r.cmd) |
| 105 | } |
| 106 | if r.dryRun { |
| 107 | return nil |
| 108 | } |
| 109 | args := []string{"/bin/sh", "-c", r.cmd} |
| 110 | cmd := exec.Cmd{ |
| 111 | Path: args[0], |
| 112 | Args: args, |
| 113 | } |
| 114 | out, err := cmd.CombinedOutput() |
| 115 | fmt.Printf("%s", out) |
| 116 | exit := exitStatus(err) |
| 117 | if r.ignoreError && exit != 0 { |
| 118 | fmt.Printf("[%s] Error %d (ignored)\n", r.output, exit) |
| 119 | err = nil |
| 120 | } |
| 121 | return err |
| 122 | } |
| 123 | |
| 124 | func exitStatus(err error) int { |
| 125 | if err == nil { |
| 126 | return 0 |
| 127 | } |
| 128 | exit := 1 |
| 129 | if err, ok := err.(*exec.ExitError); ok { |
| 130 | if w, ok := err.ProcessState.Sys().(syscall.WaitStatus); ok { |
| 131 | return w.ExitStatus() |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 132 | } |
| 133 | } |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 134 | return exit |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 135 | } |
| 136 | |
Shinichiro Hamaji | 20a4376 | 2015-04-02 02:42:25 +0900 | [diff] [blame] | 137 | func replaceSuffix(s string, newsuf string) string { |
| 138 | // TODO: Factor out the logic around suffix rules and use |
| 139 | // it from substitution references. |
| 140 | // http://www.gnu.org/software/make/manual/make.html#Substitution-Refs |
Shinichiro Hamaji | 5e2c3c7 | 2015-04-03 15:04:54 +0900 | [diff] [blame] | 141 | return fmt.Sprintf("%s.%s", stripExt(s), newsuf) |
Shinichiro Hamaji | 20a4376 | 2015-04-02 02:42:25 +0900 | [diff] [blame] | 142 | } |
| 143 | |
Shinichiro Hamaji | 0d0af5b | 2015-04-02 05:17:23 +0900 | [diff] [blame] | 144 | func (ex *Executor) canPickImplicitRule(rule *Rule, output string) bool { |
| 145 | outputPattern := rule.outputPatterns[0] |
| 146 | if !matchPattern(outputPattern, output) { |
| 147 | return false |
| 148 | } |
| 149 | for _, input := range rule.inputs { |
| 150 | input = substPattern(outputPattern, input, output) |
Fumitoshi Ukai | 6b37883 | 2015-04-06 16:54:37 +0900 | [diff] [blame] | 151 | if !ex.exists(input) { |
Shinichiro Hamaji | 0d0af5b | 2015-04-02 05:17:23 +0900 | [diff] [blame] | 152 | return false |
| 153 | } |
| 154 | } |
| 155 | return true |
| 156 | } |
| 157 | |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 158 | func (ex *Executor) pickRule(output string) (*Rule, bool) { |
| 159 | rule, present := ex.rules[output] |
| 160 | if present { |
Fumitoshi Ukai | d92c487 | 2015-04-06 16:17:31 +0900 | [diff] [blame] | 161 | if len(rule.cmds) > 0 { |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 162 | return rule, true |
| 163 | } |
Fumitoshi Ukai | d92c487 | 2015-04-06 16:17:31 +0900 | [diff] [blame] | 164 | // If none of the explicit rules for a target has commands, |
| 165 | // then `make' searches for an applicable implicit rule to |
| 166 | // find some commands. |
| 167 | } |
| 168 | |
| 169 | for _, irule := range ex.implicitRules { |
| 170 | if !ex.canPickImplicitRule(irule, output) { |
| 171 | continue |
| 172 | } |
| 173 | if rule != nil { |
| 174 | r := &Rule{} |
| 175 | *r = *rule |
| 176 | r.outputPatterns = irule.outputPatterns |
| 177 | // implicit rule's prerequisites will be used for $< |
| 178 | r.inputs = append(irule.inputs, r.inputs...) |
| 179 | if irule.vars != nil { |
| 180 | r.vars = NewVarTab(rule.vars) |
| 181 | for k, v := range irule.vars.m { |
| 182 | r.vars.Assign(k, v) |
| 183 | } |
| 184 | } |
| 185 | r.cmds = irule.cmds |
| 186 | // TODO(ukai): filename, lineno? |
| 187 | r.cmdLineno = irule.cmdLineno |
| 188 | return r, true |
| 189 | } |
| 190 | // TODO(ukai): check len(irule.cmd) ? |
| 191 | return irule, true |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 192 | } |
| 193 | |
Shinichiro Hamaji | abc80d2 | 2015-04-02 01:56:32 +0900 | [diff] [blame] | 194 | outputSuffix := filepath.Ext(output) |
Fumitoshi Ukai | d92c487 | 2015-04-06 16:17:31 +0900 | [diff] [blame] | 195 | if !strings.HasPrefix(outputSuffix, ".") { |
| 196 | return rule, rule != nil |
Shinichiro Hamaji | abc80d2 | 2015-04-02 01:56:32 +0900 | [diff] [blame] | 197 | } |
Fumitoshi Ukai | d92c487 | 2015-04-06 16:17:31 +0900 | [diff] [blame] | 198 | rules, present := ex.suffixRules[outputSuffix[1:]] |
| 199 | if !present { |
| 200 | return rule, rule != nil |
| 201 | } |
| 202 | for _, irule := range rules { |
| 203 | if len(irule.inputs) != 1 { |
| 204 | panic(fmt.Sprintf("unexpected number of input for a suffix rule (%d)", len(irule.inputs))) |
| 205 | } |
Fumitoshi Ukai | 6b37883 | 2015-04-06 16:54:37 +0900 | [diff] [blame] | 206 | if !ex.exists(replaceSuffix(output, irule.inputs[0])) { |
Fumitoshi Ukai | d92c487 | 2015-04-06 16:17:31 +0900 | [diff] [blame] | 207 | continue |
| 208 | } |
| 209 | if rule != nil { |
| 210 | r := &Rule{} |
| 211 | *r = *rule |
| 212 | // TODO(ukai): input order is correct? |
| 213 | r.inputs = append([]string{replaceSuffix(output, irule.inputs[0])}, r.inputs...) |
| 214 | r.vars = NewVarTab(rule.vars) |
Fumitoshi Ukai | 6b37883 | 2015-04-06 16:54:37 +0900 | [diff] [blame] | 215 | if irule.vars != nil { |
| 216 | for k, v := range irule.vars.m { |
| 217 | r.vars.Assign(k, v) |
| 218 | } |
Fumitoshi Ukai | d92c487 | 2015-04-06 16:17:31 +0900 | [diff] [blame] | 219 | } |
| 220 | r.cmds = irule.cmds |
| 221 | // TODO(ukai): filename, lineno? |
| 222 | r.cmdLineno = irule.cmdLineno |
| 223 | return r, true |
| 224 | } |
| 225 | // TODO(ukai): check len(irule.cmd) ? |
| 226 | return irule, true |
| 227 | } |
| 228 | return rule, rule != nil |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 229 | } |
| 230 | |
Fumitoshi Ukai | 8edcb79 | 2015-04-02 11:23:23 +0900 | [diff] [blame] | 231 | func (ex *Executor) build(vars *VarTab, output string) (int64, error) { |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 232 | Log("Building: %s", output) |
Fumitoshi Ukai | 4b82f19 | 2015-04-07 11:36:42 +0900 | [diff] [blame] | 233 | outputTs, ok := ex.done[output] |
| 234 | if ok { |
| 235 | Log("Building: %s already done: %d", outputTs) |
| 236 | return outputTs, nil |
| 237 | } |
| 238 | outputTs = getTimestamp(output) |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 239 | |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 240 | rule, present := ex.pickRule(output) |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 241 | if !present { |
Fumitoshi Ukai | cf2b038 | 2015-03-30 17:48:54 +0900 | [diff] [blame] | 242 | if outputTs >= 0 { |
Fumitoshi Ukai | f8efa0a | 2015-03-30 18:10:11 +0900 | [diff] [blame] | 243 | return outputTs, nil |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 244 | } |
Fumitoshi Ukai | f8efa0a | 2015-03-30 18:10:11 +0900 | [diff] [blame] | 245 | return outputTs, fmt.Errorf("no rule to make target %q", output) |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 246 | } |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 247 | if rule.vars != nil { |
| 248 | vars = NewVarTab(vars) |
| 249 | for k, v := range rule.vars.m { |
| 250 | vars.Assign(k, v) |
| 251 | } |
| 252 | } |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 253 | |
| 254 | latest := int64(-1) |
Shinichiro Hamaji | 57aff98 | 2015-04-02 02:15:38 +0900 | [diff] [blame] | 255 | var actualInputs []string |
Fumitoshi Ukai | d92c487 | 2015-04-06 16:17:31 +0900 | [diff] [blame] | 256 | Log("Building: %s inputs:%q", output, rule.inputs) |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 257 | for _, input := range rule.inputs { |
Shinichiro Hamaji | 7b203b2 | 2015-04-01 03:44:57 +0900 | [diff] [blame] | 258 | if len(rule.outputPatterns) > 0 { |
| 259 | if len(rule.outputPatterns) > 1 { |
| 260 | panic("TODO: multiple output pattern is not supported yet") |
| 261 | } |
| 262 | input = substPattern(rule.outputPatterns[0], input, output) |
Shinichiro Hamaji | abc80d2 | 2015-04-02 01:56:32 +0900 | [diff] [blame] | 263 | } else if rule.isSuffixRule { |
Shinichiro Hamaji | 20a4376 | 2015-04-02 02:42:25 +0900 | [diff] [blame] | 264 | input = replaceSuffix(output, input) |
Shinichiro Hamaji | 7b203b2 | 2015-04-01 03:44:57 +0900 | [diff] [blame] | 265 | } |
Shinichiro Hamaji | 57aff98 | 2015-04-02 02:15:38 +0900 | [diff] [blame] | 266 | actualInputs = append(actualInputs, input) |
Shinichiro Hamaji | 7b203b2 | 2015-04-01 03:44:57 +0900 | [diff] [blame] | 267 | |
Fumitoshi Ukai | 3e5161e | 2015-04-01 22:42:10 +0900 | [diff] [blame] | 268 | ts, err := ex.build(vars, input) |
Fumitoshi Ukai | f8efa0a | 2015-03-30 18:10:11 +0900 | [diff] [blame] | 269 | if err != nil { |
| 270 | return outputTs, err |
| 271 | } |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 272 | if latest < ts { |
| 273 | latest = ts |
| 274 | } |
| 275 | } |
| 276 | |
Shinichiro Hamaji | 5c53b57 | 2015-04-02 05:36:42 +0900 | [diff] [blame] | 277 | for _, input := range rule.orderOnlyInputs { |
Fumitoshi Ukai | f04610d | 2015-04-07 00:03:03 +0900 | [diff] [blame] | 278 | if exists(input) { |
Shinichiro Hamaji | 5c53b57 | 2015-04-02 05:36:42 +0900 | [diff] [blame] | 279 | continue |
| 280 | } |
| 281 | ts, err := ex.build(vars, input) |
| 282 | if err != nil { |
| 283 | return outputTs, err |
| 284 | } |
| 285 | if latest < ts { |
| 286 | latest = ts |
| 287 | } |
| 288 | } |
| 289 | |
Fumitoshi Ukai | cf2b038 | 2015-03-30 17:48:54 +0900 | [diff] [blame] | 290 | if outputTs >= latest { |
Fumitoshi Ukai | f8efa0a | 2015-03-30 18:10:11 +0900 | [diff] [blame] | 291 | return outputTs, nil |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 292 | } |
| 293 | |
Fumitoshi Ukai | 8edcb79 | 2015-04-02 11:23:23 +0900 | [diff] [blame] | 294 | localVars := NewVarTab(vars) |
Fumitoshi Ukai | 3e5161e | 2015-04-01 22:42:10 +0900 | [diff] [blame] | 295 | // automatic variables. |
Fumitoshi Ukai | 8edcb79 | 2015-04-02 11:23:23 +0900 | [diff] [blame] | 296 | localVars.Assign("@", SimpleVar{value: output, origin: "automatic"}) |
Shinichiro Hamaji | 57aff98 | 2015-04-02 02:15:38 +0900 | [diff] [blame] | 297 | if len(actualInputs) > 0 { |
Fumitoshi Ukai | 8edcb79 | 2015-04-02 11:23:23 +0900 | [diff] [blame] | 298 | localVars.Assign("<", SimpleVar{ |
| 299 | value: actualInputs[0], |
| 300 | origin: "automatic", |
| 301 | }) |
| 302 | localVars.Assign("^", SimpleVar{ |
| 303 | value: strings.Join(actualInputs, " "), |
| 304 | origin: "automatic", |
| 305 | }) |
Shinichiro Hamaji | 57aff98 | 2015-04-02 02:15:38 +0900 | [diff] [blame] | 306 | } |
Fumitoshi Ukai | 3e5161e | 2015-04-01 22:42:10 +0900 | [diff] [blame] | 307 | ev := newEvaluator(localVars) |
Shinichiro Hamaji | e708a9d | 2015-04-03 14:34:35 +0900 | [diff] [blame] | 308 | ev.filename = rule.filename |
| 309 | ev.lineno = rule.cmdLineno |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 310 | var runners []runner |
Fumitoshi Ukai | f04610d | 2015-04-07 00:03:03 +0900 | [diff] [blame] | 311 | Log("Building: %s cmds:%q", output, rule.cmds) |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 312 | r := runner{ |
| 313 | output: output, |
| 314 | echo: true, |
| 315 | dryRun: dryRunFlag, |
Fumitoshi Ukai | 3e5161e | 2015-04-01 22:42:10 +0900 | [diff] [blame] | 316 | } |
Fumitoshi Ukai | 457e7ba | 2015-04-08 11:24:57 +0900 | [diff] [blame] | 317 | for _, cmd := range rule.cmds { |
| 318 | runners = append(runners, evalCmd(ev, r, cmd)...) |
| 319 | } |
| 320 | for _, r := range runners { |
| 321 | err := r.run() |
| 322 | if err != nil { |
| 323 | exit := exitStatus(err) |
| 324 | fmt.Printf("[%s] Error %d: %v\n", r.output, exit, err) |
| 325 | return outputTs, err |
| 326 | } |
Fumitoshi Ukai | f8efa0a | 2015-03-30 18:10:11 +0900 | [diff] [blame] | 327 | } |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 328 | |
Fumitoshi Ukai | cf2b038 | 2015-03-30 17:48:54 +0900 | [diff] [blame] | 329 | outputTs = getTimestamp(output) |
| 330 | if outputTs < 0 { |
| 331 | outputTs = time.Now().Unix() |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 332 | } |
Fumitoshi Ukai | 4b82f19 | 2015-04-07 11:36:42 +0900 | [diff] [blame] | 333 | ex.done[output] = outputTs |
| 334 | Log("Building: %s done %d", output, outputTs) |
Fumitoshi Ukai | f8efa0a | 2015-03-30 18:10:11 +0900 | [diff] [blame] | 335 | return outputTs, nil |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 336 | } |
| 337 | |
Shinichiro Hamaji | 26fe957 | 2015-04-02 02:31:32 +0900 | [diff] [blame] | 338 | func (ex *Executor) populateSuffixRule(rule *Rule, output string) bool { |
Shinichiro Hamaji | abc80d2 | 2015-04-02 01:56:32 +0900 | [diff] [blame] | 339 | if len(output) == 0 || output[0] != '.' { |
Shinichiro Hamaji | 26fe957 | 2015-04-02 02:31:32 +0900 | [diff] [blame] | 340 | return false |
Shinichiro Hamaji | abc80d2 | 2015-04-02 01:56:32 +0900 | [diff] [blame] | 341 | } |
| 342 | rest := output[1:] |
| 343 | dotIndex := strings.IndexByte(rest, '.') |
| 344 | // If there is only a single dot or the third dot, this is not a |
| 345 | // suffix rule. |
| 346 | if dotIndex < 0 || strings.IndexByte(rest[dotIndex+1:], '.') >= 0 { |
Shinichiro Hamaji | 26fe957 | 2015-04-02 02:31:32 +0900 | [diff] [blame] | 347 | return false |
Shinichiro Hamaji | abc80d2 | 2015-04-02 01:56:32 +0900 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | // This is a suffix rule. |
| 351 | inputSuffix := rest[:dotIndex] |
| 352 | outputSuffix := rest[dotIndex+1:] |
| 353 | r := &Rule{} |
| 354 | *r = *rule |
| 355 | r.inputs = []string{inputSuffix} |
| 356 | r.isSuffixRule = true |
Shinichiro Hamaji | 20a4376 | 2015-04-02 02:42:25 +0900 | [diff] [blame] | 357 | ex.suffixRules[outputSuffix] = append([]*Rule{r}, ex.suffixRules[outputSuffix]...) |
Shinichiro Hamaji | 26fe957 | 2015-04-02 02:31:32 +0900 | [diff] [blame] | 358 | return true |
Shinichiro Hamaji | abc80d2 | 2015-04-02 01:56:32 +0900 | [diff] [blame] | 359 | } |
| 360 | |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 361 | func (ex *Executor) populateExplicitRule(rule *Rule) { |
Shinichiro Hamaji | 0b93c86 | 2015-04-07 06:15:15 +0900 | [diff] [blame] | 362 | // It seems rules with no outputs are siliently ignored. |
| 363 | if len(rule.outputs) == 0 { |
| 364 | return |
| 365 | } |
Fumitoshi Ukai | e1e3444 | 2015-04-08 13:10:05 +0900 | [diff] [blame^] | 366 | for i, input := range rule.inputs { |
| 367 | rule.inputs[i] = filepath.Clean(input) |
| 368 | } |
| 369 | for i, orderOnlyInput := range rule.orderOnlyInputs { |
| 370 | rule.orderOnlyInputs[i] = filepath.Clean(orderOnlyInput) |
| 371 | } |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 372 | for _, output := range rule.outputs { |
Fumitoshi Ukai | e1e3444 | 2015-04-08 13:10:05 +0900 | [diff] [blame^] | 373 | output = filepath.Clean(output) |
Shinichiro Hamaji | 0b93c86 | 2015-04-07 06:15:15 +0900 | [diff] [blame] | 374 | |
Shinichiro Hamaji | 26fe957 | 2015-04-02 02:31:32 +0900 | [diff] [blame] | 375 | isSuffixRule := ex.populateSuffixRule(rule, output) |
Shinichiro Hamaji | abc80d2 | 2015-04-02 01:56:32 +0900 | [diff] [blame] | 376 | |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 377 | if oldRule, present := ex.rules[output]; present { |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 378 | if oldRule.vars != nil || rule.vars != nil { |
| 379 | oldRule.isDoubleColon = rule.isDoubleColon |
| 380 | switch { |
| 381 | case rule.vars == nil && oldRule.vars != nil: |
| 382 | rule.vars = oldRule.vars |
| 383 | case rule.vars != nil && oldRule.vars == nil: |
| 384 | case rule.vars != nil && oldRule.vars != nil: |
| 385 | // parent would be the same vars? |
| 386 | for k, v := range rule.vars.m { |
| 387 | oldRule.vars.m[k] = v |
| 388 | } |
| 389 | rule.vars = oldRule.vars |
| 390 | } |
| 391 | } |
Shinichiro Hamaji | cc3abc3 | 2015-04-02 04:46:49 +0900 | [diff] [blame] | 392 | if oldRule.isDoubleColon != rule.isDoubleColon { |
| 393 | Error(rule.filename, rule.lineno, "*** target file %q has both : and :: entries.", output) |
| 394 | } |
| 395 | if len(oldRule.cmds) > 0 && len(rule.cmds) > 0 && !isSuffixRule && !rule.isDoubleColon { |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 396 | Warn(rule.filename, rule.cmdLineno, "overriding commands for target %q", output) |
| 397 | Warn(oldRule.filename, oldRule.cmdLineno, "ignoring old commands for target %q", output) |
| 398 | } |
| 399 | r := &Rule{} |
| 400 | *r = *rule |
Shinichiro Hamaji | cc3abc3 | 2015-04-02 04:46:49 +0900 | [diff] [blame] | 401 | if rule.isDoubleColon { |
| 402 | r.cmds = append(oldRule.cmds, r.cmds...) |
Fumitoshi Ukai | c97f49c | 2015-04-07 00:16:38 +0900 | [diff] [blame] | 403 | } else if len(oldRule.cmds) > 0 && len(rule.cmds) == 0 { |
| 404 | r.cmds = oldRule.cmds |
Shinichiro Hamaji | cc3abc3 | 2015-04-02 04:46:49 +0900 | [diff] [blame] | 405 | } |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 406 | r.inputs = append(r.inputs, oldRule.inputs...) |
Fumitoshi Ukai | 79fcf97 | 2015-04-07 11:10:29 +0900 | [diff] [blame] | 407 | r.orderOnlyInputs = append(r.orderOnlyInputs, oldRule.orderOnlyInputs...) |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 408 | ex.rules[output] = r |
| 409 | } else { |
| 410 | ex.rules[output] = rule |
Shinichiro Hamaji | 26fe957 | 2015-04-02 02:31:32 +0900 | [diff] [blame] | 411 | if ex.firstRule == nil && !isSuffixRule { |
| 412 | ex.firstRule = rule |
| 413 | } |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | func (ex *Executor) populateImplicitRule(rule *Rule) { |
| 419 | for _, outputPattern := range rule.outputPatterns { |
| 420 | r := &Rule{} |
| 421 | *r = *rule |
| 422 | r.outputPatterns = []string{outputPattern} |
| 423 | ex.implicitRules = append(ex.implicitRules, r) |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | func (ex *Executor) populateRules(er *EvalResult) { |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 428 | for _, rule := range er.rules { |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 429 | ex.populateExplicitRule(rule) |
| 430 | |
| 431 | if len(rule.outputs) == 0 { |
| 432 | ex.populateImplicitRule(rule) |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 433 | } |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 434 | } |
| 435 | |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 436 | // Reverse the implicit rule for easier lookup. |
| 437 | for i, r := range ex.implicitRules { |
Fumitoshi Ukai | 8edcb79 | 2015-04-02 11:23:23 +0900 | [diff] [blame] | 438 | if i >= len(ex.implicitRules)/2 { |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 439 | break |
| 440 | } |
Fumitoshi Ukai | 8edcb79 | 2015-04-02 11:23:23 +0900 | [diff] [blame] | 441 | j := len(ex.implicitRules) - i - 1 |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 442 | ex.implicitRules[i] = ex.implicitRules[j] |
| 443 | ex.implicitRules[j] = r |
| 444 | } |
| 445 | } |
| 446 | |
Shinichiro Hamaji | 574a4ef | 2015-04-06 15:20:28 +0900 | [diff] [blame] | 447 | func (ex *Executor) exec(er *EvalResult, targets []string, vars *VarTab) error { |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 448 | ex.populateRules(er) |
| 449 | |
Shinichiro Hamaji | b13f3d5 | 2015-03-30 19:29:44 +0900 | [diff] [blame] | 450 | if len(targets) == 0 { |
Shinichiro Hamaji | 26fe957 | 2015-04-02 02:31:32 +0900 | [diff] [blame] | 451 | if ex.firstRule == nil { |
| 452 | ErrorNoLocation("*** No targets.") |
| 453 | } |
| 454 | targets = append(targets, ex.firstRule.outputs[0]) |
Shinichiro Hamaji | b13f3d5 | 2015-03-30 19:29:44 +0900 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | for _, target := range targets { |
Shinichiro Hamaji | 574a4ef | 2015-04-06 15:20:28 +0900 | [diff] [blame] | 458 | _, err := ex.build(vars, target) |
Shinichiro Hamaji | b13f3d5 | 2015-03-30 19:29:44 +0900 | [diff] [blame] | 459 | if err != nil { |
| 460 | return err |
| 461 | } |
| 462 | } |
| 463 | return nil |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 464 | } |
| 465 | |
Shinichiro Hamaji | 574a4ef | 2015-04-06 15:20:28 +0900 | [diff] [blame] | 466 | func Exec(er *EvalResult, targets []string, vars *VarTab) error { |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 467 | ex := newExecutor() |
Shinichiro Hamaji | 574a4ef | 2015-04-06 15:20:28 +0900 | [diff] [blame] | 468 | return ex.exec(er, targets, vars) |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 469 | } |