Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Shinichiro Hamaji | b0f6548 | 2015-04-16 18:28:05 +0900 | [diff] [blame] | 4 | "bytes" |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 5 | "fmt" |
Shinichiro Hamaji | b0f6548 | 2015-04-16 18:28:05 +0900 | [diff] [blame] | 6 | "io" |
Shinichiro Hamaji | b0f6548 | 2015-04-16 18:28:05 +0900 | [diff] [blame] | 7 | "path/filepath" |
| 8 | "strings" |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 9 | "sync" |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 10 | ) |
| 11 | |
| 12 | type Executor struct { |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 13 | rules map[string]*Rule |
| 14 | implicitRules []*Rule |
Shinichiro Hamaji | 20a4376 | 2015-04-02 02:42:25 +0900 | [diff] [blame] | 15 | suffixRules map[string][]*Rule |
Shinichiro Hamaji | 26fe957 | 2015-04-02 02:31:32 +0900 | [diff] [blame] | 16 | firstRule *Rule |
Shinichiro Hamaji | 89551c9 | 2015-04-11 19:33:03 +0900 | [diff] [blame] | 17 | shell string |
Shinichiro Hamaji | de4d3e4 | 2015-04-12 23:14:35 +0900 | [diff] [blame] | 18 | vars Vars |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 19 | varsLock sync.Mutex |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 20 | // target -> Job, nil means the target is currently being processed. |
| 21 | done map[string]*Job |
| 22 | |
| 23 | wm *WorkerManager |
Shinichiro Hamaji | 110af33 | 2015-04-13 19:04:50 +0900 | [diff] [blame] | 24 | |
Shinichiro Hamaji | b0f6548 | 2015-04-16 18:28:05 +0900 | [diff] [blame] | 25 | currentOutput string |
| 26 | currentInputs []string |
| 27 | currentStem string |
| 28 | |
Shinichiro Hamaji | 17a8a6e | 2015-04-20 21:44:42 +0900 | [diff] [blame] | 29 | trace []string |
| 30 | buildCnt int |
| 31 | alreadyDoneCnt int |
| 32 | noRuleCnt int |
| 33 | upToDateCnt int |
| 34 | runCommandCnt int |
Shinichiro Hamaji | 110af33 | 2015-04-13 19:04:50 +0900 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | type AutoVar struct{ ex *Executor } |
| 38 | |
| 39 | func (v AutoVar) Flavor() string { return "undefined" } |
| 40 | func (v AutoVar) Origin() string { return "automatic" } |
| 41 | func (v AutoVar) IsDefined() bool { panic("not implemented") } |
| 42 | func (v AutoVar) String() string { panic("not implemented") } |
Shinichiro Hamaji | ea170b1 | 2015-04-15 10:02:33 +0900 | [diff] [blame] | 43 | func (v AutoVar) Append(*Evaluator, string) Var { |
| 44 | panic("must not be called") |
| 45 | } |
Shinichiro Hamaji | 7825b65 | 2015-06-04 13:47:14 +0900 | [diff] [blame] | 46 | func (v AutoVar) AppendVar(*Evaluator, Value) Var { |
Shinichiro Hamaji | 110af33 | 2015-04-13 19:04:50 +0900 | [diff] [blame] | 47 | panic("must not be called") |
| 48 | } |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 49 | func (v AutoVar) Serialize() SerializableVar { |
| 50 | panic(fmt.Sprintf("cannot serialize auto var: %q", v)) |
| 51 | } |
Shinichiro Hamaji | 723f56a | 2015-05-15 17:12:55 +0900 | [diff] [blame] | 52 | func (v AutoVar) Dump(w io.Writer) { |
| 53 | panic(fmt.Sprintf("cannot dump auto var: %q", v)) |
| 54 | } |
Shinichiro Hamaji | 110af33 | 2015-04-13 19:04:50 +0900 | [diff] [blame] | 55 | |
| 56 | type AutoAtVar struct{ AutoVar } |
| 57 | |
| 58 | func (v AutoAtVar) Eval(w io.Writer, ev *Evaluator) { |
| 59 | fmt.Fprint(w, v.ex.currentOutput) |
| 60 | } |
| 61 | |
| 62 | type AutoLessVar struct{ AutoVar } |
| 63 | |
| 64 | func (v AutoLessVar) Eval(w io.Writer, ev *Evaluator) { |
| 65 | if len(v.ex.currentInputs) > 0 { |
| 66 | fmt.Fprint(w, v.ex.currentInputs[0]) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | type AutoHatVar struct{ AutoVar } |
| 71 | |
| 72 | func (v AutoHatVar) Eval(w io.Writer, ev *Evaluator) { |
Shinichiro Hamaji | a622c5e | 2015-04-13 19:09:30 +0900 | [diff] [blame] | 73 | var uniqueInputs []string |
| 74 | seen := make(map[string]bool) |
| 75 | for _, input := range v.ex.currentInputs { |
| 76 | if !seen[input] { |
| 77 | seen[input] = true |
| 78 | uniqueInputs = append(uniqueInputs, input) |
| 79 | } |
| 80 | } |
| 81 | fmt.Fprint(w, strings.Join(uniqueInputs, " ")) |
| 82 | } |
| 83 | |
| 84 | type AutoPlusVar struct{ AutoVar } |
| 85 | |
| 86 | func (v AutoPlusVar) Eval(w io.Writer, ev *Evaluator) { |
Shinichiro Hamaji | 110af33 | 2015-04-13 19:04:50 +0900 | [diff] [blame] | 87 | fmt.Fprint(w, strings.Join(v.ex.currentInputs, " ")) |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 88 | } |
| 89 | |
Shinichiro Hamaji | bb9772d | 2015-04-13 21:41:22 +0900 | [diff] [blame] | 90 | type AutoStarVar struct{ AutoVar } |
| 91 | |
| 92 | func (v AutoStarVar) Eval(w io.Writer, ev *Evaluator) { |
| 93 | // TODO: Use currentStem. See auto_stem_var.mk |
| 94 | fmt.Fprint(w, stripExt(v.ex.currentOutput)) |
| 95 | } |
| 96 | |
Shinichiro Hamaji | ee8b33c | 2015-04-13 20:08:38 +0900 | [diff] [blame] | 97 | type AutoSuffixDVar struct { |
| 98 | AutoVar |
| 99 | v Var |
| 100 | } |
| 101 | |
| 102 | func (v AutoSuffixDVar) Eval(w io.Writer, ev *Evaluator) { |
| 103 | var buf bytes.Buffer |
| 104 | v.v.Eval(&buf, ev) |
| 105 | for i, tok := range splitSpaces(buf.String()) { |
| 106 | if i > 0 { |
| 107 | w.Write([]byte{' '}) |
| 108 | } |
| 109 | fmt.Fprint(w, filepath.Dir(tok)) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | type AutoSuffixFVar struct { |
| 114 | AutoVar |
| 115 | v Var |
| 116 | } |
| 117 | |
| 118 | func (v AutoSuffixFVar) Eval(w io.Writer, ev *Evaluator) { |
| 119 | var buf bytes.Buffer |
| 120 | v.v.Eval(&buf, ev) |
| 121 | for i, tok := range splitSpaces(buf.String()) { |
| 122 | if i > 0 { |
| 123 | w.Write([]byte{' '}) |
| 124 | } |
| 125 | fmt.Fprint(w, filepath.Base(tok)) |
| 126 | } |
| 127 | } |
| 128 | |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 129 | func (ex *Executor) makeJobs(n *DepNode, neededBy *Job) error { |
Shinichiro Hamaji | 17a8a6e | 2015-04-20 21:44:42 +0900 | [diff] [blame] | 130 | output := n.Output |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 131 | if neededBy != nil { |
| 132 | Log("MakeJob: %s for %s", output, neededBy.n.Output) |
| 133 | } |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 134 | ex.buildCnt++ |
| 135 | if ex.buildCnt%100 == 0 { |
| 136 | ex.reportStats() |
| 137 | } |
| 138 | |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 139 | j, present := ex.done[output] |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 140 | |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 141 | if present { |
| 142 | if j == nil { |
| 143 | if !n.IsPhony { |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 144 | fmt.Printf("Circular %s <- %s dependency dropped.\n", neededBy.n.Output, n.Output) |
| 145 | } |
| 146 | if neededBy != nil { |
| 147 | neededBy.numDeps-- |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 148 | } |
Shinichiro Hamaji | eb4da7f | 2015-04-11 20:28:54 +0900 | [diff] [blame] | 149 | } else { |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 150 | Log("%s already done: %d", j.n.Output, j.outputTs) |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 151 | if neededBy != nil { |
Shinichiro Hamaji | 55c50bd | 2015-04-27 21:05:47 +0900 | [diff] [blame] | 152 | ex.wm.ReportNewDep(j, neededBy) |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 153 | } |
Shinichiro Hamaji | eb4da7f | 2015-04-11 20:28:54 +0900 | [diff] [blame] | 154 | } |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 155 | return nil |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 156 | } |
Shinichiro Hamaji | 4e9ab1a | 2015-04-12 01:31:58 +0900 | [diff] [blame] | 157 | |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 158 | j = &Job{ |
| 159 | n: n, |
| 160 | ex: ex, |
| 161 | numDeps: len(n.Deps), |
| 162 | depsTs: int64(-1), |
| 163 | } |
| 164 | if neededBy != nil { |
| 165 | j.parents = append(j.parents, neededBy) |
| 166 | } |
| 167 | |
| 168 | ex.done[output] = nil |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 169 | // We iterate n.Deps twice. In the first run, we may modify |
| 170 | // numDeps. There will be a race if we do so after the first |
| 171 | // ex.makeJobs(d, j). |
| 172 | var deps []*DepNode |
Shinichiro Hamaji | 17a8a6e | 2015-04-20 21:44:42 +0900 | [diff] [blame] | 173 | for _, d := range n.Deps { |
| 174 | if d.IsOrderOnly && exists(d.Output) { |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 175 | j.numDeps-- |
Shinichiro Hamaji | 5c53b57 | 2015-04-02 05:36:42 +0900 | [diff] [blame] | 176 | continue |
| 177 | } |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 178 | deps = append(deps, d) |
| 179 | } |
Shinichiro Hamaji | 8e4dd9d | 2015-05-15 00:04:44 +0900 | [diff] [blame] | 180 | Log("new: %s (%d)", j.n.Output, j.numDeps) |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 181 | |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 182 | for _, d := range deps { |
Shinichiro Hamaji | 17a8a6e | 2015-04-20 21:44:42 +0900 | [diff] [blame] | 183 | ex.trace = append(ex.trace, d.Output) |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 184 | err := ex.makeJobs(d, j) |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 185 | ex.trace = ex.trace[0 : len(ex.trace)-1] |
Shinichiro Hamaji | 5c53b57 | 2015-04-02 05:36:42 +0900 | [diff] [blame] | 186 | if err != nil { |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 187 | return err |
Shinichiro Hamaji | 5c53b57 | 2015-04-02 05:36:42 +0900 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 191 | ex.done[output] = j |
| 192 | ex.wm.PostJob(j) |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 193 | |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 194 | return nil |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 195 | } |
| 196 | |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 197 | func (ex *Executor) reportStats() { |
Shinichiro Hamaji | 4926aea | 2015-05-25 18:32:18 +0900 | [diff] [blame] | 198 | if !katiLogFlag && !katiPeriodicStatsFlag { |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 199 | return |
| 200 | } |
| 201 | |
| 202 | LogStats("build=%d alreadyDone=%d noRule=%d, upToDate=%d runCommand=%d", |
| 203 | ex.buildCnt, ex.alreadyDoneCnt, ex.noRuleCnt, ex.upToDateCnt, ex.runCommandCnt) |
Shinichiro Hamaji | 294a58b | 2015-04-14 15:31:52 +0900 | [diff] [blame] | 204 | if len(ex.trace) > 1 { |
| 205 | LogStats("trace=%q", ex.trace) |
| 206 | } |
| 207 | } |
| 208 | |
Shinichiro Hamaji | 17a8a6e | 2015-04-20 21:44:42 +0900 | [diff] [blame] | 209 | func NewExecutor(vars Vars) *Executor { |
| 210 | ex := &Executor{ |
| 211 | rules: make(map[string]*Rule), |
| 212 | suffixRules: make(map[string][]*Rule), |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 213 | done: make(map[string]*Job), |
Shinichiro Hamaji | 17a8a6e | 2015-04-20 21:44:42 +0900 | [diff] [blame] | 214 | vars: vars, |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 215 | wm: NewWorkerManager(), |
Shinichiro Hamaji | 17a8a6e | 2015-04-20 21:44:42 +0900 | [diff] [blame] | 216 | } |
Shinichiro Hamaji | b0f6548 | 2015-04-16 18:28:05 +0900 | [diff] [blame] | 217 | // TODO: We should move this to somewhere around evalCmd so that |
| 218 | // we can handle SHELL in target specific variables. |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 219 | ev := newEvaluator(ex.vars) |
| 220 | ex.shell = ev.EvaluateVar("SHELL") |
Shinichiro Hamaji | 17a8a6e | 2015-04-20 21:44:42 +0900 | [diff] [blame] | 221 | for k, v := range map[string]Var{ |
| 222 | "@": AutoAtVar{AutoVar: AutoVar{ex: ex}}, |
| 223 | "<": AutoLessVar{AutoVar: AutoVar{ex: ex}}, |
| 224 | "^": AutoHatVar{AutoVar: AutoVar{ex: ex}}, |
| 225 | "+": AutoPlusVar{AutoVar: AutoVar{ex: ex}}, |
| 226 | "*": AutoStarVar{AutoVar: AutoVar{ex: ex}}, |
| 227 | } { |
| 228 | ex.vars[k] = v |
| 229 | ex.vars[k+"D"] = AutoSuffixDVar{v: v} |
| 230 | ex.vars[k+"F"] = AutoSuffixFVar{v: v} |
| 231 | } |
Shinichiro Hamaji | 8843a05 | 2015-04-13 16:46:56 +0900 | [diff] [blame] | 232 | return ex |
| 233 | } |
Shinichiro Hamaji | 9b7d462 | 2015-04-02 01:37:04 +0900 | [diff] [blame] | 234 | |
Shinichiro Hamaji | 17a8a6e | 2015-04-20 21:44:42 +0900 | [diff] [blame] | 235 | func (ex *Executor) Exec(roots []*DepNode) error { |
| 236 | for _, root := range roots { |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 237 | ex.makeJobs(root, nil) |
Shinichiro Hamaji | b13f3d5 | 2015-03-30 19:29:44 +0900 | [diff] [blame] | 238 | } |
Shinichiro Hamaji | 74a6600 | 2015-04-27 16:42:30 +0900 | [diff] [blame] | 239 | ex.wm.Wait() |
Shinichiro Hamaji | b13f3d5 | 2015-03-30 19:29:44 +0900 | [diff] [blame] | 240 | return nil |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 241 | } |
Shinichiro Hamaji | b41fd50 | 2015-04-29 03:34:07 +0900 | [diff] [blame] | 242 | |
| 243 | func (ex *Executor) createRunners(n *DepNode, avoidIO bool) ([]runner, bool) { |
Shinichiro Hamaji | 882d862 | 2015-05-26 14:40:44 +0900 | [diff] [blame] | 244 | var runners []runner |
| 245 | if len(n.Cmds) == 0 { |
| 246 | return runners, false |
| 247 | } |
| 248 | |
Shinichiro Hamaji | b41fd50 | 2015-04-29 03:34:07 +0900 | [diff] [blame] | 249 | var restores []func() |
| 250 | defer func() { |
Shinichiro Hamaji | d7044b1 | 2015-05-14 17:37:32 +0900 | [diff] [blame] | 251 | for i := len(restores) - 1; i >= 0; i-- { |
| 252 | restores[i]() |
Shinichiro Hamaji | b41fd50 | 2015-04-29 03:34:07 +0900 | [diff] [blame] | 253 | } |
| 254 | }() |
| 255 | |
| 256 | ex.varsLock.Lock() |
| 257 | restores = append(restores, func() { ex.varsLock.Unlock() }) |
| 258 | // For automatic variables. |
| 259 | ex.currentOutput = n.Output |
| 260 | ex.currentInputs = n.ActualInputs |
| 261 | for k, v := range n.TargetSpecificVars { |
| 262 | restores = append(restores, ex.vars.save(k)) |
| 263 | ex.vars[k] = v |
| 264 | } |
| 265 | |
| 266 | ev := newEvaluator(ex.vars) |
| 267 | ev.avoidIO = avoidIO |
| 268 | ev.filename = n.Filename |
| 269 | ev.lineno = n.Lineno |
Shinichiro Hamaji | b41fd50 | 2015-04-29 03:34:07 +0900 | [diff] [blame] | 270 | Log("Building: %s cmds:%q", n.Output, n.Cmds) |
| 271 | r := runner{ |
| 272 | output: n.Output, |
| 273 | echo: true, |
| 274 | shell: ex.shell, |
| 275 | } |
| 276 | for _, cmd := range n.Cmds { |
| 277 | for _, r := range evalCmd(ev, r, cmd) { |
| 278 | if len(r.cmd) != 0 { |
| 279 | runners = append(runners, r) |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | return runners, ev.hasIO |
| 284 | } |
| 285 | |
| 286 | func EvalCommands(nodes []*DepNode, vars Vars) { |
| 287 | ioCnt := 0 |
| 288 | ex := NewExecutor(vars) |
| 289 | for i, n := range nodes { |
| 290 | runners, hasIO := ex.createRunners(n, true) |
| 291 | if hasIO { |
| 292 | ioCnt++ |
Shinichiro Hamaji | cedc5c8 | 2015-05-13 17:03:20 +0900 | [diff] [blame] | 293 | if ioCnt%100 == 0 { |
Shinichiro Hamaji | b41fd50 | 2015-04-29 03:34:07 +0900 | [diff] [blame] | 294 | LogStats("%d/%d rules have IO", ioCnt, i+1) |
| 295 | } |
| 296 | continue |
| 297 | } |
| 298 | |
| 299 | n.Cmds = []string{} |
| 300 | n.TargetSpecificVars = make(Vars) |
| 301 | for _, r := range runners { |
| 302 | cmd := r.cmd |
| 303 | // TODO: Do not preserve the effect of dryRunFlag. |
| 304 | if r.echo { |
| 305 | cmd = "@" + cmd |
| 306 | } |
| 307 | if r.ignoreError { |
| 308 | cmd = "-" + cmd |
| 309 | } |
| 310 | n.Cmds = append(n.Cmds, cmd) |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | LogStats("%d/%d rules have IO", ioCnt, len(nodes)) |
| 315 | } |