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