blob: 0c4d21375a8d4bad714befa66e78ed2727409fe5 [file] [log] [blame]
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package mk2rbc
16
17import (
18 "bytes"
Sasha Smundak6609ba72021-07-22 18:32:56 -070019 "io/fs"
20 "path/filepath"
Sasha Smundakb051c4e2020-11-05 20:45:07 -080021 "strings"
22 "testing"
23)
24
25var testCases = []struct {
26 desc string
27 mkname string
28 in string
29 expected string
30}{
31 {
32 desc: "Comment",
33 mkname: "product.mk",
34 in: `
35# Comment
36# FOO= a\
37 b
38`,
39 expected: `# Comment
40# FOO= a
41# b
42load("//build/make/core:product_config.rbc", "rblf")
43
44def init(g, handle):
45 cfg = rblf.cfg(handle)
46`,
47 },
48 {
49 desc: "Name conversion",
50 mkname: "path/bar-baz.mk",
51 in: `
52# Comment
53`,
54 expected: `# Comment
55load("//build/make/core:product_config.rbc", "rblf")
56
57def init(g, handle):
58 cfg = rblf.cfg(handle)
59`,
60 },
61 {
62 desc: "Item variable",
63 mkname: "pixel3.mk",
64 in: `
65PRODUCT_NAME := Pixel 3
66PRODUCT_MODEL :=
67local_var = foo
Cole Faust3c4fc992022-02-28 16:05:01 -080068local-var-with-dashes := bar
69$(warning local-var-with-dashes: $(local-var-with-dashes))
70GLOBAL-VAR-WITH-DASHES := baz
71$(warning GLOBAL-VAR-WITH-DASHES: $(GLOBAL-VAR-WITH-DASHES))
Sasha Smundakb051c4e2020-11-05 20:45:07 -080072`,
73 expected: `load("//build/make/core:product_config.rbc", "rblf")
74
75def init(g, handle):
76 cfg = rblf.cfg(handle)
77 cfg["PRODUCT_NAME"] = "Pixel 3"
78 cfg["PRODUCT_MODEL"] = ""
79 _local_var = "foo"
Cole Faust3c4fc992022-02-28 16:05:01 -080080 _local_var_with_dashes = "bar"
81 rblf.mkwarning("pixel3.mk", "local-var-with-dashes: %s" % _local_var_with_dashes)
82 g["GLOBAL-VAR-WITH-DASHES"] = "baz"
83 rblf.mkwarning("pixel3.mk", "GLOBAL-VAR-WITH-DASHES: %s" % g["GLOBAL-VAR-WITH-DASHES"])
Sasha Smundakb051c4e2020-11-05 20:45:07 -080084`,
85 },
86 {
87 desc: "List variable",
88 mkname: "pixel4.mk",
89 in: `
90PRODUCT_PACKAGES = package1 package2
91PRODUCT_COPY_FILES += file2:target
92PRODUCT_PACKAGES += package3
93PRODUCT_COPY_FILES =
94`,
95 expected: `load("//build/make/core:product_config.rbc", "rblf")
96
97def init(g, handle):
98 cfg = rblf.cfg(handle)
99 cfg["PRODUCT_PACKAGES"] = [
100 "package1",
101 "package2",
102 ]
103 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
104 cfg["PRODUCT_COPY_FILES"] += ["file2:target"]
105 cfg["PRODUCT_PACKAGES"] += ["package3"]
106 cfg["PRODUCT_COPY_FILES"] = []
107`,
108 },
109 {
110 desc: "Unknown function",
111 mkname: "product.mk",
112 in: `
Sasha Smundak6609ba72021-07-22 18:32:56 -0700113PRODUCT_NAME := $(call foo1, bar)
114PRODUCT_NAME := $(call foo0)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800115`,
Sasha Smundak422b6142021-11-11 18:31:59 -0800116 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800117
118def init(g, handle):
119 cfg = rblf.cfg(handle)
Cole Faust1e275862022-04-26 14:28:04 -0700120 cfg["PRODUCT_NAME"] = rblf.mk2rbc_error("product.mk:2", "cannot handle invoking foo1")
121 cfg["PRODUCT_NAME"] = rblf.mk2rbc_error("product.mk:3", "cannot handle invoking foo0")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800122`,
123 },
124 {
125 desc: "Inherit configuration always",
126 mkname: "product.mk",
127 in: `
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800128$(call inherit-product, part.mk)
Sasha Smundak868c5e32021-09-23 16:20:58 -0700129ifdef PRODUCT_NAME
130$(call inherit-product, part1.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800131else # Comment
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800132$(call inherit-product, $(LOCAL_PATH)/part.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800133endif
134`,
135 expected: `load("//build/make/core:product_config.rbc", "rblf")
136load(":part.star", _part_init = "init")
Sasha Smundak868c5e32021-09-23 16:20:58 -0700137load(":part1.star|init", _part1_init = "init")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800138
139def init(g, handle):
140 cfg = rblf.cfg(handle)
Sasha Smundak868c5e32021-09-23 16:20:58 -0700141 rblf.inherit(handle, "part", _part_init)
Cole Faust71514c02022-01-27 17:21:41 -0800142 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800143 if not _part1_init:
144 rblf.mkerror("product.mk", "Cannot find %s" % (":part1.star"))
Sasha Smundak868c5e32021-09-23 16:20:58 -0700145 rblf.inherit(handle, "part1", _part1_init)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800146 else:
147 # Comment
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800148 rblf.inherit(handle, "part", _part_init)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800149`,
150 },
151 {
152 desc: "Inherit configuration if it exists",
153 mkname: "product.mk",
154 in: `
155$(call inherit-product-if-exists, part.mk)
156`,
157 expected: `load("//build/make/core:product_config.rbc", "rblf")
158load(":part.star|init", _part_init = "init")
159
160def init(g, handle):
161 cfg = rblf.cfg(handle)
Sasha Smundak6609ba72021-07-22 18:32:56 -0700162 if _part_init:
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800163 rblf.inherit(handle, "part", _part_init)
164`,
165 },
166
167 {
168 desc: "Include configuration",
169 mkname: "product.mk",
170 in: `
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800171include part.mk
Sasha Smundak868c5e32021-09-23 16:20:58 -0700172ifdef PRODUCT_NAME
173include part1.mk
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800174else
Sasha Smundak868c5e32021-09-23 16:20:58 -0700175-include $(LOCAL_PATH)/part1.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800176endif
177`,
178 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundak868c5e32021-09-23 16:20:58 -0700179load(":part.star", _part_init = "init")
180load(":part1.star|init", _part1_init = "init")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800181
182def init(g, handle):
183 cfg = rblf.cfg(handle)
Sasha Smundak868c5e32021-09-23 16:20:58 -0700184 _part_init(g, handle)
Cole Faust71514c02022-01-27 17:21:41 -0800185 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800186 if not _part1_init:
187 rblf.mkerror("product.mk", "Cannot find %s" % (":part1.star"))
Sasha Smundak868c5e32021-09-23 16:20:58 -0700188 _part1_init(g, handle)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800189 else:
Sasha Smundak868c5e32021-09-23 16:20:58 -0700190 if _part1_init != None:
191 _part1_init(g, handle)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800192`,
193 },
194
195 {
Cole Faustb0b24572023-10-06 11:53:50 -0700196 desc: "Include with trailing whitespace",
197 mkname: "product.mk",
198 in: `
199# has a trailing whitespace after cfg.mk
200include vendor/$(foo)/cfg.mk
201`,
202 expected: `# has a trailing whitespace after cfg.mk
203load("//build/make/core:product_config.rbc", "rblf")
204load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
205load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init")
206
207def init(g, handle):
208 cfg = rblf.cfg(handle)
209 _entry = {
210 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
211 "vendor/bar/baz/cfg.mk": ("vendor/bar/baz/cfg", _cfg1_init),
212 }.get("vendor/%s/cfg.mk" % _foo)
213 (_varmod, _varmod_init) = _entry if _entry else (None, None)
214 if not _varmod_init:
215 rblf.mkerror("product.mk", "Cannot find %s" % ("vendor/%s/cfg.mk" % _foo))
216 _varmod_init(g, handle)
217`,
218 },
219
220 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800221 desc: "Synonymous inherited configurations",
222 mkname: "path/product.mk",
223 in: `
Sasha Smundak6609ba72021-07-22 18:32:56 -0700224$(call inherit-product, */font.mk)
Cole Faust62e05112022-04-05 17:56:11 -0700225$(call inherit-product, $(sort $(wildcard */font.mk)))
226$(call inherit-product, $(wildcard */font.mk))
227
228include */font.mk
229include $(sort $(wildcard */font.mk))
230include $(wildcard */font.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800231`,
232 expected: `load("//build/make/core:product_config.rbc", "rblf")
Cole Faust62e05112022-04-05 17:56:11 -0700233load("//bar:font.star", _font_init = "init")
234load("//foo:font.star", _font1_init = "init")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800235
236def init(g, handle):
237 cfg = rblf.cfg(handle)
Cole Faust62e05112022-04-05 17:56:11 -0700238 rblf.inherit(handle, "bar/font", _font_init)
239 rblf.inherit(handle, "foo/font", _font1_init)
240 rblf.inherit(handle, "bar/font", _font_init)
241 rblf.inherit(handle, "foo/font", _font1_init)
242 rblf.inherit(handle, "bar/font", _font_init)
243 rblf.inherit(handle, "foo/font", _font1_init)
244 _font_init(g, handle)
245 _font1_init(g, handle)
246 _font_init(g, handle)
247 _font1_init(g, handle)
248 _font_init(g, handle)
249 _font1_init(g, handle)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800250`,
251 },
252 {
253 desc: "Directive define",
254 mkname: "product.mk",
255 in: `
256define some-macro
257 $(info foo)
258endef
259`,
Sasha Smundak422b6142021-11-11 18:31:59 -0800260 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800261
262def init(g, handle):
263 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -0800264 rblf.mk2rbc_error("product.mk:2", "define is not supported: some-macro")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800265`,
266 },
267 {
268 desc: "Ifdef",
269 mkname: "product.mk",
270 in: `
271ifdef PRODUCT_NAME
272 PRODUCT_NAME = gizmo
273else
274endif
Sasha Smundakc4fa93e2021-11-05 14:38:46 -0700275local_var :=
276ifdef local_var
277endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800278`,
279 expected: `load("//build/make/core:product_config.rbc", "rblf")
280
281def init(g, handle):
282 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800283 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800284 cfg["PRODUCT_NAME"] = "gizmo"
285 else:
286 pass
Sasha Smundakc4fa93e2021-11-05 14:38:46 -0700287 _local_var = ""
288 if _local_var:
289 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800290`,
291 },
292 {
293 desc: "Simple functions",
294 mkname: "product.mk",
295 in: `
296$(warning this is the warning)
297$(warning)
Cole Fauste309a912022-03-16 13:42:34 -0700298$(warning # this warning starts with a pound)
299$(warning this warning has a # in the middle)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800300$(info this is the info)
301$(error this is the error)
302PRODUCT_NAME:=$(shell echo *)
303`,
304 expected: `load("//build/make/core:product_config.rbc", "rblf")
305
306def init(g, handle):
307 cfg = rblf.cfg(handle)
308 rblf.mkwarning("product.mk", "this is the warning")
309 rblf.mkwarning("product.mk", "")
Cole Fauste309a912022-03-16 13:42:34 -0700310 rblf.mkwarning("product.mk", "# this warning starts with a pound")
311 rblf.mkwarning("product.mk", "this warning has a # in the middle")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800312 rblf.mkinfo("product.mk", "this is the info")
313 rblf.mkerror("product.mk", "this is the error")
314 cfg["PRODUCT_NAME"] = rblf.shell("echo *")
315`,
316 },
317 {
318 desc: "Empty if",
319 mkname: "product.mk",
320 in: `
321ifdef PRODUCT_NAME
322# Comment
Sasha Smundak6609ba72021-07-22 18:32:56 -0700323else
Sasha Smundak02183cf2021-08-16 13:36:11 -0700324 TARGET_COPY_OUT_RECOVERY := foo
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800325endif
326`,
327 expected: `load("//build/make/core:product_config.rbc", "rblf")
328
329def init(g, handle):
330 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800331 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800332 # Comment
333 pass
Sasha Smundak6609ba72021-07-22 18:32:56 -0700334 else:
Sasha Smundak422b6142021-11-11 18:31:59 -0800335 rblf.mk2rbc_error("product.mk:5", "cannot set predefined variable TARGET_COPY_OUT_RECOVERY to \"foo\", its value should be \"recovery\"")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800336`,
337 },
338 {
339 desc: "if/else/endif",
340 mkname: "product.mk",
341 in: `
342ifndef PRODUCT_NAME
343 PRODUCT_NAME=gizmo1
344else
345 PRODUCT_NAME=gizmo2
346endif
347`,
348 expected: `load("//build/make/core:product_config.rbc", "rblf")
349
350def init(g, handle):
351 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800352 if not cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800353 cfg["PRODUCT_NAME"] = "gizmo1"
354 else:
355 cfg["PRODUCT_NAME"] = "gizmo2"
356`,
357 },
358 {
359 desc: "else if",
360 mkname: "product.mk",
361 in: `
362ifdef PRODUCT_NAME
363 PRODUCT_NAME = gizmo
364else ifndef PRODUCT_PACKAGES # Comment
365endif
366 `,
367 expected: `load("//build/make/core:product_config.rbc", "rblf")
368
369def init(g, handle):
370 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800371 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800372 cfg["PRODUCT_NAME"] = "gizmo"
Cole Faust71514c02022-01-27 17:21:41 -0800373 elif not cfg.get("PRODUCT_PACKAGES", []):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800374 # Comment
375 pass
376`,
377 },
378 {
379 desc: "ifeq / ifneq",
380 mkname: "product.mk",
381 in: `
382ifeq (aosp_arm, $(TARGET_PRODUCT))
383 PRODUCT_MODEL = pix2
384else
385 PRODUCT_MODEL = pix21
386endif
387ifneq (aosp_x86, $(TARGET_PRODUCT))
388 PRODUCT_MODEL = pix3
389endif
390`,
391 expected: `load("//build/make/core:product_config.rbc", "rblf")
392
393def init(g, handle):
394 cfg = rblf.cfg(handle)
395 if "aosp_arm" == g["TARGET_PRODUCT"]:
396 cfg["PRODUCT_MODEL"] = "pix2"
397 else:
398 cfg["PRODUCT_MODEL"] = "pix21"
399 if "aosp_x86" != g["TARGET_PRODUCT"]:
400 cfg["PRODUCT_MODEL"] = "pix3"
401`,
402 },
403 {
Cole Faustf8320212021-11-10 15:05:07 -0800404 desc: "ifeq with soong_config_get",
405 mkname: "product.mk",
406 in: `
407ifeq (true,$(call soong_config_get,art_module,source_build))
408endif
409`,
410 expected: `load("//build/make/core:product_config.rbc", "rblf")
411
412def init(g, handle):
413 cfg = rblf.cfg(handle)
414 if "true" == rblf.soong_config_get(g, "art_module", "source_build"):
415 pass
416`,
417 },
418 {
Cole Faustf1f44d32021-11-16 14:52:12 -0800419 desc: "ifeq with $(NATIVE_COVERAGE)",
420 mkname: "product.mk",
421 in: `
422ifeq ($(NATIVE_COVERAGE),true)
423endif
424`,
425 expected: `load("//build/make/core:product_config.rbc", "rblf")
426
427def init(g, handle):
428 cfg = rblf.cfg(handle)
429 if g.get("NATIVE_COVERAGE", False):
430 pass
431`,
432 },
433 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800434 desc: "Check filter result",
435 mkname: "product.mk",
436 in: `
437ifeq (,$(filter userdebug eng, $(TARGET_BUILD_VARIANT)))
438endif
439ifneq (,$(filter userdebug,$(TARGET_BUILD_VARIANT))
440endif
441ifneq (,$(filter plaf,$(PLATFORM_LIST)))
442endif
443ifeq ($(TARGET_BUILD_VARIANT), $(filter $(TARGET_BUILD_VARIANT), userdebug eng))
444endif
Cole Faust9932f752022-02-08 11:56:25 -0800445ifneq (, $(filter $(TARGET_BUILD_VARIANT), userdebug eng))
446endif
447ifneq (,$(filter userdebug eng, $(TARGET_BUILD_VARIANT)))
448endif
Sasha Smundak0554d762021-07-08 18:26:12 -0700449ifneq (,$(filter true, $(v1)$(v2)))
450endif
Sasha Smundak5f463be2021-09-15 18:43:36 -0700451ifeq (,$(filter barbet coral%,$(TARGET_PRODUCT)))
452else ifneq (,$(filter barbet%,$(TARGET_PRODUCT)))
453endif
Cole Fausteec0d812021-12-06 16:23:51 -0800454ifeq (,$(filter-out sunfish_kasan, $(TARGET_PRODUCT)))
455endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800456`,
457 expected: `load("//build/make/core:product_config.rbc", "rblf")
458
459def init(g, handle):
460 cfg = rblf.cfg(handle)
Sasha Smundak5f463be2021-09-15 18:43:36 -0700461 if not rblf.filter("userdebug eng", g["TARGET_BUILD_VARIANT"]):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800462 pass
Sasha Smundak5f463be2021-09-15 18:43:36 -0700463 if rblf.filter("userdebug", g["TARGET_BUILD_VARIANT"]):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800464 pass
465 if "plaf" in g.get("PLATFORM_LIST", []):
466 pass
Cole Faust9932f752022-02-08 11:56:25 -0800467 if g["TARGET_BUILD_VARIANT"] == " ".join(rblf.filter(g["TARGET_BUILD_VARIANT"], "userdebug eng")):
468 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800469 if g["TARGET_BUILD_VARIANT"] in ["userdebug", "eng"]:
470 pass
Cole Faust9932f752022-02-08 11:56:25 -0800471 if rblf.filter("userdebug eng", g["TARGET_BUILD_VARIANT"]):
472 pass
Sasha Smundak5f463be2021-09-15 18:43:36 -0700473 if rblf.filter("true", "%s%s" % (_v1, _v2)):
474 pass
475 if not rblf.filter("barbet coral%", g["TARGET_PRODUCT"]):
476 pass
477 elif rblf.filter("barbet%", g["TARGET_PRODUCT"]):
Sasha Smundak0554d762021-07-08 18:26:12 -0700478 pass
Cole Fausteec0d812021-12-06 16:23:51 -0800479 if not rblf.filter_out("sunfish_kasan", g["TARGET_PRODUCT"]):
480 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800481`,
482 },
483 {
484 desc: "Get filter result",
485 mkname: "product.mk",
486 in: `
487PRODUCT_LIST2=$(filter-out %/foo.ko,$(wildcard path/*.ko))
488`,
489 expected: `load("//build/make/core:product_config.rbc", "rblf")
490
491def init(g, handle):
492 cfg = rblf.cfg(handle)
493 cfg["PRODUCT_LIST2"] = rblf.filter_out("%/foo.ko", rblf.expand_wildcard("path/*.ko"))
494`,
495 },
496 {
497 desc: "filter $(VAR), values",
498 mkname: "product.mk",
499 in: `
500ifeq (,$(filter $(TARGET_PRODUCT), yukawa_gms yukawa_sei510_gms)
501 ifneq (,$(filter $(TARGET_PRODUCT), yukawa_gms)
502 endif
503endif
504
505`,
506 expected: `load("//build/make/core:product_config.rbc", "rblf")
507
508def init(g, handle):
509 cfg = rblf.cfg(handle)
510 if g["TARGET_PRODUCT"] not in ["yukawa_gms", "yukawa_sei510_gms"]:
Sasha Smundak0554d762021-07-08 18:26:12 -0700511 if g["TARGET_PRODUCT"] == "yukawa_gms":
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800512 pass
513`,
514 },
515 {
Sasha Smundak0554d762021-07-08 18:26:12 -0700516 desc: "filter $(V1), $(V2)",
517 mkname: "product.mk",
518 in: `
519ifneq (, $(filter $(PRODUCT_LIST), $(TARGET_PRODUCT)))
520endif
521`,
522 expected: `load("//build/make/core:product_config.rbc", "rblf")
523
524def init(g, handle):
525 cfg = rblf.cfg(handle)
Sasha Smundak468e11f2021-08-26 09:10:23 -0700526 if rblf.filter(g.get("PRODUCT_LIST", []), g["TARGET_PRODUCT"]):
Sasha Smundak0554d762021-07-08 18:26:12 -0700527 pass
528`,
529 },
530 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800531 desc: "ifeq",
532 mkname: "product.mk",
533 in: `
534ifeq (aosp, $(TARGET_PRODUCT)) # Comment
535else ifneq (, $(TARGET_PRODUCT))
536endif
537`,
538 expected: `load("//build/make/core:product_config.rbc", "rblf")
539
540def init(g, handle):
541 cfg = rblf.cfg(handle)
542 if "aosp" == g["TARGET_PRODUCT"]:
543 # Comment
544 pass
545 elif g["TARGET_PRODUCT"]:
546 pass
547`,
548 },
549 {
550 desc: "Nested if",
551 mkname: "product.mk",
552 in: `
553ifdef PRODUCT_NAME
554 PRODUCT_PACKAGES = pack-if0
555 ifdef PRODUCT_MODEL
556 PRODUCT_PACKAGES = pack-if-if
557 else ifdef PRODUCT_NAME
558 PRODUCT_PACKAGES = pack-if-elif
559 else
560 PRODUCT_PACKAGES = pack-if-else
561 endif
562 PRODUCT_PACKAGES = pack-if
563else ifneq (,$(TARGET_PRODUCT))
564 PRODUCT_PACKAGES = pack-elif
565else
566 PRODUCT_PACKAGES = pack-else
567endif
568`,
569 expected: `load("//build/make/core:product_config.rbc", "rblf")
570
571def init(g, handle):
572 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800573 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800574 cfg["PRODUCT_PACKAGES"] = ["pack-if0"]
Cole Faust71514c02022-01-27 17:21:41 -0800575 if cfg.get("PRODUCT_MODEL", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800576 cfg["PRODUCT_PACKAGES"] = ["pack-if-if"]
Cole Faust71514c02022-01-27 17:21:41 -0800577 elif cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800578 cfg["PRODUCT_PACKAGES"] = ["pack-if-elif"]
579 else:
580 cfg["PRODUCT_PACKAGES"] = ["pack-if-else"]
581 cfg["PRODUCT_PACKAGES"] = ["pack-if"]
582 elif g["TARGET_PRODUCT"]:
583 cfg["PRODUCT_PACKAGES"] = ["pack-elif"]
584 else:
585 cfg["PRODUCT_PACKAGES"] = ["pack-else"]
586`,
587 },
588 {
589 desc: "Wildcard",
590 mkname: "product.mk",
591 in: `
592ifeq (,$(wildcard foo.mk))
593endif
594ifneq (,$(wildcard foo*.mk))
595endif
Cole Fausta99afdf2022-04-26 12:06:49 -0700596ifeq (foo1.mk foo2.mk barxyz.mk,$(wildcard foo*.mk bar*.mk))
597endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800598`,
599 expected: `load("//build/make/core:product_config.rbc", "rblf")
600
601def init(g, handle):
602 cfg = rblf.cfg(handle)
Cole Fausta99afdf2022-04-26 12:06:49 -0700603 if not rblf.expand_wildcard("foo.mk"):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800604 pass
Cole Fausta99afdf2022-04-26 12:06:49 -0700605 if rblf.expand_wildcard("foo*.mk"):
606 pass
Cole Faust72374fc2022-05-05 11:45:04 -0700607 if rblf.expand_wildcard("foo*.mk bar*.mk") == ["foo1.mk", "foo2.mk", "barxyz.mk"]:
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800608 pass
609`,
610 },
611 {
Cole Faustf8320212021-11-10 15:05:07 -0800612 desc: "if with interpolation",
613 mkname: "product.mk",
614 in: `
615ifeq ($(VARIABLE1)text$(VARIABLE2),true)
616endif
617`,
618 expected: `load("//build/make/core:product_config.rbc", "rblf")
619
620def init(g, handle):
621 cfg = rblf.cfg(handle)
622 if "%stext%s" % (g.get("VARIABLE1", ""), g.get("VARIABLE2", "")) == "true":
623 pass
624`,
625 },
626 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800627 desc: "ifneq $(X),true",
628 mkname: "product.mk",
629 in: `
630ifneq ($(VARIABLE),true)
631endif
632`,
633 expected: `load("//build/make/core:product_config.rbc", "rblf")
634
635def init(g, handle):
636 cfg = rblf.cfg(handle)
637 if g.get("VARIABLE", "") != "true":
638 pass
639`,
640 },
641 {
642 desc: "Const neq",
643 mkname: "product.mk",
644 in: `
645ifneq (1,0)
646endif
647`,
648 expected: `load("//build/make/core:product_config.rbc", "rblf")
649
650def init(g, handle):
651 cfg = rblf.cfg(handle)
652 if "1" != "0":
653 pass
654`,
655 },
656 {
657 desc: "is-board calls",
658 mkname: "product.mk",
659 in: `
660ifeq ($(call is-board-platform-in-list,msm8998), true)
661else ifneq ($(call is-board-platform,copper),true)
662else ifneq ($(call is-vendor-board-platform,QCOM),true)
663else ifeq ($(call is-product-in-list, $(PLATFORM_LIST)), true)
664endif
665`,
666 expected: `load("//build/make/core:product_config.rbc", "rblf")
667
668def init(g, handle):
669 cfg = rblf.cfg(handle)
Cole Faustb2e0b602022-01-07 15:46:58 -0800670 if rblf.board_platform_in(g, "msm8998"):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800671 pass
Cole Faustb2e0b602022-01-07 15:46:58 -0800672 elif not rblf.board_platform_is(g, "copper"):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800673 pass
Cole Faustf0632662022-04-07 13:59:24 -0700674 elif g.get("TARGET_BOARD_PLATFORM", "") not in g.get("QCOM_BOARD_PLATFORMS", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800675 pass
676 elif g["TARGET_PRODUCT"] in g.get("PLATFORM_LIST", []):
677 pass
678`,
679 },
680 {
Sasha Smundak3a9b8e82021-08-25 14:11:04 -0700681 desc: "new is-board calls",
682 mkname: "product.mk",
683 in: `
684ifneq (,$(call is-board-platform-in-list2,msm8998 $(X))
685else ifeq (,$(call is-board-platform2,copper)
686else ifneq (,$(call is-vendor-board-qcom))
687endif
688`,
689 expected: `load("//build/make/core:product_config.rbc", "rblf")
690
691def init(g, handle):
692 cfg = rblf.cfg(handle)
693 if rblf.board_platform_in(g, "msm8998 %s" % g.get("X", "")):
694 pass
695 elif not rblf.board_platform_is(g, "copper"):
696 pass
Cole Faustf0632662022-04-07 13:59:24 -0700697 elif g.get("TARGET_BOARD_PLATFORM", "") in g.get("QCOM_BOARD_PLATFORMS", ""):
Sasha Smundak3a9b8e82021-08-25 14:11:04 -0700698 pass
699`,
700 },
701 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800702 desc: "findstring call",
703 mkname: "product.mk",
704 in: `
Cole Faust0e9418c2021-12-13 16:33:25 -0800705result := $(findstring a,a b c)
706result := $(findstring b,x y z)
707`,
708 expected: `load("//build/make/core:product_config.rbc", "rblf")
709
710def init(g, handle):
711 cfg = rblf.cfg(handle)
712 _result = rblf.findstring("a", "a b c")
713 _result = rblf.findstring("b", "x y z")
714`,
715 },
716 {
717 desc: "findstring in if statement",
718 mkname: "product.mk",
719 in: `
720ifeq ($(findstring foo,$(PRODUCT_PACKAGES)),)
721endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800722ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),)
723endif
Cole Faust0e9418c2021-12-13 16:33:25 -0800724ifeq ($(findstring foo,$(PRODUCT_PACKAGES)),foo)
725endif
726ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),foo)
727endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800728`,
729 expected: `load("//build/make/core:product_config.rbc", "rblf")
730
731def init(g, handle):
732 cfg = rblf.cfg(handle)
Cole Faust0e9418c2021-12-13 16:33:25 -0800733 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") == -1:
734 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800735 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
736 pass
Cole Faust0e9418c2021-12-13 16:33:25 -0800737 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
738 pass
739 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") == -1:
740 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800741`,
742 },
743 {
744 desc: "rhs call",
745 mkname: "product.mk",
746 in: `
747PRODUCT_COPY_FILES = $(call add-to-product-copy-files-if-exists, path:distpath) \
748 $(call find-copy-subdir-files, *, fromdir, todir) $(wildcard foo.*)
749`,
750 expected: `load("//build/make/core:product_config.rbc", "rblf")
751
752def init(g, handle):
753 cfg = rblf.cfg(handle)
754 cfg["PRODUCT_COPY_FILES"] = (rblf.copy_if_exists("path:distpath") +
755 rblf.find_and_copy("*", "fromdir", "todir") +
756 rblf.expand_wildcard("foo.*"))
757`,
758 },
759 {
760 desc: "inferred type",
761 mkname: "product.mk",
762 in: `
763HIKEY_MODS := $(wildcard foo/*.ko)
764BOARD_VENDOR_KERNEL_MODULES += $(HIKEY_MODS)
765`,
766 expected: `load("//build/make/core:product_config.rbc", "rblf")
767
768def init(g, handle):
769 cfg = rblf.cfg(handle)
770 g["HIKEY_MODS"] = rblf.expand_wildcard("foo/*.ko")
771 g.setdefault("BOARD_VENDOR_KERNEL_MODULES", [])
772 g["BOARD_VENDOR_KERNEL_MODULES"] += g["HIKEY_MODS"]
773`,
774 },
775 {
776 desc: "list with vars",
777 mkname: "product.mk",
778 in: `
779PRODUCT_COPY_FILES += path1:$(TARGET_PRODUCT)/path1 $(PRODUCT_MODEL)/path2:$(TARGET_PRODUCT)/path2
780`,
781 expected: `load("//build/make/core:product_config.rbc", "rblf")
782
783def init(g, handle):
784 cfg = rblf.cfg(handle)
785 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
786 cfg["PRODUCT_COPY_FILES"] += (("path1:%s/path1" % g["TARGET_PRODUCT"]).split() +
787 ("%s/path2:%s/path2" % (cfg.get("PRODUCT_MODEL", ""), g["TARGET_PRODUCT"])).split())
788`,
789 },
790 {
791 desc: "misc calls",
792 mkname: "product.mk",
793 in: `
794$(call enforce-product-packages-exist,)
795$(call enforce-product-packages-exist, foo)
796$(call require-artifacts-in-path, foo, bar)
797$(call require-artifacts-in-path-relaxed, foo, bar)
Sasha Smundakd6797852021-11-15 13:01:53 -0800798$(call dist-for-goals, goal, from:to)
Cole Faust1cc08852022-02-28 11:12:08 -0800799$(call add-product-dex-preopt-module-config,MyModule,disable)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800800`,
801 expected: `load("//build/make/core:product_config.rbc", "rblf")
802
803def init(g, handle):
804 cfg = rblf.cfg(handle)
Cole Faust6c41b8a2022-04-13 13:53:48 -0700805 rblf.enforce_product_packages_exist(handle, "")
806 rblf.enforce_product_packages_exist(handle, "foo")
Cole Faustea9db582022-03-21 17:50:05 -0700807 rblf.require_artifacts_in_path(handle, "foo", "bar")
808 rblf.require_artifacts_in_path_relaxed(handle, "foo", "bar")
Sasha Smundakd6797852021-11-15 13:01:53 -0800809 rblf.mkdist_for_goals(g, "goal", "from:to")
Cole Faust1cc08852022-02-28 11:12:08 -0800810 rblf.add_product_dex_preopt_module_config(handle, "MyModule", "disable")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800811`,
812 },
813 {
814 desc: "list with functions",
815 mkname: "product.mk",
816 in: `
817PRODUCT_COPY_FILES := $(call find-copy-subdir-files,*.kl,from1,to1) \
818 $(call find-copy-subdir-files,*.kc,from2,to2) \
819 foo bar
820`,
821 expected: `load("//build/make/core:product_config.rbc", "rblf")
822
823def init(g, handle):
824 cfg = rblf.cfg(handle)
825 cfg["PRODUCT_COPY_FILES"] = (rblf.find_and_copy("*.kl", "from1", "to1") +
826 rblf.find_and_copy("*.kc", "from2", "to2") +
827 [
828 "foo",
829 "bar",
830 ])
831`,
832 },
833 {
834 desc: "Text functions",
835 mkname: "product.mk",
836 in: `
837PRODUCT_COPY_FILES := $(addprefix pfx-,a b c)
838PRODUCT_COPY_FILES := $(addsuffix .sff, a b c)
839PRODUCT_NAME := $(word 1, $(subst ., ,$(TARGET_BOARD_PLATFORM)))
Cole Faust94c4a9a2022-04-22 17:43:52 -0700840ifeq (1,$(words $(SOME_UNKNOWN_VARIABLE)))
841endif
842ifeq ($(words $(SOME_OTHER_VARIABLE)),$(SOME_INT_VARIABLE))
843endif
Sasha Smundak35434ed2021-11-05 16:29:56 -0700844$(info $(patsubst %.pub,$(PRODUCT_NAME)%,$(PRODUCT_ADB_KEYS)))
Cole Faust0e2b2562022-04-01 11:46:50 -0700845$(info $$(dir foo/bar): $(dir foo/bar))
Sasha Smundak16e07732021-07-23 11:38:23 -0700846$(info $(firstword $(PRODUCT_COPY_FILES)))
847$(info $(lastword $(PRODUCT_COPY_FILES)))
848$(info $(dir $(lastword $(MAKEFILE_LIST))))
849$(info $(dir $(lastword $(PRODUCT_COPY_FILES))))
850$(info $(dir $(lastword $(foobar))))
851$(info $(abspath foo/bar))
852$(info $(notdir foo/bar))
Sasha Smundak3deb9682021-07-26 18:42:25 -0700853$(call add_soong_config_namespace,snsconfig)
854$(call add_soong_config_var_value,snsconfig,imagetype,odm_image)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700855$(call soong_config_set, snsconfig, foo, foo_value)
856$(call soong_config_append, snsconfig, bar, bar_value)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700857PRODUCT_COPY_FILES := $(call copy-files,$(wildcard foo*.mk),etc)
Sasha Smundak04453082021-08-17 18:14:41 -0700858PRODUCT_COPY_FILES := $(call product-copy-files-by-pattern,from/%,to/%,a b c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800859`,
860 expected: `load("//build/make/core:product_config.rbc", "rblf")
861
862def init(g, handle):
863 cfg = rblf.cfg(handle)
864 cfg["PRODUCT_COPY_FILES"] = rblf.addprefix("pfx-", "a b c")
865 cfg["PRODUCT_COPY_FILES"] = rblf.addsuffix(".sff", "a b c")
Cole Faust94c4a9a2022-04-22 17:43:52 -0700866 cfg["PRODUCT_NAME"] = rblf.words((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " "))[0]
867 if len(rblf.words(g.get("SOME_UNKNOWN_VARIABLE", ""))) == 1:
868 pass
869 if ("%d" % (len(rblf.words(g.get("SOME_OTHER_VARIABLE", ""))))) == g.get("SOME_INT_VARIABLE", ""):
870 pass
Sasha Smundak35434ed2021-11-05 16:29:56 -0700871 rblf.mkinfo("product.mk", rblf.mkpatsubst("%.pub", "%s%%" % cfg["PRODUCT_NAME"], g.get("PRODUCT_ADB_KEYS", "")))
Cole Faust0e2b2562022-04-01 11:46:50 -0700872 rblf.mkinfo("product.mk", "$(dir foo/bar): %s" % rblf.dir("foo/bar"))
Cole Faust5a13aaf2022-04-27 17:49:35 -0700873 rblf.mkinfo("product.mk", rblf.first_word(cfg["PRODUCT_COPY_FILES"]))
874 rblf.mkinfo("product.mk", rblf.last_word(cfg["PRODUCT_COPY_FILES"]))
875 rblf.mkinfo("product.mk", rblf.dir(rblf.last_word("product.mk")))
876 rblf.mkinfo("product.mk", rblf.dir(rblf.last_word(cfg["PRODUCT_COPY_FILES"])))
877 rblf.mkinfo("product.mk", rblf.dir(rblf.last_word(_foobar)))
Sasha Smundak16e07732021-07-23 11:38:23 -0700878 rblf.mkinfo("product.mk", rblf.abspath("foo/bar"))
879 rblf.mkinfo("product.mk", rblf.notdir("foo/bar"))
Sasha Smundak65b547e2021-09-17 15:35:41 -0700880 rblf.soong_config_namespace(g, "snsconfig")
881 rblf.soong_config_set(g, "snsconfig", "imagetype", "odm_image")
882 rblf.soong_config_set(g, "snsconfig", "foo", "foo_value")
883 rblf.soong_config_append(g, "snsconfig", "bar", "bar_value")
Sasha Smundak3deb9682021-07-26 18:42:25 -0700884 cfg["PRODUCT_COPY_FILES"] = rblf.copy_files(rblf.expand_wildcard("foo*.mk"), "etc")
Sasha Smundak04453082021-08-17 18:14:41 -0700885 cfg["PRODUCT_COPY_FILES"] = rblf.product_copy_files_by_pattern("from/%", "to/%", "a b c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800886`,
887 },
888 {
Sasha Smundak9d011ab2021-07-09 16:00:57 -0700889 desc: "subst in list",
890 mkname: "product.mk",
891 in: `
892files = $(call find-copy-subdir-files,*,from,to)
893PRODUCT_COPY_FILES += $(subst foo,bar,$(files))
894`,
895 expected: `load("//build/make/core:product_config.rbc", "rblf")
896
897def init(g, handle):
898 cfg = rblf.cfg(handle)
899 _files = rblf.find_and_copy("*", "from", "to")
900 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
901 cfg["PRODUCT_COPY_FILES"] += rblf.mksubst("foo", "bar", _files)
902`,
903 },
904 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800905 desc: "assignment flavors",
906 mkname: "product.mk",
907 in: `
908PRODUCT_LIST1 := a
909PRODUCT_LIST2 += a
910PRODUCT_LIST1 += b
911PRODUCT_LIST2 += b
912PRODUCT_LIST3 ?= a
913PRODUCT_LIST1 = c
914PLATFORM_LIST += x
915PRODUCT_PACKAGES := $(PLATFORM_LIST)
916`,
917 expected: `load("//build/make/core:product_config.rbc", "rblf")
918
919def init(g, handle):
920 cfg = rblf.cfg(handle)
921 cfg["PRODUCT_LIST1"] = ["a"]
922 rblf.setdefault(handle, "PRODUCT_LIST2")
923 cfg["PRODUCT_LIST2"] += ["a"]
924 cfg["PRODUCT_LIST1"] += ["b"]
925 cfg["PRODUCT_LIST2"] += ["b"]
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800926 cfg["PRODUCT_LIST1"] = ["c"]
927 g.setdefault("PLATFORM_LIST", [])
928 g["PLATFORM_LIST"] += ["x"]
929 cfg["PRODUCT_PACKAGES"] = g["PLATFORM_LIST"][:]
930`,
931 },
932 {
933 desc: "assigment flavors2",
934 mkname: "product.mk",
935 in: `
936PRODUCT_LIST1 = a
937ifeq (0,1)
938 PRODUCT_LIST1 += b
939 PRODUCT_LIST2 += b
940endif
941PRODUCT_LIST1 += c
942PRODUCT_LIST2 += c
943`,
944 expected: `load("//build/make/core:product_config.rbc", "rblf")
945
946def init(g, handle):
947 cfg = rblf.cfg(handle)
948 cfg["PRODUCT_LIST1"] = ["a"]
949 if "0" == "1":
950 cfg["PRODUCT_LIST1"] += ["b"]
951 rblf.setdefault(handle, "PRODUCT_LIST2")
952 cfg["PRODUCT_LIST2"] += ["b"]
953 cfg["PRODUCT_LIST1"] += ["c"]
954 rblf.setdefault(handle, "PRODUCT_LIST2")
955 cfg["PRODUCT_LIST2"] += ["c"]
956`,
957 },
958 {
Cole Fauste2a37982022-03-09 16:00:17 -0800959 desc: "assigment setdefaults",
960 mkname: "product.mk",
961 in: `
962# All of these should have a setdefault because they're self-referential and not defined before
963PRODUCT_LIST1 = a $(PRODUCT_LIST1)
964PRODUCT_LIST2 ?= a $(PRODUCT_LIST2)
965PRODUCT_LIST3 += a
966
Cole Faust8e15f692023-10-09 12:26:21 -0700967# Now doing them again should not have a setdefault because they've already been set, except 2
968# which did not emit an assignment before
Cole Fauste2a37982022-03-09 16:00:17 -0800969PRODUCT_LIST1 = a $(PRODUCT_LIST1)
Cole Faust8e15f692023-10-09 12:26:21 -0700970PRODUCT_LIST2 = a $(PRODUCT_LIST2)
Cole Fauste2a37982022-03-09 16:00:17 -0800971PRODUCT_LIST3 += a
972`,
973 expected: `# All of these should have a setdefault because they're self-referential and not defined before
974load("//build/make/core:product_config.rbc", "rblf")
975
976def init(g, handle):
977 cfg = rblf.cfg(handle)
978 rblf.setdefault(handle, "PRODUCT_LIST1")
979 cfg["PRODUCT_LIST1"] = (["a"] +
980 cfg.get("PRODUCT_LIST1", []))
Cole Fauste2a37982022-03-09 16:00:17 -0800981 rblf.setdefault(handle, "PRODUCT_LIST3")
982 cfg["PRODUCT_LIST3"] += ["a"]
Cole Faust8e15f692023-10-09 12:26:21 -0700983 # Now doing them again should not have a setdefault because they've already been set, except 2
984 # which did not emit an assignment before
Cole Fauste2a37982022-03-09 16:00:17 -0800985 cfg["PRODUCT_LIST1"] = (["a"] +
986 cfg["PRODUCT_LIST1"])
Cole Faust8e15f692023-10-09 12:26:21 -0700987 rblf.setdefault(handle, "PRODUCT_LIST2")
988 cfg["PRODUCT_LIST2"] = (["a"] +
989 cfg.get("PRODUCT_LIST2", []))
Cole Fauste2a37982022-03-09 16:00:17 -0800990 cfg["PRODUCT_LIST3"] += ["a"]
991`,
992 },
993 {
Sasha Smundak3deb9682021-07-26 18:42:25 -0700994 desc: "soong namespace assignments",
995 mkname: "product.mk",
996 in: `
997SOONG_CONFIG_NAMESPACES += cvd
998SOONG_CONFIG_cvd += launch_configs
Sasha Smundak65b547e2021-09-17 15:35:41 -0700999SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
Sasha Smundak3deb9682021-07-26 18:42:25 -07001000SOONG_CONFIG_cvd += grub_config
1001SOONG_CONFIG_cvd_grub_config += grub.cfg
Sasha Smundak65b547e2021-09-17 15:35:41 -07001002x := $(SOONG_CONFIG_cvd_grub_config)
Sasha Smundak3deb9682021-07-26 18:42:25 -07001003`,
1004 expected: `load("//build/make/core:product_config.rbc", "rblf")
1005
1006def init(g, handle):
1007 cfg = rblf.cfg(handle)
Sasha Smundak65b547e2021-09-17 15:35:41 -07001008 rblf.soong_config_namespace(g, "cvd")
1009 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
1010 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
Cole Faust1e275862022-04-26 14:28:04 -07001011 _x = rblf.mk2rbc_error("product.mk:7", "SOONG_CONFIG_ variables cannot be referenced, use soong_config_get instead: SOONG_CONFIG_cvd_grub_config")
Sasha Smundak3deb9682021-07-26 18:42:25 -07001012`,
Cole Faustc00184e2021-11-08 12:08:57 -08001013 }, {
1014 desc: "soong namespace accesses",
1015 mkname: "product.mk",
1016 in: `
1017SOONG_CONFIG_NAMESPACES += cvd
1018SOONG_CONFIG_cvd += launch_configs
1019SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
1020SOONG_CONFIG_cvd += grub_config
1021SOONG_CONFIG_cvd_grub_config += grub.cfg
1022x := $(call soong_config_get,cvd,grub_config)
1023`,
1024 expected: `load("//build/make/core:product_config.rbc", "rblf")
1025
1026def init(g, handle):
1027 cfg = rblf.cfg(handle)
1028 rblf.soong_config_namespace(g, "cvd")
1029 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
1030 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
1031 _x = rblf.soong_config_get(g, "cvd", "grub_config")
1032`,
Sasha Smundak3deb9682021-07-26 18:42:25 -07001033 },
1034 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001035 desc: "string split",
1036 mkname: "product.mk",
1037 in: `
1038PRODUCT_LIST1 = a
1039local = b
1040local += c
1041FOO = d
1042FOO += e
1043PRODUCT_LIST1 += $(local)
1044PRODUCT_LIST1 += $(FOO)
1045`,
1046 expected: `load("//build/make/core:product_config.rbc", "rblf")
1047
1048def init(g, handle):
1049 cfg = rblf.cfg(handle)
1050 cfg["PRODUCT_LIST1"] = ["a"]
1051 _local = "b"
1052 _local += " " + "c"
1053 g["FOO"] = "d"
1054 g["FOO"] += " " + "e"
1055 cfg["PRODUCT_LIST1"] += (_local).split()
1056 cfg["PRODUCT_LIST1"] += (g["FOO"]).split()
1057`,
1058 },
1059 {
1060 desc: "apex_jars",
1061 mkname: "product.mk",
1062 in: `
1063PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex
1064`,
1065 expected: `load("//build/make/core:product_config.rbc", "rblf")
1066
1067def init(g, handle):
1068 cfg = rblf.cfg(handle)
1069 cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) +
1070 ["framework-minus-apex"])
1071`,
1072 },
1073 {
Cole Faust95b95cb2022-04-05 16:37:39 -07001074 desc: "strip/sort functions",
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001075 mkname: "product.mk",
1076 in: `
1077ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),)
1078 PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress)
1079endif
Cole Faust95b95cb2022-04-05 16:37:39 -07001080MY_VAR := $(sort b a c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001081`,
1082 expected: `load("//build/make/core:product_config.rbc", "rblf")
1083
1084def init(g, handle):
1085 cfg = rblf.cfg(handle)
1086 if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []):
Cole Faust816e0802022-03-04 12:04:31 -08001087 rblf.setdefault(handle, "PRODUCT_PACKAGES")
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001088 cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split()
Cole Faust95b95cb2022-04-05 16:37:39 -07001089 g["MY_VAR"] = rblf.mksort("b a c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001090`,
1091 },
1092 {
1093 desc: "strip func in condition",
1094 mkname: "product.mk",
1095 in: `
1096ifneq ($(strip $(TARGET_VENDOR)),)
1097endif
1098`,
1099 expected: `load("//build/make/core:product_config.rbc", "rblf")
1100
1101def init(g, handle):
1102 cfg = rblf.cfg(handle)
Sasha Smundak0554d762021-07-08 18:26:12 -07001103 if rblf.mkstrip(g.get("TARGET_VENDOR", "")):
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001104 pass
1105`,
1106 },
1107 {
1108 desc: "ref after set",
1109 mkname: "product.mk",
1110 in: `
1111PRODUCT_ADB_KEYS:=value
1112FOO := $(PRODUCT_ADB_KEYS)
1113ifneq (,$(PRODUCT_ADB_KEYS))
1114endif
1115`,
1116 expected: `load("//build/make/core:product_config.rbc", "rblf")
1117
1118def init(g, handle):
1119 cfg = rblf.cfg(handle)
1120 g["PRODUCT_ADB_KEYS"] = "value"
1121 g["FOO"] = g["PRODUCT_ADB_KEYS"]
1122 if g["PRODUCT_ADB_KEYS"]:
1123 pass
1124`,
1125 },
1126 {
1127 desc: "ref before set",
1128 mkname: "product.mk",
1129 in: `
1130V1 := $(PRODUCT_ADB_KEYS)
1131ifeq (,$(PRODUCT_ADB_KEYS))
1132 V2 := $(PRODUCT_ADB_KEYS)
1133 PRODUCT_ADB_KEYS:=foo
1134 V3 := $(PRODUCT_ADB_KEYS)
1135endif`,
1136 expected: `load("//build/make/core:product_config.rbc", "rblf")
1137
1138def init(g, handle):
1139 cfg = rblf.cfg(handle)
1140 g["V1"] = g.get("PRODUCT_ADB_KEYS", "")
1141 if not g.get("PRODUCT_ADB_KEYS", ""):
1142 g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
1143 g["PRODUCT_ADB_KEYS"] = "foo"
1144 g["V3"] = g["PRODUCT_ADB_KEYS"]
1145`,
1146 },
Sasha Smundak6609ba72021-07-22 18:32:56 -07001147 {
1148 desc: "Dynamic inherit path",
1149 mkname: "product.mk",
1150 in: `
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001151MY_PATH:=foo
Sasha Smundak6609ba72021-07-22 18:32:56 -07001152$(call inherit-product,vendor/$(MY_PATH)/cfg.mk)
1153`,
1154 expected: `load("//build/make/core:product_config.rbc", "rblf")
1155load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1156load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init")
1157
1158def init(g, handle):
1159 cfg = rblf.cfg(handle)
1160 g["MY_PATH"] = "foo"
1161 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001162 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1163 "vendor/bar/baz/cfg.mk": ("vendor/bar/baz/cfg", _cfg1_init),
Sasha Smundak6609ba72021-07-22 18:32:56 -07001164 }.get("vendor/%s/cfg.mk" % g["MY_PATH"])
1165 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1166 if not _varmod_init:
Cole Faust7321b092021-12-21 16:11:16 -08001167 rblf.mkerror("product.mk", "Cannot find %s" % ("vendor/%s/cfg.mk" % g["MY_PATH"]))
Sasha Smundak6609ba72021-07-22 18:32:56 -07001168 rblf.inherit(handle, _varmod, _varmod_init)
1169`,
1170 },
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001171 {
1172 desc: "Dynamic inherit with hint",
1173 mkname: "product.mk",
1174 in: `
1175MY_PATH:=foo
1176#RBC# include_top vendor/foo1
1177$(call inherit-product,$(MY_PATH)/cfg.mk)
Cole Faust9df1d732022-04-26 16:27:22 -07001178#RBC# include_top vendor/foo1
1179$(call inherit-product,$(MY_OTHER_PATH))
1180#RBC# include_top vendor/foo1
Cole Faust74ac0272022-06-14 12:45:26 -07001181$(call inherit-product,vendor/$(MY_OTHER_PATH))
1182#RBC# include_top vendor/foo1
Cole Faust9df1d732022-04-26 16:27:22 -07001183$(foreach f,$(MY_MAKEFILES), \
1184 $(call inherit-product,$(f)))
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001185`,
1186 expected: `load("//build/make/core:product_config.rbc", "rblf")
1187load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1188
1189def init(g, handle):
1190 cfg = rblf.cfg(handle)
1191 g["MY_PATH"] = "foo"
Cole Faust93f8d392022-03-02 13:31:30 -08001192 _entry = {
1193 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1194 }.get("%s/cfg.mk" % g["MY_PATH"])
1195 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1196 if not _varmod_init:
1197 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1198 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust9df1d732022-04-26 16:27:22 -07001199 _entry = {
1200 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1201 }.get(g.get("MY_OTHER_PATH", ""))
1202 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1203 if not _varmod_init:
1204 rblf.mkerror("product.mk", "Cannot find %s" % (g.get("MY_OTHER_PATH", "")))
1205 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust74ac0272022-06-14 12:45:26 -07001206 _entry = {
1207 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1208 }.get("vendor/%s" % g.get("MY_OTHER_PATH", ""))
1209 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1210 if not _varmod_init:
1211 rblf.mkerror("product.mk", "Cannot find %s" % ("vendor/%s" % g.get("MY_OTHER_PATH", "")))
1212 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust9df1d732022-04-26 16:27:22 -07001213 for f in rblf.words(g.get("MY_MAKEFILES", "")):
1214 _entry = {
1215 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1216 }.get(f)
1217 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1218 if not _varmod_init:
1219 rblf.mkerror("product.mk", "Cannot find %s" % (f))
1220 rblf.inherit(handle, _varmod, _varmod_init)
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001221`,
1222 },
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001223 {
Cole Faustf7ed5342021-12-21 14:15:12 -08001224 desc: "Dynamic inherit with duplicated hint",
1225 mkname: "product.mk",
1226 in: `
1227MY_PATH:=foo
1228#RBC# include_top vendor/foo1
1229$(call inherit-product,$(MY_PATH)/cfg.mk)
1230#RBC# include_top vendor/foo1
Cole Faust7940c6a2022-01-31 15:54:05 -08001231#RBC# include_top vendor/foo1
Cole Faustf7ed5342021-12-21 14:15:12 -08001232$(call inherit-product,$(MY_PATH)/cfg.mk)
1233`,
1234 expected: `load("//build/make/core:product_config.rbc", "rblf")
1235load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1236
1237def init(g, handle):
1238 cfg = rblf.cfg(handle)
1239 g["MY_PATH"] = "foo"
Cole Faust93f8d392022-03-02 13:31:30 -08001240 _entry = {
1241 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1242 }.get("%s/cfg.mk" % g["MY_PATH"])
1243 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1244 if not _varmod_init:
1245 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1246 rblf.inherit(handle, _varmod, _varmod_init)
1247 _entry = {
1248 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1249 }.get("%s/cfg.mk" % g["MY_PATH"])
1250 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1251 if not _varmod_init:
1252 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1253 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faustf7ed5342021-12-21 14:15:12 -08001254`,
1255 },
1256 {
Cole Faust069aba62022-01-26 17:47:33 -08001257 desc: "Dynamic inherit path that lacks hint",
Cole Faust6c934f62022-01-06 15:51:12 -08001258 mkname: "product.mk",
1259 in: `
1260#RBC# include_top foo
1261$(call inherit-product,$(MY_VAR)/font.mk)
1262
1263#RBC# include_top foo
1264
1265# There's some space and even this comment between the include_top and the inherit-product
1266
1267$(call inherit-product,$(MY_VAR)/font.mk)
1268
1269$(call inherit-product,$(MY_VAR)/font.mk)
1270`,
Cole Faust7940c6a2022-01-31 15:54:05 -08001271 expected: `load("//build/make/core:product_config.rbc", "rblf")
Cole Faust6c934f62022-01-06 15:51:12 -08001272load("//foo:font.star|init", _font_init = "init")
Cole Faust069aba62022-01-26 17:47:33 -08001273load("//bar:font.star|init", _font1_init = "init")
Cole Faust6c934f62022-01-06 15:51:12 -08001274
1275def init(g, handle):
1276 cfg = rblf.cfg(handle)
Cole Faust93f8d392022-03-02 13:31:30 -08001277 _entry = {
1278 "foo/font.mk": ("foo/font", _font_init),
1279 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1280 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1281 if not _varmod_init:
1282 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1283 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001284 # There's some space and even this comment between the include_top and the inherit-product
Cole Faust93f8d392022-03-02 13:31:30 -08001285 _entry = {
1286 "foo/font.mk": ("foo/font", _font_init),
1287 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1288 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1289 if not _varmod_init:
1290 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1291 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faustf4e72cf2022-02-08 12:49:37 -08001292 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.")
Cole Faust6c934f62022-01-06 15:51:12 -08001293 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001294 "foo/font.mk": ("foo/font", _font_init),
Cole Faust069aba62022-01-26 17:47:33 -08001295 "bar/font.mk": ("bar/font", _font1_init),
Cole Faust6c934f62022-01-06 15:51:12 -08001296 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1297 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1298 if not _varmod_init:
1299 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1300 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001301`,
1302 },
1303 {
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001304 desc: "Ignore make rules",
1305 mkname: "product.mk",
1306 in: `
Cole Faust00afd4f2022-04-26 14:01:56 -07001307foo: PRIVATE_VARIABLE = some_tool $< $@
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001308foo: foo.c
1309 gcc -o $@ $*`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001310 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001311
1312def init(g, handle):
1313 cfg = rblf.cfg(handle)
Cole Faust00afd4f2022-04-26 14:01:56 -07001314 rblf.mk2rbc_error("product.mk:2", "Only simple variables are handled")
1315 rblf.mk2rbc_error("product.mk:3", "unsupported line rule: foo: foo.c\n#gcc -o $@ $*")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001316`,
1317 },
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001318 {
1319 desc: "Flag override",
1320 mkname: "product.mk",
1321 in: `
1322override FOO:=`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001323 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001324
1325def init(g, handle):
1326 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001327 rblf.mk2rbc_error("product.mk:2", "cannot handle override directive")
Sasha Smundak422b6142021-11-11 18:31:59 -08001328`,
1329 },
1330 {
1331 desc: "Bad expression",
1332 mkname: "build/product.mk",
1333 in: `
1334ifeq (,$(call foobar))
1335endif
Cole Faust1e275862022-04-26 14:28:04 -07001336my_sources := $(local-generated-sources-dir)
Sasha Smundak422b6142021-11-11 18:31:59 -08001337`,
1338 expected: `load("//build/make/core:product_config.rbc", "rblf")
1339
1340def init(g, handle):
1341 cfg = rblf.cfg(handle)
1342 if rblf.mk2rbc_error("build/product.mk:2", "cannot handle invoking foobar"):
1343 pass
Cole Faust1e275862022-04-26 14:28:04 -07001344 _my_sources = rblf.mk2rbc_error("build/product.mk:4", "local-generated-sources-dir is not supported")
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001345`,
1346 },
Cole Faust4eadba72021-12-07 11:54:52 -08001347 {
1348 desc: "if expression",
1349 mkname: "product.mk",
1350 in: `
1351TEST_VAR := foo
1352TEST_VAR_LIST := foo
1353TEST_VAR_LIST += bar
1354TEST_VAR_2 := $(if $(TEST_VAR),bar)
1355TEST_VAR_3 := $(if $(TEST_VAR),bar,baz)
Cole Faust421a1922022-03-16 14:35:45 -07001356TEST_VAR_4 := $(if $(TEST_VAR),$(TEST_VAR_LIST))
Cole Faust4eadba72021-12-07 11:54:52 -08001357`,
1358 expected: `load("//build/make/core:product_config.rbc", "rblf")
1359
1360def init(g, handle):
1361 cfg = rblf.cfg(handle)
1362 g["TEST_VAR"] = "foo"
1363 g["TEST_VAR_LIST"] = ["foo"]
1364 g["TEST_VAR_LIST"] += ["bar"]
1365 g["TEST_VAR_2"] = ("bar" if g["TEST_VAR"] else "")
1366 g["TEST_VAR_3"] = ("bar" if g["TEST_VAR"] else "baz")
Cole Faust421a1922022-03-16 14:35:45 -07001367 g["TEST_VAR_4"] = (g["TEST_VAR_LIST"] if g["TEST_VAR"] else [])
Cole Faust4eadba72021-12-07 11:54:52 -08001368`,
1369 },
Cole Faustc36c9622021-12-07 15:20:45 -08001370 {
1371 desc: "substitution references",
1372 mkname: "product.mk",
1373 in: `
1374SOURCES := foo.c bar.c
1375OBJECTS := $(SOURCES:.c=.o)
1376OBJECTS2 := $(SOURCES:%.c=%.o)
1377`,
1378 expected: `load("//build/make/core:product_config.rbc", "rblf")
1379
1380def init(g, handle):
1381 cfg = rblf.cfg(handle)
1382 g["SOURCES"] = "foo.c bar.c"
1383 g["OBJECTS"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1384 g["OBJECTS2"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1385`,
1386 },
Cole Faustb0d32ab2021-12-09 14:00:59 -08001387 {
1388 desc: "foreach expressions",
1389 mkname: "product.mk",
1390 in: `
1391BOOT_KERNEL_MODULES := foo.ko bar.ko
1392BOOT_KERNEL_MODULES_FILTER := $(foreach m,$(BOOT_KERNEL_MODULES),%/$(m))
1393BOOT_KERNEL_MODULES_LIST := foo.ko
1394BOOT_KERNEL_MODULES_LIST += bar.ko
1395BOOT_KERNEL_MODULES_FILTER_2 := $(foreach m,$(BOOT_KERNEL_MODULES_LIST),%/$(m))
Cole Faust72374fc2022-05-05 11:45:04 -07001396NESTED_LISTS := $(foreach m,$(SOME_VAR),$(BOOT_KERNEL_MODULES_LIST))
1397NESTED_LISTS_2 := $(foreach x,$(SOME_VAR),$(foreach y,$(x),prefix$(y)))
Cole Faustb0d32ab2021-12-09 14:00:59 -08001398
Cole Faustb67aa082022-02-28 16:39:59 -08001399FOREACH_WITH_IF := $(foreach module,\
1400 $(BOOT_KERNEL_MODULES_LIST),\
1401 $(if $(filter $(module),foo.ko),,$(error module "$(module)" has an error!)))
Cole Faustf035d402022-03-28 14:02:50 -07001402
1403# Same as above, but not assigning it to a variable allows it to be converted to statements
1404$(foreach module,\
1405 $(BOOT_KERNEL_MODULES_LIST),\
1406 $(if $(filter $(module),foo.ko),,$(error module "$(module)" has an error!)))
Cole Faustb0d32ab2021-12-09 14:00:59 -08001407`,
1408 expected: `load("//build/make/core:product_config.rbc", "rblf")
1409
1410def init(g, handle):
1411 cfg = rblf.cfg(handle)
1412 g["BOOT_KERNEL_MODULES"] = "foo.ko bar.ko"
1413 g["BOOT_KERNEL_MODULES_FILTER"] = ["%%/%s" % m for m in rblf.words(g["BOOT_KERNEL_MODULES"])]
1414 g["BOOT_KERNEL_MODULES_LIST"] = ["foo.ko"]
1415 g["BOOT_KERNEL_MODULES_LIST"] += ["bar.ko"]
1416 g["BOOT_KERNEL_MODULES_FILTER_2"] = ["%%/%s" % m for m in g["BOOT_KERNEL_MODULES_LIST"]]
Cole Faust72374fc2022-05-05 11:45:04 -07001417 g["NESTED_LISTS"] = rblf.flatten_2d_list([g["BOOT_KERNEL_MODULES_LIST"] for m in rblf.words(g.get("SOME_VAR", ""))])
1418 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", ""))])
Cole Faustb67aa082022-02-28 16:39:59 -08001419 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"]]
Cole Faustf035d402022-03-28 14:02:50 -07001420 # Same as above, but not assigning it to a variable allows it to be converted to statements
1421 for module in g["BOOT_KERNEL_MODULES_LIST"]:
1422 if not rblf.filter(module, "foo.ko"):
1423 rblf.mkerror("product.mk", "module \"%s\" has an error!" % module)
Cole Faustb0d32ab2021-12-09 14:00:59 -08001424`,
1425 },
Cole Faust0484c232021-12-22 14:08:08 -08001426 {
1427 desc: "List appended to string",
1428 mkname: "product.mk",
1429 in: `
1430NATIVE_BRIDGE_PRODUCT_PACKAGES := \
1431 libnative_bridge_vdso.native_bridge \
1432 native_bridge_guest_app_process.native_bridge \
1433 native_bridge_guest_linker.native_bridge
1434
1435NATIVE_BRIDGE_MODIFIED_GUEST_LIBS := \
1436 libaaudio \
1437 libamidi \
1438 libandroid \
1439 libandroid_runtime
1440
1441NATIVE_BRIDGE_PRODUCT_PACKAGES += \
1442 $(addsuffix .native_bridge,$(NATIVE_BRIDGE_ORIG_GUEST_LIBS))
1443`,
1444 expected: `load("//build/make/core:product_config.rbc", "rblf")
1445
1446def init(g, handle):
1447 cfg = rblf.cfg(handle)
1448 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] = "libnative_bridge_vdso.native_bridge native_bridge_guest_app_process.native_bridge native_bridge_guest_linker.native_bridge"
1449 g["NATIVE_BRIDGE_MODIFIED_GUEST_LIBS"] = "libaaudio libamidi libandroid libandroid_runtime"
1450 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] += " " + " ".join(rblf.addsuffix(".native_bridge", g.get("NATIVE_BRIDGE_ORIG_GUEST_LIBS", "")))
1451`,
1452 },
Cole Faustb1103e22022-01-06 15:22:05 -08001453 {
1454 desc: "Math functions",
1455 mkname: "product.mk",
1456 in: `
1457# Test the math functions defined in build/make/common/math.mk
1458ifeq ($(call math_max,2,5),5)
1459endif
1460ifeq ($(call math_min,2,5),2)
1461endif
1462ifeq ($(call math_gt_or_eq,2,5),true)
1463endif
1464ifeq ($(call math_gt,2,5),true)
1465endif
1466ifeq ($(call math_lt,2,5),true)
1467endif
1468ifeq ($(call math_gt_or_eq,2,5),)
1469endif
1470ifeq ($(call math_gt,2,5),)
1471endif
1472ifeq ($(call math_lt,2,5),)
1473endif
1474ifeq ($(call math_gt_or_eq,$(MY_VAR), 5),true)
1475endif
1476ifeq ($(call math_gt_or_eq,$(MY_VAR),$(MY_OTHER_VAR)),true)
1477endif
1478ifeq ($(call math_gt_or_eq,100$(MY_VAR),10),true)
1479endif
1480`,
1481 expected: `# Test the math functions defined in build/make/common/math.mk
1482load("//build/make/core:product_config.rbc", "rblf")
1483
1484def init(g, handle):
1485 cfg = rblf.cfg(handle)
1486 if max(2, 5) == 5:
1487 pass
1488 if min(2, 5) == 2:
1489 pass
1490 if 2 >= 5:
1491 pass
1492 if 2 > 5:
1493 pass
1494 if 2 < 5:
1495 pass
1496 if 2 < 5:
1497 pass
1498 if 2 <= 5:
1499 pass
1500 if 2 >= 5:
1501 pass
1502 if int(g.get("MY_VAR", "")) >= 5:
1503 pass
1504 if int(g.get("MY_VAR", "")) >= int(g.get("MY_OTHER_VAR", "")):
1505 pass
1506 if int("100%s" % g.get("MY_VAR", "")) >= 10:
1507 pass
1508`,
1509 },
Cole Faustf92c9f22022-03-14 14:35:50 -07001510 {
1511 desc: "Type hints",
1512 mkname: "product.mk",
1513 in: `
1514# Test type hints
1515#RBC# type_hint list MY_VAR MY_VAR_2
1516# Unsupported type
1517#RBC# type_hint bool MY_VAR_3
1518# Invalid syntax
1519#RBC# type_hint list
1520# Duplicated variable
1521#RBC# type_hint list MY_VAR_2
1522#RBC# type_hint list my-local-var-with-dashes
Cole Faust421a1922022-03-16 14:35:45 -07001523#RBC# type_hint string MY_STRING_VAR
Cole Faustf92c9f22022-03-14 14:35:50 -07001524
1525MY_VAR := foo
1526MY_VAR_UNHINTED := foo
1527
1528# Vars set after other statements still get the hint
1529MY_VAR_2 := foo
1530
1531# You can't specify a type hint after the first statement
1532#RBC# type_hint list MY_VAR_4
1533MY_VAR_4 := foo
1534
1535my-local-var-with-dashes := foo
Cole Faust421a1922022-03-16 14:35:45 -07001536
1537MY_STRING_VAR := $(wildcard foo/bar.mk)
Cole Faustf92c9f22022-03-14 14:35:50 -07001538`,
1539 expected: `# Test type hints
1540# Unsupported type
1541load("//build/make/core:product_config.rbc", "rblf")
1542
1543def init(g, handle):
1544 cfg = rblf.cfg(handle)
1545 rblf.mk2rbc_error("product.mk:5", "Invalid type_hint annotation. Only list/string types are accepted, found bool")
1546 # Invalid syntax
1547 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")
1548 # Duplicated variable
1549 rblf.mk2rbc_error("product.mk:9", "Duplicate type hint for variable MY_VAR_2")
1550 g["MY_VAR"] = ["foo"]
1551 g["MY_VAR_UNHINTED"] = "foo"
1552 # Vars set after other statements still get the hint
1553 g["MY_VAR_2"] = ["foo"]
1554 # You can't specify a type hint after the first statement
Cole Faust421a1922022-03-16 14:35:45 -07001555 rblf.mk2rbc_error("product.mk:20", "type_hint annotations must come before the first Makefile statement")
Cole Faustf92c9f22022-03-14 14:35:50 -07001556 g["MY_VAR_4"] = "foo"
1557 _my_local_var_with_dashes = ["foo"]
Cole Faust421a1922022-03-16 14:35:45 -07001558 g["MY_STRING_VAR"] = " ".join(rblf.expand_wildcard("foo/bar.mk"))
Cole Faustf92c9f22022-03-14 14:35:50 -07001559`,
1560 },
Cole Faustf5adedc2022-03-18 14:05:06 -07001561 {
1562 desc: "Set LOCAL_PATH to my-dir",
1563 mkname: "product.mk",
1564 in: `
1565LOCAL_PATH := $(call my-dir)
1566`,
1567 expected: `load("//build/make/core:product_config.rbc", "rblf")
1568
1569def init(g, handle):
1570 cfg = rblf.cfg(handle)
1571
1572`,
1573 },
Cole Faustf035d402022-03-28 14:02:50 -07001574 {
1575 desc: "Evals",
1576 mkname: "product.mk",
1577 in: `
1578$(eval)
1579$(eval MY_VAR := foo)
1580$(eval # This is a test of eval functions)
1581$(eval $(TOO_COMPLICATED) := bar)
Cole Faust20052982022-04-22 14:43:55 -07001582$(eval include foo/font.mk)
1583$(eval $(call inherit-product,vendor/foo1/cfg.mk))
1584
Cole Faustf035d402022-03-28 14:02:50 -07001585$(foreach x,$(MY_LIST_VAR), \
1586 $(eval PRODUCT_COPY_FILES += foo/bar/$(x):$(TARGET_COPY_OUT_VENDOR)/etc/$(x)) \
Cole Faust20052982022-04-22 14:43:55 -07001587 $(if $(MY_OTHER_VAR),$(eval PRODUCT_COPY_FILES += $(MY_OTHER_VAR):foo/bar/$(x))))
Cole Faustf035d402022-03-28 14:02:50 -07001588
Cole Faust20052982022-04-22 14:43:55 -07001589$(foreach x,$(MY_LIST_VAR), \
1590 $(eval include foo/$(x).mk))
Cole Faust73660422023-01-05 11:07:47 -08001591
1592# Check that we get as least close to correct line numbers for errors on statements inside evals
1593$(eval $(call inherit-product,$(A_VAR)))
Cole Faustf035d402022-03-28 14:02:50 -07001594`,
1595 expected: `load("//build/make/core:product_config.rbc", "rblf")
Cole Faust20052982022-04-22 14:43:55 -07001596load("//foo:font.star", _font_init = "init")
1597load("//vendor/foo1:cfg.star", _cfg_init = "init")
Cole Faustf035d402022-03-28 14:02:50 -07001598
1599def init(g, handle):
1600 cfg = rblf.cfg(handle)
1601 g["MY_VAR"] = "foo"
1602 # This is a test of eval functions
Cole Faust20052982022-04-22 14:43:55 -07001603 rblf.mk2rbc_error("product.mk:5", "Eval expression too complex; only assignments, comments, includes, and inherit-products are supported")
1604 _font_init(g, handle)
1605 rblf.inherit(handle, "vendor/foo1/cfg", _cfg_init)
Cole Faustf035d402022-03-28 14:02:50 -07001606 for x in rblf.words(g.get("MY_LIST_VAR", "")):
1607 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
1608 cfg["PRODUCT_COPY_FILES"] += ("foo/bar/%s:%s/etc/%s" % (x, g.get("TARGET_COPY_OUT_VENDOR", ""), x)).split()
1609 if g.get("MY_OTHER_VAR", ""):
1610 cfg["PRODUCT_COPY_FILES"] += ("%s:foo/bar/%s" % (g.get("MY_OTHER_VAR", ""), x)).split()
Cole Faust20052982022-04-22 14:43:55 -07001611 for x in rblf.words(g.get("MY_LIST_VAR", "")):
1612 _entry = {
1613 "foo/font.mk": ("foo/font", _font_init),
Cole Faust72374fc2022-05-05 11:45:04 -07001614 }.get("foo/%s.mk" % x)
Cole Faust20052982022-04-22 14:43:55 -07001615 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1616 if not _varmod_init:
Cole Faust72374fc2022-05-05 11:45:04 -07001617 rblf.mkerror("product.mk", "Cannot find %s" % ("foo/%s.mk" % x))
Cole Faust20052982022-04-22 14:43:55 -07001618 _varmod_init(g, handle)
Cole Faust73660422023-01-05 11:07:47 -08001619 # Check that we get as least close to correct line numbers for errors on statements inside evals
1620 rblf.mk2rbc_error("product.mk:17", "inherit-product/include argument is too complex")
Cole Faustf035d402022-03-28 14:02:50 -07001621`,
1622 },
Cole Faust5d5fcc32022-04-26 18:02:05 -07001623 {
1624 desc: ".KATI_READONLY",
1625 mkname: "product.mk",
1626 in: `
1627MY_VAR := foo
1628.KATI_READONLY := MY_VAR
1629`,
1630 expected: `load("//build/make/core:product_config.rbc", "rblf")
1631
1632def init(g, handle):
1633 cfg = rblf.cfg(handle)
1634 g["MY_VAR"] = "foo"
1635`,
1636 },
Cole Faust13238772022-04-28 14:29:57 -07001637 {
1638 desc: "Complicated variable references",
1639 mkname: "product.mk",
1640 in: `
1641MY_VAR := foo
1642MY_VAR_2 := MY_VAR
1643MY_VAR_3 := $($(MY_VAR_2))
1644MY_VAR_4 := $(foo bar)
1645MY_VAR_5 := $($(MY_VAR_2) bar)
1646`,
1647 expected: `load("//build/make/core:product_config.rbc", "rblf")
1648
1649def init(g, handle):
1650 cfg = rblf.cfg(handle)
1651 g["MY_VAR"] = "foo"
1652 g["MY_VAR_2"] = "MY_VAR"
1653 g["MY_VAR_3"] = (cfg).get(g["MY_VAR_2"], (g).get(g["MY_VAR_2"], ""))
1654 g["MY_VAR_4"] = rblf.mk2rbc_error("product.mk:5", "cannot handle invoking foo")
1655 g["MY_VAR_5"] = rblf.mk2rbc_error("product.mk:6", "reference is too complex: $(MY_VAR_2) bar")
1656`,
1657 },
Cole Faustd2daabf2022-12-12 17:38:01 -08001658 {
1659 desc: "Conditional functions",
1660 mkname: "product.mk",
1661 in: `
1662B := foo
1663X := $(or $(A))
1664X := $(or $(A),$(B))
1665X := $(or $(A),$(B),$(C))
1666X := $(and $(A))
1667X := $(and $(A),$(B))
1668X := $(and $(A),$(B),$(C))
1669X := $(or $(A),$(B)) Y
1670
1671D := $(wildcard *.mk)
1672X := $(or $(B),$(D))
1673`,
1674 expected: `load("//build/make/core:product_config.rbc", "rblf")
1675
1676def init(g, handle):
1677 cfg = rblf.cfg(handle)
1678 g["B"] = "foo"
1679 g["X"] = g.get("A", "")
1680 g["X"] = g.get("A", "") or g["B"]
1681 g["X"] = g.get("A", "") or g["B"] or g.get("C", "")
1682 g["X"] = g.get("A", "")
1683 g["X"] = g.get("A", "") and g["B"]
1684 g["X"] = g.get("A", "") and g["B"] and g.get("C", "")
1685 g["X"] = "%s Y" % g.get("A", "") or g["B"]
1686 g["D"] = rblf.expand_wildcard("*.mk")
1687 g["X"] = rblf.mk2rbc_error("product.mk:12", "Expected all arguments to $(or) or $(and) to have the same type, found \"string\" and \"list\"")
1688`,
1689 },
Cole Faust2dee63d2022-12-12 18:11:00 -08001690 {
1691
1692 desc: "is-lower/is-upper",
1693 mkname: "product.mk",
1694 in: `
1695X := $(call to-lower,aBc)
1696X := $(call to-upper,aBc)
1697X := $(call to-lower,$(VAR))
1698X := $(call to-upper,$(VAR))
1699`,
1700 expected: `load("//build/make/core:product_config.rbc", "rblf")
1701
1702def init(g, handle):
1703 cfg = rblf.cfg(handle)
1704 g["X"] = ("aBc").lower()
1705 g["X"] = ("aBc").upper()
1706 g["X"] = (g.get("VAR", "")).lower()
1707 g["X"] = (g.get("VAR", "")).upper()
1708`,
1709 },
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001710}
1711
1712var known_variables = []struct {
1713 name string
1714 class varClass
1715 starlarkType
1716}{
Cole Faustf1f44d32021-11-16 14:52:12 -08001717 {"NATIVE_COVERAGE", VarClassSoong, starlarkTypeBool},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001718 {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
1719 {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
1720 {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
1721 {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
1722 {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
1723 {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
1724 {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
1725 {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
1726 {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
1727 {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
1728 {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
1729 {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
1730 {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
1731 {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
1732}
1733
Sasha Smundak6609ba72021-07-22 18:32:56 -07001734type testMakefileFinder struct {
1735 fs fs.FS
1736 root string
1737 files []string
1738}
1739
1740func (t *testMakefileFinder) Find(root string) []string {
1741 if t.files != nil || root == t.root {
1742 return t.files
1743 }
1744 t.files = make([]string, 0)
1745 fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error {
1746 if err != nil {
1747 return err
1748 }
1749 if d.IsDir() {
1750 base := filepath.Base(path)
1751 if base[0] == '.' && len(base) > 1 {
1752 return fs.SkipDir
1753 }
1754 return nil
1755 }
1756 if strings.HasSuffix(path, ".mk") {
1757 t.files = append(t.files, path)
1758 }
1759 return nil
1760 })
1761 return t.files
1762}
1763
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001764func TestGood(t *testing.T) {
1765 for _, v := range known_variables {
1766 KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
1767 }
Sasha Smundak6609ba72021-07-22 18:32:56 -07001768 fs := NewFindMockFS([]string{
1769 "vendor/foo1/cfg.mk",
1770 "vendor/bar/baz/cfg.mk",
1771 "part.mk",
1772 "foo/font.mk",
1773 "bar/font.mk",
1774 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001775 for _, test := range testCases {
1776 t.Run(test.desc,
1777 func(t *testing.T) {
1778 ss, err := Convert(Request{
Sasha Smundak422b6142021-11-11 18:31:59 -08001779 MkFile: test.mkname,
1780 Reader: bytes.NewBufferString(test.in),
Sasha Smundak422b6142021-11-11 18:31:59 -08001781 OutputSuffix: ".star",
1782 SourceFS: fs,
1783 MakefileFinder: &testMakefileFinder{fs: fs},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001784 })
1785 if err != nil {
1786 t.Error(err)
1787 return
1788 }
1789 got := ss.String()
1790 if got != test.expected {
1791 t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
1792 strings.ReplaceAll(test.expected, "\n", "␤\n"),
1793 strings.ReplaceAll(got, "\n", "␤\n"))
1794 }
1795 })
1796 }
1797}