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 | 845cb29 | 2022-01-18 10:31:14 -0800 | [diff] [blame] | 57 | func (mi moduleInfo) name() string { |
| 58 | return fmt.Sprintf("%q", MakePath2ModuleName(mi.originalPath)) |
| 59 | } |
| 60 | |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 61 | type inheritedModule interface { |
| 62 | name() string |
| 63 | entryName() string |
| 64 | emitSelect(gctx *generationContext) |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame] | 65 | pathExpr() starlarkExpr |
| 66 | needsLoadCheck() bool |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | type inheritedStaticModule struct { |
| 70 | *moduleInfo |
| 71 | loadAlways bool |
| 72 | } |
| 73 | |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 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 |
Cole Faust | 069aba6 | 2022-01-26 17:47:33 -0800 | [diff] [blame] | 89 | location ErrorLocation |
| 90 | needsWarning bool |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | func (i inheritedDynamicModule) name() string { |
| 94 | return "_varmod" |
| 95 | } |
| 96 | |
| 97 | func (i inheritedDynamicModule) entryName() string { |
| 98 | return i.name() + "_init" |
| 99 | } |
| 100 | |
| 101 | func (i inheritedDynamicModule) emitSelect(gctx *generationContext) { |
Cole Faust | 069aba6 | 2022-01-26 17:47:33 -0800 | [diff] [blame] | 102 | if i.needsWarning { |
| 103 | gctx.newLine() |
Cole Faust | f4e72cf | 2022-02-08 12:49:37 -0800 | [diff] [blame] | 104 | gctx.writef("%s.mkwarning(%q, %q)", baseName, i.location, "Please avoid starting an include path with a variable. See https://source.android.com/setup/build/bazel/product_config/issues/includes for details.") |
Cole Faust | 069aba6 | 2022-01-26 17:47:33 -0800 | [diff] [blame] | 105 | } |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 106 | gctx.newLine() |
| 107 | gctx.writef("_entry = {") |
| 108 | gctx.indentLevel++ |
| 109 | for _, mi := range i.candidateModules { |
| 110 | gctx.newLine() |
Sasha Smundak | 845cb29 | 2022-01-18 10:31:14 -0800 | [diff] [blame] | 111 | gctx.writef(`"%s": (%s, %s),`, mi.originalPath, mi.name(), mi.entryName()) |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 112 | } |
| 113 | gctx.indentLevel-- |
| 114 | gctx.newLine() |
| 115 | gctx.write("}.get(") |
| 116 | i.path.emit(gctx) |
| 117 | gctx.write(")") |
| 118 | gctx.newLine() |
| 119 | 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] | 120 | } |
| 121 | |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame] | 122 | func (i inheritedDynamicModule) pathExpr() starlarkExpr { |
| 123 | return &i.path |
| 124 | } |
| 125 | |
| 126 | func (i inheritedDynamicModule) needsLoadCheck() bool { |
| 127 | return true |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 130 | type inheritNode struct { |
Sasha Smundak | 868c5e3 | 2021-09-23 16:20:58 -0700 | [diff] [blame] | 131 | module inheritedModule |
| 132 | loadAlways bool |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | func (inn *inheritNode) emit(gctx *generationContext) { |
| 136 | // Unconditional case: |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame] | 137 | // maybe check that loaded |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 138 | // rblf.inherit(handle, <module>, module_init) |
| 139 | // Conditional case: |
| 140 | // if <module>_init != None: |
| 141 | // same as above |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 142 | inn.module.emitSelect(gctx) |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 143 | name := inn.module.name() |
| 144 | entry := inn.module.entryName() |
Sasha Smundak | 868c5e3 | 2021-09-23 16:20:58 -0700 | [diff] [blame] | 145 | if inn.loadAlways { |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame] | 146 | gctx.emitLoadCheck(inn.module) |
| 147 | gctx.newLine() |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 148 | gctx.writef("%s(handle, %s, %s)", cfnInherit, name, entry) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 149 | return |
| 150 | } |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 151 | |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame] | 152 | gctx.newLine() |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 153 | gctx.writef("if %s:", entry) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 154 | gctx.indentLevel++ |
| 155 | gctx.newLine() |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 156 | gctx.writef("%s(handle, %s, %s)", cfnInherit, name, entry) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 157 | gctx.indentLevel-- |
| 158 | } |
| 159 | |
| 160 | type includeNode struct { |
Sasha Smundak | 868c5e3 | 2021-09-23 16:20:58 -0700 | [diff] [blame] | 161 | module inheritedModule |
| 162 | loadAlways bool |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | func (inn *includeNode) emit(gctx *generationContext) { |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 166 | inn.module.emitSelect(gctx) |
| 167 | entry := inn.module.entryName() |
Sasha Smundak | 868c5e3 | 2021-09-23 16:20:58 -0700 | [diff] [blame] | 168 | if inn.loadAlways { |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame] | 169 | gctx.emitLoadCheck(inn.module) |
| 170 | gctx.newLine() |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 171 | gctx.writef("%s(g, handle)", entry) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 172 | return |
| 173 | } |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 174 | |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame] | 175 | gctx.newLine() |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 176 | gctx.writef("if %s != None:", entry) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 177 | gctx.indentLevel++ |
| 178 | gctx.newLine() |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 179 | gctx.writef("%s(g, handle)", entry) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 180 | gctx.indentLevel-- |
| 181 | } |
| 182 | |
| 183 | type assignmentFlavor int |
| 184 | |
| 185 | const ( |
| 186 | // Assignment flavors |
Cole Faust | e2a3798 | 2022-03-09 16:00:17 -0800 | [diff] [blame^] | 187 | asgnSet assignmentFlavor = iota // := or = |
| 188 | asgnMaybeSet assignmentFlavor = iota // ?= |
| 189 | asgnAppend assignmentFlavor = iota // += |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 190 | ) |
| 191 | |
| 192 | type assignmentNode struct { |
| 193 | lhs variable |
| 194 | value starlarkExpr |
| 195 | mkValue *mkparser.MakeString |
| 196 | flavor assignmentFlavor |
Sasha Smundak | 422b614 | 2021-11-11 18:31:59 -0800 | [diff] [blame] | 197 | location ErrorLocation |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 198 | isTraced bool |
| 199 | previous *assignmentNode |
| 200 | } |
| 201 | |
| 202 | func (asgn *assignmentNode) emit(gctx *generationContext) { |
| 203 | gctx.newLine() |
| 204 | gctx.inAssignment = true |
| 205 | asgn.lhs.emitSet(gctx, asgn) |
| 206 | gctx.inAssignment = false |
| 207 | |
| 208 | if asgn.isTraced { |
| 209 | gctx.newLine() |
| 210 | gctx.tracedCount++ |
| 211 | gctx.writef(`print("%s.%d: %s := ", `, gctx.starScript.mkFile, gctx.tracedCount, asgn.lhs.name()) |
| 212 | asgn.lhs.emitGet(gctx, true) |
| 213 | gctx.writef(")") |
| 214 | } |
| 215 | } |
| 216 | |
Cole Faust | e2a3798 | 2022-03-09 16:00:17 -0800 | [diff] [blame^] | 217 | func (asgn *assignmentNode) isSelfReferential() bool { |
| 218 | if asgn.flavor == asgnAppend { |
| 219 | return true |
| 220 | } |
| 221 | isSelfReferential := false |
| 222 | asgn.value.transform(func(expr starlarkExpr) starlarkExpr { |
| 223 | if ref, ok := expr.(*variableRefExpr); ok && ref.ref.name() == asgn.lhs.name() { |
| 224 | isSelfReferential = true |
| 225 | } |
| 226 | return nil |
| 227 | }) |
| 228 | return isSelfReferential |
| 229 | } |
| 230 | |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 231 | type exprNode struct { |
| 232 | expr starlarkExpr |
| 233 | } |
| 234 | |
| 235 | func (exn *exprNode) emit(gctx *generationContext) { |
| 236 | gctx.newLine() |
| 237 | exn.expr.emit(gctx) |
| 238 | } |
| 239 | |
| 240 | type ifNode struct { |
| 241 | isElif bool // true if this is 'elif' statement |
| 242 | expr starlarkExpr |
| 243 | } |
| 244 | |
| 245 | func (in *ifNode) emit(gctx *generationContext) { |
| 246 | ifElif := "if " |
| 247 | if in.isElif { |
| 248 | ifElif = "elif " |
| 249 | } |
| 250 | |
| 251 | gctx.newLine() |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 252 | gctx.write(ifElif) |
| 253 | in.expr.emit(gctx) |
| 254 | gctx.write(":") |
| 255 | } |
| 256 | |
| 257 | type elseNode struct{} |
| 258 | |
| 259 | func (br *elseNode) emit(gctx *generationContext) { |
| 260 | gctx.newLine() |
| 261 | gctx.write("else:") |
| 262 | } |
| 263 | |
| 264 | // switchCase represents as single if/elseif/else branch. All the necessary |
| 265 | // info about flavor (if/elseif/else) is supposed to be kept in `gate`. |
| 266 | type switchCase struct { |
| 267 | gate starlarkNode |
| 268 | nodes []starlarkNode |
| 269 | } |
| 270 | |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 271 | func (cb *switchCase) emit(gctx *generationContext) { |
| 272 | cb.gate.emit(gctx) |
| 273 | gctx.indentLevel++ |
| 274 | hasStatements := false |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 275 | for _, node := range cb.nodes { |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 276 | if _, ok := node.(*commentNode); !ok { |
| 277 | hasStatements = true |
| 278 | } |
| 279 | node.emit(gctx) |
| 280 | } |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 281 | if !hasStatements { |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 282 | gctx.emitPass() |
| 283 | } |
| 284 | gctx.indentLevel-- |
| 285 | } |
| 286 | |
| 287 | // A single complete if ... elseif ... else ... endif sequences |
| 288 | type switchNode struct { |
| 289 | ssCases []*switchCase |
| 290 | } |
| 291 | |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 292 | func (ssw *switchNode) emit(gctx *generationContext) { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 293 | for _, ssCase := range ssw.ssCases { |
| 294 | ssCase.emit(gctx) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 295 | } |
| 296 | } |