blob: e317cadff824464b02c6056af4fe1f9915ee0fe7 [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"
35 "strconv"
36 "strings"
37 "text/scanner"
38
39 mkparser "android/soong/androidmk/parser"
40)
41
42const (
Sasha Smundak6d852dd2021-09-27 20:34:39 -070043 annotationCommentPrefix = "RBC#"
44 baseUri = "//build/make/core:product_config.rbc"
Sasha Smundakb051c4e2020-11-05 20:45:07 -080045 // The name of the struct exported by the product_config.rbc
46 // that contains the functions and variables available to
47 // product configuration Starlark files.
48 baseName = "rblf"
49
Sasha Smundak65b547e2021-09-17 15:35:41 -070050 soongNsPrefix = "SOONG_CONFIG_"
51
Sasha Smundakb051c4e2020-11-05 20:45:07 -080052 // And here are the functions and variables:
53 cfnGetCfg = baseName + ".cfg"
54 cfnMain = baseName + ".product_configuration"
Cole Faust6ed7cb42021-10-07 17:08:46 -070055 cfnBoardMain = baseName + ".board_configuration"
Sasha Smundakb051c4e2020-11-05 20:45:07 -080056 cfnPrintVars = baseName + ".printvars"
57 cfnWarning = baseName + ".warning"
58 cfnLocalAppend = baseName + ".local_append"
59 cfnLocalSetDefault = baseName + ".local_set_default"
60 cfnInherit = baseName + ".inherit"
61 cfnSetListDefault = baseName + ".setdefault"
62)
63
64const (
Cole Faust9ebf6e42021-12-13 14:08:34 -080065 soongConfigAppend = "soong_config_append"
66 soongConfigAssign = "soong_config_set"
Sasha Smundakb051c4e2020-11-05 20:45:07 -080067)
68
Cole Faust9ebf6e42021-12-13 14:08:34 -080069var knownFunctions = map[string]interface {
70 parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr
Sasha Smundakb051c4e2020-11-05 20:45:07 -080071}{
Cole Faust9ebf6e42021-12-13 14:08:34 -080072 "abspath": &simpleCallParser{name: baseName + ".abspath", returnType: starlarkTypeString, addGlobals: false},
73 "add_soong_config_namespace": &simpleCallParser{name: baseName + ".soong_config_namespace", returnType: starlarkTypeVoid, addGlobals: true},
74 "add_soong_config_var_value": &simpleCallParser{name: baseName + ".soong_config_set", returnType: starlarkTypeVoid, addGlobals: true},
75 soongConfigAssign: &simpleCallParser{name: baseName + ".soong_config_set", returnType: starlarkTypeVoid, addGlobals: true},
76 soongConfigAppend: &simpleCallParser{name: baseName + ".soong_config_append", returnType: starlarkTypeVoid, addGlobals: true},
77 "soong_config_get": &simpleCallParser{name: baseName + ".soong_config_get", returnType: starlarkTypeString, addGlobals: true},
78 "add-to-product-copy-files-if-exists": &simpleCallParser{name: baseName + ".copy_if_exists", returnType: starlarkTypeList, addGlobals: false},
79 "addprefix": &simpleCallParser{name: baseName + ".addprefix", returnType: starlarkTypeList, addGlobals: false},
80 "addsuffix": &simpleCallParser{name: baseName + ".addsuffix", returnType: starlarkTypeList, addGlobals: false},
81 "copy-files": &simpleCallParser{name: baseName + ".copy_files", returnType: starlarkTypeList, addGlobals: false},
82 "dir": &simpleCallParser{name: baseName + ".dir", returnType: starlarkTypeList, addGlobals: false},
83 "dist-for-goals": &simpleCallParser{name: baseName + ".mkdist_for_goals", returnType: starlarkTypeVoid, addGlobals: true},
84 "enforce-product-packages-exist": &simpleCallParser{name: baseName + ".enforce_product_packages_exist", returnType: starlarkTypeVoid, addGlobals: false},
85 "error": &makeControlFuncParser{name: baseName + ".mkerror"},
86 "findstring": &simpleCallParser{name: baseName + ".findstring", returnType: starlarkTypeInt, addGlobals: false},
87 "find-copy-subdir-files": &simpleCallParser{name: baseName + ".find_and_copy", returnType: starlarkTypeList, addGlobals: false},
88 "filter": &simpleCallParser{name: baseName + ".filter", returnType: starlarkTypeList, addGlobals: false},
89 "filter-out": &simpleCallParser{name: baseName + ".filter_out", returnType: starlarkTypeList, addGlobals: false},
90 "firstword": &firstOrLastwordCallParser{isLastWord: false},
91 "foreach": &foreachCallPaser{},
92 "if": &ifCallParser{},
93 "info": &makeControlFuncParser{name: baseName + ".mkinfo"},
Cole Faustb2e0b602022-01-07 15:46:58 -080094 "is-board-platform": &simpleCallParser{name: baseName + ".board_platform_is", returnType: starlarkTypeBool, addGlobals: true},
Cole Faust9ebf6e42021-12-13 14:08:34 -080095 "is-board-platform2": &simpleCallParser{name: baseName + ".board_platform_is", returnType: starlarkTypeBool, addGlobals: true},
Cole Faustb2e0b602022-01-07 15:46:58 -080096 "is-board-platform-in-list": &simpleCallParser{name: baseName + ".board_platform_in", returnType: starlarkTypeBool, addGlobals: true},
Cole Faust9ebf6e42021-12-13 14:08:34 -080097 "is-board-platform-in-list2": &simpleCallParser{name: baseName + ".board_platform_in", returnType: starlarkTypeBool, addGlobals: true},
98 "is-product-in-list": &isProductInListCallParser{},
99 "is-vendor-board-platform": &isVendorBoardPlatformCallParser{},
100 "is-vendor-board-qcom": &isVendorBoardQcomCallParser{},
101 "lastword": &firstOrLastwordCallParser{isLastWord: true},
102 "notdir": &simpleCallParser{name: baseName + ".notdir", returnType: starlarkTypeString, addGlobals: false},
Cole Faustb1103e22022-01-06 15:22:05 -0800103 "math_max": &mathMaxOrMinCallParser{function: "max"},
104 "math_min": &mathMaxOrMinCallParser{function: "min"},
105 "math_gt_or_eq": &mathComparisonCallParser{op: ">="},
106 "math_gt": &mathComparisonCallParser{op: ">"},
107 "math_lt": &mathComparisonCallParser{op: "<"},
Cole Faust9ebf6e42021-12-13 14:08:34 -0800108 "my-dir": &myDirCallParser{},
109 "patsubst": &substCallParser{fname: "patsubst"},
110 "product-copy-files-by-pattern": &simpleCallParser{name: baseName + ".product_copy_files_by_pattern", returnType: starlarkTypeList, addGlobals: false},
111 "require-artifacts-in-path": &simpleCallParser{name: baseName + ".require_artifacts_in_path", returnType: starlarkTypeVoid, addGlobals: false},
112 "require-artifacts-in-path-relaxed": &simpleCallParser{name: baseName + ".require_artifacts_in_path_relaxed", returnType: starlarkTypeVoid, addGlobals: false},
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800113 // TODO(asmundak): remove it once all calls are removed from configuration makefiles. see b/183161002
Cole Faust9ebf6e42021-12-13 14:08:34 -0800114 "shell": &shellCallParser{},
115 "strip": &simpleCallParser{name: baseName + ".mkstrip", returnType: starlarkTypeString, addGlobals: false},
116 "subst": &substCallParser{fname: "subst"},
117 "warning": &makeControlFuncParser{name: baseName + ".mkwarning"},
118 "word": &wordCallParser{},
119 "wildcard": &simpleCallParser{name: baseName + ".expand_wildcard", returnType: starlarkTypeList, addGlobals: false},
120}
121
122// These are functions that we don't implement conversions for, but
123// we allow seeing their definitions in the product config files.
124var ignoredDefines = map[string]bool{
125 "find-word-in-list": true, // internal macro
126 "get-vendor-board-platforms": true, // internal macro, used by is-board-platform, etc.
127 "is-android-codename": true, // unused by product config
128 "is-android-codename-in-list": true, // unused by product config
129 "is-chipset-in-board-platform": true, // unused by product config
130 "is-chipset-prefix-in-board-platform": true, // unused by product config
131 "is-not-board-platform": true, // defined but never used
132 "is-platform-sdk-version-at-least": true, // unused by product config
133 "match-prefix": true, // internal macro
134 "match-word": true, // internal macro
135 "match-word-in-list": true, // internal macro
136 "tb-modules": true, // defined in hardware/amlogic/tb_modules/tb_detect.mk, unused
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800137}
138
Cole Faustb0d32ab2021-12-09 14:00:59 -0800139var identifierFullMatchRegex = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800140
141// Conversion request parameters
142type Request struct {
Sasha Smundak422b6142021-11-11 18:31:59 -0800143 MkFile string // file to convert
144 Reader io.Reader // if set, read input from this stream instead
145 RootDir string // root directory path used to resolve included files
146 OutputSuffix string // generated Starlark files suffix
147 OutputDir string // if set, root of the output hierarchy
148 ErrorLogger ErrorLogger
149 TracedVariables []string // trace assignment to these variables
150 TraceCalls bool
151 SourceFS fs.FS
152 MakefileFinder MakefileFinder
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800153}
154
Sasha Smundak7d934b92021-11-10 12:20:01 -0800155// ErrorLogger prints errors and gathers error statistics.
156// Its NewError function is called on every error encountered during the conversion.
157type ErrorLogger interface {
Sasha Smundak422b6142021-11-11 18:31:59 -0800158 NewError(el ErrorLocation, node mkparser.Node, text string, args ...interface{})
159}
160
161type ErrorLocation struct {
162 MkFile string
163 MkLine int
164}
165
166func (el ErrorLocation) String() string {
167 return fmt.Sprintf("%s:%d", el.MkFile, el.MkLine)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800168}
169
170// Derives module name for a given file. It is base name
171// (file name without suffix), with some characters replaced to make it a Starlark identifier
172func moduleNameForFile(mkFile string) string {
173 base := strings.TrimSuffix(filepath.Base(mkFile), filepath.Ext(mkFile))
174 // TODO(asmundak): what else can be in the product file names?
Sasha Smundak6609ba72021-07-22 18:32:56 -0700175 return strings.NewReplacer("-", "_", ".", "_").Replace(base)
176
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800177}
178
179func cloneMakeString(mkString *mkparser.MakeString) *mkparser.MakeString {
180 r := &mkparser.MakeString{StringPos: mkString.StringPos}
181 r.Strings = append(r.Strings, mkString.Strings...)
182 r.Variables = append(r.Variables, mkString.Variables...)
183 return r
184}
185
186func isMakeControlFunc(s string) bool {
187 return s == "error" || s == "warning" || s == "info"
188}
189
190// Starlark output generation context
191type generationContext struct {
192 buf strings.Builder
193 starScript *StarlarkScript
194 indentLevel int
195 inAssignment bool
196 tracedCount int
197}
198
199func NewGenerateContext(ss *StarlarkScript) *generationContext {
200 return &generationContext{starScript: ss}
201}
202
203// emit returns generated script
204func (gctx *generationContext) emit() string {
205 ss := gctx.starScript
206
207 // The emitted code has the following layout:
208 // <initial comments>
209 // preamble, i.e.,
210 // load statement for the runtime support
211 // load statement for each unique submodule pulled in by this one
212 // def init(g, handle):
213 // cfg = rblf.cfg(handle)
214 // <statements>
215 // <warning if conversion was not clean>
216
217 iNode := len(ss.nodes)
218 for i, node := range ss.nodes {
219 if _, ok := node.(*commentNode); !ok {
220 iNode = i
221 break
222 }
223 node.emit(gctx)
224 }
225
226 gctx.emitPreamble()
227
228 gctx.newLine()
229 // The arguments passed to the init function are the global dictionary
230 // ('g') and the product configuration dictionary ('cfg')
231 gctx.write("def init(g, handle):")
232 gctx.indentLevel++
233 if gctx.starScript.traceCalls {
234 gctx.newLine()
235 gctx.writef(`print(">%s")`, gctx.starScript.mkFile)
236 }
237 gctx.newLine()
238 gctx.writef("cfg = %s(handle)", cfnGetCfg)
239 for _, node := range ss.nodes[iNode:] {
240 node.emit(gctx)
241 }
242
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800243 if gctx.starScript.traceCalls {
244 gctx.newLine()
245 gctx.writef(`print("<%s")`, gctx.starScript.mkFile)
246 }
247 gctx.indentLevel--
248 gctx.write("\n")
249 return gctx.buf.String()
250}
251
252func (gctx *generationContext) emitPreamble() {
253 gctx.newLine()
254 gctx.writef("load(%q, %q)", baseUri, baseName)
255 // Emit exactly one load statement for each URI.
256 loadedSubConfigs := make(map[string]string)
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800257 for _, mi := range gctx.starScript.inherited {
258 uri := mi.path
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800259 if m, ok := loadedSubConfigs[uri]; ok {
260 // No need to emit load statement, but fix module name.
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800261 mi.moduleLocalName = m
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800262 continue
263 }
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800264 if mi.optional || mi.missing {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800265 uri += "|init"
266 }
267 gctx.newLine()
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800268 gctx.writef("load(%q, %s = \"init\")", uri, mi.entryName())
269 loadedSubConfigs[uri] = mi.moduleLocalName
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800270 }
271 gctx.write("\n")
272}
273
274func (gctx *generationContext) emitPass() {
275 gctx.newLine()
276 gctx.write("pass")
277}
278
279func (gctx *generationContext) write(ss ...string) {
280 for _, s := range ss {
281 gctx.buf.WriteString(s)
282 }
283}
284
285func (gctx *generationContext) writef(format string, args ...interface{}) {
286 gctx.write(fmt.Sprintf(format, args...))
287}
288
289func (gctx *generationContext) newLine() {
290 if gctx.buf.Len() == 0 {
291 return
292 }
293 gctx.write("\n")
294 gctx.writef("%*s", 2*gctx.indentLevel, "")
295}
296
Sasha Smundak422b6142021-11-11 18:31:59 -0800297func (gctx *generationContext) emitConversionError(el ErrorLocation, message string) {
298 gctx.writef(`rblf.mk2rbc_error("%s", %q)`, el, message)
299}
300
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800301func (gctx *generationContext) emitLoadCheck(im inheritedModule) {
302 if !im.needsLoadCheck() {
303 return
304 }
305 gctx.newLine()
306 gctx.writef("if not %s:", im.entryName())
307 gctx.indentLevel++
308 gctx.newLine()
309 gctx.write(`rblf.mkerror("`, gctx.starScript.mkFile, `", "Cannot find %s" % (`)
310 im.pathExpr().emit(gctx)
311 gctx.write("))")
312 gctx.indentLevel--
313}
314
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800315type knownVariable struct {
316 name string
317 class varClass
318 valueType starlarkType
319}
320
321type knownVariables map[string]knownVariable
322
323func (pcv knownVariables) NewVariable(name string, varClass varClass, valueType starlarkType) {
324 v, exists := pcv[name]
325 if !exists {
326 pcv[name] = knownVariable{name, varClass, valueType}
327 return
328 }
329 // Conflict resolution:
330 // * config class trumps everything
331 // * any type trumps unknown type
332 match := varClass == v.class
333 if !match {
334 if varClass == VarClassConfig {
335 v.class = VarClassConfig
336 match = true
337 } else if v.class == VarClassConfig {
338 match = true
339 }
340 }
341 if valueType != v.valueType {
342 if valueType != starlarkTypeUnknown {
343 if v.valueType == starlarkTypeUnknown {
344 v.valueType = valueType
345 } else {
346 match = false
347 }
348 }
349 }
350 if !match {
351 fmt.Fprintf(os.Stderr, "cannot redefine %s as %v/%v (already defined as %v/%v)\n",
352 name, varClass, valueType, v.class, v.valueType)
353 }
354}
355
356// All known product variables.
357var KnownVariables = make(knownVariables)
358
359func init() {
360 for _, kv := range []string{
361 // Kernel-related variables that we know are lists.
362 "BOARD_VENDOR_KERNEL_MODULES",
363 "BOARD_VENDOR_RAMDISK_KERNEL_MODULES",
364 "BOARD_VENDOR_RAMDISK_KERNEL_MODULES_LOAD",
365 "BOARD_RECOVERY_KERNEL_MODULES",
366 // Other variables we knwo are lists
367 "ART_APEX_JARS",
368 } {
369 KnownVariables.NewVariable(kv, VarClassSoong, starlarkTypeList)
370 }
371}
372
373type nodeReceiver interface {
374 newNode(node starlarkNode)
375}
376
377// Information about the generated Starlark script.
378type StarlarkScript struct {
Sasha Smundak422b6142021-11-11 18:31:59 -0800379 mkFile string
380 moduleName string
381 mkPos scanner.Position
382 nodes []starlarkNode
383 inherited []*moduleInfo
384 hasErrors bool
385 topDir string
386 traceCalls bool // print enter/exit each init function
387 sourceFS fs.FS
388 makefileFinder MakefileFinder
389 nodeLocator func(pos mkparser.Pos) int
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800390}
391
392func (ss *StarlarkScript) newNode(node starlarkNode) {
393 ss.nodes = append(ss.nodes, node)
394}
395
396// varAssignmentScope points to the last assignment for each variable
397// in the current block. It is used during the parsing to chain
398// the assignments to a variable together.
399type varAssignmentScope struct {
400 outer *varAssignmentScope
401 vars map[string]*assignmentNode
402}
403
404// parseContext holds the script we are generating and all the ephemeral data
405// needed during the parsing.
406type parseContext struct {
407 script *StarlarkScript
408 nodes []mkparser.Node // Makefile as parsed by mkparser
409 currentNodeIndex int // Node in it we are processing
410 ifNestLevel int
411 moduleNameCount map[string]int // count of imported modules with given basename
412 fatalError error
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800413 outputSuffix string
Sasha Smundak7d934b92021-11-10 12:20:01 -0800414 errorLogger ErrorLogger
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800415 tracedVariables map[string]bool // variables to be traced in the generated script
416 variables map[string]variable
417 varAssignments *varAssignmentScope
418 receiver nodeReceiver // receptacle for the generated starlarkNode's
419 receiverStack []nodeReceiver
420 outputDir string
Sasha Smundak6609ba72021-07-22 18:32:56 -0700421 dependentModules map[string]*moduleInfo
Sasha Smundak3deb9682021-07-26 18:42:25 -0700422 soongNamespaces map[string]map[string]bool
Sasha Smundak6d852dd2021-09-27 20:34:39 -0700423 includeTops []string
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800424}
425
426func newParseContext(ss *StarlarkScript, nodes []mkparser.Node) *parseContext {
Sasha Smundak6609ba72021-07-22 18:32:56 -0700427 topdir, _ := filepath.Split(filepath.Join(ss.topDir, "foo"))
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800428 predefined := []struct{ name, value string }{
429 {"SRC_TARGET_DIR", filepath.Join("build", "make", "target")},
430 {"LOCAL_PATH", filepath.Dir(ss.mkFile)},
Sasha Smundak6609ba72021-07-22 18:32:56 -0700431 {"TOPDIR", topdir},
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800432 // TODO(asmundak): maybe read it from build/make/core/envsetup.mk?
433 {"TARGET_COPY_OUT_SYSTEM", "system"},
434 {"TARGET_COPY_OUT_SYSTEM_OTHER", "system_other"},
435 {"TARGET_COPY_OUT_DATA", "data"},
436 {"TARGET_COPY_OUT_ASAN", filepath.Join("data", "asan")},
437 {"TARGET_COPY_OUT_OEM", "oem"},
438 {"TARGET_COPY_OUT_RAMDISK", "ramdisk"},
439 {"TARGET_COPY_OUT_DEBUG_RAMDISK", "debug_ramdisk"},
440 {"TARGET_COPY_OUT_VENDOR_DEBUG_RAMDISK", "vendor_debug_ramdisk"},
441 {"TARGET_COPY_OUT_TEST_HARNESS_RAMDISK", "test_harness_ramdisk"},
442 {"TARGET_COPY_OUT_ROOT", "root"},
443 {"TARGET_COPY_OUT_RECOVERY", "recovery"},
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800444 {"TARGET_COPY_OUT_VENDOR_RAMDISK", "vendor_ramdisk"},
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800445 // TODO(asmundak): to process internal config files, we need the following variables:
446 // BOARD_CONFIG_VENDOR_PATH
447 // TARGET_VENDOR
448 // target_base_product
449 //
450
451 // the following utility variables are set in build/make/common/core.mk:
452 {"empty", ""},
453 {"space", " "},
454 {"comma", ","},
455 {"newline", "\n"},
456 {"pound", "#"},
457 {"backslash", "\\"},
458 }
459 ctx := &parseContext{
460 script: ss,
461 nodes: nodes,
462 currentNodeIndex: 0,
463 ifNestLevel: 0,
464 moduleNameCount: make(map[string]int),
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800465 variables: make(map[string]variable),
Sasha Smundak6609ba72021-07-22 18:32:56 -0700466 dependentModules: make(map[string]*moduleInfo),
Sasha Smundak3deb9682021-07-26 18:42:25 -0700467 soongNamespaces: make(map[string]map[string]bool),
Cole Faust6c934f62022-01-06 15:51:12 -0800468 includeTops: []string{},
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800469 }
470 ctx.pushVarAssignments()
471 for _, item := range predefined {
472 ctx.variables[item.name] = &predefinedVariable{
473 baseVariable: baseVariable{nam: item.name, typ: starlarkTypeString},
474 value: &stringLiteralExpr{item.value},
475 }
476 }
477
478 return ctx
479}
480
481func (ctx *parseContext) lastAssignment(name string) *assignmentNode {
482 for va := ctx.varAssignments; va != nil; va = va.outer {
483 if v, ok := va.vars[name]; ok {
484 return v
485 }
486 }
487 return nil
488}
489
490func (ctx *parseContext) setLastAssignment(name string, asgn *assignmentNode) {
491 ctx.varAssignments.vars[name] = asgn
492}
493
494func (ctx *parseContext) pushVarAssignments() {
495 va := &varAssignmentScope{
496 outer: ctx.varAssignments,
497 vars: make(map[string]*assignmentNode),
498 }
499 ctx.varAssignments = va
500}
501
502func (ctx *parseContext) popVarAssignments() {
503 ctx.varAssignments = ctx.varAssignments.outer
504}
505
506func (ctx *parseContext) pushReceiver(rcv nodeReceiver) {
507 ctx.receiverStack = append(ctx.receiverStack, ctx.receiver)
508 ctx.receiver = rcv
509}
510
511func (ctx *parseContext) popReceiver() {
512 last := len(ctx.receiverStack) - 1
513 if last < 0 {
514 panic(fmt.Errorf("popReceiver: receiver stack empty"))
515 }
516 ctx.receiver = ctx.receiverStack[last]
517 ctx.receiverStack = ctx.receiverStack[0:last]
518}
519
520func (ctx *parseContext) hasNodes() bool {
521 return ctx.currentNodeIndex < len(ctx.nodes)
522}
523
524func (ctx *parseContext) getNode() mkparser.Node {
525 if !ctx.hasNodes() {
526 return nil
527 }
528 node := ctx.nodes[ctx.currentNodeIndex]
529 ctx.currentNodeIndex++
530 return node
531}
532
533func (ctx *parseContext) backNode() {
534 if ctx.currentNodeIndex <= 0 {
535 panic("Cannot back off")
536 }
537 ctx.currentNodeIndex--
538}
539
540func (ctx *parseContext) handleAssignment(a *mkparser.Assignment) {
541 // Handle only simple variables
542 if !a.Name.Const() {
543 ctx.errorf(a, "Only simple variables are handled")
544 return
545 }
546 name := a.Name.Strings[0]
Sasha Smundakea3bc3a2021-11-10 13:06:42 -0800547 // The `override` directive
548 // override FOO :=
549 // is parsed as an assignment to a variable named `override FOO`.
550 // There are very few places where `override` is used, just flag it.
551 if strings.HasPrefix(name, "override ") {
552 ctx.errorf(a, "cannot handle override directive")
553 }
554
Cole Faustc00184e2021-11-08 12:08:57 -0800555 // Soong configuration
Sasha Smundak3deb9682021-07-26 18:42:25 -0700556 if strings.HasPrefix(name, soongNsPrefix) {
557 ctx.handleSoongNsAssignment(strings.TrimPrefix(name, soongNsPrefix), a)
558 return
559 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800560 lhs := ctx.addVariable(name)
561 if lhs == nil {
562 ctx.errorf(a, "unknown variable %s", name)
563 return
564 }
565 _, isTraced := ctx.tracedVariables[name]
Sasha Smundak422b6142021-11-11 18:31:59 -0800566 asgn := &assignmentNode{lhs: lhs, mkValue: a.Value, isTraced: isTraced, location: ctx.errorLocation(a)}
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800567 if lhs.valueType() == starlarkTypeUnknown {
568 // Try to divine variable type from the RHS
569 asgn.value = ctx.parseMakeString(a, a.Value)
570 if xBad, ok := asgn.value.(*badExpr); ok {
571 ctx.wrapBadExpr(xBad)
572 return
573 }
574 inferred_type := asgn.value.typ()
575 if inferred_type != starlarkTypeUnknown {
Sasha Smundak9d011ab2021-07-09 16:00:57 -0700576 lhs.setValueType(inferred_type)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800577 }
578 }
579 if lhs.valueType() == starlarkTypeList {
580 xConcat := ctx.buildConcatExpr(a)
581 if xConcat == nil {
582 return
583 }
584 switch len(xConcat.items) {
585 case 0:
586 asgn.value = &listExpr{}
587 case 1:
588 asgn.value = xConcat.items[0]
589 default:
590 asgn.value = xConcat
591 }
592 } else {
593 asgn.value = ctx.parseMakeString(a, a.Value)
594 if xBad, ok := asgn.value.(*badExpr); ok {
595 ctx.wrapBadExpr(xBad)
596 return
597 }
598 }
599
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800600 asgn.previous = ctx.lastAssignment(name)
601 ctx.setLastAssignment(name, asgn)
602 switch a.Type {
603 case "=", ":=":
604 asgn.flavor = asgnSet
605 case "+=":
606 if asgn.previous == nil && !asgn.lhs.isPreset() {
607 asgn.flavor = asgnMaybeAppend
608 } else {
609 asgn.flavor = asgnAppend
610 }
611 case "?=":
612 asgn.flavor = asgnMaybeSet
613 default:
614 panic(fmt.Errorf("unexpected assignment type %s", a.Type))
615 }
616
617 ctx.receiver.newNode(asgn)
618}
619
Sasha Smundak3deb9682021-07-26 18:42:25 -0700620func (ctx *parseContext) handleSoongNsAssignment(name string, asgn *mkparser.Assignment) {
621 val := ctx.parseMakeString(asgn, asgn.Value)
622 if xBad, ok := val.(*badExpr); ok {
623 ctx.wrapBadExpr(xBad)
624 return
625 }
Sasha Smundak3deb9682021-07-26 18:42:25 -0700626
627 // Unfortunately, Soong namespaces can be set up by directly setting corresponding Make
628 // variables instead of via add_soong_config_namespace + add_soong_config_var_value.
629 // Try to divine the call from the assignment as follows:
630 if name == "NAMESPACES" {
631 // Upon seeng
632 // SOONG_CONFIG_NAMESPACES += foo
633 // remember that there is a namespace `foo` and act as we saw
634 // $(call add_soong_config_namespace,foo)
635 s, ok := maybeString(val)
636 if !ok {
637 ctx.errorf(asgn, "cannot handle variables in SOONG_CONFIG_NAMESPACES assignment, please use add_soong_config_namespace instead")
638 return
639 }
640 for _, ns := range strings.Fields(s) {
641 ctx.addSoongNamespace(ns)
642 ctx.receiver.newNode(&exprNode{&callExpr{
Cole Faust9ebf6e42021-12-13 14:08:34 -0800643 name: baseName + ".soong_config_namespace",
644 args: []starlarkExpr{&globalsExpr{}, &stringLiteralExpr{ns}},
Sasha Smundak3deb9682021-07-26 18:42:25 -0700645 returnType: starlarkTypeVoid,
646 }})
647 }
648 } else {
649 // Upon seeing
650 // SOONG_CONFIG_x_y = v
651 // find a namespace called `x` and act as if we encountered
Cole Faustc00184e2021-11-08 12:08:57 -0800652 // $(call soong_config_set,x,y,v)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700653 // or check that `x_y` is a namespace, and then add the RHS of this assignment as variables in
654 // it.
655 // Emit an error in the ambiguous situation (namespaces `foo_bar` with a variable `baz`
656 // and `foo` with a variable `bar_baz`.
657 namespaceName := ""
658 if ctx.hasSoongNamespace(name) {
659 namespaceName = name
660 }
661 var varName string
662 for pos, ch := range name {
663 if !(ch == '_' && ctx.hasSoongNamespace(name[0:pos])) {
664 continue
665 }
666 if namespaceName != "" {
667 ctx.errorf(asgn, "ambiguous soong namespace (may be either `%s` or `%s`)", namespaceName, name[0:pos])
668 return
669 }
670 namespaceName = name[0:pos]
671 varName = name[pos+1:]
672 }
673 if namespaceName == "" {
674 ctx.errorf(asgn, "cannot figure out Soong namespace, please use add_soong_config_var_value macro instead")
675 return
676 }
677 if varName == "" {
678 // Remember variables in this namespace
679 s, ok := maybeString(val)
680 if !ok {
681 ctx.errorf(asgn, "cannot handle variables in SOONG_CONFIG_ assignment, please use add_soong_config_var_value instead")
682 return
683 }
684 ctx.updateSoongNamespace(asgn.Type != "+=", namespaceName, strings.Fields(s))
685 return
686 }
687
688 // Finally, handle assignment to a namespace variable
689 if !ctx.hasNamespaceVar(namespaceName, varName) {
690 ctx.errorf(asgn, "no %s variable in %s namespace, please use add_soong_config_var_value instead", varName, namespaceName)
691 return
692 }
Cole Faust9ebf6e42021-12-13 14:08:34 -0800693 fname := baseName + "." + soongConfigAssign
Sasha Smundak65b547e2021-09-17 15:35:41 -0700694 if asgn.Type == "+=" {
Cole Faust9ebf6e42021-12-13 14:08:34 -0800695 fname = baseName + "." + soongConfigAppend
Sasha Smundak65b547e2021-09-17 15:35:41 -0700696 }
Sasha Smundak3deb9682021-07-26 18:42:25 -0700697 ctx.receiver.newNode(&exprNode{&callExpr{
Sasha Smundak65b547e2021-09-17 15:35:41 -0700698 name: fname,
Cole Faust9ebf6e42021-12-13 14:08:34 -0800699 args: []starlarkExpr{&globalsExpr{}, &stringLiteralExpr{namespaceName}, &stringLiteralExpr{varName}, val},
Sasha Smundak3deb9682021-07-26 18:42:25 -0700700 returnType: starlarkTypeVoid,
701 }})
702 }
703}
704
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800705func (ctx *parseContext) buildConcatExpr(a *mkparser.Assignment) *concatExpr {
706 xConcat := &concatExpr{}
707 var xItemList *listExpr
708 addToItemList := func(x ...starlarkExpr) {
709 if xItemList == nil {
710 xItemList = &listExpr{[]starlarkExpr{}}
711 }
712 xItemList.items = append(xItemList.items, x...)
713 }
714 finishItemList := func() {
715 if xItemList != nil {
716 xConcat.items = append(xConcat.items, xItemList)
717 xItemList = nil
718 }
719 }
720
721 items := a.Value.Words()
722 for _, item := range items {
723 // A function call in RHS is supposed to return a list, all other item
724 // expressions return individual elements.
725 switch x := ctx.parseMakeString(a, item).(type) {
726 case *badExpr:
727 ctx.wrapBadExpr(x)
728 return nil
729 case *stringLiteralExpr:
730 addToItemList(maybeConvertToStringList(x).(*listExpr).items...)
731 default:
732 switch x.typ() {
733 case starlarkTypeList:
734 finishItemList()
735 xConcat.items = append(xConcat.items, x)
736 case starlarkTypeString:
737 finishItemList()
738 xConcat.items = append(xConcat.items, &callExpr{
739 object: x,
740 name: "split",
741 args: nil,
742 returnType: starlarkTypeList,
743 })
744 default:
745 addToItemList(x)
746 }
747 }
748 }
749 if xItemList != nil {
750 xConcat.items = append(xConcat.items, xItemList)
751 }
752 return xConcat
753}
754
Sasha Smundak6609ba72021-07-22 18:32:56 -0700755func (ctx *parseContext) newDependentModule(path string, optional bool) *moduleInfo {
756 modulePath := ctx.loadedModulePath(path)
757 if mi, ok := ctx.dependentModules[modulePath]; ok {
Sasha Smundak868c5e32021-09-23 16:20:58 -0700758 mi.optional = mi.optional && optional
Sasha Smundak6609ba72021-07-22 18:32:56 -0700759 return mi
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800760 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800761 moduleName := moduleNameForFile(path)
762 moduleLocalName := "_" + moduleName
763 n, found := ctx.moduleNameCount[moduleName]
764 if found {
765 moduleLocalName += fmt.Sprintf("%d", n)
766 }
767 ctx.moduleNameCount[moduleName] = n + 1
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800768 _, err := fs.Stat(ctx.script.sourceFS, path)
Sasha Smundak6609ba72021-07-22 18:32:56 -0700769 mi := &moduleInfo{
770 path: modulePath,
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800771 originalPath: path,
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800772 moduleLocalName: moduleLocalName,
Sasha Smundak6609ba72021-07-22 18:32:56 -0700773 optional: optional,
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800774 missing: err != nil,
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800775 }
Sasha Smundak6609ba72021-07-22 18:32:56 -0700776 ctx.dependentModules[modulePath] = mi
777 ctx.script.inherited = append(ctx.script.inherited, mi)
778 return mi
779}
780
781func (ctx *parseContext) handleSubConfig(
782 v mkparser.Node, pathExpr starlarkExpr, loadAlways bool, processModule func(inheritedModule)) {
Sasha Smundak6609ba72021-07-22 18:32:56 -0700783
784 // In a simple case, the name of a module to inherit/include is known statically.
785 if path, ok := maybeString(pathExpr); ok {
Sasha Smundak868c5e32021-09-23 16:20:58 -0700786 // Note that even if this directive loads a module unconditionally, a module may be
787 // absent without causing any harm if this directive is inside an if/else block.
788 moduleShouldExist := loadAlways && ctx.ifNestLevel == 0
Sasha Smundak6609ba72021-07-22 18:32:56 -0700789 if strings.Contains(path, "*") {
790 if paths, err := fs.Glob(ctx.script.sourceFS, path); err == nil {
791 for _, p := range paths {
Sasha Smundak868c5e32021-09-23 16:20:58 -0700792 mi := ctx.newDependentModule(p, !moduleShouldExist)
793 processModule(inheritedStaticModule{mi, loadAlways})
Sasha Smundak6609ba72021-07-22 18:32:56 -0700794 }
795 } else {
796 ctx.errorf(v, "cannot glob wildcard argument")
797 }
798 } else {
Sasha Smundak868c5e32021-09-23 16:20:58 -0700799 mi := ctx.newDependentModule(path, !moduleShouldExist)
800 processModule(inheritedStaticModule{mi, loadAlways})
Sasha Smundak6609ba72021-07-22 18:32:56 -0700801 }
802 return
803 }
804
805 // If module path references variables (e.g., $(v1)/foo/$(v2)/device-config.mk), find all the paths in the
806 // source tree that may be a match and the corresponding variable values. For instance, if the source tree
807 // contains vendor1/foo/abc/dev.mk and vendor2/foo/def/dev.mk, the first one will be inherited when
808 // (v1, v2) == ('vendor1', 'abc'), and the second one when (v1, v2) == ('vendor2', 'def').
809 // We then emit the code that loads all of them, e.g.:
810 // load("//vendor1/foo/abc:dev.rbc", _dev1_init="init")
811 // load("//vendor2/foo/def/dev.rbc", _dev2_init="init")
812 // And then inherit it as follows:
813 // _e = {
814 // "vendor1/foo/abc/dev.mk": ("vendor1/foo/abc/dev", _dev1_init),
815 // "vendor2/foo/def/dev.mk": ("vendor2/foo/def/dev", _dev_init2) }.get("%s/foo/%s/dev.mk" % (v1, v2))
816 // if _e:
817 // rblf.inherit(handle, _e[0], _e[1])
818 //
819 var matchingPaths []string
820 varPath, ok := pathExpr.(*interpolateExpr)
821 if !ok {
822 ctx.errorf(v, "inherit-product/include argument is too complex")
823 return
824 }
825
826 pathPattern := []string{varPath.chunks[0]}
827 for _, chunk := range varPath.chunks[1:] {
828 if chunk != "" {
829 pathPattern = append(pathPattern, chunk)
830 }
831 }
Sasha Smundak6d852dd2021-09-27 20:34:39 -0700832 if pathPattern[0] == "" {
Cole Faust6c934f62022-01-06 15:51:12 -0800833 if len(ctx.includeTops) == 0 {
834 ctx.errorf(v, "inherit-product/include statements must not be prefixed with a variable, or must include a #RBC# include_top comment beforehand giving a root directory to search.")
835 return
836 }
Sasha Smundak6d852dd2021-09-27 20:34:39 -0700837 // If pattern starts from the top. restrict it to the directories where
838 // we know inherit-product uses dynamically calculated path.
839 for _, p := range ctx.includeTops {
840 pathPattern[0] = p
841 matchingPaths = append(matchingPaths, ctx.findMatchingPaths(pathPattern)...)
Sasha Smundak6609ba72021-07-22 18:32:56 -0700842 }
Sasha Smundak6d852dd2021-09-27 20:34:39 -0700843 } else {
844 matchingPaths = ctx.findMatchingPaths(pathPattern)
Sasha Smundak6609ba72021-07-22 18:32:56 -0700845 }
846 // Safeguard against $(call inherit-product,$(PRODUCT_PATH))
Sasha Smundak90be8c52021-08-03 11:06:10 -0700847 const maxMatchingFiles = 150
Sasha Smundak6609ba72021-07-22 18:32:56 -0700848 if len(matchingPaths) > maxMatchingFiles {
849 ctx.errorf(v, "there are >%d files matching the pattern, please rewrite it", maxMatchingFiles)
850 return
851 }
852 res := inheritedDynamicModule{*varPath, []*moduleInfo{}, loadAlways}
853 for _, p := range matchingPaths {
854 // A product configuration files discovered dynamically may attempt to inherit
855 // from another one which does not exist in this source tree. Prevent load errors
856 // by always loading the dynamic files as optional.
857 res.candidateModules = append(res.candidateModules, ctx.newDependentModule(p, true))
858 }
859 processModule(res)
860}
861
862func (ctx *parseContext) findMatchingPaths(pattern []string) []string {
863 files := ctx.script.makefileFinder.Find(ctx.script.topDir)
864 if len(pattern) == 0 {
865 return files
866 }
867
868 // Create regular expression from the pattern
869 s_regexp := "^" + regexp.QuoteMeta(pattern[0])
870 for _, s := range pattern[1:] {
871 s_regexp += ".*" + regexp.QuoteMeta(s)
872 }
873 s_regexp += "$"
874 rex := regexp.MustCompile(s_regexp)
875
876 // Now match
877 var res []string
878 for _, p := range files {
879 if rex.MatchString(p) {
880 res = append(res, p)
881 }
882 }
883 return res
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800884}
885
Cole Faust9ebf6e42021-12-13 14:08:34 -0800886func (ctx *parseContext) handleInheritModule(v mkparser.Node, args *mkparser.MakeString, loadAlways bool) {
887 args.TrimLeftSpaces()
888 args.TrimRightSpaces()
889 pathExpr := ctx.parseMakeString(v, args)
890 if _, ok := pathExpr.(*badExpr); ok {
891 ctx.errorf(v, "Unable to parse argument to inherit")
892 }
Sasha Smundak6609ba72021-07-22 18:32:56 -0700893 ctx.handleSubConfig(v, pathExpr, loadAlways, func(im inheritedModule) {
Sasha Smundak868c5e32021-09-23 16:20:58 -0700894 ctx.receiver.newNode(&inheritNode{im, loadAlways})
Sasha Smundak6609ba72021-07-22 18:32:56 -0700895 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800896}
897
898func (ctx *parseContext) handleInclude(v mkparser.Node, pathExpr starlarkExpr, loadAlways bool) {
Sasha Smundak6609ba72021-07-22 18:32:56 -0700899 ctx.handleSubConfig(v, pathExpr, loadAlways, func(im inheritedModule) {
Sasha Smundak868c5e32021-09-23 16:20:58 -0700900 ctx.receiver.newNode(&includeNode{im, loadAlways})
Sasha Smundak6609ba72021-07-22 18:32:56 -0700901 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800902}
903
904func (ctx *parseContext) handleVariable(v *mkparser.Variable) {
905 // Handle:
906 // $(call inherit-product,...)
907 // $(call inherit-product-if-exists,...)
908 // $(info xxx)
909 // $(warning xxx)
910 // $(error xxx)
Cole Faust9ebf6e42021-12-13 14:08:34 -0800911 // $(call other-custom-functions,...)
912
913 // inherit-product(-if-exists) gets converted to a series of statements,
914 // not just a single expression like parseReference returns. So handle it
915 // separately at the beginning here.
916 if strings.HasPrefix(v.Name.Dump(), "call inherit-product,") {
917 args := v.Name.Clone()
918 args.ReplaceLiteral("call inherit-product,", "")
919 ctx.handleInheritModule(v, args, true)
920 return
921 }
922 if strings.HasPrefix(v.Name.Dump(), "call inherit-product-if-exists,") {
923 args := v.Name.Clone()
924 args.ReplaceLiteral("call inherit-product-if-exists,", "")
925 ctx.handleInheritModule(v, args, false)
926 return
927 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800928 expr := ctx.parseReference(v, v.Name)
929 switch x := expr.(type) {
930 case *callExpr:
Cole Faust9ebf6e42021-12-13 14:08:34 -0800931 ctx.receiver.newNode(&exprNode{expr})
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800932 case *badExpr:
933 ctx.wrapBadExpr(x)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800934 default:
935 ctx.errorf(v, "cannot handle %s", v.Dump())
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800936 }
937}
938
939func (ctx *parseContext) handleDefine(directive *mkparser.Directive) {
Sasha Smundakf3e072a2021-07-14 12:50:28 -0700940 macro_name := strings.Fields(directive.Args.Strings[0])[0]
941 // Ignore the macros that we handle
Cole Faust9ebf6e42021-12-13 14:08:34 -0800942 _, ignored := ignoredDefines[macro_name]
943 _, known := knownFunctions[macro_name]
944 if !ignored && !known {
Sasha Smundakf3e072a2021-07-14 12:50:28 -0700945 ctx.errorf(directive, "define is not supported: %s", macro_name)
946 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800947}
948
949func (ctx *parseContext) handleIfBlock(ifDirective *mkparser.Directive) {
950 ssSwitch := &switchNode{}
951 ctx.pushReceiver(ssSwitch)
952 for ctx.processBranch(ifDirective); ctx.hasNodes() && ctx.fatalError == nil; {
953 node := ctx.getNode()
954 switch x := node.(type) {
955 case *mkparser.Directive:
956 switch x.Name {
957 case "else", "elifdef", "elifndef", "elifeq", "elifneq":
958 ctx.processBranch(x)
959 case "endif":
960 ctx.popReceiver()
961 ctx.receiver.newNode(ssSwitch)
962 return
963 default:
964 ctx.errorf(node, "unexpected directive %s", x.Name)
965 }
966 default:
967 ctx.errorf(ifDirective, "unexpected statement")
968 }
969 }
970 if ctx.fatalError == nil {
971 ctx.fatalError = fmt.Errorf("no matching endif for %s", ifDirective.Dump())
972 }
973 ctx.popReceiver()
974}
975
976// processBranch processes a single branch (if/elseif/else) until the next directive
977// on the same level.
978func (ctx *parseContext) processBranch(check *mkparser.Directive) {
979 block := switchCase{gate: ctx.parseCondition(check)}
980 defer func() {
981 ctx.popVarAssignments()
982 ctx.ifNestLevel--
983
984 }()
985 ctx.pushVarAssignments()
986 ctx.ifNestLevel++
987
988 ctx.pushReceiver(&block)
989 for ctx.hasNodes() {
990 node := ctx.getNode()
Cole Faust591a1fe2021-11-08 15:37:57 -0800991 if d, ok := node.(*mkparser.Directive); ok {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800992 switch d.Name {
993 case "else", "elifdef", "elifndef", "elifeq", "elifneq", "endif":
994 ctx.popReceiver()
995 ctx.receiver.newNode(&block)
996 ctx.backNode()
997 return
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800998 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800999 }
Cole Faust591a1fe2021-11-08 15:37:57 -08001000 ctx.handleSimpleStatement(node)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001001 }
1002 ctx.fatalError = fmt.Errorf("no matching endif for %s", check.Dump())
1003 ctx.popReceiver()
1004}
1005
1006func (ctx *parseContext) newIfDefinedNode(check *mkparser.Directive) (starlarkExpr, bool) {
1007 if !check.Args.Const() {
1008 return ctx.newBadExpr(check, "ifdef variable ref too complex: %s", check.Args.Dump()), false
1009 }
1010 v := ctx.addVariable(check.Args.Strings[0])
1011 return &variableDefinedExpr{v}, true
1012}
1013
1014func (ctx *parseContext) parseCondition(check *mkparser.Directive) starlarkNode {
1015 switch check.Name {
1016 case "ifdef", "ifndef", "elifdef", "elifndef":
1017 v, ok := ctx.newIfDefinedNode(check)
1018 if ok && strings.HasSuffix(check.Name, "ndef") {
1019 v = &notExpr{v}
1020 }
1021 return &ifNode{
1022 isElif: strings.HasPrefix(check.Name, "elif"),
1023 expr: v,
1024 }
1025 case "ifeq", "ifneq", "elifeq", "elifneq":
1026 return &ifNode{
1027 isElif: strings.HasPrefix(check.Name, "elif"),
1028 expr: ctx.parseCompare(check),
1029 }
1030 case "else":
1031 return &elseNode{}
1032 default:
1033 panic(fmt.Errorf("%s: unknown directive: %s", ctx.script.mkFile, check.Dump()))
1034 }
1035}
1036
1037func (ctx *parseContext) newBadExpr(node mkparser.Node, text string, args ...interface{}) starlarkExpr {
1038 message := fmt.Sprintf(text, args...)
1039 if ctx.errorLogger != nil {
Sasha Smundak422b6142021-11-11 18:31:59 -08001040 ctx.errorLogger.NewError(ctx.errorLocation(node), node, text, args...)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001041 }
1042 ctx.script.hasErrors = true
Sasha Smundak422b6142021-11-11 18:31:59 -08001043 return &badExpr{errorLocation: ctx.errorLocation(node), message: message}
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001044}
1045
1046func (ctx *parseContext) parseCompare(cond *mkparser.Directive) starlarkExpr {
1047 // Strip outer parentheses
1048 mkArg := cloneMakeString(cond.Args)
1049 mkArg.Strings[0] = strings.TrimLeft(mkArg.Strings[0], "( ")
1050 n := len(mkArg.Strings)
1051 mkArg.Strings[n-1] = strings.TrimRight(mkArg.Strings[n-1], ") ")
1052 args := mkArg.Split(",")
1053 // TODO(asmundak): handle the case where the arguments are in quotes and space-separated
1054 if len(args) != 2 {
1055 return ctx.newBadExpr(cond, "ifeq/ifneq len(args) != 2 %s", cond.Dump())
1056 }
1057 args[0].TrimRightSpaces()
1058 args[1].TrimLeftSpaces()
1059
1060 isEq := !strings.HasSuffix(cond.Name, "neq")
Cole Faustf8320212021-11-10 15:05:07 -08001061 xLeft := ctx.parseMakeString(cond, args[0])
1062 xRight := ctx.parseMakeString(cond, args[1])
1063 if bad, ok := xLeft.(*badExpr); ok {
1064 return bad
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001065 }
Cole Faustf8320212021-11-10 15:05:07 -08001066 if bad, ok := xRight.(*badExpr); ok {
1067 return bad
1068 }
1069
1070 if expr, ok := ctx.parseCompareSpecialCases(cond, xLeft, xRight); ok {
1071 return expr
1072 }
1073
Cole Faust9ebf6e42021-12-13 14:08:34 -08001074 var stringOperand string
1075 var otherOperand starlarkExpr
1076 if s, ok := maybeString(xLeft); ok {
1077 stringOperand = s
1078 otherOperand = xRight
1079 } else if s, ok := maybeString(xRight); ok {
1080 stringOperand = s
1081 otherOperand = xLeft
1082 }
1083
1084 not := func(expr starlarkExpr) starlarkExpr {
1085 switch typedExpr := expr.(type) {
1086 case *inExpr:
1087 typedExpr.isNot = !typedExpr.isNot
1088 return typedExpr
1089 case *eqExpr:
1090 typedExpr.isEq = !typedExpr.isEq
1091 return typedExpr
Cole Faustb1103e22022-01-06 15:22:05 -08001092 case *binaryOpExpr:
1093 switch typedExpr.op {
1094 case ">":
1095 typedExpr.op = "<="
1096 return typedExpr
1097 case "<":
1098 typedExpr.op = ">="
1099 return typedExpr
1100 case ">=":
1101 typedExpr.op = "<"
1102 return typedExpr
1103 case "<=":
1104 typedExpr.op = ">"
1105 return typedExpr
1106 default:
1107 return &notExpr{expr: expr}
1108 }
Cole Faust9ebf6e42021-12-13 14:08:34 -08001109 default:
1110 return &notExpr{expr: expr}
1111 }
1112 }
1113
1114 // If we've identified one of the operands as being a string literal, check
1115 // for some special cases we can do to simplify the resulting expression.
1116 if otherOperand != nil {
1117 if stringOperand == "" {
1118 if isEq {
1119 return not(otherOperand)
1120 } else {
1121 return otherOperand
1122 }
1123 }
1124 if stringOperand == "true" && otherOperand.typ() == starlarkTypeBool {
1125 if !isEq {
1126 return not(otherOperand)
1127 } else {
1128 return otherOperand
1129 }
1130 }
Cole Faustb1103e22022-01-06 15:22:05 -08001131 if intOperand, err := strconv.Atoi(strings.TrimSpace(stringOperand)); err == nil && otherOperand.typ() == starlarkTypeInt {
1132 return &eqExpr{
1133 left: otherOperand,
1134 right: &intLiteralExpr{literal: intOperand},
1135 isEq: isEq,
1136 }
1137 }
Cole Faust9ebf6e42021-12-13 14:08:34 -08001138 }
1139
Cole Faustf8320212021-11-10 15:05:07 -08001140 return &eqExpr{left: xLeft, right: xRight, isEq: isEq}
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001141}
1142
Cole Faustf8320212021-11-10 15:05:07 -08001143// Given an if statement's directive and the left/right starlarkExprs,
1144// check if the starlarkExprs are one of a few hardcoded special cases
1145// that can be converted to a simpler equalify expression than simply comparing
1146// the two.
1147func (ctx *parseContext) parseCompareSpecialCases(directive *mkparser.Directive, left starlarkExpr,
1148 right starlarkExpr) (starlarkExpr, bool) {
1149 isEq := !strings.HasSuffix(directive.Name, "neq")
1150
1151 // All the special cases require a call on one side and a
1152 // string literal/variable on the other. Turn the left/right variables into
1153 // call/value variables, and return false if that's not possible.
1154 var value starlarkExpr = nil
1155 call, ok := left.(*callExpr)
1156 if ok {
1157 switch right.(type) {
1158 case *stringLiteralExpr, *variableRefExpr:
1159 value = right
1160 }
1161 } else {
1162 call, _ = right.(*callExpr)
1163 switch left.(type) {
1164 case *stringLiteralExpr, *variableRefExpr:
1165 value = left
1166 }
1167 }
1168
1169 if call == nil || value == nil {
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001170 return nil, false
1171 }
Cole Faustf8320212021-11-10 15:05:07 -08001172
Cole Faustf8320212021-11-10 15:05:07 -08001173 switch call.name {
Cole Faust9ebf6e42021-12-13 14:08:34 -08001174 case baseName + ".filter", baseName + ".filter-out":
Cole Faustf8320212021-11-10 15:05:07 -08001175 return ctx.parseCompareFilterFuncResult(directive, call, value, isEq), true
Cole Faust9ebf6e42021-12-13 14:08:34 -08001176 case baseName + ".expand_wildcard":
Cole Faustf8320212021-11-10 15:05:07 -08001177 return ctx.parseCompareWildcardFuncResult(directive, call, value, !isEq), true
Cole Faust9ebf6e42021-12-13 14:08:34 -08001178 case baseName + ".findstring":
Cole Faustf8320212021-11-10 15:05:07 -08001179 return ctx.parseCheckFindstringFuncResult(directive, call, value, !isEq), true
Cole Faust9ebf6e42021-12-13 14:08:34 -08001180 case baseName + ".strip":
Cole Faustf8320212021-11-10 15:05:07 -08001181 return ctx.parseCompareStripFuncResult(directive, call, value, !isEq), true
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001182 }
Cole Faustf8320212021-11-10 15:05:07 -08001183 return nil, false
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001184}
1185
1186func (ctx *parseContext) parseCompareFilterFuncResult(cond *mkparser.Directive,
1187 filterFuncCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr {
1188 // We handle:
Sasha Smundak0554d762021-07-08 18:26:12 -07001189 // * ifeq/ifneq (,$(filter v1 v2 ..., EXPR) becomes if EXPR not in/in ["v1", "v2", ...]
1190 // * ifeq/ifneq (,$(filter EXPR, v1 v2 ...) becomes if EXPR not in/in ["v1", "v2", ...]
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001191 // * ifeq/ifneq ($(VAR),$(filter $(VAR), v1 v2 ...) becomes if VAR in/not in ["v1", "v2"]
1192 // TODO(Asmundak): check the last case works for filter-out, too.
1193 xPattern := filterFuncCall.args[0]
1194 xText := filterFuncCall.args[1]
1195 var xInList *stringLiteralExpr
Sasha Smundak0554d762021-07-08 18:26:12 -07001196 var expr starlarkExpr
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001197 var ok bool
1198 switch x := xValue.(type) {
1199 case *stringLiteralExpr:
1200 if x.literal != "" {
1201 return ctx.newBadExpr(cond, "filter comparison to non-empty value: %s", xValue)
1202 }
1203 // Either pattern or text should be const, and the
1204 // non-const one should be varRefExpr
Sasha Smundak5f463be2021-09-15 18:43:36 -07001205 if xInList, ok = xPattern.(*stringLiteralExpr); ok && !strings.ContainsRune(xInList.literal, '%') && xText.typ() == starlarkTypeList {
Sasha Smundak0554d762021-07-08 18:26:12 -07001206 expr = xText
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001207 } else if xInList, ok = xText.(*stringLiteralExpr); ok {
Sasha Smundak0554d762021-07-08 18:26:12 -07001208 expr = xPattern
1209 } else {
Sasha Smundak5f463be2021-09-15 18:43:36 -07001210 expr = &callExpr{
Sasha Smundak0554d762021-07-08 18:26:12 -07001211 object: nil,
1212 name: filterFuncCall.name,
1213 args: filterFuncCall.args,
1214 returnType: starlarkTypeBool,
1215 }
Sasha Smundak5f463be2021-09-15 18:43:36 -07001216 if negate {
1217 expr = &notExpr{expr: expr}
1218 }
1219 return expr
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001220 }
1221 case *variableRefExpr:
1222 if v, ok := xPattern.(*variableRefExpr); ok {
1223 if xInList, ok = xText.(*stringLiteralExpr); ok && v.ref.name() == x.ref.name() {
1224 // ifeq/ifneq ($(VAR),$(filter $(VAR), v1 v2 ...), flip negate,
1225 // it's the opposite to what is done when comparing to empty.
Sasha Smundak0554d762021-07-08 18:26:12 -07001226 expr = xPattern
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001227 negate = !negate
1228 }
1229 }
1230 }
Sasha Smundak0554d762021-07-08 18:26:12 -07001231 if expr != nil && xInList != nil {
1232 slExpr := newStringListExpr(strings.Fields(xInList.literal))
1233 // Generate simpler code for the common cases:
1234 if expr.typ() == starlarkTypeList {
1235 if len(slExpr.items) == 1 {
1236 // Checking that a string belongs to list
1237 return &inExpr{isNot: negate, list: expr, expr: slExpr.items[0]}
1238 } else {
1239 // TODO(asmundak):
1240 panic("TBD")
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001241 }
Sasha Smundak0554d762021-07-08 18:26:12 -07001242 } else if len(slExpr.items) == 1 {
1243 return &eqExpr{left: expr, right: slExpr.items[0], isEq: !negate}
1244 } else {
1245 return &inExpr{isNot: negate, list: newStringListExpr(strings.Fields(xInList.literal)), expr: expr}
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001246 }
1247 }
1248 return ctx.newBadExpr(cond, "filter arguments are too complex: %s", cond.Dump())
1249}
1250
1251func (ctx *parseContext) parseCompareWildcardFuncResult(directive *mkparser.Directive,
1252 xCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr {
Sasha Smundak0554d762021-07-08 18:26:12 -07001253 if !isEmptyString(xValue) {
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001254 return ctx.newBadExpr(directive, "wildcard result can be compared only to empty: %s", xValue)
1255 }
Cole Faust9ebf6e42021-12-13 14:08:34 -08001256 callFunc := baseName + ".file_wildcard_exists"
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001257 if s, ok := xCall.args[0].(*stringLiteralExpr); ok && !strings.ContainsAny(s.literal, "*?{[") {
Cole Faust9ebf6e42021-12-13 14:08:34 -08001258 callFunc = baseName + ".file_exists"
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001259 }
1260 var cc starlarkExpr = &callExpr{name: callFunc, args: xCall.args, returnType: starlarkTypeBool}
1261 if !negate {
1262 cc = &notExpr{cc}
1263 }
1264 return cc
1265}
1266
1267func (ctx *parseContext) parseCheckFindstringFuncResult(directive *mkparser.Directive,
1268 xCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr {
Sasha Smundak0554d762021-07-08 18:26:12 -07001269 if isEmptyString(xValue) {
1270 return &eqExpr{
1271 left: &callExpr{
1272 object: xCall.args[1],
1273 name: "find",
1274 args: []starlarkExpr{xCall.args[0]},
1275 returnType: starlarkTypeInt,
1276 },
1277 right: &intLiteralExpr{-1},
1278 isEq: !negate,
1279 }
Cole Faust0e9418c2021-12-13 16:33:25 -08001280 } else if s, ok := maybeString(xValue); ok {
1281 if s2, ok := maybeString(xCall.args[0]); ok && s == s2 {
1282 return &eqExpr{
1283 left: &callExpr{
1284 object: xCall.args[1],
1285 name: "find",
1286 args: []starlarkExpr{xCall.args[0]},
1287 returnType: starlarkTypeInt,
1288 },
1289 right: &intLiteralExpr{-1},
1290 isEq: negate,
1291 }
1292 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001293 }
Cole Faust0e9418c2021-12-13 16:33:25 -08001294 return ctx.newBadExpr(directive, "$(findstring) can only be compared to nothing or its first argument")
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001295}
1296
1297func (ctx *parseContext) parseCompareStripFuncResult(directive *mkparser.Directive,
1298 xCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr {
1299 if _, ok := xValue.(*stringLiteralExpr); !ok {
1300 return ctx.newBadExpr(directive, "strip result can be compared only to string: %s", xValue)
1301 }
1302 return &eqExpr{
1303 left: &callExpr{
1304 name: "strip",
1305 args: xCall.args,
1306 returnType: starlarkTypeString,
1307 },
1308 right: xValue, isEq: !negate}
1309}
1310
1311// parses $(...), returning an expression
1312func (ctx *parseContext) parseReference(node mkparser.Node, ref *mkparser.MakeString) starlarkExpr {
1313 ref.TrimLeftSpaces()
1314 ref.TrimRightSpaces()
1315 refDump := ref.Dump()
1316
1317 // Handle only the case where the first (or only) word is constant
1318 words := ref.SplitN(" ", 2)
1319 if !words[0].Const() {
1320 return ctx.newBadExpr(node, "reference is too complex: %s", refDump)
1321 }
1322
1323 // If it is a single word, it can be a simple variable
1324 // reference or a function call
Cole Faust9ebf6e42021-12-13 14:08:34 -08001325 if len(words) == 1 && !isMakeControlFunc(refDump) && refDump != "shell" {
Sasha Smundak65b547e2021-09-17 15:35:41 -07001326 if strings.HasPrefix(refDump, soongNsPrefix) {
1327 // TODO (asmundak): if we find many, maybe handle them.
Cole Faustc00184e2021-11-08 12:08:57 -08001328 return ctx.newBadExpr(node, "SOONG_CONFIG_ variables cannot be referenced, use soong_config_get instead: %s", refDump)
Sasha Smundak65b547e2021-09-17 15:35:41 -07001329 }
Cole Faustc36c9622021-12-07 15:20:45 -08001330 // Handle substitution references: https://www.gnu.org/software/make/manual/html_node/Substitution-Refs.html
1331 if strings.Contains(refDump, ":") {
1332 parts := strings.SplitN(refDump, ":", 2)
1333 substParts := strings.SplitN(parts[1], "=", 2)
1334 if len(substParts) < 2 || strings.Count(substParts[0], "%") > 1 {
1335 return ctx.newBadExpr(node, "Invalid substitution reference")
1336 }
1337 if !strings.Contains(substParts[0], "%") {
1338 if strings.Contains(substParts[1], "%") {
1339 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.")
1340 }
1341 substParts[0] = "%" + substParts[0]
1342 substParts[1] = "%" + substParts[1]
1343 }
1344 v := ctx.addVariable(parts[0])
1345 if v == nil {
1346 return ctx.newBadExpr(node, "unknown variable %s", refDump)
1347 }
1348 return &callExpr{
Cole Faust9ebf6e42021-12-13 14:08:34 -08001349 name: baseName + ".mkpatsubst",
1350 returnType: starlarkTypeString,
Cole Faustc36c9622021-12-07 15:20:45 -08001351 args: []starlarkExpr{
1352 &stringLiteralExpr{literal: substParts[0]},
1353 &stringLiteralExpr{literal: substParts[1]},
Cole Faustfc438682021-12-14 12:46:32 -08001354 NewVariableRefExpr(v, ctx.lastAssignment(v.name()) != nil),
Cole Faustc36c9622021-12-07 15:20:45 -08001355 },
1356 }
1357 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001358 if v := ctx.addVariable(refDump); v != nil {
Cole Faustfc438682021-12-14 12:46:32 -08001359 return NewVariableRefExpr(v, ctx.lastAssignment(v.name()) != nil)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001360 }
1361 return ctx.newBadExpr(node, "unknown variable %s", refDump)
1362 }
1363
1364 expr := &callExpr{name: words[0].Dump(), returnType: starlarkTypeUnknown}
Cole Faust9ebf6e42021-12-13 14:08:34 -08001365 args := mkparser.SimpleMakeString("", words[0].Pos())
1366 if len(words) >= 2 {
1367 args = words[1]
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001368 }
Cole Faust9ebf6e42021-12-13 14:08:34 -08001369 args.TrimLeftSpaces()
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001370 if expr.name == "call" {
1371 words = args.SplitN(",", 2)
1372 if words[0].Empty() || !words[0].Const() {
Sasha Smundakf2c9f8b2021-07-27 10:44:48 -07001373 return ctx.newBadExpr(node, "cannot handle %s", refDump)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001374 }
1375 expr.name = words[0].Dump()
1376 if len(words) < 2 {
Sasha Smundak6609ba72021-07-22 18:32:56 -07001377 args = &mkparser.MakeString{}
1378 } else {
1379 args = words[1]
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001380 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001381 }
1382 if kf, found := knownFunctions[expr.name]; found {
Cole Faust9ebf6e42021-12-13 14:08:34 -08001383 return kf.parse(ctx, node, args)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001384 } else {
1385 return ctx.newBadExpr(node, "cannot handle invoking %s", expr.name)
1386 }
Cole Faust9ebf6e42021-12-13 14:08:34 -08001387}
1388
1389type simpleCallParser struct {
1390 name string
1391 returnType starlarkType
1392 addGlobals bool
1393}
1394
1395func (p *simpleCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
1396 expr := &callExpr{name: p.name, returnType: p.returnType}
1397 if p.addGlobals {
1398 expr.args = append(expr.args, &globalsExpr{})
1399 }
1400 for _, arg := range args.Split(",") {
1401 arg.TrimLeftSpaces()
1402 arg.TrimRightSpaces()
1403 x := ctx.parseMakeString(node, arg)
1404 if xBad, ok := x.(*badExpr); ok {
1405 return xBad
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001406 }
Cole Faust9ebf6e42021-12-13 14:08:34 -08001407 expr.args = append(expr.args, x)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001408 }
1409 return expr
1410}
1411
Cole Faust9ebf6e42021-12-13 14:08:34 -08001412type makeControlFuncParser struct {
1413 name string
1414}
1415
1416func (p *makeControlFuncParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
1417 // Make control functions need special treatment as everything
1418 // after the name is a single text argument
1419 x := ctx.parseMakeString(node, args)
1420 if xBad, ok := x.(*badExpr); ok {
1421 return xBad
1422 }
1423 return &callExpr{
1424 name: p.name,
1425 args: []starlarkExpr{
1426 &stringLiteralExpr{ctx.script.mkFile},
1427 x,
1428 },
1429 returnType: starlarkTypeUnknown,
1430 }
1431}
1432
1433type shellCallParser struct{}
1434
1435func (p *shellCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
1436 // Shell functions need special treatment as everything
1437 // after the name is a single text argument
1438 x := ctx.parseMakeString(node, args)
1439 if xBad, ok := x.(*badExpr); ok {
1440 return xBad
1441 }
1442 return &callExpr{
1443 name: baseName + ".shell",
1444 args: []starlarkExpr{x},
1445 returnType: starlarkTypeUnknown,
1446 }
1447}
1448
1449type myDirCallParser struct{}
1450
1451func (p *myDirCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
1452 if !args.Empty() {
1453 return ctx.newBadExpr(node, "my-dir function cannot have any arguments passed to it.")
1454 }
1455 return &variableRefExpr{ctx.addVariable("LOCAL_PATH"), true}
1456}
1457
Cole Faust9ebf6e42021-12-13 14:08:34 -08001458type isProductInListCallParser struct{}
1459
1460func (p *isProductInListCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
1461 if args.Empty() {
1462 return ctx.newBadExpr(node, "is-product-in-list requires an argument")
1463 }
1464 return &inExpr{
1465 expr: &variableRefExpr{ctx.addVariable("TARGET_PRODUCT"), true},
1466 list: maybeConvertToStringList(ctx.parseMakeString(node, args)),
1467 isNot: false,
1468 }
1469}
1470
1471type isVendorBoardPlatformCallParser struct{}
1472
1473func (p *isVendorBoardPlatformCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
1474 if args.Empty() || !identifierFullMatchRegex.MatchString(args.Dump()) {
1475 return ctx.newBadExpr(node, "cannot handle non-constant argument to is-vendor-board-platform")
1476 }
1477 return &inExpr{
1478 expr: &variableRefExpr{ctx.addVariable("TARGET_BOARD_PLATFORM"), false},
1479 list: &variableRefExpr{ctx.addVariable(args.Dump() + "_BOARD_PLATFORMS"), true},
1480 isNot: false,
1481 }
1482}
1483
1484type isVendorBoardQcomCallParser struct{}
1485
1486func (p *isVendorBoardQcomCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
1487 if !args.Empty() {
1488 return ctx.newBadExpr(node, "is-vendor-board-qcom does not accept any arguments")
1489 }
1490 return &inExpr{
1491 expr: &variableRefExpr{ctx.addVariable("TARGET_BOARD_PLATFORM"), false},
1492 list: &variableRefExpr{ctx.addVariable("QCOM_BOARD_PLATFORMS"), true},
1493 isNot: false,
1494 }
1495}
1496
1497type substCallParser struct {
1498 fname string
1499}
1500
1501func (p *substCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001502 words := args.Split(",")
1503 if len(words) != 3 {
Cole Faust9ebf6e42021-12-13 14:08:34 -08001504 return ctx.newBadExpr(node, "%s function should have 3 arguments", p.fname)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001505 }
Sasha Smundak35434ed2021-11-05 16:29:56 -07001506 from := ctx.parseMakeString(node, words[0])
1507 if xBad, ok := from.(*badExpr); ok {
1508 return xBad
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001509 }
Sasha Smundak35434ed2021-11-05 16:29:56 -07001510 to := ctx.parseMakeString(node, words[1])
1511 if xBad, ok := to.(*badExpr); ok {
1512 return xBad
1513 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001514 words[2].TrimLeftSpaces()
1515 words[2].TrimRightSpaces()
1516 obj := ctx.parseMakeString(node, words[2])
Sasha Smundak9d011ab2021-07-09 16:00:57 -07001517 typ := obj.typ()
Cole Faust9ebf6e42021-12-13 14:08:34 -08001518 if typ == starlarkTypeString && p.fname == "subst" {
Sasha Smundak94b41c72021-07-12 18:30:42 -07001519 // Optimization: if it's $(subst from, to, string), emit string.replace(from, to)
Sasha Smundak9d011ab2021-07-09 16:00:57 -07001520 return &callExpr{
1521 object: obj,
1522 name: "replace",
Sasha Smundak35434ed2021-11-05 16:29:56 -07001523 args: []starlarkExpr{from, to},
Sasha Smundak9d011ab2021-07-09 16:00:57 -07001524 returnType: typ,
1525 }
1526 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001527 return &callExpr{
Cole Faust9ebf6e42021-12-13 14:08:34 -08001528 name: baseName + ".mk" + p.fname,
Sasha Smundak35434ed2021-11-05 16:29:56 -07001529 args: []starlarkExpr{from, to, obj},
Sasha Smundak9d011ab2021-07-09 16:00:57 -07001530 returnType: obj.typ(),
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001531 }
1532}
1533
Cole Faust9ebf6e42021-12-13 14:08:34 -08001534type ifCallParser struct{}
1535
1536func (p *ifCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
Cole Faust4eadba72021-12-07 11:54:52 -08001537 words := args.Split(",")
1538 if len(words) != 2 && len(words) != 3 {
1539 return ctx.newBadExpr(node, "if function should have 2 or 3 arguments, found "+strconv.Itoa(len(words)))
1540 }
1541 condition := ctx.parseMakeString(node, words[0])
1542 ifTrue := ctx.parseMakeString(node, words[1])
1543 var ifFalse starlarkExpr
1544 if len(words) == 3 {
1545 ifFalse = ctx.parseMakeString(node, words[2])
1546 } else {
1547 switch ifTrue.typ() {
1548 case starlarkTypeList:
1549 ifFalse = &listExpr{items: []starlarkExpr{}}
1550 case starlarkTypeInt:
1551 ifFalse = &intLiteralExpr{literal: 0}
1552 case starlarkTypeBool:
1553 ifFalse = &boolLiteralExpr{literal: false}
1554 default:
1555 ifFalse = &stringLiteralExpr{literal: ""}
1556 }
1557 }
1558 return &ifExpr{
1559 condition,
1560 ifTrue,
1561 ifFalse,
1562 }
1563}
1564
Cole Faust9ebf6e42021-12-13 14:08:34 -08001565type foreachCallPaser struct{}
1566
1567func (p *foreachCallPaser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
Cole Faustb0d32ab2021-12-09 14:00:59 -08001568 words := args.Split(",")
1569 if len(words) != 3 {
1570 return ctx.newBadExpr(node, "foreach function should have 3 arguments, found "+strconv.Itoa(len(words)))
1571 }
1572 if !words[0].Const() || words[0].Empty() || !identifierFullMatchRegex.MatchString(words[0].Strings[0]) {
1573 return ctx.newBadExpr(node, "first argument to foreach function must be a simple string identifier")
1574 }
1575 loopVarName := words[0].Strings[0]
1576 list := ctx.parseMakeString(node, words[1])
1577 action := ctx.parseMakeString(node, words[2]).transform(func(expr starlarkExpr) starlarkExpr {
1578 if varRefExpr, ok := expr.(*variableRefExpr); ok && varRefExpr.ref.name() == loopVarName {
1579 return &identifierExpr{loopVarName}
1580 }
1581 return nil
1582 })
1583
1584 if list.typ() != starlarkTypeList {
1585 list = &callExpr{
Cole Faust9ebf6e42021-12-13 14:08:34 -08001586 name: baseName + ".words",
1587 returnType: starlarkTypeList,
Cole Faustb0d32ab2021-12-09 14:00:59 -08001588 args: []starlarkExpr{list},
1589 }
1590 }
1591
1592 return &foreachExpr{
1593 varName: loopVarName,
1594 list: list,
1595 action: action,
1596 }
1597}
1598
Cole Faust9ebf6e42021-12-13 14:08:34 -08001599type wordCallParser struct{}
1600
1601func (p *wordCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001602 words := args.Split(",")
1603 if len(words) != 2 {
1604 return ctx.newBadExpr(node, "word function should have 2 arguments")
1605 }
1606 var index uint64 = 0
1607 if words[0].Const() {
1608 index, _ = strconv.ParseUint(strings.TrimSpace(words[0].Strings[0]), 10, 64)
1609 }
1610 if index < 1 {
1611 return ctx.newBadExpr(node, "word index should be constant positive integer")
1612 }
1613 words[1].TrimLeftSpaces()
1614 words[1].TrimRightSpaces()
1615 array := ctx.parseMakeString(node, words[1])
1616 if xBad, ok := array.(*badExpr); ok {
1617 return xBad
1618 }
1619 if array.typ() != starlarkTypeList {
1620 array = &callExpr{object: array, name: "split", returnType: starlarkTypeList}
1621 }
Cole Faustb0d32ab2021-12-09 14:00:59 -08001622 return &indexExpr{array, &intLiteralExpr{int(index - 1)}}
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001623}
1624
Cole Faust9ebf6e42021-12-13 14:08:34 -08001625type firstOrLastwordCallParser struct {
1626 isLastWord bool
1627}
1628
1629func (p *firstOrLastwordCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
Sasha Smundak16e07732021-07-23 11:38:23 -07001630 arg := ctx.parseMakeString(node, args)
1631 if bad, ok := arg.(*badExpr); ok {
1632 return bad
1633 }
1634 index := &intLiteralExpr{0}
Cole Faust9ebf6e42021-12-13 14:08:34 -08001635 if p.isLastWord {
Sasha Smundak16e07732021-07-23 11:38:23 -07001636 if v, ok := arg.(*variableRefExpr); ok && v.ref.name() == "MAKEFILE_LIST" {
1637 return &stringLiteralExpr{ctx.script.mkFile}
1638 }
1639 index.literal = -1
1640 }
1641 if arg.typ() == starlarkTypeList {
1642 return &indexExpr{arg, index}
1643 }
1644 return &indexExpr{&callExpr{object: arg, name: "split", returnType: starlarkTypeList}, index}
1645}
1646
Cole Faustb1103e22022-01-06 15:22:05 -08001647func parseIntegerArguments(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString, expectedArgs int) ([]starlarkExpr, error) {
1648 parsedArgs := make([]starlarkExpr, 0)
1649 for _, arg := range args.Split(",") {
1650 expr := ctx.parseMakeString(node, arg)
1651 if expr.typ() == starlarkTypeList {
1652 return nil, fmt.Errorf("argument to math argument has type list, which cannot be converted to int")
1653 }
1654 if s, ok := maybeString(expr); ok {
1655 intVal, err := strconv.Atoi(strings.TrimSpace(s))
1656 if err != nil {
1657 return nil, err
1658 }
1659 expr = &intLiteralExpr{literal: intVal}
1660 } else if expr.typ() != starlarkTypeInt {
1661 expr = &callExpr{
1662 name: "int",
1663 args: []starlarkExpr{expr},
1664 returnType: starlarkTypeInt,
1665 }
1666 }
1667 parsedArgs = append(parsedArgs, expr)
1668 }
1669 if len(parsedArgs) != expectedArgs {
1670 return nil, fmt.Errorf("function should have %d arguments", expectedArgs)
1671 }
1672 return parsedArgs, nil
1673}
1674
1675type mathComparisonCallParser struct {
1676 op string
1677}
1678
1679func (p *mathComparisonCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
1680 parsedArgs, err := parseIntegerArguments(ctx, node, args, 2)
1681 if err != nil {
1682 return ctx.newBadExpr(node, err.Error())
1683 }
1684 return &binaryOpExpr{
1685 left: parsedArgs[0],
1686 right: parsedArgs[1],
1687 op: p.op,
1688 returnType: starlarkTypeBool,
1689 }
1690}
1691
1692type mathMaxOrMinCallParser struct {
1693 function string
1694}
1695
1696func (p *mathMaxOrMinCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
1697 parsedArgs, err := parseIntegerArguments(ctx, node, args, 2)
1698 if err != nil {
1699 return ctx.newBadExpr(node, err.Error())
1700 }
1701 return &callExpr{
1702 object: nil,
1703 name: p.function,
1704 args: parsedArgs,
1705 returnType: starlarkTypeInt,
1706 }
1707}
1708
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001709func (ctx *parseContext) parseMakeString(node mkparser.Node, mk *mkparser.MakeString) starlarkExpr {
1710 if mk.Const() {
1711 return &stringLiteralExpr{mk.Dump()}
1712 }
1713 if mkRef, ok := mk.SingleVariable(); ok {
1714 return ctx.parseReference(node, mkRef)
1715 }
1716 // If we reached here, it's neither string literal nor a simple variable,
1717 // we need a full-blown interpolation node that will generate
1718 // "a%b%c" % (X, Y) for a$(X)b$(Y)c
Cole Faustfc438682021-12-14 12:46:32 -08001719 parts := make([]starlarkExpr, len(mk.Variables)+len(mk.Strings))
1720 for i := 0; i < len(parts); i++ {
1721 if i%2 == 0 {
1722 parts[i] = &stringLiteralExpr{literal: mk.Strings[i/2]}
1723 } else {
1724 parts[i] = ctx.parseReference(node, mk.Variables[i/2].Name)
1725 if x, ok := parts[i].(*badExpr); ok {
1726 return x
1727 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001728 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001729 }
Cole Faustfc438682021-12-14 12:46:32 -08001730 return NewInterpolateExpr(parts)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001731}
1732
1733// Handles the statements whose treatment is the same in all contexts: comment,
1734// assignment, variable (which is a macro call in reality) and all constructs that
1735// do not handle in any context ('define directive and any unrecognized stuff).
Cole Faust591a1fe2021-11-08 15:37:57 -08001736func (ctx *parseContext) handleSimpleStatement(node mkparser.Node) {
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001737 switch x := node.(type) {
1738 case *mkparser.Comment:
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001739 ctx.maybeHandleAnnotation(x)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001740 ctx.insertComment("#" + x.Comment)
1741 case *mkparser.Assignment:
1742 ctx.handleAssignment(x)
1743 case *mkparser.Variable:
1744 ctx.handleVariable(x)
1745 case *mkparser.Directive:
1746 switch x.Name {
1747 case "define":
1748 ctx.handleDefine(x)
1749 case "include", "-include":
1750 ctx.handleInclude(node, ctx.parseMakeString(node, x.Args), x.Name[0] != '-')
Cole Faust591a1fe2021-11-08 15:37:57 -08001751 case "ifeq", "ifneq", "ifdef", "ifndef":
1752 ctx.handleIfBlock(x)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001753 default:
Cole Faust591a1fe2021-11-08 15:37:57 -08001754 ctx.errorf(x, "unexpected directive %s", x.Name)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001755 }
1756 default:
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001757 ctx.errorf(x, "unsupported line %s", strings.ReplaceAll(x.Dump(), "\n", "\n#"))
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001758 }
Cole Faust6c934f62022-01-06 15:51:12 -08001759
1760 // Clear the includeTops after each non-comment statement
1761 // so that include annotations placed on certain statements don't apply
1762 // globally for the rest of the makefile was well.
1763 if _, wasComment := node.(*mkparser.Comment); !wasComment && len(ctx.includeTops) > 0 {
1764 ctx.includeTops = []string{}
1765 }
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001766}
1767
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001768// Processes annotation. An annotation is a comment that starts with #RBC# and provides
1769// a conversion hint -- say, where to look for the dynamically calculated inherit/include
1770// paths.
1771func (ctx *parseContext) maybeHandleAnnotation(cnode *mkparser.Comment) {
1772 maybeTrim := func(s, prefix string) (string, bool) {
1773 if strings.HasPrefix(s, prefix) {
1774 return strings.TrimSpace(strings.TrimPrefix(s, prefix)), true
1775 }
1776 return s, false
1777 }
1778 annotation, ok := maybeTrim(cnode.Comment, annotationCommentPrefix)
1779 if !ok {
1780 return
1781 }
1782 if p, ok := maybeTrim(annotation, "include_top"); ok {
Cole Faustf7ed5342021-12-21 14:15:12 -08001783 // Don't allow duplicate include tops, because then we will generate
1784 // invalid starlark code. (duplicate keys in the _entry dictionary)
1785 for _, top := range ctx.includeTops {
1786 if top == p {
1787 return
1788 }
1789 }
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001790 ctx.includeTops = append(ctx.includeTops, p)
1791 return
1792 }
1793 ctx.errorf(cnode, "unsupported annotation %s", cnode.Comment)
1794
1795}
1796
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001797func (ctx *parseContext) insertComment(s string) {
1798 ctx.receiver.newNode(&commentNode{strings.TrimSpace(s)})
1799}
1800
1801func (ctx *parseContext) carryAsComment(failedNode mkparser.Node) {
1802 for _, line := range strings.Split(failedNode.Dump(), "\n") {
1803 ctx.insertComment("# " + line)
1804 }
1805}
1806
1807// records that the given node failed to be converted and includes an explanatory message
1808func (ctx *parseContext) errorf(failedNode mkparser.Node, message string, args ...interface{}) {
1809 if ctx.errorLogger != nil {
Sasha Smundak422b6142021-11-11 18:31:59 -08001810 ctx.errorLogger.NewError(ctx.errorLocation(failedNode), failedNode, message, args...)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001811 }
Sasha Smundak422b6142021-11-11 18:31:59 -08001812 ctx.receiver.newNode(&exprNode{ctx.newBadExpr(failedNode, message, args...)})
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001813 ctx.script.hasErrors = true
1814}
1815
1816func (ctx *parseContext) wrapBadExpr(xBad *badExpr) {
Sasha Smundak422b6142021-11-11 18:31:59 -08001817 ctx.receiver.newNode(&exprNode{xBad})
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001818}
1819
1820func (ctx *parseContext) loadedModulePath(path string) string {
1821 // During the transition to Roboleaf some of the product configuration files
1822 // will be converted and checked in while the others will be generated on the fly
1823 // and run. The runner (rbcrun application) accommodates this by allowing three
1824 // different ways to specify the loaded file location:
1825 // 1) load(":<file>",...) loads <file> from the same directory
1826 // 2) load("//path/relative/to/source/root:<file>", ...) loads <file> source tree
1827 // 3) load("/absolute/path/to/<file> absolute path
1828 // If the file being generated and the file it wants to load are in the same directory,
1829 // generate option 1.
1830 // Otherwise, if output directory is not specified, generate 2)
1831 // Finally, if output directory has been specified and the file being generated and
1832 // the file it wants to load from are in the different directories, generate 2) or 3):
1833 // * if the file being loaded exists in the source tree, generate 2)
1834 // * otherwise, generate 3)
1835 // Finally, figure out the loaded module path and name and create a node for it
1836 loadedModuleDir := filepath.Dir(path)
1837 base := filepath.Base(path)
1838 loadedModuleName := strings.TrimSuffix(base, filepath.Ext(base)) + ctx.outputSuffix
1839 if loadedModuleDir == filepath.Dir(ctx.script.mkFile) {
1840 return ":" + loadedModuleName
1841 }
1842 if ctx.outputDir == "" {
1843 return fmt.Sprintf("//%s:%s", loadedModuleDir, loadedModuleName)
1844 }
1845 if _, err := os.Stat(filepath.Join(loadedModuleDir, loadedModuleName)); err == nil {
1846 return fmt.Sprintf("//%s:%s", loadedModuleDir, loadedModuleName)
1847 }
1848 return filepath.Join(ctx.outputDir, loadedModuleDir, loadedModuleName)
1849}
1850
Sasha Smundak3deb9682021-07-26 18:42:25 -07001851func (ctx *parseContext) addSoongNamespace(ns string) {
1852 if _, ok := ctx.soongNamespaces[ns]; ok {
1853 return
1854 }
1855 ctx.soongNamespaces[ns] = make(map[string]bool)
1856}
1857
1858func (ctx *parseContext) hasSoongNamespace(name string) bool {
1859 _, ok := ctx.soongNamespaces[name]
1860 return ok
1861}
1862
1863func (ctx *parseContext) updateSoongNamespace(replace bool, namespaceName string, varNames []string) {
1864 ctx.addSoongNamespace(namespaceName)
1865 vars := ctx.soongNamespaces[namespaceName]
1866 if replace {
1867 vars = make(map[string]bool)
1868 ctx.soongNamespaces[namespaceName] = vars
1869 }
1870 for _, v := range varNames {
1871 vars[v] = true
1872 }
1873}
1874
1875func (ctx *parseContext) hasNamespaceVar(namespaceName string, varName string) bool {
1876 vars, ok := ctx.soongNamespaces[namespaceName]
1877 if ok {
1878 _, ok = vars[varName]
1879 }
1880 return ok
1881}
1882
Sasha Smundak422b6142021-11-11 18:31:59 -08001883func (ctx *parseContext) errorLocation(node mkparser.Node) ErrorLocation {
1884 return ErrorLocation{ctx.script.mkFile, ctx.script.nodeLocator(node.Pos())}
1885}
1886
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001887func (ss *StarlarkScript) String() string {
1888 return NewGenerateContext(ss).emit()
1889}
1890
1891func (ss *StarlarkScript) SubConfigFiles() []string {
Sasha Smundak6609ba72021-07-22 18:32:56 -07001892
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001893 var subs []string
1894 for _, src := range ss.inherited {
1895 subs = append(subs, src.originalPath)
1896 }
1897 return subs
1898}
1899
1900func (ss *StarlarkScript) HasErrors() bool {
1901 return ss.hasErrors
1902}
1903
1904// Convert reads and parses a makefile. If successful, parsed tree
1905// is returned and then can be passed to String() to get the generated
1906// Starlark file.
1907func Convert(req Request) (*StarlarkScript, error) {
1908 reader := req.Reader
1909 if reader == nil {
1910 mkContents, err := ioutil.ReadFile(req.MkFile)
1911 if err != nil {
1912 return nil, err
1913 }
1914 reader = bytes.NewBuffer(mkContents)
1915 }
1916 parser := mkparser.NewParser(req.MkFile, reader)
1917 nodes, errs := parser.Parse()
1918 if len(errs) > 0 {
1919 for _, e := range errs {
1920 fmt.Fprintln(os.Stderr, "ERROR:", e)
1921 }
1922 return nil, fmt.Errorf("bad makefile %s", req.MkFile)
1923 }
1924 starScript := &StarlarkScript{
Sasha Smundak422b6142021-11-11 18:31:59 -08001925 moduleName: moduleNameForFile(req.MkFile),
1926 mkFile: req.MkFile,
1927 topDir: req.RootDir,
1928 traceCalls: req.TraceCalls,
1929 sourceFS: req.SourceFS,
1930 makefileFinder: req.MakefileFinder,
1931 nodeLocator: func(pos mkparser.Pos) int { return parser.Unpack(pos).Line },
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001932 }
1933 ctx := newParseContext(starScript, nodes)
1934 ctx.outputSuffix = req.OutputSuffix
1935 ctx.outputDir = req.OutputDir
1936 ctx.errorLogger = req.ErrorLogger
1937 if len(req.TracedVariables) > 0 {
1938 ctx.tracedVariables = make(map[string]bool)
1939 for _, v := range req.TracedVariables {
1940 ctx.tracedVariables[v] = true
1941 }
1942 }
1943 ctx.pushReceiver(starScript)
1944 for ctx.hasNodes() && ctx.fatalError == nil {
Cole Faust591a1fe2021-11-08 15:37:57 -08001945 ctx.handleSimpleStatement(ctx.getNode())
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001946 }
1947 if ctx.fatalError != nil {
1948 return nil, ctx.fatalError
1949 }
1950 return starScript, nil
1951}
1952
Cole Faust864028a2021-12-01 13:43:17 -08001953func Launcher(mainModuleUri, inputVariablesUri, mainModuleName string) string {
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001954 var buf bytes.Buffer
1955 fmt.Fprintf(&buf, "load(%q, %q)\n", baseUri, baseName)
Cole Faust864028a2021-12-01 13:43:17 -08001956 fmt.Fprintf(&buf, "load(%q, input_variables_init = \"init\")\n", inputVariablesUri)
Sasha Smundakd7d07ad2021-09-10 15:42:34 -07001957 fmt.Fprintf(&buf, "load(%q, \"init\")\n", mainModuleUri)
Cole Faust864028a2021-12-01 13:43:17 -08001958 fmt.Fprintf(&buf, "%s(%s(%q, init, input_variables_init))\n", cfnPrintVars, cfnMain, mainModuleName)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001959 return buf.String()
1960}
1961
Cole Faust6ed7cb42021-10-07 17:08:46 -07001962func BoardLauncher(mainModuleUri string, inputVariablesUri string) string {
1963 var buf bytes.Buffer
1964 fmt.Fprintf(&buf, "load(%q, %q)\n", baseUri, baseName)
1965 fmt.Fprintf(&buf, "load(%q, \"init\")\n", mainModuleUri)
1966 fmt.Fprintf(&buf, "load(%q, input_variables_init = \"init\")\n", inputVariablesUri)
1967 fmt.Fprintf(&buf, "globals, cfg, globals_base = %s(init, input_variables_init)\n", cfnBoardMain)
1968 fmt.Fprintf(&buf, "# TODO: Some product config variables need to be printed, but most are readonly so we can't just print cfg here.\n")
Cole Faust3c1868b2021-11-22 16:34:11 -08001969 fmt.Fprintf(&buf, "%s((globals, cfg, globals_base))\n", cfnPrintVars)
Cole Faust6ed7cb42021-10-07 17:08:46 -07001970 return buf.String()
1971}
1972
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001973func MakePath2ModuleName(mkPath string) string {
1974 return strings.TrimSuffix(mkPath, filepath.Ext(mkPath))
1975}