blob: c2452ea29560f9a266a4412ce383afd7ffd2a486 [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
Sasha Smundakc4fa93e2021-11-05 14:38:46 -0700229local_var :=
230ifdef local_var
231endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800232`,
233 expected: `load("//build/make/core:product_config.rbc", "rblf")
234
235def init(g, handle):
236 cfg = rblf.cfg(handle)
237 if g.get("PRODUCT_NAME") != None:
238 cfg["PRODUCT_NAME"] = "gizmo"
239 else:
240 pass
Sasha Smundakc4fa93e2021-11-05 14:38:46 -0700241 _local_var = ""
242 if _local_var:
243 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800244`,
245 },
246 {
247 desc: "Simple functions",
248 mkname: "product.mk",
249 in: `
250$(warning this is the warning)
251$(warning)
252$(info this is the info)
253$(error this is the error)
254PRODUCT_NAME:=$(shell echo *)
255`,
256 expected: `load("//build/make/core:product_config.rbc", "rblf")
257
258def init(g, handle):
259 cfg = rblf.cfg(handle)
260 rblf.mkwarning("product.mk", "this is the warning")
261 rblf.mkwarning("product.mk", "")
262 rblf.mkinfo("product.mk", "this is the info")
263 rblf.mkerror("product.mk", "this is the error")
264 cfg["PRODUCT_NAME"] = rblf.shell("echo *")
265`,
266 },
267 {
268 desc: "Empty if",
269 mkname: "product.mk",
270 in: `
271ifdef PRODUCT_NAME
272# Comment
Sasha Smundak6609ba72021-07-22 18:32:56 -0700273else
Sasha Smundak02183cf2021-08-16 13:36:11 -0700274 TARGET_COPY_OUT_RECOVERY := foo
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800275endif
276`,
277 expected: `load("//build/make/core:product_config.rbc", "rblf")
278
279def init(g, handle):
280 cfg = rblf.cfg(handle)
281 if g.get("PRODUCT_NAME") != None:
282 # Comment
283 pass
Sasha Smundak6609ba72021-07-22 18:32:56 -0700284 else:
Sasha Smundak02183cf2021-08-16 13:36:11 -0700285 # 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 -0700286 pass
287 rblf.warning("product.mk", "partially successful conversion")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800288`,
289 },
290 {
291 desc: "if/else/endif",
292 mkname: "product.mk",
293 in: `
294ifndef PRODUCT_NAME
295 PRODUCT_NAME=gizmo1
296else
297 PRODUCT_NAME=gizmo2
298endif
299`,
300 expected: `load("//build/make/core:product_config.rbc", "rblf")
301
302def init(g, handle):
303 cfg = rblf.cfg(handle)
304 if not g.get("PRODUCT_NAME") != None:
305 cfg["PRODUCT_NAME"] = "gizmo1"
306 else:
307 cfg["PRODUCT_NAME"] = "gizmo2"
308`,
309 },
310 {
311 desc: "else if",
312 mkname: "product.mk",
313 in: `
314ifdef PRODUCT_NAME
315 PRODUCT_NAME = gizmo
316else ifndef PRODUCT_PACKAGES # Comment
317endif
318 `,
319 expected: `load("//build/make/core:product_config.rbc", "rblf")
320
321def init(g, handle):
322 cfg = rblf.cfg(handle)
323 if g.get("PRODUCT_NAME") != None:
324 cfg["PRODUCT_NAME"] = "gizmo"
325 elif not g.get("PRODUCT_PACKAGES") != None:
326 # Comment
327 pass
328`,
329 },
330 {
331 desc: "ifeq / ifneq",
332 mkname: "product.mk",
333 in: `
334ifeq (aosp_arm, $(TARGET_PRODUCT))
335 PRODUCT_MODEL = pix2
336else
337 PRODUCT_MODEL = pix21
338endif
339ifneq (aosp_x86, $(TARGET_PRODUCT))
340 PRODUCT_MODEL = pix3
341endif
342`,
343 expected: `load("//build/make/core:product_config.rbc", "rblf")
344
345def init(g, handle):
346 cfg = rblf.cfg(handle)
347 if "aosp_arm" == g["TARGET_PRODUCT"]:
348 cfg["PRODUCT_MODEL"] = "pix2"
349 else:
350 cfg["PRODUCT_MODEL"] = "pix21"
351 if "aosp_x86" != g["TARGET_PRODUCT"]:
352 cfg["PRODUCT_MODEL"] = "pix3"
353`,
354 },
355 {
356 desc: "Check filter result",
357 mkname: "product.mk",
358 in: `
359ifeq (,$(filter userdebug eng, $(TARGET_BUILD_VARIANT)))
360endif
361ifneq (,$(filter userdebug,$(TARGET_BUILD_VARIANT))
362endif
363ifneq (,$(filter plaf,$(PLATFORM_LIST)))
364endif
365ifeq ($(TARGET_BUILD_VARIANT), $(filter $(TARGET_BUILD_VARIANT), userdebug eng))
366endif
Sasha Smundak0554d762021-07-08 18:26:12 -0700367ifneq (,$(filter true, $(v1)$(v2)))
368endif
Sasha Smundak5f463be2021-09-15 18:43:36 -0700369ifeq (,$(filter barbet coral%,$(TARGET_PRODUCT)))
370else ifneq (,$(filter barbet%,$(TARGET_PRODUCT)))
371endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800372`,
373 expected: `load("//build/make/core:product_config.rbc", "rblf")
374
375def init(g, handle):
376 cfg = rblf.cfg(handle)
Sasha Smundak5f463be2021-09-15 18:43:36 -0700377 if not rblf.filter("userdebug eng", g["TARGET_BUILD_VARIANT"]):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800378 pass
Sasha Smundak5f463be2021-09-15 18:43:36 -0700379 if rblf.filter("userdebug", g["TARGET_BUILD_VARIANT"]):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800380 pass
381 if "plaf" in g.get("PLATFORM_LIST", []):
382 pass
383 if g["TARGET_BUILD_VARIANT"] in ["userdebug", "eng"]:
384 pass
Sasha Smundak5f463be2021-09-15 18:43:36 -0700385 if rblf.filter("true", "%s%s" % (_v1, _v2)):
386 pass
387 if not rblf.filter("barbet coral%", g["TARGET_PRODUCT"]):
388 pass
389 elif rblf.filter("barbet%", g["TARGET_PRODUCT"]):
Sasha Smundak0554d762021-07-08 18:26:12 -0700390 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800391`,
392 },
393 {
394 desc: "Get filter result",
395 mkname: "product.mk",
396 in: `
397PRODUCT_LIST2=$(filter-out %/foo.ko,$(wildcard path/*.ko))
398`,
399 expected: `load("//build/make/core:product_config.rbc", "rblf")
400
401def init(g, handle):
402 cfg = rblf.cfg(handle)
403 cfg["PRODUCT_LIST2"] = rblf.filter_out("%/foo.ko", rblf.expand_wildcard("path/*.ko"))
404`,
405 },
406 {
407 desc: "filter $(VAR), values",
408 mkname: "product.mk",
409 in: `
410ifeq (,$(filter $(TARGET_PRODUCT), yukawa_gms yukawa_sei510_gms)
411 ifneq (,$(filter $(TARGET_PRODUCT), yukawa_gms)
412 endif
413endif
414
415`,
416 expected: `load("//build/make/core:product_config.rbc", "rblf")
417
418def init(g, handle):
419 cfg = rblf.cfg(handle)
420 if g["TARGET_PRODUCT"] not in ["yukawa_gms", "yukawa_sei510_gms"]:
Sasha Smundak0554d762021-07-08 18:26:12 -0700421 if g["TARGET_PRODUCT"] == "yukawa_gms":
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800422 pass
423`,
424 },
425 {
Sasha Smundak0554d762021-07-08 18:26:12 -0700426 desc: "filter $(V1), $(V2)",
427 mkname: "product.mk",
428 in: `
429ifneq (, $(filter $(PRODUCT_LIST), $(TARGET_PRODUCT)))
430endif
431`,
432 expected: `load("//build/make/core:product_config.rbc", "rblf")
433
434def init(g, handle):
435 cfg = rblf.cfg(handle)
Sasha Smundak468e11f2021-08-26 09:10:23 -0700436 if rblf.filter(g.get("PRODUCT_LIST", []), g["TARGET_PRODUCT"]):
Sasha Smundak0554d762021-07-08 18:26:12 -0700437 pass
438`,
439 },
440 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800441 desc: "ifeq",
442 mkname: "product.mk",
443 in: `
444ifeq (aosp, $(TARGET_PRODUCT)) # Comment
445else ifneq (, $(TARGET_PRODUCT))
446endif
447`,
448 expected: `load("//build/make/core:product_config.rbc", "rblf")
449
450def init(g, handle):
451 cfg = rblf.cfg(handle)
452 if "aosp" == g["TARGET_PRODUCT"]:
453 # Comment
454 pass
455 elif g["TARGET_PRODUCT"]:
456 pass
457`,
458 },
459 {
460 desc: "Nested if",
461 mkname: "product.mk",
462 in: `
463ifdef PRODUCT_NAME
464 PRODUCT_PACKAGES = pack-if0
465 ifdef PRODUCT_MODEL
466 PRODUCT_PACKAGES = pack-if-if
467 else ifdef PRODUCT_NAME
468 PRODUCT_PACKAGES = pack-if-elif
469 else
470 PRODUCT_PACKAGES = pack-if-else
471 endif
472 PRODUCT_PACKAGES = pack-if
473else ifneq (,$(TARGET_PRODUCT))
474 PRODUCT_PACKAGES = pack-elif
475else
476 PRODUCT_PACKAGES = pack-else
477endif
478`,
479 expected: `load("//build/make/core:product_config.rbc", "rblf")
480
481def init(g, handle):
482 cfg = rblf.cfg(handle)
483 if g.get("PRODUCT_NAME") != None:
484 cfg["PRODUCT_PACKAGES"] = ["pack-if0"]
485 if g.get("PRODUCT_MODEL") != None:
486 cfg["PRODUCT_PACKAGES"] = ["pack-if-if"]
487 elif g.get("PRODUCT_NAME") != None:
488 cfg["PRODUCT_PACKAGES"] = ["pack-if-elif"]
489 else:
490 cfg["PRODUCT_PACKAGES"] = ["pack-if-else"]
491 cfg["PRODUCT_PACKAGES"] = ["pack-if"]
492 elif g["TARGET_PRODUCT"]:
493 cfg["PRODUCT_PACKAGES"] = ["pack-elif"]
494 else:
495 cfg["PRODUCT_PACKAGES"] = ["pack-else"]
496`,
497 },
498 {
499 desc: "Wildcard",
500 mkname: "product.mk",
501 in: `
502ifeq (,$(wildcard foo.mk))
503endif
504ifneq (,$(wildcard foo*.mk))
505endif
506`,
507 expected: `load("//build/make/core:product_config.rbc", "rblf")
508
509def init(g, handle):
510 cfg = rblf.cfg(handle)
511 if not rblf.file_exists("foo.mk"):
512 pass
513 if rblf.file_wildcard_exists("foo*.mk"):
514 pass
515`,
516 },
517 {
518 desc: "ifneq $(X),true",
519 mkname: "product.mk",
520 in: `
521ifneq ($(VARIABLE),true)
522endif
523`,
524 expected: `load("//build/make/core:product_config.rbc", "rblf")
525
526def init(g, handle):
527 cfg = rblf.cfg(handle)
528 if g.get("VARIABLE", "") != "true":
529 pass
530`,
531 },
532 {
533 desc: "Const neq",
534 mkname: "product.mk",
535 in: `
536ifneq (1,0)
537endif
538`,
539 expected: `load("//build/make/core:product_config.rbc", "rblf")
540
541def init(g, handle):
542 cfg = rblf.cfg(handle)
543 if "1" != "0":
544 pass
545`,
546 },
547 {
548 desc: "is-board calls",
549 mkname: "product.mk",
550 in: `
551ifeq ($(call is-board-platform-in-list,msm8998), true)
552else ifneq ($(call is-board-platform,copper),true)
553else ifneq ($(call is-vendor-board-platform,QCOM),true)
554else ifeq ($(call is-product-in-list, $(PLATFORM_LIST)), true)
555endif
556`,
557 expected: `load("//build/make/core:product_config.rbc", "rblf")
558
559def init(g, handle):
560 cfg = rblf.cfg(handle)
561 if g.get("TARGET_BOARD_PLATFORM", "") in ["msm8998"]:
562 pass
563 elif g.get("TARGET_BOARD_PLATFORM", "") != "copper":
564 pass
565 elif g.get("TARGET_BOARD_PLATFORM", "") not in g["QCOM_BOARD_PLATFORMS"]:
566 pass
567 elif g["TARGET_PRODUCT"] in g.get("PLATFORM_LIST", []):
568 pass
569`,
570 },
571 {
Sasha Smundak3a9b8e82021-08-25 14:11:04 -0700572 desc: "new is-board calls",
573 mkname: "product.mk",
574 in: `
575ifneq (,$(call is-board-platform-in-list2,msm8998 $(X))
576else ifeq (,$(call is-board-platform2,copper)
577else ifneq (,$(call is-vendor-board-qcom))
578endif
579`,
580 expected: `load("//build/make/core:product_config.rbc", "rblf")
581
582def init(g, handle):
583 cfg = rblf.cfg(handle)
584 if rblf.board_platform_in(g, "msm8998 %s" % g.get("X", "")):
585 pass
586 elif not rblf.board_platform_is(g, "copper"):
587 pass
Sasha Smundak4f1f1182021-11-04 17:57:39 -0700588 elif g.get("TARGET_BOARD_PLATFORM", "") in g["QCOM_BOARD_PLATFORMS"]:
Sasha Smundak3a9b8e82021-08-25 14:11:04 -0700589 pass
590`,
591 },
592 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800593 desc: "findstring call",
594 mkname: "product.mk",
595 in: `
596ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),)
597endif
598`,
599 expected: `load("//build/make/core:product_config.rbc", "rblf")
600
601def init(g, handle):
602 cfg = rblf.cfg(handle)
603 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
604 pass
605`,
606 },
607 {
608 desc: "rhs call",
609 mkname: "product.mk",
610 in: `
611PRODUCT_COPY_FILES = $(call add-to-product-copy-files-if-exists, path:distpath) \
612 $(call find-copy-subdir-files, *, fromdir, todir) $(wildcard foo.*)
613`,
614 expected: `load("//build/make/core:product_config.rbc", "rblf")
615
616def init(g, handle):
617 cfg = rblf.cfg(handle)
618 cfg["PRODUCT_COPY_FILES"] = (rblf.copy_if_exists("path:distpath") +
619 rblf.find_and_copy("*", "fromdir", "todir") +
620 rblf.expand_wildcard("foo.*"))
621`,
622 },
623 {
624 desc: "inferred type",
625 mkname: "product.mk",
626 in: `
627HIKEY_MODS := $(wildcard foo/*.ko)
628BOARD_VENDOR_KERNEL_MODULES += $(HIKEY_MODS)
629`,
630 expected: `load("//build/make/core:product_config.rbc", "rblf")
631
632def init(g, handle):
633 cfg = rblf.cfg(handle)
634 g["HIKEY_MODS"] = rblf.expand_wildcard("foo/*.ko")
635 g.setdefault("BOARD_VENDOR_KERNEL_MODULES", [])
636 g["BOARD_VENDOR_KERNEL_MODULES"] += g["HIKEY_MODS"]
637`,
638 },
639 {
640 desc: "list with vars",
641 mkname: "product.mk",
642 in: `
643PRODUCT_COPY_FILES += path1:$(TARGET_PRODUCT)/path1 $(PRODUCT_MODEL)/path2:$(TARGET_PRODUCT)/path2
644`,
645 expected: `load("//build/make/core:product_config.rbc", "rblf")
646
647def init(g, handle):
648 cfg = rblf.cfg(handle)
649 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
650 cfg["PRODUCT_COPY_FILES"] += (("path1:%s/path1" % g["TARGET_PRODUCT"]).split() +
651 ("%s/path2:%s/path2" % (cfg.get("PRODUCT_MODEL", ""), g["TARGET_PRODUCT"])).split())
652`,
653 },
654 {
655 desc: "misc calls",
656 mkname: "product.mk",
657 in: `
658$(call enforce-product-packages-exist,)
659$(call enforce-product-packages-exist, foo)
660$(call require-artifacts-in-path, foo, bar)
661$(call require-artifacts-in-path-relaxed, foo, bar)
662`,
663 expected: `load("//build/make/core:product_config.rbc", "rblf")
664
665def init(g, handle):
666 cfg = rblf.cfg(handle)
667 rblf.enforce_product_packages_exist("")
668 rblf.enforce_product_packages_exist("foo")
669 rblf.require_artifacts_in_path("foo", "bar")
670 rblf.require_artifacts_in_path_relaxed("foo", "bar")
671`,
672 },
673 {
674 desc: "list with functions",
675 mkname: "product.mk",
676 in: `
677PRODUCT_COPY_FILES := $(call find-copy-subdir-files,*.kl,from1,to1) \
678 $(call find-copy-subdir-files,*.kc,from2,to2) \
679 foo bar
680`,
681 expected: `load("//build/make/core:product_config.rbc", "rblf")
682
683def init(g, handle):
684 cfg = rblf.cfg(handle)
685 cfg["PRODUCT_COPY_FILES"] = (rblf.find_and_copy("*.kl", "from1", "to1") +
686 rblf.find_and_copy("*.kc", "from2", "to2") +
687 [
688 "foo",
689 "bar",
690 ])
691`,
692 },
693 {
694 desc: "Text functions",
695 mkname: "product.mk",
696 in: `
697PRODUCT_COPY_FILES := $(addprefix pfx-,a b c)
698PRODUCT_COPY_FILES := $(addsuffix .sff, a b c)
699PRODUCT_NAME := $(word 1, $(subst ., ,$(TARGET_BOARD_PLATFORM)))
Sasha Smundak35434ed2021-11-05 16:29:56 -0700700$(info $(patsubst %.pub,$(PRODUCT_NAME)%,$(PRODUCT_ADB_KEYS)))
Sasha Smundak16e07732021-07-23 11:38:23 -0700701$(info $(dir foo/bar))
702$(info $(firstword $(PRODUCT_COPY_FILES)))
703$(info $(lastword $(PRODUCT_COPY_FILES)))
704$(info $(dir $(lastword $(MAKEFILE_LIST))))
705$(info $(dir $(lastword $(PRODUCT_COPY_FILES))))
706$(info $(dir $(lastword $(foobar))))
707$(info $(abspath foo/bar))
708$(info $(notdir foo/bar))
Sasha Smundak3deb9682021-07-26 18:42:25 -0700709$(call add_soong_config_namespace,snsconfig)
710$(call add_soong_config_var_value,snsconfig,imagetype,odm_image)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700711$(call soong_config_set, snsconfig, foo, foo_value)
712$(call soong_config_append, snsconfig, bar, bar_value)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700713PRODUCT_COPY_FILES := $(call copy-files,$(wildcard foo*.mk),etc)
Sasha Smundak04453082021-08-17 18:14:41 -0700714PRODUCT_COPY_FILES := $(call product-copy-files-by-pattern,from/%,to/%,a b c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800715`,
716 expected: `load("//build/make/core:product_config.rbc", "rblf")
717
718def init(g, handle):
719 cfg = rblf.cfg(handle)
720 cfg["PRODUCT_COPY_FILES"] = rblf.addprefix("pfx-", "a b c")
721 cfg["PRODUCT_COPY_FILES"] = rblf.addsuffix(".sff", "a b c")
722 cfg["PRODUCT_NAME"] = ((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " ")).split()[0]
Sasha Smundak35434ed2021-11-05 16:29:56 -0700723 rblf.mkinfo("product.mk", rblf.mkpatsubst("%.pub", "%s%%" % cfg["PRODUCT_NAME"], g.get("PRODUCT_ADB_KEYS", "")))
Sasha Smundak16e07732021-07-23 11:38:23 -0700724 rblf.mkinfo("product.mk", rblf.dir("foo/bar"))
725 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][0])
726 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][-1])
727 rblf.mkinfo("product.mk", rblf.dir("product.mk"))
728 rblf.mkinfo("product.mk", rblf.dir(cfg["PRODUCT_COPY_FILES"][-1]))
729 rblf.mkinfo("product.mk", rblf.dir((_foobar).split()[-1]))
730 rblf.mkinfo("product.mk", rblf.abspath("foo/bar"))
731 rblf.mkinfo("product.mk", rblf.notdir("foo/bar"))
Sasha Smundak65b547e2021-09-17 15:35:41 -0700732 rblf.soong_config_namespace(g, "snsconfig")
733 rblf.soong_config_set(g, "snsconfig", "imagetype", "odm_image")
734 rblf.soong_config_set(g, "snsconfig", "foo", "foo_value")
735 rblf.soong_config_append(g, "snsconfig", "bar", "bar_value")
Sasha Smundak3deb9682021-07-26 18:42:25 -0700736 cfg["PRODUCT_COPY_FILES"] = rblf.copy_files(rblf.expand_wildcard("foo*.mk"), "etc")
Sasha Smundak04453082021-08-17 18:14:41 -0700737 cfg["PRODUCT_COPY_FILES"] = rblf.product_copy_files_by_pattern("from/%", "to/%", "a b c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800738`,
739 },
740 {
Sasha Smundak9d011ab2021-07-09 16:00:57 -0700741 desc: "subst in list",
742 mkname: "product.mk",
743 in: `
744files = $(call find-copy-subdir-files,*,from,to)
745PRODUCT_COPY_FILES += $(subst foo,bar,$(files))
746`,
747 expected: `load("//build/make/core:product_config.rbc", "rblf")
748
749def init(g, handle):
750 cfg = rblf.cfg(handle)
751 _files = rblf.find_and_copy("*", "from", "to")
752 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
753 cfg["PRODUCT_COPY_FILES"] += rblf.mksubst("foo", "bar", _files)
754`,
755 },
756 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800757 desc: "assignment flavors",
758 mkname: "product.mk",
759 in: `
760PRODUCT_LIST1 := a
761PRODUCT_LIST2 += a
762PRODUCT_LIST1 += b
763PRODUCT_LIST2 += b
764PRODUCT_LIST3 ?= a
765PRODUCT_LIST1 = c
766PLATFORM_LIST += x
767PRODUCT_PACKAGES := $(PLATFORM_LIST)
768`,
769 expected: `load("//build/make/core:product_config.rbc", "rblf")
770
771def init(g, handle):
772 cfg = rblf.cfg(handle)
773 cfg["PRODUCT_LIST1"] = ["a"]
774 rblf.setdefault(handle, "PRODUCT_LIST2")
775 cfg["PRODUCT_LIST2"] += ["a"]
776 cfg["PRODUCT_LIST1"] += ["b"]
777 cfg["PRODUCT_LIST2"] += ["b"]
778 if cfg.get("PRODUCT_LIST3") == None:
779 cfg["PRODUCT_LIST3"] = ["a"]
780 cfg["PRODUCT_LIST1"] = ["c"]
781 g.setdefault("PLATFORM_LIST", [])
782 g["PLATFORM_LIST"] += ["x"]
783 cfg["PRODUCT_PACKAGES"] = g["PLATFORM_LIST"][:]
784`,
785 },
786 {
787 desc: "assigment flavors2",
788 mkname: "product.mk",
789 in: `
790PRODUCT_LIST1 = a
791ifeq (0,1)
792 PRODUCT_LIST1 += b
793 PRODUCT_LIST2 += b
794endif
795PRODUCT_LIST1 += c
796PRODUCT_LIST2 += c
797`,
798 expected: `load("//build/make/core:product_config.rbc", "rblf")
799
800def init(g, handle):
801 cfg = rblf.cfg(handle)
802 cfg["PRODUCT_LIST1"] = ["a"]
803 if "0" == "1":
804 cfg["PRODUCT_LIST1"] += ["b"]
805 rblf.setdefault(handle, "PRODUCT_LIST2")
806 cfg["PRODUCT_LIST2"] += ["b"]
807 cfg["PRODUCT_LIST1"] += ["c"]
808 rblf.setdefault(handle, "PRODUCT_LIST2")
809 cfg["PRODUCT_LIST2"] += ["c"]
810`,
811 },
812 {
Sasha Smundak3deb9682021-07-26 18:42:25 -0700813 desc: "soong namespace assignments",
814 mkname: "product.mk",
815 in: `
816SOONG_CONFIG_NAMESPACES += cvd
817SOONG_CONFIG_cvd += launch_configs
Sasha Smundak65b547e2021-09-17 15:35:41 -0700818SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
Sasha Smundak3deb9682021-07-26 18:42:25 -0700819SOONG_CONFIG_cvd += grub_config
820SOONG_CONFIG_cvd_grub_config += grub.cfg
Sasha Smundak65b547e2021-09-17 15:35:41 -0700821x := $(SOONG_CONFIG_cvd_grub_config)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700822`,
823 expected: `load("//build/make/core:product_config.rbc", "rblf")
824
825def init(g, handle):
826 cfg = rblf.cfg(handle)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700827 rblf.soong_config_namespace(g, "cvd")
828 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
829 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
830 # MK2RBC TRANSLATION ERROR: SOONG_CONFIG_ variables cannot be referenced: SOONG_CONFIG_cvd_grub_config
831 # x := $(SOONG_CONFIG_cvd_grub_config)
832 rblf.warning("product.mk", "partially successful conversion")
Sasha Smundak3deb9682021-07-26 18:42:25 -0700833`,
834 },
835 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800836 desc: "string split",
837 mkname: "product.mk",
838 in: `
839PRODUCT_LIST1 = a
840local = b
841local += c
842FOO = d
843FOO += e
844PRODUCT_LIST1 += $(local)
845PRODUCT_LIST1 += $(FOO)
846`,
847 expected: `load("//build/make/core:product_config.rbc", "rblf")
848
849def init(g, handle):
850 cfg = rblf.cfg(handle)
851 cfg["PRODUCT_LIST1"] = ["a"]
852 _local = "b"
853 _local += " " + "c"
854 g["FOO"] = "d"
855 g["FOO"] += " " + "e"
856 cfg["PRODUCT_LIST1"] += (_local).split()
857 cfg["PRODUCT_LIST1"] += (g["FOO"]).split()
858`,
859 },
860 {
861 desc: "apex_jars",
862 mkname: "product.mk",
863 in: `
864PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex
865`,
866 expected: `load("//build/make/core:product_config.rbc", "rblf")
867
868def init(g, handle):
869 cfg = rblf.cfg(handle)
870 cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) +
871 ["framework-minus-apex"])
872`,
873 },
874 {
875 desc: "strip function",
876 mkname: "product.mk",
877 in: `
878ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),)
879 PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress)
880endif
881`,
882 expected: `load("//build/make/core:product_config.rbc", "rblf")
883
884def init(g, handle):
885 cfg = rblf.cfg(handle)
886 if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []):
887 cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split()
888`,
889 },
890 {
891 desc: "strip func in condition",
892 mkname: "product.mk",
893 in: `
894ifneq ($(strip $(TARGET_VENDOR)),)
895endif
896`,
897 expected: `load("//build/make/core:product_config.rbc", "rblf")
898
899def init(g, handle):
900 cfg = rblf.cfg(handle)
Sasha Smundak0554d762021-07-08 18:26:12 -0700901 if rblf.mkstrip(g.get("TARGET_VENDOR", "")):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800902 pass
903`,
904 },
905 {
906 desc: "ref after set",
907 mkname: "product.mk",
908 in: `
909PRODUCT_ADB_KEYS:=value
910FOO := $(PRODUCT_ADB_KEYS)
911ifneq (,$(PRODUCT_ADB_KEYS))
912endif
913`,
914 expected: `load("//build/make/core:product_config.rbc", "rblf")
915
916def init(g, handle):
917 cfg = rblf.cfg(handle)
918 g["PRODUCT_ADB_KEYS"] = "value"
919 g["FOO"] = g["PRODUCT_ADB_KEYS"]
920 if g["PRODUCT_ADB_KEYS"]:
921 pass
922`,
923 },
924 {
925 desc: "ref before set",
926 mkname: "product.mk",
927 in: `
928V1 := $(PRODUCT_ADB_KEYS)
929ifeq (,$(PRODUCT_ADB_KEYS))
930 V2 := $(PRODUCT_ADB_KEYS)
931 PRODUCT_ADB_KEYS:=foo
932 V3 := $(PRODUCT_ADB_KEYS)
933endif`,
934 expected: `load("//build/make/core:product_config.rbc", "rblf")
935
936def init(g, handle):
937 cfg = rblf.cfg(handle)
938 g["V1"] = g.get("PRODUCT_ADB_KEYS", "")
939 if not g.get("PRODUCT_ADB_KEYS", ""):
940 g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
941 g["PRODUCT_ADB_KEYS"] = "foo"
942 g["V3"] = g["PRODUCT_ADB_KEYS"]
943`,
944 },
Sasha Smundak6609ba72021-07-22 18:32:56 -0700945 {
946 desc: "Dynamic inherit path",
947 mkname: "product.mk",
948 in: `
Sasha Smundak6d852dd2021-09-27 20:34:39 -0700949MY_PATH:=foo
Sasha Smundak6609ba72021-07-22 18:32:56 -0700950$(call inherit-product,vendor/$(MY_PATH)/cfg.mk)
951`,
952 expected: `load("//build/make/core:product_config.rbc", "rblf")
953load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
954load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init")
955
956def init(g, handle):
957 cfg = rblf.cfg(handle)
958 g["MY_PATH"] = "foo"
959 _entry = {
960 "vendor/foo1/cfg.mk": ("_cfg", _cfg_init),
961 "vendor/bar/baz/cfg.mk": ("_cfg1", _cfg1_init),
962 }.get("vendor/%s/cfg.mk" % g["MY_PATH"])
963 (_varmod, _varmod_init) = _entry if _entry else (None, None)
964 if not _varmod_init:
965 rblf.mkerror("cannot")
966 rblf.inherit(handle, _varmod, _varmod_init)
967`,
968 },
Sasha Smundak6d852dd2021-09-27 20:34:39 -0700969 {
970 desc: "Dynamic inherit with hint",
971 mkname: "product.mk",
972 in: `
973MY_PATH:=foo
974#RBC# include_top vendor/foo1
975$(call inherit-product,$(MY_PATH)/cfg.mk)
976`,
977 expected: `load("//build/make/core:product_config.rbc", "rblf")
978load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
979
980def init(g, handle):
981 cfg = rblf.cfg(handle)
982 g["MY_PATH"] = "foo"
983 #RBC# include_top vendor/foo1
984 _entry = {
985 "vendor/foo1/cfg.mk": ("_cfg", _cfg_init),
986 }.get("%s/cfg.mk" % g["MY_PATH"])
987 (_varmod, _varmod_init) = _entry if _entry else (None, None)
988 if not _varmod_init:
989 rblf.mkerror("cannot")
990 rblf.inherit(handle, _varmod, _varmod_init)
991`,
992 },
Sasha Smundak2afb9d72021-10-24 15:16:59 -0700993 {
994 desc: "Ignore make rules",
995 mkname: "product.mk",
996 in: `
997foo: foo.c
998 gcc -o $@ $*`,
999 expected: `# MK2RBC TRANSLATION ERROR: unsupported line rule: foo: foo.c
1000#gcc -o $@ $*
1001# rule: foo: foo.c
1002# gcc -o $@ $*
1003load("//build/make/core:product_config.rbc", "rblf")
1004
1005def init(g, handle):
1006 cfg = rblf.cfg(handle)
1007 rblf.warning("product.mk", "partially successful conversion")
1008`,
1009 },
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001010}
1011
1012var known_variables = []struct {
1013 name string
1014 class varClass
1015 starlarkType
1016}{
1017 {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
1018 {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
1019 {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
1020 {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
1021 {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
1022 {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
1023 {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
1024 {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
1025 {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
1026 {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
1027 {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
1028 {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
1029 {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
1030 {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
1031}
1032
Sasha Smundak6609ba72021-07-22 18:32:56 -07001033type testMakefileFinder struct {
1034 fs fs.FS
1035 root string
1036 files []string
1037}
1038
1039func (t *testMakefileFinder) Find(root string) []string {
1040 if t.files != nil || root == t.root {
1041 return t.files
1042 }
1043 t.files = make([]string, 0)
1044 fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error {
1045 if err != nil {
1046 return err
1047 }
1048 if d.IsDir() {
1049 base := filepath.Base(path)
1050 if base[0] == '.' && len(base) > 1 {
1051 return fs.SkipDir
1052 }
1053 return nil
1054 }
1055 if strings.HasSuffix(path, ".mk") {
1056 t.files = append(t.files, path)
1057 }
1058 return nil
1059 })
1060 return t.files
1061}
1062
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001063func TestGood(t *testing.T) {
1064 for _, v := range known_variables {
1065 KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
1066 }
Sasha Smundak6609ba72021-07-22 18:32:56 -07001067 fs := NewFindMockFS([]string{
1068 "vendor/foo1/cfg.mk",
1069 "vendor/bar/baz/cfg.mk",
1070 "part.mk",
1071 "foo/font.mk",
1072 "bar/font.mk",
1073 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001074 for _, test := range testCases {
1075 t.Run(test.desc,
1076 func(t *testing.T) {
1077 ss, err := Convert(Request{
1078 MkFile: test.mkname,
1079 Reader: bytes.NewBufferString(test.in),
1080 RootDir: ".",
1081 OutputSuffix: ".star",
1082 WarnPartialSuccess: true,
Sasha Smundak6609ba72021-07-22 18:32:56 -07001083 SourceFS: fs,
1084 MakefileFinder: &testMakefileFinder{fs: fs},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001085 })
1086 if err != nil {
1087 t.Error(err)
1088 return
1089 }
1090 got := ss.String()
1091 if got != test.expected {
1092 t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
1093 strings.ReplaceAll(test.expected, "\n", "␤\n"),
1094 strings.ReplaceAll(got, "\n", "␤\n"))
1095 }
1096 })
1097 }
1098}