Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1 | // Copyright 2021 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // Convert makefile containing device configuration to Starlark file |
| 16 | // The conversion can handle the following constructs in a makefile: |
| 17 | // * comments |
| 18 | // * simple variable assignments |
| 19 | // * $(call init-product,<file>) |
| 20 | // * $(call inherit-product-if-exists |
| 21 | // * if directives |
| 22 | // All other constructs are carried over to the output starlark file as comments. |
| 23 | // |
| 24 | package mk2rbc |
| 25 | |
| 26 | import ( |
| 27 | "bytes" |
| 28 | "fmt" |
| 29 | "io" |
| 30 | "io/ioutil" |
| 31 | "os" |
| 32 | "path/filepath" |
| 33 | "regexp" |
| 34 | "strconv" |
| 35 | "strings" |
| 36 | "text/scanner" |
| 37 | |
| 38 | mkparser "android/soong/androidmk/parser" |
| 39 | ) |
| 40 | |
| 41 | const ( |
| 42 | baseUri = "//build/make/core:product_config.rbc" |
| 43 | // The name of the struct exported by the product_config.rbc |
| 44 | // that contains the functions and variables available to |
| 45 | // product configuration Starlark files. |
| 46 | baseName = "rblf" |
| 47 | |
| 48 | // And here are the functions and variables: |
| 49 | cfnGetCfg = baseName + ".cfg" |
| 50 | cfnMain = baseName + ".product_configuration" |
| 51 | cfnPrintVars = baseName + ".printvars" |
| 52 | cfnWarning = baseName + ".warning" |
| 53 | cfnLocalAppend = baseName + ".local_append" |
| 54 | cfnLocalSetDefault = baseName + ".local_set_default" |
| 55 | cfnInherit = baseName + ".inherit" |
| 56 | cfnSetListDefault = baseName + ".setdefault" |
| 57 | ) |
| 58 | |
| 59 | const ( |
| 60 | // Phony makefile functions, they are eventually rewritten |
| 61 | // according to knownFunctions map |
| 62 | fileExistsPhony = "$file_exists" |
| 63 | wildcardExistsPhony = "$wildcard_exists" |
| 64 | ) |
| 65 | |
| 66 | const ( |
| 67 | callLoadAlways = "inherit-product" |
| 68 | callLoadIf = "inherit-product-if-exists" |
| 69 | ) |
| 70 | |
| 71 | var knownFunctions = map[string]struct { |
| 72 | // The name of the runtime function this function call in makefiles maps to. |
| 73 | // If it starts with !, then this makefile function call is rewritten to |
| 74 | // something else. |
| 75 | runtimeName string |
| 76 | returnType starlarkType |
| 77 | }{ |
| 78 | fileExistsPhony: {baseName + ".file_exists", starlarkTypeBool}, |
| 79 | wildcardExistsPhony: {baseName + ".file_wildcard_exists", starlarkTypeBool}, |
| 80 | "add-to-product-copy-files-if-exists": {baseName + ".copy_if_exists", starlarkTypeList}, |
| 81 | "addprefix": {baseName + ".addprefix", starlarkTypeList}, |
| 82 | "addsuffix": {baseName + ".addsuffix", starlarkTypeList}, |
| 83 | "enforce-product-packages-exist": {baseName + ".enforce_product_packages_exist", starlarkTypeVoid}, |
| 84 | "error": {baseName + ".mkerror", starlarkTypeVoid}, |
| 85 | "findstring": {"!findstring", starlarkTypeInt}, |
| 86 | "find-copy-subdir-files": {baseName + ".find_and_copy", starlarkTypeList}, |
Sasha Smundak | f3e072a | 2021-07-14 12:50:28 -0700 | [diff] [blame] | 87 | "find-word-in-list": {"!find-word-in-list", starlarkTypeUnknown}, // internal macro |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 88 | "filter": {baseName + ".filter", starlarkTypeList}, |
| 89 | "filter-out": {baseName + ".filter_out", starlarkTypeList}, |
Sasha Smundak | f3e072a | 2021-07-14 12:50:28 -0700 | [diff] [blame] | 90 | "get-vendor-board-platforms": {"!get-vendor-board-platforms", starlarkTypeList}, // internal macro, used by is-board-platform, etc. |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 91 | "info": {baseName + ".mkinfo", starlarkTypeVoid}, |
Sasha Smundak | f3e072a | 2021-07-14 12:50:28 -0700 | [diff] [blame] | 92 | "is-android-codename": {"!is-android-codename", starlarkTypeBool}, // unused by product config |
| 93 | "is-android-codename-in-list": {"!is-android-codename-in-list", starlarkTypeBool}, // unused by product config |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 94 | "is-board-platform": {"!is-board-platform", starlarkTypeBool}, |
| 95 | "is-board-platform-in-list": {"!is-board-platform-in-list", starlarkTypeBool}, |
Sasha Smundak | f3e072a | 2021-07-14 12:50:28 -0700 | [diff] [blame] | 96 | "is-chipset-in-board-platform": {"!is-chipset-in-board-platform", starlarkTypeUnknown}, // unused by product config |
| 97 | "is-chipset-prefix-in-board-platform": {"!is-chipset-prefix-in-board-platform", starlarkTypeBool}, // unused by product config |
| 98 | "is-not-board-platform": {"!is-not-board-platform", starlarkTypeBool}, // defined but never used |
| 99 | "is-platform-sdk-version-at-least": {"!is-platform-sdk-version-at-least", starlarkTypeBool}, // unused by product config |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 100 | "is-product-in-list": {"!is-product-in-list", starlarkTypeBool}, |
| 101 | "is-vendor-board-platform": {"!is-vendor-board-platform", starlarkTypeBool}, |
| 102 | callLoadAlways: {"!inherit-product", starlarkTypeVoid}, |
| 103 | callLoadIf: {"!inherit-product-if-exists", starlarkTypeVoid}, |
Sasha Smundak | f3e072a | 2021-07-14 12:50:28 -0700 | [diff] [blame] | 104 | "match-prefix": {"!match-prefix", starlarkTypeUnknown}, // internal macro |
| 105 | "match-word": {"!match-word", starlarkTypeUnknown}, // internal macro |
| 106 | "match-word-in-list": {"!match-word-in-list", starlarkTypeUnknown}, // internal macro |
Sasha Smundak | 94b41c7 | 2021-07-12 18:30:42 -0700 | [diff] [blame] | 107 | "patsubst": {baseName + ".mkpatsubst", starlarkTypeString}, |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 108 | "produce_copy_files": {baseName + ".produce_copy_files", starlarkTypeList}, |
| 109 | "require-artifacts-in-path": {baseName + ".require_artifacts_in_path", starlarkTypeVoid}, |
| 110 | "require-artifacts-in-path-relaxed": {baseName + ".require_artifacts_in_path_relaxed", starlarkTypeVoid}, |
| 111 | // TODO(asmundak): remove it once all calls are removed from configuration makefiles. see b/183161002 |
Sasha Smundak | f3e072a | 2021-07-14 12:50:28 -0700 | [diff] [blame] | 112 | "shell": {baseName + ".shell", starlarkTypeString}, |
| 113 | "strip": {baseName + ".mkstrip", starlarkTypeString}, |
| 114 | "tb-modules": {"!tb-modules", starlarkTypeUnknown}, // defined in hardware/amlogic/tb_modules/tb_detect.mk, unused |
| 115 | "subst": {baseName + ".mksubst", starlarkTypeString}, |
| 116 | "warning": {baseName + ".mkwarning", starlarkTypeVoid}, |
| 117 | "word": {baseName + "!word", starlarkTypeString}, |
| 118 | "wildcard": {baseName + ".expand_wildcard", starlarkTypeList}, |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | var builtinFuncRex = regexp.MustCompile( |
| 122 | "^(addprefix|addsuffix|abspath|and|basename|call|dir|error|eval" + |
| 123 | "|flavor|foreach|file|filter|filter-out|findstring|firstword|guile" + |
| 124 | "|if|info|join|lastword|notdir|or|origin|patsubst|realpath" + |
| 125 | "|shell|sort|strip|subst|suffix|value|warning|word|wordlist|words" + |
| 126 | "|wildcard)") |
| 127 | |
| 128 | // Conversion request parameters |
| 129 | type Request struct { |
| 130 | MkFile string // file to convert |
| 131 | Reader io.Reader // if set, read input from this stream instead |
| 132 | RootDir string // root directory path used to resolve included files |
| 133 | OutputSuffix string // generated Starlark files suffix |
| 134 | OutputDir string // if set, root of the output hierarchy |
| 135 | ErrorLogger ErrorMonitorCB |
| 136 | TracedVariables []string // trace assignment to these variables |
| 137 | TraceCalls bool |
| 138 | WarnPartialSuccess bool |
| 139 | } |
| 140 | |
| 141 | // An error sink allowing to gather error statistics. |
| 142 | // NewError is called on every error encountered during processing. |
| 143 | type ErrorMonitorCB interface { |
| 144 | NewError(s string, node mkparser.Node, args ...interface{}) |
| 145 | } |
| 146 | |
| 147 | // Derives module name for a given file. It is base name |
| 148 | // (file name without suffix), with some characters replaced to make it a Starlark identifier |
| 149 | func moduleNameForFile(mkFile string) string { |
| 150 | base := strings.TrimSuffix(filepath.Base(mkFile), filepath.Ext(mkFile)) |
| 151 | // TODO(asmundak): what else can be in the product file names? |
| 152 | return strings.ReplaceAll(base, "-", "_") |
| 153 | } |
| 154 | |
| 155 | func cloneMakeString(mkString *mkparser.MakeString) *mkparser.MakeString { |
| 156 | r := &mkparser.MakeString{StringPos: mkString.StringPos} |
| 157 | r.Strings = append(r.Strings, mkString.Strings...) |
| 158 | r.Variables = append(r.Variables, mkString.Variables...) |
| 159 | return r |
| 160 | } |
| 161 | |
| 162 | func isMakeControlFunc(s string) bool { |
| 163 | return s == "error" || s == "warning" || s == "info" |
| 164 | } |
| 165 | |
| 166 | // Starlark output generation context |
| 167 | type generationContext struct { |
| 168 | buf strings.Builder |
| 169 | starScript *StarlarkScript |
| 170 | indentLevel int |
| 171 | inAssignment bool |
| 172 | tracedCount int |
| 173 | } |
| 174 | |
| 175 | func NewGenerateContext(ss *StarlarkScript) *generationContext { |
| 176 | return &generationContext{starScript: ss} |
| 177 | } |
| 178 | |
| 179 | // emit returns generated script |
| 180 | func (gctx *generationContext) emit() string { |
| 181 | ss := gctx.starScript |
| 182 | |
| 183 | // The emitted code has the following layout: |
| 184 | // <initial comments> |
| 185 | // preamble, i.e., |
| 186 | // load statement for the runtime support |
| 187 | // load statement for each unique submodule pulled in by this one |
| 188 | // def init(g, handle): |
| 189 | // cfg = rblf.cfg(handle) |
| 190 | // <statements> |
| 191 | // <warning if conversion was not clean> |
| 192 | |
| 193 | iNode := len(ss.nodes) |
| 194 | for i, node := range ss.nodes { |
| 195 | if _, ok := node.(*commentNode); !ok { |
| 196 | iNode = i |
| 197 | break |
| 198 | } |
| 199 | node.emit(gctx) |
| 200 | } |
| 201 | |
| 202 | gctx.emitPreamble() |
| 203 | |
| 204 | gctx.newLine() |
| 205 | // The arguments passed to the init function are the global dictionary |
| 206 | // ('g') and the product configuration dictionary ('cfg') |
| 207 | gctx.write("def init(g, handle):") |
| 208 | gctx.indentLevel++ |
| 209 | if gctx.starScript.traceCalls { |
| 210 | gctx.newLine() |
| 211 | gctx.writef(`print(">%s")`, gctx.starScript.mkFile) |
| 212 | } |
| 213 | gctx.newLine() |
| 214 | gctx.writef("cfg = %s(handle)", cfnGetCfg) |
| 215 | for _, node := range ss.nodes[iNode:] { |
| 216 | node.emit(gctx) |
| 217 | } |
| 218 | |
| 219 | if ss.hasErrors && ss.warnPartialSuccess { |
| 220 | gctx.newLine() |
| 221 | gctx.writef("%s(%q, %q)", cfnWarning, filepath.Base(ss.mkFile), "partially successful conversion") |
| 222 | } |
| 223 | if gctx.starScript.traceCalls { |
| 224 | gctx.newLine() |
| 225 | gctx.writef(`print("<%s")`, gctx.starScript.mkFile) |
| 226 | } |
| 227 | gctx.indentLevel-- |
| 228 | gctx.write("\n") |
| 229 | return gctx.buf.String() |
| 230 | } |
| 231 | |
| 232 | func (gctx *generationContext) emitPreamble() { |
| 233 | gctx.newLine() |
| 234 | gctx.writef("load(%q, %q)", baseUri, baseName) |
| 235 | // Emit exactly one load statement for each URI. |
| 236 | loadedSubConfigs := make(map[string]string) |
| 237 | for _, sc := range gctx.starScript.inherited { |
| 238 | uri := sc.path |
| 239 | if m, ok := loadedSubConfigs[uri]; ok { |
| 240 | // No need to emit load statement, but fix module name. |
| 241 | sc.moduleLocalName = m |
| 242 | continue |
| 243 | } |
| 244 | if !sc.loadAlways { |
| 245 | uri += "|init" |
| 246 | } |
| 247 | gctx.newLine() |
| 248 | gctx.writef("load(%q, %s = \"init\")", uri, sc.entryName()) |
| 249 | loadedSubConfigs[uri] = sc.moduleLocalName |
| 250 | } |
| 251 | gctx.write("\n") |
| 252 | } |
| 253 | |
| 254 | func (gctx *generationContext) emitPass() { |
| 255 | gctx.newLine() |
| 256 | gctx.write("pass") |
| 257 | } |
| 258 | |
| 259 | func (gctx *generationContext) write(ss ...string) { |
| 260 | for _, s := range ss { |
| 261 | gctx.buf.WriteString(s) |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | func (gctx *generationContext) writef(format string, args ...interface{}) { |
| 266 | gctx.write(fmt.Sprintf(format, args...)) |
| 267 | } |
| 268 | |
| 269 | func (gctx *generationContext) newLine() { |
| 270 | if gctx.buf.Len() == 0 { |
| 271 | return |
| 272 | } |
| 273 | gctx.write("\n") |
| 274 | gctx.writef("%*s", 2*gctx.indentLevel, "") |
| 275 | } |
| 276 | |
| 277 | type knownVariable struct { |
| 278 | name string |
| 279 | class varClass |
| 280 | valueType starlarkType |
| 281 | } |
| 282 | |
| 283 | type knownVariables map[string]knownVariable |
| 284 | |
| 285 | func (pcv knownVariables) NewVariable(name string, varClass varClass, valueType starlarkType) { |
| 286 | v, exists := pcv[name] |
| 287 | if !exists { |
| 288 | pcv[name] = knownVariable{name, varClass, valueType} |
| 289 | return |
| 290 | } |
| 291 | // Conflict resolution: |
| 292 | // * config class trumps everything |
| 293 | // * any type trumps unknown type |
| 294 | match := varClass == v.class |
| 295 | if !match { |
| 296 | if varClass == VarClassConfig { |
| 297 | v.class = VarClassConfig |
| 298 | match = true |
| 299 | } else if v.class == VarClassConfig { |
| 300 | match = true |
| 301 | } |
| 302 | } |
| 303 | if valueType != v.valueType { |
| 304 | if valueType != starlarkTypeUnknown { |
| 305 | if v.valueType == starlarkTypeUnknown { |
| 306 | v.valueType = valueType |
| 307 | } else { |
| 308 | match = false |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | if !match { |
| 313 | fmt.Fprintf(os.Stderr, "cannot redefine %s as %v/%v (already defined as %v/%v)\n", |
| 314 | name, varClass, valueType, v.class, v.valueType) |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | // All known product variables. |
| 319 | var KnownVariables = make(knownVariables) |
| 320 | |
| 321 | func init() { |
| 322 | for _, kv := range []string{ |
| 323 | // Kernel-related variables that we know are lists. |
| 324 | "BOARD_VENDOR_KERNEL_MODULES", |
| 325 | "BOARD_VENDOR_RAMDISK_KERNEL_MODULES", |
| 326 | "BOARD_VENDOR_RAMDISK_KERNEL_MODULES_LOAD", |
| 327 | "BOARD_RECOVERY_KERNEL_MODULES", |
| 328 | // Other variables we knwo are lists |
| 329 | "ART_APEX_JARS", |
| 330 | } { |
| 331 | KnownVariables.NewVariable(kv, VarClassSoong, starlarkTypeList) |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | type nodeReceiver interface { |
| 336 | newNode(node starlarkNode) |
| 337 | } |
| 338 | |
| 339 | // Information about the generated Starlark script. |
| 340 | type StarlarkScript struct { |
| 341 | mkFile string |
| 342 | moduleName string |
| 343 | mkPos scanner.Position |
| 344 | nodes []starlarkNode |
| 345 | inherited []*inheritedModule |
| 346 | hasErrors bool |
| 347 | topDir string |
| 348 | traceCalls bool // print enter/exit each init function |
| 349 | warnPartialSuccess bool |
| 350 | } |
| 351 | |
| 352 | func (ss *StarlarkScript) newNode(node starlarkNode) { |
| 353 | ss.nodes = append(ss.nodes, node) |
| 354 | } |
| 355 | |
| 356 | // varAssignmentScope points to the last assignment for each variable |
| 357 | // in the current block. It is used during the parsing to chain |
| 358 | // the assignments to a variable together. |
| 359 | type varAssignmentScope struct { |
| 360 | outer *varAssignmentScope |
| 361 | vars map[string]*assignmentNode |
| 362 | } |
| 363 | |
| 364 | // parseContext holds the script we are generating and all the ephemeral data |
| 365 | // needed during the parsing. |
| 366 | type parseContext struct { |
| 367 | script *StarlarkScript |
| 368 | nodes []mkparser.Node // Makefile as parsed by mkparser |
| 369 | currentNodeIndex int // Node in it we are processing |
| 370 | ifNestLevel int |
| 371 | moduleNameCount map[string]int // count of imported modules with given basename |
| 372 | fatalError error |
| 373 | builtinMakeVars map[string]starlarkExpr |
| 374 | outputSuffix string |
| 375 | errorLogger ErrorMonitorCB |
| 376 | tracedVariables map[string]bool // variables to be traced in the generated script |
| 377 | variables map[string]variable |
| 378 | varAssignments *varAssignmentScope |
| 379 | receiver nodeReceiver // receptacle for the generated starlarkNode's |
| 380 | receiverStack []nodeReceiver |
| 381 | outputDir string |
| 382 | } |
| 383 | |
| 384 | func newParseContext(ss *StarlarkScript, nodes []mkparser.Node) *parseContext { |
| 385 | predefined := []struct{ name, value string }{ |
| 386 | {"SRC_TARGET_DIR", filepath.Join("build", "make", "target")}, |
| 387 | {"LOCAL_PATH", filepath.Dir(ss.mkFile)}, |
| 388 | {"TOPDIR", ss.topDir}, |
| 389 | // TODO(asmundak): maybe read it from build/make/core/envsetup.mk? |
| 390 | {"TARGET_COPY_OUT_SYSTEM", "system"}, |
| 391 | {"TARGET_COPY_OUT_SYSTEM_OTHER", "system_other"}, |
| 392 | {"TARGET_COPY_OUT_DATA", "data"}, |
| 393 | {"TARGET_COPY_OUT_ASAN", filepath.Join("data", "asan")}, |
| 394 | {"TARGET_COPY_OUT_OEM", "oem"}, |
| 395 | {"TARGET_COPY_OUT_RAMDISK", "ramdisk"}, |
| 396 | {"TARGET_COPY_OUT_DEBUG_RAMDISK", "debug_ramdisk"}, |
| 397 | {"TARGET_COPY_OUT_VENDOR_DEBUG_RAMDISK", "vendor_debug_ramdisk"}, |
| 398 | {"TARGET_COPY_OUT_TEST_HARNESS_RAMDISK", "test_harness_ramdisk"}, |
| 399 | {"TARGET_COPY_OUT_ROOT", "root"}, |
| 400 | {"TARGET_COPY_OUT_RECOVERY", "recovery"}, |
| 401 | {"TARGET_COPY_OUT_VENDOR", "||VENDOR-PATH-PH||"}, |
| 402 | {"TARGET_COPY_OUT_VENDOR_RAMDISK", "vendor_ramdisk"}, |
| 403 | {"TARGET_COPY_OUT_PRODUCT", "||PRODUCT-PATH-PH||"}, |
| 404 | {"TARGET_COPY_OUT_PRODUCT_SERVICES", "||PRODUCT-PATH-PH||"}, |
| 405 | {"TARGET_COPY_OUT_SYSTEM_EXT", "||SYSTEM_EXT-PATH-PH||"}, |
| 406 | {"TARGET_COPY_OUT_ODM", "||ODM-PATH-PH||"}, |
| 407 | {"TARGET_COPY_OUT_VENDOR_DLKM", "||VENDOR_DLKM-PATH-PH||"}, |
| 408 | {"TARGET_COPY_OUT_ODM_DLKM", "||ODM_DLKM-PATH-PH||"}, |
| 409 | // TODO(asmundak): to process internal config files, we need the following variables: |
| 410 | // BOARD_CONFIG_VENDOR_PATH |
| 411 | // TARGET_VENDOR |
| 412 | // target_base_product |
| 413 | // |
| 414 | |
| 415 | // the following utility variables are set in build/make/common/core.mk: |
| 416 | {"empty", ""}, |
| 417 | {"space", " "}, |
| 418 | {"comma", ","}, |
| 419 | {"newline", "\n"}, |
| 420 | {"pound", "#"}, |
| 421 | {"backslash", "\\"}, |
| 422 | } |
| 423 | ctx := &parseContext{ |
| 424 | script: ss, |
| 425 | nodes: nodes, |
| 426 | currentNodeIndex: 0, |
| 427 | ifNestLevel: 0, |
| 428 | moduleNameCount: make(map[string]int), |
| 429 | builtinMakeVars: map[string]starlarkExpr{}, |
| 430 | variables: make(map[string]variable), |
| 431 | } |
| 432 | ctx.pushVarAssignments() |
| 433 | for _, item := range predefined { |
| 434 | ctx.variables[item.name] = &predefinedVariable{ |
| 435 | baseVariable: baseVariable{nam: item.name, typ: starlarkTypeString}, |
| 436 | value: &stringLiteralExpr{item.value}, |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | return ctx |
| 441 | } |
| 442 | |
| 443 | func (ctx *parseContext) lastAssignment(name string) *assignmentNode { |
| 444 | for va := ctx.varAssignments; va != nil; va = va.outer { |
| 445 | if v, ok := va.vars[name]; ok { |
| 446 | return v |
| 447 | } |
| 448 | } |
| 449 | return nil |
| 450 | } |
| 451 | |
| 452 | func (ctx *parseContext) setLastAssignment(name string, asgn *assignmentNode) { |
| 453 | ctx.varAssignments.vars[name] = asgn |
| 454 | } |
| 455 | |
| 456 | func (ctx *parseContext) pushVarAssignments() { |
| 457 | va := &varAssignmentScope{ |
| 458 | outer: ctx.varAssignments, |
| 459 | vars: make(map[string]*assignmentNode), |
| 460 | } |
| 461 | ctx.varAssignments = va |
| 462 | } |
| 463 | |
| 464 | func (ctx *parseContext) popVarAssignments() { |
| 465 | ctx.varAssignments = ctx.varAssignments.outer |
| 466 | } |
| 467 | |
| 468 | func (ctx *parseContext) pushReceiver(rcv nodeReceiver) { |
| 469 | ctx.receiverStack = append(ctx.receiverStack, ctx.receiver) |
| 470 | ctx.receiver = rcv |
| 471 | } |
| 472 | |
| 473 | func (ctx *parseContext) popReceiver() { |
| 474 | last := len(ctx.receiverStack) - 1 |
| 475 | if last < 0 { |
| 476 | panic(fmt.Errorf("popReceiver: receiver stack empty")) |
| 477 | } |
| 478 | ctx.receiver = ctx.receiverStack[last] |
| 479 | ctx.receiverStack = ctx.receiverStack[0:last] |
| 480 | } |
| 481 | |
| 482 | func (ctx *parseContext) hasNodes() bool { |
| 483 | return ctx.currentNodeIndex < len(ctx.nodes) |
| 484 | } |
| 485 | |
| 486 | func (ctx *parseContext) getNode() mkparser.Node { |
| 487 | if !ctx.hasNodes() { |
| 488 | return nil |
| 489 | } |
| 490 | node := ctx.nodes[ctx.currentNodeIndex] |
| 491 | ctx.currentNodeIndex++ |
| 492 | return node |
| 493 | } |
| 494 | |
| 495 | func (ctx *parseContext) backNode() { |
| 496 | if ctx.currentNodeIndex <= 0 { |
| 497 | panic("Cannot back off") |
| 498 | } |
| 499 | ctx.currentNodeIndex-- |
| 500 | } |
| 501 | |
| 502 | func (ctx *parseContext) handleAssignment(a *mkparser.Assignment) { |
| 503 | // Handle only simple variables |
| 504 | if !a.Name.Const() { |
| 505 | ctx.errorf(a, "Only simple variables are handled") |
| 506 | return |
| 507 | } |
| 508 | name := a.Name.Strings[0] |
| 509 | lhs := ctx.addVariable(name) |
| 510 | if lhs == nil { |
| 511 | ctx.errorf(a, "unknown variable %s", name) |
| 512 | return |
| 513 | } |
| 514 | _, isTraced := ctx.tracedVariables[name] |
| 515 | asgn := &assignmentNode{lhs: lhs, mkValue: a.Value, isTraced: isTraced} |
| 516 | if lhs.valueType() == starlarkTypeUnknown { |
| 517 | // Try to divine variable type from the RHS |
| 518 | asgn.value = ctx.parseMakeString(a, a.Value) |
| 519 | if xBad, ok := asgn.value.(*badExpr); ok { |
| 520 | ctx.wrapBadExpr(xBad) |
| 521 | return |
| 522 | } |
| 523 | inferred_type := asgn.value.typ() |
| 524 | if inferred_type != starlarkTypeUnknown { |
Sasha Smundak | 9d011ab | 2021-07-09 16:00:57 -0700 | [diff] [blame] | 525 | lhs.setValueType(inferred_type) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | if lhs.valueType() == starlarkTypeList { |
| 529 | xConcat := ctx.buildConcatExpr(a) |
| 530 | if xConcat == nil { |
| 531 | return |
| 532 | } |
| 533 | switch len(xConcat.items) { |
| 534 | case 0: |
| 535 | asgn.value = &listExpr{} |
| 536 | case 1: |
| 537 | asgn.value = xConcat.items[0] |
| 538 | default: |
| 539 | asgn.value = xConcat |
| 540 | } |
| 541 | } else { |
| 542 | asgn.value = ctx.parseMakeString(a, a.Value) |
| 543 | if xBad, ok := asgn.value.(*badExpr); ok { |
| 544 | ctx.wrapBadExpr(xBad) |
| 545 | return |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | // TODO(asmundak): move evaluation to a separate pass |
| 550 | asgn.value, _ = asgn.value.eval(ctx.builtinMakeVars) |
| 551 | |
| 552 | asgn.previous = ctx.lastAssignment(name) |
| 553 | ctx.setLastAssignment(name, asgn) |
| 554 | switch a.Type { |
| 555 | case "=", ":=": |
| 556 | asgn.flavor = asgnSet |
| 557 | case "+=": |
| 558 | if asgn.previous == nil && !asgn.lhs.isPreset() { |
| 559 | asgn.flavor = asgnMaybeAppend |
| 560 | } else { |
| 561 | asgn.flavor = asgnAppend |
| 562 | } |
| 563 | case "?=": |
| 564 | asgn.flavor = asgnMaybeSet |
| 565 | default: |
| 566 | panic(fmt.Errorf("unexpected assignment type %s", a.Type)) |
| 567 | } |
| 568 | |
| 569 | ctx.receiver.newNode(asgn) |
| 570 | } |
| 571 | |
| 572 | func (ctx *parseContext) buildConcatExpr(a *mkparser.Assignment) *concatExpr { |
| 573 | xConcat := &concatExpr{} |
| 574 | var xItemList *listExpr |
| 575 | addToItemList := func(x ...starlarkExpr) { |
| 576 | if xItemList == nil { |
| 577 | xItemList = &listExpr{[]starlarkExpr{}} |
| 578 | } |
| 579 | xItemList.items = append(xItemList.items, x...) |
| 580 | } |
| 581 | finishItemList := func() { |
| 582 | if xItemList != nil { |
| 583 | xConcat.items = append(xConcat.items, xItemList) |
| 584 | xItemList = nil |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | items := a.Value.Words() |
| 589 | for _, item := range items { |
| 590 | // A function call in RHS is supposed to return a list, all other item |
| 591 | // expressions return individual elements. |
| 592 | switch x := ctx.parseMakeString(a, item).(type) { |
| 593 | case *badExpr: |
| 594 | ctx.wrapBadExpr(x) |
| 595 | return nil |
| 596 | case *stringLiteralExpr: |
| 597 | addToItemList(maybeConvertToStringList(x).(*listExpr).items...) |
| 598 | default: |
| 599 | switch x.typ() { |
| 600 | case starlarkTypeList: |
| 601 | finishItemList() |
| 602 | xConcat.items = append(xConcat.items, x) |
| 603 | case starlarkTypeString: |
| 604 | finishItemList() |
| 605 | xConcat.items = append(xConcat.items, &callExpr{ |
| 606 | object: x, |
| 607 | name: "split", |
| 608 | args: nil, |
| 609 | returnType: starlarkTypeList, |
| 610 | }) |
| 611 | default: |
| 612 | addToItemList(x) |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | if xItemList != nil { |
| 617 | xConcat.items = append(xConcat.items, xItemList) |
| 618 | } |
| 619 | return xConcat |
| 620 | } |
| 621 | |
| 622 | func (ctx *parseContext) newInheritedModule(v mkparser.Node, pathExpr starlarkExpr, loadAlways bool) *inheritedModule { |
| 623 | var path string |
| 624 | x, _ := pathExpr.eval(ctx.builtinMakeVars) |
| 625 | s, ok := x.(*stringLiteralExpr) |
| 626 | if !ok { |
| 627 | ctx.errorf(v, "inherit-product/include argument is too complex") |
| 628 | return nil |
| 629 | } |
| 630 | |
| 631 | path = s.literal |
| 632 | moduleName := moduleNameForFile(path) |
| 633 | moduleLocalName := "_" + moduleName |
| 634 | n, found := ctx.moduleNameCount[moduleName] |
| 635 | if found { |
| 636 | moduleLocalName += fmt.Sprintf("%d", n) |
| 637 | } |
| 638 | ctx.moduleNameCount[moduleName] = n + 1 |
| 639 | ln := &inheritedModule{ |
| 640 | path: ctx.loadedModulePath(path), |
| 641 | originalPath: path, |
| 642 | moduleName: moduleName, |
| 643 | moduleLocalName: moduleLocalName, |
| 644 | loadAlways: loadAlways, |
| 645 | } |
| 646 | ctx.script.inherited = append(ctx.script.inherited, ln) |
| 647 | return ln |
| 648 | } |
| 649 | |
| 650 | func (ctx *parseContext) handleInheritModule(v mkparser.Node, pathExpr starlarkExpr, loadAlways bool) { |
| 651 | if im := ctx.newInheritedModule(v, pathExpr, loadAlways); im != nil { |
| 652 | ctx.receiver.newNode(&inheritNode{im}) |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | func (ctx *parseContext) handleInclude(v mkparser.Node, pathExpr starlarkExpr, loadAlways bool) { |
| 657 | if ln := ctx.newInheritedModule(v, pathExpr, loadAlways); ln != nil { |
| 658 | ctx.receiver.newNode(&includeNode{ln}) |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | func (ctx *parseContext) handleVariable(v *mkparser.Variable) { |
| 663 | // Handle: |
| 664 | // $(call inherit-product,...) |
| 665 | // $(call inherit-product-if-exists,...) |
| 666 | // $(info xxx) |
| 667 | // $(warning xxx) |
| 668 | // $(error xxx) |
| 669 | expr := ctx.parseReference(v, v.Name) |
| 670 | switch x := expr.(type) { |
| 671 | case *callExpr: |
| 672 | if x.name == callLoadAlways || x.name == callLoadIf { |
| 673 | ctx.handleInheritModule(v, x.args[0], x.name == callLoadAlways) |
| 674 | } else if isMakeControlFunc(x.name) { |
| 675 | // File name is the first argument |
| 676 | args := []starlarkExpr{ |
| 677 | &stringLiteralExpr{ctx.script.mkFile}, |
| 678 | x.args[0], |
| 679 | } |
| 680 | ctx.receiver.newNode(&exprNode{ |
| 681 | &callExpr{name: x.name, args: args, returnType: starlarkTypeUnknown}, |
| 682 | }) |
| 683 | } else { |
| 684 | ctx.receiver.newNode(&exprNode{expr}) |
| 685 | } |
| 686 | case *badExpr: |
| 687 | ctx.wrapBadExpr(x) |
| 688 | return |
| 689 | default: |
| 690 | ctx.errorf(v, "cannot handle %s", v.Dump()) |
| 691 | return |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | func (ctx *parseContext) handleDefine(directive *mkparser.Directive) { |
Sasha Smundak | f3e072a | 2021-07-14 12:50:28 -0700 | [diff] [blame] | 696 | macro_name := strings.Fields(directive.Args.Strings[0])[0] |
| 697 | // Ignore the macros that we handle |
| 698 | if _, ok := knownFunctions[macro_name]; !ok { |
| 699 | ctx.errorf(directive, "define is not supported: %s", macro_name) |
| 700 | } |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | func (ctx *parseContext) handleIfBlock(ifDirective *mkparser.Directive) { |
| 704 | ssSwitch := &switchNode{} |
| 705 | ctx.pushReceiver(ssSwitch) |
| 706 | for ctx.processBranch(ifDirective); ctx.hasNodes() && ctx.fatalError == nil; { |
| 707 | node := ctx.getNode() |
| 708 | switch x := node.(type) { |
| 709 | case *mkparser.Directive: |
| 710 | switch x.Name { |
| 711 | case "else", "elifdef", "elifndef", "elifeq", "elifneq": |
| 712 | ctx.processBranch(x) |
| 713 | case "endif": |
| 714 | ctx.popReceiver() |
| 715 | ctx.receiver.newNode(ssSwitch) |
| 716 | return |
| 717 | default: |
| 718 | ctx.errorf(node, "unexpected directive %s", x.Name) |
| 719 | } |
| 720 | default: |
| 721 | ctx.errorf(ifDirective, "unexpected statement") |
| 722 | } |
| 723 | } |
| 724 | if ctx.fatalError == nil { |
| 725 | ctx.fatalError = fmt.Errorf("no matching endif for %s", ifDirective.Dump()) |
| 726 | } |
| 727 | ctx.popReceiver() |
| 728 | } |
| 729 | |
| 730 | // processBranch processes a single branch (if/elseif/else) until the next directive |
| 731 | // on the same level. |
| 732 | func (ctx *parseContext) processBranch(check *mkparser.Directive) { |
| 733 | block := switchCase{gate: ctx.parseCondition(check)} |
| 734 | defer func() { |
| 735 | ctx.popVarAssignments() |
| 736 | ctx.ifNestLevel-- |
| 737 | |
| 738 | }() |
| 739 | ctx.pushVarAssignments() |
| 740 | ctx.ifNestLevel++ |
| 741 | |
| 742 | ctx.pushReceiver(&block) |
| 743 | for ctx.hasNodes() { |
| 744 | node := ctx.getNode() |
| 745 | if ctx.handleSimpleStatement(node) { |
| 746 | continue |
| 747 | } |
| 748 | switch d := node.(type) { |
| 749 | case *mkparser.Directive: |
| 750 | switch d.Name { |
| 751 | case "else", "elifdef", "elifndef", "elifeq", "elifneq", "endif": |
| 752 | ctx.popReceiver() |
| 753 | ctx.receiver.newNode(&block) |
| 754 | ctx.backNode() |
| 755 | return |
| 756 | case "ifdef", "ifndef", "ifeq", "ifneq": |
| 757 | ctx.handleIfBlock(d) |
| 758 | default: |
| 759 | ctx.errorf(d, "unexpected directive %s", d.Name) |
| 760 | } |
| 761 | default: |
| 762 | ctx.errorf(node, "unexpected statement") |
| 763 | } |
| 764 | } |
| 765 | ctx.fatalError = fmt.Errorf("no matching endif for %s", check.Dump()) |
| 766 | ctx.popReceiver() |
| 767 | } |
| 768 | |
| 769 | func (ctx *parseContext) newIfDefinedNode(check *mkparser.Directive) (starlarkExpr, bool) { |
| 770 | if !check.Args.Const() { |
| 771 | return ctx.newBadExpr(check, "ifdef variable ref too complex: %s", check.Args.Dump()), false |
| 772 | } |
| 773 | v := ctx.addVariable(check.Args.Strings[0]) |
| 774 | return &variableDefinedExpr{v}, true |
| 775 | } |
| 776 | |
| 777 | func (ctx *parseContext) parseCondition(check *mkparser.Directive) starlarkNode { |
| 778 | switch check.Name { |
| 779 | case "ifdef", "ifndef", "elifdef", "elifndef": |
| 780 | v, ok := ctx.newIfDefinedNode(check) |
| 781 | if ok && strings.HasSuffix(check.Name, "ndef") { |
| 782 | v = ¬Expr{v} |
| 783 | } |
| 784 | return &ifNode{ |
| 785 | isElif: strings.HasPrefix(check.Name, "elif"), |
| 786 | expr: v, |
| 787 | } |
| 788 | case "ifeq", "ifneq", "elifeq", "elifneq": |
| 789 | return &ifNode{ |
| 790 | isElif: strings.HasPrefix(check.Name, "elif"), |
| 791 | expr: ctx.parseCompare(check), |
| 792 | } |
| 793 | case "else": |
| 794 | return &elseNode{} |
| 795 | default: |
| 796 | panic(fmt.Errorf("%s: unknown directive: %s", ctx.script.mkFile, check.Dump())) |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | func (ctx *parseContext) newBadExpr(node mkparser.Node, text string, args ...interface{}) starlarkExpr { |
| 801 | message := fmt.Sprintf(text, args...) |
| 802 | if ctx.errorLogger != nil { |
| 803 | ctx.errorLogger.NewError(text, node, args) |
| 804 | } |
| 805 | ctx.script.hasErrors = true |
| 806 | return &badExpr{node, message} |
| 807 | } |
| 808 | |
| 809 | func (ctx *parseContext) parseCompare(cond *mkparser.Directive) starlarkExpr { |
| 810 | // Strip outer parentheses |
| 811 | mkArg := cloneMakeString(cond.Args) |
| 812 | mkArg.Strings[0] = strings.TrimLeft(mkArg.Strings[0], "( ") |
| 813 | n := len(mkArg.Strings) |
| 814 | mkArg.Strings[n-1] = strings.TrimRight(mkArg.Strings[n-1], ") ") |
| 815 | args := mkArg.Split(",") |
| 816 | // TODO(asmundak): handle the case where the arguments are in quotes and space-separated |
| 817 | if len(args) != 2 { |
| 818 | return ctx.newBadExpr(cond, "ifeq/ifneq len(args) != 2 %s", cond.Dump()) |
| 819 | } |
| 820 | args[0].TrimRightSpaces() |
| 821 | args[1].TrimLeftSpaces() |
| 822 | |
| 823 | isEq := !strings.HasSuffix(cond.Name, "neq") |
| 824 | switch xLeft := ctx.parseMakeString(cond, args[0]).(type) { |
| 825 | case *stringLiteralExpr, *variableRefExpr: |
| 826 | switch xRight := ctx.parseMakeString(cond, args[1]).(type) { |
| 827 | case *stringLiteralExpr, *variableRefExpr: |
| 828 | return &eqExpr{left: xLeft, right: xRight, isEq: isEq} |
| 829 | case *badExpr: |
| 830 | return xRight |
| 831 | default: |
| 832 | expr, ok := ctx.parseCheckFunctionCallResult(cond, xLeft, args[1]) |
| 833 | if ok { |
| 834 | return expr |
| 835 | } |
| 836 | return ctx.newBadExpr(cond, "right operand is too complex: %s", args[1].Dump()) |
| 837 | } |
| 838 | case *badExpr: |
| 839 | return xLeft |
| 840 | default: |
| 841 | switch xRight := ctx.parseMakeString(cond, args[1]).(type) { |
| 842 | case *stringLiteralExpr, *variableRefExpr: |
| 843 | expr, ok := ctx.parseCheckFunctionCallResult(cond, xRight, args[0]) |
| 844 | if ok { |
| 845 | return expr |
| 846 | } |
| 847 | return ctx.newBadExpr(cond, "left operand is too complex: %s", args[0].Dump()) |
| 848 | case *badExpr: |
| 849 | return xRight |
| 850 | default: |
| 851 | return ctx.newBadExpr(cond, "operands are too complex: (%s,%s)", args[0].Dump(), args[1].Dump()) |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | func (ctx *parseContext) parseCheckFunctionCallResult(directive *mkparser.Directive, xValue starlarkExpr, |
| 857 | varArg *mkparser.MakeString) (starlarkExpr, bool) { |
| 858 | mkSingleVar, ok := varArg.SingleVariable() |
| 859 | if !ok { |
| 860 | return nil, false |
| 861 | } |
| 862 | expr := ctx.parseReference(directive, mkSingleVar) |
| 863 | negate := strings.HasSuffix(directive.Name, "neq") |
| 864 | checkIsSomethingFunction := func(xCall *callExpr) starlarkExpr { |
| 865 | s, ok := maybeString(xValue) |
| 866 | if !ok || s != "true" { |
| 867 | return ctx.newBadExpr(directive, |
| 868 | fmt.Sprintf("the result of %s can be compared only to 'true'", xCall.name)) |
| 869 | } |
| 870 | if len(xCall.args) < 1 { |
| 871 | return ctx.newBadExpr(directive, "%s requires an argument", xCall.name) |
| 872 | } |
| 873 | return nil |
| 874 | } |
| 875 | switch x := expr.(type) { |
| 876 | case *callExpr: |
| 877 | switch x.name { |
| 878 | case "filter": |
| 879 | return ctx.parseCompareFilterFuncResult(directive, x, xValue, !negate), true |
| 880 | case "filter-out": |
| 881 | return ctx.parseCompareFilterFuncResult(directive, x, xValue, negate), true |
| 882 | case "wildcard": |
| 883 | return ctx.parseCompareWildcardFuncResult(directive, x, xValue, negate), true |
| 884 | case "findstring": |
| 885 | return ctx.parseCheckFindstringFuncResult(directive, x, xValue, negate), true |
| 886 | case "strip": |
| 887 | return ctx.parseCompareStripFuncResult(directive, x, xValue, negate), true |
| 888 | case "is-board-platform": |
| 889 | if xBad := checkIsSomethingFunction(x); xBad != nil { |
| 890 | return xBad, true |
| 891 | } |
| 892 | return &eqExpr{ |
| 893 | left: &variableRefExpr{ctx.addVariable("TARGET_BOARD_PLATFORM"), false}, |
| 894 | right: x.args[0], |
| 895 | isEq: !negate, |
| 896 | }, true |
| 897 | case "is-board-platform-in-list": |
| 898 | if xBad := checkIsSomethingFunction(x); xBad != nil { |
| 899 | return xBad, true |
| 900 | } |
| 901 | return &inExpr{ |
| 902 | expr: &variableRefExpr{ctx.addVariable("TARGET_BOARD_PLATFORM"), false}, |
| 903 | list: maybeConvertToStringList(x.args[0]), |
| 904 | isNot: negate, |
| 905 | }, true |
| 906 | case "is-product-in-list": |
| 907 | if xBad := checkIsSomethingFunction(x); xBad != nil { |
| 908 | return xBad, true |
| 909 | } |
| 910 | return &inExpr{ |
| 911 | expr: &variableRefExpr{ctx.addVariable("TARGET_PRODUCT"), true}, |
| 912 | list: maybeConvertToStringList(x.args[0]), |
| 913 | isNot: negate, |
| 914 | }, true |
| 915 | case "is-vendor-board-platform": |
| 916 | if xBad := checkIsSomethingFunction(x); xBad != nil { |
| 917 | return xBad, true |
| 918 | } |
| 919 | s, ok := maybeString(x.args[0]) |
| 920 | if !ok { |
| 921 | return ctx.newBadExpr(directive, "cannot handle non-constant argument to is-vendor-board-platform"), true |
| 922 | } |
| 923 | return &inExpr{ |
| 924 | expr: &variableRefExpr{ctx.addVariable("TARGET_BOARD_PLATFORM"), false}, |
| 925 | list: &variableRefExpr{ctx.addVariable(s + "_BOARD_PLATFORMS"), true}, |
| 926 | isNot: negate, |
| 927 | }, true |
| 928 | default: |
| 929 | return ctx.newBadExpr(directive, "Unknown function in ifeq: %s", x.name), true |
| 930 | } |
| 931 | case *badExpr: |
| 932 | return x, true |
| 933 | default: |
| 934 | return nil, false |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | func (ctx *parseContext) parseCompareFilterFuncResult(cond *mkparser.Directive, |
| 939 | filterFuncCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr { |
| 940 | // We handle: |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame^] | 941 | // * ifeq/ifneq (,$(filter v1 v2 ..., EXPR) becomes if EXPR not in/in ["v1", "v2", ...] |
| 942 | // * ifeq/ifneq (,$(filter EXPR, v1 v2 ...) becomes if EXPR not in/in ["v1", "v2", ...] |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 943 | // * ifeq/ifneq ($(VAR),$(filter $(VAR), v1 v2 ...) becomes if VAR in/not in ["v1", "v2"] |
| 944 | // TODO(Asmundak): check the last case works for filter-out, too. |
| 945 | xPattern := filterFuncCall.args[0] |
| 946 | xText := filterFuncCall.args[1] |
| 947 | var xInList *stringLiteralExpr |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame^] | 948 | var expr starlarkExpr |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 949 | var ok bool |
| 950 | switch x := xValue.(type) { |
| 951 | case *stringLiteralExpr: |
| 952 | if x.literal != "" { |
| 953 | return ctx.newBadExpr(cond, "filter comparison to non-empty value: %s", xValue) |
| 954 | } |
| 955 | // Either pattern or text should be const, and the |
| 956 | // non-const one should be varRefExpr |
| 957 | if xInList, ok = xPattern.(*stringLiteralExpr); ok { |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame^] | 958 | expr = xText |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 959 | } else if xInList, ok = xText.(*stringLiteralExpr); ok { |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame^] | 960 | expr = xPattern |
| 961 | } else { |
| 962 | return &callExpr{ |
| 963 | object: nil, |
| 964 | name: filterFuncCall.name, |
| 965 | args: filterFuncCall.args, |
| 966 | returnType: starlarkTypeBool, |
| 967 | } |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 968 | } |
| 969 | case *variableRefExpr: |
| 970 | if v, ok := xPattern.(*variableRefExpr); ok { |
| 971 | if xInList, ok = xText.(*stringLiteralExpr); ok && v.ref.name() == x.ref.name() { |
| 972 | // ifeq/ifneq ($(VAR),$(filter $(VAR), v1 v2 ...), flip negate, |
| 973 | // it's the opposite to what is done when comparing to empty. |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame^] | 974 | expr = xPattern |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 975 | negate = !negate |
| 976 | } |
| 977 | } |
| 978 | } |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame^] | 979 | if expr != nil && xInList != nil { |
| 980 | slExpr := newStringListExpr(strings.Fields(xInList.literal)) |
| 981 | // Generate simpler code for the common cases: |
| 982 | if expr.typ() == starlarkTypeList { |
| 983 | if len(slExpr.items) == 1 { |
| 984 | // Checking that a string belongs to list |
| 985 | return &inExpr{isNot: negate, list: expr, expr: slExpr.items[0]} |
| 986 | } else { |
| 987 | // TODO(asmundak): |
| 988 | panic("TBD") |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 989 | } |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame^] | 990 | } else if len(slExpr.items) == 1 { |
| 991 | return &eqExpr{left: expr, right: slExpr.items[0], isEq: !negate} |
| 992 | } else { |
| 993 | return &inExpr{isNot: negate, list: newStringListExpr(strings.Fields(xInList.literal)), expr: expr} |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | return ctx.newBadExpr(cond, "filter arguments are too complex: %s", cond.Dump()) |
| 997 | } |
| 998 | |
| 999 | func (ctx *parseContext) parseCompareWildcardFuncResult(directive *mkparser.Directive, |
| 1000 | xCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr { |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame^] | 1001 | if !isEmptyString(xValue) { |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1002 | return ctx.newBadExpr(directive, "wildcard result can be compared only to empty: %s", xValue) |
| 1003 | } |
| 1004 | callFunc := wildcardExistsPhony |
| 1005 | if s, ok := xCall.args[0].(*stringLiteralExpr); ok && !strings.ContainsAny(s.literal, "*?{[") { |
| 1006 | callFunc = fileExistsPhony |
| 1007 | } |
| 1008 | var cc starlarkExpr = &callExpr{name: callFunc, args: xCall.args, returnType: starlarkTypeBool} |
| 1009 | if !negate { |
| 1010 | cc = ¬Expr{cc} |
| 1011 | } |
| 1012 | return cc |
| 1013 | } |
| 1014 | |
| 1015 | func (ctx *parseContext) parseCheckFindstringFuncResult(directive *mkparser.Directive, |
| 1016 | xCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr { |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame^] | 1017 | if isEmptyString(xValue) { |
| 1018 | return &eqExpr{ |
| 1019 | left: &callExpr{ |
| 1020 | object: xCall.args[1], |
| 1021 | name: "find", |
| 1022 | args: []starlarkExpr{xCall.args[0]}, |
| 1023 | returnType: starlarkTypeInt, |
| 1024 | }, |
| 1025 | right: &intLiteralExpr{-1}, |
| 1026 | isEq: !negate, |
| 1027 | } |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1028 | } |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame^] | 1029 | return ctx.newBadExpr(directive, "findstring result can be compared only to empty: %s", xValue) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | func (ctx *parseContext) parseCompareStripFuncResult(directive *mkparser.Directive, |
| 1033 | xCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr { |
| 1034 | if _, ok := xValue.(*stringLiteralExpr); !ok { |
| 1035 | return ctx.newBadExpr(directive, "strip result can be compared only to string: %s", xValue) |
| 1036 | } |
| 1037 | return &eqExpr{ |
| 1038 | left: &callExpr{ |
| 1039 | name: "strip", |
| 1040 | args: xCall.args, |
| 1041 | returnType: starlarkTypeString, |
| 1042 | }, |
| 1043 | right: xValue, isEq: !negate} |
| 1044 | } |
| 1045 | |
| 1046 | // parses $(...), returning an expression |
| 1047 | func (ctx *parseContext) parseReference(node mkparser.Node, ref *mkparser.MakeString) starlarkExpr { |
| 1048 | ref.TrimLeftSpaces() |
| 1049 | ref.TrimRightSpaces() |
| 1050 | refDump := ref.Dump() |
| 1051 | |
| 1052 | // Handle only the case where the first (or only) word is constant |
| 1053 | words := ref.SplitN(" ", 2) |
| 1054 | if !words[0].Const() { |
| 1055 | return ctx.newBadExpr(node, "reference is too complex: %s", refDump) |
| 1056 | } |
| 1057 | |
| 1058 | // If it is a single word, it can be a simple variable |
| 1059 | // reference or a function call |
| 1060 | if len(words) == 1 { |
| 1061 | if isMakeControlFunc(refDump) || refDump == "shell" { |
| 1062 | return &callExpr{ |
| 1063 | name: refDump, |
| 1064 | args: []starlarkExpr{&stringLiteralExpr{""}}, |
| 1065 | returnType: starlarkTypeUnknown, |
| 1066 | } |
| 1067 | } |
| 1068 | if v := ctx.addVariable(refDump); v != nil { |
| 1069 | return &variableRefExpr{v, ctx.lastAssignment(v.name()) != nil} |
| 1070 | } |
| 1071 | return ctx.newBadExpr(node, "unknown variable %s", refDump) |
| 1072 | } |
| 1073 | |
| 1074 | expr := &callExpr{name: words[0].Dump(), returnType: starlarkTypeUnknown} |
| 1075 | args := words[1] |
| 1076 | args.TrimLeftSpaces() |
| 1077 | // Make control functions and shell need special treatment as everything |
| 1078 | // after the name is a single text argument |
| 1079 | if isMakeControlFunc(expr.name) || expr.name == "shell" { |
| 1080 | x := ctx.parseMakeString(node, args) |
| 1081 | if xBad, ok := x.(*badExpr); ok { |
| 1082 | return xBad |
| 1083 | } |
| 1084 | expr.args = []starlarkExpr{x} |
| 1085 | return expr |
| 1086 | } |
| 1087 | if expr.name == "call" { |
| 1088 | words = args.SplitN(",", 2) |
| 1089 | if words[0].Empty() || !words[0].Const() { |
Sasha Smundak | f2c9f8b | 2021-07-27 10:44:48 -0700 | [diff] [blame] | 1090 | return ctx.newBadExpr(node, "cannot handle %s", refDump) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1091 | } |
| 1092 | expr.name = words[0].Dump() |
| 1093 | if len(words) < 2 { |
| 1094 | return expr |
| 1095 | } |
| 1096 | args = words[1] |
| 1097 | } |
| 1098 | if kf, found := knownFunctions[expr.name]; found { |
| 1099 | expr.returnType = kf.returnType |
| 1100 | } else { |
| 1101 | return ctx.newBadExpr(node, "cannot handle invoking %s", expr.name) |
| 1102 | } |
| 1103 | switch expr.name { |
| 1104 | case "word": |
| 1105 | return ctx.parseWordFunc(node, args) |
Sasha Smundak | 94b41c7 | 2021-07-12 18:30:42 -0700 | [diff] [blame] | 1106 | case "subst", "patsubst": |
| 1107 | return ctx.parseSubstFunc(node, expr.name, args) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1108 | default: |
| 1109 | for _, arg := range args.Split(",") { |
| 1110 | arg.TrimLeftSpaces() |
| 1111 | arg.TrimRightSpaces() |
| 1112 | x := ctx.parseMakeString(node, arg) |
| 1113 | if xBad, ok := x.(*badExpr); ok { |
| 1114 | return xBad |
| 1115 | } |
| 1116 | expr.args = append(expr.args, x) |
| 1117 | } |
| 1118 | } |
| 1119 | return expr |
| 1120 | } |
| 1121 | |
Sasha Smundak | 94b41c7 | 2021-07-12 18:30:42 -0700 | [diff] [blame] | 1122 | func (ctx *parseContext) parseSubstFunc(node mkparser.Node, fname string, args *mkparser.MakeString) starlarkExpr { |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1123 | words := args.Split(",") |
| 1124 | if len(words) != 3 { |
Sasha Smundak | 94b41c7 | 2021-07-12 18:30:42 -0700 | [diff] [blame] | 1125 | return ctx.newBadExpr(node, "%s function should have 3 arguments", fname) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1126 | } |
| 1127 | if !words[0].Const() || !words[1].Const() { |
Sasha Smundak | 94b41c7 | 2021-07-12 18:30:42 -0700 | [diff] [blame] | 1128 | return ctx.newBadExpr(node, "%s function's from and to arguments should be constant", fname) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1129 | } |
| 1130 | from := words[0].Strings[0] |
| 1131 | to := words[1].Strings[0] |
| 1132 | words[2].TrimLeftSpaces() |
| 1133 | words[2].TrimRightSpaces() |
| 1134 | obj := ctx.parseMakeString(node, words[2]) |
Sasha Smundak | 9d011ab | 2021-07-09 16:00:57 -0700 | [diff] [blame] | 1135 | typ := obj.typ() |
Sasha Smundak | 94b41c7 | 2021-07-12 18:30:42 -0700 | [diff] [blame] | 1136 | if typ == starlarkTypeString && fname == "subst" { |
| 1137 | // Optimization: if it's $(subst from, to, string), emit string.replace(from, to) |
Sasha Smundak | 9d011ab | 2021-07-09 16:00:57 -0700 | [diff] [blame] | 1138 | return &callExpr{ |
| 1139 | object: obj, |
| 1140 | name: "replace", |
| 1141 | args: []starlarkExpr{&stringLiteralExpr{from}, &stringLiteralExpr{to}}, |
| 1142 | returnType: typ, |
| 1143 | } |
| 1144 | } |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1145 | return &callExpr{ |
Sasha Smundak | 94b41c7 | 2021-07-12 18:30:42 -0700 | [diff] [blame] | 1146 | name: fname, |
Sasha Smundak | 9d011ab | 2021-07-09 16:00:57 -0700 | [diff] [blame] | 1147 | args: []starlarkExpr{&stringLiteralExpr{from}, &stringLiteralExpr{to}, obj}, |
| 1148 | returnType: obj.typ(), |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | func (ctx *parseContext) parseWordFunc(node mkparser.Node, args *mkparser.MakeString) starlarkExpr { |
| 1153 | words := args.Split(",") |
| 1154 | if len(words) != 2 { |
| 1155 | return ctx.newBadExpr(node, "word function should have 2 arguments") |
| 1156 | } |
| 1157 | var index uint64 = 0 |
| 1158 | if words[0].Const() { |
| 1159 | index, _ = strconv.ParseUint(strings.TrimSpace(words[0].Strings[0]), 10, 64) |
| 1160 | } |
| 1161 | if index < 1 { |
| 1162 | return ctx.newBadExpr(node, "word index should be constant positive integer") |
| 1163 | } |
| 1164 | words[1].TrimLeftSpaces() |
| 1165 | words[1].TrimRightSpaces() |
| 1166 | array := ctx.parseMakeString(node, words[1]) |
| 1167 | if xBad, ok := array.(*badExpr); ok { |
| 1168 | return xBad |
| 1169 | } |
| 1170 | if array.typ() != starlarkTypeList { |
| 1171 | array = &callExpr{object: array, name: "split", returnType: starlarkTypeList} |
| 1172 | } |
| 1173 | return indexExpr{array, &intLiteralExpr{int(index - 1)}} |
| 1174 | } |
| 1175 | |
| 1176 | func (ctx *parseContext) parseMakeString(node mkparser.Node, mk *mkparser.MakeString) starlarkExpr { |
| 1177 | if mk.Const() { |
| 1178 | return &stringLiteralExpr{mk.Dump()} |
| 1179 | } |
| 1180 | if mkRef, ok := mk.SingleVariable(); ok { |
| 1181 | return ctx.parseReference(node, mkRef) |
| 1182 | } |
| 1183 | // If we reached here, it's neither string literal nor a simple variable, |
| 1184 | // we need a full-blown interpolation node that will generate |
| 1185 | // "a%b%c" % (X, Y) for a$(X)b$(Y)c |
| 1186 | xInterp := &interpolateExpr{args: make([]starlarkExpr, len(mk.Variables))} |
| 1187 | for i, ref := range mk.Variables { |
| 1188 | arg := ctx.parseReference(node, ref.Name) |
| 1189 | if x, ok := arg.(*badExpr); ok { |
| 1190 | return x |
| 1191 | } |
| 1192 | xInterp.args[i] = arg |
| 1193 | } |
| 1194 | xInterp.chunks = append(xInterp.chunks, mk.Strings...) |
| 1195 | return xInterp |
| 1196 | } |
| 1197 | |
| 1198 | // Handles the statements whose treatment is the same in all contexts: comment, |
| 1199 | // assignment, variable (which is a macro call in reality) and all constructs that |
| 1200 | // do not handle in any context ('define directive and any unrecognized stuff). |
| 1201 | // Return true if we handled it. |
| 1202 | func (ctx *parseContext) handleSimpleStatement(node mkparser.Node) bool { |
| 1203 | handled := true |
| 1204 | switch x := node.(type) { |
| 1205 | case *mkparser.Comment: |
| 1206 | ctx.insertComment("#" + x.Comment) |
| 1207 | case *mkparser.Assignment: |
| 1208 | ctx.handleAssignment(x) |
| 1209 | case *mkparser.Variable: |
| 1210 | ctx.handleVariable(x) |
| 1211 | case *mkparser.Directive: |
| 1212 | switch x.Name { |
| 1213 | case "define": |
| 1214 | ctx.handleDefine(x) |
| 1215 | case "include", "-include": |
| 1216 | ctx.handleInclude(node, ctx.parseMakeString(node, x.Args), x.Name[0] != '-') |
| 1217 | default: |
| 1218 | handled = false |
| 1219 | } |
| 1220 | default: |
| 1221 | ctx.errorf(x, "unsupported line %s", x.Dump()) |
| 1222 | } |
| 1223 | return handled |
| 1224 | } |
| 1225 | |
| 1226 | func (ctx *parseContext) insertComment(s string) { |
| 1227 | ctx.receiver.newNode(&commentNode{strings.TrimSpace(s)}) |
| 1228 | } |
| 1229 | |
| 1230 | func (ctx *parseContext) carryAsComment(failedNode mkparser.Node) { |
| 1231 | for _, line := range strings.Split(failedNode.Dump(), "\n") { |
| 1232 | ctx.insertComment("# " + line) |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | // records that the given node failed to be converted and includes an explanatory message |
| 1237 | func (ctx *parseContext) errorf(failedNode mkparser.Node, message string, args ...interface{}) { |
| 1238 | if ctx.errorLogger != nil { |
| 1239 | ctx.errorLogger.NewError(message, failedNode, args...) |
| 1240 | } |
| 1241 | message = fmt.Sprintf(message, args...) |
| 1242 | ctx.insertComment(fmt.Sprintf("# MK2RBC TRANSLATION ERROR: %s", message)) |
| 1243 | ctx.carryAsComment(failedNode) |
| 1244 | ctx.script.hasErrors = true |
| 1245 | } |
| 1246 | |
| 1247 | func (ctx *parseContext) wrapBadExpr(xBad *badExpr) { |
| 1248 | ctx.insertComment(fmt.Sprintf("# MK2RBC TRANSLATION ERROR: %s", xBad.message)) |
| 1249 | ctx.carryAsComment(xBad.node) |
| 1250 | } |
| 1251 | |
| 1252 | func (ctx *parseContext) loadedModulePath(path string) string { |
| 1253 | // During the transition to Roboleaf some of the product configuration files |
| 1254 | // will be converted and checked in while the others will be generated on the fly |
| 1255 | // and run. The runner (rbcrun application) accommodates this by allowing three |
| 1256 | // different ways to specify the loaded file location: |
| 1257 | // 1) load(":<file>",...) loads <file> from the same directory |
| 1258 | // 2) load("//path/relative/to/source/root:<file>", ...) loads <file> source tree |
| 1259 | // 3) load("/absolute/path/to/<file> absolute path |
| 1260 | // If the file being generated and the file it wants to load are in the same directory, |
| 1261 | // generate option 1. |
| 1262 | // Otherwise, if output directory is not specified, generate 2) |
| 1263 | // Finally, if output directory has been specified and the file being generated and |
| 1264 | // the file it wants to load from are in the different directories, generate 2) or 3): |
| 1265 | // * if the file being loaded exists in the source tree, generate 2) |
| 1266 | // * otherwise, generate 3) |
| 1267 | // Finally, figure out the loaded module path and name and create a node for it |
| 1268 | loadedModuleDir := filepath.Dir(path) |
| 1269 | base := filepath.Base(path) |
| 1270 | loadedModuleName := strings.TrimSuffix(base, filepath.Ext(base)) + ctx.outputSuffix |
| 1271 | if loadedModuleDir == filepath.Dir(ctx.script.mkFile) { |
| 1272 | return ":" + loadedModuleName |
| 1273 | } |
| 1274 | if ctx.outputDir == "" { |
| 1275 | return fmt.Sprintf("//%s:%s", loadedModuleDir, loadedModuleName) |
| 1276 | } |
| 1277 | if _, err := os.Stat(filepath.Join(loadedModuleDir, loadedModuleName)); err == nil { |
| 1278 | return fmt.Sprintf("//%s:%s", loadedModuleDir, loadedModuleName) |
| 1279 | } |
| 1280 | return filepath.Join(ctx.outputDir, loadedModuleDir, loadedModuleName) |
| 1281 | } |
| 1282 | |
| 1283 | func (ss *StarlarkScript) String() string { |
| 1284 | return NewGenerateContext(ss).emit() |
| 1285 | } |
| 1286 | |
| 1287 | func (ss *StarlarkScript) SubConfigFiles() []string { |
| 1288 | var subs []string |
| 1289 | for _, src := range ss.inherited { |
| 1290 | subs = append(subs, src.originalPath) |
| 1291 | } |
| 1292 | return subs |
| 1293 | } |
| 1294 | |
| 1295 | func (ss *StarlarkScript) HasErrors() bool { |
| 1296 | return ss.hasErrors |
| 1297 | } |
| 1298 | |
| 1299 | // Convert reads and parses a makefile. If successful, parsed tree |
| 1300 | // is returned and then can be passed to String() to get the generated |
| 1301 | // Starlark file. |
| 1302 | func Convert(req Request) (*StarlarkScript, error) { |
| 1303 | reader := req.Reader |
| 1304 | if reader == nil { |
| 1305 | mkContents, err := ioutil.ReadFile(req.MkFile) |
| 1306 | if err != nil { |
| 1307 | return nil, err |
| 1308 | } |
| 1309 | reader = bytes.NewBuffer(mkContents) |
| 1310 | } |
| 1311 | parser := mkparser.NewParser(req.MkFile, reader) |
| 1312 | nodes, errs := parser.Parse() |
| 1313 | if len(errs) > 0 { |
| 1314 | for _, e := range errs { |
| 1315 | fmt.Fprintln(os.Stderr, "ERROR:", e) |
| 1316 | } |
| 1317 | return nil, fmt.Errorf("bad makefile %s", req.MkFile) |
| 1318 | } |
| 1319 | starScript := &StarlarkScript{ |
| 1320 | moduleName: moduleNameForFile(req.MkFile), |
| 1321 | mkFile: req.MkFile, |
| 1322 | topDir: req.RootDir, |
| 1323 | traceCalls: req.TraceCalls, |
| 1324 | warnPartialSuccess: req.WarnPartialSuccess, |
| 1325 | } |
| 1326 | ctx := newParseContext(starScript, nodes) |
| 1327 | ctx.outputSuffix = req.OutputSuffix |
| 1328 | ctx.outputDir = req.OutputDir |
| 1329 | ctx.errorLogger = req.ErrorLogger |
| 1330 | if len(req.TracedVariables) > 0 { |
| 1331 | ctx.tracedVariables = make(map[string]bool) |
| 1332 | for _, v := range req.TracedVariables { |
| 1333 | ctx.tracedVariables[v] = true |
| 1334 | } |
| 1335 | } |
| 1336 | ctx.pushReceiver(starScript) |
| 1337 | for ctx.hasNodes() && ctx.fatalError == nil { |
| 1338 | node := ctx.getNode() |
| 1339 | if ctx.handleSimpleStatement(node) { |
| 1340 | continue |
| 1341 | } |
| 1342 | switch x := node.(type) { |
| 1343 | case *mkparser.Directive: |
| 1344 | switch x.Name { |
| 1345 | case "ifeq", "ifneq", "ifdef", "ifndef": |
| 1346 | ctx.handleIfBlock(x) |
| 1347 | default: |
| 1348 | ctx.errorf(x, "unexpected directive %s", x.Name) |
| 1349 | } |
| 1350 | default: |
| 1351 | ctx.errorf(x, "unsupported line") |
| 1352 | } |
| 1353 | } |
| 1354 | if ctx.fatalError != nil { |
| 1355 | return nil, ctx.fatalError |
| 1356 | } |
| 1357 | return starScript, nil |
| 1358 | } |
| 1359 | |
| 1360 | func Launcher(path, name string) string { |
| 1361 | var buf bytes.Buffer |
| 1362 | fmt.Fprintf(&buf, "load(%q, %q)\n", baseUri, baseName) |
| 1363 | fmt.Fprintf(&buf, "load(%q, \"init\")\n", path) |
| 1364 | fmt.Fprintf(&buf, "g, config = %s(%q, init)\n", cfnMain, name) |
| 1365 | fmt.Fprintf(&buf, "%s(g, config)\n", cfnPrintVars) |
| 1366 | return buf.String() |
| 1367 | } |
| 1368 | |
| 1369 | func MakePath2ModuleName(mkPath string) string { |
| 1370 | return strings.TrimSuffix(mkPath, filepath.Ext(mkPath)) |
| 1371 | } |