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