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