blob: d3ac7fc7f77ebf6f9e7f90a9fbdf3ecfb93eb103 [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)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700684$(call soong_config_set, snsconfig, foo, foo_value)
685$(call soong_config_append, snsconfig, bar, bar_value)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700686PRODUCT_COPY_FILES := $(call copy-files,$(wildcard foo*.mk),etc)
Sasha Smundak04453082021-08-17 18:14:41 -0700687PRODUCT_COPY_FILES := $(call product-copy-files-by-pattern,from/%,to/%,a b c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800688`,
689 expected: `load("//build/make/core:product_config.rbc", "rblf")
690
691def init(g, handle):
692 cfg = rblf.cfg(handle)
693 cfg["PRODUCT_COPY_FILES"] = rblf.addprefix("pfx-", "a b c")
694 cfg["PRODUCT_COPY_FILES"] = rblf.addsuffix(".sff", "a b c")
695 cfg["PRODUCT_NAME"] = ((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " ")).split()[0]
Sasha Smundak94b41c72021-07-12 18:30:42 -0700696 rblf.mkinfo("product.mk", rblf.mkpatsubst("%.pub", "%", g.get("PRODUCT_ADB_KEYS", "")))
Sasha Smundak16e07732021-07-23 11:38:23 -0700697 rblf.mkinfo("product.mk", rblf.dir("foo/bar"))
698 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][0])
699 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][-1])
700 rblf.mkinfo("product.mk", rblf.dir("product.mk"))
701 rblf.mkinfo("product.mk", rblf.dir(cfg["PRODUCT_COPY_FILES"][-1]))
702 rblf.mkinfo("product.mk", rblf.dir((_foobar).split()[-1]))
703 rblf.mkinfo("product.mk", rblf.abspath("foo/bar"))
704 rblf.mkinfo("product.mk", rblf.notdir("foo/bar"))
Sasha Smundak65b547e2021-09-17 15:35:41 -0700705 rblf.soong_config_namespace(g, "snsconfig")
706 rblf.soong_config_set(g, "snsconfig", "imagetype", "odm_image")
707 rblf.soong_config_set(g, "snsconfig", "foo", "foo_value")
708 rblf.soong_config_append(g, "snsconfig", "bar", "bar_value")
Sasha Smundak3deb9682021-07-26 18:42:25 -0700709 cfg["PRODUCT_COPY_FILES"] = rblf.copy_files(rblf.expand_wildcard("foo*.mk"), "etc")
Sasha Smundak04453082021-08-17 18:14:41 -0700710 cfg["PRODUCT_COPY_FILES"] = rblf.product_copy_files_by_pattern("from/%", "to/%", "a b c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800711`,
712 },
713 {
Sasha Smundak9d011ab2021-07-09 16:00:57 -0700714 desc: "subst in list",
715 mkname: "product.mk",
716 in: `
717files = $(call find-copy-subdir-files,*,from,to)
718PRODUCT_COPY_FILES += $(subst foo,bar,$(files))
719`,
720 expected: `load("//build/make/core:product_config.rbc", "rblf")
721
722def init(g, handle):
723 cfg = rblf.cfg(handle)
724 _files = rblf.find_and_copy("*", "from", "to")
725 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
726 cfg["PRODUCT_COPY_FILES"] += rblf.mksubst("foo", "bar", _files)
727`,
728 },
729 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800730 desc: "assignment flavors",
731 mkname: "product.mk",
732 in: `
733PRODUCT_LIST1 := a
734PRODUCT_LIST2 += a
735PRODUCT_LIST1 += b
736PRODUCT_LIST2 += b
737PRODUCT_LIST3 ?= a
738PRODUCT_LIST1 = c
739PLATFORM_LIST += x
740PRODUCT_PACKAGES := $(PLATFORM_LIST)
741`,
742 expected: `load("//build/make/core:product_config.rbc", "rblf")
743
744def init(g, handle):
745 cfg = rblf.cfg(handle)
746 cfg["PRODUCT_LIST1"] = ["a"]
747 rblf.setdefault(handle, "PRODUCT_LIST2")
748 cfg["PRODUCT_LIST2"] += ["a"]
749 cfg["PRODUCT_LIST1"] += ["b"]
750 cfg["PRODUCT_LIST2"] += ["b"]
751 if cfg.get("PRODUCT_LIST3") == None:
752 cfg["PRODUCT_LIST3"] = ["a"]
753 cfg["PRODUCT_LIST1"] = ["c"]
754 g.setdefault("PLATFORM_LIST", [])
755 g["PLATFORM_LIST"] += ["x"]
756 cfg["PRODUCT_PACKAGES"] = g["PLATFORM_LIST"][:]
757`,
758 },
759 {
760 desc: "assigment flavors2",
761 mkname: "product.mk",
762 in: `
763PRODUCT_LIST1 = a
764ifeq (0,1)
765 PRODUCT_LIST1 += b
766 PRODUCT_LIST2 += b
767endif
768PRODUCT_LIST1 += c
769PRODUCT_LIST2 += c
770`,
771 expected: `load("//build/make/core:product_config.rbc", "rblf")
772
773def init(g, handle):
774 cfg = rblf.cfg(handle)
775 cfg["PRODUCT_LIST1"] = ["a"]
776 if "0" == "1":
777 cfg["PRODUCT_LIST1"] += ["b"]
778 rblf.setdefault(handle, "PRODUCT_LIST2")
779 cfg["PRODUCT_LIST2"] += ["b"]
780 cfg["PRODUCT_LIST1"] += ["c"]
781 rblf.setdefault(handle, "PRODUCT_LIST2")
782 cfg["PRODUCT_LIST2"] += ["c"]
783`,
784 },
785 {
Sasha Smundak3deb9682021-07-26 18:42:25 -0700786 desc: "soong namespace assignments",
787 mkname: "product.mk",
788 in: `
789SOONG_CONFIG_NAMESPACES += cvd
790SOONG_CONFIG_cvd += launch_configs
Sasha Smundak65b547e2021-09-17 15:35:41 -0700791SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
Sasha Smundak3deb9682021-07-26 18:42:25 -0700792SOONG_CONFIG_cvd += grub_config
793SOONG_CONFIG_cvd_grub_config += grub.cfg
Sasha Smundak65b547e2021-09-17 15:35:41 -0700794x := $(SOONG_CONFIG_cvd_grub_config)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700795`,
796 expected: `load("//build/make/core:product_config.rbc", "rblf")
797
798def init(g, handle):
799 cfg = rblf.cfg(handle)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700800 rblf.soong_config_namespace(g, "cvd")
801 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
802 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
803 # MK2RBC TRANSLATION ERROR: SOONG_CONFIG_ variables cannot be referenced: SOONG_CONFIG_cvd_grub_config
804 # x := $(SOONG_CONFIG_cvd_grub_config)
805 rblf.warning("product.mk", "partially successful conversion")
Sasha Smundak3deb9682021-07-26 18:42:25 -0700806`,
807 },
808 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800809 desc: "string split",
810 mkname: "product.mk",
811 in: `
812PRODUCT_LIST1 = a
813local = b
814local += c
815FOO = d
816FOO += e
817PRODUCT_LIST1 += $(local)
818PRODUCT_LIST1 += $(FOO)
819`,
820 expected: `load("//build/make/core:product_config.rbc", "rblf")
821
822def init(g, handle):
823 cfg = rblf.cfg(handle)
824 cfg["PRODUCT_LIST1"] = ["a"]
825 _local = "b"
826 _local += " " + "c"
827 g["FOO"] = "d"
828 g["FOO"] += " " + "e"
829 cfg["PRODUCT_LIST1"] += (_local).split()
830 cfg["PRODUCT_LIST1"] += (g["FOO"]).split()
831`,
832 },
833 {
834 desc: "apex_jars",
835 mkname: "product.mk",
836 in: `
837PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex
838`,
839 expected: `load("//build/make/core:product_config.rbc", "rblf")
840
841def init(g, handle):
842 cfg = rblf.cfg(handle)
843 cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) +
844 ["framework-minus-apex"])
845`,
846 },
847 {
848 desc: "strip function",
849 mkname: "product.mk",
850 in: `
851ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),)
852 PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress)
853endif
854`,
855 expected: `load("//build/make/core:product_config.rbc", "rblf")
856
857def init(g, handle):
858 cfg = rblf.cfg(handle)
859 if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []):
860 cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split()
861`,
862 },
863 {
864 desc: "strip func in condition",
865 mkname: "product.mk",
866 in: `
867ifneq ($(strip $(TARGET_VENDOR)),)
868endif
869`,
870 expected: `load("//build/make/core:product_config.rbc", "rblf")
871
872def init(g, handle):
873 cfg = rblf.cfg(handle)
Sasha Smundak0554d762021-07-08 18:26:12 -0700874 if rblf.mkstrip(g.get("TARGET_VENDOR", "")):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800875 pass
876`,
877 },
878 {
879 desc: "ref after set",
880 mkname: "product.mk",
881 in: `
882PRODUCT_ADB_KEYS:=value
883FOO := $(PRODUCT_ADB_KEYS)
884ifneq (,$(PRODUCT_ADB_KEYS))
885endif
886`,
887 expected: `load("//build/make/core:product_config.rbc", "rblf")
888
889def init(g, handle):
890 cfg = rblf.cfg(handle)
891 g["PRODUCT_ADB_KEYS"] = "value"
892 g["FOO"] = g["PRODUCT_ADB_KEYS"]
893 if g["PRODUCT_ADB_KEYS"]:
894 pass
895`,
896 },
897 {
898 desc: "ref before set",
899 mkname: "product.mk",
900 in: `
901V1 := $(PRODUCT_ADB_KEYS)
902ifeq (,$(PRODUCT_ADB_KEYS))
903 V2 := $(PRODUCT_ADB_KEYS)
904 PRODUCT_ADB_KEYS:=foo
905 V3 := $(PRODUCT_ADB_KEYS)
906endif`,
907 expected: `load("//build/make/core:product_config.rbc", "rblf")
908
909def init(g, handle):
910 cfg = rblf.cfg(handle)
911 g["V1"] = g.get("PRODUCT_ADB_KEYS", "")
912 if not g.get("PRODUCT_ADB_KEYS", ""):
913 g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
914 g["PRODUCT_ADB_KEYS"] = "foo"
915 g["V3"] = g["PRODUCT_ADB_KEYS"]
916`,
917 },
Sasha Smundak6609ba72021-07-22 18:32:56 -0700918 {
919 desc: "Dynamic inherit path",
920 mkname: "product.mk",
921 in: `
Sasha Smundak6d852dd2021-09-27 20:34:39 -0700922MY_PATH:=foo
Sasha Smundak6609ba72021-07-22 18:32:56 -0700923$(call inherit-product,vendor/$(MY_PATH)/cfg.mk)
924`,
925 expected: `load("//build/make/core:product_config.rbc", "rblf")
926load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
927load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init")
928
929def init(g, handle):
930 cfg = rblf.cfg(handle)
931 g["MY_PATH"] = "foo"
932 _entry = {
933 "vendor/foo1/cfg.mk": ("_cfg", _cfg_init),
934 "vendor/bar/baz/cfg.mk": ("_cfg1", _cfg1_init),
935 }.get("vendor/%s/cfg.mk" % g["MY_PATH"])
936 (_varmod, _varmod_init) = _entry if _entry else (None, None)
937 if not _varmod_init:
938 rblf.mkerror("cannot")
939 rblf.inherit(handle, _varmod, _varmod_init)
940`,
941 },
Sasha Smundak6d852dd2021-09-27 20:34:39 -0700942 {
943 desc: "Dynamic inherit with hint",
944 mkname: "product.mk",
945 in: `
946MY_PATH:=foo
947#RBC# include_top vendor/foo1
948$(call inherit-product,$(MY_PATH)/cfg.mk)
949`,
950 expected: `load("//build/make/core:product_config.rbc", "rblf")
951load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
952
953def init(g, handle):
954 cfg = rblf.cfg(handle)
955 g["MY_PATH"] = "foo"
956 #RBC# include_top vendor/foo1
957 _entry = {
958 "vendor/foo1/cfg.mk": ("_cfg", _cfg_init),
959 }.get("%s/cfg.mk" % g["MY_PATH"])
960 (_varmod, _varmod_init) = _entry if _entry else (None, None)
961 if not _varmod_init:
962 rblf.mkerror("cannot")
963 rblf.inherit(handle, _varmod, _varmod_init)
964`,
965 },
Sasha Smundak2afb9d72021-10-24 15:16:59 -0700966 {
967 desc: "Ignore make rules",
968 mkname: "product.mk",
969 in: `
970foo: foo.c
971 gcc -o $@ $*`,
972 expected: `# MK2RBC TRANSLATION ERROR: unsupported line rule: foo: foo.c
973#gcc -o $@ $*
974# rule: foo: foo.c
975# gcc -o $@ $*
976load("//build/make/core:product_config.rbc", "rblf")
977
978def init(g, handle):
979 cfg = rblf.cfg(handle)
980 rblf.warning("product.mk", "partially successful conversion")
981`,
982 },
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800983}
984
985var known_variables = []struct {
986 name string
987 class varClass
988 starlarkType
989}{
990 {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
991 {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
992 {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
993 {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
994 {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
995 {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
996 {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
997 {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
998 {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
999 {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
1000 {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
1001 {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
1002 {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
1003 {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
1004}
1005
Sasha Smundak6609ba72021-07-22 18:32:56 -07001006type testMakefileFinder struct {
1007 fs fs.FS
1008 root string
1009 files []string
1010}
1011
1012func (t *testMakefileFinder) Find(root string) []string {
1013 if t.files != nil || root == t.root {
1014 return t.files
1015 }
1016 t.files = make([]string, 0)
1017 fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error {
1018 if err != nil {
1019 return err
1020 }
1021 if d.IsDir() {
1022 base := filepath.Base(path)
1023 if base[0] == '.' && len(base) > 1 {
1024 return fs.SkipDir
1025 }
1026 return nil
1027 }
1028 if strings.HasSuffix(path, ".mk") {
1029 t.files = append(t.files, path)
1030 }
1031 return nil
1032 })
1033 return t.files
1034}
1035
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001036func TestGood(t *testing.T) {
1037 for _, v := range known_variables {
1038 KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
1039 }
Sasha Smundak6609ba72021-07-22 18:32:56 -07001040 fs := NewFindMockFS([]string{
1041 "vendor/foo1/cfg.mk",
1042 "vendor/bar/baz/cfg.mk",
1043 "part.mk",
1044 "foo/font.mk",
1045 "bar/font.mk",
1046 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001047 for _, test := range testCases {
1048 t.Run(test.desc,
1049 func(t *testing.T) {
1050 ss, err := Convert(Request{
1051 MkFile: test.mkname,
1052 Reader: bytes.NewBufferString(test.in),
1053 RootDir: ".",
1054 OutputSuffix: ".star",
1055 WarnPartialSuccess: true,
Sasha Smundak6609ba72021-07-22 18:32:56 -07001056 SourceFS: fs,
1057 MakefileFinder: &testMakefileFinder{fs: fs},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001058 })
1059 if err != nil {
1060 t.Error(err)
1061 return
1062 }
1063 got := ss.String()
1064 if got != test.expected {
1065 t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
1066 strings.ReplaceAll(test.expected, "\n", "␤\n"),
1067 strings.ReplaceAll(got, "\n", "␤\n"))
1068 }
1069 })
1070 }
1071}