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 | // Convert makefile containing device configuration to Starlark file |
| 16 | // The conversion can handle the following constructs in a makefile: |
| 17 | // * comments |
| 18 | // * simple variable assignments |
| 19 | // * $(call init-product,<file>) |
| 20 | // * $(call inherit-product-if-exists |
| 21 | // * if directives |
| 22 | // All other constructs are carried over to the output starlark file as comments. |
| 23 | // |
| 24 | package mk2rbc |
| 25 | |
| 26 | import ( |
| 27 | "bytes" |
| 28 | "fmt" |
| 29 | "io" |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 30 | "io/fs" |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 31 | "io/ioutil" |
| 32 | "os" |
| 33 | "path/filepath" |
| 34 | "regexp" |
| 35 | "strconv" |
| 36 | "strings" |
| 37 | "text/scanner" |
| 38 | |
| 39 | mkparser "android/soong/androidmk/parser" |
| 40 | ) |
| 41 | |
| 42 | const ( |
Sasha Smundak | 6d852dd | 2021-09-27 20:34:39 -0700 | [diff] [blame] | 43 | annotationCommentPrefix = "RBC#" |
| 44 | baseUri = "//build/make/core:product_config.rbc" |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 45 | // The name of the struct exported by the product_config.rbc |
| 46 | // that contains the functions and variables available to |
| 47 | // product configuration Starlark files. |
| 48 | baseName = "rblf" |
| 49 | |
Sasha Smundak | 65b547e | 2021-09-17 15:35:41 -0700 | [diff] [blame] | 50 | soongNsPrefix = "SOONG_CONFIG_" |
| 51 | |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 52 | // And here are the functions and variables: |
| 53 | cfnGetCfg = baseName + ".cfg" |
| 54 | cfnMain = baseName + ".product_configuration" |
Cole Faust | 6ed7cb4 | 2021-10-07 17:08:46 -0700 | [diff] [blame] | 55 | cfnBoardMain = baseName + ".board_configuration" |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 56 | cfnPrintVars = baseName + ".printvars" |
| 57 | cfnWarning = baseName + ".warning" |
| 58 | cfnLocalAppend = baseName + ".local_append" |
| 59 | cfnLocalSetDefault = baseName + ".local_set_default" |
| 60 | cfnInherit = baseName + ".inherit" |
| 61 | cfnSetListDefault = baseName + ".setdefault" |
| 62 | ) |
| 63 | |
| 64 | const ( |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 65 | soongConfigAppend = "soong_config_append" |
| 66 | soongConfigAssign = "soong_config_set" |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 67 | ) |
| 68 | |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 69 | var knownFunctions = map[string]interface { |
| 70 | parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 71 | }{ |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 72 | "abspath": &simpleCallParser{name: baseName + ".abspath", returnType: starlarkTypeString, addGlobals: false}, |
| 73 | "add_soong_config_namespace": &simpleCallParser{name: baseName + ".soong_config_namespace", returnType: starlarkTypeVoid, addGlobals: true}, |
| 74 | "add_soong_config_var_value": &simpleCallParser{name: baseName + ".soong_config_set", returnType: starlarkTypeVoid, addGlobals: true}, |
| 75 | soongConfigAssign: &simpleCallParser{name: baseName + ".soong_config_set", returnType: starlarkTypeVoid, addGlobals: true}, |
| 76 | soongConfigAppend: &simpleCallParser{name: baseName + ".soong_config_append", returnType: starlarkTypeVoid, addGlobals: true}, |
| 77 | "soong_config_get": &simpleCallParser{name: baseName + ".soong_config_get", returnType: starlarkTypeString, addGlobals: true}, |
| 78 | "add-to-product-copy-files-if-exists": &simpleCallParser{name: baseName + ".copy_if_exists", returnType: starlarkTypeList, addGlobals: false}, |
| 79 | "addprefix": &simpleCallParser{name: baseName + ".addprefix", returnType: starlarkTypeList, addGlobals: false}, |
| 80 | "addsuffix": &simpleCallParser{name: baseName + ".addsuffix", returnType: starlarkTypeList, addGlobals: false}, |
| 81 | "copy-files": &simpleCallParser{name: baseName + ".copy_files", returnType: starlarkTypeList, addGlobals: false}, |
| 82 | "dir": &simpleCallParser{name: baseName + ".dir", returnType: starlarkTypeList, addGlobals: false}, |
| 83 | "dist-for-goals": &simpleCallParser{name: baseName + ".mkdist_for_goals", returnType: starlarkTypeVoid, addGlobals: true}, |
| 84 | "enforce-product-packages-exist": &simpleCallParser{name: baseName + ".enforce_product_packages_exist", returnType: starlarkTypeVoid, addGlobals: false}, |
| 85 | "error": &makeControlFuncParser{name: baseName + ".mkerror"}, |
| 86 | "findstring": &simpleCallParser{name: baseName + ".findstring", returnType: starlarkTypeInt, addGlobals: false}, |
| 87 | "find-copy-subdir-files": &simpleCallParser{name: baseName + ".find_and_copy", returnType: starlarkTypeList, addGlobals: false}, |
| 88 | "filter": &simpleCallParser{name: baseName + ".filter", returnType: starlarkTypeList, addGlobals: false}, |
| 89 | "filter-out": &simpleCallParser{name: baseName + ".filter_out", returnType: starlarkTypeList, addGlobals: false}, |
| 90 | "firstword": &firstOrLastwordCallParser{isLastWord: false}, |
| 91 | "foreach": &foreachCallPaser{}, |
| 92 | "if": &ifCallParser{}, |
| 93 | "info": &makeControlFuncParser{name: baseName + ".mkinfo"}, |
Cole Faust | b2e0b60 | 2022-01-07 15:46:58 -0800 | [diff] [blame] | 94 | "is-board-platform": &simpleCallParser{name: baseName + ".board_platform_is", returnType: starlarkTypeBool, addGlobals: true}, |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 95 | "is-board-platform2": &simpleCallParser{name: baseName + ".board_platform_is", returnType: starlarkTypeBool, addGlobals: true}, |
Cole Faust | b2e0b60 | 2022-01-07 15:46:58 -0800 | [diff] [blame] | 96 | "is-board-platform-in-list": &simpleCallParser{name: baseName + ".board_platform_in", returnType: starlarkTypeBool, addGlobals: true}, |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 97 | "is-board-platform-in-list2": &simpleCallParser{name: baseName + ".board_platform_in", returnType: starlarkTypeBool, addGlobals: true}, |
| 98 | "is-product-in-list": &isProductInListCallParser{}, |
| 99 | "is-vendor-board-platform": &isVendorBoardPlatformCallParser{}, |
| 100 | "is-vendor-board-qcom": &isVendorBoardQcomCallParser{}, |
| 101 | "lastword": &firstOrLastwordCallParser{isLastWord: true}, |
| 102 | "notdir": &simpleCallParser{name: baseName + ".notdir", returnType: starlarkTypeString, addGlobals: false}, |
Cole Faust | b1103e2 | 2022-01-06 15:22:05 -0800 | [diff] [blame] | 103 | "math_max": &mathMaxOrMinCallParser{function: "max"}, |
| 104 | "math_min": &mathMaxOrMinCallParser{function: "min"}, |
| 105 | "math_gt_or_eq": &mathComparisonCallParser{op: ">="}, |
| 106 | "math_gt": &mathComparisonCallParser{op: ">"}, |
| 107 | "math_lt": &mathComparisonCallParser{op: "<"}, |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 108 | "my-dir": &myDirCallParser{}, |
| 109 | "patsubst": &substCallParser{fname: "patsubst"}, |
| 110 | "product-copy-files-by-pattern": &simpleCallParser{name: baseName + ".product_copy_files_by_pattern", returnType: starlarkTypeList, addGlobals: false}, |
| 111 | "require-artifacts-in-path": &simpleCallParser{name: baseName + ".require_artifacts_in_path", returnType: starlarkTypeVoid, addGlobals: false}, |
| 112 | "require-artifacts-in-path-relaxed": &simpleCallParser{name: baseName + ".require_artifacts_in_path_relaxed", returnType: starlarkTypeVoid, addGlobals: false}, |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 113 | // TODO(asmundak): remove it once all calls are removed from configuration makefiles. see b/183161002 |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 114 | "shell": &shellCallParser{}, |
| 115 | "strip": &simpleCallParser{name: baseName + ".mkstrip", returnType: starlarkTypeString, addGlobals: false}, |
| 116 | "subst": &substCallParser{fname: "subst"}, |
| 117 | "warning": &makeControlFuncParser{name: baseName + ".mkwarning"}, |
| 118 | "word": &wordCallParser{}, |
| 119 | "wildcard": &simpleCallParser{name: baseName + ".expand_wildcard", returnType: starlarkTypeList, addGlobals: false}, |
| 120 | } |
| 121 | |
| 122 | // These are functions that we don't implement conversions for, but |
| 123 | // we allow seeing their definitions in the product config files. |
| 124 | var ignoredDefines = map[string]bool{ |
| 125 | "find-word-in-list": true, // internal macro |
| 126 | "get-vendor-board-platforms": true, // internal macro, used by is-board-platform, etc. |
| 127 | "is-android-codename": true, // unused by product config |
| 128 | "is-android-codename-in-list": true, // unused by product config |
| 129 | "is-chipset-in-board-platform": true, // unused by product config |
| 130 | "is-chipset-prefix-in-board-platform": true, // unused by product config |
| 131 | "is-not-board-platform": true, // defined but never used |
| 132 | "is-platform-sdk-version-at-least": true, // unused by product config |
| 133 | "match-prefix": true, // internal macro |
| 134 | "match-word": true, // internal macro |
| 135 | "match-word-in-list": true, // internal macro |
| 136 | "tb-modules": true, // defined in hardware/amlogic/tb_modules/tb_detect.mk, unused |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Cole Faust | b0d32ab | 2021-12-09 14:00:59 -0800 | [diff] [blame] | 139 | var identifierFullMatchRegex = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$") |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 140 | |
| 141 | // Conversion request parameters |
| 142 | type Request struct { |
Sasha Smundak | 422b614 | 2021-11-11 18:31:59 -0800 | [diff] [blame] | 143 | MkFile string // file to convert |
| 144 | Reader io.Reader // if set, read input from this stream instead |
Sasha Smundak | 422b614 | 2021-11-11 18:31:59 -0800 | [diff] [blame] | 145 | OutputSuffix string // generated Starlark files suffix |
| 146 | OutputDir string // if set, root of the output hierarchy |
| 147 | ErrorLogger ErrorLogger |
| 148 | TracedVariables []string // trace assignment to these variables |
| 149 | TraceCalls bool |
| 150 | SourceFS fs.FS |
| 151 | MakefileFinder MakefileFinder |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Sasha Smundak | 7d934b9 | 2021-11-10 12:20:01 -0800 | [diff] [blame] | 154 | // ErrorLogger prints errors and gathers error statistics. |
| 155 | // Its NewError function is called on every error encountered during the conversion. |
| 156 | type ErrorLogger interface { |
Sasha Smundak | 422b614 | 2021-11-11 18:31:59 -0800 | [diff] [blame] | 157 | NewError(el ErrorLocation, node mkparser.Node, text string, args ...interface{}) |
| 158 | } |
| 159 | |
| 160 | type ErrorLocation struct { |
| 161 | MkFile string |
| 162 | MkLine int |
| 163 | } |
| 164 | |
| 165 | func (el ErrorLocation) String() string { |
| 166 | return fmt.Sprintf("%s:%d", el.MkFile, el.MkLine) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | // Derives module name for a given file. It is base name |
| 170 | // (file name without suffix), with some characters replaced to make it a Starlark identifier |
| 171 | func moduleNameForFile(mkFile string) string { |
| 172 | base := strings.TrimSuffix(filepath.Base(mkFile), filepath.Ext(mkFile)) |
| 173 | // TODO(asmundak): what else can be in the product file names? |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 174 | return strings.NewReplacer("-", "_", ".", "_").Replace(base) |
| 175 | |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | func cloneMakeString(mkString *mkparser.MakeString) *mkparser.MakeString { |
| 179 | r := &mkparser.MakeString{StringPos: mkString.StringPos} |
| 180 | r.Strings = append(r.Strings, mkString.Strings...) |
| 181 | r.Variables = append(r.Variables, mkString.Variables...) |
| 182 | return r |
| 183 | } |
| 184 | |
| 185 | func isMakeControlFunc(s string) bool { |
| 186 | return s == "error" || s == "warning" || s == "info" |
| 187 | } |
| 188 | |
| 189 | // Starlark output generation context |
| 190 | type generationContext struct { |
| 191 | buf strings.Builder |
| 192 | starScript *StarlarkScript |
| 193 | indentLevel int |
| 194 | inAssignment bool |
| 195 | tracedCount int |
| 196 | } |
| 197 | |
| 198 | func NewGenerateContext(ss *StarlarkScript) *generationContext { |
| 199 | return &generationContext{starScript: ss} |
| 200 | } |
| 201 | |
| 202 | // emit returns generated script |
| 203 | func (gctx *generationContext) emit() string { |
| 204 | ss := gctx.starScript |
| 205 | |
| 206 | // The emitted code has the following layout: |
| 207 | // <initial comments> |
| 208 | // preamble, i.e., |
| 209 | // load statement for the runtime support |
| 210 | // load statement for each unique submodule pulled in by this one |
| 211 | // def init(g, handle): |
| 212 | // cfg = rblf.cfg(handle) |
| 213 | // <statements> |
| 214 | // <warning if conversion was not clean> |
| 215 | |
| 216 | iNode := len(ss.nodes) |
| 217 | for i, node := range ss.nodes { |
| 218 | if _, ok := node.(*commentNode); !ok { |
| 219 | iNode = i |
| 220 | break |
| 221 | } |
| 222 | node.emit(gctx) |
| 223 | } |
| 224 | |
| 225 | gctx.emitPreamble() |
| 226 | |
| 227 | gctx.newLine() |
| 228 | // The arguments passed to the init function are the global dictionary |
| 229 | // ('g') and the product configuration dictionary ('cfg') |
| 230 | gctx.write("def init(g, handle):") |
| 231 | gctx.indentLevel++ |
| 232 | if gctx.starScript.traceCalls { |
| 233 | gctx.newLine() |
| 234 | gctx.writef(`print(">%s")`, gctx.starScript.mkFile) |
| 235 | } |
| 236 | gctx.newLine() |
| 237 | gctx.writef("cfg = %s(handle)", cfnGetCfg) |
| 238 | for _, node := range ss.nodes[iNode:] { |
| 239 | node.emit(gctx) |
| 240 | } |
| 241 | |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 242 | if gctx.starScript.traceCalls { |
| 243 | gctx.newLine() |
| 244 | gctx.writef(`print("<%s")`, gctx.starScript.mkFile) |
| 245 | } |
| 246 | gctx.indentLevel-- |
| 247 | gctx.write("\n") |
| 248 | return gctx.buf.String() |
| 249 | } |
| 250 | |
| 251 | func (gctx *generationContext) emitPreamble() { |
| 252 | gctx.newLine() |
| 253 | gctx.writef("load(%q, %q)", baseUri, baseName) |
| 254 | // Emit exactly one load statement for each URI. |
| 255 | loadedSubConfigs := make(map[string]string) |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame] | 256 | for _, mi := range gctx.starScript.inherited { |
| 257 | uri := mi.path |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 258 | if m, ok := loadedSubConfigs[uri]; ok { |
| 259 | // No need to emit load statement, but fix module name. |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame] | 260 | mi.moduleLocalName = m |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 261 | continue |
| 262 | } |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame] | 263 | if mi.optional || mi.missing { |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 264 | uri += "|init" |
| 265 | } |
| 266 | gctx.newLine() |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame] | 267 | gctx.writef("load(%q, %s = \"init\")", uri, mi.entryName()) |
| 268 | loadedSubConfigs[uri] = mi.moduleLocalName |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 269 | } |
| 270 | gctx.write("\n") |
| 271 | } |
| 272 | |
| 273 | func (gctx *generationContext) emitPass() { |
| 274 | gctx.newLine() |
| 275 | gctx.write("pass") |
| 276 | } |
| 277 | |
| 278 | func (gctx *generationContext) write(ss ...string) { |
| 279 | for _, s := range ss { |
| 280 | gctx.buf.WriteString(s) |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | func (gctx *generationContext) writef(format string, args ...interface{}) { |
| 285 | gctx.write(fmt.Sprintf(format, args...)) |
| 286 | } |
| 287 | |
| 288 | func (gctx *generationContext) newLine() { |
| 289 | if gctx.buf.Len() == 0 { |
| 290 | return |
| 291 | } |
| 292 | gctx.write("\n") |
| 293 | gctx.writef("%*s", 2*gctx.indentLevel, "") |
| 294 | } |
| 295 | |
Sasha Smundak | 422b614 | 2021-11-11 18:31:59 -0800 | [diff] [blame] | 296 | func (gctx *generationContext) emitConversionError(el ErrorLocation, message string) { |
| 297 | gctx.writef(`rblf.mk2rbc_error("%s", %q)`, el, message) |
| 298 | } |
| 299 | |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame] | 300 | func (gctx *generationContext) emitLoadCheck(im inheritedModule) { |
| 301 | if !im.needsLoadCheck() { |
| 302 | return |
| 303 | } |
| 304 | gctx.newLine() |
| 305 | gctx.writef("if not %s:", im.entryName()) |
| 306 | gctx.indentLevel++ |
| 307 | gctx.newLine() |
| 308 | gctx.write(`rblf.mkerror("`, gctx.starScript.mkFile, `", "Cannot find %s" % (`) |
| 309 | im.pathExpr().emit(gctx) |
| 310 | gctx.write("))") |
| 311 | gctx.indentLevel-- |
| 312 | } |
| 313 | |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 314 | type knownVariable struct { |
| 315 | name string |
| 316 | class varClass |
| 317 | valueType starlarkType |
| 318 | } |
| 319 | |
| 320 | type knownVariables map[string]knownVariable |
| 321 | |
| 322 | func (pcv knownVariables) NewVariable(name string, varClass varClass, valueType starlarkType) { |
| 323 | v, exists := pcv[name] |
| 324 | if !exists { |
| 325 | pcv[name] = knownVariable{name, varClass, valueType} |
| 326 | return |
| 327 | } |
| 328 | // Conflict resolution: |
| 329 | // * config class trumps everything |
| 330 | // * any type trumps unknown type |
| 331 | match := varClass == v.class |
| 332 | if !match { |
| 333 | if varClass == VarClassConfig { |
| 334 | v.class = VarClassConfig |
| 335 | match = true |
| 336 | } else if v.class == VarClassConfig { |
| 337 | match = true |
| 338 | } |
| 339 | } |
| 340 | if valueType != v.valueType { |
| 341 | if valueType != starlarkTypeUnknown { |
| 342 | if v.valueType == starlarkTypeUnknown { |
| 343 | v.valueType = valueType |
| 344 | } else { |
| 345 | match = false |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | if !match { |
| 350 | fmt.Fprintf(os.Stderr, "cannot redefine %s as %v/%v (already defined as %v/%v)\n", |
| 351 | name, varClass, valueType, v.class, v.valueType) |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | // All known product variables. |
| 356 | var KnownVariables = make(knownVariables) |
| 357 | |
| 358 | func init() { |
| 359 | for _, kv := range []string{ |
| 360 | // Kernel-related variables that we know are lists. |
| 361 | "BOARD_VENDOR_KERNEL_MODULES", |
| 362 | "BOARD_VENDOR_RAMDISK_KERNEL_MODULES", |
| 363 | "BOARD_VENDOR_RAMDISK_KERNEL_MODULES_LOAD", |
| 364 | "BOARD_RECOVERY_KERNEL_MODULES", |
| 365 | // Other variables we knwo are lists |
| 366 | "ART_APEX_JARS", |
| 367 | } { |
| 368 | KnownVariables.NewVariable(kv, VarClassSoong, starlarkTypeList) |
| 369 | } |
| 370 | } |
| 371 | |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 372 | // Information about the generated Starlark script. |
| 373 | type StarlarkScript struct { |
Sasha Smundak | 422b614 | 2021-11-11 18:31:59 -0800 | [diff] [blame] | 374 | mkFile string |
| 375 | moduleName string |
| 376 | mkPos scanner.Position |
| 377 | nodes []starlarkNode |
| 378 | inherited []*moduleInfo |
| 379 | hasErrors bool |
Sasha Smundak | 422b614 | 2021-11-11 18:31:59 -0800 | [diff] [blame] | 380 | traceCalls bool // print enter/exit each init function |
| 381 | sourceFS fs.FS |
| 382 | makefileFinder MakefileFinder |
| 383 | nodeLocator func(pos mkparser.Pos) int |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 386 | // varAssignmentScope points to the last assignment for each variable |
| 387 | // in the current block. It is used during the parsing to chain |
| 388 | // the assignments to a variable together. |
| 389 | type varAssignmentScope struct { |
| 390 | outer *varAssignmentScope |
| 391 | vars map[string]*assignmentNode |
| 392 | } |
| 393 | |
| 394 | // parseContext holds the script we are generating and all the ephemeral data |
| 395 | // needed during the parsing. |
| 396 | type parseContext struct { |
| 397 | script *StarlarkScript |
| 398 | nodes []mkparser.Node // Makefile as parsed by mkparser |
| 399 | currentNodeIndex int // Node in it we are processing |
| 400 | ifNestLevel int |
| 401 | moduleNameCount map[string]int // count of imported modules with given basename |
| 402 | fatalError error |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 403 | outputSuffix string |
Sasha Smundak | 7d934b9 | 2021-11-10 12:20:01 -0800 | [diff] [blame] | 404 | errorLogger ErrorLogger |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 405 | tracedVariables map[string]bool // variables to be traced in the generated script |
| 406 | variables map[string]variable |
| 407 | varAssignments *varAssignmentScope |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 408 | outputDir string |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 409 | dependentModules map[string]*moduleInfo |
Sasha Smundak | 3deb968 | 2021-07-26 18:42:25 -0700 | [diff] [blame] | 410 | soongNamespaces map[string]map[string]bool |
Sasha Smundak | 6d852dd | 2021-09-27 20:34:39 -0700 | [diff] [blame] | 411 | includeTops []string |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | func newParseContext(ss *StarlarkScript, nodes []mkparser.Node) *parseContext { |
| 415 | predefined := []struct{ name, value string }{ |
| 416 | {"SRC_TARGET_DIR", filepath.Join("build", "make", "target")}, |
| 417 | {"LOCAL_PATH", filepath.Dir(ss.mkFile)}, |
Cole Faust | 9b6111a | 2022-02-02 15:38:33 -0800 | [diff] [blame^] | 418 | {"TOPDIR", ""}, // TOPDIR is just set to an empty string in cleanbuild.mk and core.mk |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 419 | // TODO(asmundak): maybe read it from build/make/core/envsetup.mk? |
| 420 | {"TARGET_COPY_OUT_SYSTEM", "system"}, |
| 421 | {"TARGET_COPY_OUT_SYSTEM_OTHER", "system_other"}, |
| 422 | {"TARGET_COPY_OUT_DATA", "data"}, |
| 423 | {"TARGET_COPY_OUT_ASAN", filepath.Join("data", "asan")}, |
| 424 | {"TARGET_COPY_OUT_OEM", "oem"}, |
| 425 | {"TARGET_COPY_OUT_RAMDISK", "ramdisk"}, |
| 426 | {"TARGET_COPY_OUT_DEBUG_RAMDISK", "debug_ramdisk"}, |
| 427 | {"TARGET_COPY_OUT_VENDOR_DEBUG_RAMDISK", "vendor_debug_ramdisk"}, |
| 428 | {"TARGET_COPY_OUT_TEST_HARNESS_RAMDISK", "test_harness_ramdisk"}, |
| 429 | {"TARGET_COPY_OUT_ROOT", "root"}, |
| 430 | {"TARGET_COPY_OUT_RECOVERY", "recovery"}, |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 431 | {"TARGET_COPY_OUT_VENDOR_RAMDISK", "vendor_ramdisk"}, |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 432 | // TODO(asmundak): to process internal config files, we need the following variables: |
| 433 | // BOARD_CONFIG_VENDOR_PATH |
| 434 | // TARGET_VENDOR |
| 435 | // target_base_product |
| 436 | // |
| 437 | |
| 438 | // the following utility variables are set in build/make/common/core.mk: |
| 439 | {"empty", ""}, |
| 440 | {"space", " "}, |
| 441 | {"comma", ","}, |
| 442 | {"newline", "\n"}, |
| 443 | {"pound", "#"}, |
| 444 | {"backslash", "\\"}, |
| 445 | } |
| 446 | ctx := &parseContext{ |
| 447 | script: ss, |
| 448 | nodes: nodes, |
| 449 | currentNodeIndex: 0, |
| 450 | ifNestLevel: 0, |
| 451 | moduleNameCount: make(map[string]int), |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 452 | variables: make(map[string]variable), |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 453 | dependentModules: make(map[string]*moduleInfo), |
Sasha Smundak | 3deb968 | 2021-07-26 18:42:25 -0700 | [diff] [blame] | 454 | soongNamespaces: make(map[string]map[string]bool), |
Cole Faust | 6c934f6 | 2022-01-06 15:51:12 -0800 | [diff] [blame] | 455 | includeTops: []string{}, |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 456 | } |
| 457 | ctx.pushVarAssignments() |
| 458 | for _, item := range predefined { |
| 459 | ctx.variables[item.name] = &predefinedVariable{ |
| 460 | baseVariable: baseVariable{nam: item.name, typ: starlarkTypeString}, |
| 461 | value: &stringLiteralExpr{item.value}, |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | return ctx |
| 466 | } |
| 467 | |
| 468 | func (ctx *parseContext) lastAssignment(name string) *assignmentNode { |
| 469 | for va := ctx.varAssignments; va != nil; va = va.outer { |
| 470 | if v, ok := va.vars[name]; ok { |
| 471 | return v |
| 472 | } |
| 473 | } |
| 474 | return nil |
| 475 | } |
| 476 | |
| 477 | func (ctx *parseContext) setLastAssignment(name string, asgn *assignmentNode) { |
| 478 | ctx.varAssignments.vars[name] = asgn |
| 479 | } |
| 480 | |
| 481 | func (ctx *parseContext) pushVarAssignments() { |
| 482 | va := &varAssignmentScope{ |
| 483 | outer: ctx.varAssignments, |
| 484 | vars: make(map[string]*assignmentNode), |
| 485 | } |
| 486 | ctx.varAssignments = va |
| 487 | } |
| 488 | |
| 489 | func (ctx *parseContext) popVarAssignments() { |
| 490 | ctx.varAssignments = ctx.varAssignments.outer |
| 491 | } |
| 492 | |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 493 | func (ctx *parseContext) hasNodes() bool { |
| 494 | return ctx.currentNodeIndex < len(ctx.nodes) |
| 495 | } |
| 496 | |
| 497 | func (ctx *parseContext) getNode() mkparser.Node { |
| 498 | if !ctx.hasNodes() { |
| 499 | return nil |
| 500 | } |
| 501 | node := ctx.nodes[ctx.currentNodeIndex] |
| 502 | ctx.currentNodeIndex++ |
| 503 | return node |
| 504 | } |
| 505 | |
| 506 | func (ctx *parseContext) backNode() { |
| 507 | if ctx.currentNodeIndex <= 0 { |
| 508 | panic("Cannot back off") |
| 509 | } |
| 510 | ctx.currentNodeIndex-- |
| 511 | } |
| 512 | |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 513 | func (ctx *parseContext) handleAssignment(a *mkparser.Assignment) []starlarkNode { |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 514 | // Handle only simple variables |
| 515 | if !a.Name.Const() { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 516 | return []starlarkNode{ctx.newBadNode(a, "Only simple variables are handled")} |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 517 | } |
| 518 | name := a.Name.Strings[0] |
Sasha Smundak | ea3bc3a | 2021-11-10 13:06:42 -0800 | [diff] [blame] | 519 | // The `override` directive |
| 520 | // override FOO := |
| 521 | // is parsed as an assignment to a variable named `override FOO`. |
| 522 | // There are very few places where `override` is used, just flag it. |
| 523 | if strings.HasPrefix(name, "override ") { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 524 | return []starlarkNode{ctx.newBadNode(a, "cannot handle override directive")} |
Sasha Smundak | ea3bc3a | 2021-11-10 13:06:42 -0800 | [diff] [blame] | 525 | } |
| 526 | |
Cole Faust | c00184e | 2021-11-08 12:08:57 -0800 | [diff] [blame] | 527 | // Soong configuration |
Sasha Smundak | 3deb968 | 2021-07-26 18:42:25 -0700 | [diff] [blame] | 528 | if strings.HasPrefix(name, soongNsPrefix) { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 529 | return ctx.handleSoongNsAssignment(strings.TrimPrefix(name, soongNsPrefix), a) |
Sasha Smundak | 3deb968 | 2021-07-26 18:42:25 -0700 | [diff] [blame] | 530 | } |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 531 | lhs := ctx.addVariable(name) |
| 532 | if lhs == nil { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 533 | return []starlarkNode{ctx.newBadNode(a, "unknown variable %s", name)} |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 534 | } |
| 535 | _, isTraced := ctx.tracedVariables[name] |
Sasha Smundak | 422b614 | 2021-11-11 18:31:59 -0800 | [diff] [blame] | 536 | asgn := &assignmentNode{lhs: lhs, mkValue: a.Value, isTraced: isTraced, location: ctx.errorLocation(a)} |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 537 | if lhs.valueType() == starlarkTypeUnknown { |
| 538 | // Try to divine variable type from the RHS |
| 539 | asgn.value = ctx.parseMakeString(a, a.Value) |
| 540 | if xBad, ok := asgn.value.(*badExpr); ok { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 541 | return []starlarkNode{&exprNode{xBad}} |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 542 | } |
| 543 | inferred_type := asgn.value.typ() |
| 544 | if inferred_type != starlarkTypeUnknown { |
Sasha Smundak | 9d011ab | 2021-07-09 16:00:57 -0700 | [diff] [blame] | 545 | lhs.setValueType(inferred_type) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | if lhs.valueType() == starlarkTypeList { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 549 | xConcat, xBad := ctx.buildConcatExpr(a) |
| 550 | if xBad != nil { |
| 551 | return []starlarkNode{&exprNode{expr: xBad}} |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 552 | } |
| 553 | switch len(xConcat.items) { |
| 554 | case 0: |
| 555 | asgn.value = &listExpr{} |
| 556 | case 1: |
| 557 | asgn.value = xConcat.items[0] |
| 558 | default: |
| 559 | asgn.value = xConcat |
| 560 | } |
| 561 | } else { |
| 562 | asgn.value = ctx.parseMakeString(a, a.Value) |
| 563 | if xBad, ok := asgn.value.(*badExpr); ok { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 564 | return []starlarkNode{&exprNode{expr: xBad}} |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 568 | asgn.previous = ctx.lastAssignment(name) |
| 569 | ctx.setLastAssignment(name, asgn) |
| 570 | switch a.Type { |
| 571 | case "=", ":=": |
| 572 | asgn.flavor = asgnSet |
| 573 | case "+=": |
| 574 | if asgn.previous == nil && !asgn.lhs.isPreset() { |
| 575 | asgn.flavor = asgnMaybeAppend |
| 576 | } else { |
| 577 | asgn.flavor = asgnAppend |
| 578 | } |
| 579 | case "?=": |
| 580 | asgn.flavor = asgnMaybeSet |
| 581 | default: |
| 582 | panic(fmt.Errorf("unexpected assignment type %s", a.Type)) |
| 583 | } |
| 584 | |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 585 | return []starlarkNode{asgn} |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 586 | } |
| 587 | |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 588 | func (ctx *parseContext) handleSoongNsAssignment(name string, asgn *mkparser.Assignment) []starlarkNode { |
Sasha Smundak | 3deb968 | 2021-07-26 18:42:25 -0700 | [diff] [blame] | 589 | val := ctx.parseMakeString(asgn, asgn.Value) |
| 590 | if xBad, ok := val.(*badExpr); ok { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 591 | return []starlarkNode{&exprNode{expr: xBad}} |
Sasha Smundak | 3deb968 | 2021-07-26 18:42:25 -0700 | [diff] [blame] | 592 | } |
Sasha Smundak | 3deb968 | 2021-07-26 18:42:25 -0700 | [diff] [blame] | 593 | |
| 594 | // Unfortunately, Soong namespaces can be set up by directly setting corresponding Make |
| 595 | // variables instead of via add_soong_config_namespace + add_soong_config_var_value. |
| 596 | // Try to divine the call from the assignment as follows: |
| 597 | if name == "NAMESPACES" { |
| 598 | // Upon seeng |
| 599 | // SOONG_CONFIG_NAMESPACES += foo |
| 600 | // remember that there is a namespace `foo` and act as we saw |
| 601 | // $(call add_soong_config_namespace,foo) |
| 602 | s, ok := maybeString(val) |
| 603 | if !ok { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 604 | return []starlarkNode{ctx.newBadNode(asgn, "cannot handle variables in SOONG_CONFIG_NAMESPACES assignment, please use add_soong_config_namespace instead")} |
Sasha Smundak | 3deb968 | 2021-07-26 18:42:25 -0700 | [diff] [blame] | 605 | } |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 606 | result := make([]starlarkNode, 0) |
Sasha Smundak | 3deb968 | 2021-07-26 18:42:25 -0700 | [diff] [blame] | 607 | for _, ns := range strings.Fields(s) { |
| 608 | ctx.addSoongNamespace(ns) |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 609 | result = append(result, &exprNode{&callExpr{ |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 610 | name: baseName + ".soong_config_namespace", |
| 611 | args: []starlarkExpr{&globalsExpr{}, &stringLiteralExpr{ns}}, |
Sasha Smundak | 3deb968 | 2021-07-26 18:42:25 -0700 | [diff] [blame] | 612 | returnType: starlarkTypeVoid, |
| 613 | }}) |
| 614 | } |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 615 | return result |
Sasha Smundak | 3deb968 | 2021-07-26 18:42:25 -0700 | [diff] [blame] | 616 | } else { |
| 617 | // Upon seeing |
| 618 | // SOONG_CONFIG_x_y = v |
| 619 | // find a namespace called `x` and act as if we encountered |
Cole Faust | c00184e | 2021-11-08 12:08:57 -0800 | [diff] [blame] | 620 | // $(call soong_config_set,x,y,v) |
Sasha Smundak | 3deb968 | 2021-07-26 18:42:25 -0700 | [diff] [blame] | 621 | // or check that `x_y` is a namespace, and then add the RHS of this assignment as variables in |
| 622 | // it. |
| 623 | // Emit an error in the ambiguous situation (namespaces `foo_bar` with a variable `baz` |
| 624 | // and `foo` with a variable `bar_baz`. |
| 625 | namespaceName := "" |
| 626 | if ctx.hasSoongNamespace(name) { |
| 627 | namespaceName = name |
| 628 | } |
| 629 | var varName string |
| 630 | for pos, ch := range name { |
| 631 | if !(ch == '_' && ctx.hasSoongNamespace(name[0:pos])) { |
| 632 | continue |
| 633 | } |
| 634 | if namespaceName != "" { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 635 | return []starlarkNode{ctx.newBadNode(asgn, "ambiguous soong namespace (may be either `%s` or `%s`)", namespaceName, name[0:pos])} |
Sasha Smundak | 3deb968 | 2021-07-26 18:42:25 -0700 | [diff] [blame] | 636 | } |
| 637 | namespaceName = name[0:pos] |
| 638 | varName = name[pos+1:] |
| 639 | } |
| 640 | if namespaceName == "" { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 641 | return []starlarkNode{ctx.newBadNode(asgn, "cannot figure out Soong namespace, please use add_soong_config_var_value macro instead")} |
Sasha Smundak | 3deb968 | 2021-07-26 18:42:25 -0700 | [diff] [blame] | 642 | } |
| 643 | if varName == "" { |
| 644 | // Remember variables in this namespace |
| 645 | s, ok := maybeString(val) |
| 646 | if !ok { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 647 | return []starlarkNode{ctx.newBadNode(asgn, "cannot handle variables in SOONG_CONFIG_ assignment, please use add_soong_config_var_value instead")} |
Sasha Smundak | 3deb968 | 2021-07-26 18:42:25 -0700 | [diff] [blame] | 648 | } |
| 649 | ctx.updateSoongNamespace(asgn.Type != "+=", namespaceName, strings.Fields(s)) |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 650 | return []starlarkNode{} |
Sasha Smundak | 3deb968 | 2021-07-26 18:42:25 -0700 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | // Finally, handle assignment to a namespace variable |
| 654 | if !ctx.hasNamespaceVar(namespaceName, varName) { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 655 | return []starlarkNode{ctx.newBadNode(asgn, "no %s variable in %s namespace, please use add_soong_config_var_value instead", varName, namespaceName)} |
Sasha Smundak | 3deb968 | 2021-07-26 18:42:25 -0700 | [diff] [blame] | 656 | } |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 657 | fname := baseName + "." + soongConfigAssign |
Sasha Smundak | 65b547e | 2021-09-17 15:35:41 -0700 | [diff] [blame] | 658 | if asgn.Type == "+=" { |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 659 | fname = baseName + "." + soongConfigAppend |
Sasha Smundak | 65b547e | 2021-09-17 15:35:41 -0700 | [diff] [blame] | 660 | } |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 661 | return []starlarkNode{&exprNode{&callExpr{ |
Sasha Smundak | 65b547e | 2021-09-17 15:35:41 -0700 | [diff] [blame] | 662 | name: fname, |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 663 | args: []starlarkExpr{&globalsExpr{}, &stringLiteralExpr{namespaceName}, &stringLiteralExpr{varName}, val}, |
Sasha Smundak | 3deb968 | 2021-07-26 18:42:25 -0700 | [diff] [blame] | 664 | returnType: starlarkTypeVoid, |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 665 | }}} |
Sasha Smundak | 3deb968 | 2021-07-26 18:42:25 -0700 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 669 | func (ctx *parseContext) buildConcatExpr(a *mkparser.Assignment) (*concatExpr, *badExpr) { |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 670 | xConcat := &concatExpr{} |
| 671 | var xItemList *listExpr |
| 672 | addToItemList := func(x ...starlarkExpr) { |
| 673 | if xItemList == nil { |
| 674 | xItemList = &listExpr{[]starlarkExpr{}} |
| 675 | } |
| 676 | xItemList.items = append(xItemList.items, x...) |
| 677 | } |
| 678 | finishItemList := func() { |
| 679 | if xItemList != nil { |
| 680 | xConcat.items = append(xConcat.items, xItemList) |
| 681 | xItemList = nil |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | items := a.Value.Words() |
| 686 | for _, item := range items { |
| 687 | // A function call in RHS is supposed to return a list, all other item |
| 688 | // expressions return individual elements. |
| 689 | switch x := ctx.parseMakeString(a, item).(type) { |
| 690 | case *badExpr: |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 691 | return nil, x |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 692 | case *stringLiteralExpr: |
| 693 | addToItemList(maybeConvertToStringList(x).(*listExpr).items...) |
| 694 | default: |
| 695 | switch x.typ() { |
| 696 | case starlarkTypeList: |
| 697 | finishItemList() |
| 698 | xConcat.items = append(xConcat.items, x) |
| 699 | case starlarkTypeString: |
| 700 | finishItemList() |
| 701 | xConcat.items = append(xConcat.items, &callExpr{ |
| 702 | object: x, |
| 703 | name: "split", |
| 704 | args: nil, |
| 705 | returnType: starlarkTypeList, |
| 706 | }) |
| 707 | default: |
| 708 | addToItemList(x) |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | if xItemList != nil { |
| 713 | xConcat.items = append(xConcat.items, xItemList) |
| 714 | } |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 715 | return xConcat, nil |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 716 | } |
| 717 | |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 718 | func (ctx *parseContext) newDependentModule(path string, optional bool) *moduleInfo { |
| 719 | modulePath := ctx.loadedModulePath(path) |
| 720 | if mi, ok := ctx.dependentModules[modulePath]; ok { |
Sasha Smundak | 868c5e3 | 2021-09-23 16:20:58 -0700 | [diff] [blame] | 721 | mi.optional = mi.optional && optional |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 722 | return mi |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 723 | } |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 724 | moduleName := moduleNameForFile(path) |
| 725 | moduleLocalName := "_" + moduleName |
| 726 | n, found := ctx.moduleNameCount[moduleName] |
| 727 | if found { |
| 728 | moduleLocalName += fmt.Sprintf("%d", n) |
| 729 | } |
| 730 | ctx.moduleNameCount[moduleName] = n + 1 |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame] | 731 | _, err := fs.Stat(ctx.script.sourceFS, path) |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 732 | mi := &moduleInfo{ |
| 733 | path: modulePath, |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 734 | originalPath: path, |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 735 | moduleLocalName: moduleLocalName, |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 736 | optional: optional, |
Sasha Smundak | 6bc132a | 2022-01-10 17:02:16 -0800 | [diff] [blame] | 737 | missing: err != nil, |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 738 | } |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 739 | ctx.dependentModules[modulePath] = mi |
| 740 | ctx.script.inherited = append(ctx.script.inherited, mi) |
| 741 | return mi |
| 742 | } |
| 743 | |
| 744 | func (ctx *parseContext) handleSubConfig( |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 745 | v mkparser.Node, pathExpr starlarkExpr, loadAlways bool, processModule func(inheritedModule) starlarkNode) []starlarkNode { |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 746 | |
| 747 | // In a simple case, the name of a module to inherit/include is known statically. |
| 748 | if path, ok := maybeString(pathExpr); ok { |
Sasha Smundak | 868c5e3 | 2021-09-23 16:20:58 -0700 | [diff] [blame] | 749 | // Note that even if this directive loads a module unconditionally, a module may be |
| 750 | // absent without causing any harm if this directive is inside an if/else block. |
| 751 | moduleShouldExist := loadAlways && ctx.ifNestLevel == 0 |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 752 | if strings.Contains(path, "*") { |
| 753 | if paths, err := fs.Glob(ctx.script.sourceFS, path); err == nil { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 754 | result := make([]starlarkNode, 0) |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 755 | for _, p := range paths { |
Sasha Smundak | 868c5e3 | 2021-09-23 16:20:58 -0700 | [diff] [blame] | 756 | mi := ctx.newDependentModule(p, !moduleShouldExist) |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 757 | result = append(result, processModule(inheritedStaticModule{mi, loadAlways})) |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 758 | } |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 759 | return result |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 760 | } else { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 761 | return []starlarkNode{ctx.newBadNode(v, "cannot glob wildcard argument")} |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 762 | } |
| 763 | } else { |
Sasha Smundak | 868c5e3 | 2021-09-23 16:20:58 -0700 | [diff] [blame] | 764 | mi := ctx.newDependentModule(path, !moduleShouldExist) |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 765 | return []starlarkNode{processModule(inheritedStaticModule{mi, loadAlways})} |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 766 | } |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | // If module path references variables (e.g., $(v1)/foo/$(v2)/device-config.mk), find all the paths in the |
| 770 | // source tree that may be a match and the corresponding variable values. For instance, if the source tree |
| 771 | // contains vendor1/foo/abc/dev.mk and vendor2/foo/def/dev.mk, the first one will be inherited when |
| 772 | // (v1, v2) == ('vendor1', 'abc'), and the second one when (v1, v2) == ('vendor2', 'def'). |
| 773 | // We then emit the code that loads all of them, e.g.: |
| 774 | // load("//vendor1/foo/abc:dev.rbc", _dev1_init="init") |
| 775 | // load("//vendor2/foo/def/dev.rbc", _dev2_init="init") |
| 776 | // And then inherit it as follows: |
| 777 | // _e = { |
| 778 | // "vendor1/foo/abc/dev.mk": ("vendor1/foo/abc/dev", _dev1_init), |
| 779 | // "vendor2/foo/def/dev.mk": ("vendor2/foo/def/dev", _dev_init2) }.get("%s/foo/%s/dev.mk" % (v1, v2)) |
| 780 | // if _e: |
| 781 | // rblf.inherit(handle, _e[0], _e[1]) |
| 782 | // |
| 783 | var matchingPaths []string |
| 784 | varPath, ok := pathExpr.(*interpolateExpr) |
| 785 | if !ok { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 786 | return []starlarkNode{ctx.newBadNode(v, "inherit-product/include argument is too complex")} |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | pathPattern := []string{varPath.chunks[0]} |
| 790 | for _, chunk := range varPath.chunks[1:] { |
| 791 | if chunk != "" { |
| 792 | pathPattern = append(pathPattern, chunk) |
| 793 | } |
| 794 | } |
Cole Faust | 069aba6 | 2022-01-26 17:47:33 -0800 | [diff] [blame] | 795 | if pathPattern[0] == "" && len(ctx.includeTops) > 0 { |
Sasha Smundak | 6d852dd | 2021-09-27 20:34:39 -0700 | [diff] [blame] | 796 | // If pattern starts from the top. restrict it to the directories where |
| 797 | // we know inherit-product uses dynamically calculated path. |
| 798 | for _, p := range ctx.includeTops { |
| 799 | pathPattern[0] = p |
| 800 | matchingPaths = append(matchingPaths, ctx.findMatchingPaths(pathPattern)...) |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 801 | } |
Sasha Smundak | 6d852dd | 2021-09-27 20:34:39 -0700 | [diff] [blame] | 802 | } else { |
| 803 | matchingPaths = ctx.findMatchingPaths(pathPattern) |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 804 | } |
| 805 | // Safeguard against $(call inherit-product,$(PRODUCT_PATH)) |
Sasha Smundak | 90be8c5 | 2021-08-03 11:06:10 -0700 | [diff] [blame] | 806 | const maxMatchingFiles = 150 |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 807 | if len(matchingPaths) > maxMatchingFiles { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 808 | return []starlarkNode{ctx.newBadNode(v, "there are >%d files matching the pattern, please rewrite it", maxMatchingFiles)} |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 809 | } |
Cole Faust | 069aba6 | 2022-01-26 17:47:33 -0800 | [diff] [blame] | 810 | if len(matchingPaths) == 1 { |
| 811 | res := inheritedStaticModule{ctx.newDependentModule(matchingPaths[0], loadAlways && ctx.ifNestLevel == 0), loadAlways} |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 812 | return []starlarkNode{processModule(res)} |
Cole Faust | 069aba6 | 2022-01-26 17:47:33 -0800 | [diff] [blame] | 813 | } else { |
| 814 | needsWarning := pathPattern[0] == "" && len(ctx.includeTops) == 0 |
| 815 | res := inheritedDynamicModule{*varPath, []*moduleInfo{}, loadAlways, ctx.errorLocation(v), needsWarning} |
| 816 | for _, p := range matchingPaths { |
| 817 | // A product configuration files discovered dynamically may attempt to inherit |
| 818 | // from another one which does not exist in this source tree. Prevent load errors |
| 819 | // by always loading the dynamic files as optional. |
| 820 | res.candidateModules = append(res.candidateModules, ctx.newDependentModule(p, true)) |
| 821 | } |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 822 | return []starlarkNode{processModule(res)} |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 823 | } |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | func (ctx *parseContext) findMatchingPaths(pattern []string) []string { |
Cole Faust | 9b6111a | 2022-02-02 15:38:33 -0800 | [diff] [blame^] | 827 | files := ctx.script.makefileFinder.Find(".") |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 828 | if len(pattern) == 0 { |
| 829 | return files |
| 830 | } |
| 831 | |
| 832 | // Create regular expression from the pattern |
| 833 | s_regexp := "^" + regexp.QuoteMeta(pattern[0]) |
| 834 | for _, s := range pattern[1:] { |
| 835 | s_regexp += ".*" + regexp.QuoteMeta(s) |
| 836 | } |
| 837 | s_regexp += "$" |
| 838 | rex := regexp.MustCompile(s_regexp) |
| 839 | |
| 840 | // Now match |
| 841 | var res []string |
| 842 | for _, p := range files { |
| 843 | if rex.MatchString(p) { |
| 844 | res = append(res, p) |
| 845 | } |
| 846 | } |
| 847 | return res |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 848 | } |
| 849 | |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 850 | func (ctx *parseContext) handleInheritModule(v mkparser.Node, args *mkparser.MakeString, loadAlways bool) []starlarkNode { |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 851 | args.TrimLeftSpaces() |
| 852 | args.TrimRightSpaces() |
| 853 | pathExpr := ctx.parseMakeString(v, args) |
| 854 | if _, ok := pathExpr.(*badExpr); ok { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 855 | return []starlarkNode{ctx.newBadNode(v, "Unable to parse argument to inherit")} |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 856 | } |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 857 | return ctx.handleSubConfig(v, pathExpr, loadAlways, func(im inheritedModule) starlarkNode { |
| 858 | return &inheritNode{im, loadAlways} |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 859 | }) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 860 | } |
| 861 | |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 862 | func (ctx *parseContext) handleInclude(v mkparser.Node, pathExpr starlarkExpr, loadAlways bool) []starlarkNode { |
| 863 | return ctx.handleSubConfig(v, pathExpr, loadAlways, func(im inheritedModule) starlarkNode { |
| 864 | return &includeNode{im, loadAlways} |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 865 | }) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 866 | } |
| 867 | |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 868 | func (ctx *parseContext) handleVariable(v *mkparser.Variable) []starlarkNode { |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 869 | // Handle: |
| 870 | // $(call inherit-product,...) |
| 871 | // $(call inherit-product-if-exists,...) |
| 872 | // $(info xxx) |
| 873 | // $(warning xxx) |
| 874 | // $(error xxx) |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 875 | // $(call other-custom-functions,...) |
| 876 | |
| 877 | // inherit-product(-if-exists) gets converted to a series of statements, |
| 878 | // not just a single expression like parseReference returns. So handle it |
| 879 | // separately at the beginning here. |
| 880 | if strings.HasPrefix(v.Name.Dump(), "call inherit-product,") { |
| 881 | args := v.Name.Clone() |
| 882 | args.ReplaceLiteral("call inherit-product,", "") |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 883 | return ctx.handleInheritModule(v, args, true) |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 884 | } |
| 885 | if strings.HasPrefix(v.Name.Dump(), "call inherit-product-if-exists,") { |
| 886 | args := v.Name.Clone() |
| 887 | args.ReplaceLiteral("call inherit-product-if-exists,", "") |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 888 | return ctx.handleInheritModule(v, args, false) |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 889 | } |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 890 | return []starlarkNode{&exprNode{expr: ctx.parseReference(v, v.Name)}} |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 891 | } |
| 892 | |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 893 | func (ctx *parseContext) maybeHandleDefine(directive *mkparser.Directive) starlarkNode { |
Sasha Smundak | f3e072a | 2021-07-14 12:50:28 -0700 | [diff] [blame] | 894 | macro_name := strings.Fields(directive.Args.Strings[0])[0] |
| 895 | // Ignore the macros that we handle |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 896 | _, ignored := ignoredDefines[macro_name] |
| 897 | _, known := knownFunctions[macro_name] |
| 898 | if !ignored && !known { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 899 | return ctx.newBadNode(directive, "define is not supported: %s", macro_name) |
Sasha Smundak | f3e072a | 2021-07-14 12:50:28 -0700 | [diff] [blame] | 900 | } |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 901 | return nil |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 902 | } |
| 903 | |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 904 | func (ctx *parseContext) handleIfBlock(ifDirective *mkparser.Directive) starlarkNode { |
| 905 | ssSwitch := &switchNode{ |
| 906 | ssCases: []*switchCase{ctx.processBranch(ifDirective)}, |
| 907 | } |
| 908 | for ctx.hasNodes() && ctx.fatalError == nil { |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 909 | node := ctx.getNode() |
| 910 | switch x := node.(type) { |
| 911 | case *mkparser.Directive: |
| 912 | switch x.Name { |
| 913 | case "else", "elifdef", "elifndef", "elifeq", "elifneq": |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 914 | ssSwitch.ssCases = append(ssSwitch.ssCases, ctx.processBranch(x)) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 915 | case "endif": |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 916 | return ssSwitch |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 917 | default: |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 918 | return ctx.newBadNode(node, "unexpected directive %s", x.Name) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 919 | } |
| 920 | default: |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 921 | return ctx.newBadNode(ifDirective, "unexpected statement") |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | if ctx.fatalError == nil { |
| 925 | ctx.fatalError = fmt.Errorf("no matching endif for %s", ifDirective.Dump()) |
| 926 | } |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 927 | return ctx.newBadNode(ifDirective, "no matching endif for %s", ifDirective.Dump()) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | // processBranch processes a single branch (if/elseif/else) until the next directive |
| 931 | // on the same level. |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 932 | func (ctx *parseContext) processBranch(check *mkparser.Directive) *switchCase { |
| 933 | block := &switchCase{gate: ctx.parseCondition(check)} |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 934 | defer func() { |
| 935 | ctx.popVarAssignments() |
| 936 | ctx.ifNestLevel-- |
| 937 | |
| 938 | }() |
| 939 | ctx.pushVarAssignments() |
| 940 | ctx.ifNestLevel++ |
| 941 | |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 942 | for ctx.hasNodes() { |
| 943 | node := ctx.getNode() |
Cole Faust | 591a1fe | 2021-11-08 15:37:57 -0800 | [diff] [blame] | 944 | if d, ok := node.(*mkparser.Directive); ok { |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 945 | switch d.Name { |
| 946 | case "else", "elifdef", "elifndef", "elifeq", "elifneq", "endif": |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 947 | ctx.backNode() |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 948 | return block |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 949 | } |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 950 | } |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 951 | block.nodes = append(block.nodes, ctx.handleSimpleStatement(node)...) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 952 | } |
| 953 | ctx.fatalError = fmt.Errorf("no matching endif for %s", check.Dump()) |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 954 | return block |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 955 | } |
| 956 | |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 957 | func (ctx *parseContext) parseCondition(check *mkparser.Directive) starlarkNode { |
| 958 | switch check.Name { |
| 959 | case "ifdef", "ifndef", "elifdef", "elifndef": |
Cole Faust | 71514c0 | 2022-01-27 17:21:41 -0800 | [diff] [blame] | 960 | if !check.Args.Const() { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 961 | return ctx.newBadNode(check, "ifdef variable ref too complex: %s", check.Args.Dump()) |
Cole Faust | 71514c0 | 2022-01-27 17:21:41 -0800 | [diff] [blame] | 962 | } |
| 963 | v := NewVariableRefExpr(ctx.addVariable(check.Args.Strings[0]), false) |
| 964 | if strings.HasSuffix(check.Name, "ndef") { |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 965 | v = ¬Expr{v} |
| 966 | } |
| 967 | return &ifNode{ |
| 968 | isElif: strings.HasPrefix(check.Name, "elif"), |
| 969 | expr: v, |
| 970 | } |
| 971 | case "ifeq", "ifneq", "elifeq", "elifneq": |
| 972 | return &ifNode{ |
| 973 | isElif: strings.HasPrefix(check.Name, "elif"), |
| 974 | expr: ctx.parseCompare(check), |
| 975 | } |
| 976 | case "else": |
| 977 | return &elseNode{} |
| 978 | default: |
| 979 | panic(fmt.Errorf("%s: unknown directive: %s", ctx.script.mkFile, check.Dump())) |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | func (ctx *parseContext) newBadExpr(node mkparser.Node, text string, args ...interface{}) starlarkExpr { |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 984 | if ctx.errorLogger != nil { |
Sasha Smundak | 422b614 | 2021-11-11 18:31:59 -0800 | [diff] [blame] | 985 | ctx.errorLogger.NewError(ctx.errorLocation(node), node, text, args...) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 986 | } |
| 987 | ctx.script.hasErrors = true |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 988 | return &badExpr{errorLocation: ctx.errorLocation(node), message: fmt.Sprintf(text, args...)} |
| 989 | } |
| 990 | |
| 991 | // records that the given node failed to be converted and includes an explanatory message |
| 992 | func (ctx *parseContext) newBadNode(failedNode mkparser.Node, message string, args ...interface{}) starlarkNode { |
| 993 | return &exprNode{ctx.newBadExpr(failedNode, message, args...)} |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | func (ctx *parseContext) parseCompare(cond *mkparser.Directive) starlarkExpr { |
| 997 | // Strip outer parentheses |
| 998 | mkArg := cloneMakeString(cond.Args) |
| 999 | mkArg.Strings[0] = strings.TrimLeft(mkArg.Strings[0], "( ") |
| 1000 | n := len(mkArg.Strings) |
| 1001 | mkArg.Strings[n-1] = strings.TrimRight(mkArg.Strings[n-1], ") ") |
| 1002 | args := mkArg.Split(",") |
| 1003 | // TODO(asmundak): handle the case where the arguments are in quotes and space-separated |
| 1004 | if len(args) != 2 { |
| 1005 | return ctx.newBadExpr(cond, "ifeq/ifneq len(args) != 2 %s", cond.Dump()) |
| 1006 | } |
| 1007 | args[0].TrimRightSpaces() |
| 1008 | args[1].TrimLeftSpaces() |
| 1009 | |
| 1010 | isEq := !strings.HasSuffix(cond.Name, "neq") |
Cole Faust | f832021 | 2021-11-10 15:05:07 -0800 | [diff] [blame] | 1011 | xLeft := ctx.parseMakeString(cond, args[0]) |
| 1012 | xRight := ctx.parseMakeString(cond, args[1]) |
| 1013 | if bad, ok := xLeft.(*badExpr); ok { |
| 1014 | return bad |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1015 | } |
Cole Faust | f832021 | 2021-11-10 15:05:07 -0800 | [diff] [blame] | 1016 | if bad, ok := xRight.(*badExpr); ok { |
| 1017 | return bad |
| 1018 | } |
| 1019 | |
| 1020 | if expr, ok := ctx.parseCompareSpecialCases(cond, xLeft, xRight); ok { |
| 1021 | return expr |
| 1022 | } |
| 1023 | |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1024 | var stringOperand string |
| 1025 | var otherOperand starlarkExpr |
| 1026 | if s, ok := maybeString(xLeft); ok { |
| 1027 | stringOperand = s |
| 1028 | otherOperand = xRight |
| 1029 | } else if s, ok := maybeString(xRight); ok { |
| 1030 | stringOperand = s |
| 1031 | otherOperand = xLeft |
| 1032 | } |
| 1033 | |
| 1034 | not := func(expr starlarkExpr) starlarkExpr { |
| 1035 | switch typedExpr := expr.(type) { |
| 1036 | case *inExpr: |
| 1037 | typedExpr.isNot = !typedExpr.isNot |
| 1038 | return typedExpr |
| 1039 | case *eqExpr: |
| 1040 | typedExpr.isEq = !typedExpr.isEq |
| 1041 | return typedExpr |
Cole Faust | b1103e2 | 2022-01-06 15:22:05 -0800 | [diff] [blame] | 1042 | case *binaryOpExpr: |
| 1043 | switch typedExpr.op { |
| 1044 | case ">": |
| 1045 | typedExpr.op = "<=" |
| 1046 | return typedExpr |
| 1047 | case "<": |
| 1048 | typedExpr.op = ">=" |
| 1049 | return typedExpr |
| 1050 | case ">=": |
| 1051 | typedExpr.op = "<" |
| 1052 | return typedExpr |
| 1053 | case "<=": |
| 1054 | typedExpr.op = ">" |
| 1055 | return typedExpr |
| 1056 | default: |
| 1057 | return ¬Expr{expr: expr} |
| 1058 | } |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1059 | default: |
| 1060 | return ¬Expr{expr: expr} |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | // If we've identified one of the operands as being a string literal, check |
| 1065 | // for some special cases we can do to simplify the resulting expression. |
| 1066 | if otherOperand != nil { |
| 1067 | if stringOperand == "" { |
| 1068 | if isEq { |
| 1069 | return not(otherOperand) |
| 1070 | } else { |
| 1071 | return otherOperand |
| 1072 | } |
| 1073 | } |
| 1074 | if stringOperand == "true" && otherOperand.typ() == starlarkTypeBool { |
| 1075 | if !isEq { |
| 1076 | return not(otherOperand) |
| 1077 | } else { |
| 1078 | return otherOperand |
| 1079 | } |
| 1080 | } |
Cole Faust | b1103e2 | 2022-01-06 15:22:05 -0800 | [diff] [blame] | 1081 | if intOperand, err := strconv.Atoi(strings.TrimSpace(stringOperand)); err == nil && otherOperand.typ() == starlarkTypeInt { |
| 1082 | return &eqExpr{ |
| 1083 | left: otherOperand, |
| 1084 | right: &intLiteralExpr{literal: intOperand}, |
| 1085 | isEq: isEq, |
| 1086 | } |
| 1087 | } |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1088 | } |
| 1089 | |
Cole Faust | f832021 | 2021-11-10 15:05:07 -0800 | [diff] [blame] | 1090 | return &eqExpr{left: xLeft, right: xRight, isEq: isEq} |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1091 | } |
| 1092 | |
Cole Faust | f832021 | 2021-11-10 15:05:07 -0800 | [diff] [blame] | 1093 | // Given an if statement's directive and the left/right starlarkExprs, |
| 1094 | // check if the starlarkExprs are one of a few hardcoded special cases |
| 1095 | // that can be converted to a simpler equalify expression than simply comparing |
| 1096 | // the two. |
| 1097 | func (ctx *parseContext) parseCompareSpecialCases(directive *mkparser.Directive, left starlarkExpr, |
| 1098 | right starlarkExpr) (starlarkExpr, bool) { |
| 1099 | isEq := !strings.HasSuffix(directive.Name, "neq") |
| 1100 | |
| 1101 | // All the special cases require a call on one side and a |
| 1102 | // string literal/variable on the other. Turn the left/right variables into |
| 1103 | // call/value variables, and return false if that's not possible. |
| 1104 | var value starlarkExpr = nil |
| 1105 | call, ok := left.(*callExpr) |
| 1106 | if ok { |
| 1107 | switch right.(type) { |
| 1108 | case *stringLiteralExpr, *variableRefExpr: |
| 1109 | value = right |
| 1110 | } |
| 1111 | } else { |
| 1112 | call, _ = right.(*callExpr) |
| 1113 | switch left.(type) { |
| 1114 | case *stringLiteralExpr, *variableRefExpr: |
| 1115 | value = left |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | if call == nil || value == nil { |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1120 | return nil, false |
| 1121 | } |
Cole Faust | f832021 | 2021-11-10 15:05:07 -0800 | [diff] [blame] | 1122 | |
Cole Faust | f832021 | 2021-11-10 15:05:07 -0800 | [diff] [blame] | 1123 | switch call.name { |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1124 | case baseName + ".filter", baseName + ".filter-out": |
Cole Faust | f832021 | 2021-11-10 15:05:07 -0800 | [diff] [blame] | 1125 | return ctx.parseCompareFilterFuncResult(directive, call, value, isEq), true |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1126 | case baseName + ".expand_wildcard": |
Cole Faust | f832021 | 2021-11-10 15:05:07 -0800 | [diff] [blame] | 1127 | return ctx.parseCompareWildcardFuncResult(directive, call, value, !isEq), true |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1128 | case baseName + ".findstring": |
Cole Faust | f832021 | 2021-11-10 15:05:07 -0800 | [diff] [blame] | 1129 | return ctx.parseCheckFindstringFuncResult(directive, call, value, !isEq), true |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1130 | case baseName + ".strip": |
Cole Faust | f832021 | 2021-11-10 15:05:07 -0800 | [diff] [blame] | 1131 | return ctx.parseCompareStripFuncResult(directive, call, value, !isEq), true |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1132 | } |
Cole Faust | f832021 | 2021-11-10 15:05:07 -0800 | [diff] [blame] | 1133 | return nil, false |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | func (ctx *parseContext) parseCompareFilterFuncResult(cond *mkparser.Directive, |
| 1137 | filterFuncCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr { |
| 1138 | // We handle: |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame] | 1139 | // * ifeq/ifneq (,$(filter v1 v2 ..., EXPR) becomes if EXPR not in/in ["v1", "v2", ...] |
| 1140 | // * ifeq/ifneq (,$(filter EXPR, v1 v2 ...) becomes if EXPR not in/in ["v1", "v2", ...] |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1141 | // * ifeq/ifneq ($(VAR),$(filter $(VAR), v1 v2 ...) becomes if VAR in/not in ["v1", "v2"] |
| 1142 | // TODO(Asmundak): check the last case works for filter-out, too. |
| 1143 | xPattern := filterFuncCall.args[0] |
| 1144 | xText := filterFuncCall.args[1] |
| 1145 | var xInList *stringLiteralExpr |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame] | 1146 | var expr starlarkExpr |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1147 | var ok bool |
| 1148 | switch x := xValue.(type) { |
| 1149 | case *stringLiteralExpr: |
| 1150 | if x.literal != "" { |
| 1151 | return ctx.newBadExpr(cond, "filter comparison to non-empty value: %s", xValue) |
| 1152 | } |
| 1153 | // Either pattern or text should be const, and the |
| 1154 | // non-const one should be varRefExpr |
Sasha Smundak | 5f463be | 2021-09-15 18:43:36 -0700 | [diff] [blame] | 1155 | if xInList, ok = xPattern.(*stringLiteralExpr); ok && !strings.ContainsRune(xInList.literal, '%') && xText.typ() == starlarkTypeList { |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame] | 1156 | expr = xText |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1157 | } else if xInList, ok = xText.(*stringLiteralExpr); ok { |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame] | 1158 | expr = xPattern |
| 1159 | } else { |
Sasha Smundak | 5f463be | 2021-09-15 18:43:36 -0700 | [diff] [blame] | 1160 | expr = &callExpr{ |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame] | 1161 | object: nil, |
| 1162 | name: filterFuncCall.name, |
| 1163 | args: filterFuncCall.args, |
| 1164 | returnType: starlarkTypeBool, |
| 1165 | } |
Sasha Smundak | 5f463be | 2021-09-15 18:43:36 -0700 | [diff] [blame] | 1166 | if negate { |
| 1167 | expr = ¬Expr{expr: expr} |
| 1168 | } |
| 1169 | return expr |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1170 | } |
| 1171 | case *variableRefExpr: |
| 1172 | if v, ok := xPattern.(*variableRefExpr); ok { |
| 1173 | if xInList, ok = xText.(*stringLiteralExpr); ok && v.ref.name() == x.ref.name() { |
| 1174 | // ifeq/ifneq ($(VAR),$(filter $(VAR), v1 v2 ...), flip negate, |
| 1175 | // it's the opposite to what is done when comparing to empty. |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame] | 1176 | expr = xPattern |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1177 | negate = !negate |
| 1178 | } |
| 1179 | } |
| 1180 | } |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame] | 1181 | if expr != nil && xInList != nil { |
| 1182 | slExpr := newStringListExpr(strings.Fields(xInList.literal)) |
| 1183 | // Generate simpler code for the common cases: |
| 1184 | if expr.typ() == starlarkTypeList { |
| 1185 | if len(slExpr.items) == 1 { |
| 1186 | // Checking that a string belongs to list |
| 1187 | return &inExpr{isNot: negate, list: expr, expr: slExpr.items[0]} |
| 1188 | } else { |
| 1189 | // TODO(asmundak): |
| 1190 | panic("TBD") |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1191 | } |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame] | 1192 | } else if len(slExpr.items) == 1 { |
| 1193 | return &eqExpr{left: expr, right: slExpr.items[0], isEq: !negate} |
| 1194 | } else { |
| 1195 | return &inExpr{isNot: negate, list: newStringListExpr(strings.Fields(xInList.literal)), expr: expr} |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1196 | } |
| 1197 | } |
| 1198 | return ctx.newBadExpr(cond, "filter arguments are too complex: %s", cond.Dump()) |
| 1199 | } |
| 1200 | |
| 1201 | func (ctx *parseContext) parseCompareWildcardFuncResult(directive *mkparser.Directive, |
| 1202 | xCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr { |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame] | 1203 | if !isEmptyString(xValue) { |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1204 | return ctx.newBadExpr(directive, "wildcard result can be compared only to empty: %s", xValue) |
| 1205 | } |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1206 | callFunc := baseName + ".file_wildcard_exists" |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1207 | if s, ok := xCall.args[0].(*stringLiteralExpr); ok && !strings.ContainsAny(s.literal, "*?{[") { |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1208 | callFunc = baseName + ".file_exists" |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1209 | } |
| 1210 | var cc starlarkExpr = &callExpr{name: callFunc, args: xCall.args, returnType: starlarkTypeBool} |
| 1211 | if !negate { |
| 1212 | cc = ¬Expr{cc} |
| 1213 | } |
| 1214 | return cc |
| 1215 | } |
| 1216 | |
| 1217 | func (ctx *parseContext) parseCheckFindstringFuncResult(directive *mkparser.Directive, |
| 1218 | xCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr { |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame] | 1219 | if isEmptyString(xValue) { |
| 1220 | return &eqExpr{ |
| 1221 | left: &callExpr{ |
| 1222 | object: xCall.args[1], |
| 1223 | name: "find", |
| 1224 | args: []starlarkExpr{xCall.args[0]}, |
| 1225 | returnType: starlarkTypeInt, |
| 1226 | }, |
| 1227 | right: &intLiteralExpr{-1}, |
| 1228 | isEq: !negate, |
| 1229 | } |
Cole Faust | 0e9418c | 2021-12-13 16:33:25 -0800 | [diff] [blame] | 1230 | } else if s, ok := maybeString(xValue); ok { |
| 1231 | if s2, ok := maybeString(xCall.args[0]); ok && s == s2 { |
| 1232 | return &eqExpr{ |
| 1233 | left: &callExpr{ |
| 1234 | object: xCall.args[1], |
| 1235 | name: "find", |
| 1236 | args: []starlarkExpr{xCall.args[0]}, |
| 1237 | returnType: starlarkTypeInt, |
| 1238 | }, |
| 1239 | right: &intLiteralExpr{-1}, |
| 1240 | isEq: negate, |
| 1241 | } |
| 1242 | } |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1243 | } |
Cole Faust | 0e9418c | 2021-12-13 16:33:25 -0800 | [diff] [blame] | 1244 | return ctx.newBadExpr(directive, "$(findstring) can only be compared to nothing or its first argument") |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | func (ctx *parseContext) parseCompareStripFuncResult(directive *mkparser.Directive, |
| 1248 | xCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr { |
| 1249 | if _, ok := xValue.(*stringLiteralExpr); !ok { |
| 1250 | return ctx.newBadExpr(directive, "strip result can be compared only to string: %s", xValue) |
| 1251 | } |
| 1252 | return &eqExpr{ |
| 1253 | left: &callExpr{ |
| 1254 | name: "strip", |
| 1255 | args: xCall.args, |
| 1256 | returnType: starlarkTypeString, |
| 1257 | }, |
| 1258 | right: xValue, isEq: !negate} |
| 1259 | } |
| 1260 | |
| 1261 | // parses $(...), returning an expression |
| 1262 | func (ctx *parseContext) parseReference(node mkparser.Node, ref *mkparser.MakeString) starlarkExpr { |
| 1263 | ref.TrimLeftSpaces() |
| 1264 | ref.TrimRightSpaces() |
| 1265 | refDump := ref.Dump() |
| 1266 | |
| 1267 | // Handle only the case where the first (or only) word is constant |
| 1268 | words := ref.SplitN(" ", 2) |
| 1269 | if !words[0].Const() { |
| 1270 | return ctx.newBadExpr(node, "reference is too complex: %s", refDump) |
| 1271 | } |
| 1272 | |
| 1273 | // If it is a single word, it can be a simple variable |
| 1274 | // reference or a function call |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1275 | if len(words) == 1 && !isMakeControlFunc(refDump) && refDump != "shell" { |
Sasha Smundak | 65b547e | 2021-09-17 15:35:41 -0700 | [diff] [blame] | 1276 | if strings.HasPrefix(refDump, soongNsPrefix) { |
| 1277 | // TODO (asmundak): if we find many, maybe handle them. |
Cole Faust | c00184e | 2021-11-08 12:08:57 -0800 | [diff] [blame] | 1278 | return ctx.newBadExpr(node, "SOONG_CONFIG_ variables cannot be referenced, use soong_config_get instead: %s", refDump) |
Sasha Smundak | 65b547e | 2021-09-17 15:35:41 -0700 | [diff] [blame] | 1279 | } |
Cole Faust | c36c962 | 2021-12-07 15:20:45 -0800 | [diff] [blame] | 1280 | // Handle substitution references: https://www.gnu.org/software/make/manual/html_node/Substitution-Refs.html |
| 1281 | if strings.Contains(refDump, ":") { |
| 1282 | parts := strings.SplitN(refDump, ":", 2) |
| 1283 | substParts := strings.SplitN(parts[1], "=", 2) |
| 1284 | if len(substParts) < 2 || strings.Count(substParts[0], "%") > 1 { |
| 1285 | return ctx.newBadExpr(node, "Invalid substitution reference") |
| 1286 | } |
| 1287 | if !strings.Contains(substParts[0], "%") { |
| 1288 | if strings.Contains(substParts[1], "%") { |
| 1289 | return ctx.newBadExpr(node, "A substitution reference must have a %% in the \"before\" part of the substitution if it has one in the \"after\" part.") |
| 1290 | } |
| 1291 | substParts[0] = "%" + substParts[0] |
| 1292 | substParts[1] = "%" + substParts[1] |
| 1293 | } |
| 1294 | v := ctx.addVariable(parts[0]) |
| 1295 | if v == nil { |
| 1296 | return ctx.newBadExpr(node, "unknown variable %s", refDump) |
| 1297 | } |
| 1298 | return &callExpr{ |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1299 | name: baseName + ".mkpatsubst", |
| 1300 | returnType: starlarkTypeString, |
Cole Faust | c36c962 | 2021-12-07 15:20:45 -0800 | [diff] [blame] | 1301 | args: []starlarkExpr{ |
| 1302 | &stringLiteralExpr{literal: substParts[0]}, |
| 1303 | &stringLiteralExpr{literal: substParts[1]}, |
Cole Faust | fc43868 | 2021-12-14 12:46:32 -0800 | [diff] [blame] | 1304 | NewVariableRefExpr(v, ctx.lastAssignment(v.name()) != nil), |
Cole Faust | c36c962 | 2021-12-07 15:20:45 -0800 | [diff] [blame] | 1305 | }, |
| 1306 | } |
| 1307 | } |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1308 | if v := ctx.addVariable(refDump); v != nil { |
Cole Faust | fc43868 | 2021-12-14 12:46:32 -0800 | [diff] [blame] | 1309 | return NewVariableRefExpr(v, ctx.lastAssignment(v.name()) != nil) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1310 | } |
| 1311 | return ctx.newBadExpr(node, "unknown variable %s", refDump) |
| 1312 | } |
| 1313 | |
| 1314 | expr := &callExpr{name: words[0].Dump(), returnType: starlarkTypeUnknown} |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1315 | args := mkparser.SimpleMakeString("", words[0].Pos()) |
| 1316 | if len(words) >= 2 { |
| 1317 | args = words[1] |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1318 | } |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1319 | args.TrimLeftSpaces() |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1320 | if expr.name == "call" { |
| 1321 | words = args.SplitN(",", 2) |
| 1322 | if words[0].Empty() || !words[0].Const() { |
Sasha Smundak | f2c9f8b | 2021-07-27 10:44:48 -0700 | [diff] [blame] | 1323 | return ctx.newBadExpr(node, "cannot handle %s", refDump) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1324 | } |
| 1325 | expr.name = words[0].Dump() |
| 1326 | if len(words) < 2 { |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 1327 | args = &mkparser.MakeString{} |
| 1328 | } else { |
| 1329 | args = words[1] |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1330 | } |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1331 | } |
| 1332 | if kf, found := knownFunctions[expr.name]; found { |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1333 | return kf.parse(ctx, node, args) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1334 | } else { |
| 1335 | return ctx.newBadExpr(node, "cannot handle invoking %s", expr.name) |
| 1336 | } |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | type simpleCallParser struct { |
| 1340 | name string |
| 1341 | returnType starlarkType |
| 1342 | addGlobals bool |
| 1343 | } |
| 1344 | |
| 1345 | func (p *simpleCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr { |
| 1346 | expr := &callExpr{name: p.name, returnType: p.returnType} |
| 1347 | if p.addGlobals { |
| 1348 | expr.args = append(expr.args, &globalsExpr{}) |
| 1349 | } |
| 1350 | for _, arg := range args.Split(",") { |
| 1351 | arg.TrimLeftSpaces() |
| 1352 | arg.TrimRightSpaces() |
| 1353 | x := ctx.parseMakeString(node, arg) |
| 1354 | if xBad, ok := x.(*badExpr); ok { |
| 1355 | return xBad |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1356 | } |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1357 | expr.args = append(expr.args, x) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1358 | } |
| 1359 | return expr |
| 1360 | } |
| 1361 | |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1362 | type makeControlFuncParser struct { |
| 1363 | name string |
| 1364 | } |
| 1365 | |
| 1366 | func (p *makeControlFuncParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr { |
| 1367 | // Make control functions need special treatment as everything |
| 1368 | // after the name is a single text argument |
| 1369 | x := ctx.parseMakeString(node, args) |
| 1370 | if xBad, ok := x.(*badExpr); ok { |
| 1371 | return xBad |
| 1372 | } |
| 1373 | return &callExpr{ |
| 1374 | name: p.name, |
| 1375 | args: []starlarkExpr{ |
| 1376 | &stringLiteralExpr{ctx.script.mkFile}, |
| 1377 | x, |
| 1378 | }, |
| 1379 | returnType: starlarkTypeUnknown, |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | type shellCallParser struct{} |
| 1384 | |
| 1385 | func (p *shellCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr { |
| 1386 | // Shell functions need special treatment as everything |
| 1387 | // after the name is a single text argument |
| 1388 | x := ctx.parseMakeString(node, args) |
| 1389 | if xBad, ok := x.(*badExpr); ok { |
| 1390 | return xBad |
| 1391 | } |
| 1392 | return &callExpr{ |
| 1393 | name: baseName + ".shell", |
| 1394 | args: []starlarkExpr{x}, |
| 1395 | returnType: starlarkTypeUnknown, |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | type myDirCallParser struct{} |
| 1400 | |
| 1401 | func (p *myDirCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr { |
| 1402 | if !args.Empty() { |
| 1403 | return ctx.newBadExpr(node, "my-dir function cannot have any arguments passed to it.") |
| 1404 | } |
| 1405 | return &variableRefExpr{ctx.addVariable("LOCAL_PATH"), true} |
| 1406 | } |
| 1407 | |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1408 | type isProductInListCallParser struct{} |
| 1409 | |
| 1410 | func (p *isProductInListCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr { |
| 1411 | if args.Empty() { |
| 1412 | return ctx.newBadExpr(node, "is-product-in-list requires an argument") |
| 1413 | } |
| 1414 | return &inExpr{ |
| 1415 | expr: &variableRefExpr{ctx.addVariable("TARGET_PRODUCT"), true}, |
| 1416 | list: maybeConvertToStringList(ctx.parseMakeString(node, args)), |
| 1417 | isNot: false, |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | type isVendorBoardPlatformCallParser struct{} |
| 1422 | |
| 1423 | func (p *isVendorBoardPlatformCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr { |
| 1424 | if args.Empty() || !identifierFullMatchRegex.MatchString(args.Dump()) { |
| 1425 | return ctx.newBadExpr(node, "cannot handle non-constant argument to is-vendor-board-platform") |
| 1426 | } |
| 1427 | return &inExpr{ |
| 1428 | expr: &variableRefExpr{ctx.addVariable("TARGET_BOARD_PLATFORM"), false}, |
| 1429 | list: &variableRefExpr{ctx.addVariable(args.Dump() + "_BOARD_PLATFORMS"), true}, |
| 1430 | isNot: false, |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | type isVendorBoardQcomCallParser struct{} |
| 1435 | |
| 1436 | func (p *isVendorBoardQcomCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr { |
| 1437 | if !args.Empty() { |
| 1438 | return ctx.newBadExpr(node, "is-vendor-board-qcom does not accept any arguments") |
| 1439 | } |
| 1440 | return &inExpr{ |
| 1441 | expr: &variableRefExpr{ctx.addVariable("TARGET_BOARD_PLATFORM"), false}, |
| 1442 | list: &variableRefExpr{ctx.addVariable("QCOM_BOARD_PLATFORMS"), true}, |
| 1443 | isNot: false, |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | type substCallParser struct { |
| 1448 | fname string |
| 1449 | } |
| 1450 | |
| 1451 | func (p *substCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr { |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1452 | words := args.Split(",") |
| 1453 | if len(words) != 3 { |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1454 | return ctx.newBadExpr(node, "%s function should have 3 arguments", p.fname) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1455 | } |
Sasha Smundak | 35434ed | 2021-11-05 16:29:56 -0700 | [diff] [blame] | 1456 | from := ctx.parseMakeString(node, words[0]) |
| 1457 | if xBad, ok := from.(*badExpr); ok { |
| 1458 | return xBad |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1459 | } |
Sasha Smundak | 35434ed | 2021-11-05 16:29:56 -0700 | [diff] [blame] | 1460 | to := ctx.parseMakeString(node, words[1]) |
| 1461 | if xBad, ok := to.(*badExpr); ok { |
| 1462 | return xBad |
| 1463 | } |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1464 | words[2].TrimLeftSpaces() |
| 1465 | words[2].TrimRightSpaces() |
| 1466 | obj := ctx.parseMakeString(node, words[2]) |
Sasha Smundak | 9d011ab | 2021-07-09 16:00:57 -0700 | [diff] [blame] | 1467 | typ := obj.typ() |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1468 | if typ == starlarkTypeString && p.fname == "subst" { |
Sasha Smundak | 94b41c7 | 2021-07-12 18:30:42 -0700 | [diff] [blame] | 1469 | // Optimization: if it's $(subst from, to, string), emit string.replace(from, to) |
Sasha Smundak | 9d011ab | 2021-07-09 16:00:57 -0700 | [diff] [blame] | 1470 | return &callExpr{ |
| 1471 | object: obj, |
| 1472 | name: "replace", |
Sasha Smundak | 35434ed | 2021-11-05 16:29:56 -0700 | [diff] [blame] | 1473 | args: []starlarkExpr{from, to}, |
Sasha Smundak | 9d011ab | 2021-07-09 16:00:57 -0700 | [diff] [blame] | 1474 | returnType: typ, |
| 1475 | } |
| 1476 | } |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1477 | return &callExpr{ |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1478 | name: baseName + ".mk" + p.fname, |
Sasha Smundak | 35434ed | 2021-11-05 16:29:56 -0700 | [diff] [blame] | 1479 | args: []starlarkExpr{from, to, obj}, |
Sasha Smundak | 9d011ab | 2021-07-09 16:00:57 -0700 | [diff] [blame] | 1480 | returnType: obj.typ(), |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1481 | } |
| 1482 | } |
| 1483 | |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1484 | type ifCallParser struct{} |
| 1485 | |
| 1486 | func (p *ifCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr { |
Cole Faust | 4eadba7 | 2021-12-07 11:54:52 -0800 | [diff] [blame] | 1487 | words := args.Split(",") |
| 1488 | if len(words) != 2 && len(words) != 3 { |
| 1489 | return ctx.newBadExpr(node, "if function should have 2 or 3 arguments, found "+strconv.Itoa(len(words))) |
| 1490 | } |
| 1491 | condition := ctx.parseMakeString(node, words[0]) |
| 1492 | ifTrue := ctx.parseMakeString(node, words[1]) |
| 1493 | var ifFalse starlarkExpr |
| 1494 | if len(words) == 3 { |
| 1495 | ifFalse = ctx.parseMakeString(node, words[2]) |
| 1496 | } else { |
| 1497 | switch ifTrue.typ() { |
| 1498 | case starlarkTypeList: |
| 1499 | ifFalse = &listExpr{items: []starlarkExpr{}} |
| 1500 | case starlarkTypeInt: |
| 1501 | ifFalse = &intLiteralExpr{literal: 0} |
| 1502 | case starlarkTypeBool: |
| 1503 | ifFalse = &boolLiteralExpr{literal: false} |
| 1504 | default: |
| 1505 | ifFalse = &stringLiteralExpr{literal: ""} |
| 1506 | } |
| 1507 | } |
| 1508 | return &ifExpr{ |
| 1509 | condition, |
| 1510 | ifTrue, |
| 1511 | ifFalse, |
| 1512 | } |
| 1513 | } |
| 1514 | |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1515 | type foreachCallPaser struct{} |
| 1516 | |
| 1517 | func (p *foreachCallPaser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr { |
Cole Faust | b0d32ab | 2021-12-09 14:00:59 -0800 | [diff] [blame] | 1518 | words := args.Split(",") |
| 1519 | if len(words) != 3 { |
| 1520 | return ctx.newBadExpr(node, "foreach function should have 3 arguments, found "+strconv.Itoa(len(words))) |
| 1521 | } |
| 1522 | if !words[0].Const() || words[0].Empty() || !identifierFullMatchRegex.MatchString(words[0].Strings[0]) { |
| 1523 | return ctx.newBadExpr(node, "first argument to foreach function must be a simple string identifier") |
| 1524 | } |
| 1525 | loopVarName := words[0].Strings[0] |
| 1526 | list := ctx.parseMakeString(node, words[1]) |
| 1527 | action := ctx.parseMakeString(node, words[2]).transform(func(expr starlarkExpr) starlarkExpr { |
| 1528 | if varRefExpr, ok := expr.(*variableRefExpr); ok && varRefExpr.ref.name() == loopVarName { |
| 1529 | return &identifierExpr{loopVarName} |
| 1530 | } |
| 1531 | return nil |
| 1532 | }) |
| 1533 | |
| 1534 | if list.typ() != starlarkTypeList { |
| 1535 | list = &callExpr{ |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1536 | name: baseName + ".words", |
| 1537 | returnType: starlarkTypeList, |
Cole Faust | b0d32ab | 2021-12-09 14:00:59 -0800 | [diff] [blame] | 1538 | args: []starlarkExpr{list}, |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | return &foreachExpr{ |
| 1543 | varName: loopVarName, |
| 1544 | list: list, |
| 1545 | action: action, |
| 1546 | } |
| 1547 | } |
| 1548 | |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1549 | type wordCallParser struct{} |
| 1550 | |
| 1551 | func (p *wordCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr { |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1552 | words := args.Split(",") |
| 1553 | if len(words) != 2 { |
| 1554 | return ctx.newBadExpr(node, "word function should have 2 arguments") |
| 1555 | } |
| 1556 | var index uint64 = 0 |
| 1557 | if words[0].Const() { |
| 1558 | index, _ = strconv.ParseUint(strings.TrimSpace(words[0].Strings[0]), 10, 64) |
| 1559 | } |
| 1560 | if index < 1 { |
| 1561 | return ctx.newBadExpr(node, "word index should be constant positive integer") |
| 1562 | } |
| 1563 | words[1].TrimLeftSpaces() |
| 1564 | words[1].TrimRightSpaces() |
| 1565 | array := ctx.parseMakeString(node, words[1]) |
| 1566 | if xBad, ok := array.(*badExpr); ok { |
| 1567 | return xBad |
| 1568 | } |
| 1569 | if array.typ() != starlarkTypeList { |
| 1570 | array = &callExpr{object: array, name: "split", returnType: starlarkTypeList} |
| 1571 | } |
Cole Faust | b0d32ab | 2021-12-09 14:00:59 -0800 | [diff] [blame] | 1572 | return &indexExpr{array, &intLiteralExpr{int(index - 1)}} |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1573 | } |
| 1574 | |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1575 | type firstOrLastwordCallParser struct { |
| 1576 | isLastWord bool |
| 1577 | } |
| 1578 | |
| 1579 | func (p *firstOrLastwordCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr { |
Sasha Smundak | 16e0773 | 2021-07-23 11:38:23 -0700 | [diff] [blame] | 1580 | arg := ctx.parseMakeString(node, args) |
| 1581 | if bad, ok := arg.(*badExpr); ok { |
| 1582 | return bad |
| 1583 | } |
| 1584 | index := &intLiteralExpr{0} |
Cole Faust | 9ebf6e4 | 2021-12-13 14:08:34 -0800 | [diff] [blame] | 1585 | if p.isLastWord { |
Sasha Smundak | 16e0773 | 2021-07-23 11:38:23 -0700 | [diff] [blame] | 1586 | if v, ok := arg.(*variableRefExpr); ok && v.ref.name() == "MAKEFILE_LIST" { |
| 1587 | return &stringLiteralExpr{ctx.script.mkFile} |
| 1588 | } |
| 1589 | index.literal = -1 |
| 1590 | } |
| 1591 | if arg.typ() == starlarkTypeList { |
| 1592 | return &indexExpr{arg, index} |
| 1593 | } |
| 1594 | return &indexExpr{&callExpr{object: arg, name: "split", returnType: starlarkTypeList}, index} |
| 1595 | } |
| 1596 | |
Cole Faust | b1103e2 | 2022-01-06 15:22:05 -0800 | [diff] [blame] | 1597 | func parseIntegerArguments(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString, expectedArgs int) ([]starlarkExpr, error) { |
| 1598 | parsedArgs := make([]starlarkExpr, 0) |
| 1599 | for _, arg := range args.Split(",") { |
| 1600 | expr := ctx.parseMakeString(node, arg) |
| 1601 | if expr.typ() == starlarkTypeList { |
| 1602 | return nil, fmt.Errorf("argument to math argument has type list, which cannot be converted to int") |
| 1603 | } |
| 1604 | if s, ok := maybeString(expr); ok { |
| 1605 | intVal, err := strconv.Atoi(strings.TrimSpace(s)) |
| 1606 | if err != nil { |
| 1607 | return nil, err |
| 1608 | } |
| 1609 | expr = &intLiteralExpr{literal: intVal} |
| 1610 | } else if expr.typ() != starlarkTypeInt { |
| 1611 | expr = &callExpr{ |
| 1612 | name: "int", |
| 1613 | args: []starlarkExpr{expr}, |
| 1614 | returnType: starlarkTypeInt, |
| 1615 | } |
| 1616 | } |
| 1617 | parsedArgs = append(parsedArgs, expr) |
| 1618 | } |
| 1619 | if len(parsedArgs) != expectedArgs { |
| 1620 | return nil, fmt.Errorf("function should have %d arguments", expectedArgs) |
| 1621 | } |
| 1622 | return parsedArgs, nil |
| 1623 | } |
| 1624 | |
| 1625 | type mathComparisonCallParser struct { |
| 1626 | op string |
| 1627 | } |
| 1628 | |
| 1629 | func (p *mathComparisonCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr { |
| 1630 | parsedArgs, err := parseIntegerArguments(ctx, node, args, 2) |
| 1631 | if err != nil { |
| 1632 | return ctx.newBadExpr(node, err.Error()) |
| 1633 | } |
| 1634 | return &binaryOpExpr{ |
| 1635 | left: parsedArgs[0], |
| 1636 | right: parsedArgs[1], |
| 1637 | op: p.op, |
| 1638 | returnType: starlarkTypeBool, |
| 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | type mathMaxOrMinCallParser struct { |
| 1643 | function string |
| 1644 | } |
| 1645 | |
| 1646 | func (p *mathMaxOrMinCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr { |
| 1647 | parsedArgs, err := parseIntegerArguments(ctx, node, args, 2) |
| 1648 | if err != nil { |
| 1649 | return ctx.newBadExpr(node, err.Error()) |
| 1650 | } |
| 1651 | return &callExpr{ |
| 1652 | object: nil, |
| 1653 | name: p.function, |
| 1654 | args: parsedArgs, |
| 1655 | returnType: starlarkTypeInt, |
| 1656 | } |
| 1657 | } |
| 1658 | |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1659 | func (ctx *parseContext) parseMakeString(node mkparser.Node, mk *mkparser.MakeString) starlarkExpr { |
| 1660 | if mk.Const() { |
| 1661 | return &stringLiteralExpr{mk.Dump()} |
| 1662 | } |
| 1663 | if mkRef, ok := mk.SingleVariable(); ok { |
| 1664 | return ctx.parseReference(node, mkRef) |
| 1665 | } |
| 1666 | // If we reached here, it's neither string literal nor a simple variable, |
| 1667 | // we need a full-blown interpolation node that will generate |
| 1668 | // "a%b%c" % (X, Y) for a$(X)b$(Y)c |
Cole Faust | fc43868 | 2021-12-14 12:46:32 -0800 | [diff] [blame] | 1669 | parts := make([]starlarkExpr, len(mk.Variables)+len(mk.Strings)) |
| 1670 | for i := 0; i < len(parts); i++ { |
| 1671 | if i%2 == 0 { |
| 1672 | parts[i] = &stringLiteralExpr{literal: mk.Strings[i/2]} |
| 1673 | } else { |
| 1674 | parts[i] = ctx.parseReference(node, mk.Variables[i/2].Name) |
| 1675 | if x, ok := parts[i].(*badExpr); ok { |
| 1676 | return x |
| 1677 | } |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1678 | } |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1679 | } |
Cole Faust | fc43868 | 2021-12-14 12:46:32 -0800 | [diff] [blame] | 1680 | return NewInterpolateExpr(parts) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | // Handles the statements whose treatment is the same in all contexts: comment, |
| 1684 | // assignment, variable (which is a macro call in reality) and all constructs that |
| 1685 | // do not handle in any context ('define directive and any unrecognized stuff). |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 1686 | func (ctx *parseContext) handleSimpleStatement(node mkparser.Node) []starlarkNode { |
| 1687 | var result []starlarkNode |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1688 | switch x := node.(type) { |
| 1689 | case *mkparser.Comment: |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 1690 | if n, handled := ctx.maybeHandleAnnotation(x); handled && n != nil { |
| 1691 | result = []starlarkNode{n} |
| 1692 | } else if !handled { |
| 1693 | result = []starlarkNode{&commentNode{strings.TrimSpace("#" + x.Comment)}} |
Cole Faust | 7940c6a | 2022-01-31 15:54:05 -0800 | [diff] [blame] | 1694 | } |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1695 | case *mkparser.Assignment: |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 1696 | result = ctx.handleAssignment(x) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1697 | case *mkparser.Variable: |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 1698 | result = ctx.handleVariable(x) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1699 | case *mkparser.Directive: |
| 1700 | switch x.Name { |
| 1701 | case "define": |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 1702 | if res := ctx.maybeHandleDefine(x); res != nil { |
| 1703 | result = []starlarkNode{res} |
| 1704 | } |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1705 | case "include", "-include": |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 1706 | result = ctx.handleInclude(node, ctx.parseMakeString(node, x.Args), x.Name[0] != '-') |
Cole Faust | 591a1fe | 2021-11-08 15:37:57 -0800 | [diff] [blame] | 1707 | case "ifeq", "ifneq", "ifdef", "ifndef": |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 1708 | result = []starlarkNode{ctx.handleIfBlock(x)} |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1709 | default: |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 1710 | result = []starlarkNode{ctx.newBadNode(x, "unexpected directive %s", x.Name)} |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1711 | } |
| 1712 | default: |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 1713 | result = []starlarkNode{ctx.newBadNode(x, "unsupported line %s", strings.ReplaceAll(x.Dump(), "\n", "\n#"))} |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1714 | } |
Cole Faust | 6c934f6 | 2022-01-06 15:51:12 -0800 | [diff] [blame] | 1715 | |
| 1716 | // Clear the includeTops after each non-comment statement |
| 1717 | // so that include annotations placed on certain statements don't apply |
| 1718 | // globally for the rest of the makefile was well. |
| 1719 | if _, wasComment := node.(*mkparser.Comment); !wasComment && len(ctx.includeTops) > 0 { |
| 1720 | ctx.includeTops = []string{} |
| 1721 | } |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 1722 | |
| 1723 | if result == nil { |
| 1724 | result = []starlarkNode{} |
| 1725 | } |
| 1726 | return result |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1727 | } |
| 1728 | |
Sasha Smundak | 6d852dd | 2021-09-27 20:34:39 -0700 | [diff] [blame] | 1729 | // Processes annotation. An annotation is a comment that starts with #RBC# and provides |
| 1730 | // a conversion hint -- say, where to look for the dynamically calculated inherit/include |
Cole Faust | 7940c6a | 2022-01-31 15:54:05 -0800 | [diff] [blame] | 1731 | // paths. Returns true if the comment was a successfully-handled annotation. |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 1732 | func (ctx *parseContext) maybeHandleAnnotation(cnode *mkparser.Comment) (starlarkNode, bool) { |
Sasha Smundak | 6d852dd | 2021-09-27 20:34:39 -0700 | [diff] [blame] | 1733 | maybeTrim := func(s, prefix string) (string, bool) { |
| 1734 | if strings.HasPrefix(s, prefix) { |
| 1735 | return strings.TrimSpace(strings.TrimPrefix(s, prefix)), true |
| 1736 | } |
| 1737 | return s, false |
| 1738 | } |
| 1739 | annotation, ok := maybeTrim(cnode.Comment, annotationCommentPrefix) |
| 1740 | if !ok { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 1741 | return nil, false |
Sasha Smundak | 6d852dd | 2021-09-27 20:34:39 -0700 | [diff] [blame] | 1742 | } |
| 1743 | if p, ok := maybeTrim(annotation, "include_top"); ok { |
Cole Faust | f7ed534 | 2021-12-21 14:15:12 -0800 | [diff] [blame] | 1744 | // Don't allow duplicate include tops, because then we will generate |
| 1745 | // invalid starlark code. (duplicate keys in the _entry dictionary) |
| 1746 | for _, top := range ctx.includeTops { |
| 1747 | if top == p { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 1748 | return nil, true |
Cole Faust | f7ed534 | 2021-12-21 14:15:12 -0800 | [diff] [blame] | 1749 | } |
| 1750 | } |
Sasha Smundak | 6d852dd | 2021-09-27 20:34:39 -0700 | [diff] [blame] | 1751 | ctx.includeTops = append(ctx.includeTops, p) |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 1752 | return nil, true |
Sasha Smundak | 6d852dd | 2021-09-27 20:34:39 -0700 | [diff] [blame] | 1753 | } |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 1754 | return ctx.newBadNode(cnode, "unsupported annotation %s", cnode.Comment), true |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | func (ctx *parseContext) loadedModulePath(path string) string { |
| 1758 | // During the transition to Roboleaf some of the product configuration files |
| 1759 | // will be converted and checked in while the others will be generated on the fly |
| 1760 | // and run. The runner (rbcrun application) accommodates this by allowing three |
| 1761 | // different ways to specify the loaded file location: |
| 1762 | // 1) load(":<file>",...) loads <file> from the same directory |
| 1763 | // 2) load("//path/relative/to/source/root:<file>", ...) loads <file> source tree |
| 1764 | // 3) load("/absolute/path/to/<file> absolute path |
| 1765 | // If the file being generated and the file it wants to load are in the same directory, |
| 1766 | // generate option 1. |
| 1767 | // Otherwise, if output directory is not specified, generate 2) |
| 1768 | // Finally, if output directory has been specified and the file being generated and |
| 1769 | // the file it wants to load from are in the different directories, generate 2) or 3): |
| 1770 | // * if the file being loaded exists in the source tree, generate 2) |
| 1771 | // * otherwise, generate 3) |
| 1772 | // Finally, figure out the loaded module path and name and create a node for it |
| 1773 | loadedModuleDir := filepath.Dir(path) |
| 1774 | base := filepath.Base(path) |
| 1775 | loadedModuleName := strings.TrimSuffix(base, filepath.Ext(base)) + ctx.outputSuffix |
| 1776 | if loadedModuleDir == filepath.Dir(ctx.script.mkFile) { |
| 1777 | return ":" + loadedModuleName |
| 1778 | } |
| 1779 | if ctx.outputDir == "" { |
| 1780 | return fmt.Sprintf("//%s:%s", loadedModuleDir, loadedModuleName) |
| 1781 | } |
| 1782 | if _, err := os.Stat(filepath.Join(loadedModuleDir, loadedModuleName)); err == nil { |
| 1783 | return fmt.Sprintf("//%s:%s", loadedModuleDir, loadedModuleName) |
| 1784 | } |
| 1785 | return filepath.Join(ctx.outputDir, loadedModuleDir, loadedModuleName) |
| 1786 | } |
| 1787 | |
Sasha Smundak | 3deb968 | 2021-07-26 18:42:25 -0700 | [diff] [blame] | 1788 | func (ctx *parseContext) addSoongNamespace(ns string) { |
| 1789 | if _, ok := ctx.soongNamespaces[ns]; ok { |
| 1790 | return |
| 1791 | } |
| 1792 | ctx.soongNamespaces[ns] = make(map[string]bool) |
| 1793 | } |
| 1794 | |
| 1795 | func (ctx *parseContext) hasSoongNamespace(name string) bool { |
| 1796 | _, ok := ctx.soongNamespaces[name] |
| 1797 | return ok |
| 1798 | } |
| 1799 | |
| 1800 | func (ctx *parseContext) updateSoongNamespace(replace bool, namespaceName string, varNames []string) { |
| 1801 | ctx.addSoongNamespace(namespaceName) |
| 1802 | vars := ctx.soongNamespaces[namespaceName] |
| 1803 | if replace { |
| 1804 | vars = make(map[string]bool) |
| 1805 | ctx.soongNamespaces[namespaceName] = vars |
| 1806 | } |
| 1807 | for _, v := range varNames { |
| 1808 | vars[v] = true |
| 1809 | } |
| 1810 | } |
| 1811 | |
| 1812 | func (ctx *parseContext) hasNamespaceVar(namespaceName string, varName string) bool { |
| 1813 | vars, ok := ctx.soongNamespaces[namespaceName] |
| 1814 | if ok { |
| 1815 | _, ok = vars[varName] |
| 1816 | } |
| 1817 | return ok |
| 1818 | } |
| 1819 | |
Sasha Smundak | 422b614 | 2021-11-11 18:31:59 -0800 | [diff] [blame] | 1820 | func (ctx *parseContext) errorLocation(node mkparser.Node) ErrorLocation { |
| 1821 | return ErrorLocation{ctx.script.mkFile, ctx.script.nodeLocator(node.Pos())} |
| 1822 | } |
| 1823 | |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1824 | func (ss *StarlarkScript) String() string { |
| 1825 | return NewGenerateContext(ss).emit() |
| 1826 | } |
| 1827 | |
| 1828 | func (ss *StarlarkScript) SubConfigFiles() []string { |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame] | 1829 | |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1830 | var subs []string |
| 1831 | for _, src := range ss.inherited { |
| 1832 | subs = append(subs, src.originalPath) |
| 1833 | } |
| 1834 | return subs |
| 1835 | } |
| 1836 | |
| 1837 | func (ss *StarlarkScript) HasErrors() bool { |
| 1838 | return ss.hasErrors |
| 1839 | } |
| 1840 | |
| 1841 | // Convert reads and parses a makefile. If successful, parsed tree |
| 1842 | // is returned and then can be passed to String() to get the generated |
| 1843 | // Starlark file. |
| 1844 | func Convert(req Request) (*StarlarkScript, error) { |
| 1845 | reader := req.Reader |
| 1846 | if reader == nil { |
| 1847 | mkContents, err := ioutil.ReadFile(req.MkFile) |
| 1848 | if err != nil { |
| 1849 | return nil, err |
| 1850 | } |
| 1851 | reader = bytes.NewBuffer(mkContents) |
| 1852 | } |
| 1853 | parser := mkparser.NewParser(req.MkFile, reader) |
| 1854 | nodes, errs := parser.Parse() |
| 1855 | if len(errs) > 0 { |
| 1856 | for _, e := range errs { |
| 1857 | fmt.Fprintln(os.Stderr, "ERROR:", e) |
| 1858 | } |
| 1859 | return nil, fmt.Errorf("bad makefile %s", req.MkFile) |
| 1860 | } |
| 1861 | starScript := &StarlarkScript{ |
Sasha Smundak | 422b614 | 2021-11-11 18:31:59 -0800 | [diff] [blame] | 1862 | moduleName: moduleNameForFile(req.MkFile), |
| 1863 | mkFile: req.MkFile, |
Sasha Smundak | 422b614 | 2021-11-11 18:31:59 -0800 | [diff] [blame] | 1864 | traceCalls: req.TraceCalls, |
| 1865 | sourceFS: req.SourceFS, |
| 1866 | makefileFinder: req.MakefileFinder, |
| 1867 | nodeLocator: func(pos mkparser.Pos) int { return parser.Unpack(pos).Line }, |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 1868 | nodes: make([]starlarkNode, 0), |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1869 | } |
| 1870 | ctx := newParseContext(starScript, nodes) |
| 1871 | ctx.outputSuffix = req.OutputSuffix |
| 1872 | ctx.outputDir = req.OutputDir |
| 1873 | ctx.errorLogger = req.ErrorLogger |
| 1874 | if len(req.TracedVariables) > 0 { |
| 1875 | ctx.tracedVariables = make(map[string]bool) |
| 1876 | for _, v := range req.TracedVariables { |
| 1877 | ctx.tracedVariables[v] = true |
| 1878 | } |
| 1879 | } |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1880 | for ctx.hasNodes() && ctx.fatalError == nil { |
Cole Faust | dd569ae | 2022-01-31 15:48:29 -0800 | [diff] [blame] | 1881 | starScript.nodes = append(starScript.nodes, ctx.handleSimpleStatement(ctx.getNode())...) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1882 | } |
| 1883 | if ctx.fatalError != nil { |
| 1884 | return nil, ctx.fatalError |
| 1885 | } |
| 1886 | return starScript, nil |
| 1887 | } |
| 1888 | |
Cole Faust | 864028a | 2021-12-01 13:43:17 -0800 | [diff] [blame] | 1889 | func Launcher(mainModuleUri, inputVariablesUri, mainModuleName string) string { |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1890 | var buf bytes.Buffer |
| 1891 | fmt.Fprintf(&buf, "load(%q, %q)\n", baseUri, baseName) |
Cole Faust | 864028a | 2021-12-01 13:43:17 -0800 | [diff] [blame] | 1892 | fmt.Fprintf(&buf, "load(%q, input_variables_init = \"init\")\n", inputVariablesUri) |
Sasha Smundak | d7d07ad | 2021-09-10 15:42:34 -0700 | [diff] [blame] | 1893 | fmt.Fprintf(&buf, "load(%q, \"init\")\n", mainModuleUri) |
Cole Faust | 864028a | 2021-12-01 13:43:17 -0800 | [diff] [blame] | 1894 | fmt.Fprintf(&buf, "%s(%s(%q, init, input_variables_init))\n", cfnPrintVars, cfnMain, mainModuleName) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1895 | return buf.String() |
| 1896 | } |
| 1897 | |
Cole Faust | 6ed7cb4 | 2021-10-07 17:08:46 -0700 | [diff] [blame] | 1898 | func BoardLauncher(mainModuleUri string, inputVariablesUri string) string { |
| 1899 | var buf bytes.Buffer |
| 1900 | fmt.Fprintf(&buf, "load(%q, %q)\n", baseUri, baseName) |
| 1901 | fmt.Fprintf(&buf, "load(%q, \"init\")\n", mainModuleUri) |
| 1902 | fmt.Fprintf(&buf, "load(%q, input_variables_init = \"init\")\n", inputVariablesUri) |
| 1903 | fmt.Fprintf(&buf, "globals, cfg, globals_base = %s(init, input_variables_init)\n", cfnBoardMain) |
| 1904 | fmt.Fprintf(&buf, "# TODO: Some product config variables need to be printed, but most are readonly so we can't just print cfg here.\n") |
Cole Faust | 3c1868b | 2021-11-22 16:34:11 -0800 | [diff] [blame] | 1905 | fmt.Fprintf(&buf, "%s((globals, cfg, globals_base))\n", cfnPrintVars) |
Cole Faust | 6ed7cb4 | 2021-10-07 17:08:46 -0700 | [diff] [blame] | 1906 | return buf.String() |
| 1907 | } |
| 1908 | |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1909 | func MakePath2ModuleName(mkPath string) string { |
| 1910 | return strings.TrimSuffix(mkPath, filepath.Ext(mkPath)) |
| 1911 | } |