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