Shinichiro Hamaji | b69bf8a | 2015-06-10 14:52:06 +0900 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 15 | package kati |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 16 | |
| 17 | import ( |
| 18 | "bytes" |
Shinichiro Hamaji | 4220be3 | 2015-05-26 13:30:18 +0900 | [diff] [blame] | 19 | "crypto/sha1" |
Fumitoshi Ukai | f8efa0a | 2015-03-30 18:10:11 +0900 | [diff] [blame] | 20 | "fmt" |
Fumitoshi Ukai | 9042b99 | 2015-06-23 16:10:27 +0900 | [diff] [blame] | 21 | "os" |
Shinichiro Hamaji | 2450bfa | 2015-05-29 16:43:28 +0900 | [diff] [blame] | 22 | "path/filepath" |
Fumitoshi Ukai | 612e086 | 2015-08-10 16:09:09 +0900 | [diff] [blame] | 23 | "strconv" |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 24 | "strings" |
Fumitoshi Ukai | 0af4452 | 2015-06-25 15:26:08 +0900 | [diff] [blame] | 25 | "sync" |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 26 | |
| 27 | "github.com/golang/glog" |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 28 | ) |
| 29 | |
Fumitoshi Ukai | 0af4452 | 2015-06-25 15:26:08 +0900 | [diff] [blame] | 30 | type fileState int |
Fumitoshi Ukai | 936de10 | 2015-06-08 11:21:16 +0900 | [diff] [blame] | 31 | |
Shinichiro Hamaji | 45cde1d | 2015-05-25 18:21:23 +0900 | [diff] [blame] | 32 | const ( |
Fumitoshi Ukai | 0af4452 | 2015-06-25 15:26:08 +0900 | [diff] [blame] | 33 | fileExists fileState = iota |
| 34 | fileNotExists |
| 35 | fileInconsistent // Modified during kati is running. |
Shinichiro Hamaji | 45cde1d | 2015-05-25 18:21:23 +0900 | [diff] [blame] | 36 | ) |
| 37 | |
Fumitoshi Ukai | 0af4452 | 2015-06-25 15:26:08 +0900 | [diff] [blame] | 38 | type accessedMakefile struct { |
Shinichiro Hamaji | f3ea5d0 | 2015-05-20 17:13:00 +0900 | [diff] [blame] | 39 | Filename string |
Shinichiro Hamaji | 4220be3 | 2015-05-26 13:30:18 +0900 | [diff] [blame] | 40 | Hash [sha1.Size]byte |
Fumitoshi Ukai | 0af4452 | 2015-06-25 15:26:08 +0900 | [diff] [blame] | 41 | State fileState |
| 42 | } |
| 43 | |
| 44 | type accessCache struct { |
| 45 | mu sync.Mutex |
| 46 | m map[string]*accessedMakefile |
| 47 | } |
| 48 | |
| 49 | func newAccessCache() *accessCache { |
| 50 | return &accessCache{ |
| 51 | m: make(map[string]*accessedMakefile), |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func (ac *accessCache) update(fn string, hash [sha1.Size]byte, st fileState) string { |
| 56 | if ac == nil { |
| 57 | return "" |
| 58 | } |
| 59 | ac.mu.Lock() |
| 60 | defer ac.mu.Unlock() |
| 61 | rm, present := ac.m[fn] |
| 62 | if present { |
| 63 | switch rm.State { |
| 64 | case fileExists: |
| 65 | if st != fileExists { |
| 66 | return fmt.Sprintf("%s was removed after the previous read", fn) |
| 67 | } else if !bytes.Equal(hash[:], rm.Hash[:]) { |
| 68 | ac.m[fn].State = fileInconsistent |
| 69 | return fmt.Sprintf("%s was modified after the previous read", fn) |
| 70 | } |
| 71 | return "" |
| 72 | case fileNotExists: |
| 73 | if st != fileNotExists { |
| 74 | ac.m[fn].State = fileInconsistent |
| 75 | return fmt.Sprintf("%s was created after the previous read", fn) |
| 76 | } |
| 77 | case fileInconsistent: |
| 78 | return "" |
| 79 | } |
| 80 | return "" |
| 81 | } |
| 82 | ac.m[fn] = &accessedMakefile{ |
| 83 | Filename: fn, |
| 84 | Hash: hash, |
| 85 | State: st, |
| 86 | } |
| 87 | return "" |
| 88 | } |
| 89 | |
| 90 | func (ac *accessCache) Slice() []*accessedMakefile { |
| 91 | if ac == nil { |
| 92 | return nil |
| 93 | } |
| 94 | ac.mu.Lock() |
| 95 | defer ac.mu.Unlock() |
| 96 | r := []*accessedMakefile{} |
| 97 | for _, v := range ac.m { |
| 98 | r = append(r, v) |
| 99 | } |
| 100 | return r |
Shinichiro Hamaji | f3ea5d0 | 2015-05-20 17:13:00 +0900 | [diff] [blame] | 101 | } |
| 102 | |
Fumitoshi Ukai | 08eda65 | 2015-06-25 16:27:10 +0900 | [diff] [blame] | 103 | type evalResult struct { |
Fumitoshi Ukai | 0af4452 | 2015-06-25 15:26:08 +0900 | [diff] [blame] | 104 | vars Vars |
Fumitoshi Ukai | adc1444 | 2015-06-25 16:10:30 +0900 | [diff] [blame] | 105 | rules []*rule |
Fumitoshi Ukai | 0af4452 | 2015-06-25 15:26:08 +0900 | [diff] [blame] | 106 | ruleVars map[string]Vars |
| 107 | accessedMks []*accessedMakefile |
| 108 | exports map[string]bool |
Fumitoshi Ukai | 09fcd52 | 2015-07-15 14:31:50 +0900 | [diff] [blame] | 109 | vpaths searchPaths |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 110 | } |
| 111 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 112 | type srcpos struct { |
| 113 | filename string |
| 114 | lineno int |
| 115 | } |
| 116 | |
| 117 | func (p srcpos) String() string { |
| 118 | return fmt.Sprintf("%s:%d", p.filename, p.lineno) |
| 119 | } |
| 120 | |
| 121 | // EvalError is an error in kati evaluation. |
| 122 | type EvalError struct { |
| 123 | Filename string |
| 124 | Lineno int |
| 125 | Err error |
| 126 | } |
| 127 | |
| 128 | func (e EvalError) Error() string { |
| 129 | return fmt.Sprintf("%s:%d: %v", e.Filename, e.Lineno, e.Err) |
| 130 | } |
| 131 | |
| 132 | func (p srcpos) errorf(f string, args ...interface{}) error { |
| 133 | return EvalError{ |
| 134 | Filename: p.filename, |
| 135 | Lineno: p.lineno, |
| 136 | Err: fmt.Errorf(f, args...), |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | func (p srcpos) error(err error) error { |
| 141 | if _, ok := err.(EvalError); ok { |
| 142 | return err |
| 143 | } |
| 144 | return EvalError{ |
| 145 | Filename: p.filename, |
| 146 | Lineno: p.lineno, |
| 147 | Err: err, |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // Evaluator manages makefile evaluation. |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 152 | type Evaluator struct { |
Fumitoshi Ukai | f0a2ba7 | 2015-04-19 00:02:32 +0900 | [diff] [blame] | 153 | paramVars []tmpval // $1 => paramVars[1] |
Shinichiro Hamaji | a485d2f | 2015-04-16 14:03:24 +0900 | [diff] [blame] | 154 | outVars Vars |
Fumitoshi Ukai | adc1444 | 2015-06-25 16:10:30 +0900 | [diff] [blame] | 155 | outRules []*rule |
Shinichiro Hamaji | a485d2f | 2015-04-16 14:03:24 +0900 | [diff] [blame] | 156 | outRuleVars map[string]Vars |
| 157 | vars Vars |
Fumitoshi Ukai | adc1444 | 2015-06-25 16:10:30 +0900 | [diff] [blame] | 158 | lastRule *rule |
Shinichiro Hamaji | a485d2f | 2015-04-16 14:03:24 +0900 | [diff] [blame] | 159 | currentScope Vars |
Fumitoshi Ukai | 0af4452 | 2015-06-25 15:26:08 +0900 | [diff] [blame] | 160 | cache *accessCache |
Shinichiro Hamaji | 7e52142 | 2015-05-29 14:23:30 +0900 | [diff] [blame] | 161 | exports map[string]bool |
Fumitoshi Ukai | 09fcd52 | 2015-07-15 14:31:50 +0900 | [diff] [blame] | 162 | vpaths []vpath |
Shinichiro Hamaji | 51d53d9 | 2015-03-30 18:32:16 +0900 | [diff] [blame] | 163 | |
Fumitoshi Ukai | e5bf758 | 2015-07-28 16:07:51 +0900 | [diff] [blame] | 164 | avoidIO bool |
| 165 | hasIO bool |
| 166 | // delayedOutputs are commands which should run at ninja-time |
| 167 | // (i.e., info, warning, and error). |
| 168 | delayedOutputs []string |
| 169 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 170 | srcpos |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 171 | } |
| 172 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 173 | // NewEvaluator creates new Evaluator. |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 174 | func NewEvaluator(vars map[string]Var) *Evaluator { |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 175 | return &Evaluator{ |
Shinichiro Hamaji | a485d2f | 2015-04-16 14:03:24 +0900 | [diff] [blame] | 176 | outVars: make(Vars), |
| 177 | vars: vars, |
| 178 | outRuleVars: make(map[string]Vars), |
Shinichiro Hamaji | 7e52142 | 2015-05-29 14:23:30 +0900 | [diff] [blame] | 179 | exports: make(map[string]bool), |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Fumitoshi Ukai | a4a0225 | 2015-07-09 14:25:18 +0900 | [diff] [blame] | 183 | func (ev *Evaluator) args(buf *evalBuffer, args ...Value) ([][]byte, error) { |
Fumitoshi Ukai | 77411fb | 2015-06-19 15:46:21 +0900 | [diff] [blame] | 184 | pos := make([]int, 0, len(args)) |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 185 | for _, arg := range args { |
Fumitoshi Ukai | a4a0225 | 2015-07-09 14:25:18 +0900 | [diff] [blame] | 186 | buf.resetSep() |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 187 | err := arg.Eval(buf, ev) |
| 188 | if err != nil { |
| 189 | return nil, err |
| 190 | } |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 191 | pos = append(pos, buf.Len()) |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 192 | } |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 193 | v := buf.Bytes() |
Fumitoshi Ukai | d0bf88c | 2015-05-08 10:54:32 +0900 | [diff] [blame] | 194 | buf.args = buf.args[:0] |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 195 | s := 0 |
| 196 | for _, p := range pos { |
Fumitoshi Ukai | d0bf88c | 2015-05-08 10:54:32 +0900 | [diff] [blame] | 197 | buf.args = append(buf.args, v[s:p]) |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 198 | s = p |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 199 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 200 | return buf.args, nil |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 201 | } |
| 202 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 203 | func (ev *Evaluator) evalAssign(ast *assignAST) error { |
Shinichiro Hamaji | 0b93c86 | 2015-04-07 06:15:15 +0900 | [diff] [blame] | 204 | ev.lastRule = nil |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 205 | lhs, rhs, err := ev.evalAssignAST(ast) |
| 206 | if err != nil { |
| 207 | return err |
| 208 | } |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 209 | if glog.V(1) { |
| 210 | glog.Infof("ASSIGN: %s=%q (flavor:%q)", lhs, rhs, rhs.Flavor()) |
Fumitoshi Ukai | 5c04ad8 | 2015-06-18 16:14:04 +0900 | [diff] [blame] | 211 | } |
Fumitoshi Ukai | e16c2b7 | 2015-06-19 00:15:59 +0900 | [diff] [blame] | 212 | if lhs == "" { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 213 | return ast.errorf("*** empty variable name.") |
Shinichiro Hamaji | 46ee5cf | 2015-04-06 18:07:10 +0900 | [diff] [blame] | 214 | } |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 215 | ev.outVars.Assign(lhs, rhs) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 216 | return nil |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 217 | } |
| 218 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 219 | func (ev *Evaluator) evalAssignAST(ast *assignAST) (string, Var, error) { |
| 220 | ev.srcpos = ast.srcpos |
Shinichiro Hamaji | 51d53d9 | 2015-03-30 18:32:16 +0900 | [diff] [blame] | 221 | |
Fumitoshi Ukai | 00178d1 | 2015-04-18 00:11:05 +0900 | [diff] [blame] | 222 | var lhs string |
Shinichiro Hamaji | 7825b65 | 2015-06-04 13:47:14 +0900 | [diff] [blame] | 223 | switch v := ast.lhs.(type) { |
Fumitoshi Ukai | 00178d1 | 2015-04-18 00:11:05 +0900 | [diff] [blame] | 224 | case literal: |
| 225 | lhs = string(v) |
| 226 | case tmpval: |
| 227 | lhs = string(v) |
| 228 | default: |
Fumitoshi Ukai | a4a0225 | 2015-07-09 14:25:18 +0900 | [diff] [blame] | 229 | buf := newEbuf() |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 230 | err := v.Eval(buf, ev) |
| 231 | if err != nil { |
| 232 | return "", nil, err |
| 233 | } |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 234 | lhs = string(trimSpaceBytes(buf.Bytes())) |
Fumitoshi Ukai | a4a0225 | 2015-07-09 14:25:18 +0900 | [diff] [blame] | 235 | buf.release() |
Fumitoshi Ukai | 00178d1 | 2015-04-18 00:11:05 +0900 | [diff] [blame] | 236 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 237 | rhs, err := ast.evalRHS(ev, lhs) |
| 238 | if err != nil { |
| 239 | return "", nil, err |
| 240 | } |
| 241 | return lhs, rhs, nil |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 242 | } |
| 243 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 244 | func (ev *Evaluator) setTargetSpecificVar(assign *assignAST, output string) error { |
Shinichiro Hamaji | a485d2f | 2015-04-16 14:03:24 +0900 | [diff] [blame] | 245 | vars, present := ev.outRuleVars[output] |
| 246 | if !present { |
| 247 | vars = make(Vars) |
| 248 | ev.outRuleVars[output] = vars |
| 249 | } |
| 250 | ev.currentScope = vars |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 251 | lhs, rhs, err := ev.evalAssignAST(assign) |
| 252 | if err != nil { |
| 253 | return err |
| 254 | } |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 255 | if glog.V(1) { |
| 256 | glog.Infof("rule outputs:%q assign:%q%s%q (flavor:%q)", output, lhs, assign.op, rhs, rhs.Flavor()) |
Fumitoshi Ukai | 5c04ad8 | 2015-06-18 16:14:04 +0900 | [diff] [blame] | 257 | } |
Fumitoshi Ukai | 7bf992d | 2015-06-25 12:42:19 +0900 | [diff] [blame] | 258 | vars.Assign(lhs, &targetSpecificVar{v: rhs, op: assign.op}) |
Shinichiro Hamaji | a485d2f | 2015-04-16 14:03:24 +0900 | [diff] [blame] | 259 | ev.currentScope = nil |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 260 | return nil |
Shinichiro Hamaji | a485d2f | 2015-04-16 14:03:24 +0900 | [diff] [blame] | 261 | } |
| 262 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 263 | func (ev *Evaluator) evalMaybeRule(ast *maybeRuleAST) error { |
Shinichiro Hamaji | 0b93c86 | 2015-04-07 06:15:15 +0900 | [diff] [blame] | 264 | ev.lastRule = nil |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 265 | ev.srcpos = ast.srcpos |
Shinichiro Hamaji | 51d53d9 | 2015-03-30 18:32:16 +0900 | [diff] [blame] | 266 | |
Fumitoshi Ukai | 6addc2f | 2015-07-22 11:33:35 +0900 | [diff] [blame] | 267 | if glog.V(1) { |
| 268 | glog.Infof("maybe rule %s: %q assign:%v", ev.srcpos, ast.expr, ast.assign) |
| 269 | } |
Fumitoshi Ukai | 201df42 | 2015-07-07 17:31:05 +0900 | [diff] [blame] | 270 | |
Fumitoshi Ukai | a4a0225 | 2015-07-09 14:25:18 +0900 | [diff] [blame] | 271 | abuf := newEbuf() |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 272 | aexpr := toExpr(ast.expr) |
| 273 | var rhs expr |
| 274 | semi := ast.semi |
| 275 | for i, v := range aexpr { |
Fumitoshi Ukai | 66c4ee1 | 2015-07-28 16:31:58 +0900 | [diff] [blame] | 276 | var hashFound bool |
Fumitoshi Ukai | a4a0225 | 2015-07-09 14:25:18 +0900 | [diff] [blame] | 277 | var buf evalBuffer |
| 278 | buf.resetSep() |
Fumitoshi Ukai | 66c4ee1 | 2015-07-28 16:31:58 +0900 | [diff] [blame] | 279 | switch v.(type) { |
| 280 | case literal, tmpval: |
| 281 | s := v.String() |
| 282 | i := strings.Index(s, "#") |
| 283 | if i >= 0 { |
| 284 | hashFound = true |
| 285 | v = tmpval(trimRightSpaceBytes([]byte(s[:i]))) |
| 286 | } |
| 287 | } |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 288 | err := v.Eval(&buf, ev) |
| 289 | if err != nil { |
| 290 | return err |
| 291 | } |
| 292 | b := buf.Bytes() |
Fumitoshi Ukai | 201df42 | 2015-07-07 17:31:05 +0900 | [diff] [blame] | 293 | if ast.isRule { |
| 294 | abuf.Write(b) |
| 295 | continue |
| 296 | } |
Fumitoshi Ukai | 6addc2f | 2015-07-22 11:33:35 +0900 | [diff] [blame] | 297 | eq := findLiteralChar(b, '=', 0, skipVar) |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 298 | if eq >= 0 { |
| 299 | abuf.Write(b[:eq+1]) |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 300 | if eq+1 < len(b) { |
| 301 | rhs = append(rhs, tmpval(trimLeftSpaceBytes(b[eq+1:]))) |
| 302 | } |
| 303 | if i+1 < len(aexpr) { |
| 304 | rhs = append(rhs, aexpr[i+1:]...) |
| 305 | } |
| 306 | if ast.semi != nil { |
| 307 | rhs = append(rhs, literal(';')) |
| 308 | sexpr, _, err := parseExpr(ast.semi, nil, parseOp{}) |
| 309 | if err != nil { |
| 310 | return err |
| 311 | } |
| 312 | rhs = append(rhs, toExpr(sexpr)...) |
| 313 | semi = nil |
| 314 | } |
| 315 | break |
| 316 | } |
| 317 | abuf.Write(b) |
Fumitoshi Ukai | 66c4ee1 | 2015-07-28 16:31:58 +0900 | [diff] [blame] | 318 | if hashFound { |
| 319 | break |
| 320 | } |
Fumitoshi Ukai | 5c04ad8 | 2015-06-18 16:14:04 +0900 | [diff] [blame] | 321 | } |
Shinichiro Hamaji | 0493261 | 2015-03-31 23:46:56 +0900 | [diff] [blame] | 322 | |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 323 | line := abuf.Bytes() |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 324 | r := &rule{srcpos: ast.srcpos} |
Fumitoshi Ukai | 6addc2f | 2015-07-22 11:33:35 +0900 | [diff] [blame] | 325 | if glog.V(1) { |
| 326 | glog.Infof("rule? %s: %q assign:%v rhs:%s", r.srcpos, line, ast.assign, rhs) |
| 327 | } |
Fumitoshi Ukai | 201df42 | 2015-07-07 17:31:05 +0900 | [diff] [blame] | 328 | assign, err := r.parse(line, ast.assign, rhs) |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 329 | if err != nil { |
Fumitoshi Ukai | f5b916d | 2015-07-15 15:57:19 +0900 | [diff] [blame] | 330 | ws := newWordScanner(line) |
| 331 | if ws.Scan() { |
| 332 | if string(ws.Bytes()) == "override" { |
Fumitoshi Ukai | 1ed9437 | 2015-07-15 16:28:20 +0900 | [diff] [blame] | 333 | warnNoPrefix(ast.srcpos, "invalid `override' directive") |
Fumitoshi Ukai | f5b916d | 2015-07-15 15:57:19 +0900 | [diff] [blame] | 334 | return nil |
| 335 | } |
| 336 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 337 | return ast.error(err) |
Shinichiro Hamaji | 21a9b5f | 2015-04-01 02:42:59 +0900 | [diff] [blame] | 338 | } |
Fumitoshi Ukai | a4a0225 | 2015-07-09 14:25:18 +0900 | [diff] [blame] | 339 | abuf.release() |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 340 | if glog.V(1) { |
| 341 | glog.Infof("rule %q assign:%v rhs:%v=> outputs:%q, inputs:%q", ast.expr, ast.assign, rhs, r.outputs, r.inputs) |
Fumitoshi Ukai | 5c04ad8 | 2015-06-18 16:14:04 +0900 | [diff] [blame] | 342 | } |
Shinichiro Hamaji | de82971 | 2015-03-31 18:26:56 +0900 | [diff] [blame] | 343 | |
Shinichiro Hamaji | 0493261 | 2015-03-31 23:46:56 +0900 | [diff] [blame] | 344 | // TODO: Pretty print. |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 345 | // glog.V(1).Infof("RULE: %s=%s (%d commands)", lhs, rhs, len(cmds)) |
Shinichiro Hamaji | 0493261 | 2015-03-31 23:46:56 +0900 | [diff] [blame] | 346 | |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 347 | if assign != nil { |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 348 | glog.V(1).Infof("target specific var: %#v", assign) |
Fumitoshi Ukai | adc1444 | 2015-06-25 16:10:30 +0900 | [diff] [blame] | 349 | for _, output := range r.outputs { |
Shinichiro Hamaji | a485d2f | 2015-04-16 14:03:24 +0900 | [diff] [blame] | 350 | ev.setTargetSpecificVar(assign, output) |
Shinichiro Hamaji | 6cc5c61 | 2015-04-15 12:59:40 +0900 | [diff] [blame] | 351 | } |
Fumitoshi Ukai | adc1444 | 2015-06-25 16:10:30 +0900 | [diff] [blame] | 352 | for _, output := range r.outputPatterns { |
Fumitoshi Ukai | 935de96 | 2015-04-28 17:08:20 +0900 | [diff] [blame] | 353 | ev.setTargetSpecificVar(assign, output.String()) |
Shinichiro Hamaji | ea170b1 | 2015-04-15 10:02:33 +0900 | [diff] [blame] | 354 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 355 | return nil |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 356 | } |
Shinichiro Hamaji | a485d2f | 2015-04-16 14:03:24 +0900 | [diff] [blame] | 357 | |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 358 | if semi != nil { |
| 359 | r.cmds = append(r.cmds, string(semi)) |
Shinichiro Hamaji | a485d2f | 2015-04-16 14:03:24 +0900 | [diff] [blame] | 360 | } |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 361 | if glog.V(1) { |
| 362 | glog.Infof("rule outputs:%q cmds:%q", r.outputs, r.cmds) |
Fumitoshi Ukai | 5c04ad8 | 2015-06-18 16:14:04 +0900 | [diff] [blame] | 363 | } |
Fumitoshi Ukai | adc1444 | 2015-06-25 16:10:30 +0900 | [diff] [blame] | 364 | ev.lastRule = r |
| 365 | ev.outRules = append(ev.outRules, r) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 366 | return nil |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 367 | } |
| 368 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 369 | func (ev *Evaluator) evalCommand(ast *commandAST) error { |
| 370 | ev.srcpos = ast.srcpos |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 371 | if ev.lastRule == nil || ev.lastRule.outputs == nil { |
Shinichiro Hamaji | b0e8116 | 2015-04-09 07:50:06 +0900 | [diff] [blame] | 372 | // This could still be an assignment statement. See |
| 373 | // assign_after_tab.mk. |
| 374 | if strings.IndexByte(ast.cmd, '=') >= 0 { |
Shinichiro Hamaji | c88618f | 2015-04-11 19:58:06 +0900 | [diff] [blame] | 375 | line := trimLeftSpace(ast.cmd) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 376 | mk, err := parseMakefileString(line, ast.srcpos) |
Shinichiro Hamaji | b0e8116 | 2015-04-09 07:50:06 +0900 | [diff] [blame] | 377 | if err != nil { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 378 | return ast.errorf("parse failed: %q: %v", line, err) |
Shinichiro Hamaji | b0e8116 | 2015-04-09 07:50:06 +0900 | [diff] [blame] | 379 | } |
Fumitoshi Ukai | bcb3b4b | 2015-07-07 18:00:13 +0900 | [diff] [blame] | 380 | if len(mk.stmts) >= 1 && mk.stmts[len(mk.stmts)-1].(*assignAST) != nil { |
| 381 | for _, stmt := range mk.stmts { |
| 382 | err = ev.eval(stmt) |
| 383 | if err != nil { |
| 384 | return err |
| 385 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 386 | } |
Shinichiro Hamaji | b0e8116 | 2015-04-09 07:50:06 +0900 | [diff] [blame] | 387 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 388 | return nil |
Shinichiro Hamaji | b0e8116 | 2015-04-09 07:50:06 +0900 | [diff] [blame] | 389 | } |
Shinichiro Hamaji | 0b2929a | 2015-05-28 16:31:10 +0900 | [diff] [blame] | 390 | // Or, a comment is OK. |
| 391 | if strings.TrimSpace(ast.cmd)[0] == '#' { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 392 | return nil |
Shinichiro Hamaji | 0b2929a | 2015-05-28 16:31:10 +0900 | [diff] [blame] | 393 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 394 | return ast.errorf("*** commands commence before first target.") |
Shinichiro Hamaji | 0b93c86 | 2015-04-07 06:15:15 +0900 | [diff] [blame] | 395 | } |
| 396 | ev.lastRule.cmds = append(ev.lastRule.cmds, ast.cmd) |
| 397 | if ev.lastRule.cmdLineno == 0 { |
| 398 | ev.lastRule.cmdLineno = ast.lineno |
| 399 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 400 | return nil |
Shinichiro Hamaji | 0b93c86 | 2015-04-07 06:15:15 +0900 | [diff] [blame] | 401 | } |
| 402 | |
Fumitoshi Ukai | 612e086 | 2015-08-10 16:09:09 +0900 | [diff] [blame] | 403 | func (ev *Evaluator) paramVar(name string) (Var, error) { |
| 404 | idx, err := strconv.ParseInt(name, 10, 32) |
| 405 | if err != nil { |
| 406 | return nil, fmt.Errorf("param: %s: %v", name, err) |
| 407 | } |
| 408 | i := int(idx) |
| 409 | if i < 0 || i >= len(ev.paramVars) { |
| 410 | return nil, fmt.Errorf("param: %s out of %d", name, len(ev.paramVars)) |
| 411 | } |
| 412 | return &automaticVar{value: []byte(ev.paramVars[i])}, nil |
| 413 | } |
| 414 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 415 | // LookupVar looks up named variable. |
Fumitoshi Ukai | 8edcb79 | 2015-04-02 11:23:23 +0900 | [diff] [blame] | 416 | func (ev *Evaluator) LookupVar(name string) Var { |
Shinichiro Hamaji | a485d2f | 2015-04-16 14:03:24 +0900 | [diff] [blame] | 417 | if ev.currentScope != nil { |
| 418 | v := ev.currentScope.Lookup(name) |
| 419 | if v.IsDefined() { |
| 420 | return v |
| 421 | } |
| 422 | } |
| 423 | v := ev.outVars.Lookup(name) |
| 424 | if v.IsDefined() { |
| 425 | return v |
| 426 | } |
Fumitoshi Ukai | 612e086 | 2015-08-10 16:09:09 +0900 | [diff] [blame] | 427 | v, err := ev.paramVar(name) |
| 428 | if err == nil { |
| 429 | return v |
| 430 | } |
Shinichiro Hamaji | a485d2f | 2015-04-16 14:03:24 +0900 | [diff] [blame] | 431 | return ev.vars.Lookup(name) |
| 432 | } |
| 433 | |
Fumitoshi Ukai | a483ec0 | 2015-06-26 10:12:02 +0900 | [diff] [blame] | 434 | func (ev *Evaluator) lookupVarInCurrentScope(name string) Var { |
Shinichiro Hamaji | a485d2f | 2015-04-16 14:03:24 +0900 | [diff] [blame] | 435 | if ev.currentScope != nil { |
| 436 | v := ev.currentScope.Lookup(name) |
| 437 | return v |
| 438 | } |
Fumitoshi Ukai | 8edcb79 | 2015-04-02 11:23:23 +0900 | [diff] [blame] | 439 | v := ev.outVars.Lookup(name) |
| 440 | if v.IsDefined() { |
| 441 | return v |
Shinichiro Hamaji | 69b7f65 | 2015-03-31 01:01:59 +0900 | [diff] [blame] | 442 | } |
Fumitoshi Ukai | 612e086 | 2015-08-10 16:09:09 +0900 | [diff] [blame] | 443 | v, err := ev.paramVar(name) |
| 444 | if err == nil { |
| 445 | return v |
| 446 | } |
Fumitoshi Ukai | 8edcb79 | 2015-04-02 11:23:23 +0900 | [diff] [blame] | 447 | return ev.vars.Lookup(name) |
Shinichiro Hamaji | 69b7f65 | 2015-03-31 01:01:59 +0900 | [diff] [blame] | 448 | } |
| 449 | |
Fumitoshi Ukai | 936de10 | 2015-06-08 11:21:16 +0900 | [diff] [blame] | 450 | // EvaluateVar evaluates variable named name. |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 451 | // Only for a few special uses such as getting SHELL and handling |
| 452 | // export/unexport. |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 453 | func (ev *Evaluator) EvaluateVar(name string) (string, error) { |
Fumitoshi Ukai | a4a0225 | 2015-07-09 14:25:18 +0900 | [diff] [blame] | 454 | var buf evalBuffer |
| 455 | buf.resetSep() |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 456 | err := ev.LookupVar(name).Eval(&buf, ev) |
| 457 | if err != nil { |
| 458 | return "", err |
| 459 | } |
| 460 | return buf.String(), nil |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 461 | } |
| 462 | |
Fumitoshi Ukai | 08eda65 | 2015-06-25 16:27:10 +0900 | [diff] [blame] | 463 | func (ev *Evaluator) evalIncludeFile(fname string, mk makefile) error { |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 464 | te := traceEvent.begin("include", literal(fname), traceEventMain) |
Fumitoshi Ukai | 47f401f | 2015-04-30 17:29:50 +0900 | [diff] [blame] | 465 | defer func() { |
Fumitoshi Ukai | 432a242 | 2015-06-11 15:16:29 +0900 | [diff] [blame] | 466 | traceEvent.end(te) |
Fumitoshi Ukai | 47f401f | 2015-04-30 17:29:50 +0900 | [diff] [blame] | 467 | }() |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 468 | var err error |
Fumitoshi Ukai | 47f401f | 2015-04-30 17:29:50 +0900 | [diff] [blame] | 469 | makefileList := ev.outVars.Lookup("MAKEFILE_LIST") |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 470 | makefileList, err = makefileList.Append(ev, mk.filename) |
| 471 | if err != nil { |
| 472 | return err |
| 473 | } |
Fumitoshi Ukai | 47f401f | 2015-04-30 17:29:50 +0900 | [diff] [blame] | 474 | ev.outVars.Assign("MAKEFILE_LIST", makefileList) |
| 475 | |
| 476 | for _, stmt := range mk.stmts { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 477 | err = ev.eval(stmt) |
| 478 | if err != nil { |
| 479 | return err |
| 480 | } |
Fumitoshi Ukai | 47f401f | 2015-04-30 17:29:50 +0900 | [diff] [blame] | 481 | } |
| 482 | return nil |
| 483 | } |
| 484 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 485 | func (ev *Evaluator) evalInclude(ast *includeAST) error { |
Shinichiro Hamaji | 0b93c86 | 2015-04-07 06:15:15 +0900 | [diff] [blame] | 486 | ev.lastRule = nil |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 487 | ev.srcpos = ast.srcpos |
Shinichiro Hamaji | d7bef60 | 2015-03-30 19:55:32 +0900 | [diff] [blame] | 488 | |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 489 | glog.Infof("%s include %q", ev.srcpos, ast.expr) |
Fumitoshi Ukai | e9aa380 | 2015-07-03 11:33:23 +0900 | [diff] [blame] | 490 | v, _, err := parseExpr([]byte(ast.expr), nil, parseOp{}) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 491 | if err != nil { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 492 | return ast.errorf("parse failed: %q: %v", ast.expr, err) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 493 | } |
Fumitoshi Ukai | a4a0225 | 2015-07-09 14:25:18 +0900 | [diff] [blame] | 494 | var buf evalBuffer |
| 495 | buf.resetSep() |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 496 | err = v.Eval(&buf, ev) |
| 497 | if err != nil { |
| 498 | return ast.errorf("%v", err) |
| 499 | } |
Shinichiro Hamaji | 2450bfa | 2015-05-29 16:43:28 +0900 | [diff] [blame] | 500 | pats := splitSpaces(buf.String()) |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 501 | buf.Reset() |
Shinichiro Hamaji | 3e71846 | 2015-05-29 17:03:12 +0900 | [diff] [blame] | 502 | |
| 503 | var files []string |
Shinichiro Hamaji | 2450bfa | 2015-05-29 16:43:28 +0900 | [diff] [blame] | 504 | for _, pat := range pats { |
Shinichiro Hamaji | 3e71846 | 2015-05-29 17:03:12 +0900 | [diff] [blame] | 505 | if strings.Contains(pat, "*") || strings.Contains(pat, "?") { |
| 506 | matched, err := filepath.Glob(pat) |
Shinichiro Hamaji | 2450bfa | 2015-05-29 16:43:28 +0900 | [diff] [blame] | 507 | if err != nil { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 508 | return ast.errorf("glob error: %s: %v", pat, err) |
Shinichiro Hamaji | 2450bfa | 2015-05-29 16:43:28 +0900 | [diff] [blame] | 509 | } |
Shinichiro Hamaji | 3e71846 | 2015-05-29 17:03:12 +0900 | [diff] [blame] | 510 | files = append(files, matched...) |
| 511 | } else { |
| 512 | files = append(files, pat) |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | for _, fn := range files { |
Shinichiro Hamaji | d953332 | 2015-06-27 05:48:38 +0900 | [diff] [blame] | 517 | fn = trimLeadingCurdir(fn) |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 518 | if IgnoreOptionalInclude != "" && ast.op == "-include" && matchPattern(fn, IgnoreOptionalInclude) { |
Shinichiro Hamaji | 781a465 | 2015-06-02 16:23:50 +0900 | [diff] [blame] | 519 | continue |
| 520 | } |
Fumitoshi Ukai | 9042b99 | 2015-06-23 16:10:27 +0900 | [diff] [blame] | 521 | mk, hash, err := makefileCache.parse(fn) |
| 522 | if os.IsNotExist(err) { |
Shinichiro Hamaji | 3e71846 | 2015-05-29 17:03:12 +0900 | [diff] [blame] | 523 | if ast.op == "include" { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 524 | return ev.errorf("%v\nNOTE: kati does not support generating missing makefiles", err) |
Shinichiro Hamaji | 3e71846 | 2015-05-29 17:03:12 +0900 | [diff] [blame] | 525 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 526 | msg := ev.cache.update(fn, hash, fileNotExists) |
| 527 | if msg != "" { |
| 528 | warn(ev.srcpos, "%s", msg) |
| 529 | } |
| 530 | continue |
Shinichiro Hamaji | 3e71846 | 2015-05-29 17:03:12 +0900 | [diff] [blame] | 531 | } |
Fumitoshi Ukai | 0af4452 | 2015-06-25 15:26:08 +0900 | [diff] [blame] | 532 | msg := ev.cache.update(fn, hash, fileExists) |
| 533 | if msg != "" { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 534 | warn(ev.srcpos, "%s", msg) |
Fumitoshi Ukai | 0af4452 | 2015-06-25 15:26:08 +0900 | [diff] [blame] | 535 | } |
Fumitoshi Ukai | 9042b99 | 2015-06-23 16:10:27 +0900 | [diff] [blame] | 536 | err = ev.evalIncludeFile(fn, mk) |
Shinichiro Hamaji | 3e71846 | 2015-05-29 17:03:12 +0900 | [diff] [blame] | 537 | if err != nil { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 538 | return err |
Shinichiro Hamaji | 2450bfa | 2015-05-29 16:43:28 +0900 | [diff] [blame] | 539 | } |
Shinichiro Hamaji | d7bef60 | 2015-03-30 19:55:32 +0900 | [diff] [blame] | 540 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 541 | return nil |
Shinichiro Hamaji | d7bef60 | 2015-03-30 19:55:32 +0900 | [diff] [blame] | 542 | } |
| 543 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 544 | func (ev *Evaluator) evalIf(iast *ifAST) error { |
Shinichiro Hamaji | af1e816 | 2015-03-31 02:15:37 +0900 | [diff] [blame] | 545 | var isTrue bool |
Fumitoshi Ukai | 91ed5d7 | 2015-06-25 13:08:09 +0900 | [diff] [blame] | 546 | switch iast.op { |
Shinichiro Hamaji | 497754d | 2015-03-31 02:02:11 +0900 | [diff] [blame] | 547 | case "ifdef", "ifndef": |
Fumitoshi Ukai | 91ed5d7 | 2015-06-25 13:08:09 +0900 | [diff] [blame] | 548 | expr := iast.lhs |
Fumitoshi Ukai | a4a0225 | 2015-07-09 14:25:18 +0900 | [diff] [blame] | 549 | buf := newEbuf() |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 550 | err := expr.Eval(buf, ev) |
| 551 | if err != nil { |
| 552 | return iast.errorf("%v\n expr:%s", err, expr) |
| 553 | } |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 554 | v := ev.LookupVar(buf.String()) |
| 555 | buf.Reset() |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 556 | err = v.Eval(buf, ev) |
| 557 | if err != nil { |
| 558 | return iast.errorf("%v\n expr:%s=>%s", err, expr, v) |
| 559 | } |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 560 | value := buf.String() |
| 561 | val := buf.Len() |
Fumitoshi Ukai | a4a0225 | 2015-07-09 14:25:18 +0900 | [diff] [blame] | 562 | buf.release() |
Fumitoshi Ukai | 91ed5d7 | 2015-06-25 13:08:09 +0900 | [diff] [blame] | 563 | isTrue = (val > 0) == (iast.op == "ifdef") |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 564 | if glog.V(1) { |
| 565 | glog.Infof("%s lhs=%q value=%q => %t", iast.op, iast.lhs, value, isTrue) |
Fumitoshi Ukai | 5c04ad8 | 2015-06-18 16:14:04 +0900 | [diff] [blame] | 566 | } |
Shinichiro Hamaji | 497754d | 2015-03-31 02:02:11 +0900 | [diff] [blame] | 567 | case "ifeq", "ifneq": |
Fumitoshi Ukai | 91ed5d7 | 2015-06-25 13:08:09 +0900 | [diff] [blame] | 568 | lexpr := iast.lhs |
| 569 | rexpr := iast.rhs |
Fumitoshi Ukai | a4a0225 | 2015-07-09 14:25:18 +0900 | [diff] [blame] | 570 | buf := newEbuf() |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 571 | params, err := ev.args(buf, lexpr, rexpr) |
| 572 | if err != nil { |
| 573 | return iast.errorf("%v\n (%s,%s)", err, lexpr, rexpr) |
| 574 | } |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 575 | lhs := string(params[0]) |
| 576 | rhs := string(params[1]) |
Fumitoshi Ukai | a4a0225 | 2015-07-09 14:25:18 +0900 | [diff] [blame] | 577 | buf.release() |
Fumitoshi Ukai | 91ed5d7 | 2015-06-25 13:08:09 +0900 | [diff] [blame] | 578 | isTrue = (lhs == rhs) == (iast.op == "ifeq") |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 579 | if glog.V(1) { |
| 580 | glog.Infof("%s lhs=%q %q rhs=%q %q => %t", iast.op, iast.lhs, lhs, iast.rhs, rhs, isTrue) |
Fumitoshi Ukai | 5c04ad8 | 2015-06-18 16:14:04 +0900 | [diff] [blame] | 581 | } |
Shinichiro Hamaji | 497754d | 2015-03-31 02:02:11 +0900 | [diff] [blame] | 582 | default: |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 583 | return iast.errorf("unknown if statement: %q", iast.op) |
Shinichiro Hamaji | 497754d | 2015-03-31 02:02:11 +0900 | [diff] [blame] | 584 | } |
Shinichiro Hamaji | af1e816 | 2015-03-31 02:15:37 +0900 | [diff] [blame] | 585 | |
Fumitoshi Ukai | 91ed5d7 | 2015-06-25 13:08:09 +0900 | [diff] [blame] | 586 | var stmts []ast |
Shinichiro Hamaji | af1e816 | 2015-03-31 02:15:37 +0900 | [diff] [blame] | 587 | if isTrue { |
Fumitoshi Ukai | 91ed5d7 | 2015-06-25 13:08:09 +0900 | [diff] [blame] | 588 | stmts = iast.trueStmts |
Shinichiro Hamaji | af1e816 | 2015-03-31 02:15:37 +0900 | [diff] [blame] | 589 | } else { |
Fumitoshi Ukai | 91ed5d7 | 2015-06-25 13:08:09 +0900 | [diff] [blame] | 590 | stmts = iast.falseStmts |
Shinichiro Hamaji | af1e816 | 2015-03-31 02:15:37 +0900 | [diff] [blame] | 591 | } |
Shinichiro Hamaji | 497754d | 2015-03-31 02:02:11 +0900 | [diff] [blame] | 592 | for _, stmt := range stmts { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 593 | err := ev.eval(stmt) |
| 594 | if err != nil { |
| 595 | return err |
| 596 | } |
Shinichiro Hamaji | 497754d | 2015-03-31 02:02:11 +0900 | [diff] [blame] | 597 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 598 | return nil |
Shinichiro Hamaji | 497754d | 2015-03-31 02:02:11 +0900 | [diff] [blame] | 599 | } |
| 600 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 601 | func (ev *Evaluator) evalExport(ast *exportAST) error { |
Shinichiro Hamaji | f61033d | 2015-05-29 15:01:48 +0900 | [diff] [blame] | 602 | ev.lastRule = nil |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 603 | ev.srcpos = ast.srcpos |
Shinichiro Hamaji | f61033d | 2015-05-29 15:01:48 +0900 | [diff] [blame] | 604 | |
Fumitoshi Ukai | e9aa380 | 2015-07-03 11:33:23 +0900 | [diff] [blame] | 605 | v, _, err := parseExpr(ast.expr, nil, parseOp{}) |
Shinichiro Hamaji | f61033d | 2015-05-29 15:01:48 +0900 | [diff] [blame] | 606 | if err != nil { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 607 | return ast.errorf("failed to parse: %q: %v", string(ast.expr), err) |
Shinichiro Hamaji | f61033d | 2015-05-29 15:01:48 +0900 | [diff] [blame] | 608 | } |
Fumitoshi Ukai | a4a0225 | 2015-07-09 14:25:18 +0900 | [diff] [blame] | 609 | var buf evalBuffer |
| 610 | buf.resetSep() |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 611 | err = v.Eval(&buf, ev) |
| 612 | if err != nil { |
| 613 | return ast.errorf("%v\n expr:%s", err, v) |
| 614 | } |
Fumitoshi Ukai | f5b916d | 2015-07-15 15:57:19 +0900 | [diff] [blame] | 615 | if ast.hasEqual { |
| 616 | ev.exports[string(trimSpaceBytes(buf.Bytes()))] = ast.export |
| 617 | } else { |
| 618 | for _, n := range splitSpacesBytes(buf.Bytes()) { |
| 619 | ev.exports[string(n)] = ast.export |
| 620 | } |
Shinichiro Hamaji | f61033d | 2015-05-29 15:01:48 +0900 | [diff] [blame] | 621 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 622 | return nil |
Shinichiro Hamaji | 7e52142 | 2015-05-29 14:23:30 +0900 | [diff] [blame] | 623 | } |
| 624 | |
Fumitoshi Ukai | 09fcd52 | 2015-07-15 14:31:50 +0900 | [diff] [blame] | 625 | func (ev *Evaluator) evalVpath(ast *vpathAST) error { |
| 626 | ev.lastRule = nil |
| 627 | ev.srcpos = ast.srcpos |
| 628 | |
| 629 | var ebuf evalBuffer |
| 630 | ebuf.resetSep() |
| 631 | err := ast.expr.Eval(&ebuf, ev) |
| 632 | if err != nil { |
| 633 | return ast.errorf("%v\n expr:%s", err, ast.expr) |
| 634 | } |
| 635 | ws := newWordScanner(ebuf.Bytes()) |
| 636 | if !ws.Scan() { |
| 637 | ev.vpaths = nil |
| 638 | return nil |
| 639 | } |
| 640 | pat := string(ws.Bytes()) |
| 641 | if !ws.Scan() { |
| 642 | vpaths := ev.vpaths |
| 643 | ev.vpaths = nil |
| 644 | for _, v := range vpaths { |
| 645 | if v.pattern == pat { |
| 646 | continue |
| 647 | } |
| 648 | ev.vpaths = append(ev.vpaths, v) |
| 649 | } |
| 650 | return nil |
| 651 | } |
| 652 | // The search path, DIRECTORIES, is a list of directories to be |
| 653 | // searched, separated by colons (semi-colons on MS-DOS and |
| 654 | // MS-Windows) or blanks, just like the search path used in the |
| 655 | // `VPATH' variable. |
| 656 | var dirs []string |
| 657 | for { |
| 658 | for _, dir := range bytes.Split(ws.Bytes(), []byte{':'}) { |
| 659 | dirs = append(dirs, string(dir)) |
| 660 | } |
| 661 | if !ws.Scan() { |
| 662 | break |
| 663 | } |
| 664 | } |
| 665 | ev.vpaths = append(ev.vpaths, vpath{ |
| 666 | pattern: pat, |
| 667 | dirs: dirs, |
| 668 | }) |
| 669 | return nil |
| 670 | } |
| 671 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 672 | func (ev *Evaluator) eval(stmt ast) error { |
| 673 | return stmt.eval(ev) |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 674 | } |
| 675 | |
Fumitoshi Ukai | 08eda65 | 2015-06-25 16:27:10 +0900 | [diff] [blame] | 676 | func eval(mk makefile, vars Vars, useCache bool) (er *evalResult, err error) { |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 677 | ev := NewEvaluator(vars) |
Fumitoshi Ukai | 0af4452 | 2015-06-25 15:26:08 +0900 | [diff] [blame] | 678 | if useCache { |
| 679 | ev.cache = newAccessCache() |
| 680 | } |
Shinichiro Hamaji | 491e73f | 2015-04-07 12:41:59 +0900 | [diff] [blame] | 681 | |
Fumitoshi Ukai | 936de10 | 2015-06-08 11:21:16 +0900 | [diff] [blame] | 682 | makefileList := vars.Lookup("MAKEFILE_LIST") |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 683 | if !makefileList.IsDefined() { |
Fumitoshi Ukai | 7655f18 | 2015-07-15 14:05:41 +0900 | [diff] [blame] | 684 | makefileList = &simpleVar{value: []string{""}, origin: "file"} |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 685 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 686 | makefileList, err = makefileList.Append(ev, mk.filename) |
| 687 | if err != nil { |
| 688 | return nil, err |
| 689 | } |
Fumitoshi Ukai | 936de10 | 2015-06-08 11:21:16 +0900 | [diff] [blame] | 690 | ev.outVars.Assign("MAKEFILE_LIST", makefileList) |
Shinichiro Hamaji | 491e73f | 2015-04-07 12:41:59 +0900 | [diff] [blame] | 691 | |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 692 | for _, stmt := range mk.stmts { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 693 | err = ev.eval(stmt) |
| 694 | if err != nil { |
| 695 | return nil, err |
| 696 | } |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 697 | } |
Shinichiro Hamaji | 750ae2e | 2015-05-20 12:59:15 +0900 | [diff] [blame] | 698 | |
Fumitoshi Ukai | 09fcd52 | 2015-07-15 14:31:50 +0900 | [diff] [blame] | 699 | vpaths := searchPaths{ |
| 700 | vpaths: ev.vpaths, |
| 701 | } |
| 702 | v, found := ev.outVars["VPATH"] |
| 703 | if found { |
| 704 | wb := newWbuf() |
| 705 | err := v.Eval(wb, ev) |
| 706 | if err != nil { |
| 707 | return nil, err |
| 708 | } |
| 709 | // In the 'VPATH' variable, directory names are separated |
| 710 | // by colons or blanks. (on windows, semi-colons) |
| 711 | for _, word := range wb.words { |
| 712 | for _, dir := range bytes.Split(word, []byte{':'}) { |
| 713 | vpaths.dirs = append(vpaths.dirs, string(dir)) |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | glog.Infof("vpaths: %#v", vpaths) |
| 718 | |
Fumitoshi Ukai | 08eda65 | 2015-06-25 16:27:10 +0900 | [diff] [blame] | 719 | return &evalResult{ |
Fumitoshi Ukai | 0af4452 | 2015-06-25 15:26:08 +0900 | [diff] [blame] | 720 | vars: ev.outVars, |
| 721 | rules: ev.outRules, |
| 722 | ruleVars: ev.outRuleVars, |
| 723 | accessedMks: ev.cache.Slice(), |
| 724 | exports: ev.exports, |
Fumitoshi Ukai | 09fcd52 | 2015-07-15 14:31:50 +0900 | [diff] [blame] | 725 | vpaths: vpaths, |
Fumitoshi Ukai | f8efa0a | 2015-03-30 18:10:11 +0900 | [diff] [blame] | 726 | }, nil |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 727 | } |