blob: 2d768aeacddd7461da0242d51bd2947fb23b14d1 [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 Smundak422b6142021-11-11 18:31:59 -0800108 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800109
110def init(g, handle):
111 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -0800112 rblf.mk2rbc_error("product.mk:2", "cannot handle invoking foo1")
113 rblf.mk2rbc_error("product.mk:3", "cannot handle invoking foo0")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800114`,
115 },
116 {
117 desc: "Inherit configuration always",
118 mkname: "product.mk",
119 in: `
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800120$(call inherit-product, part.mk)
Sasha Smundak868c5e32021-09-23 16:20:58 -0700121ifdef PRODUCT_NAME
122$(call inherit-product, part1.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800123else # Comment
Sasha Smundak868c5e32021-09-23 16:20:58 -0700124$(call inherit-product, $(LOCAL_PATH)/part1.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800125endif
126`,
127 expected: `load("//build/make/core:product_config.rbc", "rblf")
128load(":part.star", _part_init = "init")
Sasha Smundak868c5e32021-09-23 16:20:58 -0700129load(":part1.star|init", _part1_init = "init")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800130
131def init(g, handle):
132 cfg = rblf.cfg(handle)
Sasha Smundak868c5e32021-09-23 16:20:58 -0700133 rblf.inherit(handle, "part", _part_init)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800134 if g.get("PRODUCT_NAME") != None:
Sasha Smundak868c5e32021-09-23 16:20:58 -0700135 rblf.inherit(handle, "part1", _part1_init)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800136 else:
137 # Comment
Sasha Smundak868c5e32021-09-23 16:20:58 -0700138 rblf.inherit(handle, "part1", _part1_init)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800139`,
140 },
141 {
142 desc: "Inherit configuration if it exists",
143 mkname: "product.mk",
144 in: `
145$(call inherit-product-if-exists, part.mk)
146`,
147 expected: `load("//build/make/core:product_config.rbc", "rblf")
148load(":part.star|init", _part_init = "init")
149
150def init(g, handle):
151 cfg = rblf.cfg(handle)
Sasha Smundak6609ba72021-07-22 18:32:56 -0700152 if _part_init:
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800153 rblf.inherit(handle, "part", _part_init)
154`,
155 },
156
157 {
158 desc: "Include configuration",
159 mkname: "product.mk",
160 in: `
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800161include part.mk
Sasha Smundak868c5e32021-09-23 16:20:58 -0700162ifdef PRODUCT_NAME
163include part1.mk
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800164else
Sasha Smundak868c5e32021-09-23 16:20:58 -0700165-include $(LOCAL_PATH)/part1.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800166endif
167`,
168 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundak868c5e32021-09-23 16:20:58 -0700169load(":part.star", _part_init = "init")
170load(":part1.star|init", _part1_init = "init")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800171
172def init(g, handle):
173 cfg = rblf.cfg(handle)
Sasha Smundak868c5e32021-09-23 16:20:58 -0700174 _part_init(g, handle)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800175 if g.get("PRODUCT_NAME") != None:
Sasha Smundak868c5e32021-09-23 16:20:58 -0700176 _part1_init(g, handle)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800177 else:
Sasha Smundak868c5e32021-09-23 16:20:58 -0700178 if _part1_init != None:
179 _part1_init(g, handle)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800180`,
181 },
182
183 {
184 desc: "Synonymous inherited configurations",
185 mkname: "path/product.mk",
186 in: `
Sasha Smundak6609ba72021-07-22 18:32:56 -0700187$(call inherit-product, */font.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800188`,
189 expected: `load("//build/make/core:product_config.rbc", "rblf")
190load("//foo:font.star", _font_init = "init")
191load("//bar:font.star", _font1_init = "init")
192
193def init(g, handle):
194 cfg = rblf.cfg(handle)
195 rblf.inherit(handle, "foo/font", _font_init)
196 rblf.inherit(handle, "bar/font", _font1_init)
197`,
198 },
199 {
200 desc: "Directive define",
201 mkname: "product.mk",
202 in: `
203define some-macro
204 $(info foo)
205endef
206`,
Sasha Smundak422b6142021-11-11 18:31:59 -0800207 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800208
209def init(g, handle):
210 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -0800211 rblf.mk2rbc_error("product.mk:2", "define is not supported: some-macro")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800212`,
213 },
214 {
215 desc: "Ifdef",
216 mkname: "product.mk",
217 in: `
218ifdef PRODUCT_NAME
219 PRODUCT_NAME = gizmo
220else
221endif
Sasha Smundakc4fa93e2021-11-05 14:38:46 -0700222local_var :=
223ifdef local_var
224endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800225`,
226 expected: `load("//build/make/core:product_config.rbc", "rblf")
227
228def init(g, handle):
229 cfg = rblf.cfg(handle)
230 if g.get("PRODUCT_NAME") != None:
231 cfg["PRODUCT_NAME"] = "gizmo"
232 else:
233 pass
Sasha Smundakc4fa93e2021-11-05 14:38:46 -0700234 _local_var = ""
235 if _local_var:
236 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800237`,
238 },
239 {
240 desc: "Simple functions",
241 mkname: "product.mk",
242 in: `
243$(warning this is the warning)
244$(warning)
245$(info this is the info)
246$(error this is the error)
247PRODUCT_NAME:=$(shell echo *)
248`,
249 expected: `load("//build/make/core:product_config.rbc", "rblf")
250
251def init(g, handle):
252 cfg = rblf.cfg(handle)
253 rblf.mkwarning("product.mk", "this is the warning")
254 rblf.mkwarning("product.mk", "")
255 rblf.mkinfo("product.mk", "this is the info")
256 rblf.mkerror("product.mk", "this is the error")
257 cfg["PRODUCT_NAME"] = rblf.shell("echo *")
258`,
259 },
260 {
261 desc: "Empty if",
262 mkname: "product.mk",
263 in: `
264ifdef PRODUCT_NAME
265# Comment
Sasha Smundak6609ba72021-07-22 18:32:56 -0700266else
Sasha Smundak02183cf2021-08-16 13:36:11 -0700267 TARGET_COPY_OUT_RECOVERY := foo
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800268endif
269`,
270 expected: `load("//build/make/core:product_config.rbc", "rblf")
271
272def init(g, handle):
273 cfg = rblf.cfg(handle)
274 if g.get("PRODUCT_NAME") != None:
275 # Comment
276 pass
Sasha Smundak6609ba72021-07-22 18:32:56 -0700277 else:
Sasha Smundak422b6142021-11-11 18:31:59 -0800278 rblf.mk2rbc_error("product.mk:5", "cannot set predefined variable TARGET_COPY_OUT_RECOVERY to \"foo\", its value should be \"recovery\"")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800279`,
280 },
281 {
282 desc: "if/else/endif",
283 mkname: "product.mk",
284 in: `
285ifndef PRODUCT_NAME
286 PRODUCT_NAME=gizmo1
287else
288 PRODUCT_NAME=gizmo2
289endif
290`,
291 expected: `load("//build/make/core:product_config.rbc", "rblf")
292
293def init(g, handle):
294 cfg = rblf.cfg(handle)
295 if not g.get("PRODUCT_NAME") != None:
296 cfg["PRODUCT_NAME"] = "gizmo1"
297 else:
298 cfg["PRODUCT_NAME"] = "gizmo2"
299`,
300 },
301 {
302 desc: "else if",
303 mkname: "product.mk",
304 in: `
305ifdef PRODUCT_NAME
306 PRODUCT_NAME = gizmo
307else ifndef PRODUCT_PACKAGES # Comment
308endif
309 `,
310 expected: `load("//build/make/core:product_config.rbc", "rblf")
311
312def init(g, handle):
313 cfg = rblf.cfg(handle)
314 if g.get("PRODUCT_NAME") != None:
315 cfg["PRODUCT_NAME"] = "gizmo"
316 elif not g.get("PRODUCT_PACKAGES") != None:
317 # Comment
318 pass
319`,
320 },
321 {
322 desc: "ifeq / ifneq",
323 mkname: "product.mk",
324 in: `
325ifeq (aosp_arm, $(TARGET_PRODUCT))
326 PRODUCT_MODEL = pix2
327else
328 PRODUCT_MODEL = pix21
329endif
330ifneq (aosp_x86, $(TARGET_PRODUCT))
331 PRODUCT_MODEL = pix3
332endif
333`,
334 expected: `load("//build/make/core:product_config.rbc", "rblf")
335
336def init(g, handle):
337 cfg = rblf.cfg(handle)
338 if "aosp_arm" == g["TARGET_PRODUCT"]:
339 cfg["PRODUCT_MODEL"] = "pix2"
340 else:
341 cfg["PRODUCT_MODEL"] = "pix21"
342 if "aosp_x86" != g["TARGET_PRODUCT"]:
343 cfg["PRODUCT_MODEL"] = "pix3"
344`,
345 },
346 {
Cole Faustf8320212021-11-10 15:05:07 -0800347 desc: "ifeq with soong_config_get",
348 mkname: "product.mk",
349 in: `
350ifeq (true,$(call soong_config_get,art_module,source_build))
351endif
352`,
353 expected: `load("//build/make/core:product_config.rbc", "rblf")
354
355def init(g, handle):
356 cfg = rblf.cfg(handle)
357 if "true" == rblf.soong_config_get(g, "art_module", "source_build"):
358 pass
359`,
360 },
361 {
Cole Faustf1f44d32021-11-16 14:52:12 -0800362 desc: "ifeq with $(NATIVE_COVERAGE)",
363 mkname: "product.mk",
364 in: `
365ifeq ($(NATIVE_COVERAGE),true)
366endif
367`,
368 expected: `load("//build/make/core:product_config.rbc", "rblf")
369
370def init(g, handle):
371 cfg = rblf.cfg(handle)
372 if g.get("NATIVE_COVERAGE", False):
373 pass
374`,
375 },
376 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800377 desc: "Check filter result",
378 mkname: "product.mk",
379 in: `
380ifeq (,$(filter userdebug eng, $(TARGET_BUILD_VARIANT)))
381endif
382ifneq (,$(filter userdebug,$(TARGET_BUILD_VARIANT))
383endif
384ifneq (,$(filter plaf,$(PLATFORM_LIST)))
385endif
386ifeq ($(TARGET_BUILD_VARIANT), $(filter $(TARGET_BUILD_VARIANT), userdebug eng))
387endif
Sasha Smundak0554d762021-07-08 18:26:12 -0700388ifneq (,$(filter true, $(v1)$(v2)))
389endif
Sasha Smundak5f463be2021-09-15 18:43:36 -0700390ifeq (,$(filter barbet coral%,$(TARGET_PRODUCT)))
391else ifneq (,$(filter barbet%,$(TARGET_PRODUCT)))
392endif
Cole Fausteec0d812021-12-06 16:23:51 -0800393ifeq (,$(filter-out sunfish_kasan, $(TARGET_PRODUCT)))
394endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800395`,
396 expected: `load("//build/make/core:product_config.rbc", "rblf")
397
398def init(g, handle):
399 cfg = rblf.cfg(handle)
Sasha Smundak5f463be2021-09-15 18:43:36 -0700400 if not rblf.filter("userdebug eng", g["TARGET_BUILD_VARIANT"]):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800401 pass
Sasha Smundak5f463be2021-09-15 18:43:36 -0700402 if rblf.filter("userdebug", g["TARGET_BUILD_VARIANT"]):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800403 pass
404 if "plaf" in g.get("PLATFORM_LIST", []):
405 pass
406 if g["TARGET_BUILD_VARIANT"] in ["userdebug", "eng"]:
407 pass
Sasha Smundak5f463be2021-09-15 18:43:36 -0700408 if rblf.filter("true", "%s%s" % (_v1, _v2)):
409 pass
410 if not rblf.filter("barbet coral%", g["TARGET_PRODUCT"]):
411 pass
412 elif rblf.filter("barbet%", g["TARGET_PRODUCT"]):
Sasha Smundak0554d762021-07-08 18:26:12 -0700413 pass
Cole Fausteec0d812021-12-06 16:23:51 -0800414 if not rblf.filter_out("sunfish_kasan", g["TARGET_PRODUCT"]):
415 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800416`,
417 },
418 {
419 desc: "Get filter result",
420 mkname: "product.mk",
421 in: `
422PRODUCT_LIST2=$(filter-out %/foo.ko,$(wildcard path/*.ko))
423`,
424 expected: `load("//build/make/core:product_config.rbc", "rblf")
425
426def init(g, handle):
427 cfg = rblf.cfg(handle)
428 cfg["PRODUCT_LIST2"] = rblf.filter_out("%/foo.ko", rblf.expand_wildcard("path/*.ko"))
429`,
430 },
431 {
432 desc: "filter $(VAR), values",
433 mkname: "product.mk",
434 in: `
435ifeq (,$(filter $(TARGET_PRODUCT), yukawa_gms yukawa_sei510_gms)
436 ifneq (,$(filter $(TARGET_PRODUCT), yukawa_gms)
437 endif
438endif
439
440`,
441 expected: `load("//build/make/core:product_config.rbc", "rblf")
442
443def init(g, handle):
444 cfg = rblf.cfg(handle)
445 if g["TARGET_PRODUCT"] not in ["yukawa_gms", "yukawa_sei510_gms"]:
Sasha Smundak0554d762021-07-08 18:26:12 -0700446 if g["TARGET_PRODUCT"] == "yukawa_gms":
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800447 pass
448`,
449 },
450 {
Sasha Smundak0554d762021-07-08 18:26:12 -0700451 desc: "filter $(V1), $(V2)",
452 mkname: "product.mk",
453 in: `
454ifneq (, $(filter $(PRODUCT_LIST), $(TARGET_PRODUCT)))
455endif
456`,
457 expected: `load("//build/make/core:product_config.rbc", "rblf")
458
459def init(g, handle):
460 cfg = rblf.cfg(handle)
Sasha Smundak468e11f2021-08-26 09:10:23 -0700461 if rblf.filter(g.get("PRODUCT_LIST", []), g["TARGET_PRODUCT"]):
Sasha Smundak0554d762021-07-08 18:26:12 -0700462 pass
463`,
464 },
465 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800466 desc: "ifeq",
467 mkname: "product.mk",
468 in: `
469ifeq (aosp, $(TARGET_PRODUCT)) # Comment
470else ifneq (, $(TARGET_PRODUCT))
471endif
472`,
473 expected: `load("//build/make/core:product_config.rbc", "rblf")
474
475def init(g, handle):
476 cfg = rblf.cfg(handle)
477 if "aosp" == g["TARGET_PRODUCT"]:
478 # Comment
479 pass
480 elif g["TARGET_PRODUCT"]:
481 pass
482`,
483 },
484 {
485 desc: "Nested if",
486 mkname: "product.mk",
487 in: `
488ifdef PRODUCT_NAME
489 PRODUCT_PACKAGES = pack-if0
490 ifdef PRODUCT_MODEL
491 PRODUCT_PACKAGES = pack-if-if
492 else ifdef PRODUCT_NAME
493 PRODUCT_PACKAGES = pack-if-elif
494 else
495 PRODUCT_PACKAGES = pack-if-else
496 endif
497 PRODUCT_PACKAGES = pack-if
498else ifneq (,$(TARGET_PRODUCT))
499 PRODUCT_PACKAGES = pack-elif
500else
501 PRODUCT_PACKAGES = pack-else
502endif
503`,
504 expected: `load("//build/make/core:product_config.rbc", "rblf")
505
506def init(g, handle):
507 cfg = rblf.cfg(handle)
508 if g.get("PRODUCT_NAME") != None:
509 cfg["PRODUCT_PACKAGES"] = ["pack-if0"]
510 if g.get("PRODUCT_MODEL") != None:
511 cfg["PRODUCT_PACKAGES"] = ["pack-if-if"]
512 elif g.get("PRODUCT_NAME") != None:
513 cfg["PRODUCT_PACKAGES"] = ["pack-if-elif"]
514 else:
515 cfg["PRODUCT_PACKAGES"] = ["pack-if-else"]
516 cfg["PRODUCT_PACKAGES"] = ["pack-if"]
517 elif g["TARGET_PRODUCT"]:
518 cfg["PRODUCT_PACKAGES"] = ["pack-elif"]
519 else:
520 cfg["PRODUCT_PACKAGES"] = ["pack-else"]
521`,
522 },
523 {
524 desc: "Wildcard",
525 mkname: "product.mk",
526 in: `
527ifeq (,$(wildcard foo.mk))
528endif
529ifneq (,$(wildcard foo*.mk))
530endif
531`,
532 expected: `load("//build/make/core:product_config.rbc", "rblf")
533
534def init(g, handle):
535 cfg = rblf.cfg(handle)
536 if not rblf.file_exists("foo.mk"):
537 pass
538 if rblf.file_wildcard_exists("foo*.mk"):
539 pass
540`,
541 },
542 {
Cole Faustf8320212021-11-10 15:05:07 -0800543 desc: "if with interpolation",
544 mkname: "product.mk",
545 in: `
546ifeq ($(VARIABLE1)text$(VARIABLE2),true)
547endif
548`,
549 expected: `load("//build/make/core:product_config.rbc", "rblf")
550
551def init(g, handle):
552 cfg = rblf.cfg(handle)
553 if "%stext%s" % (g.get("VARIABLE1", ""), g.get("VARIABLE2", "")) == "true":
554 pass
555`,
556 },
557 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800558 desc: "ifneq $(X),true",
559 mkname: "product.mk",
560 in: `
561ifneq ($(VARIABLE),true)
562endif
563`,
564 expected: `load("//build/make/core:product_config.rbc", "rblf")
565
566def init(g, handle):
567 cfg = rblf.cfg(handle)
568 if g.get("VARIABLE", "") != "true":
569 pass
570`,
571 },
572 {
573 desc: "Const neq",
574 mkname: "product.mk",
575 in: `
576ifneq (1,0)
577endif
578`,
579 expected: `load("//build/make/core:product_config.rbc", "rblf")
580
581def init(g, handle):
582 cfg = rblf.cfg(handle)
583 if "1" != "0":
584 pass
585`,
586 },
587 {
588 desc: "is-board calls",
589 mkname: "product.mk",
590 in: `
591ifeq ($(call is-board-platform-in-list,msm8998), true)
592else ifneq ($(call is-board-platform,copper),true)
593else ifneq ($(call is-vendor-board-platform,QCOM),true)
594else ifeq ($(call is-product-in-list, $(PLATFORM_LIST)), true)
595endif
596`,
597 expected: `load("//build/make/core:product_config.rbc", "rblf")
598
599def init(g, handle):
600 cfg = rblf.cfg(handle)
601 if g.get("TARGET_BOARD_PLATFORM", "") in ["msm8998"]:
602 pass
603 elif g.get("TARGET_BOARD_PLATFORM", "") != "copper":
604 pass
605 elif g.get("TARGET_BOARD_PLATFORM", "") not in g["QCOM_BOARD_PLATFORMS"]:
606 pass
607 elif g["TARGET_PRODUCT"] in g.get("PLATFORM_LIST", []):
608 pass
609`,
610 },
611 {
Sasha Smundak3a9b8e82021-08-25 14:11:04 -0700612 desc: "new is-board calls",
613 mkname: "product.mk",
614 in: `
615ifneq (,$(call is-board-platform-in-list2,msm8998 $(X))
616else ifeq (,$(call is-board-platform2,copper)
617else ifneq (,$(call is-vendor-board-qcom))
618endif
619`,
620 expected: `load("//build/make/core:product_config.rbc", "rblf")
621
622def init(g, handle):
623 cfg = rblf.cfg(handle)
624 if rblf.board_platform_in(g, "msm8998 %s" % g.get("X", "")):
625 pass
626 elif not rblf.board_platform_is(g, "copper"):
627 pass
Sasha Smundak4f1f1182021-11-04 17:57:39 -0700628 elif g.get("TARGET_BOARD_PLATFORM", "") in g["QCOM_BOARD_PLATFORMS"]:
Sasha Smundak3a9b8e82021-08-25 14:11:04 -0700629 pass
630`,
631 },
632 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800633 desc: "findstring call",
634 mkname: "product.mk",
635 in: `
Cole Faust0e9418c2021-12-13 16:33:25 -0800636result := $(findstring a,a b c)
637result := $(findstring b,x y z)
638`,
639 expected: `load("//build/make/core:product_config.rbc", "rblf")
640
641def init(g, handle):
642 cfg = rblf.cfg(handle)
643 _result = rblf.findstring("a", "a b c")
644 _result = rblf.findstring("b", "x y z")
645`,
646 },
647 {
648 desc: "findstring in if statement",
649 mkname: "product.mk",
650 in: `
651ifeq ($(findstring foo,$(PRODUCT_PACKAGES)),)
652endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800653ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),)
654endif
Cole Faust0e9418c2021-12-13 16:33:25 -0800655ifeq ($(findstring foo,$(PRODUCT_PACKAGES)),foo)
656endif
657ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),foo)
658endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800659`,
660 expected: `load("//build/make/core:product_config.rbc", "rblf")
661
662def init(g, handle):
663 cfg = rblf.cfg(handle)
Cole Faust0e9418c2021-12-13 16:33:25 -0800664 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") == -1:
665 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800666 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
667 pass
Cole Faust0e9418c2021-12-13 16:33:25 -0800668 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
669 pass
670 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") == -1:
671 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800672`,
673 },
674 {
675 desc: "rhs call",
676 mkname: "product.mk",
677 in: `
678PRODUCT_COPY_FILES = $(call add-to-product-copy-files-if-exists, path:distpath) \
679 $(call find-copy-subdir-files, *, fromdir, todir) $(wildcard foo.*)
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.copy_if_exists("path:distpath") +
686 rblf.find_and_copy("*", "fromdir", "todir") +
687 rblf.expand_wildcard("foo.*"))
688`,
689 },
690 {
691 desc: "inferred type",
692 mkname: "product.mk",
693 in: `
694HIKEY_MODS := $(wildcard foo/*.ko)
695BOARD_VENDOR_KERNEL_MODULES += $(HIKEY_MODS)
696`,
697 expected: `load("//build/make/core:product_config.rbc", "rblf")
698
699def init(g, handle):
700 cfg = rblf.cfg(handle)
701 g["HIKEY_MODS"] = rblf.expand_wildcard("foo/*.ko")
702 g.setdefault("BOARD_VENDOR_KERNEL_MODULES", [])
703 g["BOARD_VENDOR_KERNEL_MODULES"] += g["HIKEY_MODS"]
704`,
705 },
706 {
707 desc: "list with vars",
708 mkname: "product.mk",
709 in: `
710PRODUCT_COPY_FILES += path1:$(TARGET_PRODUCT)/path1 $(PRODUCT_MODEL)/path2:$(TARGET_PRODUCT)/path2
711`,
712 expected: `load("//build/make/core:product_config.rbc", "rblf")
713
714def init(g, handle):
715 cfg = rblf.cfg(handle)
716 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
717 cfg["PRODUCT_COPY_FILES"] += (("path1:%s/path1" % g["TARGET_PRODUCT"]).split() +
718 ("%s/path2:%s/path2" % (cfg.get("PRODUCT_MODEL", ""), g["TARGET_PRODUCT"])).split())
719`,
720 },
721 {
722 desc: "misc calls",
723 mkname: "product.mk",
724 in: `
725$(call enforce-product-packages-exist,)
726$(call enforce-product-packages-exist, foo)
727$(call require-artifacts-in-path, foo, bar)
728$(call require-artifacts-in-path-relaxed, foo, bar)
Sasha Smundakd6797852021-11-15 13:01:53 -0800729$(call dist-for-goals, goal, from:to)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800730`,
731 expected: `load("//build/make/core:product_config.rbc", "rblf")
732
733def init(g, handle):
734 cfg = rblf.cfg(handle)
735 rblf.enforce_product_packages_exist("")
736 rblf.enforce_product_packages_exist("foo")
737 rblf.require_artifacts_in_path("foo", "bar")
738 rblf.require_artifacts_in_path_relaxed("foo", "bar")
Sasha Smundakd6797852021-11-15 13:01:53 -0800739 rblf.mkdist_for_goals(g, "goal", "from:to")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800740`,
741 },
742 {
743 desc: "list with functions",
744 mkname: "product.mk",
745 in: `
746PRODUCT_COPY_FILES := $(call find-copy-subdir-files,*.kl,from1,to1) \
747 $(call find-copy-subdir-files,*.kc,from2,to2) \
748 foo bar
749`,
750 expected: `load("//build/make/core:product_config.rbc", "rblf")
751
752def init(g, handle):
753 cfg = rblf.cfg(handle)
754 cfg["PRODUCT_COPY_FILES"] = (rblf.find_and_copy("*.kl", "from1", "to1") +
755 rblf.find_and_copy("*.kc", "from2", "to2") +
756 [
757 "foo",
758 "bar",
759 ])
760`,
761 },
762 {
763 desc: "Text functions",
764 mkname: "product.mk",
765 in: `
766PRODUCT_COPY_FILES := $(addprefix pfx-,a b c)
767PRODUCT_COPY_FILES := $(addsuffix .sff, a b c)
768PRODUCT_NAME := $(word 1, $(subst ., ,$(TARGET_BOARD_PLATFORM)))
Sasha Smundak35434ed2021-11-05 16:29:56 -0700769$(info $(patsubst %.pub,$(PRODUCT_NAME)%,$(PRODUCT_ADB_KEYS)))
Sasha Smundak16e07732021-07-23 11:38:23 -0700770$(info $(dir foo/bar))
771$(info $(firstword $(PRODUCT_COPY_FILES)))
772$(info $(lastword $(PRODUCT_COPY_FILES)))
773$(info $(dir $(lastword $(MAKEFILE_LIST))))
774$(info $(dir $(lastword $(PRODUCT_COPY_FILES))))
775$(info $(dir $(lastword $(foobar))))
776$(info $(abspath foo/bar))
777$(info $(notdir foo/bar))
Sasha Smundak3deb9682021-07-26 18:42:25 -0700778$(call add_soong_config_namespace,snsconfig)
779$(call add_soong_config_var_value,snsconfig,imagetype,odm_image)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700780$(call soong_config_set, snsconfig, foo, foo_value)
781$(call soong_config_append, snsconfig, bar, bar_value)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700782PRODUCT_COPY_FILES := $(call copy-files,$(wildcard foo*.mk),etc)
Sasha Smundak04453082021-08-17 18:14:41 -0700783PRODUCT_COPY_FILES := $(call product-copy-files-by-pattern,from/%,to/%,a b c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800784`,
785 expected: `load("//build/make/core:product_config.rbc", "rblf")
786
787def init(g, handle):
788 cfg = rblf.cfg(handle)
789 cfg["PRODUCT_COPY_FILES"] = rblf.addprefix("pfx-", "a b c")
790 cfg["PRODUCT_COPY_FILES"] = rblf.addsuffix(".sff", "a b c")
791 cfg["PRODUCT_NAME"] = ((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " ")).split()[0]
Sasha Smundak35434ed2021-11-05 16:29:56 -0700792 rblf.mkinfo("product.mk", rblf.mkpatsubst("%.pub", "%s%%" % cfg["PRODUCT_NAME"], g.get("PRODUCT_ADB_KEYS", "")))
Sasha Smundak16e07732021-07-23 11:38:23 -0700793 rblf.mkinfo("product.mk", rblf.dir("foo/bar"))
794 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][0])
795 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][-1])
796 rblf.mkinfo("product.mk", rblf.dir("product.mk"))
797 rblf.mkinfo("product.mk", rblf.dir(cfg["PRODUCT_COPY_FILES"][-1]))
798 rblf.mkinfo("product.mk", rblf.dir((_foobar).split()[-1]))
799 rblf.mkinfo("product.mk", rblf.abspath("foo/bar"))
800 rblf.mkinfo("product.mk", rblf.notdir("foo/bar"))
Sasha Smundak65b547e2021-09-17 15:35:41 -0700801 rblf.soong_config_namespace(g, "snsconfig")
802 rblf.soong_config_set(g, "snsconfig", "imagetype", "odm_image")
803 rblf.soong_config_set(g, "snsconfig", "foo", "foo_value")
804 rblf.soong_config_append(g, "snsconfig", "bar", "bar_value")
Sasha Smundak3deb9682021-07-26 18:42:25 -0700805 cfg["PRODUCT_COPY_FILES"] = rblf.copy_files(rblf.expand_wildcard("foo*.mk"), "etc")
Sasha Smundak04453082021-08-17 18:14:41 -0700806 cfg["PRODUCT_COPY_FILES"] = rblf.product_copy_files_by_pattern("from/%", "to/%", "a b c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800807`,
808 },
809 {
Sasha Smundak9d011ab2021-07-09 16:00:57 -0700810 desc: "subst in list",
811 mkname: "product.mk",
812 in: `
813files = $(call find-copy-subdir-files,*,from,to)
814PRODUCT_COPY_FILES += $(subst foo,bar,$(files))
815`,
816 expected: `load("//build/make/core:product_config.rbc", "rblf")
817
818def init(g, handle):
819 cfg = rblf.cfg(handle)
820 _files = rblf.find_and_copy("*", "from", "to")
821 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
822 cfg["PRODUCT_COPY_FILES"] += rblf.mksubst("foo", "bar", _files)
823`,
824 },
825 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800826 desc: "assignment flavors",
827 mkname: "product.mk",
828 in: `
829PRODUCT_LIST1 := a
830PRODUCT_LIST2 += a
831PRODUCT_LIST1 += b
832PRODUCT_LIST2 += b
833PRODUCT_LIST3 ?= a
834PRODUCT_LIST1 = c
835PLATFORM_LIST += x
836PRODUCT_PACKAGES := $(PLATFORM_LIST)
837`,
838 expected: `load("//build/make/core:product_config.rbc", "rblf")
839
840def init(g, handle):
841 cfg = rblf.cfg(handle)
842 cfg["PRODUCT_LIST1"] = ["a"]
843 rblf.setdefault(handle, "PRODUCT_LIST2")
844 cfg["PRODUCT_LIST2"] += ["a"]
845 cfg["PRODUCT_LIST1"] += ["b"]
846 cfg["PRODUCT_LIST2"] += ["b"]
847 if cfg.get("PRODUCT_LIST3") == None:
848 cfg["PRODUCT_LIST3"] = ["a"]
849 cfg["PRODUCT_LIST1"] = ["c"]
850 g.setdefault("PLATFORM_LIST", [])
851 g["PLATFORM_LIST"] += ["x"]
852 cfg["PRODUCT_PACKAGES"] = g["PLATFORM_LIST"][:]
853`,
854 },
855 {
856 desc: "assigment flavors2",
857 mkname: "product.mk",
858 in: `
859PRODUCT_LIST1 = a
860ifeq (0,1)
861 PRODUCT_LIST1 += b
862 PRODUCT_LIST2 += b
863endif
864PRODUCT_LIST1 += c
865PRODUCT_LIST2 += c
866`,
867 expected: `load("//build/make/core:product_config.rbc", "rblf")
868
869def init(g, handle):
870 cfg = rblf.cfg(handle)
871 cfg["PRODUCT_LIST1"] = ["a"]
872 if "0" == "1":
873 cfg["PRODUCT_LIST1"] += ["b"]
874 rblf.setdefault(handle, "PRODUCT_LIST2")
875 cfg["PRODUCT_LIST2"] += ["b"]
876 cfg["PRODUCT_LIST1"] += ["c"]
877 rblf.setdefault(handle, "PRODUCT_LIST2")
878 cfg["PRODUCT_LIST2"] += ["c"]
879`,
880 },
881 {
Sasha Smundak3deb9682021-07-26 18:42:25 -0700882 desc: "soong namespace assignments",
883 mkname: "product.mk",
884 in: `
885SOONG_CONFIG_NAMESPACES += cvd
886SOONG_CONFIG_cvd += launch_configs
Sasha Smundak65b547e2021-09-17 15:35:41 -0700887SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
Sasha Smundak3deb9682021-07-26 18:42:25 -0700888SOONG_CONFIG_cvd += grub_config
889SOONG_CONFIG_cvd_grub_config += grub.cfg
Sasha Smundak65b547e2021-09-17 15:35:41 -0700890x := $(SOONG_CONFIG_cvd_grub_config)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700891`,
892 expected: `load("//build/make/core:product_config.rbc", "rblf")
893
894def init(g, handle):
895 cfg = rblf.cfg(handle)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700896 rblf.soong_config_namespace(g, "cvd")
897 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
898 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
Sasha Smundak422b6142021-11-11 18:31:59 -0800899 rblf.mk2rbc_error("product.mk:7", "SOONG_CONFIG_ variables cannot be referenced, use soong_config_get instead: SOONG_CONFIG_cvd_grub_config")
Sasha Smundak3deb9682021-07-26 18:42:25 -0700900`,
Cole Faustc00184e2021-11-08 12:08:57 -0800901 }, {
902 desc: "soong namespace accesses",
903 mkname: "product.mk",
904 in: `
905SOONG_CONFIG_NAMESPACES += cvd
906SOONG_CONFIG_cvd += launch_configs
907SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
908SOONG_CONFIG_cvd += grub_config
909SOONG_CONFIG_cvd_grub_config += grub.cfg
910x := $(call soong_config_get,cvd,grub_config)
911`,
912 expected: `load("//build/make/core:product_config.rbc", "rblf")
913
914def init(g, handle):
915 cfg = rblf.cfg(handle)
916 rblf.soong_config_namespace(g, "cvd")
917 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
918 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
919 _x = rblf.soong_config_get(g, "cvd", "grub_config")
920`,
Sasha Smundak3deb9682021-07-26 18:42:25 -0700921 },
922 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800923 desc: "string split",
924 mkname: "product.mk",
925 in: `
926PRODUCT_LIST1 = a
927local = b
928local += c
929FOO = d
930FOO += e
931PRODUCT_LIST1 += $(local)
932PRODUCT_LIST1 += $(FOO)
933`,
934 expected: `load("//build/make/core:product_config.rbc", "rblf")
935
936def init(g, handle):
937 cfg = rblf.cfg(handle)
938 cfg["PRODUCT_LIST1"] = ["a"]
939 _local = "b"
940 _local += " " + "c"
941 g["FOO"] = "d"
942 g["FOO"] += " " + "e"
943 cfg["PRODUCT_LIST1"] += (_local).split()
944 cfg["PRODUCT_LIST1"] += (g["FOO"]).split()
945`,
946 },
947 {
948 desc: "apex_jars",
949 mkname: "product.mk",
950 in: `
951PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex
952`,
953 expected: `load("//build/make/core:product_config.rbc", "rblf")
954
955def init(g, handle):
956 cfg = rblf.cfg(handle)
957 cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) +
958 ["framework-minus-apex"])
959`,
960 },
961 {
962 desc: "strip function",
963 mkname: "product.mk",
964 in: `
965ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),)
966 PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress)
967endif
968`,
969 expected: `load("//build/make/core:product_config.rbc", "rblf")
970
971def init(g, handle):
972 cfg = rblf.cfg(handle)
973 if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []):
974 cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split()
975`,
976 },
977 {
978 desc: "strip func in condition",
979 mkname: "product.mk",
980 in: `
981ifneq ($(strip $(TARGET_VENDOR)),)
982endif
983`,
984 expected: `load("//build/make/core:product_config.rbc", "rblf")
985
986def init(g, handle):
987 cfg = rblf.cfg(handle)
Sasha Smundak0554d762021-07-08 18:26:12 -0700988 if rblf.mkstrip(g.get("TARGET_VENDOR", "")):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800989 pass
990`,
991 },
992 {
993 desc: "ref after set",
994 mkname: "product.mk",
995 in: `
996PRODUCT_ADB_KEYS:=value
997FOO := $(PRODUCT_ADB_KEYS)
998ifneq (,$(PRODUCT_ADB_KEYS))
999endif
1000`,
1001 expected: `load("//build/make/core:product_config.rbc", "rblf")
1002
1003def init(g, handle):
1004 cfg = rblf.cfg(handle)
1005 g["PRODUCT_ADB_KEYS"] = "value"
1006 g["FOO"] = g["PRODUCT_ADB_KEYS"]
1007 if g["PRODUCT_ADB_KEYS"]:
1008 pass
1009`,
1010 },
1011 {
1012 desc: "ref before set",
1013 mkname: "product.mk",
1014 in: `
1015V1 := $(PRODUCT_ADB_KEYS)
1016ifeq (,$(PRODUCT_ADB_KEYS))
1017 V2 := $(PRODUCT_ADB_KEYS)
1018 PRODUCT_ADB_KEYS:=foo
1019 V3 := $(PRODUCT_ADB_KEYS)
1020endif`,
1021 expected: `load("//build/make/core:product_config.rbc", "rblf")
1022
1023def init(g, handle):
1024 cfg = rblf.cfg(handle)
1025 g["V1"] = g.get("PRODUCT_ADB_KEYS", "")
1026 if not g.get("PRODUCT_ADB_KEYS", ""):
1027 g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
1028 g["PRODUCT_ADB_KEYS"] = "foo"
1029 g["V3"] = g["PRODUCT_ADB_KEYS"]
1030`,
1031 },
Sasha Smundak6609ba72021-07-22 18:32:56 -07001032 {
1033 desc: "Dynamic inherit path",
1034 mkname: "product.mk",
1035 in: `
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001036MY_PATH:=foo
Sasha Smundak6609ba72021-07-22 18:32:56 -07001037$(call inherit-product,vendor/$(MY_PATH)/cfg.mk)
1038`,
1039 expected: `load("//build/make/core:product_config.rbc", "rblf")
1040load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1041load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init")
1042
1043def init(g, handle):
1044 cfg = rblf.cfg(handle)
1045 g["MY_PATH"] = "foo"
1046 _entry = {
1047 "vendor/foo1/cfg.mk": ("_cfg", _cfg_init),
1048 "vendor/bar/baz/cfg.mk": ("_cfg1", _cfg1_init),
1049 }.get("vendor/%s/cfg.mk" % g["MY_PATH"])
1050 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1051 if not _varmod_init:
Cole Faust7321b092021-12-21 16:11:16 -08001052 rblf.mkerror("product.mk", "Cannot find %s" % ("vendor/%s/cfg.mk" % g["MY_PATH"]))
Sasha Smundak6609ba72021-07-22 18:32:56 -07001053 rblf.inherit(handle, _varmod, _varmod_init)
1054`,
1055 },
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001056 {
1057 desc: "Dynamic inherit with hint",
1058 mkname: "product.mk",
1059 in: `
1060MY_PATH:=foo
1061#RBC# include_top vendor/foo1
1062$(call inherit-product,$(MY_PATH)/cfg.mk)
1063`,
1064 expected: `load("//build/make/core:product_config.rbc", "rblf")
1065load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1066
1067def init(g, handle):
1068 cfg = rblf.cfg(handle)
1069 g["MY_PATH"] = "foo"
1070 #RBC# include_top vendor/foo1
1071 _entry = {
1072 "vendor/foo1/cfg.mk": ("_cfg", _cfg_init),
1073 }.get("%s/cfg.mk" % g["MY_PATH"])
1074 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1075 if not _varmod_init:
Cole Faust7321b092021-12-21 16:11:16 -08001076 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001077 rblf.inherit(handle, _varmod, _varmod_init)
1078`,
1079 },
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001080 {
Cole Faustf7ed5342021-12-21 14:15:12 -08001081 desc: "Dynamic inherit with duplicated hint",
1082 mkname: "product.mk",
1083 in: `
1084MY_PATH:=foo
1085#RBC# include_top vendor/foo1
1086$(call inherit-product,$(MY_PATH)/cfg.mk)
1087#RBC# include_top vendor/foo1
1088$(call inherit-product,$(MY_PATH)/cfg.mk)
1089`,
1090 expected: `load("//build/make/core:product_config.rbc", "rblf")
1091load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1092
1093def init(g, handle):
1094 cfg = rblf.cfg(handle)
1095 g["MY_PATH"] = "foo"
1096 #RBC# include_top vendor/foo1
1097 _entry = {
1098 "vendor/foo1/cfg.mk": ("_cfg", _cfg_init),
1099 }.get("%s/cfg.mk" % g["MY_PATH"])
1100 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1101 if not _varmod_init:
Cole Faust7321b092021-12-21 16:11:16 -08001102 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
Cole Faustf7ed5342021-12-21 14:15:12 -08001103 rblf.inherit(handle, _varmod, _varmod_init)
1104 #RBC# include_top vendor/foo1
1105 _entry = {
1106 "vendor/foo1/cfg.mk": ("_cfg", _cfg_init),
1107 }.get("%s/cfg.mk" % g["MY_PATH"])
1108 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1109 if not _varmod_init:
Cole Faust7321b092021-12-21 16:11:16 -08001110 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
Cole Faustf7ed5342021-12-21 14:15:12 -08001111 rblf.inherit(handle, _varmod, _varmod_init)
1112`,
1113 },
1114 {
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001115 desc: "Ignore make rules",
1116 mkname: "product.mk",
1117 in: `
1118foo: foo.c
1119 gcc -o $@ $*`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001120 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001121
1122def init(g, handle):
1123 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001124 rblf.mk2rbc_error("product.mk:2", "unsupported line rule: foo: foo.c\n#gcc -o $@ $*")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001125`,
1126 },
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001127 {
1128 desc: "Flag override",
1129 mkname: "product.mk",
1130 in: `
1131override FOO:=`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001132 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001133
1134def init(g, handle):
1135 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001136 rblf.mk2rbc_error("product.mk:2", "cannot handle override directive")
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001137 g["override FOO"] = ""
Sasha Smundak422b6142021-11-11 18:31:59 -08001138`,
1139 },
1140 {
1141 desc: "Bad expression",
1142 mkname: "build/product.mk",
1143 in: `
1144ifeq (,$(call foobar))
1145endif
1146`,
1147 expected: `load("//build/make/core:product_config.rbc", "rblf")
1148
1149def init(g, handle):
1150 cfg = rblf.cfg(handle)
1151 if rblf.mk2rbc_error("build/product.mk:2", "cannot handle invoking foobar"):
1152 pass
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001153`,
1154 },
Cole Faust4eadba72021-12-07 11:54:52 -08001155 {
1156 desc: "if expression",
1157 mkname: "product.mk",
1158 in: `
1159TEST_VAR := foo
1160TEST_VAR_LIST := foo
1161TEST_VAR_LIST += bar
1162TEST_VAR_2 := $(if $(TEST_VAR),bar)
1163TEST_VAR_3 := $(if $(TEST_VAR),bar,baz)
1164TEST_VAR_3 := $(if $(TEST_VAR),$(TEST_VAR_LIST))
1165`,
1166 expected: `load("//build/make/core:product_config.rbc", "rblf")
1167
1168def init(g, handle):
1169 cfg = rblf.cfg(handle)
1170 g["TEST_VAR"] = "foo"
1171 g["TEST_VAR_LIST"] = ["foo"]
1172 g["TEST_VAR_LIST"] += ["bar"]
1173 g["TEST_VAR_2"] = ("bar" if g["TEST_VAR"] else "")
1174 g["TEST_VAR_3"] = ("bar" if g["TEST_VAR"] else "baz")
1175 g["TEST_VAR_3"] = (g["TEST_VAR_LIST"] if g["TEST_VAR"] else [])
1176`,
1177 },
Cole Faustc36c9622021-12-07 15:20:45 -08001178 {
1179 desc: "substitution references",
1180 mkname: "product.mk",
1181 in: `
1182SOURCES := foo.c bar.c
1183OBJECTS := $(SOURCES:.c=.o)
1184OBJECTS2 := $(SOURCES:%.c=%.o)
1185`,
1186 expected: `load("//build/make/core:product_config.rbc", "rblf")
1187
1188def init(g, handle):
1189 cfg = rblf.cfg(handle)
1190 g["SOURCES"] = "foo.c bar.c"
1191 g["OBJECTS"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1192 g["OBJECTS2"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1193`,
1194 },
Cole Faustb0d32ab2021-12-09 14:00:59 -08001195 {
1196 desc: "foreach expressions",
1197 mkname: "product.mk",
1198 in: `
1199BOOT_KERNEL_MODULES := foo.ko bar.ko
1200BOOT_KERNEL_MODULES_FILTER := $(foreach m,$(BOOT_KERNEL_MODULES),%/$(m))
1201BOOT_KERNEL_MODULES_LIST := foo.ko
1202BOOT_KERNEL_MODULES_LIST += bar.ko
1203BOOT_KERNEL_MODULES_FILTER_2 := $(foreach m,$(BOOT_KERNEL_MODULES_LIST),%/$(m))
1204
1205`,
1206 expected: `load("//build/make/core:product_config.rbc", "rblf")
1207
1208def init(g, handle):
1209 cfg = rblf.cfg(handle)
1210 g["BOOT_KERNEL_MODULES"] = "foo.ko bar.ko"
1211 g["BOOT_KERNEL_MODULES_FILTER"] = ["%%/%s" % m for m in rblf.words(g["BOOT_KERNEL_MODULES"])]
1212 g["BOOT_KERNEL_MODULES_LIST"] = ["foo.ko"]
1213 g["BOOT_KERNEL_MODULES_LIST"] += ["bar.ko"]
1214 g["BOOT_KERNEL_MODULES_FILTER_2"] = ["%%/%s" % m for m in g["BOOT_KERNEL_MODULES_LIST"]]
1215`,
1216 },
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001217}
1218
1219var known_variables = []struct {
1220 name string
1221 class varClass
1222 starlarkType
1223}{
Cole Faustf1f44d32021-11-16 14:52:12 -08001224 {"NATIVE_COVERAGE", VarClassSoong, starlarkTypeBool},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001225 {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
1226 {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
1227 {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
1228 {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
1229 {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
1230 {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
1231 {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
1232 {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
1233 {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
1234 {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
1235 {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
1236 {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
1237 {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
1238 {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
1239}
1240
Sasha Smundak6609ba72021-07-22 18:32:56 -07001241type testMakefileFinder struct {
1242 fs fs.FS
1243 root string
1244 files []string
1245}
1246
1247func (t *testMakefileFinder) Find(root string) []string {
1248 if t.files != nil || root == t.root {
1249 return t.files
1250 }
1251 t.files = make([]string, 0)
1252 fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error {
1253 if err != nil {
1254 return err
1255 }
1256 if d.IsDir() {
1257 base := filepath.Base(path)
1258 if base[0] == '.' && len(base) > 1 {
1259 return fs.SkipDir
1260 }
1261 return nil
1262 }
1263 if strings.HasSuffix(path, ".mk") {
1264 t.files = append(t.files, path)
1265 }
1266 return nil
1267 })
1268 return t.files
1269}
1270
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001271func TestGood(t *testing.T) {
1272 for _, v := range known_variables {
1273 KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
1274 }
Sasha Smundak6609ba72021-07-22 18:32:56 -07001275 fs := NewFindMockFS([]string{
1276 "vendor/foo1/cfg.mk",
1277 "vendor/bar/baz/cfg.mk",
1278 "part.mk",
1279 "foo/font.mk",
1280 "bar/font.mk",
1281 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001282 for _, test := range testCases {
1283 t.Run(test.desc,
1284 func(t *testing.T) {
1285 ss, err := Convert(Request{
Sasha Smundak422b6142021-11-11 18:31:59 -08001286 MkFile: test.mkname,
1287 Reader: bytes.NewBufferString(test.in),
1288 RootDir: ".",
1289 OutputSuffix: ".star",
1290 SourceFS: fs,
1291 MakefileFinder: &testMakefileFinder{fs: fs},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001292 })
1293 if err != nil {
1294 t.Error(err)
1295 return
1296 }
1297 got := ss.String()
1298 if got != test.expected {
1299 t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
1300 strings.ReplaceAll(test.expected, "\n", "␤\n"),
1301 strings.ReplaceAll(got, "\n", "␤\n"))
1302 }
1303 })
1304 }
1305}