blob: fe1fa08134c688e425c126ef04dc44eb297e1446 [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: `
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800123$(call inherit-product, part.mk)
Sasha Smundak868c5e32021-09-23 16:20:58 -0700124ifdef PRODUCT_NAME
125$(call inherit-product, part1.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800126else # Comment
Sasha Smundak868c5e32021-09-23 16:20:58 -0700127$(call inherit-product, $(LOCAL_PATH)/part1.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800128endif
129`,
130 expected: `load("//build/make/core:product_config.rbc", "rblf")
131load(":part.star", _part_init = "init")
Sasha Smundak868c5e32021-09-23 16:20:58 -0700132load(":part1.star|init", _part1_init = "init")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800133
134def init(g, handle):
135 cfg = rblf.cfg(handle)
Sasha Smundak868c5e32021-09-23 16:20:58 -0700136 rblf.inherit(handle, "part", _part_init)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800137 if g.get("PRODUCT_NAME") != None:
Sasha Smundak868c5e32021-09-23 16:20:58 -0700138 rblf.inherit(handle, "part1", _part1_init)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800139 else:
140 # Comment
Sasha Smundak868c5e32021-09-23 16:20:58 -0700141 rblf.inherit(handle, "part1", _part1_init)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800142`,
143 },
144 {
145 desc: "Inherit configuration if it exists",
146 mkname: "product.mk",
147 in: `
148$(call inherit-product-if-exists, part.mk)
149`,
150 expected: `load("//build/make/core:product_config.rbc", "rblf")
151load(":part.star|init", _part_init = "init")
152
153def init(g, handle):
154 cfg = rblf.cfg(handle)
Sasha Smundak6609ba72021-07-22 18:32:56 -0700155 if _part_init:
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800156 rblf.inherit(handle, "part", _part_init)
157`,
158 },
159
160 {
161 desc: "Include configuration",
162 mkname: "product.mk",
163 in: `
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800164include part.mk
Sasha Smundak868c5e32021-09-23 16:20:58 -0700165ifdef PRODUCT_NAME
166include part1.mk
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800167else
Sasha Smundak868c5e32021-09-23 16:20:58 -0700168-include $(LOCAL_PATH)/part1.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800169endif
170`,
171 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundak868c5e32021-09-23 16:20:58 -0700172load(":part.star", _part_init = "init")
173load(":part1.star|init", _part1_init = "init")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800174
175def init(g, handle):
176 cfg = rblf.cfg(handle)
Sasha Smundak868c5e32021-09-23 16:20:58 -0700177 _part_init(g, handle)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800178 if g.get("PRODUCT_NAME") != None:
Sasha Smundak868c5e32021-09-23 16:20:58 -0700179 _part1_init(g, handle)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800180 else:
Sasha Smundak868c5e32021-09-23 16:20:58 -0700181 if _part1_init != None:
182 _part1_init(g, handle)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800183`,
184 },
185
186 {
187 desc: "Synonymous inherited configurations",
188 mkname: "path/product.mk",
189 in: `
Sasha Smundak6609ba72021-07-22 18:32:56 -0700190$(call inherit-product, */font.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800191`,
192 expected: `load("//build/make/core:product_config.rbc", "rblf")
193load("//foo:font.star", _font_init = "init")
194load("//bar:font.star", _font1_init = "init")
195
196def init(g, handle):
197 cfg = rblf.cfg(handle)
198 rblf.inherit(handle, "foo/font", _font_init)
199 rblf.inherit(handle, "bar/font", _font1_init)
200`,
201 },
202 {
203 desc: "Directive define",
204 mkname: "product.mk",
205 in: `
206define some-macro
207 $(info foo)
208endef
209`,
210 expected: `# MK2RBC TRANSLATION ERROR: define is not supported: some-macro
211# define some-macro
212# $(info foo)
213# endef
214load("//build/make/core:product_config.rbc", "rblf")
215
216def init(g, handle):
217 cfg = rblf.cfg(handle)
218 rblf.warning("product.mk", "partially successful conversion")
219`,
220 },
221 {
222 desc: "Ifdef",
223 mkname: "product.mk",
224 in: `
225ifdef PRODUCT_NAME
226 PRODUCT_NAME = gizmo
227else
228endif
229`,
230 expected: `load("//build/make/core:product_config.rbc", "rblf")
231
232def init(g, handle):
233 cfg = rblf.cfg(handle)
234 if g.get("PRODUCT_NAME") != None:
235 cfg["PRODUCT_NAME"] = "gizmo"
236 else:
237 pass
238`,
239 },
240 {
241 desc: "Simple functions",
242 mkname: "product.mk",
243 in: `
244$(warning this is the warning)
245$(warning)
246$(info this is the info)
247$(error this is the error)
248PRODUCT_NAME:=$(shell echo *)
249`,
250 expected: `load("//build/make/core:product_config.rbc", "rblf")
251
252def init(g, handle):
253 cfg = rblf.cfg(handle)
254 rblf.mkwarning("product.mk", "this is the warning")
255 rblf.mkwarning("product.mk", "")
256 rblf.mkinfo("product.mk", "this is the info")
257 rblf.mkerror("product.mk", "this is the error")
258 cfg["PRODUCT_NAME"] = rblf.shell("echo *")
259`,
260 },
261 {
262 desc: "Empty if",
263 mkname: "product.mk",
264 in: `
265ifdef PRODUCT_NAME
266# Comment
Sasha Smundak6609ba72021-07-22 18:32:56 -0700267else
Sasha Smundak02183cf2021-08-16 13:36:11 -0700268 TARGET_COPY_OUT_RECOVERY := foo
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800269endif
270`,
271 expected: `load("//build/make/core:product_config.rbc", "rblf")
272
273def init(g, handle):
274 cfg = rblf.cfg(handle)
275 if g.get("PRODUCT_NAME") != None:
276 # Comment
277 pass
Sasha Smundak6609ba72021-07-22 18:32:56 -0700278 else:
Sasha Smundak02183cf2021-08-16 13:36:11 -0700279 # 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 -0700280 pass
281 rblf.warning("product.mk", "partially successful conversion")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800282`,
283 },
284 {
285 desc: "if/else/endif",
286 mkname: "product.mk",
287 in: `
288ifndef PRODUCT_NAME
289 PRODUCT_NAME=gizmo1
290else
291 PRODUCT_NAME=gizmo2
292endif
293`,
294 expected: `load("//build/make/core:product_config.rbc", "rblf")
295
296def init(g, handle):
297 cfg = rblf.cfg(handle)
298 if not g.get("PRODUCT_NAME") != None:
299 cfg["PRODUCT_NAME"] = "gizmo1"
300 else:
301 cfg["PRODUCT_NAME"] = "gizmo2"
302`,
303 },
304 {
305 desc: "else if",
306 mkname: "product.mk",
307 in: `
308ifdef PRODUCT_NAME
309 PRODUCT_NAME = gizmo
310else ifndef PRODUCT_PACKAGES # Comment
311endif
312 `,
313 expected: `load("//build/make/core:product_config.rbc", "rblf")
314
315def init(g, handle):
316 cfg = rblf.cfg(handle)
317 if g.get("PRODUCT_NAME") != None:
318 cfg["PRODUCT_NAME"] = "gizmo"
319 elif not g.get("PRODUCT_PACKAGES") != None:
320 # Comment
321 pass
322`,
323 },
324 {
325 desc: "ifeq / ifneq",
326 mkname: "product.mk",
327 in: `
328ifeq (aosp_arm, $(TARGET_PRODUCT))
329 PRODUCT_MODEL = pix2
330else
331 PRODUCT_MODEL = pix21
332endif
333ifneq (aosp_x86, $(TARGET_PRODUCT))
334 PRODUCT_MODEL = pix3
335endif
336`,
337 expected: `load("//build/make/core:product_config.rbc", "rblf")
338
339def init(g, handle):
340 cfg = rblf.cfg(handle)
341 if "aosp_arm" == g["TARGET_PRODUCT"]:
342 cfg["PRODUCT_MODEL"] = "pix2"
343 else:
344 cfg["PRODUCT_MODEL"] = "pix21"
345 if "aosp_x86" != g["TARGET_PRODUCT"]:
346 cfg["PRODUCT_MODEL"] = "pix3"
347`,
348 },
349 {
350 desc: "Check filter result",
351 mkname: "product.mk",
352 in: `
353ifeq (,$(filter userdebug eng, $(TARGET_BUILD_VARIANT)))
354endif
355ifneq (,$(filter userdebug,$(TARGET_BUILD_VARIANT))
356endif
357ifneq (,$(filter plaf,$(PLATFORM_LIST)))
358endif
359ifeq ($(TARGET_BUILD_VARIANT), $(filter $(TARGET_BUILD_VARIANT), userdebug eng))
360endif
Sasha Smundak0554d762021-07-08 18:26:12 -0700361ifneq (,$(filter true, $(v1)$(v2)))
362endif
Sasha Smundak5f463be2021-09-15 18:43:36 -0700363ifeq (,$(filter barbet coral%,$(TARGET_PRODUCT)))
364else ifneq (,$(filter barbet%,$(TARGET_PRODUCT)))
365endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800366`,
367 expected: `load("//build/make/core:product_config.rbc", "rblf")
368
369def init(g, handle):
370 cfg = rblf.cfg(handle)
Sasha Smundak5f463be2021-09-15 18:43:36 -0700371 if not rblf.filter("userdebug eng", g["TARGET_BUILD_VARIANT"]):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800372 pass
Sasha Smundak5f463be2021-09-15 18:43:36 -0700373 if rblf.filter("userdebug", g["TARGET_BUILD_VARIANT"]):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800374 pass
375 if "plaf" in g.get("PLATFORM_LIST", []):
376 pass
377 if g["TARGET_BUILD_VARIANT"] in ["userdebug", "eng"]:
378 pass
Sasha Smundak5f463be2021-09-15 18:43:36 -0700379 if rblf.filter("true", "%s%s" % (_v1, _v2)):
380 pass
381 if not rblf.filter("barbet coral%", g["TARGET_PRODUCT"]):
382 pass
383 elif rblf.filter("barbet%", g["TARGET_PRODUCT"]):
Sasha Smundak0554d762021-07-08 18:26:12 -0700384 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800385`,
386 },
387 {
388 desc: "Get filter result",
389 mkname: "product.mk",
390 in: `
391PRODUCT_LIST2=$(filter-out %/foo.ko,$(wildcard path/*.ko))
392`,
393 expected: `load("//build/make/core:product_config.rbc", "rblf")
394
395def init(g, handle):
396 cfg = rblf.cfg(handle)
397 cfg["PRODUCT_LIST2"] = rblf.filter_out("%/foo.ko", rblf.expand_wildcard("path/*.ko"))
398`,
399 },
400 {
401 desc: "filter $(VAR), values",
402 mkname: "product.mk",
403 in: `
404ifeq (,$(filter $(TARGET_PRODUCT), yukawa_gms yukawa_sei510_gms)
405 ifneq (,$(filter $(TARGET_PRODUCT), yukawa_gms)
406 endif
407endif
408
409`,
410 expected: `load("//build/make/core:product_config.rbc", "rblf")
411
412def init(g, handle):
413 cfg = rblf.cfg(handle)
414 if g["TARGET_PRODUCT"] not in ["yukawa_gms", "yukawa_sei510_gms"]:
Sasha Smundak0554d762021-07-08 18:26:12 -0700415 if g["TARGET_PRODUCT"] == "yukawa_gms":
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800416 pass
417`,
418 },
419 {
Sasha Smundak0554d762021-07-08 18:26:12 -0700420 desc: "filter $(V1), $(V2)",
421 mkname: "product.mk",
422 in: `
423ifneq (, $(filter $(PRODUCT_LIST), $(TARGET_PRODUCT)))
424endif
425`,
426 expected: `load("//build/make/core:product_config.rbc", "rblf")
427
428def init(g, handle):
429 cfg = rblf.cfg(handle)
Sasha Smundak468e11f2021-08-26 09:10:23 -0700430 if rblf.filter(g.get("PRODUCT_LIST", []), g["TARGET_PRODUCT"]):
Sasha Smundak0554d762021-07-08 18:26:12 -0700431 pass
432`,
433 },
434 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800435 desc: "ifeq",
436 mkname: "product.mk",
437 in: `
438ifeq (aosp, $(TARGET_PRODUCT)) # Comment
439else ifneq (, $(TARGET_PRODUCT))
440endif
441`,
442 expected: `load("//build/make/core:product_config.rbc", "rblf")
443
444def init(g, handle):
445 cfg = rblf.cfg(handle)
446 if "aosp" == g["TARGET_PRODUCT"]:
447 # Comment
448 pass
449 elif g["TARGET_PRODUCT"]:
450 pass
451`,
452 },
453 {
454 desc: "Nested if",
455 mkname: "product.mk",
456 in: `
457ifdef PRODUCT_NAME
458 PRODUCT_PACKAGES = pack-if0
459 ifdef PRODUCT_MODEL
460 PRODUCT_PACKAGES = pack-if-if
461 else ifdef PRODUCT_NAME
462 PRODUCT_PACKAGES = pack-if-elif
463 else
464 PRODUCT_PACKAGES = pack-if-else
465 endif
466 PRODUCT_PACKAGES = pack-if
467else ifneq (,$(TARGET_PRODUCT))
468 PRODUCT_PACKAGES = pack-elif
469else
470 PRODUCT_PACKAGES = pack-else
471endif
472`,
473 expected: `load("//build/make/core:product_config.rbc", "rblf")
474
475def init(g, handle):
476 cfg = rblf.cfg(handle)
477 if g.get("PRODUCT_NAME") != None:
478 cfg["PRODUCT_PACKAGES"] = ["pack-if0"]
479 if g.get("PRODUCT_MODEL") != None:
480 cfg["PRODUCT_PACKAGES"] = ["pack-if-if"]
481 elif g.get("PRODUCT_NAME") != None:
482 cfg["PRODUCT_PACKAGES"] = ["pack-if-elif"]
483 else:
484 cfg["PRODUCT_PACKAGES"] = ["pack-if-else"]
485 cfg["PRODUCT_PACKAGES"] = ["pack-if"]
486 elif g["TARGET_PRODUCT"]:
487 cfg["PRODUCT_PACKAGES"] = ["pack-elif"]
488 else:
489 cfg["PRODUCT_PACKAGES"] = ["pack-else"]
490`,
491 },
492 {
493 desc: "Wildcard",
494 mkname: "product.mk",
495 in: `
496ifeq (,$(wildcard foo.mk))
497endif
498ifneq (,$(wildcard foo*.mk))
499endif
500`,
501 expected: `load("//build/make/core:product_config.rbc", "rblf")
502
503def init(g, handle):
504 cfg = rblf.cfg(handle)
505 if not rblf.file_exists("foo.mk"):
506 pass
507 if rblf.file_wildcard_exists("foo*.mk"):
508 pass
509`,
510 },
511 {
512 desc: "ifneq $(X),true",
513 mkname: "product.mk",
514 in: `
515ifneq ($(VARIABLE),true)
516endif
517`,
518 expected: `load("//build/make/core:product_config.rbc", "rblf")
519
520def init(g, handle):
521 cfg = rblf.cfg(handle)
522 if g.get("VARIABLE", "") != "true":
523 pass
524`,
525 },
526 {
527 desc: "Const neq",
528 mkname: "product.mk",
529 in: `
530ifneq (1,0)
531endif
532`,
533 expected: `load("//build/make/core:product_config.rbc", "rblf")
534
535def init(g, handle):
536 cfg = rblf.cfg(handle)
537 if "1" != "0":
538 pass
539`,
540 },
541 {
542 desc: "is-board calls",
543 mkname: "product.mk",
544 in: `
545ifeq ($(call is-board-platform-in-list,msm8998), true)
546else ifneq ($(call is-board-platform,copper),true)
547else ifneq ($(call is-vendor-board-platform,QCOM),true)
548else ifeq ($(call is-product-in-list, $(PLATFORM_LIST)), true)
549endif
550`,
551 expected: `load("//build/make/core:product_config.rbc", "rblf")
552
553def init(g, handle):
554 cfg = rblf.cfg(handle)
555 if g.get("TARGET_BOARD_PLATFORM", "") in ["msm8998"]:
556 pass
557 elif g.get("TARGET_BOARD_PLATFORM", "") != "copper":
558 pass
559 elif g.get("TARGET_BOARD_PLATFORM", "") not in g["QCOM_BOARD_PLATFORMS"]:
560 pass
561 elif g["TARGET_PRODUCT"] in g.get("PLATFORM_LIST", []):
562 pass
563`,
564 },
565 {
566 desc: "findstring call",
567 mkname: "product.mk",
568 in: `
569ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),)
570endif
571`,
572 expected: `load("//build/make/core:product_config.rbc", "rblf")
573
574def init(g, handle):
575 cfg = rblf.cfg(handle)
576 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
577 pass
578`,
579 },
580 {
581 desc: "rhs call",
582 mkname: "product.mk",
583 in: `
584PRODUCT_COPY_FILES = $(call add-to-product-copy-files-if-exists, path:distpath) \
585 $(call find-copy-subdir-files, *, fromdir, todir) $(wildcard foo.*)
586`,
587 expected: `load("//build/make/core:product_config.rbc", "rblf")
588
589def init(g, handle):
590 cfg = rblf.cfg(handle)
591 cfg["PRODUCT_COPY_FILES"] = (rblf.copy_if_exists("path:distpath") +
592 rblf.find_and_copy("*", "fromdir", "todir") +
593 rblf.expand_wildcard("foo.*"))
594`,
595 },
596 {
597 desc: "inferred type",
598 mkname: "product.mk",
599 in: `
600HIKEY_MODS := $(wildcard foo/*.ko)
601BOARD_VENDOR_KERNEL_MODULES += $(HIKEY_MODS)
602`,
603 expected: `load("//build/make/core:product_config.rbc", "rblf")
604
605def init(g, handle):
606 cfg = rblf.cfg(handle)
607 g["HIKEY_MODS"] = rblf.expand_wildcard("foo/*.ko")
608 g.setdefault("BOARD_VENDOR_KERNEL_MODULES", [])
609 g["BOARD_VENDOR_KERNEL_MODULES"] += g["HIKEY_MODS"]
610`,
611 },
612 {
613 desc: "list with vars",
614 mkname: "product.mk",
615 in: `
616PRODUCT_COPY_FILES += path1:$(TARGET_PRODUCT)/path1 $(PRODUCT_MODEL)/path2:$(TARGET_PRODUCT)/path2
617`,
618 expected: `load("//build/make/core:product_config.rbc", "rblf")
619
620def init(g, handle):
621 cfg = rblf.cfg(handle)
622 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
623 cfg["PRODUCT_COPY_FILES"] += (("path1:%s/path1" % g["TARGET_PRODUCT"]).split() +
624 ("%s/path2:%s/path2" % (cfg.get("PRODUCT_MODEL", ""), g["TARGET_PRODUCT"])).split())
625`,
626 },
627 {
628 desc: "misc calls",
629 mkname: "product.mk",
630 in: `
631$(call enforce-product-packages-exist,)
632$(call enforce-product-packages-exist, foo)
633$(call require-artifacts-in-path, foo, bar)
634$(call require-artifacts-in-path-relaxed, foo, bar)
635`,
636 expected: `load("//build/make/core:product_config.rbc", "rblf")
637
638def init(g, handle):
639 cfg = rblf.cfg(handle)
640 rblf.enforce_product_packages_exist("")
641 rblf.enforce_product_packages_exist("foo")
642 rblf.require_artifacts_in_path("foo", "bar")
643 rblf.require_artifacts_in_path_relaxed("foo", "bar")
644`,
645 },
646 {
647 desc: "list with functions",
648 mkname: "product.mk",
649 in: `
650PRODUCT_COPY_FILES := $(call find-copy-subdir-files,*.kl,from1,to1) \
651 $(call find-copy-subdir-files,*.kc,from2,to2) \
652 foo bar
653`,
654 expected: `load("//build/make/core:product_config.rbc", "rblf")
655
656def init(g, handle):
657 cfg = rblf.cfg(handle)
658 cfg["PRODUCT_COPY_FILES"] = (rblf.find_and_copy("*.kl", "from1", "to1") +
659 rblf.find_and_copy("*.kc", "from2", "to2") +
660 [
661 "foo",
662 "bar",
663 ])
664`,
665 },
666 {
667 desc: "Text functions",
668 mkname: "product.mk",
669 in: `
670PRODUCT_COPY_FILES := $(addprefix pfx-,a b c)
671PRODUCT_COPY_FILES := $(addsuffix .sff, a b c)
672PRODUCT_NAME := $(word 1, $(subst ., ,$(TARGET_BOARD_PLATFORM)))
Sasha Smundak94b41c72021-07-12 18:30:42 -0700673$(info $(patsubst %.pub,%,$(PRODUCT_ADB_KEYS)))
Sasha Smundak16e07732021-07-23 11:38:23 -0700674$(info $(dir foo/bar))
675$(info $(firstword $(PRODUCT_COPY_FILES)))
676$(info $(lastword $(PRODUCT_COPY_FILES)))
677$(info $(dir $(lastword $(MAKEFILE_LIST))))
678$(info $(dir $(lastword $(PRODUCT_COPY_FILES))))
679$(info $(dir $(lastword $(foobar))))
680$(info $(abspath foo/bar))
681$(info $(notdir foo/bar))
Sasha Smundak3deb9682021-07-26 18:42:25 -0700682$(call add_soong_config_namespace,snsconfig)
683$(call add_soong_config_var_value,snsconfig,imagetype,odm_image)
684PRODUCT_COPY_FILES := $(call copy-files,$(wildcard foo*.mk),etc)
Sasha Smundak04453082021-08-17 18:14:41 -0700685PRODUCT_COPY_FILES := $(call product-copy-files-by-pattern,from/%,to/%,a b c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800686`,
687 expected: `load("//build/make/core:product_config.rbc", "rblf")
688
689def init(g, handle):
690 cfg = rblf.cfg(handle)
691 cfg["PRODUCT_COPY_FILES"] = rblf.addprefix("pfx-", "a b c")
692 cfg["PRODUCT_COPY_FILES"] = rblf.addsuffix(".sff", "a b c")
693 cfg["PRODUCT_NAME"] = ((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " ")).split()[0]
Sasha Smundak94b41c72021-07-12 18:30:42 -0700694 rblf.mkinfo("product.mk", rblf.mkpatsubst("%.pub", "%", g.get("PRODUCT_ADB_KEYS", "")))
Sasha Smundak16e07732021-07-23 11:38:23 -0700695 rblf.mkinfo("product.mk", rblf.dir("foo/bar"))
696 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][0])
697 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][-1])
698 rblf.mkinfo("product.mk", rblf.dir("product.mk"))
699 rblf.mkinfo("product.mk", rblf.dir(cfg["PRODUCT_COPY_FILES"][-1]))
700 rblf.mkinfo("product.mk", rblf.dir((_foobar).split()[-1]))
701 rblf.mkinfo("product.mk", rblf.abspath("foo/bar"))
702 rblf.mkinfo("product.mk", rblf.notdir("foo/bar"))
Sasha Smundak3deb9682021-07-26 18:42:25 -0700703 rblf.add_soong_config_namespace(g, "snsconfig")
704 rblf.add_soong_config_var_value(g, "snsconfig", "imagetype", "odm_image")
705 cfg["PRODUCT_COPY_FILES"] = rblf.copy_files(rblf.expand_wildcard("foo*.mk"), "etc")
Sasha Smundak04453082021-08-17 18:14:41 -0700706 cfg["PRODUCT_COPY_FILES"] = rblf.product_copy_files_by_pattern("from/%", "to/%", "a b c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800707`,
708 },
709 {
Sasha Smundak9d011ab2021-07-09 16:00:57 -0700710 desc: "subst in list",
711 mkname: "product.mk",
712 in: `
713files = $(call find-copy-subdir-files,*,from,to)
714PRODUCT_COPY_FILES += $(subst foo,bar,$(files))
715`,
716 expected: `load("//build/make/core:product_config.rbc", "rblf")
717
718def init(g, handle):
719 cfg = rblf.cfg(handle)
720 _files = rblf.find_and_copy("*", "from", "to")
721 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
722 cfg["PRODUCT_COPY_FILES"] += rblf.mksubst("foo", "bar", _files)
723`,
724 },
725 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800726 desc: "assignment flavors",
727 mkname: "product.mk",
728 in: `
729PRODUCT_LIST1 := a
730PRODUCT_LIST2 += a
731PRODUCT_LIST1 += b
732PRODUCT_LIST2 += b
733PRODUCT_LIST3 ?= a
734PRODUCT_LIST1 = c
735PLATFORM_LIST += x
736PRODUCT_PACKAGES := $(PLATFORM_LIST)
737`,
738 expected: `load("//build/make/core:product_config.rbc", "rblf")
739
740def init(g, handle):
741 cfg = rblf.cfg(handle)
742 cfg["PRODUCT_LIST1"] = ["a"]
743 rblf.setdefault(handle, "PRODUCT_LIST2")
744 cfg["PRODUCT_LIST2"] += ["a"]
745 cfg["PRODUCT_LIST1"] += ["b"]
746 cfg["PRODUCT_LIST2"] += ["b"]
747 if cfg.get("PRODUCT_LIST3") == None:
748 cfg["PRODUCT_LIST3"] = ["a"]
749 cfg["PRODUCT_LIST1"] = ["c"]
750 g.setdefault("PLATFORM_LIST", [])
751 g["PLATFORM_LIST"] += ["x"]
752 cfg["PRODUCT_PACKAGES"] = g["PLATFORM_LIST"][:]
753`,
754 },
755 {
756 desc: "assigment flavors2",
757 mkname: "product.mk",
758 in: `
759PRODUCT_LIST1 = a
760ifeq (0,1)
761 PRODUCT_LIST1 += b
762 PRODUCT_LIST2 += b
763endif
764PRODUCT_LIST1 += c
765PRODUCT_LIST2 += c
766`,
767 expected: `load("//build/make/core:product_config.rbc", "rblf")
768
769def init(g, handle):
770 cfg = rblf.cfg(handle)
771 cfg["PRODUCT_LIST1"] = ["a"]
772 if "0" == "1":
773 cfg["PRODUCT_LIST1"] += ["b"]
774 rblf.setdefault(handle, "PRODUCT_LIST2")
775 cfg["PRODUCT_LIST2"] += ["b"]
776 cfg["PRODUCT_LIST1"] += ["c"]
777 rblf.setdefault(handle, "PRODUCT_LIST2")
778 cfg["PRODUCT_LIST2"] += ["c"]
779`,
780 },
781 {
Sasha Smundak3deb9682021-07-26 18:42:25 -0700782 desc: "soong namespace assignments",
783 mkname: "product.mk",
784 in: `
785SOONG_CONFIG_NAMESPACES += cvd
786SOONG_CONFIG_cvd += launch_configs
787SOONG_CONFIG_cvd_launch_configs += cvd_config_auto.json
788SOONG_CONFIG_cvd += grub_config
789SOONG_CONFIG_cvd_grub_config += grub.cfg
790`,
791 expected: `load("//build/make/core:product_config.rbc", "rblf")
792
793def init(g, handle):
794 cfg = rblf.cfg(handle)
795 rblf.add_soong_config_namespace(g, "cvd")
796 rblf.add_soong_config_var_value(g, "cvd", "launch_configs", "cvd_config_auto.json")
797 rblf.add_soong_config_var_value(g, "cvd", "grub_config", "grub.cfg")
798`,
799 },
800 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800801 desc: "string split",
802 mkname: "product.mk",
803 in: `
804PRODUCT_LIST1 = a
805local = b
806local += c
807FOO = d
808FOO += e
809PRODUCT_LIST1 += $(local)
810PRODUCT_LIST1 += $(FOO)
811`,
812 expected: `load("//build/make/core:product_config.rbc", "rblf")
813
814def init(g, handle):
815 cfg = rblf.cfg(handle)
816 cfg["PRODUCT_LIST1"] = ["a"]
817 _local = "b"
818 _local += " " + "c"
819 g["FOO"] = "d"
820 g["FOO"] += " " + "e"
821 cfg["PRODUCT_LIST1"] += (_local).split()
822 cfg["PRODUCT_LIST1"] += (g["FOO"]).split()
823`,
824 },
825 {
826 desc: "apex_jars",
827 mkname: "product.mk",
828 in: `
829PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex
830`,
831 expected: `load("//build/make/core:product_config.rbc", "rblf")
832
833def init(g, handle):
834 cfg = rblf.cfg(handle)
835 cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) +
836 ["framework-minus-apex"])
837`,
838 },
839 {
840 desc: "strip function",
841 mkname: "product.mk",
842 in: `
843ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),)
844 PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress)
845endif
846`,
847 expected: `load("//build/make/core:product_config.rbc", "rblf")
848
849def init(g, handle):
850 cfg = rblf.cfg(handle)
851 if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []):
852 cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split()
853`,
854 },
855 {
856 desc: "strip func in condition",
857 mkname: "product.mk",
858 in: `
859ifneq ($(strip $(TARGET_VENDOR)),)
860endif
861`,
862 expected: `load("//build/make/core:product_config.rbc", "rblf")
863
864def init(g, handle):
865 cfg = rblf.cfg(handle)
Sasha Smundak0554d762021-07-08 18:26:12 -0700866 if rblf.mkstrip(g.get("TARGET_VENDOR", "")):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800867 pass
868`,
869 },
870 {
871 desc: "ref after set",
872 mkname: "product.mk",
873 in: `
874PRODUCT_ADB_KEYS:=value
875FOO := $(PRODUCT_ADB_KEYS)
876ifneq (,$(PRODUCT_ADB_KEYS))
877endif
878`,
879 expected: `load("//build/make/core:product_config.rbc", "rblf")
880
881def init(g, handle):
882 cfg = rblf.cfg(handle)
883 g["PRODUCT_ADB_KEYS"] = "value"
884 g["FOO"] = g["PRODUCT_ADB_KEYS"]
885 if g["PRODUCT_ADB_KEYS"]:
886 pass
887`,
888 },
889 {
890 desc: "ref before set",
891 mkname: "product.mk",
892 in: `
893V1 := $(PRODUCT_ADB_KEYS)
894ifeq (,$(PRODUCT_ADB_KEYS))
895 V2 := $(PRODUCT_ADB_KEYS)
896 PRODUCT_ADB_KEYS:=foo
897 V3 := $(PRODUCT_ADB_KEYS)
898endif`,
899 expected: `load("//build/make/core:product_config.rbc", "rblf")
900
901def init(g, handle):
902 cfg = rblf.cfg(handle)
903 g["V1"] = g.get("PRODUCT_ADB_KEYS", "")
904 if not g.get("PRODUCT_ADB_KEYS", ""):
905 g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
906 g["PRODUCT_ADB_KEYS"] = "foo"
907 g["V3"] = g["PRODUCT_ADB_KEYS"]
908`,
909 },
Sasha Smundak6609ba72021-07-22 18:32:56 -0700910 {
911 desc: "Dynamic inherit path",
912 mkname: "product.mk",
913 in: `
914MY_PATH=foo
915$(call inherit-product,vendor/$(MY_PATH)/cfg.mk)
916`,
917 expected: `load("//build/make/core:product_config.rbc", "rblf")
918load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
919load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init")
920
921def init(g, handle):
922 cfg = rblf.cfg(handle)
923 g["MY_PATH"] = "foo"
924 _entry = {
925 "vendor/foo1/cfg.mk": ("_cfg", _cfg_init),
926 "vendor/bar/baz/cfg.mk": ("_cfg1", _cfg1_init),
927 }.get("vendor/%s/cfg.mk" % g["MY_PATH"])
928 (_varmod, _varmod_init) = _entry if _entry else (None, None)
929 if not _varmod_init:
930 rblf.mkerror("cannot")
931 rblf.inherit(handle, _varmod, _varmod_init)
932`,
933 },
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800934}
935
936var known_variables = []struct {
937 name string
938 class varClass
939 starlarkType
940}{
941 {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
942 {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
943 {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
944 {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
945 {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
946 {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
947 {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
948 {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
949 {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
950 {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
951 {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
952 {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
953 {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
954 {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
955}
956
Sasha Smundak6609ba72021-07-22 18:32:56 -0700957type testMakefileFinder struct {
958 fs fs.FS
959 root string
960 files []string
961}
962
963func (t *testMakefileFinder) Find(root string) []string {
964 if t.files != nil || root == t.root {
965 return t.files
966 }
967 t.files = make([]string, 0)
968 fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error {
969 if err != nil {
970 return err
971 }
972 if d.IsDir() {
973 base := filepath.Base(path)
974 if base[0] == '.' && len(base) > 1 {
975 return fs.SkipDir
976 }
977 return nil
978 }
979 if strings.HasSuffix(path, ".mk") {
980 t.files = append(t.files, path)
981 }
982 return nil
983 })
984 return t.files
985}
986
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800987func TestGood(t *testing.T) {
988 for _, v := range known_variables {
989 KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
990 }
Sasha Smundak6609ba72021-07-22 18:32:56 -0700991 fs := NewFindMockFS([]string{
992 "vendor/foo1/cfg.mk",
993 "vendor/bar/baz/cfg.mk",
994 "part.mk",
995 "foo/font.mk",
996 "bar/font.mk",
997 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800998 for _, test := range testCases {
999 t.Run(test.desc,
1000 func(t *testing.T) {
1001 ss, err := Convert(Request{
1002 MkFile: test.mkname,
1003 Reader: bytes.NewBufferString(test.in),
1004 RootDir: ".",
1005 OutputSuffix: ".star",
1006 WarnPartialSuccess: true,
Sasha Smundak6609ba72021-07-22 18:32:56 -07001007 SourceFS: fs,
1008 MakefileFinder: &testMakefileFinder{fs: fs},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001009 })
1010 if err != nil {
1011 t.Error(err)
1012 return
1013 }
1014 got := ss.String()
1015 if got != test.expected {
1016 t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
1017 strings.ReplaceAll(test.expected, "\n", "␤\n"),
1018 strings.ReplaceAll(got, "\n", "␤\n"))
1019 }
1020 })
1021 }
1022}