blob: 46212ee8b939447d4a188f0273107ecd72020faa [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
Sasha Smundak02183cf2021-08-16 13:36:11 -0700262 TARGET_COPY_OUT_RECOVERY := 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:
Sasha Smundak02183cf2021-08-16 13:36:11 -0700273 # MK2RBC TRANSLATION ERROR: cannot set predefined variable TARGET_COPY_OUT_RECOVERY to "foo", its value should be "recovery"
Sasha Smundak6609ba72021-07-22 18:32:56 -0700274 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 Smundak16e07732021-07-23 11:38:23 -0700661$(info $(dir foo/bar))
662$(info $(firstword $(PRODUCT_COPY_FILES)))
663$(info $(lastword $(PRODUCT_COPY_FILES)))
664$(info $(dir $(lastword $(MAKEFILE_LIST))))
665$(info $(dir $(lastword $(PRODUCT_COPY_FILES))))
666$(info $(dir $(lastword $(foobar))))
667$(info $(abspath foo/bar))
668$(info $(notdir foo/bar))
Sasha Smundak3deb9682021-07-26 18:42:25 -0700669$(call add_soong_config_namespace,snsconfig)
670$(call add_soong_config_var_value,snsconfig,imagetype,odm_image)
671PRODUCT_COPY_FILES := $(call copy-files,$(wildcard foo*.mk),etc)
Sasha Smundak04453082021-08-17 18:14:41 -0700672PRODUCT_COPY_FILES := $(call product-copy-files-by-pattern,from/%,to/%,a b c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800673`,
674 expected: `load("//build/make/core:product_config.rbc", "rblf")
675
676def init(g, handle):
677 cfg = rblf.cfg(handle)
678 cfg["PRODUCT_COPY_FILES"] = rblf.addprefix("pfx-", "a b c")
679 cfg["PRODUCT_COPY_FILES"] = rblf.addsuffix(".sff", "a b c")
680 cfg["PRODUCT_NAME"] = ((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " ")).split()[0]
Sasha Smundak94b41c72021-07-12 18:30:42 -0700681 rblf.mkinfo("product.mk", rblf.mkpatsubst("%.pub", "%", g.get("PRODUCT_ADB_KEYS", "")))
Sasha Smundak16e07732021-07-23 11:38:23 -0700682 rblf.mkinfo("product.mk", rblf.dir("foo/bar"))
683 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][0])
684 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][-1])
685 rblf.mkinfo("product.mk", rblf.dir("product.mk"))
686 rblf.mkinfo("product.mk", rblf.dir(cfg["PRODUCT_COPY_FILES"][-1]))
687 rblf.mkinfo("product.mk", rblf.dir((_foobar).split()[-1]))
688 rblf.mkinfo("product.mk", rblf.abspath("foo/bar"))
689 rblf.mkinfo("product.mk", rblf.notdir("foo/bar"))
Sasha Smundak3deb9682021-07-26 18:42:25 -0700690 rblf.add_soong_config_namespace(g, "snsconfig")
691 rblf.add_soong_config_var_value(g, "snsconfig", "imagetype", "odm_image")
692 cfg["PRODUCT_COPY_FILES"] = rblf.copy_files(rblf.expand_wildcard("foo*.mk"), "etc")
Sasha Smundak04453082021-08-17 18:14:41 -0700693 cfg["PRODUCT_COPY_FILES"] = rblf.product_copy_files_by_pattern("from/%", "to/%", "a b c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800694`,
695 },
696 {
Sasha Smundak9d011ab2021-07-09 16:00:57 -0700697 desc: "subst in list",
698 mkname: "product.mk",
699 in: `
700files = $(call find-copy-subdir-files,*,from,to)
701PRODUCT_COPY_FILES += $(subst foo,bar,$(files))
702`,
703 expected: `load("//build/make/core:product_config.rbc", "rblf")
704
705def init(g, handle):
706 cfg = rblf.cfg(handle)
707 _files = rblf.find_and_copy("*", "from", "to")
708 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
709 cfg["PRODUCT_COPY_FILES"] += rblf.mksubst("foo", "bar", _files)
710`,
711 },
712 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800713 desc: "assignment flavors",
714 mkname: "product.mk",
715 in: `
716PRODUCT_LIST1 := a
717PRODUCT_LIST2 += a
718PRODUCT_LIST1 += b
719PRODUCT_LIST2 += b
720PRODUCT_LIST3 ?= a
721PRODUCT_LIST1 = c
722PLATFORM_LIST += x
723PRODUCT_PACKAGES := $(PLATFORM_LIST)
724`,
725 expected: `load("//build/make/core:product_config.rbc", "rblf")
726
727def init(g, handle):
728 cfg = rblf.cfg(handle)
729 cfg["PRODUCT_LIST1"] = ["a"]
730 rblf.setdefault(handle, "PRODUCT_LIST2")
731 cfg["PRODUCT_LIST2"] += ["a"]
732 cfg["PRODUCT_LIST1"] += ["b"]
733 cfg["PRODUCT_LIST2"] += ["b"]
734 if cfg.get("PRODUCT_LIST3") == None:
735 cfg["PRODUCT_LIST3"] = ["a"]
736 cfg["PRODUCT_LIST1"] = ["c"]
737 g.setdefault("PLATFORM_LIST", [])
738 g["PLATFORM_LIST"] += ["x"]
739 cfg["PRODUCT_PACKAGES"] = g["PLATFORM_LIST"][:]
740`,
741 },
742 {
743 desc: "assigment flavors2",
744 mkname: "product.mk",
745 in: `
746PRODUCT_LIST1 = a
747ifeq (0,1)
748 PRODUCT_LIST1 += b
749 PRODUCT_LIST2 += b
750endif
751PRODUCT_LIST1 += c
752PRODUCT_LIST2 += c
753`,
754 expected: `load("//build/make/core:product_config.rbc", "rblf")
755
756def init(g, handle):
757 cfg = rblf.cfg(handle)
758 cfg["PRODUCT_LIST1"] = ["a"]
759 if "0" == "1":
760 cfg["PRODUCT_LIST1"] += ["b"]
761 rblf.setdefault(handle, "PRODUCT_LIST2")
762 cfg["PRODUCT_LIST2"] += ["b"]
763 cfg["PRODUCT_LIST1"] += ["c"]
764 rblf.setdefault(handle, "PRODUCT_LIST2")
765 cfg["PRODUCT_LIST2"] += ["c"]
766`,
767 },
768 {
Sasha Smundak3deb9682021-07-26 18:42:25 -0700769 desc: "soong namespace assignments",
770 mkname: "product.mk",
771 in: `
772SOONG_CONFIG_NAMESPACES += cvd
773SOONG_CONFIG_cvd += launch_configs
774SOONG_CONFIG_cvd_launch_configs += cvd_config_auto.json
775SOONG_CONFIG_cvd += grub_config
776SOONG_CONFIG_cvd_grub_config += grub.cfg
777`,
778 expected: `load("//build/make/core:product_config.rbc", "rblf")
779
780def init(g, handle):
781 cfg = rblf.cfg(handle)
782 rblf.add_soong_config_namespace(g, "cvd")
783 rblf.add_soong_config_var_value(g, "cvd", "launch_configs", "cvd_config_auto.json")
784 rblf.add_soong_config_var_value(g, "cvd", "grub_config", "grub.cfg")
785`,
786 },
787 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800788 desc: "string split",
789 mkname: "product.mk",
790 in: `
791PRODUCT_LIST1 = a
792local = b
793local += c
794FOO = d
795FOO += e
796PRODUCT_LIST1 += $(local)
797PRODUCT_LIST1 += $(FOO)
798`,
799 expected: `load("//build/make/core:product_config.rbc", "rblf")
800
801def init(g, handle):
802 cfg = rblf.cfg(handle)
803 cfg["PRODUCT_LIST1"] = ["a"]
804 _local = "b"
805 _local += " " + "c"
806 g["FOO"] = "d"
807 g["FOO"] += " " + "e"
808 cfg["PRODUCT_LIST1"] += (_local).split()
809 cfg["PRODUCT_LIST1"] += (g["FOO"]).split()
810`,
811 },
812 {
813 desc: "apex_jars",
814 mkname: "product.mk",
815 in: `
816PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex
817`,
818 expected: `load("//build/make/core:product_config.rbc", "rblf")
819
820def init(g, handle):
821 cfg = rblf.cfg(handle)
822 cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) +
823 ["framework-minus-apex"])
824`,
825 },
826 {
827 desc: "strip function",
828 mkname: "product.mk",
829 in: `
830ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),)
831 PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress)
832endif
833`,
834 expected: `load("//build/make/core:product_config.rbc", "rblf")
835
836def init(g, handle):
837 cfg = rblf.cfg(handle)
838 if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []):
839 cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split()
840`,
841 },
842 {
843 desc: "strip func in condition",
844 mkname: "product.mk",
845 in: `
846ifneq ($(strip $(TARGET_VENDOR)),)
847endif
848`,
849 expected: `load("//build/make/core:product_config.rbc", "rblf")
850
851def init(g, handle):
852 cfg = rblf.cfg(handle)
Sasha Smundak0554d762021-07-08 18:26:12 -0700853 if rblf.mkstrip(g.get("TARGET_VENDOR", "")):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800854 pass
855`,
856 },
857 {
858 desc: "ref after set",
859 mkname: "product.mk",
860 in: `
861PRODUCT_ADB_KEYS:=value
862FOO := $(PRODUCT_ADB_KEYS)
863ifneq (,$(PRODUCT_ADB_KEYS))
864endif
865`,
866 expected: `load("//build/make/core:product_config.rbc", "rblf")
867
868def init(g, handle):
869 cfg = rblf.cfg(handle)
870 g["PRODUCT_ADB_KEYS"] = "value"
871 g["FOO"] = g["PRODUCT_ADB_KEYS"]
872 if g["PRODUCT_ADB_KEYS"]:
873 pass
874`,
875 },
876 {
877 desc: "ref before set",
878 mkname: "product.mk",
879 in: `
880V1 := $(PRODUCT_ADB_KEYS)
881ifeq (,$(PRODUCT_ADB_KEYS))
882 V2 := $(PRODUCT_ADB_KEYS)
883 PRODUCT_ADB_KEYS:=foo
884 V3 := $(PRODUCT_ADB_KEYS)
885endif`,
886 expected: `load("//build/make/core:product_config.rbc", "rblf")
887
888def init(g, handle):
889 cfg = rblf.cfg(handle)
890 g["V1"] = g.get("PRODUCT_ADB_KEYS", "")
891 if not g.get("PRODUCT_ADB_KEYS", ""):
892 g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
893 g["PRODUCT_ADB_KEYS"] = "foo"
894 g["V3"] = g["PRODUCT_ADB_KEYS"]
895`,
896 },
Sasha Smundak6609ba72021-07-22 18:32:56 -0700897 {
898 desc: "Dynamic inherit path",
899 mkname: "product.mk",
900 in: `
901MY_PATH=foo
902$(call inherit-product,vendor/$(MY_PATH)/cfg.mk)
903`,
904 expected: `load("//build/make/core:product_config.rbc", "rblf")
905load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
906load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init")
907
908def init(g, handle):
909 cfg = rblf.cfg(handle)
910 g["MY_PATH"] = "foo"
911 _entry = {
912 "vendor/foo1/cfg.mk": ("_cfg", _cfg_init),
913 "vendor/bar/baz/cfg.mk": ("_cfg1", _cfg1_init),
914 }.get("vendor/%s/cfg.mk" % g["MY_PATH"])
915 (_varmod, _varmod_init) = _entry if _entry else (None, None)
916 if not _varmod_init:
917 rblf.mkerror("cannot")
918 rblf.inherit(handle, _varmod, _varmod_init)
919`,
920 },
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800921}
922
923var known_variables = []struct {
924 name string
925 class varClass
926 starlarkType
927}{
928 {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
929 {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
930 {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
931 {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
932 {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
933 {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
934 {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
935 {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
936 {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
937 {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
938 {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
939 {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
940 {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
941 {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
942}
943
Sasha Smundak6609ba72021-07-22 18:32:56 -0700944type testMakefileFinder struct {
945 fs fs.FS
946 root string
947 files []string
948}
949
950func (t *testMakefileFinder) Find(root string) []string {
951 if t.files != nil || root == t.root {
952 return t.files
953 }
954 t.files = make([]string, 0)
955 fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error {
956 if err != nil {
957 return err
958 }
959 if d.IsDir() {
960 base := filepath.Base(path)
961 if base[0] == '.' && len(base) > 1 {
962 return fs.SkipDir
963 }
964 return nil
965 }
966 if strings.HasSuffix(path, ".mk") {
967 t.files = append(t.files, path)
968 }
969 return nil
970 })
971 return t.files
972}
973
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800974func TestGood(t *testing.T) {
975 for _, v := range known_variables {
976 KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
977 }
Sasha Smundak6609ba72021-07-22 18:32:56 -0700978 fs := NewFindMockFS([]string{
979 "vendor/foo1/cfg.mk",
980 "vendor/bar/baz/cfg.mk",
981 "part.mk",
982 "foo/font.mk",
983 "bar/font.mk",
984 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800985 for _, test := range testCases {
986 t.Run(test.desc,
987 func(t *testing.T) {
988 ss, err := Convert(Request{
989 MkFile: test.mkname,
990 Reader: bytes.NewBufferString(test.in),
991 RootDir: ".",
992 OutputSuffix: ".star",
993 WarnPartialSuccess: true,
Sasha Smundak6609ba72021-07-22 18:32:56 -0700994 SourceFS: fs,
995 MakefileFinder: &testMakefileFinder{fs: fs},
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800996 })
997 if err != nil {
998 t.Error(err)
999 return
1000 }
1001 got := ss.String()
1002 if got != test.expected {
1003 t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
1004 strings.ReplaceAll(test.expected, "\n", "␤\n"),
1005 strings.ReplaceAll(got, "\n", "␤\n"))
1006 }
1007 })
1008 }
1009}