blob: a9b61978ab7c49011ae362f77b22908cd931ecf9 [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"]
926 if cfg.get("PRODUCT_LIST3") == None:
927 cfg["PRODUCT_LIST3"] = ["a"]
928 cfg["PRODUCT_LIST1"] = ["c"]
929 g.setdefault("PLATFORM_LIST", [])
930 g["PLATFORM_LIST"] += ["x"]
931 cfg["PRODUCT_PACKAGES"] = g["PLATFORM_LIST"][:]
932`,
933 },
934 {
935 desc: "assigment flavors2",
936 mkname: "product.mk",
937 in: `
938PRODUCT_LIST1 = a
939ifeq (0,1)
940 PRODUCT_LIST1 += b
941 PRODUCT_LIST2 += b
942endif
943PRODUCT_LIST1 += c
944PRODUCT_LIST2 += c
945`,
946 expected: `load("//build/make/core:product_config.rbc", "rblf")
947
948def init(g, handle):
949 cfg = rblf.cfg(handle)
950 cfg["PRODUCT_LIST1"] = ["a"]
951 if "0" == "1":
952 cfg["PRODUCT_LIST1"] += ["b"]
953 rblf.setdefault(handle, "PRODUCT_LIST2")
954 cfg["PRODUCT_LIST2"] += ["b"]
955 cfg["PRODUCT_LIST1"] += ["c"]
956 rblf.setdefault(handle, "PRODUCT_LIST2")
957 cfg["PRODUCT_LIST2"] += ["c"]
958`,
959 },
960 {
Cole Fauste2a37982022-03-09 16:00:17 -0800961 desc: "assigment setdefaults",
962 mkname: "product.mk",
963 in: `
964# All of these should have a setdefault because they're self-referential and not defined before
965PRODUCT_LIST1 = a $(PRODUCT_LIST1)
966PRODUCT_LIST2 ?= a $(PRODUCT_LIST2)
967PRODUCT_LIST3 += a
968
969# Now doing them again should not have a setdefault because they've already been set
970PRODUCT_LIST1 = a $(PRODUCT_LIST1)
971PRODUCT_LIST2 ?= a $(PRODUCT_LIST2)
972PRODUCT_LIST3 += a
973`,
974 expected: `# All of these should have a setdefault because they're self-referential and not defined before
975load("//build/make/core:product_config.rbc", "rblf")
976
977def init(g, handle):
978 cfg = rblf.cfg(handle)
979 rblf.setdefault(handle, "PRODUCT_LIST1")
980 cfg["PRODUCT_LIST1"] = (["a"] +
981 cfg.get("PRODUCT_LIST1", []))
982 if cfg.get("PRODUCT_LIST2") == None:
983 rblf.setdefault(handle, "PRODUCT_LIST2")
984 cfg["PRODUCT_LIST2"] = (["a"] +
985 cfg.get("PRODUCT_LIST2", []))
986 rblf.setdefault(handle, "PRODUCT_LIST3")
987 cfg["PRODUCT_LIST3"] += ["a"]
988 # Now doing them again should not have a setdefault because they've already been set
989 cfg["PRODUCT_LIST1"] = (["a"] +
990 cfg["PRODUCT_LIST1"])
991 if cfg.get("PRODUCT_LIST2") == None:
992 cfg["PRODUCT_LIST2"] = (["a"] +
993 cfg["PRODUCT_LIST2"])
994 cfg["PRODUCT_LIST3"] += ["a"]
995`,
996 },
997 {
Sasha Smundak3deb9682021-07-26 18:42:25 -0700998 desc: "soong namespace assignments",
999 mkname: "product.mk",
1000 in: `
1001SOONG_CONFIG_NAMESPACES += cvd
1002SOONG_CONFIG_cvd += launch_configs
Sasha Smundak65b547e2021-09-17 15:35:41 -07001003SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
Sasha Smundak3deb9682021-07-26 18:42:25 -07001004SOONG_CONFIG_cvd += grub_config
1005SOONG_CONFIG_cvd_grub_config += grub.cfg
Sasha Smundak65b547e2021-09-17 15:35:41 -07001006x := $(SOONG_CONFIG_cvd_grub_config)
Sasha Smundak3deb9682021-07-26 18:42:25 -07001007`,
1008 expected: `load("//build/make/core:product_config.rbc", "rblf")
1009
1010def init(g, handle):
1011 cfg = rblf.cfg(handle)
Sasha Smundak65b547e2021-09-17 15:35:41 -07001012 rblf.soong_config_namespace(g, "cvd")
1013 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
1014 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
Cole Faust1e275862022-04-26 14:28:04 -07001015 _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 -07001016`,
Cole Faustc00184e2021-11-08 12:08:57 -08001017 }, {
1018 desc: "soong namespace accesses",
1019 mkname: "product.mk",
1020 in: `
1021SOONG_CONFIG_NAMESPACES += cvd
1022SOONG_CONFIG_cvd += launch_configs
1023SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
1024SOONG_CONFIG_cvd += grub_config
1025SOONG_CONFIG_cvd_grub_config += grub.cfg
1026x := $(call soong_config_get,cvd,grub_config)
1027`,
1028 expected: `load("//build/make/core:product_config.rbc", "rblf")
1029
1030def init(g, handle):
1031 cfg = rblf.cfg(handle)
1032 rblf.soong_config_namespace(g, "cvd")
1033 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
1034 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
1035 _x = rblf.soong_config_get(g, "cvd", "grub_config")
1036`,
Sasha Smundak3deb9682021-07-26 18:42:25 -07001037 },
1038 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001039 desc: "string split",
1040 mkname: "product.mk",
1041 in: `
1042PRODUCT_LIST1 = a
1043local = b
1044local += c
1045FOO = d
1046FOO += e
1047PRODUCT_LIST1 += $(local)
1048PRODUCT_LIST1 += $(FOO)
1049`,
1050 expected: `load("//build/make/core:product_config.rbc", "rblf")
1051
1052def init(g, handle):
1053 cfg = rblf.cfg(handle)
1054 cfg["PRODUCT_LIST1"] = ["a"]
1055 _local = "b"
1056 _local += " " + "c"
1057 g["FOO"] = "d"
1058 g["FOO"] += " " + "e"
1059 cfg["PRODUCT_LIST1"] += (_local).split()
1060 cfg["PRODUCT_LIST1"] += (g["FOO"]).split()
1061`,
1062 },
1063 {
1064 desc: "apex_jars",
1065 mkname: "product.mk",
1066 in: `
1067PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex
1068`,
1069 expected: `load("//build/make/core:product_config.rbc", "rblf")
1070
1071def init(g, handle):
1072 cfg = rblf.cfg(handle)
1073 cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) +
1074 ["framework-minus-apex"])
1075`,
1076 },
1077 {
Cole Faust95b95cb2022-04-05 16:37:39 -07001078 desc: "strip/sort functions",
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001079 mkname: "product.mk",
1080 in: `
1081ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),)
1082 PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress)
1083endif
Cole Faust95b95cb2022-04-05 16:37:39 -07001084MY_VAR := $(sort b a c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001085`,
1086 expected: `load("//build/make/core:product_config.rbc", "rblf")
1087
1088def init(g, handle):
1089 cfg = rblf.cfg(handle)
1090 if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []):
Cole Faust816e0802022-03-04 12:04:31 -08001091 rblf.setdefault(handle, "PRODUCT_PACKAGES")
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001092 cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split()
Cole Faust95b95cb2022-04-05 16:37:39 -07001093 g["MY_VAR"] = rblf.mksort("b a c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001094`,
1095 },
1096 {
1097 desc: "strip func in condition",
1098 mkname: "product.mk",
1099 in: `
1100ifneq ($(strip $(TARGET_VENDOR)),)
1101endif
1102`,
1103 expected: `load("//build/make/core:product_config.rbc", "rblf")
1104
1105def init(g, handle):
1106 cfg = rblf.cfg(handle)
Sasha Smundak0554d762021-07-08 18:26:12 -07001107 if rblf.mkstrip(g.get("TARGET_VENDOR", "")):
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001108 pass
1109`,
1110 },
1111 {
1112 desc: "ref after set",
1113 mkname: "product.mk",
1114 in: `
1115PRODUCT_ADB_KEYS:=value
1116FOO := $(PRODUCT_ADB_KEYS)
1117ifneq (,$(PRODUCT_ADB_KEYS))
1118endif
1119`,
1120 expected: `load("//build/make/core:product_config.rbc", "rblf")
1121
1122def init(g, handle):
1123 cfg = rblf.cfg(handle)
1124 g["PRODUCT_ADB_KEYS"] = "value"
1125 g["FOO"] = g["PRODUCT_ADB_KEYS"]
1126 if g["PRODUCT_ADB_KEYS"]:
1127 pass
1128`,
1129 },
1130 {
1131 desc: "ref before set",
1132 mkname: "product.mk",
1133 in: `
1134V1 := $(PRODUCT_ADB_KEYS)
1135ifeq (,$(PRODUCT_ADB_KEYS))
1136 V2 := $(PRODUCT_ADB_KEYS)
1137 PRODUCT_ADB_KEYS:=foo
1138 V3 := $(PRODUCT_ADB_KEYS)
1139endif`,
1140 expected: `load("//build/make/core:product_config.rbc", "rblf")
1141
1142def init(g, handle):
1143 cfg = rblf.cfg(handle)
1144 g["V1"] = g.get("PRODUCT_ADB_KEYS", "")
1145 if not g.get("PRODUCT_ADB_KEYS", ""):
1146 g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
1147 g["PRODUCT_ADB_KEYS"] = "foo"
1148 g["V3"] = g["PRODUCT_ADB_KEYS"]
1149`,
1150 },
Sasha Smundak6609ba72021-07-22 18:32:56 -07001151 {
1152 desc: "Dynamic inherit path",
1153 mkname: "product.mk",
1154 in: `
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001155MY_PATH:=foo
Sasha Smundak6609ba72021-07-22 18:32:56 -07001156$(call inherit-product,vendor/$(MY_PATH)/cfg.mk)
1157`,
1158 expected: `load("//build/make/core:product_config.rbc", "rblf")
1159load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1160load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init")
1161
1162def init(g, handle):
1163 cfg = rblf.cfg(handle)
1164 g["MY_PATH"] = "foo"
1165 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001166 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1167 "vendor/bar/baz/cfg.mk": ("vendor/bar/baz/cfg", _cfg1_init),
Sasha Smundak6609ba72021-07-22 18:32:56 -07001168 }.get("vendor/%s/cfg.mk" % g["MY_PATH"])
1169 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1170 if not _varmod_init:
Cole Faust7321b092021-12-21 16:11:16 -08001171 rblf.mkerror("product.mk", "Cannot find %s" % ("vendor/%s/cfg.mk" % g["MY_PATH"]))
Sasha Smundak6609ba72021-07-22 18:32:56 -07001172 rblf.inherit(handle, _varmod, _varmod_init)
1173`,
1174 },
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001175 {
1176 desc: "Dynamic inherit with hint",
1177 mkname: "product.mk",
1178 in: `
1179MY_PATH:=foo
1180#RBC# include_top vendor/foo1
1181$(call inherit-product,$(MY_PATH)/cfg.mk)
Cole Faust9df1d732022-04-26 16:27:22 -07001182#RBC# include_top vendor/foo1
1183$(call inherit-product,$(MY_OTHER_PATH))
1184#RBC# include_top vendor/foo1
Cole Faust74ac0272022-06-14 12:45:26 -07001185$(call inherit-product,vendor/$(MY_OTHER_PATH))
1186#RBC# include_top vendor/foo1
Cole Faust9df1d732022-04-26 16:27:22 -07001187$(foreach f,$(MY_MAKEFILES), \
1188 $(call inherit-product,$(f)))
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001189`,
1190 expected: `load("//build/make/core:product_config.rbc", "rblf")
1191load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1192
1193def init(g, handle):
1194 cfg = rblf.cfg(handle)
1195 g["MY_PATH"] = "foo"
Cole Faust93f8d392022-03-02 13:31:30 -08001196 _entry = {
1197 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1198 }.get("%s/cfg.mk" % g["MY_PATH"])
1199 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1200 if not _varmod_init:
1201 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1202 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust9df1d732022-04-26 16:27:22 -07001203 _entry = {
1204 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1205 }.get(g.get("MY_OTHER_PATH", ""))
1206 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1207 if not _varmod_init:
1208 rblf.mkerror("product.mk", "Cannot find %s" % (g.get("MY_OTHER_PATH", "")))
1209 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust74ac0272022-06-14 12:45:26 -07001210 _entry = {
1211 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1212 }.get("vendor/%s" % g.get("MY_OTHER_PATH", ""))
1213 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1214 if not _varmod_init:
1215 rblf.mkerror("product.mk", "Cannot find %s" % ("vendor/%s" % g.get("MY_OTHER_PATH", "")))
1216 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust9df1d732022-04-26 16:27:22 -07001217 for f in rblf.words(g.get("MY_MAKEFILES", "")):
1218 _entry = {
1219 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1220 }.get(f)
1221 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1222 if not _varmod_init:
1223 rblf.mkerror("product.mk", "Cannot find %s" % (f))
1224 rblf.inherit(handle, _varmod, _varmod_init)
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001225`,
1226 },
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001227 {
Cole Faustf7ed5342021-12-21 14:15:12 -08001228 desc: "Dynamic inherit with duplicated hint",
1229 mkname: "product.mk",
1230 in: `
1231MY_PATH:=foo
1232#RBC# include_top vendor/foo1
1233$(call inherit-product,$(MY_PATH)/cfg.mk)
1234#RBC# include_top vendor/foo1
Cole Faust7940c6a2022-01-31 15:54:05 -08001235#RBC# include_top vendor/foo1
Cole Faustf7ed5342021-12-21 14:15:12 -08001236$(call inherit-product,$(MY_PATH)/cfg.mk)
1237`,
1238 expected: `load("//build/make/core:product_config.rbc", "rblf")
1239load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1240
1241def init(g, handle):
1242 cfg = rblf.cfg(handle)
1243 g["MY_PATH"] = "foo"
Cole Faust93f8d392022-03-02 13:31:30 -08001244 _entry = {
1245 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1246 }.get("%s/cfg.mk" % g["MY_PATH"])
1247 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1248 if not _varmod_init:
1249 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1250 rblf.inherit(handle, _varmod, _varmod_init)
1251 _entry = {
1252 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1253 }.get("%s/cfg.mk" % g["MY_PATH"])
1254 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1255 if not _varmod_init:
1256 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1257 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faustf7ed5342021-12-21 14:15:12 -08001258`,
1259 },
1260 {
Cole Faust069aba62022-01-26 17:47:33 -08001261 desc: "Dynamic inherit path that lacks hint",
Cole Faust6c934f62022-01-06 15:51:12 -08001262 mkname: "product.mk",
1263 in: `
1264#RBC# include_top foo
1265$(call inherit-product,$(MY_VAR)/font.mk)
1266
1267#RBC# include_top foo
1268
1269# There's some space and even this comment between the include_top and the inherit-product
1270
1271$(call inherit-product,$(MY_VAR)/font.mk)
1272
1273$(call inherit-product,$(MY_VAR)/font.mk)
1274`,
Cole Faust7940c6a2022-01-31 15:54:05 -08001275 expected: `load("//build/make/core:product_config.rbc", "rblf")
Cole Faust6c934f62022-01-06 15:51:12 -08001276load("//foo:font.star|init", _font_init = "init")
Cole Faust069aba62022-01-26 17:47:33 -08001277load("//bar:font.star|init", _font1_init = "init")
Cole Faust6c934f62022-01-06 15:51:12 -08001278
1279def init(g, handle):
1280 cfg = rblf.cfg(handle)
Cole Faust93f8d392022-03-02 13:31:30 -08001281 _entry = {
1282 "foo/font.mk": ("foo/font", _font_init),
1283 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1284 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1285 if not _varmod_init:
1286 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1287 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001288 # There's some space and even this comment between the include_top and the inherit-product
Cole Faust93f8d392022-03-02 13:31:30 -08001289 _entry = {
1290 "foo/font.mk": ("foo/font", _font_init),
1291 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1292 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1293 if not _varmod_init:
1294 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1295 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faustf4e72cf2022-02-08 12:49:37 -08001296 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 -08001297 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001298 "foo/font.mk": ("foo/font", _font_init),
Cole Faust069aba62022-01-26 17:47:33 -08001299 "bar/font.mk": ("bar/font", _font1_init),
Cole Faust6c934f62022-01-06 15:51:12 -08001300 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1301 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1302 if not _varmod_init:
1303 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1304 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001305`,
1306 },
1307 {
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001308 desc: "Ignore make rules",
1309 mkname: "product.mk",
1310 in: `
Cole Faust00afd4f2022-04-26 14:01:56 -07001311foo: PRIVATE_VARIABLE = some_tool $< $@
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001312foo: foo.c
1313 gcc -o $@ $*`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001314 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001315
1316def init(g, handle):
1317 cfg = rblf.cfg(handle)
Cole Faust00afd4f2022-04-26 14:01:56 -07001318 rblf.mk2rbc_error("product.mk:2", "Only simple variables are handled")
1319 rblf.mk2rbc_error("product.mk:3", "unsupported line rule: foo: foo.c\n#gcc -o $@ $*")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001320`,
1321 },
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001322 {
1323 desc: "Flag override",
1324 mkname: "product.mk",
1325 in: `
1326override FOO:=`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001327 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001328
1329def init(g, handle):
1330 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001331 rblf.mk2rbc_error("product.mk:2", "cannot handle override directive")
Sasha Smundak422b6142021-11-11 18:31:59 -08001332`,
1333 },
1334 {
1335 desc: "Bad expression",
1336 mkname: "build/product.mk",
1337 in: `
1338ifeq (,$(call foobar))
1339endif
Cole Faust1e275862022-04-26 14:28:04 -07001340my_sources := $(local-generated-sources-dir)
Sasha Smundak422b6142021-11-11 18:31:59 -08001341`,
1342 expected: `load("//build/make/core:product_config.rbc", "rblf")
1343
1344def init(g, handle):
1345 cfg = rblf.cfg(handle)
1346 if rblf.mk2rbc_error("build/product.mk:2", "cannot handle invoking foobar"):
1347 pass
Cole Faust1e275862022-04-26 14:28:04 -07001348 _my_sources = rblf.mk2rbc_error("build/product.mk:4", "local-generated-sources-dir is not supported")
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001349`,
1350 },
Cole Faust4eadba72021-12-07 11:54:52 -08001351 {
1352 desc: "if expression",
1353 mkname: "product.mk",
1354 in: `
1355TEST_VAR := foo
1356TEST_VAR_LIST := foo
1357TEST_VAR_LIST += bar
1358TEST_VAR_2 := $(if $(TEST_VAR),bar)
1359TEST_VAR_3 := $(if $(TEST_VAR),bar,baz)
Cole Faust421a1922022-03-16 14:35:45 -07001360TEST_VAR_4 := $(if $(TEST_VAR),$(TEST_VAR_LIST))
Cole Faust4eadba72021-12-07 11:54:52 -08001361`,
1362 expected: `load("//build/make/core:product_config.rbc", "rblf")
1363
1364def init(g, handle):
1365 cfg = rblf.cfg(handle)
1366 g["TEST_VAR"] = "foo"
1367 g["TEST_VAR_LIST"] = ["foo"]
1368 g["TEST_VAR_LIST"] += ["bar"]
1369 g["TEST_VAR_2"] = ("bar" if g["TEST_VAR"] else "")
1370 g["TEST_VAR_3"] = ("bar" if g["TEST_VAR"] else "baz")
Cole Faust421a1922022-03-16 14:35:45 -07001371 g["TEST_VAR_4"] = (g["TEST_VAR_LIST"] if g["TEST_VAR"] else [])
Cole Faust4eadba72021-12-07 11:54:52 -08001372`,
1373 },
Cole Faustc36c9622021-12-07 15:20:45 -08001374 {
1375 desc: "substitution references",
1376 mkname: "product.mk",
1377 in: `
1378SOURCES := foo.c bar.c
1379OBJECTS := $(SOURCES:.c=.o)
1380OBJECTS2 := $(SOURCES:%.c=%.o)
1381`,
1382 expected: `load("//build/make/core:product_config.rbc", "rblf")
1383
1384def init(g, handle):
1385 cfg = rblf.cfg(handle)
1386 g["SOURCES"] = "foo.c bar.c"
1387 g["OBJECTS"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1388 g["OBJECTS2"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1389`,
1390 },
Cole Faustb0d32ab2021-12-09 14:00:59 -08001391 {
1392 desc: "foreach expressions",
1393 mkname: "product.mk",
1394 in: `
1395BOOT_KERNEL_MODULES := foo.ko bar.ko
1396BOOT_KERNEL_MODULES_FILTER := $(foreach m,$(BOOT_KERNEL_MODULES),%/$(m))
1397BOOT_KERNEL_MODULES_LIST := foo.ko
1398BOOT_KERNEL_MODULES_LIST += bar.ko
1399BOOT_KERNEL_MODULES_FILTER_2 := $(foreach m,$(BOOT_KERNEL_MODULES_LIST),%/$(m))
Cole Faust72374fc2022-05-05 11:45:04 -07001400NESTED_LISTS := $(foreach m,$(SOME_VAR),$(BOOT_KERNEL_MODULES_LIST))
1401NESTED_LISTS_2 := $(foreach x,$(SOME_VAR),$(foreach y,$(x),prefix$(y)))
Cole Faustb0d32ab2021-12-09 14:00:59 -08001402
Cole Faustb67aa082022-02-28 16:39:59 -08001403FOREACH_WITH_IF := $(foreach module,\
1404 $(BOOT_KERNEL_MODULES_LIST),\
1405 $(if $(filter $(module),foo.ko),,$(error module "$(module)" has an error!)))
Cole Faustf035d402022-03-28 14:02:50 -07001406
1407# Same as above, but not assigning it to a variable allows it to be converted to statements
1408$(foreach module,\
1409 $(BOOT_KERNEL_MODULES_LIST),\
1410 $(if $(filter $(module),foo.ko),,$(error module "$(module)" has an error!)))
Cole Faustb0d32ab2021-12-09 14:00:59 -08001411`,
1412 expected: `load("//build/make/core:product_config.rbc", "rblf")
1413
1414def init(g, handle):
1415 cfg = rblf.cfg(handle)
1416 g["BOOT_KERNEL_MODULES"] = "foo.ko bar.ko"
1417 g["BOOT_KERNEL_MODULES_FILTER"] = ["%%/%s" % m for m in rblf.words(g["BOOT_KERNEL_MODULES"])]
1418 g["BOOT_KERNEL_MODULES_LIST"] = ["foo.ko"]
1419 g["BOOT_KERNEL_MODULES_LIST"] += ["bar.ko"]
1420 g["BOOT_KERNEL_MODULES_FILTER_2"] = ["%%/%s" % m for m in g["BOOT_KERNEL_MODULES_LIST"]]
Cole Faust72374fc2022-05-05 11:45:04 -07001421 g["NESTED_LISTS"] = rblf.flatten_2d_list([g["BOOT_KERNEL_MODULES_LIST"] for m in rblf.words(g.get("SOME_VAR", ""))])
1422 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 -08001423 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 -07001424 # Same as above, but not assigning it to a variable allows it to be converted to statements
1425 for module in g["BOOT_KERNEL_MODULES_LIST"]:
1426 if not rblf.filter(module, "foo.ko"):
1427 rblf.mkerror("product.mk", "module \"%s\" has an error!" % module)
Cole Faustb0d32ab2021-12-09 14:00:59 -08001428`,
1429 },
Cole Faust0484c232021-12-22 14:08:08 -08001430 {
1431 desc: "List appended to string",
1432 mkname: "product.mk",
1433 in: `
1434NATIVE_BRIDGE_PRODUCT_PACKAGES := \
1435 libnative_bridge_vdso.native_bridge \
1436 native_bridge_guest_app_process.native_bridge \
1437 native_bridge_guest_linker.native_bridge
1438
1439NATIVE_BRIDGE_MODIFIED_GUEST_LIBS := \
1440 libaaudio \
1441 libamidi \
1442 libandroid \
1443 libandroid_runtime
1444
1445NATIVE_BRIDGE_PRODUCT_PACKAGES += \
1446 $(addsuffix .native_bridge,$(NATIVE_BRIDGE_ORIG_GUEST_LIBS))
1447`,
1448 expected: `load("//build/make/core:product_config.rbc", "rblf")
1449
1450def init(g, handle):
1451 cfg = rblf.cfg(handle)
1452 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] = "libnative_bridge_vdso.native_bridge native_bridge_guest_app_process.native_bridge native_bridge_guest_linker.native_bridge"
1453 g["NATIVE_BRIDGE_MODIFIED_GUEST_LIBS"] = "libaaudio libamidi libandroid libandroid_runtime"
1454 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] += " " + " ".join(rblf.addsuffix(".native_bridge", g.get("NATIVE_BRIDGE_ORIG_GUEST_LIBS", "")))
1455`,
1456 },
Cole Faustb1103e22022-01-06 15:22:05 -08001457 {
1458 desc: "Math functions",
1459 mkname: "product.mk",
1460 in: `
1461# Test the math functions defined in build/make/common/math.mk
1462ifeq ($(call math_max,2,5),5)
1463endif
1464ifeq ($(call math_min,2,5),2)
1465endif
1466ifeq ($(call math_gt_or_eq,2,5),true)
1467endif
1468ifeq ($(call math_gt,2,5),true)
1469endif
1470ifeq ($(call math_lt,2,5),true)
1471endif
1472ifeq ($(call math_gt_or_eq,2,5),)
1473endif
1474ifeq ($(call math_gt,2,5),)
1475endif
1476ifeq ($(call math_lt,2,5),)
1477endif
1478ifeq ($(call math_gt_or_eq,$(MY_VAR), 5),true)
1479endif
1480ifeq ($(call math_gt_or_eq,$(MY_VAR),$(MY_OTHER_VAR)),true)
1481endif
1482ifeq ($(call math_gt_or_eq,100$(MY_VAR),10),true)
1483endif
1484`,
1485 expected: `# Test the math functions defined in build/make/common/math.mk
1486load("//build/make/core:product_config.rbc", "rblf")
1487
1488def init(g, handle):
1489 cfg = rblf.cfg(handle)
1490 if max(2, 5) == 5:
1491 pass
1492 if min(2, 5) == 2:
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 2 <= 5:
1503 pass
1504 if 2 >= 5:
1505 pass
1506 if int(g.get("MY_VAR", "")) >= 5:
1507 pass
1508 if int(g.get("MY_VAR", "")) >= int(g.get("MY_OTHER_VAR", "")):
1509 pass
1510 if int("100%s" % g.get("MY_VAR", "")) >= 10:
1511 pass
1512`,
1513 },
Cole Faustf92c9f22022-03-14 14:35:50 -07001514 {
1515 desc: "Type hints",
1516 mkname: "product.mk",
1517 in: `
1518# Test type hints
1519#RBC# type_hint list MY_VAR MY_VAR_2
1520# Unsupported type
1521#RBC# type_hint bool MY_VAR_3
1522# Invalid syntax
1523#RBC# type_hint list
1524# Duplicated variable
1525#RBC# type_hint list MY_VAR_2
1526#RBC# type_hint list my-local-var-with-dashes
Cole Faust421a1922022-03-16 14:35:45 -07001527#RBC# type_hint string MY_STRING_VAR
Cole Faustf92c9f22022-03-14 14:35:50 -07001528
1529MY_VAR := foo
1530MY_VAR_UNHINTED := foo
1531
1532# Vars set after other statements still get the hint
1533MY_VAR_2 := foo
1534
1535# You can't specify a type hint after the first statement
1536#RBC# type_hint list MY_VAR_4
1537MY_VAR_4 := foo
1538
1539my-local-var-with-dashes := foo
Cole Faust421a1922022-03-16 14:35:45 -07001540
1541MY_STRING_VAR := $(wildcard foo/bar.mk)
Cole Faustf92c9f22022-03-14 14:35:50 -07001542`,
1543 expected: `# Test type hints
1544# Unsupported type
1545load("//build/make/core:product_config.rbc", "rblf")
1546
1547def init(g, handle):
1548 cfg = rblf.cfg(handle)
1549 rblf.mk2rbc_error("product.mk:5", "Invalid type_hint annotation. Only list/string types are accepted, found bool")
1550 # Invalid syntax
1551 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")
1552 # Duplicated variable
1553 rblf.mk2rbc_error("product.mk:9", "Duplicate type hint for variable MY_VAR_2")
1554 g["MY_VAR"] = ["foo"]
1555 g["MY_VAR_UNHINTED"] = "foo"
1556 # Vars set after other statements still get the hint
1557 g["MY_VAR_2"] = ["foo"]
1558 # You can't specify a type hint after the first statement
Cole Faust421a1922022-03-16 14:35:45 -07001559 rblf.mk2rbc_error("product.mk:20", "type_hint annotations must come before the first Makefile statement")
Cole Faustf92c9f22022-03-14 14:35:50 -07001560 g["MY_VAR_4"] = "foo"
1561 _my_local_var_with_dashes = ["foo"]
Cole Faust421a1922022-03-16 14:35:45 -07001562 g["MY_STRING_VAR"] = " ".join(rblf.expand_wildcard("foo/bar.mk"))
Cole Faustf92c9f22022-03-14 14:35:50 -07001563`,
1564 },
Cole Faustf5adedc2022-03-18 14:05:06 -07001565 {
1566 desc: "Set LOCAL_PATH to my-dir",
1567 mkname: "product.mk",
1568 in: `
1569LOCAL_PATH := $(call my-dir)
1570`,
1571 expected: `load("//build/make/core:product_config.rbc", "rblf")
1572
1573def init(g, handle):
1574 cfg = rblf.cfg(handle)
1575
1576`,
1577 },
Cole Faustf035d402022-03-28 14:02:50 -07001578 {
1579 desc: "Evals",
1580 mkname: "product.mk",
1581 in: `
1582$(eval)
1583$(eval MY_VAR := foo)
1584$(eval # This is a test of eval functions)
1585$(eval $(TOO_COMPLICATED) := bar)
Cole Faust20052982022-04-22 14:43:55 -07001586$(eval include foo/font.mk)
1587$(eval $(call inherit-product,vendor/foo1/cfg.mk))
1588
Cole Faustf035d402022-03-28 14:02:50 -07001589$(foreach x,$(MY_LIST_VAR), \
1590 $(eval PRODUCT_COPY_FILES += foo/bar/$(x):$(TARGET_COPY_OUT_VENDOR)/etc/$(x)) \
Cole Faust20052982022-04-22 14:43:55 -07001591 $(if $(MY_OTHER_VAR),$(eval PRODUCT_COPY_FILES += $(MY_OTHER_VAR):foo/bar/$(x))))
Cole Faustf035d402022-03-28 14:02:50 -07001592
Cole Faust20052982022-04-22 14:43:55 -07001593$(foreach x,$(MY_LIST_VAR), \
1594 $(eval include foo/$(x).mk))
Cole Faust73660422023-01-05 11:07:47 -08001595
1596# Check that we get as least close to correct line numbers for errors on statements inside evals
1597$(eval $(call inherit-product,$(A_VAR)))
Cole Faustf035d402022-03-28 14:02:50 -07001598`,
1599 expected: `load("//build/make/core:product_config.rbc", "rblf")
Cole Faust20052982022-04-22 14:43:55 -07001600load("//foo:font.star", _font_init = "init")
1601load("//vendor/foo1:cfg.star", _cfg_init = "init")
Cole Faustf035d402022-03-28 14:02:50 -07001602
1603def init(g, handle):
1604 cfg = rblf.cfg(handle)
1605 g["MY_VAR"] = "foo"
1606 # This is a test of eval functions
Cole Faust20052982022-04-22 14:43:55 -07001607 rblf.mk2rbc_error("product.mk:5", "Eval expression too complex; only assignments, comments, includes, and inherit-products are supported")
1608 _font_init(g, handle)
1609 rblf.inherit(handle, "vendor/foo1/cfg", _cfg_init)
Cole Faustf035d402022-03-28 14:02:50 -07001610 for x in rblf.words(g.get("MY_LIST_VAR", "")):
1611 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
1612 cfg["PRODUCT_COPY_FILES"] += ("foo/bar/%s:%s/etc/%s" % (x, g.get("TARGET_COPY_OUT_VENDOR", ""), x)).split()
1613 if g.get("MY_OTHER_VAR", ""):
1614 cfg["PRODUCT_COPY_FILES"] += ("%s:foo/bar/%s" % (g.get("MY_OTHER_VAR", ""), x)).split()
Cole Faust20052982022-04-22 14:43:55 -07001615 for x in rblf.words(g.get("MY_LIST_VAR", "")):
1616 _entry = {
1617 "foo/font.mk": ("foo/font", _font_init),
Cole Faust72374fc2022-05-05 11:45:04 -07001618 }.get("foo/%s.mk" % x)
Cole Faust20052982022-04-22 14:43:55 -07001619 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1620 if not _varmod_init:
Cole Faust72374fc2022-05-05 11:45:04 -07001621 rblf.mkerror("product.mk", "Cannot find %s" % ("foo/%s.mk" % x))
Cole Faust20052982022-04-22 14:43:55 -07001622 _varmod_init(g, handle)
Cole Faust73660422023-01-05 11:07:47 -08001623 # Check that we get as least close to correct line numbers for errors on statements inside evals
1624 rblf.mk2rbc_error("product.mk:17", "inherit-product/include argument is too complex")
Cole Faustf035d402022-03-28 14:02:50 -07001625`,
1626 },
Cole Faust5d5fcc32022-04-26 18:02:05 -07001627 {
1628 desc: ".KATI_READONLY",
1629 mkname: "product.mk",
1630 in: `
1631MY_VAR := foo
1632.KATI_READONLY := MY_VAR
1633`,
1634 expected: `load("//build/make/core:product_config.rbc", "rblf")
1635
1636def init(g, handle):
1637 cfg = rblf.cfg(handle)
1638 g["MY_VAR"] = "foo"
1639`,
1640 },
Cole Faust13238772022-04-28 14:29:57 -07001641 {
1642 desc: "Complicated variable references",
1643 mkname: "product.mk",
1644 in: `
1645MY_VAR := foo
1646MY_VAR_2 := MY_VAR
1647MY_VAR_3 := $($(MY_VAR_2))
1648MY_VAR_4 := $(foo bar)
1649MY_VAR_5 := $($(MY_VAR_2) bar)
1650`,
1651 expected: `load("//build/make/core:product_config.rbc", "rblf")
1652
1653def init(g, handle):
1654 cfg = rblf.cfg(handle)
1655 g["MY_VAR"] = "foo"
1656 g["MY_VAR_2"] = "MY_VAR"
1657 g["MY_VAR_3"] = (cfg).get(g["MY_VAR_2"], (g).get(g["MY_VAR_2"], ""))
1658 g["MY_VAR_4"] = rblf.mk2rbc_error("product.mk:5", "cannot handle invoking foo")
1659 g["MY_VAR_5"] = rblf.mk2rbc_error("product.mk:6", "reference is too complex: $(MY_VAR_2) bar")
1660`,
1661 },
Cole Faustd2daabf2022-12-12 17:38:01 -08001662 {
1663 desc: "Conditional functions",
1664 mkname: "product.mk",
1665 in: `
1666B := foo
1667X := $(or $(A))
1668X := $(or $(A),$(B))
1669X := $(or $(A),$(B),$(C))
1670X := $(and $(A))
1671X := $(and $(A),$(B))
1672X := $(and $(A),$(B),$(C))
1673X := $(or $(A),$(B)) Y
1674
1675D := $(wildcard *.mk)
1676X := $(or $(B),$(D))
1677`,
1678 expected: `load("//build/make/core:product_config.rbc", "rblf")
1679
1680def init(g, handle):
1681 cfg = rblf.cfg(handle)
1682 g["B"] = "foo"
1683 g["X"] = g.get("A", "")
1684 g["X"] = g.get("A", "") or g["B"]
1685 g["X"] = g.get("A", "") or g["B"] or g.get("C", "")
1686 g["X"] = g.get("A", "")
1687 g["X"] = g.get("A", "") and g["B"]
1688 g["X"] = g.get("A", "") and g["B"] and g.get("C", "")
1689 g["X"] = "%s Y" % g.get("A", "") or g["B"]
1690 g["D"] = rblf.expand_wildcard("*.mk")
1691 g["X"] = rblf.mk2rbc_error("product.mk:12", "Expected all arguments to $(or) or $(and) to have the same type, found \"string\" and \"list\"")
1692`,
1693 },
Cole Faust2dee63d2022-12-12 18:11:00 -08001694 {
1695
1696 desc: "is-lower/is-upper",
1697 mkname: "product.mk",
1698 in: `
1699X := $(call to-lower,aBc)
1700X := $(call to-upper,aBc)
1701X := $(call to-lower,$(VAR))
1702X := $(call to-upper,$(VAR))
1703`,
1704 expected: `load("//build/make/core:product_config.rbc", "rblf")
1705
1706def init(g, handle):
1707 cfg = rblf.cfg(handle)
1708 g["X"] = ("aBc").lower()
1709 g["X"] = ("aBc").upper()
1710 g["X"] = (g.get("VAR", "")).lower()
1711 g["X"] = (g.get("VAR", "")).upper()
1712`,
1713 },
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001714}
1715
1716var known_variables = []struct {
1717 name string
1718 class varClass
1719 starlarkType
1720}{
Cole Faustf1f44d32021-11-16 14:52:12 -08001721 {"NATIVE_COVERAGE", VarClassSoong, starlarkTypeBool},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001722 {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
1723 {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
1724 {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
1725 {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
1726 {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
1727 {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
1728 {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
1729 {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
1730 {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
1731 {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
1732 {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
1733 {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
1734 {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
1735 {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
1736}
1737
Sasha Smundak6609ba72021-07-22 18:32:56 -07001738type testMakefileFinder struct {
1739 fs fs.FS
1740 root string
1741 files []string
1742}
1743
1744func (t *testMakefileFinder) Find(root string) []string {
1745 if t.files != nil || root == t.root {
1746 return t.files
1747 }
1748 t.files = make([]string, 0)
1749 fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error {
1750 if err != nil {
1751 return err
1752 }
1753 if d.IsDir() {
1754 base := filepath.Base(path)
1755 if base[0] == '.' && len(base) > 1 {
1756 return fs.SkipDir
1757 }
1758 return nil
1759 }
1760 if strings.HasSuffix(path, ".mk") {
1761 t.files = append(t.files, path)
1762 }
1763 return nil
1764 })
1765 return t.files
1766}
1767
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001768func TestGood(t *testing.T) {
1769 for _, v := range known_variables {
1770 KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
1771 }
Sasha Smundak6609ba72021-07-22 18:32:56 -07001772 fs := NewFindMockFS([]string{
1773 "vendor/foo1/cfg.mk",
1774 "vendor/bar/baz/cfg.mk",
1775 "part.mk",
1776 "foo/font.mk",
1777 "bar/font.mk",
1778 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001779 for _, test := range testCases {
1780 t.Run(test.desc,
1781 func(t *testing.T) {
1782 ss, err := Convert(Request{
Sasha Smundak422b6142021-11-11 18:31:59 -08001783 MkFile: test.mkname,
1784 Reader: bytes.NewBufferString(test.in),
Sasha Smundak422b6142021-11-11 18:31:59 -08001785 OutputSuffix: ".star",
1786 SourceFS: fs,
1787 MakefileFinder: &testMakefileFinder{fs: fs},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001788 })
1789 if err != nil {
1790 t.Error(err)
1791 return
1792 }
1793 got := ss.String()
1794 if got != test.expected {
1795 t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
1796 strings.ReplaceAll(test.expected, "\n", "␤\n"),
1797 strings.ReplaceAll(got, "\n", "␤\n"))
1798 }
1799 })
1800 }
1801}