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