mk2rbc.go 74 KB

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