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