blob: 6c76137deba3bc05063a9cee77bf83d1fbce82ed [file] [log] [blame]
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001// 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//
24package mk2rbc
25
26import (
27 "bytes"
28 "fmt"
29 "io"
Sasha Smundak6609ba72021-07-22 18:32:56 -070030 "io/fs"
Sasha Smundakb051c4e2020-11-05 20:45:07 -080031 "io/ioutil"
32 "os"
33 "path/filepath"
34 "regexp"
Cole Faust62e05112022-04-05 17:56:11 -070035 "sort"
Sasha Smundakb051c4e2020-11-05 20:45:07 -080036 "strconv"
37 "strings"
38 "text/scanner"
39
40 mkparser "android/soong/androidmk/parser"
41)
42
43const (
Sasha Smundak6d852dd2021-09-27 20:34:39 -070044 annotationCommentPrefix = "RBC#"
45 baseUri = "//build/make/core:product_config.rbc"
Sasha Smundakb051c4e2020-11-05 20:45:07 -080046 // 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 Smundak65b547e2021-09-17 15:35:41 -070051 soongNsPrefix = "SOONG_CONFIG_"
52
Sasha Smundakb051c4e2020-11-05 20:45:07 -080053 // And here are the functions and variables:
Cole Fauste2a37982022-03-09 16:00:17 -080054 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 Smundakb051c4e2020-11-05 20:45:07 -080060)
61
62const (
Cole Faust9ebf6e42021-12-13 14:08:34 -080063 soongConfigAppend = "soong_config_append"
64 soongConfigAssign = "soong_config_set"
Sasha Smundakb051c4e2020-11-05 20:45:07 -080065)
66
Cole Faust9ebf6e42021-12-13 14:08:34 -080067var knownFunctions = map[string]interface {
68 parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr
Sasha Smundakb051c4e2020-11-05 20:45:07 -080069}{
Cole Faust1cc08852022-02-28 11:12:08 -080070 "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 Faust0e2b2562022-04-01 11:46:50 -070081 "dir": &simpleCallParser{name: baseName + ".dir", returnType: starlarkTypeString},
Cole Faust1cc08852022-02-28 11:12:08 -080082 "dist-for-goals": &simpleCallParser{name: baseName + ".mkdist_for_goals", returnType: starlarkTypeVoid, addGlobals: true},
Cole Faust6c41b8a2022-04-13 13:53:48 -070083 "enforce-product-packages-exist": &simpleCallParser{name: baseName + ".enforce_product_packages_exist", returnType: starlarkTypeVoid, addHandle: true},
Cole Faust1cc08852022-02-28 11:12:08 -080084 "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 Faustf035d402022-03-28 14:02:50 -070090 "foreach": &foreachCallParser{},
Cole Faust1cc08852022-02-28 11:12:08 -080091 "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 Faustea9db582022-03-21 17:50:05 -0700110 "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 Smundakb051c4e2020-11-05 20:45:07 -0800112 // TODO(asmundak): remove it once all calls are removed from configuration makefiles. see b/183161002
Cole Faust9ebf6e42021-12-13 14:08:34 -0800113 "shell": &shellCallParser{},
Cole Faust95b95cb2022-04-05 16:37:39 -0700114 "sort": &simpleCallParser{name: baseName + ".mksort", returnType: starlarkTypeList},
Cole Faust1cc08852022-02-28 11:12:08 -0800115 "strip": &simpleCallParser{name: baseName + ".mkstrip", returnType: starlarkTypeString},
Cole Faust9ebf6e42021-12-13 14:08:34 -0800116 "subst": &substCallParser{fname: "subst"},
117 "warning": &makeControlFuncParser{name: baseName + ".mkwarning"},
118 "word": &wordCallParser{},
Cole Faust94c4a9a2022-04-22 17:43:52 -0700119 "words": &wordsCallParser{},
Cole Faust1cc08852022-02-28 11:12:08 -0800120 "wildcard": &simpleCallParser{name: baseName + ".expand_wildcard", returnType: starlarkTypeList},
Cole Faust9ebf6e42021-12-13 14:08:34 -0800121}
122
Cole Faustf035d402022-03-28 14:02:50 -0700123// The same as knownFunctions, but returns a []starlarkNode instead of a starlarkExpr
124var knownNodeFunctions = map[string]interface {
125 parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) []starlarkNode
126}{
127 "eval": &evalNodeParser{},
128 "if": &ifCallNodeParser{},
129 "inherit-product": &inheritProductCallParser{loadAlways: true},
130 "inherit-product-if-exists": &inheritProductCallParser{loadAlways: false},
131 "foreach": &foreachCallNodeParser{},
132}
133
Cole Faust9ebf6e42021-12-13 14:08:34 -0800134// These are functions that we don't implement conversions for, but
135// we allow seeing their definitions in the product config files.
136var ignoredDefines = map[string]bool{
137 "find-word-in-list": true, // internal macro
138 "get-vendor-board-platforms": true, // internal macro, used by is-board-platform, etc.
139 "is-android-codename": true, // unused by product config
140 "is-android-codename-in-list": true, // unused by product config
141 "is-chipset-in-board-platform": true, // unused by product config
142 "is-chipset-prefix-in-board-platform": true, // unused by product config
143 "is-not-board-platform": true, // defined but never used
144 "is-platform-sdk-version-at-least": true, // unused by product config
145 "match-prefix": true, // internal macro
146 "match-word": true, // internal macro
147 "match-word-in-list": true, // internal macro
148 "tb-modules": true, // defined in hardware/amlogic/tb_modules/tb_detect.mk, unused
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800149}
150
Cole Faustb0d32ab2021-12-09 14:00:59 -0800151var identifierFullMatchRegex = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800152
153// Conversion request parameters
154type Request struct {
Sasha Smundak422b6142021-11-11 18:31:59 -0800155 MkFile string // file to convert
156 Reader io.Reader // if set, read input from this stream instead
Sasha Smundak422b6142021-11-11 18:31:59 -0800157 OutputSuffix string // generated Starlark files suffix
158 OutputDir string // if set, root of the output hierarchy
159 ErrorLogger ErrorLogger
160 TracedVariables []string // trace assignment to these variables
161 TraceCalls bool
162 SourceFS fs.FS
163 MakefileFinder MakefileFinder
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800164}
165
Sasha Smundak7d934b92021-11-10 12:20:01 -0800166// ErrorLogger prints errors and gathers error statistics.
167// Its NewError function is called on every error encountered during the conversion.
168type ErrorLogger interface {
Sasha Smundak422b6142021-11-11 18:31:59 -0800169 NewError(el ErrorLocation, node mkparser.Node, text string, args ...interface{})
170}
171
172type ErrorLocation struct {
173 MkFile string
174 MkLine int
175}
176
177func (el ErrorLocation) String() string {
178 return fmt.Sprintf("%s:%d", el.MkFile, el.MkLine)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800179}
180
181// Derives module name for a given file. It is base name
182// (file name without suffix), with some characters replaced to make it a Starlark identifier
183func moduleNameForFile(mkFile string) string {
184 base := strings.TrimSuffix(filepath.Base(mkFile), filepath.Ext(mkFile))
185 // TODO(asmundak): what else can be in the product file names?
Sasha Smundak6609ba72021-07-22 18:32:56 -0700186 return strings.NewReplacer("-", "_", ".", "_").Replace(base)
187
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800188}
189
190func cloneMakeString(mkString *mkparser.MakeString) *mkparser.MakeString {
191 r := &mkparser.MakeString{StringPos: mkString.StringPos}
192 r.Strings = append(r.Strings, mkString.Strings...)
193 r.Variables = append(r.Variables, mkString.Variables...)
194 return r
195}
196
197func isMakeControlFunc(s string) bool {
198 return s == "error" || s == "warning" || s == "info"
199}
200
Cole Faustf0632662022-04-07 13:59:24 -0700201// varAssignmentScope points to the last assignment for each variable
202// in the current block. It is used during the parsing to chain
203// the assignments to a variable together.
204type varAssignmentScope struct {
205 outer *varAssignmentScope
206 vars map[string]bool
207}
208
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800209// Starlark output generation context
210type generationContext struct {
Cole Faustf0632662022-04-07 13:59:24 -0700211 buf strings.Builder
212 starScript *StarlarkScript
213 indentLevel int
214 inAssignment bool
215 tracedCount int
216 varAssignments *varAssignmentScope
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800217}
218
219func NewGenerateContext(ss *StarlarkScript) *generationContext {
Cole Faustf0632662022-04-07 13:59:24 -0700220 return &generationContext{
221 starScript: ss,
222 varAssignments: &varAssignmentScope{
223 outer: nil,
224 vars: make(map[string]bool),
225 },
226 }
227}
228
229func (gctx *generationContext) pushVariableAssignments() {
230 va := &varAssignmentScope{
231 outer: gctx.varAssignments,
232 vars: make(map[string]bool),
233 }
234 gctx.varAssignments = va
235}
236
237func (gctx *generationContext) popVariableAssignments() {
238 gctx.varAssignments = gctx.varAssignments.outer
239}
240
241func (gctx *generationContext) hasBeenAssigned(v variable) bool {
242 for va := gctx.varAssignments; va != nil; va = va.outer {
243 if _, ok := va.vars[v.name()]; ok {
244 return true
245 }
246 }
247 return false
248}
249
250func (gctx *generationContext) setHasBeenAssigned(v variable) {
251 gctx.varAssignments.vars[v.name()] = true
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800252}
253
254// emit returns generated script
255func (gctx *generationContext) emit() string {
256 ss := gctx.starScript
257
258 // The emitted code has the following layout:
259 // <initial comments>
260 // preamble, i.e.,
261 // load statement for the runtime support
262 // load statement for each unique submodule pulled in by this one
263 // def init(g, handle):
264 // cfg = rblf.cfg(handle)
265 // <statements>
266 // <warning if conversion was not clean>
267
268 iNode := len(ss.nodes)
269 for i, node := range ss.nodes {
270 if _, ok := node.(*commentNode); !ok {
271 iNode = i
272 break
273 }
274 node.emit(gctx)
275 }
276
277 gctx.emitPreamble()
278
279 gctx.newLine()
280 // The arguments passed to the init function are the global dictionary
281 // ('g') and the product configuration dictionary ('cfg')
282 gctx.write("def init(g, handle):")
283 gctx.indentLevel++
284 if gctx.starScript.traceCalls {
285 gctx.newLine()
286 gctx.writef(`print(">%s")`, gctx.starScript.mkFile)
287 }
288 gctx.newLine()
289 gctx.writef("cfg = %s(handle)", cfnGetCfg)
290 for _, node := range ss.nodes[iNode:] {
291 node.emit(gctx)
292 }
293
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800294 if gctx.starScript.traceCalls {
295 gctx.newLine()
296 gctx.writef(`print("<%s")`, gctx.starScript.mkFile)
297 }
298 gctx.indentLevel--
299 gctx.write("\n")
300 return gctx.buf.String()
301}
302
303func (gctx *generationContext) emitPreamble() {
304 gctx.newLine()
305 gctx.writef("load(%q, %q)", baseUri, baseName)
306 // Emit exactly one load statement for each URI.
307 loadedSubConfigs := make(map[string]string)
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800308 for _, mi := range gctx.starScript.inherited {
309 uri := mi.path
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800310 if m, ok := loadedSubConfigs[uri]; ok {
311 // No need to emit load statement, but fix module name.
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800312 mi.moduleLocalName = m
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800313 continue
314 }
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800315 if mi.optional || mi.missing {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800316 uri += "|init"
317 }
318 gctx.newLine()
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800319 gctx.writef("load(%q, %s = \"init\")", uri, mi.entryName())
320 loadedSubConfigs[uri] = mi.moduleLocalName
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800321 }
322 gctx.write("\n")
323}
324
325func (gctx *generationContext) emitPass() {
326 gctx.newLine()
327 gctx.write("pass")
328}
329
330func (gctx *generationContext) write(ss ...string) {
331 for _, s := range ss {
332 gctx.buf.WriteString(s)
333 }
334}
335
336func (gctx *generationContext) writef(format string, args ...interface{}) {
337 gctx.write(fmt.Sprintf(format, args...))
338}
339
340func (gctx *generationContext) newLine() {
341 if gctx.buf.Len() == 0 {
342 return
343 }
344 gctx.write("\n")
345 gctx.writef("%*s", 2*gctx.indentLevel, "")
346}
347
Sasha Smundak422b6142021-11-11 18:31:59 -0800348func (gctx *generationContext) emitConversionError(el ErrorLocation, message string) {
349 gctx.writef(`rblf.mk2rbc_error("%s", %q)`, el, message)
350}
351
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800352func (gctx *generationContext) emitLoadCheck(im inheritedModule) {
353 if !im.needsLoadCheck() {
354 return
355 }
356 gctx.newLine()
357 gctx.writef("if not %s:", im.entryName())
358 gctx.indentLevel++
359 gctx.newLine()
360 gctx.write(`rblf.mkerror("`, gctx.starScript.mkFile, `", "Cannot find %s" % (`)
361 im.pathExpr().emit(gctx)
362 gctx.write("))")
363 gctx.indentLevel--
364}
365
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800366type knownVariable struct {
367 name string
368 class varClass
369 valueType starlarkType
370}
371
372type knownVariables map[string]knownVariable
373
374func (pcv knownVariables) NewVariable(name string, varClass varClass, valueType starlarkType) {
375 v, exists := pcv[name]
376 if !exists {
377 pcv[name] = knownVariable{name, varClass, valueType}
378 return
379 }
380 // Conflict resolution:
381 // * config class trumps everything
382 // * any type trumps unknown type
383 match := varClass == v.class
384 if !match {
385 if varClass == VarClassConfig {
386 v.class = VarClassConfig
387 match = true
388 } else if v.class == VarClassConfig {
389 match = true
390 }
391 }
392 if valueType != v.valueType {
393 if valueType != starlarkTypeUnknown {
394 if v.valueType == starlarkTypeUnknown {
395 v.valueType = valueType
396 } else {
397 match = false
398 }
399 }
400 }
401 if !match {
402 fmt.Fprintf(os.Stderr, "cannot redefine %s as %v/%v (already defined as %v/%v)\n",
403 name, varClass, valueType, v.class, v.valueType)
404 }
405}
406
407// All known product variables.
408var KnownVariables = make(knownVariables)
409
410func init() {
411 for _, kv := range []string{
412 // Kernel-related variables that we know are lists.
413 "BOARD_VENDOR_KERNEL_MODULES",
414 "BOARD_VENDOR_RAMDISK_KERNEL_MODULES",
415 "BOARD_VENDOR_RAMDISK_KERNEL_MODULES_LOAD",
416 "BOARD_RECOVERY_KERNEL_MODULES",
417 // Other variables we knwo are lists
418 "ART_APEX_JARS",
419 } {
420 KnownVariables.NewVariable(kv, VarClassSoong, starlarkTypeList)
421 }
422}
423
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800424// Information about the generated Starlark script.
425type StarlarkScript struct {
Sasha Smundak422b6142021-11-11 18:31:59 -0800426 mkFile string
427 moduleName string
428 mkPos scanner.Position
429 nodes []starlarkNode
430 inherited []*moduleInfo
431 hasErrors bool
Sasha Smundak422b6142021-11-11 18:31:59 -0800432 traceCalls bool // print enter/exit each init function
433 sourceFS fs.FS
434 makefileFinder MakefileFinder
435 nodeLocator func(pos mkparser.Pos) int
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800436}
437
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800438// parseContext holds the script we are generating and all the ephemeral data
439// needed during the parsing.
440type parseContext struct {
441 script *StarlarkScript
442 nodes []mkparser.Node // Makefile as parsed by mkparser
443 currentNodeIndex int // Node in it we are processing
444 ifNestLevel int
445 moduleNameCount map[string]int // count of imported modules with given basename
446 fatalError error
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800447 outputSuffix string
Sasha Smundak7d934b92021-11-10 12:20:01 -0800448 errorLogger ErrorLogger
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800449 tracedVariables map[string]bool // variables to be traced in the generated script
450 variables map[string]variable
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800451 outputDir string
Sasha Smundak6609ba72021-07-22 18:32:56 -0700452 dependentModules map[string]*moduleInfo
Sasha Smundak3deb9682021-07-26 18:42:25 -0700453 soongNamespaces map[string]map[string]bool
Sasha Smundak6d852dd2021-09-27 20:34:39 -0700454 includeTops []string
Cole Faustf92c9f22022-03-14 14:35:50 -0700455 typeHints map[string]starlarkType
456 atTopOfMakefile bool
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800457}
458
459func newParseContext(ss *StarlarkScript, nodes []mkparser.Node) *parseContext {
460 predefined := []struct{ name, value string }{
461 {"SRC_TARGET_DIR", filepath.Join("build", "make", "target")},
462 {"LOCAL_PATH", filepath.Dir(ss.mkFile)},
Cole Faust9b6111a2022-02-02 15:38:33 -0800463 {"TOPDIR", ""}, // TOPDIR is just set to an empty string in cleanbuild.mk and core.mk
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800464 // TODO(asmundak): maybe read it from build/make/core/envsetup.mk?
465 {"TARGET_COPY_OUT_SYSTEM", "system"},
466 {"TARGET_COPY_OUT_SYSTEM_OTHER", "system_other"},
467 {"TARGET_COPY_OUT_DATA", "data"},
468 {"TARGET_COPY_OUT_ASAN", filepath.Join("data", "asan")},
469 {"TARGET_COPY_OUT_OEM", "oem"},
470 {"TARGET_COPY_OUT_RAMDISK", "ramdisk"},
471 {"TARGET_COPY_OUT_DEBUG_RAMDISK", "debug_ramdisk"},
472 {"TARGET_COPY_OUT_VENDOR_DEBUG_RAMDISK", "vendor_debug_ramdisk"},
473 {"TARGET_COPY_OUT_TEST_HARNESS_RAMDISK", "test_harness_ramdisk"},
474 {"TARGET_COPY_OUT_ROOT", "root"},
475 {"TARGET_COPY_OUT_RECOVERY", "recovery"},
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800476 {"TARGET_COPY_OUT_VENDOR_RAMDISK", "vendor_ramdisk"},
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800477 // TODO(asmundak): to process internal config files, we need the following variables:
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800478 // TARGET_VENDOR
479 // target_base_product
480 //
481
482 // the following utility variables are set in build/make/common/core.mk:
483 {"empty", ""},
484 {"space", " "},
485 {"comma", ","},
486 {"newline", "\n"},
487 {"pound", "#"},
488 {"backslash", "\\"},
489 }
490 ctx := &parseContext{
491 script: ss,
492 nodes: nodes,
493 currentNodeIndex: 0,
494 ifNestLevel: 0,
495 moduleNameCount: make(map[string]int),
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800496 variables: make(map[string]variable),
Sasha Smundak6609ba72021-07-22 18:32:56 -0700497 dependentModules: make(map[string]*moduleInfo),
Sasha Smundak3deb9682021-07-26 18:42:25 -0700498 soongNamespaces: make(map[string]map[string]bool),
Cole Faust6c934f62022-01-06 15:51:12 -0800499 includeTops: []string{},
Cole Faustf92c9f22022-03-14 14:35:50 -0700500 typeHints: make(map[string]starlarkType),
501 atTopOfMakefile: true,
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800502 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800503 for _, item := range predefined {
504 ctx.variables[item.name] = &predefinedVariable{
505 baseVariable: baseVariable{nam: item.name, typ: starlarkTypeString},
506 value: &stringLiteralExpr{item.value},
507 }
508 }
509
510 return ctx
511}
512
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800513func (ctx *parseContext) hasNodes() bool {
514 return ctx.currentNodeIndex < len(ctx.nodes)
515}
516
517func (ctx *parseContext) getNode() mkparser.Node {
518 if !ctx.hasNodes() {
519 return nil
520 }
521 node := ctx.nodes[ctx.currentNodeIndex]
522 ctx.currentNodeIndex++
523 return node
524}
525
526func (ctx *parseContext) backNode() {
527 if ctx.currentNodeIndex <= 0 {
528 panic("Cannot back off")
529 }
530 ctx.currentNodeIndex--
531}
532
Cole Faustdd569ae2022-01-31 15:48:29 -0800533func (ctx *parseContext) handleAssignment(a *mkparser.Assignment) []starlarkNode {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800534 // Handle only simple variables
535 if !a.Name.Const() {
Cole Faustdd569ae2022-01-31 15:48:29 -0800536 return []starlarkNode{ctx.newBadNode(a, "Only simple variables are handled")}
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800537 }
538 name := a.Name.Strings[0]
Sasha Smundakea3bc3a2021-11-10 13:06:42 -0800539 // The `override` directive
540 // override FOO :=
541 // is parsed as an assignment to a variable named `override FOO`.
542 // There are very few places where `override` is used, just flag it.
543 if strings.HasPrefix(name, "override ") {
Cole Faustdd569ae2022-01-31 15:48:29 -0800544 return []starlarkNode{ctx.newBadNode(a, "cannot handle override directive")}
Sasha Smundakea3bc3a2021-11-10 13:06:42 -0800545 }
546
Cole Faustc00184e2021-11-08 12:08:57 -0800547 // Soong configuration
Sasha Smundak3deb9682021-07-26 18:42:25 -0700548 if strings.HasPrefix(name, soongNsPrefix) {
Cole Faustdd569ae2022-01-31 15:48:29 -0800549 return ctx.handleSoongNsAssignment(strings.TrimPrefix(name, soongNsPrefix), a)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700550 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800551 lhs := ctx.addVariable(name)
552 if lhs == nil {
Cole Faustdd569ae2022-01-31 15:48:29 -0800553 return []starlarkNode{ctx.newBadNode(a, "unknown variable %s", name)}
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800554 }
Cole Faust3c4fc992022-02-28 16:05:01 -0800555 _, isTraced := ctx.tracedVariables[lhs.name()]
Sasha Smundak422b6142021-11-11 18:31:59 -0800556 asgn := &assignmentNode{lhs: lhs, mkValue: a.Value, isTraced: isTraced, location: ctx.errorLocation(a)}
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800557 if lhs.valueType() == starlarkTypeUnknown {
558 // Try to divine variable type from the RHS
559 asgn.value = ctx.parseMakeString(a, a.Value)
560 if xBad, ok := asgn.value.(*badExpr); ok {
Cole Faustdd569ae2022-01-31 15:48:29 -0800561 return []starlarkNode{&exprNode{xBad}}
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800562 }
563 inferred_type := asgn.value.typ()
564 if inferred_type != starlarkTypeUnknown {
Sasha Smundak9d011ab2021-07-09 16:00:57 -0700565 lhs.setValueType(inferred_type)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800566 }
567 }
568 if lhs.valueType() == starlarkTypeList {
Cole Faustdd569ae2022-01-31 15:48:29 -0800569 xConcat, xBad := ctx.buildConcatExpr(a)
570 if xBad != nil {
571 return []starlarkNode{&exprNode{expr: xBad}}
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800572 }
573 switch len(xConcat.items) {
574 case 0:
575 asgn.value = &listExpr{}
576 case 1:
577 asgn.value = xConcat.items[0]
578 default:
579 asgn.value = xConcat
580 }
581 } else {
582 asgn.value = ctx.parseMakeString(a, a.Value)
583 if xBad, ok := asgn.value.(*badExpr); ok {
Cole Faustdd569ae2022-01-31 15:48:29 -0800584 return []starlarkNode{&exprNode{expr: xBad}}
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800585 }
586 }
587
Cole Faust421a1922022-03-16 14:35:45 -0700588 if asgn.lhs.valueType() == starlarkTypeString &&
589 asgn.value.typ() != starlarkTypeUnknown &&
590 asgn.value.typ() != starlarkTypeString {
591 asgn.value = &toStringExpr{expr: asgn.value}
592 }
593
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800594 switch a.Type {
595 case "=", ":=":
596 asgn.flavor = asgnSet
597 case "+=":
Cole Fauste2a37982022-03-09 16:00:17 -0800598 asgn.flavor = asgnAppend
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800599 case "?=":
600 asgn.flavor = asgnMaybeSet
601 default:
602 panic(fmt.Errorf("unexpected assignment type %s", a.Type))
603 }
604
Cole Faustdd569ae2022-01-31 15:48:29 -0800605 return []starlarkNode{asgn}
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800606}
607
Cole Faustdd569ae2022-01-31 15:48:29 -0800608func (ctx *parseContext) handleSoongNsAssignment(name string, asgn *mkparser.Assignment) []starlarkNode {
Sasha Smundak3deb9682021-07-26 18:42:25 -0700609 val := ctx.parseMakeString(asgn, asgn.Value)
610 if xBad, ok := val.(*badExpr); ok {
Cole Faustdd569ae2022-01-31 15:48:29 -0800611 return []starlarkNode{&exprNode{expr: xBad}}
Sasha Smundak3deb9682021-07-26 18:42:25 -0700612 }
Sasha Smundak3deb9682021-07-26 18:42:25 -0700613
614 // Unfortunately, Soong namespaces can be set up by directly setting corresponding Make
615 // variables instead of via add_soong_config_namespace + add_soong_config_var_value.
616 // Try to divine the call from the assignment as follows:
617 if name == "NAMESPACES" {
618 // Upon seeng
619 // SOONG_CONFIG_NAMESPACES += foo
620 // remember that there is a namespace `foo` and act as we saw
621 // $(call add_soong_config_namespace,foo)
622 s, ok := maybeString(val)
623 if !ok {
Cole Faustdd569ae2022-01-31 15:48:29 -0800624 return []starlarkNode{ctx.newBadNode(asgn, "cannot handle variables in SOONG_CONFIG_NAMESPACES assignment, please use add_soong_config_namespace instead")}
Sasha Smundak3deb9682021-07-26 18:42:25 -0700625 }
Cole Faustdd569ae2022-01-31 15:48:29 -0800626 result := make([]starlarkNode, 0)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700627 for _, ns := range strings.Fields(s) {
628 ctx.addSoongNamespace(ns)
Cole Faustdd569ae2022-01-31 15:48:29 -0800629 result = append(result, &exprNode{&callExpr{
Cole Faust9ebf6e42021-12-13 14:08:34 -0800630 name: baseName + ".soong_config_namespace",
631 args: []starlarkExpr{&globalsExpr{}, &stringLiteralExpr{ns}},
Sasha Smundak3deb9682021-07-26 18:42:25 -0700632 returnType: starlarkTypeVoid,
633 }})
634 }
Cole Faustdd569ae2022-01-31 15:48:29 -0800635 return result
Sasha Smundak3deb9682021-07-26 18:42:25 -0700636 } else {
637 // Upon seeing
638 // SOONG_CONFIG_x_y = v
639 // find a namespace called `x` and act as if we encountered
Cole Faustc00184e2021-11-08 12:08:57 -0800640 // $(call soong_config_set,x,y,v)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700641 // or check that `x_y` is a namespace, and then add the RHS of this assignment as variables in
642 // it.
643 // Emit an error in the ambiguous situation (namespaces `foo_bar` with a variable `baz`
644 // and `foo` with a variable `bar_baz`.
645 namespaceName := ""
646 if ctx.hasSoongNamespace(name) {
647 namespaceName = name
648 }
649 var varName string
650 for pos, ch := range name {
651 if !(ch == '_' && ctx.hasSoongNamespace(name[0:pos])) {
652 continue
653 }
654 if namespaceName != "" {
Cole Faustdd569ae2022-01-31 15:48:29 -0800655 return []starlarkNode{ctx.newBadNode(asgn, "ambiguous soong namespace (may be either `%s` or `%s`)", namespaceName, name[0:pos])}
Sasha Smundak3deb9682021-07-26 18:42:25 -0700656 }
657 namespaceName = name[0:pos]
658 varName = name[pos+1:]
659 }
660 if namespaceName == "" {
Cole Faustdd569ae2022-01-31 15:48:29 -0800661 return []starlarkNode{ctx.newBadNode(asgn, "cannot figure out Soong namespace, please use add_soong_config_var_value macro instead")}
Sasha Smundak3deb9682021-07-26 18:42:25 -0700662 }
663 if varName == "" {
664 // Remember variables in this namespace
665 s, ok := maybeString(val)
666 if !ok {
Cole Faustdd569ae2022-01-31 15:48:29 -0800667 return []starlarkNode{ctx.newBadNode(asgn, "cannot handle variables in SOONG_CONFIG_ assignment, please use add_soong_config_var_value instead")}
Sasha Smundak3deb9682021-07-26 18:42:25 -0700668 }
669 ctx.updateSoongNamespace(asgn.Type != "+=", namespaceName, strings.Fields(s))
Cole Faustdd569ae2022-01-31 15:48:29 -0800670 return []starlarkNode{}
Sasha Smundak3deb9682021-07-26 18:42:25 -0700671 }
672
673 // Finally, handle assignment to a namespace variable
674 if !ctx.hasNamespaceVar(namespaceName, varName) {
Cole Faustdd569ae2022-01-31 15:48:29 -0800675 return []starlarkNode{ctx.newBadNode(asgn, "no %s variable in %s namespace, please use add_soong_config_var_value instead", varName, namespaceName)}
Sasha Smundak3deb9682021-07-26 18:42:25 -0700676 }
Cole Faust9ebf6e42021-12-13 14:08:34 -0800677 fname := baseName + "." + soongConfigAssign
Sasha Smundak65b547e2021-09-17 15:35:41 -0700678 if asgn.Type == "+=" {
Cole Faust9ebf6e42021-12-13 14:08:34 -0800679 fname = baseName + "." + soongConfigAppend
Sasha Smundak65b547e2021-09-17 15:35:41 -0700680 }
Cole Faustdd569ae2022-01-31 15:48:29 -0800681 return []starlarkNode{&exprNode{&callExpr{
Sasha Smundak65b547e2021-09-17 15:35:41 -0700682 name: fname,
Cole Faust9ebf6e42021-12-13 14:08:34 -0800683 args: []starlarkExpr{&globalsExpr{}, &stringLiteralExpr{namespaceName}, &stringLiteralExpr{varName}, val},
Sasha Smundak3deb9682021-07-26 18:42:25 -0700684 returnType: starlarkTypeVoid,
Cole Faustdd569ae2022-01-31 15:48:29 -0800685 }}}
Sasha Smundak3deb9682021-07-26 18:42:25 -0700686 }
687}
688
Cole Faustdd569ae2022-01-31 15:48:29 -0800689func (ctx *parseContext) buildConcatExpr(a *mkparser.Assignment) (*concatExpr, *badExpr) {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800690 xConcat := &concatExpr{}
691 var xItemList *listExpr
692 addToItemList := func(x ...starlarkExpr) {
693 if xItemList == nil {
694 xItemList = &listExpr{[]starlarkExpr{}}
695 }
696 xItemList.items = append(xItemList.items, x...)
697 }
698 finishItemList := func() {
699 if xItemList != nil {
700 xConcat.items = append(xConcat.items, xItemList)
701 xItemList = nil
702 }
703 }
704
705 items := a.Value.Words()
706 for _, item := range items {
707 // A function call in RHS is supposed to return a list, all other item
708 // expressions return individual elements.
709 switch x := ctx.parseMakeString(a, item).(type) {
710 case *badExpr:
Cole Faustdd569ae2022-01-31 15:48:29 -0800711 return nil, x
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800712 case *stringLiteralExpr:
713 addToItemList(maybeConvertToStringList(x).(*listExpr).items...)
714 default:
715 switch x.typ() {
716 case starlarkTypeList:
717 finishItemList()
718 xConcat.items = append(xConcat.items, x)
719 case starlarkTypeString:
720 finishItemList()
721 xConcat.items = append(xConcat.items, &callExpr{
722 object: x,
723 name: "split",
724 args: nil,
725 returnType: starlarkTypeList,
726 })
727 default:
728 addToItemList(x)
729 }
730 }
731 }
732 if xItemList != nil {
733 xConcat.items = append(xConcat.items, xItemList)
734 }
Cole Faustdd569ae2022-01-31 15:48:29 -0800735 return xConcat, nil
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800736}
737
Sasha Smundak6609ba72021-07-22 18:32:56 -0700738func (ctx *parseContext) newDependentModule(path string, optional bool) *moduleInfo {
739 modulePath := ctx.loadedModulePath(path)
740 if mi, ok := ctx.dependentModules[modulePath]; ok {
Sasha Smundak868c5e32021-09-23 16:20:58 -0700741 mi.optional = mi.optional && optional
Sasha Smundak6609ba72021-07-22 18:32:56 -0700742 return mi
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800743 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800744 moduleName := moduleNameForFile(path)
745 moduleLocalName := "_" + moduleName
746 n, found := ctx.moduleNameCount[moduleName]
747 if found {
748 moduleLocalName += fmt.Sprintf("%d", n)
749 }
750 ctx.moduleNameCount[moduleName] = n + 1
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800751 _, err := fs.Stat(ctx.script.sourceFS, path)
Sasha Smundak6609ba72021-07-22 18:32:56 -0700752 mi := &moduleInfo{
753 path: modulePath,
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800754 originalPath: path,
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800755 moduleLocalName: moduleLocalName,
Sasha Smundak6609ba72021-07-22 18:32:56 -0700756 optional: optional,
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800757 missing: err != nil,
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800758 }
Sasha Smundak6609ba72021-07-22 18:32:56 -0700759 ctx.dependentModules[modulePath] = mi
760 ctx.script.inherited = append(ctx.script.inherited, mi)
761 return mi
762}
763
764func (ctx *parseContext) handleSubConfig(
Cole Faustdd569ae2022-01-31 15:48:29 -0800765 v mkparser.Node, pathExpr starlarkExpr, loadAlways bool, processModule func(inheritedModule) starlarkNode) []starlarkNode {
Sasha Smundak6609ba72021-07-22 18:32:56 -0700766
Cole Faust62e05112022-04-05 17:56:11 -0700767 // Allow seeing $(sort $(wildcard realPathExpr)) or $(wildcard realPathExpr)
768 // because those are functionally the same as not having the sort/wildcard calls.
769 if ce, ok := pathExpr.(*callExpr); ok && ce.name == "rblf.mksort" && len(ce.args) == 1 {
770 if ce2, ok2 := ce.args[0].(*callExpr); ok2 && ce2.name == "rblf.expand_wildcard" && len(ce2.args) == 1 {
771 pathExpr = ce2.args[0]
772 }
773 } else if ce2, ok2 := pathExpr.(*callExpr); ok2 && ce2.name == "rblf.expand_wildcard" && len(ce2.args) == 1 {
774 pathExpr = ce2.args[0]
775 }
776
Sasha Smundak6609ba72021-07-22 18:32:56 -0700777 // In a simple case, the name of a module to inherit/include is known statically.
778 if path, ok := maybeString(pathExpr); ok {
Sasha Smundak868c5e32021-09-23 16:20:58 -0700779 // Note that even if this directive loads a module unconditionally, a module may be
780 // absent without causing any harm if this directive is inside an if/else block.
781 moduleShouldExist := loadAlways && ctx.ifNestLevel == 0
Sasha Smundak6609ba72021-07-22 18:32:56 -0700782 if strings.Contains(path, "*") {
783 if paths, err := fs.Glob(ctx.script.sourceFS, path); err == nil {
Cole Faust62e05112022-04-05 17:56:11 -0700784 sort.Strings(paths)
Cole Faustdd569ae2022-01-31 15:48:29 -0800785 result := make([]starlarkNode, 0)
Sasha Smundak6609ba72021-07-22 18:32:56 -0700786 for _, p := range paths {
Sasha Smundak868c5e32021-09-23 16:20:58 -0700787 mi := ctx.newDependentModule(p, !moduleShouldExist)
Cole Faustdd569ae2022-01-31 15:48:29 -0800788 result = append(result, processModule(inheritedStaticModule{mi, loadAlways}))
Sasha Smundak6609ba72021-07-22 18:32:56 -0700789 }
Cole Faustdd569ae2022-01-31 15:48:29 -0800790 return result
Sasha Smundak6609ba72021-07-22 18:32:56 -0700791 } else {
Cole Faustdd569ae2022-01-31 15:48:29 -0800792 return []starlarkNode{ctx.newBadNode(v, "cannot glob wildcard argument")}
Sasha Smundak6609ba72021-07-22 18:32:56 -0700793 }
794 } else {
Sasha Smundak868c5e32021-09-23 16:20:58 -0700795 mi := ctx.newDependentModule(path, !moduleShouldExist)
Cole Faustdd569ae2022-01-31 15:48:29 -0800796 return []starlarkNode{processModule(inheritedStaticModule{mi, loadAlways})}
Sasha Smundak6609ba72021-07-22 18:32:56 -0700797 }
Sasha Smundak6609ba72021-07-22 18:32:56 -0700798 }
799
800 // If module path references variables (e.g., $(v1)/foo/$(v2)/device-config.mk), find all the paths in the
801 // source tree that may be a match and the corresponding variable values. For instance, if the source tree
802 // contains vendor1/foo/abc/dev.mk and vendor2/foo/def/dev.mk, the first one will be inherited when
803 // (v1, v2) == ('vendor1', 'abc'), and the second one when (v1, v2) == ('vendor2', 'def').
804 // We then emit the code that loads all of them, e.g.:
805 // load("//vendor1/foo/abc:dev.rbc", _dev1_init="init")
806 // load("//vendor2/foo/def/dev.rbc", _dev2_init="init")
807 // And then inherit it as follows:
808 // _e = {
809 // "vendor1/foo/abc/dev.mk": ("vendor1/foo/abc/dev", _dev1_init),
810 // "vendor2/foo/def/dev.mk": ("vendor2/foo/def/dev", _dev_init2) }.get("%s/foo/%s/dev.mk" % (v1, v2))
811 // if _e:
812 // rblf.inherit(handle, _e[0], _e[1])
813 //
814 var matchingPaths []string
815 varPath, ok := pathExpr.(*interpolateExpr)
816 if !ok {
Cole Faustdd569ae2022-01-31 15:48:29 -0800817 return []starlarkNode{ctx.newBadNode(v, "inherit-product/include argument is too complex")}
Sasha Smundak6609ba72021-07-22 18:32:56 -0700818 }
819
820 pathPattern := []string{varPath.chunks[0]}
821 for _, chunk := range varPath.chunks[1:] {
822 if chunk != "" {
823 pathPattern = append(pathPattern, chunk)
824 }
825 }
Cole Faust069aba62022-01-26 17:47:33 -0800826 if pathPattern[0] == "" && len(ctx.includeTops) > 0 {
Sasha Smundak6d852dd2021-09-27 20:34:39 -0700827 // If pattern starts from the top. restrict it to the directories where
828 // we know inherit-product uses dynamically calculated path.
829 for _, p := range ctx.includeTops {
830 pathPattern[0] = p
831 matchingPaths = append(matchingPaths, ctx.findMatchingPaths(pathPattern)...)
Sasha Smundak6609ba72021-07-22 18:32:56 -0700832 }
Sasha Smundak6d852dd2021-09-27 20:34:39 -0700833 } else {
834 matchingPaths = ctx.findMatchingPaths(pathPattern)
Sasha Smundak6609ba72021-07-22 18:32:56 -0700835 }
836 // Safeguard against $(call inherit-product,$(PRODUCT_PATH))
Sasha Smundak90be8c52021-08-03 11:06:10 -0700837 const maxMatchingFiles = 150
Sasha Smundak6609ba72021-07-22 18:32:56 -0700838 if len(matchingPaths) > maxMatchingFiles {
Cole Faustdd569ae2022-01-31 15:48:29 -0800839 return []starlarkNode{ctx.newBadNode(v, "there are >%d files matching the pattern, please rewrite it", maxMatchingFiles)}
Sasha Smundak6609ba72021-07-22 18:32:56 -0700840 }
Cole Faust93f8d392022-03-02 13:31:30 -0800841
842 needsWarning := pathPattern[0] == "" && len(ctx.includeTops) == 0
843 res := inheritedDynamicModule{*varPath, []*moduleInfo{}, loadAlways, ctx.errorLocation(v), needsWarning}
844 for _, p := range matchingPaths {
845 // A product configuration files discovered dynamically may attempt to inherit
846 // from another one which does not exist in this source tree. Prevent load errors
847 // by always loading the dynamic files as optional.
848 res.candidateModules = append(res.candidateModules, ctx.newDependentModule(p, true))
Sasha Smundak6609ba72021-07-22 18:32:56 -0700849 }
Cole Faust93f8d392022-03-02 13:31:30 -0800850 return []starlarkNode{processModule(res)}
Sasha Smundak6609ba72021-07-22 18:32:56 -0700851}
852
853func (ctx *parseContext) findMatchingPaths(pattern []string) []string {
Cole Faust9b6111a2022-02-02 15:38:33 -0800854 files := ctx.script.makefileFinder.Find(".")
Sasha Smundak6609ba72021-07-22 18:32:56 -0700855 if len(pattern) == 0 {
856 return files
857 }
858
859 // Create regular expression from the pattern
860 s_regexp := "^" + regexp.QuoteMeta(pattern[0])
861 for _, s := range pattern[1:] {
862 s_regexp += ".*" + regexp.QuoteMeta(s)
863 }
864 s_regexp += "$"
865 rex := regexp.MustCompile(s_regexp)
866
867 // Now match
868 var res []string
869 for _, p := range files {
870 if rex.MatchString(p) {
871 res = append(res, p)
872 }
873 }
874 return res
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800875}
876
Cole Faustf035d402022-03-28 14:02:50 -0700877type inheritProductCallParser struct {
878 loadAlways bool
879}
880
881func (p *inheritProductCallParser) parse(ctx *parseContext, v mkparser.Node, args *mkparser.MakeString) []starlarkNode {
Cole Faust9ebf6e42021-12-13 14:08:34 -0800882 args.TrimLeftSpaces()
883 args.TrimRightSpaces()
884 pathExpr := ctx.parseMakeString(v, args)
885 if _, ok := pathExpr.(*badExpr); ok {
Cole Faustdd569ae2022-01-31 15:48:29 -0800886 return []starlarkNode{ctx.newBadNode(v, "Unable to parse argument to inherit")}
Cole Faust9ebf6e42021-12-13 14:08:34 -0800887 }
Cole Faustf035d402022-03-28 14:02:50 -0700888 return ctx.handleSubConfig(v, pathExpr, p.loadAlways, func(im inheritedModule) starlarkNode {
889 return &inheritNode{im, p.loadAlways}
Sasha Smundak6609ba72021-07-22 18:32:56 -0700890 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800891}
892
Cole Faustdd569ae2022-01-31 15:48:29 -0800893func (ctx *parseContext) handleInclude(v mkparser.Node, pathExpr starlarkExpr, loadAlways bool) []starlarkNode {
894 return ctx.handleSubConfig(v, pathExpr, loadAlways, func(im inheritedModule) starlarkNode {
895 return &includeNode{im, loadAlways}
Sasha Smundak6609ba72021-07-22 18:32:56 -0700896 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800897}
898
Cole Faustdd569ae2022-01-31 15:48:29 -0800899func (ctx *parseContext) handleVariable(v *mkparser.Variable) []starlarkNode {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800900 // Handle:
901 // $(call inherit-product,...)
902 // $(call inherit-product-if-exists,...)
903 // $(info xxx)
904 // $(warning xxx)
905 // $(error xxx)
Cole Faust9ebf6e42021-12-13 14:08:34 -0800906 // $(call other-custom-functions,...)
907
Cole Faustf035d402022-03-28 14:02:50 -0700908 if name, args, ok := ctx.maybeParseFunctionCall(v, v.Name); ok {
909 if kf, ok := knownNodeFunctions[name]; ok {
910 return kf.parse(ctx, v, args)
911 }
Cole Faust9ebf6e42021-12-13 14:08:34 -0800912 }
Cole Faustf035d402022-03-28 14:02:50 -0700913
Cole Faustdd569ae2022-01-31 15:48:29 -0800914 return []starlarkNode{&exprNode{expr: ctx.parseReference(v, v.Name)}}
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800915}
916
Cole Faustdd569ae2022-01-31 15:48:29 -0800917func (ctx *parseContext) maybeHandleDefine(directive *mkparser.Directive) starlarkNode {
Sasha Smundakf3e072a2021-07-14 12:50:28 -0700918 macro_name := strings.Fields(directive.Args.Strings[0])[0]
919 // Ignore the macros that we handle
Cole Faust9ebf6e42021-12-13 14:08:34 -0800920 _, ignored := ignoredDefines[macro_name]
921 _, known := knownFunctions[macro_name]
922 if !ignored && !known {
Cole Faustdd569ae2022-01-31 15:48:29 -0800923 return ctx.newBadNode(directive, "define is not supported: %s", macro_name)
Sasha Smundakf3e072a2021-07-14 12:50:28 -0700924 }
Cole Faustdd569ae2022-01-31 15:48:29 -0800925 return nil
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800926}
927
Cole Faustdd569ae2022-01-31 15:48:29 -0800928func (ctx *parseContext) handleIfBlock(ifDirective *mkparser.Directive) starlarkNode {
929 ssSwitch := &switchNode{
930 ssCases: []*switchCase{ctx.processBranch(ifDirective)},
931 }
932 for ctx.hasNodes() && ctx.fatalError == nil {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800933 node := ctx.getNode()
934 switch x := node.(type) {
935 case *mkparser.Directive:
936 switch x.Name {
937 case "else", "elifdef", "elifndef", "elifeq", "elifneq":
Cole Faustdd569ae2022-01-31 15:48:29 -0800938 ssSwitch.ssCases = append(ssSwitch.ssCases, ctx.processBranch(x))
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800939 case "endif":
Cole Faustdd569ae2022-01-31 15:48:29 -0800940 return ssSwitch
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800941 default:
Cole Faustdd569ae2022-01-31 15:48:29 -0800942 return ctx.newBadNode(node, "unexpected directive %s", x.Name)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800943 }
944 default:
Cole Faustdd569ae2022-01-31 15:48:29 -0800945 return ctx.newBadNode(ifDirective, "unexpected statement")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800946 }
947 }
948 if ctx.fatalError == nil {
949 ctx.fatalError = fmt.Errorf("no matching endif for %s", ifDirective.Dump())
950 }
Cole Faustdd569ae2022-01-31 15:48:29 -0800951 return ctx.newBadNode(ifDirective, "no matching endif for %s", ifDirective.Dump())
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800952}
953
954// processBranch processes a single branch (if/elseif/else) until the next directive
955// on the same level.
Cole Faustdd569ae2022-01-31 15:48:29 -0800956func (ctx *parseContext) processBranch(check *mkparser.Directive) *switchCase {
957 block := &switchCase{gate: ctx.parseCondition(check)}
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800958 defer func() {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800959 ctx.ifNestLevel--
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800960 }()
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800961 ctx.ifNestLevel++
962
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800963 for ctx.hasNodes() {
964 node := ctx.getNode()
Cole Faust591a1fe2021-11-08 15:37:57 -0800965 if d, ok := node.(*mkparser.Directive); ok {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800966 switch d.Name {
967 case "else", "elifdef", "elifndef", "elifeq", "elifneq", "endif":
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800968 ctx.backNode()
Cole Faustdd569ae2022-01-31 15:48:29 -0800969 return block
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800970 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800971 }
Cole Faustdd569ae2022-01-31 15:48:29 -0800972 block.nodes = append(block.nodes, ctx.handleSimpleStatement(node)...)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800973 }
974 ctx.fatalError = fmt.Errorf("no matching endif for %s", check.Dump())
Cole Faustdd569ae2022-01-31 15:48:29 -0800975 return block
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800976}
977
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800978func (ctx *parseContext) parseCondition(check *mkparser.Directive) starlarkNode {
979 switch check.Name {
980 case "ifdef", "ifndef", "elifdef", "elifndef":
Cole Faust71514c02022-01-27 17:21:41 -0800981 if !check.Args.Const() {
Cole Faustdd569ae2022-01-31 15:48:29 -0800982 return ctx.newBadNode(check, "ifdef variable ref too complex: %s", check.Args.Dump())
Cole Faust71514c02022-01-27 17:21:41 -0800983 }
Cole Faustf0632662022-04-07 13:59:24 -0700984 v := NewVariableRefExpr(ctx.addVariable(check.Args.Strings[0]))
Cole Faust71514c02022-01-27 17:21:41 -0800985 if strings.HasSuffix(check.Name, "ndef") {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800986 v = &notExpr{v}
987 }
988 return &ifNode{
989 isElif: strings.HasPrefix(check.Name, "elif"),
990 expr: v,
991 }
992 case "ifeq", "ifneq", "elifeq", "elifneq":
993 return &ifNode{
994 isElif: strings.HasPrefix(check.Name, "elif"),
995 expr: ctx.parseCompare(check),
996 }
997 case "else":
998 return &elseNode{}
999 default:
1000 panic(fmt.Errorf("%s: unknown directive: %s", ctx.script.mkFile, check.Dump()))
1001 }
1002}
1003
1004func (ctx *parseContext) newBadExpr(node mkparser.Node, text string, args ...interface{}) starlarkExpr {
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001005 if ctx.errorLogger != nil {
Sasha Smundak422b6142021-11-11 18:31:59 -08001006 ctx.errorLogger.NewError(ctx.errorLocation(node), node, text, args...)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001007 }
1008 ctx.script.hasErrors = true
Cole Faustdd569ae2022-01-31 15:48:29 -08001009 return &badExpr{errorLocation: ctx.errorLocation(node), message: fmt.Sprintf(text, args...)}
1010}
1011
1012// records that the given node failed to be converted and includes an explanatory message
1013func (ctx *parseContext) newBadNode(failedNode mkparser.Node, message string, args ...interface{}) starlarkNode {
1014 return &exprNode{ctx.newBadExpr(failedNode, message, args...)}
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001015}
1016
1017func (ctx *parseContext) parseCompare(cond *mkparser.Directive) starlarkExpr {
1018 // Strip outer parentheses
1019 mkArg := cloneMakeString(cond.Args)
1020 mkArg.Strings[0] = strings.TrimLeft(mkArg.Strings[0], "( ")
1021 n := len(mkArg.Strings)
1022 mkArg.Strings[n-1] = strings.TrimRight(mkArg.Strings[n-1], ") ")
1023 args := mkArg.Split(",")
1024 // TODO(asmundak): handle the case where the arguments are in quotes and space-separated
1025 if len(args) != 2 {
1026 return ctx.newBadExpr(cond, "ifeq/ifneq len(args) != 2 %s", cond.Dump())
1027 }
1028 args[0].TrimRightSpaces()
1029 args[1].TrimLeftSpaces()
1030
1031 isEq := !strings.HasSuffix(cond.Name, "neq")
Cole Faustf8320212021-11-10 15:05:07 -08001032 xLeft := ctx.parseMakeString(cond, args[0])
1033 xRight := ctx.parseMakeString(cond, args[1])
1034 if bad, ok := xLeft.(*badExpr); ok {
1035 return bad
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001036 }
Cole Faustf8320212021-11-10 15:05:07 -08001037 if bad, ok := xRight.(*badExpr); ok {
1038 return bad
1039 }
1040
1041 if expr, ok := ctx.parseCompareSpecialCases(cond, xLeft, xRight); ok {
1042 return expr
1043 }
1044
Cole Faust9ebf6e42021-12-13 14:08:34 -08001045 var stringOperand string
1046 var otherOperand starlarkExpr
1047 if s, ok := maybeString(xLeft); ok {
1048 stringOperand = s
1049 otherOperand = xRight
1050 } else if s, ok := maybeString(xRight); ok {
1051 stringOperand = s
1052 otherOperand = xLeft
1053 }
1054
Cole Faust9ebf6e42021-12-13 14:08:34 -08001055 // If we've identified one of the operands as being a string literal, check
1056 // for some special cases we can do to simplify the resulting expression.
1057 if otherOperand != nil {
1058 if stringOperand == "" {
1059 if isEq {
Cole Faustf035d402022-03-28 14:02:50 -07001060 return negateExpr(otherOperand)
Cole Faust9ebf6e42021-12-13 14:08:34 -08001061 } else {
1062 return otherOperand
1063 }
1064 }
1065 if stringOperand == "true" && otherOperand.typ() == starlarkTypeBool {
1066 if !isEq {
Cole Faustf035d402022-03-28 14:02:50 -07001067 return negateExpr(otherOperand)
Cole Faust9ebf6e42021-12-13 14:08:34 -08001068 } else {
1069 return otherOperand
1070 }
1071 }
Cole Faustb1103e22022-01-06 15:22:05 -08001072 if intOperand, err := strconv.Atoi(strings.TrimSpace(stringOperand)); err == nil && otherOperand.typ() == starlarkTypeInt {
1073 return &eqExpr{
1074 left: otherOperand,
1075 right: &intLiteralExpr{literal: intOperand},
1076 isEq: isEq,
1077 }
1078 }
Cole Faust9ebf6e42021-12-13 14:08:34 -08001079 }
1080
Cole Faustf8320212021-11-10 15:05:07 -08001081 return &eqExpr{left: xLeft, right: xRight, isEq: isEq}
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001082}
1083
Cole Faustf8320212021-11-10 15:05:07 -08001084// Given an if statement's directive and the left/right starlarkExprs,
1085// check if the starlarkExprs are one of a few hardcoded special cases
Cole Faust9932f752022-02-08 11:56:25 -08001086// that can be converted to a simpler equality expression than simply comparing
Cole Faustf8320212021-11-10 15:05:07 -08001087// the two.
1088func (ctx *parseContext) parseCompareSpecialCases(directive *mkparser.Directive, left starlarkExpr,
1089 right starlarkExpr) (starlarkExpr, bool) {
1090 isEq := !strings.HasSuffix(directive.Name, "neq")
1091
1092 // All the special cases require a call on one side and a
1093 // string literal/variable on the other. Turn the left/right variables into
1094 // call/value variables, and return false if that's not possible.
1095 var value starlarkExpr = nil
1096 call, ok := left.(*callExpr)
1097 if ok {
1098 switch right.(type) {
1099 case *stringLiteralExpr, *variableRefExpr:
1100 value = right
1101 }
1102 } else {
1103 call, _ = right.(*callExpr)
1104 switch left.(type) {
1105 case *stringLiteralExpr, *variableRefExpr:
1106 value = left
1107 }
1108 }
1109
1110 if call == nil || value == nil {
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001111 return nil, false
1112 }
Cole Faustf8320212021-11-10 15:05:07 -08001113
Cole Faustf8320212021-11-10 15:05:07 -08001114 switch call.name {
Cole Faust9932f752022-02-08 11:56:25 -08001115 case baseName + ".filter":
1116 return ctx.parseCompareFilterFuncResult(directive, call, value, isEq)
Cole Faust9ebf6e42021-12-13 14:08:34 -08001117 case baseName + ".expand_wildcard":
Cole Faustf8320212021-11-10 15:05:07 -08001118 return ctx.parseCompareWildcardFuncResult(directive, call, value, !isEq), true
Cole Faust9ebf6e42021-12-13 14:08:34 -08001119 case baseName + ".findstring":
Cole Faustf8320212021-11-10 15:05:07 -08001120 return ctx.parseCheckFindstringFuncResult(directive, call, value, !isEq), true
Cole Faust9ebf6e42021-12-13 14:08:34 -08001121 case baseName + ".strip":
Cole Faustf8320212021-11-10 15:05:07 -08001122 return ctx.parseCompareStripFuncResult(directive, call, value, !isEq), true
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001123 }
Cole Faustf8320212021-11-10 15:05:07 -08001124 return nil, false
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001125}
1126
1127func (ctx *parseContext) parseCompareFilterFuncResult(cond *mkparser.Directive,
Cole Faust9932f752022-02-08 11:56:25 -08001128 filterFuncCall *callExpr, xValue starlarkExpr, negate bool) (starlarkExpr, bool) {
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001129 // We handle:
Sasha Smundak0554d762021-07-08 18:26:12 -07001130 // * ifeq/ifneq (,$(filter v1 v2 ..., EXPR) becomes if EXPR not in/in ["v1", "v2", ...]
1131 // * ifeq/ifneq (,$(filter EXPR, v1 v2 ...) becomes if EXPR not in/in ["v1", "v2", ...]
Cole Faust9932f752022-02-08 11:56:25 -08001132 if x, ok := xValue.(*stringLiteralExpr); !ok || x.literal != "" {
1133 return nil, false
1134 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001135 xPattern := filterFuncCall.args[0]
1136 xText := filterFuncCall.args[1]
1137 var xInList *stringLiteralExpr
Sasha Smundak0554d762021-07-08 18:26:12 -07001138 var expr starlarkExpr
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001139 var ok bool
Cole Faust9932f752022-02-08 11:56:25 -08001140 if xInList, ok = xPattern.(*stringLiteralExpr); ok && !strings.ContainsRune(xInList.literal, '%') && xText.typ() == starlarkTypeList {
1141 expr = xText
1142 } else if xInList, ok = xText.(*stringLiteralExpr); ok {
1143 expr = xPattern
1144 } else {
1145 return nil, false
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001146 }
Cole Faust9932f752022-02-08 11:56:25 -08001147 slExpr := newStringListExpr(strings.Fields(xInList.literal))
1148 // Generate simpler code for the common cases:
1149 if expr.typ() == starlarkTypeList {
1150 if len(slExpr.items) == 1 {
1151 // Checking that a string belongs to list
1152 return &inExpr{isNot: negate, list: expr, expr: slExpr.items[0]}, true
Sasha Smundak0554d762021-07-08 18:26:12 -07001153 } else {
Cole Faust9932f752022-02-08 11:56:25 -08001154 return nil, false
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001155 }
Cole Faust9932f752022-02-08 11:56:25 -08001156 } else if len(slExpr.items) == 1 {
1157 return &eqExpr{left: expr, right: slExpr.items[0], isEq: !negate}, true
1158 } else {
1159 return &inExpr{isNot: negate, list: newStringListExpr(strings.Fields(xInList.literal)), expr: expr}, true
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001160 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001161}
1162
1163func (ctx *parseContext) parseCompareWildcardFuncResult(directive *mkparser.Directive,
1164 xCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr {
Sasha Smundak0554d762021-07-08 18:26:12 -07001165 if !isEmptyString(xValue) {
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001166 return ctx.newBadExpr(directive, "wildcard result can be compared only to empty: %s", xValue)
1167 }
Cole Faust9ebf6e42021-12-13 14:08:34 -08001168 callFunc := baseName + ".file_wildcard_exists"
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001169 if s, ok := xCall.args[0].(*stringLiteralExpr); ok && !strings.ContainsAny(s.literal, "*?{[") {
Cole Faust9ebf6e42021-12-13 14:08:34 -08001170 callFunc = baseName + ".file_exists"
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001171 }
1172 var cc starlarkExpr = &callExpr{name: callFunc, args: xCall.args, returnType: starlarkTypeBool}
1173 if !negate {
1174 cc = &notExpr{cc}
1175 }
1176 return cc
1177}
1178
1179func (ctx *parseContext) parseCheckFindstringFuncResult(directive *mkparser.Directive,
1180 xCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr {
Sasha Smundak0554d762021-07-08 18:26:12 -07001181 if isEmptyString(xValue) {
1182 return &eqExpr{
1183 left: &callExpr{
1184 object: xCall.args[1],
1185 name: "find",
1186 args: []starlarkExpr{xCall.args[0]},
1187 returnType: starlarkTypeInt,
1188 },
1189 right: &intLiteralExpr{-1},
1190 isEq: !negate,
1191 }
Cole Faust0e9418c2021-12-13 16:33:25 -08001192 } else if s, ok := maybeString(xValue); ok {
1193 if s2, ok := maybeString(xCall.args[0]); ok && s == s2 {
1194 return &eqExpr{
1195 left: &callExpr{
1196 object: xCall.args[1],
1197 name: "find",
1198 args: []starlarkExpr{xCall.args[0]},
1199 returnType: starlarkTypeInt,
1200 },
1201 right: &intLiteralExpr{-1},
1202 isEq: negate,
1203 }
1204 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001205 }
Cole Faust0e9418c2021-12-13 16:33:25 -08001206 return ctx.newBadExpr(directive, "$(findstring) can only be compared to nothing or its first argument")
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001207}
1208
1209func (ctx *parseContext) parseCompareStripFuncResult(directive *mkparser.Directive,
1210 xCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr {
1211 if _, ok := xValue.(*stringLiteralExpr); !ok {
1212 return ctx.newBadExpr(directive, "strip result can be compared only to string: %s", xValue)
1213 }
1214 return &eqExpr{
1215 left: &callExpr{
1216 name: "strip",
1217 args: xCall.args,
1218 returnType: starlarkTypeString,
1219 },
1220 right: xValue, isEq: !negate}
1221}
1222
Cole Faustf035d402022-03-28 14:02:50 -07001223func (ctx *parseContext) maybeParseFunctionCall(node mkparser.Node, ref *mkparser.MakeString) (name string, args *mkparser.MakeString, ok bool) {
1224 ref.TrimLeftSpaces()
1225 ref.TrimRightSpaces()
1226
1227 words := ref.SplitN(" ", 2)
1228 if !words[0].Const() {
1229 return "", nil, false
1230 }
1231
1232 name = words[0].Dump()
1233 args = mkparser.SimpleMakeString("", words[0].Pos())
1234 if len(words) >= 2 {
1235 args = words[1]
1236 }
1237 args.TrimLeftSpaces()
1238 if name == "call" {
1239 words = args.SplitN(",", 2)
1240 if words[0].Empty() || !words[0].Const() {
1241 return "", nil, false
1242 }
1243 name = words[0].Dump()
1244 if len(words) < 2 {
Cole Faust6c41b8a2022-04-13 13:53:48 -07001245 args = mkparser.SimpleMakeString("", words[0].Pos())
Cole Faustf035d402022-03-28 14:02:50 -07001246 } else {
1247 args = words[1]
1248 }
1249 }
1250 ok = true
1251 return
1252}
1253
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001254// parses $(...), returning an expression
1255func (ctx *parseContext) parseReference(node mkparser.Node, ref *mkparser.MakeString) starlarkExpr {
1256 ref.TrimLeftSpaces()
1257 ref.TrimRightSpaces()
1258 refDump := ref.Dump()
1259
1260 // Handle only the case where the first (or only) word is constant
1261 words := ref.SplitN(" ", 2)
1262 if !words[0].Const() {
1263 return ctx.newBadExpr(node, "reference is too complex: %s", refDump)
1264 }
1265
1266 // If it is a single word, it can be a simple variable
1267 // reference or a function call
Cole Faustf035d402022-03-28 14:02:50 -07001268 if len(words) == 1 && !isMakeControlFunc(refDump) && refDump != "shell" && refDump != "eval" {
Sasha Smundak65b547e2021-09-17 15:35:41 -07001269 if strings.HasPrefix(refDump, soongNsPrefix) {
1270 // TODO (asmundak): if we find many, maybe handle them.
Cole Faustc00184e2021-11-08 12:08:57 -08001271 return ctx.newBadExpr(node, "SOONG_CONFIG_ variables cannot be referenced, use soong_config_get instead: %s", refDump)
Sasha Smundak65b547e2021-09-17 15:35:41 -07001272 }
Cole Faustc36c9622021-12-07 15:20:45 -08001273 // Handle substitution references: https://www.gnu.org/software/make/manual/html_node/Substitution-Refs.html
1274 if strings.Contains(refDump, ":") {
1275 parts := strings.SplitN(refDump, ":", 2)
1276 substParts := strings.SplitN(parts[1], "=", 2)
1277 if len(substParts) < 2 || strings.Count(substParts[0], "%") > 1 {
1278 return ctx.newBadExpr(node, "Invalid substitution reference")
1279 }
1280 if !strings.Contains(substParts[0], "%") {
1281 if strings.Contains(substParts[1], "%") {
1282 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.")
1283 }
1284 substParts[0] = "%" + substParts[0]
1285 substParts[1] = "%" + substParts[1]
1286 }
1287 v := ctx.addVariable(parts[0])
1288 if v == nil {
1289 return ctx.newBadExpr(node, "unknown variable %s", refDump)
1290 }
1291 return &callExpr{
Cole Faust9ebf6e42021-12-13 14:08:34 -08001292 name: baseName + ".mkpatsubst",
1293 returnType: starlarkTypeString,
Cole Faustc36c9622021-12-07 15:20:45 -08001294 args: []starlarkExpr{
1295 &stringLiteralExpr{literal: substParts[0]},
1296 &stringLiteralExpr{literal: substParts[1]},
Cole Faustf0632662022-04-07 13:59:24 -07001297 NewVariableRefExpr(v),
Cole Faustc36c9622021-12-07 15:20:45 -08001298 },
1299 }
1300 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001301 if v := ctx.addVariable(refDump); v != nil {
Cole Faustf0632662022-04-07 13:59:24 -07001302 return NewVariableRefExpr(v)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001303 }
1304 return ctx.newBadExpr(node, "unknown variable %s", refDump)
1305 }
1306
Cole Faustf035d402022-03-28 14:02:50 -07001307 if name, args, ok := ctx.maybeParseFunctionCall(node, ref); ok {
1308 if kf, found := knownFunctions[name]; found {
1309 return kf.parse(ctx, node, args)
Sasha Smundak6609ba72021-07-22 18:32:56 -07001310 } else {
Cole Faustf035d402022-03-28 14:02:50 -07001311 return ctx.newBadExpr(node, "cannot handle invoking %s", name)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001312 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001313 } else {
Cole Faustf035d402022-03-28 14:02:50 -07001314 return ctx.newBadExpr(node, "cannot handle %s", refDump)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001315 }
Cole Faust9ebf6e42021-12-13 14:08:34 -08001316}
1317
1318type simpleCallParser struct {
1319 name string
1320 returnType starlarkType
1321 addGlobals bool
Cole Faust1cc08852022-02-28 11:12:08 -08001322 addHandle bool
Cole Faust9ebf6e42021-12-13 14:08:34 -08001323}
1324
1325func (p *simpleCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
1326 expr := &callExpr{name: p.name, returnType: p.returnType}
1327 if p.addGlobals {
1328 expr.args = append(expr.args, &globalsExpr{})
1329 }
Cole Faust1cc08852022-02-28 11:12:08 -08001330 if p.addHandle {
1331 expr.args = append(expr.args, &identifierExpr{name: "handle"})
1332 }
Cole Faust9ebf6e42021-12-13 14:08:34 -08001333 for _, arg := range args.Split(",") {
1334 arg.TrimLeftSpaces()
1335 arg.TrimRightSpaces()
1336 x := ctx.parseMakeString(node, arg)
1337 if xBad, ok := x.(*badExpr); ok {
1338 return xBad
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001339 }
Cole Faust9ebf6e42021-12-13 14:08:34 -08001340 expr.args = append(expr.args, x)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001341 }
1342 return expr
1343}
1344
Cole Faust9ebf6e42021-12-13 14:08:34 -08001345type makeControlFuncParser struct {
1346 name string
1347}
1348
1349func (p *makeControlFuncParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
1350 // Make control functions need special treatment as everything
1351 // after the name is a single text argument
1352 x := ctx.parseMakeString(node, args)
1353 if xBad, ok := x.(*badExpr); ok {
1354 return xBad
1355 }
1356 return &callExpr{
1357 name: p.name,
1358 args: []starlarkExpr{
1359 &stringLiteralExpr{ctx.script.mkFile},
1360 x,
1361 },
1362 returnType: starlarkTypeUnknown,
1363 }
1364}
1365
1366type shellCallParser struct{}
1367
1368func (p *shellCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
1369 // Shell functions need special treatment as everything
1370 // after the name is a single text argument
1371 x := ctx.parseMakeString(node, args)
1372 if xBad, ok := x.(*badExpr); ok {
1373 return xBad
1374 }
1375 return &callExpr{
1376 name: baseName + ".shell",
1377 args: []starlarkExpr{x},
1378 returnType: starlarkTypeUnknown,
1379 }
1380}
1381
1382type myDirCallParser struct{}
1383
1384func (p *myDirCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
1385 if !args.Empty() {
1386 return ctx.newBadExpr(node, "my-dir function cannot have any arguments passed to it.")
1387 }
Cole Faustf5adedc2022-03-18 14:05:06 -07001388 return &stringLiteralExpr{literal: filepath.Dir(ctx.script.mkFile)}
Cole Faust9ebf6e42021-12-13 14:08:34 -08001389}
1390
Cole Faust9ebf6e42021-12-13 14:08:34 -08001391type isProductInListCallParser struct{}
1392
1393func (p *isProductInListCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
1394 if args.Empty() {
1395 return ctx.newBadExpr(node, "is-product-in-list requires an argument")
1396 }
1397 return &inExpr{
Cole Faustf0632662022-04-07 13:59:24 -07001398 expr: NewVariableRefExpr(ctx.addVariable("TARGET_PRODUCT")),
Cole Faust9ebf6e42021-12-13 14:08:34 -08001399 list: maybeConvertToStringList(ctx.parseMakeString(node, args)),
1400 isNot: false,
1401 }
1402}
1403
1404type isVendorBoardPlatformCallParser struct{}
1405
1406func (p *isVendorBoardPlatformCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
1407 if args.Empty() || !identifierFullMatchRegex.MatchString(args.Dump()) {
1408 return ctx.newBadExpr(node, "cannot handle non-constant argument to is-vendor-board-platform")
1409 }
1410 return &inExpr{
Cole Faustf0632662022-04-07 13:59:24 -07001411 expr: NewVariableRefExpr(ctx.addVariable("TARGET_BOARD_PLATFORM")),
1412 list: NewVariableRefExpr(ctx.addVariable(args.Dump() + "_BOARD_PLATFORMS")),
Cole Faust9ebf6e42021-12-13 14:08:34 -08001413 isNot: false,
1414 }
1415}
1416
1417type isVendorBoardQcomCallParser struct{}
1418
1419func (p *isVendorBoardQcomCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
1420 if !args.Empty() {
1421 return ctx.newBadExpr(node, "is-vendor-board-qcom does not accept any arguments")
1422 }
1423 return &inExpr{
Cole Faustf0632662022-04-07 13:59:24 -07001424 expr: NewVariableRefExpr(ctx.addVariable("TARGET_BOARD_PLATFORM")),
1425 list: NewVariableRefExpr(ctx.addVariable("QCOM_BOARD_PLATFORMS")),
Cole Faust9ebf6e42021-12-13 14:08:34 -08001426 isNot: false,
1427 }
1428}
1429
1430type substCallParser struct {
1431 fname string
1432}
1433
1434func (p *substCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001435 words := args.Split(",")
1436 if len(words) != 3 {
Cole Faust9ebf6e42021-12-13 14:08:34 -08001437 return ctx.newBadExpr(node, "%s function should have 3 arguments", p.fname)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001438 }
Sasha Smundak35434ed2021-11-05 16:29:56 -07001439 from := ctx.parseMakeString(node, words[0])
1440 if xBad, ok := from.(*badExpr); ok {
1441 return xBad
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001442 }
Sasha Smundak35434ed2021-11-05 16:29:56 -07001443 to := ctx.parseMakeString(node, words[1])
1444 if xBad, ok := to.(*badExpr); ok {
1445 return xBad
1446 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001447 words[2].TrimLeftSpaces()
1448 words[2].TrimRightSpaces()
1449 obj := ctx.parseMakeString(node, words[2])
Sasha Smundak9d011ab2021-07-09 16:00:57 -07001450 typ := obj.typ()
Cole Faust9ebf6e42021-12-13 14:08:34 -08001451 if typ == starlarkTypeString && p.fname == "subst" {
Sasha Smundak94b41c72021-07-12 18:30:42 -07001452 // Optimization: if it's $(subst from, to, string), emit string.replace(from, to)
Sasha Smundak9d011ab2021-07-09 16:00:57 -07001453 return &callExpr{
1454 object: obj,
1455 name: "replace",
Sasha Smundak35434ed2021-11-05 16:29:56 -07001456 args: []starlarkExpr{from, to},
Sasha Smundak9d011ab2021-07-09 16:00:57 -07001457 returnType: typ,
1458 }
1459 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001460 return &callExpr{
Cole Faust9ebf6e42021-12-13 14:08:34 -08001461 name: baseName + ".mk" + p.fname,
Sasha Smundak35434ed2021-11-05 16:29:56 -07001462 args: []starlarkExpr{from, to, obj},
Sasha Smundak9d011ab2021-07-09 16:00:57 -07001463 returnType: obj.typ(),
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001464 }
1465}
1466
Cole Faust9ebf6e42021-12-13 14:08:34 -08001467type ifCallParser struct{}
1468
1469func (p *ifCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
Cole Faust4eadba72021-12-07 11:54:52 -08001470 words := args.Split(",")
1471 if len(words) != 2 && len(words) != 3 {
1472 return ctx.newBadExpr(node, "if function should have 2 or 3 arguments, found "+strconv.Itoa(len(words)))
1473 }
1474 condition := ctx.parseMakeString(node, words[0])
1475 ifTrue := ctx.parseMakeString(node, words[1])
1476 var ifFalse starlarkExpr
1477 if len(words) == 3 {
1478 ifFalse = ctx.parseMakeString(node, words[2])
1479 } else {
1480 switch ifTrue.typ() {
1481 case starlarkTypeList:
1482 ifFalse = &listExpr{items: []starlarkExpr{}}
1483 case starlarkTypeInt:
1484 ifFalse = &intLiteralExpr{literal: 0}
1485 case starlarkTypeBool:
1486 ifFalse = &boolLiteralExpr{literal: false}
1487 default:
1488 ifFalse = &stringLiteralExpr{literal: ""}
1489 }
1490 }
1491 return &ifExpr{
1492 condition,
1493 ifTrue,
1494 ifFalse,
1495 }
1496}
1497
Cole Faustf035d402022-03-28 14:02:50 -07001498type ifCallNodeParser struct{}
Cole Faust9ebf6e42021-12-13 14:08:34 -08001499
Cole Faustf035d402022-03-28 14:02:50 -07001500func (p *ifCallNodeParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) []starlarkNode {
1501 words := args.Split(",")
1502 if len(words) != 2 && len(words) != 3 {
1503 return []starlarkNode{ctx.newBadNode(node, "if function should have 2 or 3 arguments, found "+strconv.Itoa(len(words)))}
1504 }
1505
1506 ifn := &ifNode{expr: ctx.parseMakeString(node, words[0])}
1507 cases := []*switchCase{
1508 {
1509 gate: ifn,
1510 nodes: ctx.parseNodeMakeString(node, words[1]),
1511 },
1512 }
1513 if len(words) == 3 {
1514 cases = append(cases, &switchCase{
1515 gate: &elseNode{},
1516 nodes: ctx.parseNodeMakeString(node, words[2]),
1517 })
1518 }
1519 if len(cases) == 2 {
1520 if len(cases[1].nodes) == 0 {
1521 // Remove else branch if it has no contents
1522 cases = cases[:1]
1523 } else if len(cases[0].nodes) == 0 {
1524 // If the if branch has no contents but the else does,
1525 // move them to the if and negate its condition
1526 ifn.expr = negateExpr(ifn.expr)
1527 cases[0].nodes = cases[1].nodes
1528 cases = cases[:1]
1529 }
1530 }
1531
1532 return []starlarkNode{&switchNode{ssCases: cases}}
1533}
1534
1535type foreachCallParser struct{}
1536
1537func (p *foreachCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
Cole Faustb0d32ab2021-12-09 14:00:59 -08001538 words := args.Split(",")
1539 if len(words) != 3 {
1540 return ctx.newBadExpr(node, "foreach function should have 3 arguments, found "+strconv.Itoa(len(words)))
1541 }
1542 if !words[0].Const() || words[0].Empty() || !identifierFullMatchRegex.MatchString(words[0].Strings[0]) {
1543 return ctx.newBadExpr(node, "first argument to foreach function must be a simple string identifier")
1544 }
1545 loopVarName := words[0].Strings[0]
1546 list := ctx.parseMakeString(node, words[1])
1547 action := ctx.parseMakeString(node, words[2]).transform(func(expr starlarkExpr) starlarkExpr {
1548 if varRefExpr, ok := expr.(*variableRefExpr); ok && varRefExpr.ref.name() == loopVarName {
1549 return &identifierExpr{loopVarName}
1550 }
1551 return nil
1552 })
1553
1554 if list.typ() != starlarkTypeList {
1555 list = &callExpr{
Cole Faust9ebf6e42021-12-13 14:08:34 -08001556 name: baseName + ".words",
1557 returnType: starlarkTypeList,
Cole Faustb0d32ab2021-12-09 14:00:59 -08001558 args: []starlarkExpr{list},
1559 }
1560 }
1561
1562 return &foreachExpr{
1563 varName: loopVarName,
1564 list: list,
1565 action: action,
1566 }
1567}
1568
Cole Faustf035d402022-03-28 14:02:50 -07001569func transformNode(node starlarkNode, transformer func(expr starlarkExpr) starlarkExpr) {
1570 switch a := node.(type) {
1571 case *ifNode:
1572 a.expr = a.expr.transform(transformer)
1573 case *switchCase:
1574 transformNode(a.gate, transformer)
1575 for _, n := range a.nodes {
1576 transformNode(n, transformer)
1577 }
1578 case *switchNode:
1579 for _, n := range a.ssCases {
1580 transformNode(n, transformer)
1581 }
1582 case *exprNode:
1583 a.expr = a.expr.transform(transformer)
1584 case *assignmentNode:
1585 a.value = a.value.transform(transformer)
1586 case *foreachNode:
1587 a.list = a.list.transform(transformer)
1588 for _, n := range a.actions {
1589 transformNode(n, transformer)
1590 }
1591 }
1592}
1593
1594type foreachCallNodeParser struct{}
1595
1596func (p *foreachCallNodeParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) []starlarkNode {
1597 words := args.Split(",")
1598 if len(words) != 3 {
1599 return []starlarkNode{ctx.newBadNode(node, "foreach function should have 3 arguments, found "+strconv.Itoa(len(words)))}
1600 }
1601 if !words[0].Const() || words[0].Empty() || !identifierFullMatchRegex.MatchString(words[0].Strings[0]) {
1602 return []starlarkNode{ctx.newBadNode(node, "first argument to foreach function must be a simple string identifier")}
1603 }
1604
1605 loopVarName := words[0].Strings[0]
1606
1607 list := ctx.parseMakeString(node, words[1])
1608 if list.typ() != starlarkTypeList {
1609 list = &callExpr{
1610 name: baseName + ".words",
1611 returnType: starlarkTypeList,
1612 args: []starlarkExpr{list},
1613 }
1614 }
1615
1616 actions := ctx.parseNodeMakeString(node, words[2])
1617 // TODO(colefaust): Replace transforming code with something more elegant
1618 for _, action := range actions {
1619 transformNode(action, func(expr starlarkExpr) starlarkExpr {
1620 if varRefExpr, ok := expr.(*variableRefExpr); ok && varRefExpr.ref.name() == loopVarName {
1621 return &identifierExpr{loopVarName}
1622 }
1623 return nil
1624 })
1625 }
1626
1627 return []starlarkNode{&foreachNode{
1628 varName: loopVarName,
1629 list: list,
1630 actions: actions,
1631 }}
1632}
1633
Cole Faust9ebf6e42021-12-13 14:08:34 -08001634type wordCallParser struct{}
1635
1636func (p *wordCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001637 words := args.Split(",")
1638 if len(words) != 2 {
1639 return ctx.newBadExpr(node, "word function should have 2 arguments")
1640 }
Cole Faust94c4a9a2022-04-22 17:43:52 -07001641 var index = 0
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001642 if words[0].Const() {
Cole Faust94c4a9a2022-04-22 17:43:52 -07001643 if i, err := strconv.Atoi(strings.TrimSpace(words[0].Strings[0])); err == nil {
1644 index = i
1645 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001646 }
1647 if index < 1 {
1648 return ctx.newBadExpr(node, "word index should be constant positive integer")
1649 }
1650 words[1].TrimLeftSpaces()
1651 words[1].TrimRightSpaces()
1652 array := ctx.parseMakeString(node, words[1])
Cole Faust94c4a9a2022-04-22 17:43:52 -07001653 if bad, ok := array.(*badExpr); ok {
1654 return bad
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001655 }
1656 if array.typ() != starlarkTypeList {
Cole Faust94c4a9a2022-04-22 17:43:52 -07001657 array = &callExpr{
1658 name: baseName + ".words",
1659 args: []starlarkExpr{array},
1660 returnType: starlarkTypeList,
1661 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001662 }
Cole Faust94c4a9a2022-04-22 17:43:52 -07001663 return &indexExpr{array, &intLiteralExpr{index - 1}}
1664}
1665
1666type wordsCallParser struct{}
1667
1668func (p *wordsCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
1669 args.TrimLeftSpaces()
1670 args.TrimRightSpaces()
1671 array := ctx.parseMakeString(node, args)
1672 if bad, ok := array.(*badExpr); ok {
1673 return bad
1674 }
1675 if array.typ() != starlarkTypeList {
1676 array = &callExpr{
1677 name: baseName + ".words",
1678 args: []starlarkExpr{array},
1679 returnType: starlarkTypeList,
1680 }
1681 }
1682 return &callExpr{
1683 name: "len",
1684 args: []starlarkExpr{array},
1685 returnType: starlarkTypeInt,
1686 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001687}
1688
Cole Faust9ebf6e42021-12-13 14:08:34 -08001689type firstOrLastwordCallParser struct {
1690 isLastWord bool
1691}
1692
1693func (p *firstOrLastwordCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
Sasha Smundak16e07732021-07-23 11:38:23 -07001694 arg := ctx.parseMakeString(node, args)
1695 if bad, ok := arg.(*badExpr); ok {
1696 return bad
1697 }
1698 index := &intLiteralExpr{0}
Cole Faust9ebf6e42021-12-13 14:08:34 -08001699 if p.isLastWord {
Sasha Smundak16e07732021-07-23 11:38:23 -07001700 if v, ok := arg.(*variableRefExpr); ok && v.ref.name() == "MAKEFILE_LIST" {
1701 return &stringLiteralExpr{ctx.script.mkFile}
1702 }
1703 index.literal = -1
1704 }
1705 if arg.typ() == starlarkTypeList {
1706 return &indexExpr{arg, index}
1707 }
1708 return &indexExpr{&callExpr{object: arg, name: "split", returnType: starlarkTypeList}, index}
1709}
1710
Cole Faustb1103e22022-01-06 15:22:05 -08001711func parseIntegerArguments(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString, expectedArgs int) ([]starlarkExpr, error) {
1712 parsedArgs := make([]starlarkExpr, 0)
1713 for _, arg := range args.Split(",") {
1714 expr := ctx.parseMakeString(node, arg)
1715 if expr.typ() == starlarkTypeList {
1716 return nil, fmt.Errorf("argument to math argument has type list, which cannot be converted to int")
1717 }
1718 if s, ok := maybeString(expr); ok {
1719 intVal, err := strconv.Atoi(strings.TrimSpace(s))
1720 if err != nil {
1721 return nil, err
1722 }
1723 expr = &intLiteralExpr{literal: intVal}
1724 } else if expr.typ() != starlarkTypeInt {
1725 expr = &callExpr{
1726 name: "int",
1727 args: []starlarkExpr{expr},
1728 returnType: starlarkTypeInt,
1729 }
1730 }
1731 parsedArgs = append(parsedArgs, expr)
1732 }
1733 if len(parsedArgs) != expectedArgs {
1734 return nil, fmt.Errorf("function should have %d arguments", expectedArgs)
1735 }
1736 return parsedArgs, nil
1737}
1738
1739type mathComparisonCallParser struct {
1740 op string
1741}
1742
1743func (p *mathComparisonCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
1744 parsedArgs, err := parseIntegerArguments(ctx, node, args, 2)
1745 if err != nil {
1746 return ctx.newBadExpr(node, err.Error())
1747 }
1748 return &binaryOpExpr{
1749 left: parsedArgs[0],
1750 right: parsedArgs[1],
1751 op: p.op,
1752 returnType: starlarkTypeBool,
1753 }
1754}
1755
1756type mathMaxOrMinCallParser struct {
1757 function string
1758}
1759
1760func (p *mathMaxOrMinCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
1761 parsedArgs, err := parseIntegerArguments(ctx, node, args, 2)
1762 if err != nil {
1763 return ctx.newBadExpr(node, err.Error())
1764 }
1765 return &callExpr{
1766 object: nil,
1767 name: p.function,
1768 args: parsedArgs,
1769 returnType: starlarkTypeInt,
1770 }
1771}
1772
Cole Faustf035d402022-03-28 14:02:50 -07001773type evalNodeParser struct{}
1774
1775func (p *evalNodeParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) []starlarkNode {
1776 parser := mkparser.NewParser("Eval expression", strings.NewReader(args.Dump()))
1777 nodes, errs := parser.Parse()
1778 if errs != nil {
1779 return []starlarkNode{ctx.newBadNode(node, "Unable to parse eval statement")}
1780 }
1781
1782 if len(nodes) == 0 {
1783 return []starlarkNode{}
1784 } else if len(nodes) == 1 {
1785 switch n := nodes[0].(type) {
1786 case *mkparser.Assignment:
1787 if n.Name.Const() {
1788 return ctx.handleAssignment(n)
1789 }
1790 case *mkparser.Comment:
1791 return []starlarkNode{&commentNode{strings.TrimSpace("#" + n.Comment)}}
1792 }
1793 }
1794
1795 return []starlarkNode{ctx.newBadNode(node, "Eval expression too complex; only assignments and comments are supported")}
1796}
1797
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001798func (ctx *parseContext) parseMakeString(node mkparser.Node, mk *mkparser.MakeString) starlarkExpr {
1799 if mk.Const() {
1800 return &stringLiteralExpr{mk.Dump()}
1801 }
1802 if mkRef, ok := mk.SingleVariable(); ok {
1803 return ctx.parseReference(node, mkRef)
1804 }
1805 // If we reached here, it's neither string literal nor a simple variable,
1806 // we need a full-blown interpolation node that will generate
1807 // "a%b%c" % (X, Y) for a$(X)b$(Y)c
Cole Faustfc438682021-12-14 12:46:32 -08001808 parts := make([]starlarkExpr, len(mk.Variables)+len(mk.Strings))
1809 for i := 0; i < len(parts); i++ {
1810 if i%2 == 0 {
1811 parts[i] = &stringLiteralExpr{literal: mk.Strings[i/2]}
1812 } else {
1813 parts[i] = ctx.parseReference(node, mk.Variables[i/2].Name)
1814 if x, ok := parts[i].(*badExpr); ok {
1815 return x
1816 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001817 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001818 }
Cole Faustfc438682021-12-14 12:46:32 -08001819 return NewInterpolateExpr(parts)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001820}
1821
Cole Faustf035d402022-03-28 14:02:50 -07001822func (ctx *parseContext) parseNodeMakeString(node mkparser.Node, mk *mkparser.MakeString) []starlarkNode {
1823 // Discard any constant values in the make string, as they would be top level
1824 // string literals and do nothing.
1825 result := make([]starlarkNode, 0, len(mk.Variables))
1826 for i := range mk.Variables {
1827 result = append(result, ctx.handleVariable(&mk.Variables[i])...)
1828 }
1829 return result
1830}
1831
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001832// Handles the statements whose treatment is the same in all contexts: comment,
1833// assignment, variable (which is a macro call in reality) and all constructs that
1834// do not handle in any context ('define directive and any unrecognized stuff).
Cole Faustdd569ae2022-01-31 15:48:29 -08001835func (ctx *parseContext) handleSimpleStatement(node mkparser.Node) []starlarkNode {
1836 var result []starlarkNode
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001837 switch x := node.(type) {
1838 case *mkparser.Comment:
Cole Faustdd569ae2022-01-31 15:48:29 -08001839 if n, handled := ctx.maybeHandleAnnotation(x); handled && n != nil {
1840 result = []starlarkNode{n}
1841 } else if !handled {
1842 result = []starlarkNode{&commentNode{strings.TrimSpace("#" + x.Comment)}}
Cole Faust7940c6a2022-01-31 15:54:05 -08001843 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001844 case *mkparser.Assignment:
Cole Faustdd569ae2022-01-31 15:48:29 -08001845 result = ctx.handleAssignment(x)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001846 case *mkparser.Variable:
Cole Faustdd569ae2022-01-31 15:48:29 -08001847 result = ctx.handleVariable(x)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001848 case *mkparser.Directive:
1849 switch x.Name {
1850 case "define":
Cole Faustdd569ae2022-01-31 15:48:29 -08001851 if res := ctx.maybeHandleDefine(x); res != nil {
1852 result = []starlarkNode{res}
1853 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001854 case "include", "-include":
Cole Faustdd569ae2022-01-31 15:48:29 -08001855 result = ctx.handleInclude(node, ctx.parseMakeString(node, x.Args), x.Name[0] != '-')
Cole Faust591a1fe2021-11-08 15:37:57 -08001856 case "ifeq", "ifneq", "ifdef", "ifndef":
Cole Faustdd569ae2022-01-31 15:48:29 -08001857 result = []starlarkNode{ctx.handleIfBlock(x)}
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001858 default:
Cole Faustdd569ae2022-01-31 15:48:29 -08001859 result = []starlarkNode{ctx.newBadNode(x, "unexpected directive %s", x.Name)}
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001860 }
1861 default:
Cole Faustdd569ae2022-01-31 15:48:29 -08001862 result = []starlarkNode{ctx.newBadNode(x, "unsupported line %s", strings.ReplaceAll(x.Dump(), "\n", "\n#"))}
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001863 }
Cole Faust6c934f62022-01-06 15:51:12 -08001864
1865 // Clear the includeTops after each non-comment statement
1866 // so that include annotations placed on certain statements don't apply
1867 // globally for the rest of the makefile was well.
Cole Faustf92c9f22022-03-14 14:35:50 -07001868 if _, wasComment := node.(*mkparser.Comment); !wasComment {
1869 ctx.atTopOfMakefile = false
Cole Faust6c934f62022-01-06 15:51:12 -08001870 ctx.includeTops = []string{}
1871 }
Cole Faustdd569ae2022-01-31 15:48:29 -08001872
1873 if result == nil {
1874 result = []starlarkNode{}
1875 }
Cole Faustf035d402022-03-28 14:02:50 -07001876
Cole Faustdd569ae2022-01-31 15:48:29 -08001877 return result
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001878}
1879
Cole Faustf92c9f22022-03-14 14:35:50 -07001880// The types allowed in a type_hint
1881var typeHintMap = map[string]starlarkType{
1882 "string": starlarkTypeString,
1883 "list": starlarkTypeList,
1884}
1885
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001886// Processes annotation. An annotation is a comment that starts with #RBC# and provides
1887// a conversion hint -- say, where to look for the dynamically calculated inherit/include
Cole Faust7940c6a2022-01-31 15:54:05 -08001888// paths. Returns true if the comment was a successfully-handled annotation.
Cole Faustdd569ae2022-01-31 15:48:29 -08001889func (ctx *parseContext) maybeHandleAnnotation(cnode *mkparser.Comment) (starlarkNode, bool) {
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001890 maybeTrim := func(s, prefix string) (string, bool) {
1891 if strings.HasPrefix(s, prefix) {
1892 return strings.TrimSpace(strings.TrimPrefix(s, prefix)), true
1893 }
1894 return s, false
1895 }
1896 annotation, ok := maybeTrim(cnode.Comment, annotationCommentPrefix)
1897 if !ok {
Cole Faustdd569ae2022-01-31 15:48:29 -08001898 return nil, false
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001899 }
1900 if p, ok := maybeTrim(annotation, "include_top"); ok {
Cole Faustf7ed5342021-12-21 14:15:12 -08001901 // Don't allow duplicate include tops, because then we will generate
1902 // invalid starlark code. (duplicate keys in the _entry dictionary)
1903 for _, top := range ctx.includeTops {
1904 if top == p {
Cole Faustdd569ae2022-01-31 15:48:29 -08001905 return nil, true
Cole Faustf7ed5342021-12-21 14:15:12 -08001906 }
1907 }
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001908 ctx.includeTops = append(ctx.includeTops, p)
Cole Faustdd569ae2022-01-31 15:48:29 -08001909 return nil, true
Cole Faustf92c9f22022-03-14 14:35:50 -07001910 } else if p, ok := maybeTrim(annotation, "type_hint"); ok {
1911 // Type hints must come at the beginning the file, to avoid confusion
1912 // if a type hint was specified later and thus only takes effect for half
1913 // of the file.
1914 if !ctx.atTopOfMakefile {
1915 return ctx.newBadNode(cnode, "type_hint annotations must come before the first Makefile statement"), true
1916 }
1917
1918 parts := strings.Fields(p)
1919 if len(parts) <= 1 {
1920 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
1921 }
1922
1923 var varType starlarkType
1924 if varType, ok = typeHintMap[parts[0]]; !ok {
1925 varType = starlarkTypeUnknown
1926 }
1927 if varType == starlarkTypeUnknown {
1928 return ctx.newBadNode(cnode, "Invalid type_hint annotation. Only list/string types are accepted, found %s", parts[0]), true
1929 }
1930
1931 for _, name := range parts[1:] {
1932 // Don't allow duplicate type hints
1933 if _, ok := ctx.typeHints[name]; ok {
1934 return ctx.newBadNode(cnode, "Duplicate type hint for variable %s", name), true
1935 }
1936 ctx.typeHints[name] = varType
1937 }
1938 return nil, true
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001939 }
Cole Faustdd569ae2022-01-31 15:48:29 -08001940 return ctx.newBadNode(cnode, "unsupported annotation %s", cnode.Comment), true
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001941}
1942
1943func (ctx *parseContext) loadedModulePath(path string) string {
1944 // During the transition to Roboleaf some of the product configuration files
1945 // will be converted and checked in while the others will be generated on the fly
1946 // and run. The runner (rbcrun application) accommodates this by allowing three
1947 // different ways to specify the loaded file location:
1948 // 1) load(":<file>",...) loads <file> from the same directory
1949 // 2) load("//path/relative/to/source/root:<file>", ...) loads <file> source tree
1950 // 3) load("/absolute/path/to/<file> absolute path
1951 // If the file being generated and the file it wants to load are in the same directory,
1952 // generate option 1.
1953 // Otherwise, if output directory is not specified, generate 2)
1954 // Finally, if output directory has been specified and the file being generated and
1955 // the file it wants to load from are in the different directories, generate 2) or 3):
1956 // * if the file being loaded exists in the source tree, generate 2)
1957 // * otherwise, generate 3)
1958 // Finally, figure out the loaded module path and name and create a node for it
1959 loadedModuleDir := filepath.Dir(path)
1960 base := filepath.Base(path)
1961 loadedModuleName := strings.TrimSuffix(base, filepath.Ext(base)) + ctx.outputSuffix
1962 if loadedModuleDir == filepath.Dir(ctx.script.mkFile) {
1963 return ":" + loadedModuleName
1964 }
1965 if ctx.outputDir == "" {
1966 return fmt.Sprintf("//%s:%s", loadedModuleDir, loadedModuleName)
1967 }
1968 if _, err := os.Stat(filepath.Join(loadedModuleDir, loadedModuleName)); err == nil {
1969 return fmt.Sprintf("//%s:%s", loadedModuleDir, loadedModuleName)
1970 }
1971 return filepath.Join(ctx.outputDir, loadedModuleDir, loadedModuleName)
1972}
1973
Sasha Smundak3deb9682021-07-26 18:42:25 -07001974func (ctx *parseContext) addSoongNamespace(ns string) {
1975 if _, ok := ctx.soongNamespaces[ns]; ok {
1976 return
1977 }
1978 ctx.soongNamespaces[ns] = make(map[string]bool)
1979}
1980
1981func (ctx *parseContext) hasSoongNamespace(name string) bool {
1982 _, ok := ctx.soongNamespaces[name]
1983 return ok
1984}
1985
1986func (ctx *parseContext) updateSoongNamespace(replace bool, namespaceName string, varNames []string) {
1987 ctx.addSoongNamespace(namespaceName)
1988 vars := ctx.soongNamespaces[namespaceName]
1989 if replace {
1990 vars = make(map[string]bool)
1991 ctx.soongNamespaces[namespaceName] = vars
1992 }
1993 for _, v := range varNames {
1994 vars[v] = true
1995 }
1996}
1997
1998func (ctx *parseContext) hasNamespaceVar(namespaceName string, varName string) bool {
1999 vars, ok := ctx.soongNamespaces[namespaceName]
2000 if ok {
2001 _, ok = vars[varName]
2002 }
2003 return ok
2004}
2005
Sasha Smundak422b6142021-11-11 18:31:59 -08002006func (ctx *parseContext) errorLocation(node mkparser.Node) ErrorLocation {
2007 return ErrorLocation{ctx.script.mkFile, ctx.script.nodeLocator(node.Pos())}
2008}
2009
Sasha Smundakb051c4e2020-11-05 20:45:07 -08002010func (ss *StarlarkScript) String() string {
2011 return NewGenerateContext(ss).emit()
2012}
2013
2014func (ss *StarlarkScript) SubConfigFiles() []string {
Sasha Smundak6609ba72021-07-22 18:32:56 -07002015
Sasha Smundakb051c4e2020-11-05 20:45:07 -08002016 var subs []string
2017 for _, src := range ss.inherited {
2018 subs = append(subs, src.originalPath)
2019 }
2020 return subs
2021}
2022
2023func (ss *StarlarkScript) HasErrors() bool {
2024 return ss.hasErrors
2025}
2026
2027// Convert reads and parses a makefile. If successful, parsed tree
2028// is returned and then can be passed to String() to get the generated
2029// Starlark file.
2030func Convert(req Request) (*StarlarkScript, error) {
2031 reader := req.Reader
2032 if reader == nil {
2033 mkContents, err := ioutil.ReadFile(req.MkFile)
2034 if err != nil {
2035 return nil, err
2036 }
2037 reader = bytes.NewBuffer(mkContents)
2038 }
2039 parser := mkparser.NewParser(req.MkFile, reader)
2040 nodes, errs := parser.Parse()
2041 if len(errs) > 0 {
2042 for _, e := range errs {
2043 fmt.Fprintln(os.Stderr, "ERROR:", e)
2044 }
2045 return nil, fmt.Errorf("bad makefile %s", req.MkFile)
2046 }
2047 starScript := &StarlarkScript{
Sasha Smundak422b6142021-11-11 18:31:59 -08002048 moduleName: moduleNameForFile(req.MkFile),
2049 mkFile: req.MkFile,
Sasha Smundak422b6142021-11-11 18:31:59 -08002050 traceCalls: req.TraceCalls,
2051 sourceFS: req.SourceFS,
2052 makefileFinder: req.MakefileFinder,
2053 nodeLocator: func(pos mkparser.Pos) int { return parser.Unpack(pos).Line },
Cole Faustdd569ae2022-01-31 15:48:29 -08002054 nodes: make([]starlarkNode, 0),
Sasha Smundakb051c4e2020-11-05 20:45:07 -08002055 }
2056 ctx := newParseContext(starScript, nodes)
2057 ctx.outputSuffix = req.OutputSuffix
2058 ctx.outputDir = req.OutputDir
2059 ctx.errorLogger = req.ErrorLogger
2060 if len(req.TracedVariables) > 0 {
2061 ctx.tracedVariables = make(map[string]bool)
2062 for _, v := range req.TracedVariables {
2063 ctx.tracedVariables[v] = true
2064 }
2065 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08002066 for ctx.hasNodes() && ctx.fatalError == nil {
Cole Faustdd569ae2022-01-31 15:48:29 -08002067 starScript.nodes = append(starScript.nodes, ctx.handleSimpleStatement(ctx.getNode())...)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08002068 }
2069 if ctx.fatalError != nil {
2070 return nil, ctx.fatalError
2071 }
2072 return starScript, nil
2073}
2074
Cole Faust864028a2021-12-01 13:43:17 -08002075func Launcher(mainModuleUri, inputVariablesUri, mainModuleName string) string {
Sasha Smundakb051c4e2020-11-05 20:45:07 -08002076 var buf bytes.Buffer
2077 fmt.Fprintf(&buf, "load(%q, %q)\n", baseUri, baseName)
Cole Faust864028a2021-12-01 13:43:17 -08002078 fmt.Fprintf(&buf, "load(%q, input_variables_init = \"init\")\n", inputVariablesUri)
Sasha Smundakd7d07ad2021-09-10 15:42:34 -07002079 fmt.Fprintf(&buf, "load(%q, \"init\")\n", mainModuleUri)
Cole Faust864028a2021-12-01 13:43:17 -08002080 fmt.Fprintf(&buf, "%s(%s(%q, init, input_variables_init))\n", cfnPrintVars, cfnMain, mainModuleName)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08002081 return buf.String()
2082}
2083
Cole Faust6ed7cb42021-10-07 17:08:46 -07002084func BoardLauncher(mainModuleUri string, inputVariablesUri string) string {
2085 var buf bytes.Buffer
2086 fmt.Fprintf(&buf, "load(%q, %q)\n", baseUri, baseName)
2087 fmt.Fprintf(&buf, "load(%q, \"init\")\n", mainModuleUri)
2088 fmt.Fprintf(&buf, "load(%q, input_variables_init = \"init\")\n", inputVariablesUri)
Cole Fausta0604662022-02-28 11:53:58 -08002089 fmt.Fprintf(&buf, "%s(%s(init, input_variables_init))\n", cfnPrintVars, cfnBoardMain)
Cole Faust6ed7cb42021-10-07 17:08:46 -07002090 return buf.String()
2091}
2092
Sasha Smundakb051c4e2020-11-05 20:45:07 -08002093func MakePath2ModuleName(mkPath string) string {
2094 return strings.TrimSuffix(mkPath, filepath.Ext(mkPath))
2095}