mk2rbc_test.go 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  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. package mk2rbc
  15. import (
  16. "bytes"
  17. "io/fs"
  18. "path/filepath"
  19. "strings"
  20. "testing"
  21. )
  22. var testCases = []struct {
  23. desc string
  24. mkname string
  25. in string
  26. expected string
  27. }{
  28. {
  29. desc: "Comment",
  30. mkname: "product.mk",
  31. in: `
  32. # Comment
  33. # FOO= a\
  34. b
  35. `,
  36. expected: `# Comment
  37. # FOO= a
  38. # b
  39. load("//build/make/core:product_config.rbc", "rblf")
  40. def init(g, handle):
  41. cfg = rblf.cfg(handle)
  42. `,
  43. },
  44. {
  45. desc: "Name conversion",
  46. mkname: "path/bar-baz.mk",
  47. in: `
  48. # Comment
  49. `,
  50. expected: `# Comment
  51. load("//build/make/core:product_config.rbc", "rblf")
  52. def init(g, handle):
  53. cfg = rblf.cfg(handle)
  54. `,
  55. },
  56. {
  57. desc: "Item variable",
  58. mkname: "pixel3.mk",
  59. in: `
  60. PRODUCT_NAME := Pixel 3
  61. PRODUCT_MODEL :=
  62. local_var = foo
  63. local-var-with-dashes := bar
  64. $(warning local-var-with-dashes: $(local-var-with-dashes))
  65. GLOBAL-VAR-WITH-DASHES := baz
  66. $(warning GLOBAL-VAR-WITH-DASHES: $(GLOBAL-VAR-WITH-DASHES))
  67. `,
  68. expected: `load("//build/make/core:product_config.rbc", "rblf")
  69. def init(g, handle):
  70. cfg = rblf.cfg(handle)
  71. cfg["PRODUCT_NAME"] = "Pixel 3"
  72. cfg["PRODUCT_MODEL"] = ""
  73. _local_var = "foo"
  74. _local_var_with_dashes = "bar"
  75. rblf.mkwarning("pixel3.mk", "local-var-with-dashes: %s" % _local_var_with_dashes)
  76. g["GLOBAL-VAR-WITH-DASHES"] = "baz"
  77. rblf.mkwarning("pixel3.mk", "GLOBAL-VAR-WITH-DASHES: %s" % g["GLOBAL-VAR-WITH-DASHES"])
  78. `,
  79. },
  80. {
  81. desc: "List variable",
  82. mkname: "pixel4.mk",
  83. in: `
  84. PRODUCT_PACKAGES = package1 package2
  85. PRODUCT_COPY_FILES += file2:target
  86. PRODUCT_PACKAGES += package3
  87. PRODUCT_COPY_FILES =
  88. `,
  89. expected: `load("//build/make/core:product_config.rbc", "rblf")
  90. def init(g, handle):
  91. cfg = rblf.cfg(handle)
  92. cfg["PRODUCT_PACKAGES"] = [
  93. "package1",
  94. "package2",
  95. ]
  96. rblf.setdefault(handle, "PRODUCT_COPY_FILES")
  97. cfg["PRODUCT_COPY_FILES"] += ["file2:target"]
  98. cfg["PRODUCT_PACKAGES"] += ["package3"]
  99. cfg["PRODUCT_COPY_FILES"] = []
  100. `,
  101. },
  102. {
  103. desc: "Unknown function",
  104. mkname: "product.mk",
  105. in: `
  106. PRODUCT_NAME := $(call foo1, bar)
  107. PRODUCT_NAME := $(call foo0)
  108. `,
  109. expected: `load("//build/make/core:product_config.rbc", "rblf")
  110. def init(g, handle):
  111. cfg = rblf.cfg(handle)
  112. cfg["PRODUCT_NAME"] = rblf.mk2rbc_error("product.mk:2", "cannot handle invoking foo1")
  113. cfg["PRODUCT_NAME"] = rblf.mk2rbc_error("product.mk:3", "cannot handle invoking foo0")
  114. `,
  115. },
  116. {
  117. desc: "Inherit configuration always",
  118. mkname: "product.mk",
  119. in: `
  120. $(call inherit-product, part.mk)
  121. ifdef PRODUCT_NAME
  122. $(call inherit-product, part1.mk)
  123. else # Comment
  124. $(call inherit-product, $(LOCAL_PATH)/part.mk)
  125. endif
  126. `,
  127. expected: `load("//build/make/core:product_config.rbc", "rblf")
  128. load(":part.star", _part_init = "init")
  129. load(":part1.star|init", _part1_init = "init")
  130. def init(g, handle):
  131. cfg = rblf.cfg(handle)
  132. rblf.inherit(handle, "part", _part_init)
  133. if cfg.get("PRODUCT_NAME", ""):
  134. if not _part1_init:
  135. rblf.mkerror("product.mk", "Cannot find %s" % (":part1.star"))
  136. rblf.inherit(handle, "part1", _part1_init)
  137. else:
  138. # Comment
  139. rblf.inherit(handle, "part", _part_init)
  140. `,
  141. },
  142. {
  143. desc: "Inherit configuration if it exists",
  144. mkname: "product.mk",
  145. in: `
  146. $(call inherit-product-if-exists, part.mk)
  147. `,
  148. expected: `load("//build/make/core:product_config.rbc", "rblf")
  149. load(":part.star|init", _part_init = "init")
  150. def init(g, handle):
  151. cfg = rblf.cfg(handle)
  152. if _part_init:
  153. rblf.inherit(handle, "part", _part_init)
  154. `,
  155. },
  156. {
  157. desc: "Include configuration",
  158. mkname: "product.mk",
  159. in: `
  160. include part.mk
  161. ifdef PRODUCT_NAME
  162. include part1.mk
  163. else
  164. -include $(LOCAL_PATH)/part1.mk)
  165. endif
  166. `,
  167. expected: `load("//build/make/core:product_config.rbc", "rblf")
  168. load(":part.star", _part_init = "init")
  169. load(":part1.star|init", _part1_init = "init")
  170. def init(g, handle):
  171. cfg = rblf.cfg(handle)
  172. _part_init(g, handle)
  173. if cfg.get("PRODUCT_NAME", ""):
  174. if not _part1_init:
  175. rblf.mkerror("product.mk", "Cannot find %s" % (":part1.star"))
  176. _part1_init(g, handle)
  177. else:
  178. if _part1_init != None:
  179. _part1_init(g, handle)
  180. `,
  181. },
  182. {
  183. desc: "Synonymous inherited configurations",
  184. mkname: "path/product.mk",
  185. in: `
  186. $(call inherit-product, */font.mk)
  187. $(call inherit-product, $(sort $(wildcard */font.mk)))
  188. $(call inherit-product, $(wildcard */font.mk))
  189. include */font.mk
  190. include $(sort $(wildcard */font.mk))
  191. include $(wildcard */font.mk)
  192. `,
  193. expected: `load("//build/make/core:product_config.rbc", "rblf")
  194. load("//bar:font.star", _font_init = "init")
  195. load("//foo:font.star", _font1_init = "init")
  196. def init(g, handle):
  197. cfg = rblf.cfg(handle)
  198. rblf.inherit(handle, "bar/font", _font_init)
  199. rblf.inherit(handle, "foo/font", _font1_init)
  200. rblf.inherit(handle, "bar/font", _font_init)
  201. rblf.inherit(handle, "foo/font", _font1_init)
  202. rblf.inherit(handle, "bar/font", _font_init)
  203. rblf.inherit(handle, "foo/font", _font1_init)
  204. _font_init(g, handle)
  205. _font1_init(g, handle)
  206. _font_init(g, handle)
  207. _font1_init(g, handle)
  208. _font_init(g, handle)
  209. _font1_init(g, handle)
  210. `,
  211. },
  212. {
  213. desc: "Directive define",
  214. mkname: "product.mk",
  215. in: `
  216. define some-macro
  217. $(info foo)
  218. endef
  219. `,
  220. expected: `load("//build/make/core:product_config.rbc", "rblf")
  221. def init(g, handle):
  222. cfg = rblf.cfg(handle)
  223. rblf.mk2rbc_error("product.mk:2", "define is not supported: some-macro")
  224. `,
  225. },
  226. {
  227. desc: "Ifdef",
  228. mkname: "product.mk",
  229. in: `
  230. ifdef PRODUCT_NAME
  231. PRODUCT_NAME = gizmo
  232. else
  233. endif
  234. local_var :=
  235. ifdef local_var
  236. endif
  237. `,
  238. expected: `load("//build/make/core:product_config.rbc", "rblf")
  239. def init(g, handle):
  240. cfg = rblf.cfg(handle)
  241. if cfg.get("PRODUCT_NAME", ""):
  242. cfg["PRODUCT_NAME"] = "gizmo"
  243. else:
  244. pass
  245. _local_var = ""
  246. if _local_var:
  247. pass
  248. `,
  249. },
  250. {
  251. desc: "Simple functions",
  252. mkname: "product.mk",
  253. in: `
  254. $(warning this is the warning)
  255. $(warning)
  256. $(warning # this warning starts with a pound)
  257. $(warning this warning has a # in the middle)
  258. $(info this is the info)
  259. $(error this is the error)
  260. PRODUCT_NAME:=$(shell echo *)
  261. `,
  262. expected: `load("//build/make/core:product_config.rbc", "rblf")
  263. def init(g, handle):
  264. cfg = rblf.cfg(handle)
  265. rblf.mkwarning("product.mk", "this is the warning")
  266. rblf.mkwarning("product.mk", "")
  267. rblf.mkwarning("product.mk", "# this warning starts with a pound")
  268. rblf.mkwarning("product.mk", "this warning has a # in the middle")
  269. rblf.mkinfo("product.mk", "this is the info")
  270. rblf.mkerror("product.mk", "this is the error")
  271. cfg["PRODUCT_NAME"] = rblf.shell("echo *")
  272. `,
  273. },
  274. {
  275. desc: "Empty if",
  276. mkname: "product.mk",
  277. in: `
  278. ifdef PRODUCT_NAME
  279. # Comment
  280. else
  281. TARGET_COPY_OUT_RECOVERY := foo
  282. endif
  283. `,
  284. expected: `load("//build/make/core:product_config.rbc", "rblf")
  285. def init(g, handle):
  286. cfg = rblf.cfg(handle)
  287. if cfg.get("PRODUCT_NAME", ""):
  288. # Comment
  289. pass
  290. else:
  291. rblf.mk2rbc_error("product.mk:5", "cannot set predefined variable TARGET_COPY_OUT_RECOVERY to \"foo\", its value should be \"recovery\"")
  292. `,
  293. },
  294. {
  295. desc: "if/else/endif",
  296. mkname: "product.mk",
  297. in: `
  298. ifndef PRODUCT_NAME
  299. PRODUCT_NAME=gizmo1
  300. else
  301. PRODUCT_NAME=gizmo2
  302. endif
  303. `,
  304. expected: `load("//build/make/core:product_config.rbc", "rblf")
  305. def init(g, handle):
  306. cfg = rblf.cfg(handle)
  307. if not cfg.get("PRODUCT_NAME", ""):
  308. cfg["PRODUCT_NAME"] = "gizmo1"
  309. else:
  310. cfg["PRODUCT_NAME"] = "gizmo2"
  311. `,
  312. },
  313. {
  314. desc: "else if",
  315. mkname: "product.mk",
  316. in: `
  317. ifdef PRODUCT_NAME
  318. PRODUCT_NAME = gizmo
  319. else ifndef PRODUCT_PACKAGES # Comment
  320. endif
  321. `,
  322. expected: `load("//build/make/core:product_config.rbc", "rblf")
  323. def init(g, handle):
  324. cfg = rblf.cfg(handle)
  325. if cfg.get("PRODUCT_NAME", ""):
  326. cfg["PRODUCT_NAME"] = "gizmo"
  327. elif not cfg.get("PRODUCT_PACKAGES", []):
  328. # Comment
  329. pass
  330. `,
  331. },
  332. {
  333. desc: "ifeq / ifneq",
  334. mkname: "product.mk",
  335. in: `
  336. ifeq (aosp_arm, $(TARGET_PRODUCT))
  337. PRODUCT_MODEL = pix2
  338. else
  339. PRODUCT_MODEL = pix21
  340. endif
  341. ifneq (aosp_x86, $(TARGET_PRODUCT))
  342. PRODUCT_MODEL = pix3
  343. endif
  344. `,
  345. expected: `load("//build/make/core:product_config.rbc", "rblf")
  346. def init(g, handle):
  347. cfg = rblf.cfg(handle)
  348. if "aosp_arm" == g["TARGET_PRODUCT"]:
  349. cfg["PRODUCT_MODEL"] = "pix2"
  350. else:
  351. cfg["PRODUCT_MODEL"] = "pix21"
  352. if "aosp_x86" != g["TARGET_PRODUCT"]:
  353. cfg["PRODUCT_MODEL"] = "pix3"
  354. `,
  355. },
  356. {
  357. desc: "ifeq with soong_config_get",
  358. mkname: "product.mk",
  359. in: `
  360. ifeq (true,$(call soong_config_get,art_module,source_build))
  361. endif
  362. `,
  363. expected: `load("//build/make/core:product_config.rbc", "rblf")
  364. def init(g, handle):
  365. cfg = rblf.cfg(handle)
  366. if "true" == rblf.soong_config_get(g, "art_module", "source_build"):
  367. pass
  368. `,
  369. },
  370. {
  371. desc: "ifeq with $(NATIVE_COVERAGE)",
  372. mkname: "product.mk",
  373. in: `
  374. ifeq ($(NATIVE_COVERAGE),true)
  375. endif
  376. `,
  377. expected: `load("//build/make/core:product_config.rbc", "rblf")
  378. def init(g, handle):
  379. cfg = rblf.cfg(handle)
  380. if g.get("NATIVE_COVERAGE", False):
  381. pass
  382. `,
  383. },
  384. {
  385. desc: "Check filter result",
  386. mkname: "product.mk",
  387. in: `
  388. ifeq (,$(filter userdebug eng, $(TARGET_BUILD_VARIANT)))
  389. endif
  390. ifneq (,$(filter userdebug,$(TARGET_BUILD_VARIANT))
  391. endif
  392. ifneq (,$(filter plaf,$(PLATFORM_LIST)))
  393. endif
  394. ifeq ($(TARGET_BUILD_VARIANT), $(filter $(TARGET_BUILD_VARIANT), userdebug eng))
  395. endif
  396. ifneq (, $(filter $(TARGET_BUILD_VARIANT), userdebug eng))
  397. endif
  398. ifneq (,$(filter userdebug eng, $(TARGET_BUILD_VARIANT)))
  399. endif
  400. ifneq (,$(filter true, $(v1)$(v2)))
  401. endif
  402. ifeq (,$(filter barbet coral%,$(TARGET_PRODUCT)))
  403. else ifneq (,$(filter barbet%,$(TARGET_PRODUCT)))
  404. endif
  405. ifeq (,$(filter-out sunfish_kasan, $(TARGET_PRODUCT)))
  406. endif
  407. `,
  408. expected: `load("//build/make/core:product_config.rbc", "rblf")
  409. def init(g, handle):
  410. cfg = rblf.cfg(handle)
  411. if not rblf.filter("userdebug eng", g["TARGET_BUILD_VARIANT"]):
  412. pass
  413. if rblf.filter("userdebug", g["TARGET_BUILD_VARIANT"]):
  414. pass
  415. if "plaf" in g.get("PLATFORM_LIST", []):
  416. pass
  417. if g["TARGET_BUILD_VARIANT"] == " ".join(rblf.filter(g["TARGET_BUILD_VARIANT"], "userdebug eng")):
  418. pass
  419. if g["TARGET_BUILD_VARIANT"] in ["userdebug", "eng"]:
  420. pass
  421. if rblf.filter("userdebug eng", g["TARGET_BUILD_VARIANT"]):
  422. pass
  423. if rblf.filter("true", "%s%s" % (_v1, _v2)):
  424. pass
  425. if not rblf.filter("barbet coral%", g["TARGET_PRODUCT"]):
  426. pass
  427. elif rblf.filter("barbet%", g["TARGET_PRODUCT"]):
  428. pass
  429. if not rblf.filter_out("sunfish_kasan", g["TARGET_PRODUCT"]):
  430. pass
  431. `,
  432. },
  433. {
  434. desc: "Get filter result",
  435. mkname: "product.mk",
  436. in: `
  437. PRODUCT_LIST2=$(filter-out %/foo.ko,$(wildcard path/*.ko))
  438. `,
  439. expected: `load("//build/make/core:product_config.rbc", "rblf")
  440. def init(g, handle):
  441. cfg = rblf.cfg(handle)
  442. cfg["PRODUCT_LIST2"] = rblf.filter_out("%/foo.ko", rblf.expand_wildcard("path/*.ko"))
  443. `,
  444. },
  445. {
  446. desc: "filter $(VAR), values",
  447. mkname: "product.mk",
  448. in: `
  449. ifeq (,$(filter $(TARGET_PRODUCT), yukawa_gms yukawa_sei510_gms)
  450. ifneq (,$(filter $(TARGET_PRODUCT), yukawa_gms)
  451. endif
  452. endif
  453. `,
  454. expected: `load("//build/make/core:product_config.rbc", "rblf")
  455. def init(g, handle):
  456. cfg = rblf.cfg(handle)
  457. if g["TARGET_PRODUCT"] not in ["yukawa_gms", "yukawa_sei510_gms"]:
  458. if g["TARGET_PRODUCT"] == "yukawa_gms":
  459. pass
  460. `,
  461. },
  462. {
  463. desc: "filter $(V1), $(V2)",
  464. mkname: "product.mk",
  465. in: `
  466. ifneq (, $(filter $(PRODUCT_LIST), $(TARGET_PRODUCT)))
  467. endif
  468. `,
  469. expected: `load("//build/make/core:product_config.rbc", "rblf")
  470. def init(g, handle):
  471. cfg = rblf.cfg(handle)
  472. if rblf.filter(g.get("PRODUCT_LIST", []), g["TARGET_PRODUCT"]):
  473. pass
  474. `,
  475. },
  476. {
  477. desc: "ifeq",
  478. mkname: "product.mk",
  479. in: `
  480. ifeq (aosp, $(TARGET_PRODUCT)) # Comment
  481. else ifneq (, $(TARGET_PRODUCT))
  482. endif
  483. `,
  484. expected: `load("//build/make/core:product_config.rbc", "rblf")
  485. def init(g, handle):
  486. cfg = rblf.cfg(handle)
  487. if "aosp" == g["TARGET_PRODUCT"]:
  488. # Comment
  489. pass
  490. elif g["TARGET_PRODUCT"]:
  491. pass
  492. `,
  493. },
  494. {
  495. desc: "Nested if",
  496. mkname: "product.mk",
  497. in: `
  498. ifdef PRODUCT_NAME
  499. PRODUCT_PACKAGES = pack-if0
  500. ifdef PRODUCT_MODEL
  501. PRODUCT_PACKAGES = pack-if-if
  502. else ifdef PRODUCT_NAME
  503. PRODUCT_PACKAGES = pack-if-elif
  504. else
  505. PRODUCT_PACKAGES = pack-if-else
  506. endif
  507. PRODUCT_PACKAGES = pack-if
  508. else ifneq (,$(TARGET_PRODUCT))
  509. PRODUCT_PACKAGES = pack-elif
  510. else
  511. PRODUCT_PACKAGES = pack-else
  512. endif
  513. `,
  514. expected: `load("//build/make/core:product_config.rbc", "rblf")
  515. def init(g, handle):
  516. cfg = rblf.cfg(handle)
  517. if cfg.get("PRODUCT_NAME", ""):
  518. cfg["PRODUCT_PACKAGES"] = ["pack-if0"]
  519. if cfg.get("PRODUCT_MODEL", ""):
  520. cfg["PRODUCT_PACKAGES"] = ["pack-if-if"]
  521. elif cfg.get("PRODUCT_NAME", ""):
  522. cfg["PRODUCT_PACKAGES"] = ["pack-if-elif"]
  523. else:
  524. cfg["PRODUCT_PACKAGES"] = ["pack-if-else"]
  525. cfg["PRODUCT_PACKAGES"] = ["pack-if"]
  526. elif g["TARGET_PRODUCT"]:
  527. cfg["PRODUCT_PACKAGES"] = ["pack-elif"]
  528. else:
  529. cfg["PRODUCT_PACKAGES"] = ["pack-else"]
  530. `,
  531. },
  532. {
  533. desc: "Wildcard",
  534. mkname: "product.mk",
  535. in: `
  536. ifeq (,$(wildcard foo.mk))
  537. endif
  538. ifneq (,$(wildcard foo*.mk))
  539. endif
  540. ifeq (foo1.mk foo2.mk barxyz.mk,$(wildcard foo*.mk bar*.mk))
  541. endif
  542. `,
  543. expected: `load("//build/make/core:product_config.rbc", "rblf")
  544. def init(g, handle):
  545. cfg = rblf.cfg(handle)
  546. if not rblf.expand_wildcard("foo.mk"):
  547. pass
  548. if rblf.expand_wildcard("foo*.mk"):
  549. pass
  550. if rblf.expand_wildcard("foo*.mk bar*.mk") == ["foo1.mk", "foo2.mk", "barxyz.mk"]:
  551. pass
  552. `,
  553. },
  554. {
  555. desc: "if with interpolation",
  556. mkname: "product.mk",
  557. in: `
  558. ifeq ($(VARIABLE1)text$(VARIABLE2),true)
  559. endif
  560. `,
  561. expected: `load("//build/make/core:product_config.rbc", "rblf")
  562. def init(g, handle):
  563. cfg = rblf.cfg(handle)
  564. if "%stext%s" % (g.get("VARIABLE1", ""), g.get("VARIABLE2", "")) == "true":
  565. pass
  566. `,
  567. },
  568. {
  569. desc: "ifneq $(X),true",
  570. mkname: "product.mk",
  571. in: `
  572. ifneq ($(VARIABLE),true)
  573. endif
  574. `,
  575. expected: `load("//build/make/core:product_config.rbc", "rblf")
  576. def init(g, handle):
  577. cfg = rblf.cfg(handle)
  578. if g.get("VARIABLE", "") != "true":
  579. pass
  580. `,
  581. },
  582. {
  583. desc: "Const neq",
  584. mkname: "product.mk",
  585. in: `
  586. ifneq (1,0)
  587. endif
  588. `,
  589. expected: `load("//build/make/core:product_config.rbc", "rblf")
  590. def init(g, handle):
  591. cfg = rblf.cfg(handle)
  592. if "1" != "0":
  593. pass
  594. `,
  595. },
  596. {
  597. desc: "is-board calls",
  598. mkname: "product.mk",
  599. in: `
  600. ifeq ($(call is-board-platform-in-list,msm8998), true)
  601. else ifneq ($(call is-board-platform,copper),true)
  602. else ifneq ($(call is-vendor-board-platform,QCOM),true)
  603. else ifeq ($(call is-product-in-list, $(PLATFORM_LIST)), true)
  604. endif
  605. `,
  606. expected: `load("//build/make/core:product_config.rbc", "rblf")
  607. def init(g, handle):
  608. cfg = rblf.cfg(handle)
  609. if rblf.board_platform_in(g, "msm8998"):
  610. pass
  611. elif not rblf.board_platform_is(g, "copper"):
  612. pass
  613. elif g.get("TARGET_BOARD_PLATFORM", "") not in g.get("QCOM_BOARD_PLATFORMS", ""):
  614. pass
  615. elif g["TARGET_PRODUCT"] in g.get("PLATFORM_LIST", []):
  616. pass
  617. `,
  618. },
  619. {
  620. desc: "new is-board calls",
  621. mkname: "product.mk",
  622. in: `
  623. ifneq (,$(call is-board-platform-in-list2,msm8998 $(X))
  624. else ifeq (,$(call is-board-platform2,copper)
  625. else ifneq (,$(call is-vendor-board-qcom))
  626. endif
  627. `,
  628. expected: `load("//build/make/core:product_config.rbc", "rblf")
  629. def init(g, handle):
  630. cfg = rblf.cfg(handle)
  631. if rblf.board_platform_in(g, "msm8998 %s" % g.get("X", "")):
  632. pass
  633. elif not rblf.board_platform_is(g, "copper"):
  634. pass
  635. elif g.get("TARGET_BOARD_PLATFORM", "") in g.get("QCOM_BOARD_PLATFORMS", ""):
  636. pass
  637. `,
  638. },
  639. {
  640. desc: "findstring call",
  641. mkname: "product.mk",
  642. in: `
  643. result := $(findstring a,a b c)
  644. result := $(findstring b,x y z)
  645. `,
  646. expected: `load("//build/make/core:product_config.rbc", "rblf")
  647. def init(g, handle):
  648. cfg = rblf.cfg(handle)
  649. _result = rblf.findstring("a", "a b c")
  650. _result = rblf.findstring("b", "x y z")
  651. `,
  652. },
  653. {
  654. desc: "findstring in if statement",
  655. mkname: "product.mk",
  656. in: `
  657. ifeq ($(findstring foo,$(PRODUCT_PACKAGES)),)
  658. endif
  659. ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),)
  660. endif
  661. ifeq ($(findstring foo,$(PRODUCT_PACKAGES)),foo)
  662. endif
  663. ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),foo)
  664. endif
  665. `,
  666. expected: `load("//build/make/core:product_config.rbc", "rblf")
  667. def init(g, handle):
  668. cfg = rblf.cfg(handle)
  669. if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") == -1:
  670. pass
  671. if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
  672. pass
  673. if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
  674. pass
  675. if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") == -1:
  676. pass
  677. `,
  678. },
  679. {
  680. desc: "rhs call",
  681. mkname: "product.mk",
  682. in: `
  683. PRODUCT_COPY_FILES = $(call add-to-product-copy-files-if-exists, path:distpath) \
  684. $(call find-copy-subdir-files, *, fromdir, todir) $(wildcard foo.*)
  685. `,
  686. expected: `load("//build/make/core:product_config.rbc", "rblf")
  687. def init(g, handle):
  688. cfg = rblf.cfg(handle)
  689. cfg["PRODUCT_COPY_FILES"] = (rblf.copy_if_exists("path:distpath") +
  690. rblf.find_and_copy("*", "fromdir", "todir") +
  691. rblf.expand_wildcard("foo.*"))
  692. `,
  693. },
  694. {
  695. desc: "inferred type",
  696. mkname: "product.mk",
  697. in: `
  698. HIKEY_MODS := $(wildcard foo/*.ko)
  699. BOARD_VENDOR_KERNEL_MODULES += $(HIKEY_MODS)
  700. `,
  701. expected: `load("//build/make/core:product_config.rbc", "rblf")
  702. def init(g, handle):
  703. cfg = rblf.cfg(handle)
  704. g["HIKEY_MODS"] = rblf.expand_wildcard("foo/*.ko")
  705. g.setdefault("BOARD_VENDOR_KERNEL_MODULES", [])
  706. g["BOARD_VENDOR_KERNEL_MODULES"] += g["HIKEY_MODS"]
  707. `,
  708. },
  709. {
  710. desc: "list with vars",
  711. mkname: "product.mk",
  712. in: `
  713. PRODUCT_COPY_FILES += path1:$(TARGET_PRODUCT)/path1 $(PRODUCT_MODEL)/path2:$(TARGET_PRODUCT)/path2
  714. `,
  715. expected: `load("//build/make/core:product_config.rbc", "rblf")
  716. def init(g, handle):
  717. cfg = rblf.cfg(handle)
  718. rblf.setdefault(handle, "PRODUCT_COPY_FILES")
  719. cfg["PRODUCT_COPY_FILES"] += (("path1:%s/path1" % g["TARGET_PRODUCT"]).split() +
  720. ("%s/path2:%s/path2" % (cfg.get("PRODUCT_MODEL", ""), g["TARGET_PRODUCT"])).split())
  721. `,
  722. },
  723. {
  724. desc: "misc calls",
  725. mkname: "product.mk",
  726. in: `
  727. $(call enforce-product-packages-exist,)
  728. $(call enforce-product-packages-exist, foo)
  729. $(call require-artifacts-in-path, foo, bar)
  730. $(call require-artifacts-in-path-relaxed, foo, bar)
  731. $(call dist-for-goals, goal, from:to)
  732. $(call add-product-dex-preopt-module-config,MyModule,disable)
  733. `,
  734. expected: `load("//build/make/core:product_config.rbc", "rblf")
  735. def init(g, handle):
  736. cfg = rblf.cfg(handle)
  737. rblf.enforce_product_packages_exist(handle, "")
  738. rblf.enforce_product_packages_exist(handle, "foo")
  739. rblf.require_artifacts_in_path(handle, "foo", "bar")
  740. rblf.require_artifacts_in_path_relaxed(handle, "foo", "bar")
  741. rblf.mkdist_for_goals(g, "goal", "from:to")
  742. rblf.add_product_dex_preopt_module_config(handle, "MyModule", "disable")
  743. `,
  744. },
  745. {
  746. desc: "list with functions",
  747. mkname: "product.mk",
  748. in: `
  749. PRODUCT_COPY_FILES := $(call find-copy-subdir-files,*.kl,from1,to1) \
  750. $(call find-copy-subdir-files,*.kc,from2,to2) \
  751. foo bar
  752. `,
  753. expected: `load("//build/make/core:product_config.rbc", "rblf")
  754. def init(g, handle):
  755. cfg = rblf.cfg(handle)
  756. cfg["PRODUCT_COPY_FILES"] = (rblf.find_and_copy("*.kl", "from1", "to1") +
  757. rblf.find_and_copy("*.kc", "from2", "to2") +
  758. [
  759. "foo",
  760. "bar",
  761. ])
  762. `,
  763. },
  764. {
  765. desc: "Text functions",
  766. mkname: "product.mk",
  767. in: `
  768. PRODUCT_COPY_FILES := $(addprefix pfx-,a b c)
  769. PRODUCT_COPY_FILES := $(addsuffix .sff, a b c)
  770. PRODUCT_NAME := $(word 1, $(subst ., ,$(TARGET_BOARD_PLATFORM)))
  771. ifeq (1,$(words $(SOME_UNKNOWN_VARIABLE)))
  772. endif
  773. ifeq ($(words $(SOME_OTHER_VARIABLE)),$(SOME_INT_VARIABLE))
  774. endif
  775. $(info $(patsubst %.pub,$(PRODUCT_NAME)%,$(PRODUCT_ADB_KEYS)))
  776. $(info $$(dir foo/bar): $(dir foo/bar))
  777. $(info $(firstword $(PRODUCT_COPY_FILES)))
  778. $(info $(lastword $(PRODUCT_COPY_FILES)))
  779. $(info $(dir $(lastword $(MAKEFILE_LIST))))
  780. $(info $(dir $(lastword $(PRODUCT_COPY_FILES))))
  781. $(info $(dir $(lastword $(foobar))))
  782. $(info $(abspath foo/bar))
  783. $(info $(notdir foo/bar))
  784. $(call add_soong_config_namespace,snsconfig)
  785. $(call add_soong_config_var_value,snsconfig,imagetype,odm_image)
  786. $(call soong_config_set, snsconfig, foo, foo_value)
  787. $(call soong_config_append, snsconfig, bar, bar_value)
  788. PRODUCT_COPY_FILES := $(call copy-files,$(wildcard foo*.mk),etc)
  789. PRODUCT_COPY_FILES := $(call product-copy-files-by-pattern,from/%,to/%,a b c)
  790. `,
  791. expected: `load("//build/make/core:product_config.rbc", "rblf")
  792. def init(g, handle):
  793. cfg = rblf.cfg(handle)
  794. cfg["PRODUCT_COPY_FILES"] = rblf.addprefix("pfx-", "a b c")
  795. cfg["PRODUCT_COPY_FILES"] = rblf.addsuffix(".sff", "a b c")
  796. cfg["PRODUCT_NAME"] = rblf.words((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " "))[0]
  797. if len(rblf.words(g.get("SOME_UNKNOWN_VARIABLE", ""))) == 1:
  798. pass
  799. if ("%d" % (len(rblf.words(g.get("SOME_OTHER_VARIABLE", ""))))) == g.get("SOME_INT_VARIABLE", ""):
  800. pass
  801. rblf.mkinfo("product.mk", rblf.mkpatsubst("%.pub", "%s%%" % cfg["PRODUCT_NAME"], g.get("PRODUCT_ADB_KEYS", "")))
  802. rblf.mkinfo("product.mk", "$(dir foo/bar): %s" % rblf.dir("foo/bar"))
  803. rblf.mkinfo("product.mk", rblf.first_word(cfg["PRODUCT_COPY_FILES"]))
  804. rblf.mkinfo("product.mk", rblf.last_word(cfg["PRODUCT_COPY_FILES"]))
  805. rblf.mkinfo("product.mk", rblf.dir(rblf.last_word("product.mk")))
  806. rblf.mkinfo("product.mk", rblf.dir(rblf.last_word(cfg["PRODUCT_COPY_FILES"])))
  807. rblf.mkinfo("product.mk", rblf.dir(rblf.last_word(_foobar)))
  808. rblf.mkinfo("product.mk", rblf.abspath("foo/bar"))
  809. rblf.mkinfo("product.mk", rblf.notdir("foo/bar"))
  810. rblf.soong_config_namespace(g, "snsconfig")
  811. rblf.soong_config_set(g, "snsconfig", "imagetype", "odm_image")
  812. rblf.soong_config_set(g, "snsconfig", "foo", "foo_value")
  813. rblf.soong_config_append(g, "snsconfig", "bar", "bar_value")
  814. cfg["PRODUCT_COPY_FILES"] = rblf.copy_files(rblf.expand_wildcard("foo*.mk"), "etc")
  815. cfg["PRODUCT_COPY_FILES"] = rblf.product_copy_files_by_pattern("from/%", "to/%", "a b c")
  816. `,
  817. },
  818. {
  819. desc: "subst in list",
  820. mkname: "product.mk",
  821. in: `
  822. files = $(call find-copy-subdir-files,*,from,to)
  823. PRODUCT_COPY_FILES += $(subst foo,bar,$(files))
  824. `,
  825. expected: `load("//build/make/core:product_config.rbc", "rblf")
  826. def init(g, handle):
  827. cfg = rblf.cfg(handle)
  828. _files = rblf.find_and_copy("*", "from", "to")
  829. rblf.setdefault(handle, "PRODUCT_COPY_FILES")
  830. cfg["PRODUCT_COPY_FILES"] += rblf.mksubst("foo", "bar", _files)
  831. `,
  832. },
  833. {
  834. desc: "assignment flavors",
  835. mkname: "product.mk",
  836. in: `
  837. PRODUCT_LIST1 := a
  838. PRODUCT_LIST2 += a
  839. PRODUCT_LIST1 += b
  840. PRODUCT_LIST2 += b
  841. PRODUCT_LIST3 ?= a
  842. PRODUCT_LIST1 = c
  843. PLATFORM_LIST += x
  844. PRODUCT_PACKAGES := $(PLATFORM_LIST)
  845. `,
  846. expected: `load("//build/make/core:product_config.rbc", "rblf")
  847. def init(g, handle):
  848. cfg = rblf.cfg(handle)
  849. cfg["PRODUCT_LIST1"] = ["a"]
  850. rblf.setdefault(handle, "PRODUCT_LIST2")
  851. cfg["PRODUCT_LIST2"] += ["a"]
  852. cfg["PRODUCT_LIST1"] += ["b"]
  853. cfg["PRODUCT_LIST2"] += ["b"]
  854. if cfg.get("PRODUCT_LIST3") == None:
  855. cfg["PRODUCT_LIST3"] = ["a"]
  856. cfg["PRODUCT_LIST1"] = ["c"]
  857. g.setdefault("PLATFORM_LIST", [])
  858. g["PLATFORM_LIST"] += ["x"]
  859. cfg["PRODUCT_PACKAGES"] = g["PLATFORM_LIST"][:]
  860. `,
  861. },
  862. {
  863. desc: "assigment flavors2",
  864. mkname: "product.mk",
  865. in: `
  866. PRODUCT_LIST1 = a
  867. ifeq (0,1)
  868. PRODUCT_LIST1 += b
  869. PRODUCT_LIST2 += b
  870. endif
  871. PRODUCT_LIST1 += c
  872. PRODUCT_LIST2 += c
  873. `,
  874. expected: `load("//build/make/core:product_config.rbc", "rblf")
  875. def init(g, handle):
  876. cfg = rblf.cfg(handle)
  877. cfg["PRODUCT_LIST1"] = ["a"]
  878. if "0" == "1":
  879. cfg["PRODUCT_LIST1"] += ["b"]
  880. rblf.setdefault(handle, "PRODUCT_LIST2")
  881. cfg["PRODUCT_LIST2"] += ["b"]
  882. cfg["PRODUCT_LIST1"] += ["c"]
  883. rblf.setdefault(handle, "PRODUCT_LIST2")
  884. cfg["PRODUCT_LIST2"] += ["c"]
  885. `,
  886. },
  887. {
  888. desc: "assigment setdefaults",
  889. mkname: "product.mk",
  890. in: `
  891. # All of these should have a setdefault because they're self-referential and not defined before
  892. PRODUCT_LIST1 = a $(PRODUCT_LIST1)
  893. PRODUCT_LIST2 ?= a $(PRODUCT_LIST2)
  894. PRODUCT_LIST3 += a
  895. # Now doing them again should not have a setdefault because they've already been set
  896. PRODUCT_LIST1 = a $(PRODUCT_LIST1)
  897. PRODUCT_LIST2 ?= a $(PRODUCT_LIST2)
  898. PRODUCT_LIST3 += a
  899. `,
  900. expected: `# All of these should have a setdefault because they're self-referential and not defined before
  901. load("//build/make/core:product_config.rbc", "rblf")
  902. def init(g, handle):
  903. cfg = rblf.cfg(handle)
  904. rblf.setdefault(handle, "PRODUCT_LIST1")
  905. cfg["PRODUCT_LIST1"] = (["a"] +
  906. cfg.get("PRODUCT_LIST1", []))
  907. if cfg.get("PRODUCT_LIST2") == None:
  908. rblf.setdefault(handle, "PRODUCT_LIST2")
  909. cfg["PRODUCT_LIST2"] = (["a"] +
  910. cfg.get("PRODUCT_LIST2", []))
  911. rblf.setdefault(handle, "PRODUCT_LIST3")
  912. cfg["PRODUCT_LIST3"] += ["a"]
  913. # Now doing them again should not have a setdefault because they've already been set
  914. cfg["PRODUCT_LIST1"] = (["a"] +
  915. cfg["PRODUCT_LIST1"])
  916. if cfg.get("PRODUCT_LIST2") == None:
  917. cfg["PRODUCT_LIST2"] = (["a"] +
  918. cfg["PRODUCT_LIST2"])
  919. cfg["PRODUCT_LIST3"] += ["a"]
  920. `,
  921. },
  922. {
  923. desc: "soong namespace assignments",
  924. mkname: "product.mk",
  925. in: `
  926. SOONG_CONFIG_NAMESPACES += cvd
  927. SOONG_CONFIG_cvd += launch_configs
  928. SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
  929. SOONG_CONFIG_cvd += grub_config
  930. SOONG_CONFIG_cvd_grub_config += grub.cfg
  931. x := $(SOONG_CONFIG_cvd_grub_config)
  932. `,
  933. expected: `load("//build/make/core:product_config.rbc", "rblf")
  934. def init(g, handle):
  935. cfg = rblf.cfg(handle)
  936. rblf.soong_config_namespace(g, "cvd")
  937. rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
  938. rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
  939. _x = rblf.mk2rbc_error("product.mk:7", "SOONG_CONFIG_ variables cannot be referenced, use soong_config_get instead: SOONG_CONFIG_cvd_grub_config")
  940. `,
  941. }, {
  942. desc: "soong namespace accesses",
  943. mkname: "product.mk",
  944. in: `
  945. SOONG_CONFIG_NAMESPACES += cvd
  946. SOONG_CONFIG_cvd += launch_configs
  947. SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
  948. SOONG_CONFIG_cvd += grub_config
  949. SOONG_CONFIG_cvd_grub_config += grub.cfg
  950. x := $(call soong_config_get,cvd,grub_config)
  951. `,
  952. expected: `load("//build/make/core:product_config.rbc", "rblf")
  953. def init(g, handle):
  954. cfg = rblf.cfg(handle)
  955. rblf.soong_config_namespace(g, "cvd")
  956. rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
  957. rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
  958. _x = rblf.soong_config_get(g, "cvd", "grub_config")
  959. `,
  960. },
  961. {
  962. desc: "string split",
  963. mkname: "product.mk",
  964. in: `
  965. PRODUCT_LIST1 = a
  966. local = b
  967. local += c
  968. FOO = d
  969. FOO += e
  970. PRODUCT_LIST1 += $(local)
  971. PRODUCT_LIST1 += $(FOO)
  972. `,
  973. expected: `load("//build/make/core:product_config.rbc", "rblf")
  974. def init(g, handle):
  975. cfg = rblf.cfg(handle)
  976. cfg["PRODUCT_LIST1"] = ["a"]
  977. _local = "b"
  978. _local += " " + "c"
  979. g["FOO"] = "d"
  980. g["FOO"] += " " + "e"
  981. cfg["PRODUCT_LIST1"] += (_local).split()
  982. cfg["PRODUCT_LIST1"] += (g["FOO"]).split()
  983. `,
  984. },
  985. {
  986. desc: "apex_jars",
  987. mkname: "product.mk",
  988. in: `
  989. PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex
  990. `,
  991. expected: `load("//build/make/core:product_config.rbc", "rblf")
  992. def init(g, handle):
  993. cfg = rblf.cfg(handle)
  994. cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) +
  995. ["framework-minus-apex"])
  996. `,
  997. },
  998. {
  999. desc: "strip/sort functions",
  1000. mkname: "product.mk",
  1001. in: `
  1002. ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),)
  1003. PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress)
  1004. endif
  1005. MY_VAR := $(sort b a c)
  1006. `,
  1007. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1008. def init(g, handle):
  1009. cfg = rblf.cfg(handle)
  1010. if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []):
  1011. rblf.setdefault(handle, "PRODUCT_PACKAGES")
  1012. cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split()
  1013. g["MY_VAR"] = rblf.mksort("b a c")
  1014. `,
  1015. },
  1016. {
  1017. desc: "strip func in condition",
  1018. mkname: "product.mk",
  1019. in: `
  1020. ifneq ($(strip $(TARGET_VENDOR)),)
  1021. endif
  1022. `,
  1023. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1024. def init(g, handle):
  1025. cfg = rblf.cfg(handle)
  1026. if rblf.mkstrip(g.get("TARGET_VENDOR", "")):
  1027. pass
  1028. `,
  1029. },
  1030. {
  1031. desc: "ref after set",
  1032. mkname: "product.mk",
  1033. in: `
  1034. PRODUCT_ADB_KEYS:=value
  1035. FOO := $(PRODUCT_ADB_KEYS)
  1036. ifneq (,$(PRODUCT_ADB_KEYS))
  1037. endif
  1038. `,
  1039. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1040. def init(g, handle):
  1041. cfg = rblf.cfg(handle)
  1042. g["PRODUCT_ADB_KEYS"] = "value"
  1043. g["FOO"] = g["PRODUCT_ADB_KEYS"]
  1044. if g["PRODUCT_ADB_KEYS"]:
  1045. pass
  1046. `,
  1047. },
  1048. {
  1049. desc: "ref before set",
  1050. mkname: "product.mk",
  1051. in: `
  1052. V1 := $(PRODUCT_ADB_KEYS)
  1053. ifeq (,$(PRODUCT_ADB_KEYS))
  1054. V2 := $(PRODUCT_ADB_KEYS)
  1055. PRODUCT_ADB_KEYS:=foo
  1056. V3 := $(PRODUCT_ADB_KEYS)
  1057. endif`,
  1058. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1059. def init(g, handle):
  1060. cfg = rblf.cfg(handle)
  1061. g["V1"] = g.get("PRODUCT_ADB_KEYS", "")
  1062. if not g.get("PRODUCT_ADB_KEYS", ""):
  1063. g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
  1064. g["PRODUCT_ADB_KEYS"] = "foo"
  1065. g["V3"] = g["PRODUCT_ADB_KEYS"]
  1066. `,
  1067. },
  1068. {
  1069. desc: "Dynamic inherit path",
  1070. mkname: "product.mk",
  1071. in: `
  1072. MY_PATH:=foo
  1073. $(call inherit-product,vendor/$(MY_PATH)/cfg.mk)
  1074. `,
  1075. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1076. load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
  1077. load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init")
  1078. def init(g, handle):
  1079. cfg = rblf.cfg(handle)
  1080. g["MY_PATH"] = "foo"
  1081. _entry = {
  1082. "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
  1083. "vendor/bar/baz/cfg.mk": ("vendor/bar/baz/cfg", _cfg1_init),
  1084. }.get("vendor/%s/cfg.mk" % g["MY_PATH"])
  1085. (_varmod, _varmod_init) = _entry if _entry else (None, None)
  1086. if not _varmod_init:
  1087. rblf.mkerror("product.mk", "Cannot find %s" % ("vendor/%s/cfg.mk" % g["MY_PATH"]))
  1088. rblf.inherit(handle, _varmod, _varmod_init)
  1089. `,
  1090. },
  1091. {
  1092. desc: "Dynamic inherit with hint",
  1093. mkname: "product.mk",
  1094. in: `
  1095. MY_PATH:=foo
  1096. #RBC# include_top vendor/foo1
  1097. $(call inherit-product,$(MY_PATH)/cfg.mk)
  1098. #RBC# include_top vendor/foo1
  1099. $(call inherit-product,$(MY_OTHER_PATH))
  1100. #RBC# include_top vendor/foo1
  1101. $(call inherit-product,vendor/$(MY_OTHER_PATH))
  1102. #RBC# include_top vendor/foo1
  1103. $(foreach f,$(MY_MAKEFILES), \
  1104. $(call inherit-product,$(f)))
  1105. `,
  1106. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1107. load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
  1108. def init(g, handle):
  1109. cfg = rblf.cfg(handle)
  1110. g["MY_PATH"] = "foo"
  1111. _entry = {
  1112. "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
  1113. }.get("%s/cfg.mk" % g["MY_PATH"])
  1114. (_varmod, _varmod_init) = _entry if _entry else (None, None)
  1115. if not _varmod_init:
  1116. rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
  1117. rblf.inherit(handle, _varmod, _varmod_init)
  1118. _entry = {
  1119. "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
  1120. }.get(g.get("MY_OTHER_PATH", ""))
  1121. (_varmod, _varmod_init) = _entry if _entry else (None, None)
  1122. if not _varmod_init:
  1123. rblf.mkerror("product.mk", "Cannot find %s" % (g.get("MY_OTHER_PATH", "")))
  1124. rblf.inherit(handle, _varmod, _varmod_init)
  1125. _entry = {
  1126. "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
  1127. }.get("vendor/%s" % g.get("MY_OTHER_PATH", ""))
  1128. (_varmod, _varmod_init) = _entry if _entry else (None, None)
  1129. if not _varmod_init:
  1130. rblf.mkerror("product.mk", "Cannot find %s" % ("vendor/%s" % g.get("MY_OTHER_PATH", "")))
  1131. rblf.inherit(handle, _varmod, _varmod_init)
  1132. for f in rblf.words(g.get("MY_MAKEFILES", "")):
  1133. _entry = {
  1134. "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
  1135. }.get(f)
  1136. (_varmod, _varmod_init) = _entry if _entry else (None, None)
  1137. if not _varmod_init:
  1138. rblf.mkerror("product.mk", "Cannot find %s" % (f))
  1139. rblf.inherit(handle, _varmod, _varmod_init)
  1140. `,
  1141. },
  1142. {
  1143. desc: "Dynamic inherit with duplicated hint",
  1144. mkname: "product.mk",
  1145. in: `
  1146. MY_PATH:=foo
  1147. #RBC# include_top vendor/foo1
  1148. $(call inherit-product,$(MY_PATH)/cfg.mk)
  1149. #RBC# include_top vendor/foo1
  1150. #RBC# include_top vendor/foo1
  1151. $(call inherit-product,$(MY_PATH)/cfg.mk)
  1152. `,
  1153. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1154. load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
  1155. def init(g, handle):
  1156. cfg = rblf.cfg(handle)
  1157. g["MY_PATH"] = "foo"
  1158. _entry = {
  1159. "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
  1160. }.get("%s/cfg.mk" % g["MY_PATH"])
  1161. (_varmod, _varmod_init) = _entry if _entry else (None, None)
  1162. if not _varmod_init:
  1163. rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
  1164. rblf.inherit(handle, _varmod, _varmod_init)
  1165. _entry = {
  1166. "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
  1167. }.get("%s/cfg.mk" % g["MY_PATH"])
  1168. (_varmod, _varmod_init) = _entry if _entry else (None, None)
  1169. if not _varmod_init:
  1170. rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
  1171. rblf.inherit(handle, _varmod, _varmod_init)
  1172. `,
  1173. },
  1174. {
  1175. desc: "Dynamic inherit path that lacks hint",
  1176. mkname: "product.mk",
  1177. in: `
  1178. #RBC# include_top foo
  1179. $(call inherit-product,$(MY_VAR)/font.mk)
  1180. #RBC# include_top foo
  1181. # There's some space and even this comment between the include_top and the inherit-product
  1182. $(call inherit-product,$(MY_VAR)/font.mk)
  1183. $(call inherit-product,$(MY_VAR)/font.mk)
  1184. `,
  1185. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1186. load("//foo:font.star|init", _font_init = "init")
  1187. load("//bar:font.star|init", _font1_init = "init")
  1188. def init(g, handle):
  1189. cfg = rblf.cfg(handle)
  1190. _entry = {
  1191. "foo/font.mk": ("foo/font", _font_init),
  1192. }.get("%s/font.mk" % g.get("MY_VAR", ""))
  1193. (_varmod, _varmod_init) = _entry if _entry else (None, None)
  1194. if not _varmod_init:
  1195. rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
  1196. rblf.inherit(handle, _varmod, _varmod_init)
  1197. # There's some space and even this comment between the include_top and the inherit-product
  1198. _entry = {
  1199. "foo/font.mk": ("foo/font", _font_init),
  1200. }.get("%s/font.mk" % g.get("MY_VAR", ""))
  1201. (_varmod, _varmod_init) = _entry if _entry else (None, None)
  1202. if not _varmod_init:
  1203. rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
  1204. rblf.inherit(handle, _varmod, _varmod_init)
  1205. rblf.mkwarning("product.mk:11", "Please avoid starting an include path with a variable. See https://source.android.com/setup/build/bazel/product_config/issues/includes for details.")
  1206. _entry = {
  1207. "foo/font.mk": ("foo/font", _font_init),
  1208. "bar/font.mk": ("bar/font", _font1_init),
  1209. }.get("%s/font.mk" % g.get("MY_VAR", ""))
  1210. (_varmod, _varmod_init) = _entry if _entry else (None, None)
  1211. if not _varmod_init:
  1212. rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
  1213. rblf.inherit(handle, _varmod, _varmod_init)
  1214. `,
  1215. },
  1216. {
  1217. desc: "Ignore make rules",
  1218. mkname: "product.mk",
  1219. in: `
  1220. foo: PRIVATE_VARIABLE = some_tool $< $@
  1221. foo: foo.c
  1222. gcc -o $@ $*`,
  1223. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1224. def init(g, handle):
  1225. cfg = rblf.cfg(handle)
  1226. rblf.mk2rbc_error("product.mk:2", "Only simple variables are handled")
  1227. rblf.mk2rbc_error("product.mk:3", "unsupported line rule: foo: foo.c\n#gcc -o $@ $*")
  1228. `,
  1229. },
  1230. {
  1231. desc: "Flag override",
  1232. mkname: "product.mk",
  1233. in: `
  1234. override FOO:=`,
  1235. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1236. def init(g, handle):
  1237. cfg = rblf.cfg(handle)
  1238. rblf.mk2rbc_error("product.mk:2", "cannot handle override directive")
  1239. `,
  1240. },
  1241. {
  1242. desc: "Bad expression",
  1243. mkname: "build/product.mk",
  1244. in: `
  1245. ifeq (,$(call foobar))
  1246. endif
  1247. my_sources := $(local-generated-sources-dir)
  1248. `,
  1249. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1250. def init(g, handle):
  1251. cfg = rblf.cfg(handle)
  1252. if rblf.mk2rbc_error("build/product.mk:2", "cannot handle invoking foobar"):
  1253. pass
  1254. _my_sources = rblf.mk2rbc_error("build/product.mk:4", "local-generated-sources-dir is not supported")
  1255. `,
  1256. },
  1257. {
  1258. desc: "if expression",
  1259. mkname: "product.mk",
  1260. in: `
  1261. TEST_VAR := foo
  1262. TEST_VAR_LIST := foo
  1263. TEST_VAR_LIST += bar
  1264. TEST_VAR_2 := $(if $(TEST_VAR),bar)
  1265. TEST_VAR_3 := $(if $(TEST_VAR),bar,baz)
  1266. TEST_VAR_4 := $(if $(TEST_VAR),$(TEST_VAR_LIST))
  1267. `,
  1268. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1269. def init(g, handle):
  1270. cfg = rblf.cfg(handle)
  1271. g["TEST_VAR"] = "foo"
  1272. g["TEST_VAR_LIST"] = ["foo"]
  1273. g["TEST_VAR_LIST"] += ["bar"]
  1274. g["TEST_VAR_2"] = ("bar" if g["TEST_VAR"] else "")
  1275. g["TEST_VAR_3"] = ("bar" if g["TEST_VAR"] else "baz")
  1276. g["TEST_VAR_4"] = (g["TEST_VAR_LIST"] if g["TEST_VAR"] else [])
  1277. `,
  1278. },
  1279. {
  1280. desc: "substitution references",
  1281. mkname: "product.mk",
  1282. in: `
  1283. SOURCES := foo.c bar.c
  1284. OBJECTS := $(SOURCES:.c=.o)
  1285. OBJECTS2 := $(SOURCES:%.c=%.o)
  1286. `,
  1287. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1288. def init(g, handle):
  1289. cfg = rblf.cfg(handle)
  1290. g["SOURCES"] = "foo.c bar.c"
  1291. g["OBJECTS"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
  1292. g["OBJECTS2"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
  1293. `,
  1294. },
  1295. {
  1296. desc: "foreach expressions",
  1297. mkname: "product.mk",
  1298. in: `
  1299. BOOT_KERNEL_MODULES := foo.ko bar.ko
  1300. BOOT_KERNEL_MODULES_FILTER := $(foreach m,$(BOOT_KERNEL_MODULES),%/$(m))
  1301. BOOT_KERNEL_MODULES_LIST := foo.ko
  1302. BOOT_KERNEL_MODULES_LIST += bar.ko
  1303. BOOT_KERNEL_MODULES_FILTER_2 := $(foreach m,$(BOOT_KERNEL_MODULES_LIST),%/$(m))
  1304. NESTED_LISTS := $(foreach m,$(SOME_VAR),$(BOOT_KERNEL_MODULES_LIST))
  1305. NESTED_LISTS_2 := $(foreach x,$(SOME_VAR),$(foreach y,$(x),prefix$(y)))
  1306. FOREACH_WITH_IF := $(foreach module,\
  1307. $(BOOT_KERNEL_MODULES_LIST),\
  1308. $(if $(filter $(module),foo.ko),,$(error module "$(module)" has an error!)))
  1309. # Same as above, but not assigning it to a variable allows it to be converted to statements
  1310. $(foreach module,\
  1311. $(BOOT_KERNEL_MODULES_LIST),\
  1312. $(if $(filter $(module),foo.ko),,$(error module "$(module)" has an error!)))
  1313. `,
  1314. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1315. def init(g, handle):
  1316. cfg = rblf.cfg(handle)
  1317. g["BOOT_KERNEL_MODULES"] = "foo.ko bar.ko"
  1318. g["BOOT_KERNEL_MODULES_FILTER"] = ["%%/%s" % m for m in rblf.words(g["BOOT_KERNEL_MODULES"])]
  1319. g["BOOT_KERNEL_MODULES_LIST"] = ["foo.ko"]
  1320. g["BOOT_KERNEL_MODULES_LIST"] += ["bar.ko"]
  1321. g["BOOT_KERNEL_MODULES_FILTER_2"] = ["%%/%s" % m for m in g["BOOT_KERNEL_MODULES_LIST"]]
  1322. g["NESTED_LISTS"] = rblf.flatten_2d_list([g["BOOT_KERNEL_MODULES_LIST"] for m in rblf.words(g.get("SOME_VAR", ""))])
  1323. g["NESTED_LISTS_2"] = rblf.flatten_2d_list([["prefix%s" % y for y in rblf.words(x)] for x in rblf.words(g.get("SOME_VAR", ""))])
  1324. g["FOREACH_WITH_IF"] = [("" if rblf.filter(module, "foo.ko") else rblf.mkerror("product.mk", "module \"%s\" has an error!" % module)) for module in g["BOOT_KERNEL_MODULES_LIST"]]
  1325. # Same as above, but not assigning it to a variable allows it to be converted to statements
  1326. for module in g["BOOT_KERNEL_MODULES_LIST"]:
  1327. if not rblf.filter(module, "foo.ko"):
  1328. rblf.mkerror("product.mk", "module \"%s\" has an error!" % module)
  1329. `,
  1330. },
  1331. {
  1332. desc: "List appended to string",
  1333. mkname: "product.mk",
  1334. in: `
  1335. NATIVE_BRIDGE_PRODUCT_PACKAGES := \
  1336. libnative_bridge_vdso.native_bridge \
  1337. native_bridge_guest_app_process.native_bridge \
  1338. native_bridge_guest_linker.native_bridge
  1339. NATIVE_BRIDGE_MODIFIED_GUEST_LIBS := \
  1340. libaaudio \
  1341. libamidi \
  1342. libandroid \
  1343. libandroid_runtime
  1344. NATIVE_BRIDGE_PRODUCT_PACKAGES += \
  1345. $(addsuffix .native_bridge,$(NATIVE_BRIDGE_ORIG_GUEST_LIBS))
  1346. `,
  1347. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1348. def init(g, handle):
  1349. cfg = rblf.cfg(handle)
  1350. g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] = "libnative_bridge_vdso.native_bridge native_bridge_guest_app_process.native_bridge native_bridge_guest_linker.native_bridge"
  1351. g["NATIVE_BRIDGE_MODIFIED_GUEST_LIBS"] = "libaaudio libamidi libandroid libandroid_runtime"
  1352. g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] += " " + " ".join(rblf.addsuffix(".native_bridge", g.get("NATIVE_BRIDGE_ORIG_GUEST_LIBS", "")))
  1353. `,
  1354. },
  1355. {
  1356. desc: "Math functions",
  1357. mkname: "product.mk",
  1358. in: `
  1359. # Test the math functions defined in build/make/common/math.mk
  1360. ifeq ($(call math_max,2,5),5)
  1361. endif
  1362. ifeq ($(call math_min,2,5),2)
  1363. endif
  1364. ifeq ($(call math_gt_or_eq,2,5),true)
  1365. endif
  1366. ifeq ($(call math_gt,2,5),true)
  1367. endif
  1368. ifeq ($(call math_lt,2,5),true)
  1369. endif
  1370. ifeq ($(call math_gt_or_eq,2,5),)
  1371. endif
  1372. ifeq ($(call math_gt,2,5),)
  1373. endif
  1374. ifeq ($(call math_lt,2,5),)
  1375. endif
  1376. ifeq ($(call math_gt_or_eq,$(MY_VAR), 5),true)
  1377. endif
  1378. ifeq ($(call math_gt_or_eq,$(MY_VAR),$(MY_OTHER_VAR)),true)
  1379. endif
  1380. ifeq ($(call math_gt_or_eq,100$(MY_VAR),10),true)
  1381. endif
  1382. `,
  1383. expected: `# Test the math functions defined in build/make/common/math.mk
  1384. load("//build/make/core:product_config.rbc", "rblf")
  1385. def init(g, handle):
  1386. cfg = rblf.cfg(handle)
  1387. if max(2, 5) == 5:
  1388. pass
  1389. if min(2, 5) == 2:
  1390. pass
  1391. if 2 >= 5:
  1392. pass
  1393. if 2 > 5:
  1394. pass
  1395. if 2 < 5:
  1396. pass
  1397. if 2 < 5:
  1398. pass
  1399. if 2 <= 5:
  1400. pass
  1401. if 2 >= 5:
  1402. pass
  1403. if int(g.get("MY_VAR", "")) >= 5:
  1404. pass
  1405. if int(g.get("MY_VAR", "")) >= int(g.get("MY_OTHER_VAR", "")):
  1406. pass
  1407. if int("100%s" % g.get("MY_VAR", "")) >= 10:
  1408. pass
  1409. `,
  1410. },
  1411. {
  1412. desc: "Type hints",
  1413. mkname: "product.mk",
  1414. in: `
  1415. # Test type hints
  1416. #RBC# type_hint list MY_VAR MY_VAR_2
  1417. # Unsupported type
  1418. #RBC# type_hint bool MY_VAR_3
  1419. # Invalid syntax
  1420. #RBC# type_hint list
  1421. # Duplicated variable
  1422. #RBC# type_hint list MY_VAR_2
  1423. #RBC# type_hint list my-local-var-with-dashes
  1424. #RBC# type_hint string MY_STRING_VAR
  1425. MY_VAR := foo
  1426. MY_VAR_UNHINTED := foo
  1427. # Vars set after other statements still get the hint
  1428. MY_VAR_2 := foo
  1429. # You can't specify a type hint after the first statement
  1430. #RBC# type_hint list MY_VAR_4
  1431. MY_VAR_4 := foo
  1432. my-local-var-with-dashes := foo
  1433. MY_STRING_VAR := $(wildcard foo/bar.mk)
  1434. `,
  1435. expected: `# Test type hints
  1436. # Unsupported type
  1437. load("//build/make/core:product_config.rbc", "rblf")
  1438. def init(g, handle):
  1439. cfg = rblf.cfg(handle)
  1440. rblf.mk2rbc_error("product.mk:5", "Invalid type_hint annotation. Only list/string types are accepted, found bool")
  1441. # Invalid syntax
  1442. rblf.mk2rbc_error("product.mk:7", "Invalid type_hint annotation: list. Must be a variable type followed by a list of variables of that type")
  1443. # Duplicated variable
  1444. rblf.mk2rbc_error("product.mk:9", "Duplicate type hint for variable MY_VAR_2")
  1445. g["MY_VAR"] = ["foo"]
  1446. g["MY_VAR_UNHINTED"] = "foo"
  1447. # Vars set after other statements still get the hint
  1448. g["MY_VAR_2"] = ["foo"]
  1449. # You can't specify a type hint after the first statement
  1450. rblf.mk2rbc_error("product.mk:20", "type_hint annotations must come before the first Makefile statement")
  1451. g["MY_VAR_4"] = "foo"
  1452. _my_local_var_with_dashes = ["foo"]
  1453. g["MY_STRING_VAR"] = " ".join(rblf.expand_wildcard("foo/bar.mk"))
  1454. `,
  1455. },
  1456. {
  1457. desc: "Set LOCAL_PATH to my-dir",
  1458. mkname: "product.mk",
  1459. in: `
  1460. LOCAL_PATH := $(call my-dir)
  1461. `,
  1462. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1463. def init(g, handle):
  1464. cfg = rblf.cfg(handle)
  1465. `,
  1466. },
  1467. {
  1468. desc: "Evals",
  1469. mkname: "product.mk",
  1470. in: `
  1471. $(eval)
  1472. $(eval MY_VAR := foo)
  1473. $(eval # This is a test of eval functions)
  1474. $(eval $(TOO_COMPLICATED) := bar)
  1475. $(eval include foo/font.mk)
  1476. $(eval $(call inherit-product,vendor/foo1/cfg.mk))
  1477. $(foreach x,$(MY_LIST_VAR), \
  1478. $(eval PRODUCT_COPY_FILES += foo/bar/$(x):$(TARGET_COPY_OUT_VENDOR)/etc/$(x)) \
  1479. $(if $(MY_OTHER_VAR),$(eval PRODUCT_COPY_FILES += $(MY_OTHER_VAR):foo/bar/$(x))))
  1480. $(foreach x,$(MY_LIST_VAR), \
  1481. $(eval include foo/$(x).mk))
  1482. `,
  1483. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1484. load("//foo:font.star", _font_init = "init")
  1485. load("//vendor/foo1:cfg.star", _cfg_init = "init")
  1486. def init(g, handle):
  1487. cfg = rblf.cfg(handle)
  1488. g["MY_VAR"] = "foo"
  1489. # This is a test of eval functions
  1490. rblf.mk2rbc_error("product.mk:5", "Eval expression too complex; only assignments, comments, includes, and inherit-products are supported")
  1491. _font_init(g, handle)
  1492. rblf.inherit(handle, "vendor/foo1/cfg", _cfg_init)
  1493. for x in rblf.words(g.get("MY_LIST_VAR", "")):
  1494. rblf.setdefault(handle, "PRODUCT_COPY_FILES")
  1495. cfg["PRODUCT_COPY_FILES"] += ("foo/bar/%s:%s/etc/%s" % (x, g.get("TARGET_COPY_OUT_VENDOR", ""), x)).split()
  1496. if g.get("MY_OTHER_VAR", ""):
  1497. cfg["PRODUCT_COPY_FILES"] += ("%s:foo/bar/%s" % (g.get("MY_OTHER_VAR", ""), x)).split()
  1498. for x in rblf.words(g.get("MY_LIST_VAR", "")):
  1499. _entry = {
  1500. "foo/font.mk": ("foo/font", _font_init),
  1501. }.get("foo/%s.mk" % x)
  1502. (_varmod, _varmod_init) = _entry if _entry else (None, None)
  1503. if not _varmod_init:
  1504. rblf.mkerror("product.mk", "Cannot find %s" % ("foo/%s.mk" % x))
  1505. _varmod_init(g, handle)
  1506. `,
  1507. },
  1508. {
  1509. desc: ".KATI_READONLY",
  1510. mkname: "product.mk",
  1511. in: `
  1512. MY_VAR := foo
  1513. .KATI_READONLY := MY_VAR
  1514. `,
  1515. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1516. def init(g, handle):
  1517. cfg = rblf.cfg(handle)
  1518. g["MY_VAR"] = "foo"
  1519. `,
  1520. },
  1521. {
  1522. desc: "Complicated variable references",
  1523. mkname: "product.mk",
  1524. in: `
  1525. MY_VAR := foo
  1526. MY_VAR_2 := MY_VAR
  1527. MY_VAR_3 := $($(MY_VAR_2))
  1528. MY_VAR_4 := $(foo bar)
  1529. MY_VAR_5 := $($(MY_VAR_2) bar)
  1530. `,
  1531. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1532. def init(g, handle):
  1533. cfg = rblf.cfg(handle)
  1534. g["MY_VAR"] = "foo"
  1535. g["MY_VAR_2"] = "MY_VAR"
  1536. g["MY_VAR_3"] = (cfg).get(g["MY_VAR_2"], (g).get(g["MY_VAR_2"], ""))
  1537. g["MY_VAR_4"] = rblf.mk2rbc_error("product.mk:5", "cannot handle invoking foo")
  1538. g["MY_VAR_5"] = rblf.mk2rbc_error("product.mk:6", "reference is too complex: $(MY_VAR_2) bar")
  1539. `,
  1540. },
  1541. {
  1542. desc: "Conditional functions",
  1543. mkname: "product.mk",
  1544. in: `
  1545. B := foo
  1546. X := $(or $(A))
  1547. X := $(or $(A),$(B))
  1548. X := $(or $(A),$(B),$(C))
  1549. X := $(and $(A))
  1550. X := $(and $(A),$(B))
  1551. X := $(and $(A),$(B),$(C))
  1552. X := $(or $(A),$(B)) Y
  1553. D := $(wildcard *.mk)
  1554. X := $(or $(B),$(D))
  1555. `,
  1556. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1557. def init(g, handle):
  1558. cfg = rblf.cfg(handle)
  1559. g["B"] = "foo"
  1560. g["X"] = g.get("A", "")
  1561. g["X"] = g.get("A", "") or g["B"]
  1562. g["X"] = g.get("A", "") or g["B"] or g.get("C", "")
  1563. g["X"] = g.get("A", "")
  1564. g["X"] = g.get("A", "") and g["B"]
  1565. g["X"] = g.get("A", "") and g["B"] and g.get("C", "")
  1566. g["X"] = "%s Y" % g.get("A", "") or g["B"]
  1567. g["D"] = rblf.expand_wildcard("*.mk")
  1568. g["X"] = rblf.mk2rbc_error("product.mk:12", "Expected all arguments to $(or) or $(and) to have the same type, found \"string\" and \"list\"")
  1569. `,
  1570. },
  1571. }
  1572. var known_variables = []struct {
  1573. name string
  1574. class varClass
  1575. starlarkType
  1576. }{
  1577. {"NATIVE_COVERAGE", VarClassSoong, starlarkTypeBool},
  1578. {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
  1579. {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
  1580. {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
  1581. {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
  1582. {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
  1583. {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
  1584. {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
  1585. {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
  1586. {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
  1587. {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
  1588. {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
  1589. {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
  1590. {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
  1591. {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
  1592. }
  1593. type testMakefileFinder struct {
  1594. fs fs.FS
  1595. root string
  1596. files []string
  1597. }
  1598. func (t *testMakefileFinder) Find(root string) []string {
  1599. if t.files != nil || root == t.root {
  1600. return t.files
  1601. }
  1602. t.files = make([]string, 0)
  1603. fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error {
  1604. if err != nil {
  1605. return err
  1606. }
  1607. if d.IsDir() {
  1608. base := filepath.Base(path)
  1609. if base[0] == '.' && len(base) > 1 {
  1610. return fs.SkipDir
  1611. }
  1612. return nil
  1613. }
  1614. if strings.HasSuffix(path, ".mk") {
  1615. t.files = append(t.files, path)
  1616. }
  1617. return nil
  1618. })
  1619. return t.files
  1620. }
  1621. func TestGood(t *testing.T) {
  1622. for _, v := range known_variables {
  1623. KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
  1624. }
  1625. fs := NewFindMockFS([]string{
  1626. "vendor/foo1/cfg.mk",
  1627. "vendor/bar/baz/cfg.mk",
  1628. "part.mk",
  1629. "foo/font.mk",
  1630. "bar/font.mk",
  1631. })
  1632. for _, test := range testCases {
  1633. t.Run(test.desc,
  1634. func(t *testing.T) {
  1635. ss, err := Convert(Request{
  1636. MkFile: test.mkname,
  1637. Reader: bytes.NewBufferString(test.in),
  1638. OutputSuffix: ".star",
  1639. SourceFS: fs,
  1640. MakefileFinder: &testMakefileFinder{fs: fs},
  1641. })
  1642. if err != nil {
  1643. t.Error(err)
  1644. return
  1645. }
  1646. got := ss.String()
  1647. if got != test.expected {
  1648. t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
  1649. strings.ReplaceAll(test.expected, "\n", "␤\n"),
  1650. strings.ReplaceAll(got, "\n", "␤\n"))
  1651. }
  1652. })
  1653. }
  1654. }