Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1 | // Copyright 2021 Google LLC |
| 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 | |
| 15 | package mk2rbc |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strings" |
| 20 | |
| 21 | mkparser "android/soong/androidmk/parser" |
| 22 | ) |
| 23 | |
| 24 | // A parsed node for which starlark code will be generated |
| 25 | // by calling emit(). |
| 26 | type starlarkNode interface { |
| 27 | emit(ctx *generationContext) |
| 28 | } |
| 29 | |
| 30 | // Types used to keep processed makefile data: |
| 31 | type commentNode struct { |
| 32 | text string |
| 33 | } |
| 34 | |
| 35 | func (c *commentNode) emit(gctx *generationContext) { |
| 36 | chunks := strings.Split(c.text, "\\\n") |
| 37 | gctx.newLine() |
| 38 | gctx.write(chunks[0]) // It has '#' at the beginning already. |
| 39 | for _, chunk := range chunks[1:] { |
| 40 | gctx.newLine() |
| 41 | gctx.write("#", chunk) |
| 42 | } |
| 43 | } |
| 44 | |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 45 | type moduleInfo struct { |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 46 | path string // Converted Starlark file path |
| 47 | originalPath string // Makefile file path |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 48 | moduleLocalName string |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 49 | optional bool |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame^] | 50 | missing bool // a module may not exist if a module that depends on it is loaded dynamically |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 53 | func (im moduleInfo) entryName() string { |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 54 | return im.moduleLocalName + "_init" |
| 55 | } |
| 56 | |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 57 | type inheritedModule interface { |
| 58 | name() string |
| 59 | entryName() string |
| 60 | emitSelect(gctx *generationContext) |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame^] | 61 | pathExpr() starlarkExpr |
| 62 | needsLoadCheck() bool |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | type inheritedStaticModule struct { |
| 66 | *moduleInfo |
| 67 | loadAlways bool |
| 68 | } |
| 69 | |
| 70 | func (im inheritedStaticModule) name() string { |
| 71 | return fmt.Sprintf("%q", MakePath2ModuleName(im.originalPath)) |
| 72 | } |
| 73 | |
| 74 | func (im inheritedStaticModule) emitSelect(_ *generationContext) { |
| 75 | } |
| 76 | |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame^] | 77 | func (im inheritedStaticModule) pathExpr() starlarkExpr { |
| 78 | return &stringLiteralExpr{im.path} |
| 79 | } |
| 80 | |
| 81 | func (im inheritedStaticModule) needsLoadCheck() bool { |
| 82 | return im.missing |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | type inheritedDynamicModule struct { |
| 86 | path interpolateExpr |
| 87 | candidateModules []*moduleInfo |
| 88 | loadAlways bool |
| 89 | } |
| 90 | |
| 91 | func (i inheritedDynamicModule) name() string { |
| 92 | return "_varmod" |
| 93 | } |
| 94 | |
| 95 | func (i inheritedDynamicModule) entryName() string { |
| 96 | return i.name() + "_init" |
| 97 | } |
| 98 | |
| 99 | func (i inheritedDynamicModule) emitSelect(gctx *generationContext) { |
| 100 | gctx.newLine() |
| 101 | gctx.writef("_entry = {") |
| 102 | gctx.indentLevel++ |
| 103 | for _, mi := range i.candidateModules { |
| 104 | gctx.newLine() |
| 105 | gctx.writef(`"%s": (%q, %s),`, mi.originalPath, mi.moduleLocalName, mi.entryName()) |
| 106 | } |
| 107 | gctx.indentLevel-- |
| 108 | gctx.newLine() |
| 109 | gctx.write("}.get(") |
| 110 | i.path.emit(gctx) |
| 111 | gctx.write(")") |
| 112 | gctx.newLine() |
| 113 | gctx.writef("(%s, %s) = _entry if _entry else (None, None)", i.name(), i.entryName()) |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame^] | 116 | func (i inheritedDynamicModule) pathExpr() starlarkExpr { |
| 117 | return &i.path |
| 118 | } |
| 119 | |
| 120 | func (i inheritedDynamicModule) needsLoadCheck() bool { |
| 121 | return true |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 124 | type inheritNode struct { |
Sasha Smundak | 868c5e3 | 2021-09-23 16:20:58 -0700 | [diff] [blame] | 125 | module inheritedModule |
| 126 | loadAlways bool |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | func (inn *inheritNode) emit(gctx *generationContext) { |
| 130 | // Unconditional case: |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame^] | 131 | // maybe check that loaded |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 132 | // rblf.inherit(handle, <module>, module_init) |
| 133 | // Conditional case: |
| 134 | // if <module>_init != None: |
| 135 | // same as above |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 136 | inn.module.emitSelect(gctx) |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 137 | name := inn.module.name() |
| 138 | entry := inn.module.entryName() |
Sasha Smundak | 868c5e3 | 2021-09-23 16:20:58 -0700 | [diff] [blame] | 139 | if inn.loadAlways { |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame^] | 140 | gctx.emitLoadCheck(inn.module) |
| 141 | gctx.newLine() |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 142 | gctx.writef("%s(handle, %s, %s)", cfnInherit, name, entry) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 143 | return |
| 144 | } |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 145 | |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame^] | 146 | gctx.newLine() |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 147 | gctx.writef("if %s:", entry) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 148 | gctx.indentLevel++ |
| 149 | gctx.newLine() |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 150 | gctx.writef("%s(handle, %s, %s)", cfnInherit, name, entry) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 151 | gctx.indentLevel-- |
| 152 | } |
| 153 | |
| 154 | type includeNode struct { |
Sasha Smundak | 868c5e3 | 2021-09-23 16:20:58 -0700 | [diff] [blame] | 155 | module inheritedModule |
| 156 | loadAlways bool |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | func (inn *includeNode) emit(gctx *generationContext) { |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 160 | inn.module.emitSelect(gctx) |
| 161 | entry := inn.module.entryName() |
Sasha Smundak | 868c5e3 | 2021-09-23 16:20:58 -0700 | [diff] [blame] | 162 | if inn.loadAlways { |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame^] | 163 | gctx.emitLoadCheck(inn.module) |
| 164 | gctx.newLine() |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 165 | gctx.writef("%s(g, handle)", entry) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 166 | return |
| 167 | } |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 168 | |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame^] | 169 | gctx.newLine() |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 170 | gctx.writef("if %s != None:", entry) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 171 | gctx.indentLevel++ |
| 172 | gctx.newLine() |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 173 | gctx.writef("%s(g, handle)", entry) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 174 | gctx.indentLevel-- |
| 175 | } |
| 176 | |
| 177 | type assignmentFlavor int |
| 178 | |
| 179 | const ( |
| 180 | // Assignment flavors |
| 181 | asgnSet assignmentFlavor = iota // := or = |
| 182 | asgnMaybeSet assignmentFlavor = iota // ?= and variable may be unset |
| 183 | asgnAppend assignmentFlavor = iota // += and variable has been set before |
| 184 | asgnMaybeAppend assignmentFlavor = iota // += and variable may be unset |
| 185 | ) |
| 186 | |
| 187 | type assignmentNode struct { |
| 188 | lhs variable |
| 189 | value starlarkExpr |
| 190 | mkValue *mkparser.MakeString |
| 191 | flavor assignmentFlavor |
Sasha Smundak | 422b614 | 2021-11-11 18:31:59 -0800 | [diff] [blame] | 192 | location ErrorLocation |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 193 | isTraced bool |
| 194 | previous *assignmentNode |
| 195 | } |
| 196 | |
| 197 | func (asgn *assignmentNode) emit(gctx *generationContext) { |
| 198 | gctx.newLine() |
| 199 | gctx.inAssignment = true |
| 200 | asgn.lhs.emitSet(gctx, asgn) |
| 201 | gctx.inAssignment = false |
| 202 | |
| 203 | if asgn.isTraced { |
| 204 | gctx.newLine() |
| 205 | gctx.tracedCount++ |
| 206 | gctx.writef(`print("%s.%d: %s := ", `, gctx.starScript.mkFile, gctx.tracedCount, asgn.lhs.name()) |
| 207 | asgn.lhs.emitGet(gctx, true) |
| 208 | gctx.writef(")") |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | type exprNode struct { |
| 213 | expr starlarkExpr |
| 214 | } |
| 215 | |
| 216 | func (exn *exprNode) emit(gctx *generationContext) { |
| 217 | gctx.newLine() |
| 218 | exn.expr.emit(gctx) |
| 219 | } |
| 220 | |
| 221 | type ifNode struct { |
| 222 | isElif bool // true if this is 'elif' statement |
| 223 | expr starlarkExpr |
| 224 | } |
| 225 | |
| 226 | func (in *ifNode) emit(gctx *generationContext) { |
| 227 | ifElif := "if " |
| 228 | if in.isElif { |
| 229 | ifElif = "elif " |
| 230 | } |
| 231 | |
| 232 | gctx.newLine() |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 233 | gctx.write(ifElif) |
| 234 | in.expr.emit(gctx) |
| 235 | gctx.write(":") |
| 236 | } |
| 237 | |
| 238 | type elseNode struct{} |
| 239 | |
| 240 | func (br *elseNode) emit(gctx *generationContext) { |
| 241 | gctx.newLine() |
| 242 | gctx.write("else:") |
| 243 | } |
| 244 | |
| 245 | // switchCase represents as single if/elseif/else branch. All the necessary |
| 246 | // info about flavor (if/elseif/else) is supposed to be kept in `gate`. |
| 247 | type switchCase struct { |
| 248 | gate starlarkNode |
| 249 | nodes []starlarkNode |
| 250 | } |
| 251 | |
| 252 | func (cb *switchCase) newNode(node starlarkNode) { |
| 253 | cb.nodes = append(cb.nodes, node) |
| 254 | } |
| 255 | |
| 256 | func (cb *switchCase) emit(gctx *generationContext) { |
| 257 | cb.gate.emit(gctx) |
| 258 | gctx.indentLevel++ |
| 259 | hasStatements := false |
| 260 | emitNode := func(node starlarkNode) { |
| 261 | if _, ok := node.(*commentNode); !ok { |
| 262 | hasStatements = true |
| 263 | } |
| 264 | node.emit(gctx) |
| 265 | } |
| 266 | if len(cb.nodes) > 0 { |
| 267 | emitNode(cb.nodes[0]) |
| 268 | for _, node := range cb.nodes[1:] { |
| 269 | emitNode(node) |
| 270 | } |
| 271 | if !hasStatements { |
| 272 | gctx.emitPass() |
| 273 | } |
| 274 | } else { |
| 275 | gctx.emitPass() |
| 276 | } |
| 277 | gctx.indentLevel-- |
| 278 | } |
| 279 | |
| 280 | // A single complete if ... elseif ... else ... endif sequences |
| 281 | type switchNode struct { |
| 282 | ssCases []*switchCase |
| 283 | } |
| 284 | |
| 285 | func (ssw *switchNode) newNode(node starlarkNode) { |
| 286 | switch br := node.(type) { |
| 287 | case *switchCase: |
| 288 | ssw.ssCases = append(ssw.ssCases, br) |
| 289 | default: |
| 290 | panic(fmt.Errorf("expected switchCase node, got %t", br)) |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | func (ssw *switchNode) emit(gctx *generationContext) { |
| 295 | if len(ssw.ssCases) == 0 { |
| 296 | gctx.emitPass() |
| 297 | } else { |
| 298 | ssw.ssCases[0].emit(gctx) |
| 299 | for _, ssCase := range ssw.ssCases[1:] { |
| 300 | ssCase.emit(gctx) |
| 301 | } |
| 302 | } |
| 303 | } |