blob: fa3cd83f893b6531ad22c3b5a32e33ee4bb1bade [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"
19 "strings"
20 "testing"
21)
22
23var testCases = []struct {
24 desc string
25 mkname string
26 in string
27 expected string
28}{
29 {
30 desc: "Comment",
31 mkname: "product.mk",
32 in: `
33# Comment
34# FOO= a\
35 b
36`,
37 expected: `# Comment
38# FOO= a
39# b
40load("//build/make/core:product_config.rbc", "rblf")
41
42def init(g, handle):
43 cfg = rblf.cfg(handle)
44`,
45 },
46 {
47 desc: "Name conversion",
48 mkname: "path/bar-baz.mk",
49 in: `
50# Comment
51`,
52 expected: `# Comment
53load("//build/make/core:product_config.rbc", "rblf")
54
55def init(g, handle):
56 cfg = rblf.cfg(handle)
57`,
58 },
59 {
60 desc: "Item variable",
61 mkname: "pixel3.mk",
62 in: `
63PRODUCT_NAME := Pixel 3
64PRODUCT_MODEL :=
65local_var = foo
66`,
67 expected: `load("//build/make/core:product_config.rbc", "rblf")
68
69def init(g, handle):
70 cfg = rblf.cfg(handle)
71 cfg["PRODUCT_NAME"] = "Pixel 3"
72 cfg["PRODUCT_MODEL"] = ""
73 _local_var = "foo"
74`,
75 },
76 {
77 desc: "List variable",
78 mkname: "pixel4.mk",
79 in: `
80PRODUCT_PACKAGES = package1 package2
81PRODUCT_COPY_FILES += file2:target
82PRODUCT_PACKAGES += package3
83PRODUCT_COPY_FILES =
84`,
85 expected: `load("//build/make/core:product_config.rbc", "rblf")
86
87def init(g, handle):
88 cfg = rblf.cfg(handle)
89 cfg["PRODUCT_PACKAGES"] = [
90 "package1",
91 "package2",
92 ]
93 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
94 cfg["PRODUCT_COPY_FILES"] += ["file2:target"]
95 cfg["PRODUCT_PACKAGES"] += ["package3"]
96 cfg["PRODUCT_COPY_FILES"] = []
97`,
98 },
99 {
100 desc: "Unknown function",
101 mkname: "product.mk",
102 in: `
103PRODUCT_NAME := $(call foo, bar)
104`,
105 expected: `# MK2RBC TRANSLATION ERROR: cannot handle invoking foo
106# PRODUCT_NAME := $(call foo, bar)
107load("//build/make/core:product_config.rbc", "rblf")
108
109def init(g, handle):
110 cfg = rblf.cfg(handle)
111 rblf.warning("product.mk", "partially successful conversion")
112`,
113 },
114 {
115 desc: "Inherit configuration always",
116 mkname: "product.mk",
117 in: `
118ifdef PRODUCT_NAME
119$(call inherit-product, part.mk)
120else # Comment
121$(call inherit-product, $(LOCAL_PATH)/part.mk)
122endif
123`,
124 expected: `load("//build/make/core:product_config.rbc", "rblf")
125load(":part.star", _part_init = "init")
126
127def init(g, handle):
128 cfg = rblf.cfg(handle)
129 if g.get("PRODUCT_NAME") != None:
130 rblf.inherit(handle, "part", _part_init)
131 else:
132 # Comment
133 rblf.inherit(handle, "./part", _part_init)
134`,
135 },
136 {
137 desc: "Inherit configuration if it exists",
138 mkname: "product.mk",
139 in: `
140$(call inherit-product-if-exists, part.mk)
141`,
142 expected: `load("//build/make/core:product_config.rbc", "rblf")
143load(":part.star|init", _part_init = "init")
144
145def init(g, handle):
146 cfg = rblf.cfg(handle)
147 if _part_init != None:
148 rblf.inherit(handle, "part", _part_init)
149`,
150 },
151
152 {
153 desc: "Include configuration",
154 mkname: "product.mk",
155 in: `
156ifdef PRODUCT_NAME
157include part.mk
158else
159-include $(LOCAL_PATH)/part.mk)
160endif
161`,
162 expected: `load("//build/make/core:product_config.rbc", "rblf")
163load(":part.star", _part_init = "init")
164
165def init(g, handle):
166 cfg = rblf.cfg(handle)
167 if g.get("PRODUCT_NAME") != None:
168 _part_init(g, handle)
169 else:
170 if _part_init != None:
171 _part_init(g, handle)
172`,
173 },
174
175 {
176 desc: "Synonymous inherited configurations",
177 mkname: "path/product.mk",
178 in: `
179$(call inherit-product, foo/font.mk)
180$(call inherit-product, bar/font.mk)
181`,
182 expected: `load("//build/make/core:product_config.rbc", "rblf")
183load("//foo:font.star", _font_init = "init")
184load("//bar:font.star", _font1_init = "init")
185
186def init(g, handle):
187 cfg = rblf.cfg(handle)
188 rblf.inherit(handle, "foo/font", _font_init)
189 rblf.inherit(handle, "bar/font", _font1_init)
190`,
191 },
192 {
193 desc: "Directive define",
194 mkname: "product.mk",
195 in: `
196define some-macro
197 $(info foo)
198endef
199`,
200 expected: `# MK2RBC TRANSLATION ERROR: define is not supported: some-macro
201# define some-macro
202# $(info foo)
203# endef
204load("//build/make/core:product_config.rbc", "rblf")
205
206def init(g, handle):
207 cfg = rblf.cfg(handle)
208 rblf.warning("product.mk", "partially successful conversion")
209`,
210 },
211 {
212 desc: "Ifdef",
213 mkname: "product.mk",
214 in: `
215ifdef PRODUCT_NAME
216 PRODUCT_NAME = gizmo
217else
218endif
219`,
220 expected: `load("//build/make/core:product_config.rbc", "rblf")
221
222def init(g, handle):
223 cfg = rblf.cfg(handle)
224 if g.get("PRODUCT_NAME") != None:
225 cfg["PRODUCT_NAME"] = "gizmo"
226 else:
227 pass
228`,
229 },
230 {
231 desc: "Simple functions",
232 mkname: "product.mk",
233 in: `
234$(warning this is the warning)
235$(warning)
236$(info this is the info)
237$(error this is the error)
238PRODUCT_NAME:=$(shell echo *)
239`,
240 expected: `load("//build/make/core:product_config.rbc", "rblf")
241
242def init(g, handle):
243 cfg = rblf.cfg(handle)
244 rblf.mkwarning("product.mk", "this is the warning")
245 rblf.mkwarning("product.mk", "")
246 rblf.mkinfo("product.mk", "this is the info")
247 rblf.mkerror("product.mk", "this is the error")
248 cfg["PRODUCT_NAME"] = rblf.shell("echo *")
249`,
250 },
251 {
252 desc: "Empty if",
253 mkname: "product.mk",
254 in: `
255ifdef PRODUCT_NAME
256# Comment
257endif
258`,
259 expected: `load("//build/make/core:product_config.rbc", "rblf")
260
261def init(g, handle):
262 cfg = rblf.cfg(handle)
263 if g.get("PRODUCT_NAME") != None:
264 # Comment
265 pass
266`,
267 },
268 {
269 desc: "if/else/endif",
270 mkname: "product.mk",
271 in: `
272ifndef PRODUCT_NAME
273 PRODUCT_NAME=gizmo1
274else
275 PRODUCT_NAME=gizmo2
276endif
277`,
278 expected: `load("//build/make/core:product_config.rbc", "rblf")
279
280def init(g, handle):
281 cfg = rblf.cfg(handle)
282 if not g.get("PRODUCT_NAME") != None:
283 cfg["PRODUCT_NAME"] = "gizmo1"
284 else:
285 cfg["PRODUCT_NAME"] = "gizmo2"
286`,
287 },
288 {
289 desc: "else if",
290 mkname: "product.mk",
291 in: `
292ifdef PRODUCT_NAME
293 PRODUCT_NAME = gizmo
294else ifndef PRODUCT_PACKAGES # Comment
295endif
296 `,
297 expected: `load("//build/make/core:product_config.rbc", "rblf")
298
299def init(g, handle):
300 cfg = rblf.cfg(handle)
301 if g.get("PRODUCT_NAME") != None:
302 cfg["PRODUCT_NAME"] = "gizmo"
303 elif not g.get("PRODUCT_PACKAGES") != None:
304 # Comment
305 pass
306`,
307 },
308 {
309 desc: "ifeq / ifneq",
310 mkname: "product.mk",
311 in: `
312ifeq (aosp_arm, $(TARGET_PRODUCT))
313 PRODUCT_MODEL = pix2
314else
315 PRODUCT_MODEL = pix21
316endif
317ifneq (aosp_x86, $(TARGET_PRODUCT))
318 PRODUCT_MODEL = pix3
319endif
320`,
321 expected: `load("//build/make/core:product_config.rbc", "rblf")
322
323def init(g, handle):
324 cfg = rblf.cfg(handle)
325 if "aosp_arm" == g["TARGET_PRODUCT"]:
326 cfg["PRODUCT_MODEL"] = "pix2"
327 else:
328 cfg["PRODUCT_MODEL"] = "pix21"
329 if "aosp_x86" != g["TARGET_PRODUCT"]:
330 cfg["PRODUCT_MODEL"] = "pix3"
331`,
332 },
333 {
334 desc: "Check filter result",
335 mkname: "product.mk",
336 in: `
337ifeq (,$(filter userdebug eng, $(TARGET_BUILD_VARIANT)))
338endif
339ifneq (,$(filter userdebug,$(TARGET_BUILD_VARIANT))
340endif
341ifneq (,$(filter plaf,$(PLATFORM_LIST)))
342endif
343ifeq ($(TARGET_BUILD_VARIANT), $(filter $(TARGET_BUILD_VARIANT), userdebug eng))
344endif
Sasha Smundak0554d762021-07-08 18:26:12 -0700345ifneq (,$(filter true, $(v1)$(v2)))
346endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800347`,
348 expected: `load("//build/make/core:product_config.rbc", "rblf")
349
350def init(g, handle):
351 cfg = rblf.cfg(handle)
352 if g["TARGET_BUILD_VARIANT"] not in ["userdebug", "eng"]:
353 pass
Sasha Smundak0554d762021-07-08 18:26:12 -0700354 if g["TARGET_BUILD_VARIANT"] == "userdebug":
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800355 pass
356 if "plaf" in g.get("PLATFORM_LIST", []):
357 pass
358 if g["TARGET_BUILD_VARIANT"] in ["userdebug", "eng"]:
359 pass
Sasha Smundak0554d762021-07-08 18:26:12 -0700360 if "%s%s" % (_v1, _v2) == "true":
361 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800362`,
363 },
364 {
365 desc: "Get filter result",
366 mkname: "product.mk",
367 in: `
368PRODUCT_LIST2=$(filter-out %/foo.ko,$(wildcard path/*.ko))
369`,
370 expected: `load("//build/make/core:product_config.rbc", "rblf")
371
372def init(g, handle):
373 cfg = rblf.cfg(handle)
374 cfg["PRODUCT_LIST2"] = rblf.filter_out("%/foo.ko", rblf.expand_wildcard("path/*.ko"))
375`,
376 },
377 {
378 desc: "filter $(VAR), values",
379 mkname: "product.mk",
380 in: `
381ifeq (,$(filter $(TARGET_PRODUCT), yukawa_gms yukawa_sei510_gms)
382 ifneq (,$(filter $(TARGET_PRODUCT), yukawa_gms)
383 endif
384endif
385
386`,
387 expected: `load("//build/make/core:product_config.rbc", "rblf")
388
389def init(g, handle):
390 cfg = rblf.cfg(handle)
391 if g["TARGET_PRODUCT"] not in ["yukawa_gms", "yukawa_sei510_gms"]:
Sasha Smundak0554d762021-07-08 18:26:12 -0700392 if g["TARGET_PRODUCT"] == "yukawa_gms":
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800393 pass
394`,
395 },
396 {
Sasha Smundak0554d762021-07-08 18:26:12 -0700397 desc: "filter $(V1), $(V2)",
398 mkname: "product.mk",
399 in: `
400ifneq (, $(filter $(PRODUCT_LIST), $(TARGET_PRODUCT)))
401endif
402`,
403 expected: `load("//build/make/core:product_config.rbc", "rblf")
404
405def init(g, handle):
406 cfg = rblf.cfg(handle)
407 if rblf.filter(g.get("PRODUCT_LIST", ""), g["TARGET_PRODUCT"]):
408 pass
409`,
410 },
411 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800412 desc: "ifeq",
413 mkname: "product.mk",
414 in: `
415ifeq (aosp, $(TARGET_PRODUCT)) # Comment
416else ifneq (, $(TARGET_PRODUCT))
417endif
418`,
419 expected: `load("//build/make/core:product_config.rbc", "rblf")
420
421def init(g, handle):
422 cfg = rblf.cfg(handle)
423 if "aosp" == g["TARGET_PRODUCT"]:
424 # Comment
425 pass
426 elif g["TARGET_PRODUCT"]:
427 pass
428`,
429 },
430 {
431 desc: "Nested if",
432 mkname: "product.mk",
433 in: `
434ifdef PRODUCT_NAME
435 PRODUCT_PACKAGES = pack-if0
436 ifdef PRODUCT_MODEL
437 PRODUCT_PACKAGES = pack-if-if
438 else ifdef PRODUCT_NAME
439 PRODUCT_PACKAGES = pack-if-elif
440 else
441 PRODUCT_PACKAGES = pack-if-else
442 endif
443 PRODUCT_PACKAGES = pack-if
444else ifneq (,$(TARGET_PRODUCT))
445 PRODUCT_PACKAGES = pack-elif
446else
447 PRODUCT_PACKAGES = pack-else
448endif
449`,
450 expected: `load("//build/make/core:product_config.rbc", "rblf")
451
452def init(g, handle):
453 cfg = rblf.cfg(handle)
454 if g.get("PRODUCT_NAME") != None:
455 cfg["PRODUCT_PACKAGES"] = ["pack-if0"]
456 if g.get("PRODUCT_MODEL") != None:
457 cfg["PRODUCT_PACKAGES"] = ["pack-if-if"]
458 elif g.get("PRODUCT_NAME") != None:
459 cfg["PRODUCT_PACKAGES"] = ["pack-if-elif"]
460 else:
461 cfg["PRODUCT_PACKAGES"] = ["pack-if-else"]
462 cfg["PRODUCT_PACKAGES"] = ["pack-if"]
463 elif g["TARGET_PRODUCT"]:
464 cfg["PRODUCT_PACKAGES"] = ["pack-elif"]
465 else:
466 cfg["PRODUCT_PACKAGES"] = ["pack-else"]
467`,
468 },
469 {
470 desc: "Wildcard",
471 mkname: "product.mk",
472 in: `
473ifeq (,$(wildcard foo.mk))
474endif
475ifneq (,$(wildcard foo*.mk))
476endif
477`,
478 expected: `load("//build/make/core:product_config.rbc", "rblf")
479
480def init(g, handle):
481 cfg = rblf.cfg(handle)
482 if not rblf.file_exists("foo.mk"):
483 pass
484 if rblf.file_wildcard_exists("foo*.mk"):
485 pass
486`,
487 },
488 {
489 desc: "ifneq $(X),true",
490 mkname: "product.mk",
491 in: `
492ifneq ($(VARIABLE),true)
493endif
494`,
495 expected: `load("//build/make/core:product_config.rbc", "rblf")
496
497def init(g, handle):
498 cfg = rblf.cfg(handle)
499 if g.get("VARIABLE", "") != "true":
500 pass
501`,
502 },
503 {
504 desc: "Const neq",
505 mkname: "product.mk",
506 in: `
507ifneq (1,0)
508endif
509`,
510 expected: `load("//build/make/core:product_config.rbc", "rblf")
511
512def init(g, handle):
513 cfg = rblf.cfg(handle)
514 if "1" != "0":
515 pass
516`,
517 },
518 {
519 desc: "is-board calls",
520 mkname: "product.mk",
521 in: `
522ifeq ($(call is-board-platform-in-list,msm8998), true)
523else ifneq ($(call is-board-platform,copper),true)
524else ifneq ($(call is-vendor-board-platform,QCOM),true)
525else ifeq ($(call is-product-in-list, $(PLATFORM_LIST)), true)
526endif
527`,
528 expected: `load("//build/make/core:product_config.rbc", "rblf")
529
530def init(g, handle):
531 cfg = rblf.cfg(handle)
532 if g.get("TARGET_BOARD_PLATFORM", "") in ["msm8998"]:
533 pass
534 elif g.get("TARGET_BOARD_PLATFORM", "") != "copper":
535 pass
536 elif g.get("TARGET_BOARD_PLATFORM", "") not in g["QCOM_BOARD_PLATFORMS"]:
537 pass
538 elif g["TARGET_PRODUCT"] in g.get("PLATFORM_LIST", []):
539 pass
540`,
541 },
542 {
543 desc: "findstring call",
544 mkname: "product.mk",
545 in: `
546ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),)
547endif
548`,
549 expected: `load("//build/make/core:product_config.rbc", "rblf")
550
551def init(g, handle):
552 cfg = rblf.cfg(handle)
553 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
554 pass
555`,
556 },
557 {
558 desc: "rhs call",
559 mkname: "product.mk",
560 in: `
561PRODUCT_COPY_FILES = $(call add-to-product-copy-files-if-exists, path:distpath) \
562 $(call find-copy-subdir-files, *, fromdir, todir) $(wildcard foo.*)
563`,
564 expected: `load("//build/make/core:product_config.rbc", "rblf")
565
566def init(g, handle):
567 cfg = rblf.cfg(handle)
568 cfg["PRODUCT_COPY_FILES"] = (rblf.copy_if_exists("path:distpath") +
569 rblf.find_and_copy("*", "fromdir", "todir") +
570 rblf.expand_wildcard("foo.*"))
571`,
572 },
573 {
574 desc: "inferred type",
575 mkname: "product.mk",
576 in: `
577HIKEY_MODS := $(wildcard foo/*.ko)
578BOARD_VENDOR_KERNEL_MODULES += $(HIKEY_MODS)
579`,
580 expected: `load("//build/make/core:product_config.rbc", "rblf")
581
582def init(g, handle):
583 cfg = rblf.cfg(handle)
584 g["HIKEY_MODS"] = rblf.expand_wildcard("foo/*.ko")
585 g.setdefault("BOARD_VENDOR_KERNEL_MODULES", [])
586 g["BOARD_VENDOR_KERNEL_MODULES"] += g["HIKEY_MODS"]
587`,
588 },
589 {
590 desc: "list with vars",
591 mkname: "product.mk",
592 in: `
593PRODUCT_COPY_FILES += path1:$(TARGET_PRODUCT)/path1 $(PRODUCT_MODEL)/path2:$(TARGET_PRODUCT)/path2
594`,
595 expected: `load("//build/make/core:product_config.rbc", "rblf")
596
597def init(g, handle):
598 cfg = rblf.cfg(handle)
599 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
600 cfg["PRODUCT_COPY_FILES"] += (("path1:%s/path1" % g["TARGET_PRODUCT"]).split() +
601 ("%s/path2:%s/path2" % (cfg.get("PRODUCT_MODEL", ""), g["TARGET_PRODUCT"])).split())
602`,
603 },
604 {
605 desc: "misc calls",
606 mkname: "product.mk",
607 in: `
608$(call enforce-product-packages-exist,)
609$(call enforce-product-packages-exist, foo)
610$(call require-artifacts-in-path, foo, bar)
611$(call require-artifacts-in-path-relaxed, foo, bar)
612`,
613 expected: `load("//build/make/core:product_config.rbc", "rblf")
614
615def init(g, handle):
616 cfg = rblf.cfg(handle)
617 rblf.enforce_product_packages_exist("")
618 rblf.enforce_product_packages_exist("foo")
619 rblf.require_artifacts_in_path("foo", "bar")
620 rblf.require_artifacts_in_path_relaxed("foo", "bar")
621`,
622 },
623 {
624 desc: "list with functions",
625 mkname: "product.mk",
626 in: `
627PRODUCT_COPY_FILES := $(call find-copy-subdir-files,*.kl,from1,to1) \
628 $(call find-copy-subdir-files,*.kc,from2,to2) \
629 foo bar
630`,
631 expected: `load("//build/make/core:product_config.rbc", "rblf")
632
633def init(g, handle):
634 cfg = rblf.cfg(handle)
635 cfg["PRODUCT_COPY_FILES"] = (rblf.find_and_copy("*.kl", "from1", "to1") +
636 rblf.find_and_copy("*.kc", "from2", "to2") +
637 [
638 "foo",
639 "bar",
640 ])
641`,
642 },
643 {
644 desc: "Text functions",
645 mkname: "product.mk",
646 in: `
647PRODUCT_COPY_FILES := $(addprefix pfx-,a b c)
648PRODUCT_COPY_FILES := $(addsuffix .sff, a b c)
649PRODUCT_NAME := $(word 1, $(subst ., ,$(TARGET_BOARD_PLATFORM)))
Sasha Smundak94b41c72021-07-12 18:30:42 -0700650$(info $(patsubst %.pub,%,$(PRODUCT_ADB_KEYS)))
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800651
652`,
653 expected: `load("//build/make/core:product_config.rbc", "rblf")
654
655def init(g, handle):
656 cfg = rblf.cfg(handle)
657 cfg["PRODUCT_COPY_FILES"] = rblf.addprefix("pfx-", "a b c")
658 cfg["PRODUCT_COPY_FILES"] = rblf.addsuffix(".sff", "a b c")
659 cfg["PRODUCT_NAME"] = ((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " ")).split()[0]
Sasha Smundak94b41c72021-07-12 18:30:42 -0700660 rblf.mkinfo("product.mk", rblf.mkpatsubst("%.pub", "%", g.get("PRODUCT_ADB_KEYS", "")))
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800661`,
662 },
663 {
Sasha Smundak9d011ab2021-07-09 16:00:57 -0700664 desc: "subst in list",
665 mkname: "product.mk",
666 in: `
667files = $(call find-copy-subdir-files,*,from,to)
668PRODUCT_COPY_FILES += $(subst foo,bar,$(files))
669`,
670 expected: `load("//build/make/core:product_config.rbc", "rblf")
671
672def init(g, handle):
673 cfg = rblf.cfg(handle)
674 _files = rblf.find_and_copy("*", "from", "to")
675 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
676 cfg["PRODUCT_COPY_FILES"] += rblf.mksubst("foo", "bar", _files)
677`,
678 },
679 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800680 desc: "assignment flavors",
681 mkname: "product.mk",
682 in: `
683PRODUCT_LIST1 := a
684PRODUCT_LIST2 += a
685PRODUCT_LIST1 += b
686PRODUCT_LIST2 += b
687PRODUCT_LIST3 ?= a
688PRODUCT_LIST1 = c
689PLATFORM_LIST += x
690PRODUCT_PACKAGES := $(PLATFORM_LIST)
691`,
692 expected: `load("//build/make/core:product_config.rbc", "rblf")
693
694def init(g, handle):
695 cfg = rblf.cfg(handle)
696 cfg["PRODUCT_LIST1"] = ["a"]
697 rblf.setdefault(handle, "PRODUCT_LIST2")
698 cfg["PRODUCT_LIST2"] += ["a"]
699 cfg["PRODUCT_LIST1"] += ["b"]
700 cfg["PRODUCT_LIST2"] += ["b"]
701 if cfg.get("PRODUCT_LIST3") == None:
702 cfg["PRODUCT_LIST3"] = ["a"]
703 cfg["PRODUCT_LIST1"] = ["c"]
704 g.setdefault("PLATFORM_LIST", [])
705 g["PLATFORM_LIST"] += ["x"]
706 cfg["PRODUCT_PACKAGES"] = g["PLATFORM_LIST"][:]
707`,
708 },
709 {
710 desc: "assigment flavors2",
711 mkname: "product.mk",
712 in: `
713PRODUCT_LIST1 = a
714ifeq (0,1)
715 PRODUCT_LIST1 += b
716 PRODUCT_LIST2 += b
717endif
718PRODUCT_LIST1 += c
719PRODUCT_LIST2 += c
720`,
721 expected: `load("//build/make/core:product_config.rbc", "rblf")
722
723def init(g, handle):
724 cfg = rblf.cfg(handle)
725 cfg["PRODUCT_LIST1"] = ["a"]
726 if "0" == "1":
727 cfg["PRODUCT_LIST1"] += ["b"]
728 rblf.setdefault(handle, "PRODUCT_LIST2")
729 cfg["PRODUCT_LIST2"] += ["b"]
730 cfg["PRODUCT_LIST1"] += ["c"]
731 rblf.setdefault(handle, "PRODUCT_LIST2")
732 cfg["PRODUCT_LIST2"] += ["c"]
733`,
734 },
735 {
736 desc: "string split",
737 mkname: "product.mk",
738 in: `
739PRODUCT_LIST1 = a
740local = b
741local += c
742FOO = d
743FOO += e
744PRODUCT_LIST1 += $(local)
745PRODUCT_LIST1 += $(FOO)
746`,
747 expected: `load("//build/make/core:product_config.rbc", "rblf")
748
749def init(g, handle):
750 cfg = rblf.cfg(handle)
751 cfg["PRODUCT_LIST1"] = ["a"]
752 _local = "b"
753 _local += " " + "c"
754 g["FOO"] = "d"
755 g["FOO"] += " " + "e"
756 cfg["PRODUCT_LIST1"] += (_local).split()
757 cfg["PRODUCT_LIST1"] += (g["FOO"]).split()
758`,
759 },
760 {
761 desc: "apex_jars",
762 mkname: "product.mk",
763 in: `
764PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex
765`,
766 expected: `load("//build/make/core:product_config.rbc", "rblf")
767
768def init(g, handle):
769 cfg = rblf.cfg(handle)
770 cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) +
771 ["framework-minus-apex"])
772`,
773 },
774 {
775 desc: "strip function",
776 mkname: "product.mk",
777 in: `
778ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),)
779 PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress)
780endif
781`,
782 expected: `load("//build/make/core:product_config.rbc", "rblf")
783
784def init(g, handle):
785 cfg = rblf.cfg(handle)
786 if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []):
787 cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split()
788`,
789 },
790 {
791 desc: "strip func in condition",
792 mkname: "product.mk",
793 in: `
794ifneq ($(strip $(TARGET_VENDOR)),)
795endif
796`,
797 expected: `load("//build/make/core:product_config.rbc", "rblf")
798
799def init(g, handle):
800 cfg = rblf.cfg(handle)
Sasha Smundak0554d762021-07-08 18:26:12 -0700801 if rblf.mkstrip(g.get("TARGET_VENDOR", "")):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800802 pass
803`,
804 },
805 {
806 desc: "ref after set",
807 mkname: "product.mk",
808 in: `
809PRODUCT_ADB_KEYS:=value
810FOO := $(PRODUCT_ADB_KEYS)
811ifneq (,$(PRODUCT_ADB_KEYS))
812endif
813`,
814 expected: `load("//build/make/core:product_config.rbc", "rblf")
815
816def init(g, handle):
817 cfg = rblf.cfg(handle)
818 g["PRODUCT_ADB_KEYS"] = "value"
819 g["FOO"] = g["PRODUCT_ADB_KEYS"]
820 if g["PRODUCT_ADB_KEYS"]:
821 pass
822`,
823 },
824 {
825 desc: "ref before set",
826 mkname: "product.mk",
827 in: `
828V1 := $(PRODUCT_ADB_KEYS)
829ifeq (,$(PRODUCT_ADB_KEYS))
830 V2 := $(PRODUCT_ADB_KEYS)
831 PRODUCT_ADB_KEYS:=foo
832 V3 := $(PRODUCT_ADB_KEYS)
833endif`,
834 expected: `load("//build/make/core:product_config.rbc", "rblf")
835
836def init(g, handle):
837 cfg = rblf.cfg(handle)
838 g["V1"] = g.get("PRODUCT_ADB_KEYS", "")
839 if not g.get("PRODUCT_ADB_KEYS", ""):
840 g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
841 g["PRODUCT_ADB_KEYS"] = "foo"
842 g["V3"] = g["PRODUCT_ADB_KEYS"]
843`,
844 },
845}
846
847var known_variables = []struct {
848 name string
849 class varClass
850 starlarkType
851}{
852 {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
853 {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
854 {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
855 {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
856 {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
857 {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
858 {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
859 {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
860 {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
861 {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
862 {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
863 {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
864 {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
865 {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
866}
867
868func TestGood(t *testing.T) {
869 for _, v := range known_variables {
870 KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
871 }
872 for _, test := range testCases {
873 t.Run(test.desc,
874 func(t *testing.T) {
875 ss, err := Convert(Request{
876 MkFile: test.mkname,
877 Reader: bytes.NewBufferString(test.in),
878 RootDir: ".",
879 OutputSuffix: ".star",
880 WarnPartialSuccess: true,
881 })
882 if err != nil {
883 t.Error(err)
884 return
885 }
886 got := ss.String()
887 if got != test.expected {
888 t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
889 strings.ReplaceAll(test.expected, "\n", "␤\n"),
890 strings.ReplaceAll(got, "\n", "␤\n"))
891 }
892 })
893 }
894}