mk2rbc_test.go 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776
  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. # Check that we get as least close to correct line numbers for errors on statements inside evals
  1483. $(eval $(call inherit-product,$(A_VAR)))
  1484. `,
  1485. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1486. load("//foo:font.star", _font_init = "init")
  1487. load("//vendor/foo1:cfg.star", _cfg_init = "init")
  1488. def init(g, handle):
  1489. cfg = rblf.cfg(handle)
  1490. g["MY_VAR"] = "foo"
  1491. # This is a test of eval functions
  1492. rblf.mk2rbc_error("product.mk:5", "Eval expression too complex; only assignments, comments, includes, and inherit-products are supported")
  1493. _font_init(g, handle)
  1494. rblf.inherit(handle, "vendor/foo1/cfg", _cfg_init)
  1495. for x in rblf.words(g.get("MY_LIST_VAR", "")):
  1496. rblf.setdefault(handle, "PRODUCT_COPY_FILES")
  1497. cfg["PRODUCT_COPY_FILES"] += ("foo/bar/%s:%s/etc/%s" % (x, g.get("TARGET_COPY_OUT_VENDOR", ""), x)).split()
  1498. if g.get("MY_OTHER_VAR", ""):
  1499. cfg["PRODUCT_COPY_FILES"] += ("%s:foo/bar/%s" % (g.get("MY_OTHER_VAR", ""), x)).split()
  1500. for x in rblf.words(g.get("MY_LIST_VAR", "")):
  1501. _entry = {
  1502. "foo/font.mk": ("foo/font", _font_init),
  1503. }.get("foo/%s.mk" % x)
  1504. (_varmod, _varmod_init) = _entry if _entry else (None, None)
  1505. if not _varmod_init:
  1506. rblf.mkerror("product.mk", "Cannot find %s" % ("foo/%s.mk" % x))
  1507. _varmod_init(g, handle)
  1508. # Check that we get as least close to correct line numbers for errors on statements inside evals
  1509. rblf.mk2rbc_error("product.mk:17", "inherit-product/include argument is too complex")
  1510. `,
  1511. },
  1512. {
  1513. desc: ".KATI_READONLY",
  1514. mkname: "product.mk",
  1515. in: `
  1516. MY_VAR := foo
  1517. .KATI_READONLY := MY_VAR
  1518. `,
  1519. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1520. def init(g, handle):
  1521. cfg = rblf.cfg(handle)
  1522. g["MY_VAR"] = "foo"
  1523. `,
  1524. },
  1525. {
  1526. desc: "Complicated variable references",
  1527. mkname: "product.mk",
  1528. in: `
  1529. MY_VAR := foo
  1530. MY_VAR_2 := MY_VAR
  1531. MY_VAR_3 := $($(MY_VAR_2))
  1532. MY_VAR_4 := $(foo bar)
  1533. MY_VAR_5 := $($(MY_VAR_2) bar)
  1534. `,
  1535. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1536. def init(g, handle):
  1537. cfg = rblf.cfg(handle)
  1538. g["MY_VAR"] = "foo"
  1539. g["MY_VAR_2"] = "MY_VAR"
  1540. g["MY_VAR_3"] = (cfg).get(g["MY_VAR_2"], (g).get(g["MY_VAR_2"], ""))
  1541. g["MY_VAR_4"] = rblf.mk2rbc_error("product.mk:5", "cannot handle invoking foo")
  1542. g["MY_VAR_5"] = rblf.mk2rbc_error("product.mk:6", "reference is too complex: $(MY_VAR_2) bar")
  1543. `,
  1544. },
  1545. {
  1546. desc: "Conditional functions",
  1547. mkname: "product.mk",
  1548. in: `
  1549. B := foo
  1550. X := $(or $(A))
  1551. X := $(or $(A),$(B))
  1552. X := $(or $(A),$(B),$(C))
  1553. X := $(and $(A))
  1554. X := $(and $(A),$(B))
  1555. X := $(and $(A),$(B),$(C))
  1556. X := $(or $(A),$(B)) Y
  1557. D := $(wildcard *.mk)
  1558. X := $(or $(B),$(D))
  1559. `,
  1560. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1561. def init(g, handle):
  1562. cfg = rblf.cfg(handle)
  1563. g["B"] = "foo"
  1564. g["X"] = g.get("A", "")
  1565. g["X"] = g.get("A", "") or g["B"]
  1566. g["X"] = g.get("A", "") or g["B"] or g.get("C", "")
  1567. g["X"] = g.get("A", "")
  1568. g["X"] = g.get("A", "") and g["B"]
  1569. g["X"] = g.get("A", "") and g["B"] and g.get("C", "")
  1570. g["X"] = "%s Y" % g.get("A", "") or g["B"]
  1571. g["D"] = rblf.expand_wildcard("*.mk")
  1572. g["X"] = rblf.mk2rbc_error("product.mk:12", "Expected all arguments to $(or) or $(and) to have the same type, found \"string\" and \"list\"")
  1573. `,
  1574. },
  1575. {
  1576. desc: "is-lower/is-upper",
  1577. mkname: "product.mk",
  1578. in: `
  1579. X := $(call to-lower,aBc)
  1580. X := $(call to-upper,aBc)
  1581. X := $(call to-lower,$(VAR))
  1582. X := $(call to-upper,$(VAR))
  1583. `,
  1584. expected: `load("//build/make/core:product_config.rbc", "rblf")
  1585. def init(g, handle):
  1586. cfg = rblf.cfg(handle)
  1587. g["X"] = ("aBc").lower()
  1588. g["X"] = ("aBc").upper()
  1589. g["X"] = (g.get("VAR", "")).lower()
  1590. g["X"] = (g.get("VAR", "")).upper()
  1591. `,
  1592. },
  1593. }
  1594. var known_variables = []struct {
  1595. name string
  1596. class varClass
  1597. starlarkType
  1598. }{
  1599. {"NATIVE_COVERAGE", VarClassSoong, starlarkTypeBool},
  1600. {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
  1601. {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
  1602. {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
  1603. {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
  1604. {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
  1605. {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
  1606. {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
  1607. {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
  1608. {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
  1609. {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
  1610. {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
  1611. {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
  1612. {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
  1613. {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
  1614. }
  1615. type testMakefileFinder struct {
  1616. fs fs.FS
  1617. root string
  1618. files []string
  1619. }
  1620. func (t *testMakefileFinder) Find(root string) []string {
  1621. if t.files != nil || root == t.root {
  1622. return t.files
  1623. }
  1624. t.files = make([]string, 0)
  1625. fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error {
  1626. if err != nil {
  1627. return err
  1628. }
  1629. if d.IsDir() {
  1630. base := filepath.Base(path)
  1631. if base[0] == '.' && len(base) > 1 {
  1632. return fs.SkipDir
  1633. }
  1634. return nil
  1635. }
  1636. if strings.HasSuffix(path, ".mk") {
  1637. t.files = append(t.files, path)
  1638. }
  1639. return nil
  1640. })
  1641. return t.files
  1642. }
  1643. func TestGood(t *testing.T) {
  1644. for _, v := range known_variables {
  1645. KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
  1646. }
  1647. fs := NewFindMockFS([]string{
  1648. "vendor/foo1/cfg.mk",
  1649. "vendor/bar/baz/cfg.mk",
  1650. "part.mk",
  1651. "foo/font.mk",
  1652. "bar/font.mk",
  1653. })
  1654. for _, test := range testCases {
  1655. t.Run(test.desc,
  1656. func(t *testing.T) {
  1657. ss, err := Convert(Request{
  1658. MkFile: test.mkname,
  1659. Reader: bytes.NewBufferString(test.in),
  1660. OutputSuffix: ".star",
  1661. SourceFS: fs,
  1662. MakefileFinder: &testMakefileFinder{fs: fs},
  1663. })
  1664. if err != nil {
  1665. t.Error(err)
  1666. return
  1667. }
  1668. got := ss.String()
  1669. if got != test.expected {
  1670. t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
  1671. strings.ReplaceAll(test.expected, "\n", "␤\n"),
  1672. strings.ReplaceAll(got, "\n", "␤\n"))
  1673. }
  1674. })
  1675. }
  1676. }