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