blob: e1bb2bbee3575c8948695d5f70669c0dac82a933 [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: `
636ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),)
637endif
638`,
639 expected: `load("//build/make/core:product_config.rbc", "rblf")
640
641def init(g, handle):
642 cfg = rblf.cfg(handle)
643 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
644 pass
645`,
646 },
647 {
648 desc: "rhs call",
649 mkname: "product.mk",
650 in: `
651PRODUCT_COPY_FILES = $(call add-to-product-copy-files-if-exists, path:distpath) \
652 $(call find-copy-subdir-files, *, fromdir, todir) $(wildcard foo.*)
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.copy_if_exists("path:distpath") +
659 rblf.find_and_copy("*", "fromdir", "todir") +
660 rblf.expand_wildcard("foo.*"))
661`,
662 },
663 {
664 desc: "inferred type",
665 mkname: "product.mk",
666 in: `
667HIKEY_MODS := $(wildcard foo/*.ko)
668BOARD_VENDOR_KERNEL_MODULES += $(HIKEY_MODS)
669`,
670 expected: `load("//build/make/core:product_config.rbc", "rblf")
671
672def init(g, handle):
673 cfg = rblf.cfg(handle)
674 g["HIKEY_MODS"] = rblf.expand_wildcard("foo/*.ko")
675 g.setdefault("BOARD_VENDOR_KERNEL_MODULES", [])
676 g["BOARD_VENDOR_KERNEL_MODULES"] += g["HIKEY_MODS"]
677`,
678 },
679 {
680 desc: "list with vars",
681 mkname: "product.mk",
682 in: `
683PRODUCT_COPY_FILES += path1:$(TARGET_PRODUCT)/path1 $(PRODUCT_MODEL)/path2:$(TARGET_PRODUCT)/path2
684`,
685 expected: `load("//build/make/core:product_config.rbc", "rblf")
686
687def init(g, handle):
688 cfg = rblf.cfg(handle)
689 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
690 cfg["PRODUCT_COPY_FILES"] += (("path1:%s/path1" % g["TARGET_PRODUCT"]).split() +
691 ("%s/path2:%s/path2" % (cfg.get("PRODUCT_MODEL", ""), g["TARGET_PRODUCT"])).split())
692`,
693 },
694 {
695 desc: "misc calls",
696 mkname: "product.mk",
697 in: `
698$(call enforce-product-packages-exist,)
699$(call enforce-product-packages-exist, foo)
700$(call require-artifacts-in-path, foo, bar)
701$(call require-artifacts-in-path-relaxed, foo, bar)
Sasha Smundakd6797852021-11-15 13:01:53 -0800702$(call dist-for-goals, goal, from:to)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800703`,
704 expected: `load("//build/make/core:product_config.rbc", "rblf")
705
706def init(g, handle):
707 cfg = rblf.cfg(handle)
708 rblf.enforce_product_packages_exist("")
709 rblf.enforce_product_packages_exist("foo")
710 rblf.require_artifacts_in_path("foo", "bar")
711 rblf.require_artifacts_in_path_relaxed("foo", "bar")
Sasha Smundakd6797852021-11-15 13:01:53 -0800712 rblf.mkdist_for_goals(g, "goal", "from:to")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800713`,
714 },
715 {
716 desc: "list with functions",
717 mkname: "product.mk",
718 in: `
719PRODUCT_COPY_FILES := $(call find-copy-subdir-files,*.kl,from1,to1) \
720 $(call find-copy-subdir-files,*.kc,from2,to2) \
721 foo bar
722`,
723 expected: `load("//build/make/core:product_config.rbc", "rblf")
724
725def init(g, handle):
726 cfg = rblf.cfg(handle)
727 cfg["PRODUCT_COPY_FILES"] = (rblf.find_and_copy("*.kl", "from1", "to1") +
728 rblf.find_and_copy("*.kc", "from2", "to2") +
729 [
730 "foo",
731 "bar",
732 ])
733`,
734 },
735 {
736 desc: "Text functions",
737 mkname: "product.mk",
738 in: `
739PRODUCT_COPY_FILES := $(addprefix pfx-,a b c)
740PRODUCT_COPY_FILES := $(addsuffix .sff, a b c)
741PRODUCT_NAME := $(word 1, $(subst ., ,$(TARGET_BOARD_PLATFORM)))
Sasha Smundak35434ed2021-11-05 16:29:56 -0700742$(info $(patsubst %.pub,$(PRODUCT_NAME)%,$(PRODUCT_ADB_KEYS)))
Sasha Smundak16e07732021-07-23 11:38:23 -0700743$(info $(dir foo/bar))
744$(info $(firstword $(PRODUCT_COPY_FILES)))
745$(info $(lastword $(PRODUCT_COPY_FILES)))
746$(info $(dir $(lastword $(MAKEFILE_LIST))))
747$(info $(dir $(lastword $(PRODUCT_COPY_FILES))))
748$(info $(dir $(lastword $(foobar))))
749$(info $(abspath foo/bar))
750$(info $(notdir foo/bar))
Sasha Smundak3deb9682021-07-26 18:42:25 -0700751$(call add_soong_config_namespace,snsconfig)
752$(call add_soong_config_var_value,snsconfig,imagetype,odm_image)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700753$(call soong_config_set, snsconfig, foo, foo_value)
754$(call soong_config_append, snsconfig, bar, bar_value)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700755PRODUCT_COPY_FILES := $(call copy-files,$(wildcard foo*.mk),etc)
Sasha Smundak04453082021-08-17 18:14:41 -0700756PRODUCT_COPY_FILES := $(call product-copy-files-by-pattern,from/%,to/%,a b c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800757`,
758 expected: `load("//build/make/core:product_config.rbc", "rblf")
759
760def init(g, handle):
761 cfg = rblf.cfg(handle)
762 cfg["PRODUCT_COPY_FILES"] = rblf.addprefix("pfx-", "a b c")
763 cfg["PRODUCT_COPY_FILES"] = rblf.addsuffix(".sff", "a b c")
764 cfg["PRODUCT_NAME"] = ((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " ")).split()[0]
Sasha Smundak35434ed2021-11-05 16:29:56 -0700765 rblf.mkinfo("product.mk", rblf.mkpatsubst("%.pub", "%s%%" % cfg["PRODUCT_NAME"], g.get("PRODUCT_ADB_KEYS", "")))
Sasha Smundak16e07732021-07-23 11:38:23 -0700766 rblf.mkinfo("product.mk", rblf.dir("foo/bar"))
767 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][0])
768 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][-1])
769 rblf.mkinfo("product.mk", rblf.dir("product.mk"))
770 rblf.mkinfo("product.mk", rblf.dir(cfg["PRODUCT_COPY_FILES"][-1]))
771 rblf.mkinfo("product.mk", rblf.dir((_foobar).split()[-1]))
772 rblf.mkinfo("product.mk", rblf.abspath("foo/bar"))
773 rblf.mkinfo("product.mk", rblf.notdir("foo/bar"))
Sasha Smundak65b547e2021-09-17 15:35:41 -0700774 rblf.soong_config_namespace(g, "snsconfig")
775 rblf.soong_config_set(g, "snsconfig", "imagetype", "odm_image")
776 rblf.soong_config_set(g, "snsconfig", "foo", "foo_value")
777 rblf.soong_config_append(g, "snsconfig", "bar", "bar_value")
Sasha Smundak3deb9682021-07-26 18:42:25 -0700778 cfg["PRODUCT_COPY_FILES"] = rblf.copy_files(rblf.expand_wildcard("foo*.mk"), "etc")
Sasha Smundak04453082021-08-17 18:14:41 -0700779 cfg["PRODUCT_COPY_FILES"] = rblf.product_copy_files_by_pattern("from/%", "to/%", "a b c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800780`,
781 },
782 {
Sasha Smundak9d011ab2021-07-09 16:00:57 -0700783 desc: "subst in list",
784 mkname: "product.mk",
785 in: `
786files = $(call find-copy-subdir-files,*,from,to)
787PRODUCT_COPY_FILES += $(subst foo,bar,$(files))
788`,
789 expected: `load("//build/make/core:product_config.rbc", "rblf")
790
791def init(g, handle):
792 cfg = rblf.cfg(handle)
793 _files = rblf.find_and_copy("*", "from", "to")
794 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
795 cfg["PRODUCT_COPY_FILES"] += rblf.mksubst("foo", "bar", _files)
796`,
797 },
798 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800799 desc: "assignment flavors",
800 mkname: "product.mk",
801 in: `
802PRODUCT_LIST1 := a
803PRODUCT_LIST2 += a
804PRODUCT_LIST1 += b
805PRODUCT_LIST2 += b
806PRODUCT_LIST3 ?= a
807PRODUCT_LIST1 = c
808PLATFORM_LIST += x
809PRODUCT_PACKAGES := $(PLATFORM_LIST)
810`,
811 expected: `load("//build/make/core:product_config.rbc", "rblf")
812
813def init(g, handle):
814 cfg = rblf.cfg(handle)
815 cfg["PRODUCT_LIST1"] = ["a"]
816 rblf.setdefault(handle, "PRODUCT_LIST2")
817 cfg["PRODUCT_LIST2"] += ["a"]
818 cfg["PRODUCT_LIST1"] += ["b"]
819 cfg["PRODUCT_LIST2"] += ["b"]
820 if cfg.get("PRODUCT_LIST3") == None:
821 cfg["PRODUCT_LIST3"] = ["a"]
822 cfg["PRODUCT_LIST1"] = ["c"]
823 g.setdefault("PLATFORM_LIST", [])
824 g["PLATFORM_LIST"] += ["x"]
825 cfg["PRODUCT_PACKAGES"] = g["PLATFORM_LIST"][:]
826`,
827 },
828 {
829 desc: "assigment flavors2",
830 mkname: "product.mk",
831 in: `
832PRODUCT_LIST1 = a
833ifeq (0,1)
834 PRODUCT_LIST1 += b
835 PRODUCT_LIST2 += b
836endif
837PRODUCT_LIST1 += c
838PRODUCT_LIST2 += c
839`,
840 expected: `load("//build/make/core:product_config.rbc", "rblf")
841
842def init(g, handle):
843 cfg = rblf.cfg(handle)
844 cfg["PRODUCT_LIST1"] = ["a"]
845 if "0" == "1":
846 cfg["PRODUCT_LIST1"] += ["b"]
847 rblf.setdefault(handle, "PRODUCT_LIST2")
848 cfg["PRODUCT_LIST2"] += ["b"]
849 cfg["PRODUCT_LIST1"] += ["c"]
850 rblf.setdefault(handle, "PRODUCT_LIST2")
851 cfg["PRODUCT_LIST2"] += ["c"]
852`,
853 },
854 {
Sasha Smundak3deb9682021-07-26 18:42:25 -0700855 desc: "soong namespace assignments",
856 mkname: "product.mk",
857 in: `
858SOONG_CONFIG_NAMESPACES += cvd
859SOONG_CONFIG_cvd += launch_configs
Sasha Smundak65b547e2021-09-17 15:35:41 -0700860SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
Sasha Smundak3deb9682021-07-26 18:42:25 -0700861SOONG_CONFIG_cvd += grub_config
862SOONG_CONFIG_cvd_grub_config += grub.cfg
Sasha Smundak65b547e2021-09-17 15:35:41 -0700863x := $(SOONG_CONFIG_cvd_grub_config)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700864`,
865 expected: `load("//build/make/core:product_config.rbc", "rblf")
866
867def init(g, handle):
868 cfg = rblf.cfg(handle)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700869 rblf.soong_config_namespace(g, "cvd")
870 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
871 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
Sasha Smundak422b6142021-11-11 18:31:59 -0800872 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 -0700873`,
Cole Faustc00184e2021-11-08 12:08:57 -0800874 }, {
875 desc: "soong namespace accesses",
876 mkname: "product.mk",
877 in: `
878SOONG_CONFIG_NAMESPACES += cvd
879SOONG_CONFIG_cvd += launch_configs
880SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
881SOONG_CONFIG_cvd += grub_config
882SOONG_CONFIG_cvd_grub_config += grub.cfg
883x := $(call soong_config_get,cvd,grub_config)
884`,
885 expected: `load("//build/make/core:product_config.rbc", "rblf")
886
887def init(g, handle):
888 cfg = rblf.cfg(handle)
889 rblf.soong_config_namespace(g, "cvd")
890 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
891 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
892 _x = rblf.soong_config_get(g, "cvd", "grub_config")
893`,
Sasha Smundak3deb9682021-07-26 18:42:25 -0700894 },
895 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800896 desc: "string split",
897 mkname: "product.mk",
898 in: `
899PRODUCT_LIST1 = a
900local = b
901local += c
902FOO = d
903FOO += e
904PRODUCT_LIST1 += $(local)
905PRODUCT_LIST1 += $(FOO)
906`,
907 expected: `load("//build/make/core:product_config.rbc", "rblf")
908
909def init(g, handle):
910 cfg = rblf.cfg(handle)
911 cfg["PRODUCT_LIST1"] = ["a"]
912 _local = "b"
913 _local += " " + "c"
914 g["FOO"] = "d"
915 g["FOO"] += " " + "e"
916 cfg["PRODUCT_LIST1"] += (_local).split()
917 cfg["PRODUCT_LIST1"] += (g["FOO"]).split()
918`,
919 },
920 {
921 desc: "apex_jars",
922 mkname: "product.mk",
923 in: `
924PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex
925`,
926 expected: `load("//build/make/core:product_config.rbc", "rblf")
927
928def init(g, handle):
929 cfg = rblf.cfg(handle)
930 cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) +
931 ["framework-minus-apex"])
932`,
933 },
934 {
935 desc: "strip function",
936 mkname: "product.mk",
937 in: `
938ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),)
939 PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress)
940endif
941`,
942 expected: `load("//build/make/core:product_config.rbc", "rblf")
943
944def init(g, handle):
945 cfg = rblf.cfg(handle)
946 if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []):
947 cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split()
948`,
949 },
950 {
951 desc: "strip func in condition",
952 mkname: "product.mk",
953 in: `
954ifneq ($(strip $(TARGET_VENDOR)),)
955endif
956`,
957 expected: `load("//build/make/core:product_config.rbc", "rblf")
958
959def init(g, handle):
960 cfg = rblf.cfg(handle)
Sasha Smundak0554d762021-07-08 18:26:12 -0700961 if rblf.mkstrip(g.get("TARGET_VENDOR", "")):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800962 pass
963`,
964 },
965 {
966 desc: "ref after set",
967 mkname: "product.mk",
968 in: `
969PRODUCT_ADB_KEYS:=value
970FOO := $(PRODUCT_ADB_KEYS)
971ifneq (,$(PRODUCT_ADB_KEYS))
972endif
973`,
974 expected: `load("//build/make/core:product_config.rbc", "rblf")
975
976def init(g, handle):
977 cfg = rblf.cfg(handle)
978 g["PRODUCT_ADB_KEYS"] = "value"
979 g["FOO"] = g["PRODUCT_ADB_KEYS"]
980 if g["PRODUCT_ADB_KEYS"]:
981 pass
982`,
983 },
984 {
985 desc: "ref before set",
986 mkname: "product.mk",
987 in: `
988V1 := $(PRODUCT_ADB_KEYS)
989ifeq (,$(PRODUCT_ADB_KEYS))
990 V2 := $(PRODUCT_ADB_KEYS)
991 PRODUCT_ADB_KEYS:=foo
992 V3 := $(PRODUCT_ADB_KEYS)
993endif`,
994 expected: `load("//build/make/core:product_config.rbc", "rblf")
995
996def init(g, handle):
997 cfg = rblf.cfg(handle)
998 g["V1"] = g.get("PRODUCT_ADB_KEYS", "")
999 if not g.get("PRODUCT_ADB_KEYS", ""):
1000 g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
1001 g["PRODUCT_ADB_KEYS"] = "foo"
1002 g["V3"] = g["PRODUCT_ADB_KEYS"]
1003`,
1004 },
Sasha Smundak6609ba72021-07-22 18:32:56 -07001005 {
1006 desc: "Dynamic inherit path",
1007 mkname: "product.mk",
1008 in: `
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001009MY_PATH:=foo
Sasha Smundak6609ba72021-07-22 18:32:56 -07001010$(call inherit-product,vendor/$(MY_PATH)/cfg.mk)
1011`,
1012 expected: `load("//build/make/core:product_config.rbc", "rblf")
1013load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1014load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init")
1015
1016def init(g, handle):
1017 cfg = rblf.cfg(handle)
1018 g["MY_PATH"] = "foo"
1019 _entry = {
1020 "vendor/foo1/cfg.mk": ("_cfg", _cfg_init),
1021 "vendor/bar/baz/cfg.mk": ("_cfg1", _cfg1_init),
1022 }.get("vendor/%s/cfg.mk" % g["MY_PATH"])
1023 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1024 if not _varmod_init:
1025 rblf.mkerror("cannot")
1026 rblf.inherit(handle, _varmod, _varmod_init)
1027`,
1028 },
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001029 {
1030 desc: "Dynamic inherit with hint",
1031 mkname: "product.mk",
1032 in: `
1033MY_PATH:=foo
1034#RBC# include_top vendor/foo1
1035$(call inherit-product,$(MY_PATH)/cfg.mk)
1036`,
1037 expected: `load("//build/make/core:product_config.rbc", "rblf")
1038load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1039
1040def init(g, handle):
1041 cfg = rblf.cfg(handle)
1042 g["MY_PATH"] = "foo"
1043 #RBC# include_top vendor/foo1
1044 _entry = {
1045 "vendor/foo1/cfg.mk": ("_cfg", _cfg_init),
1046 }.get("%s/cfg.mk" % g["MY_PATH"])
1047 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1048 if not _varmod_init:
1049 rblf.mkerror("cannot")
1050 rblf.inherit(handle, _varmod, _varmod_init)
1051`,
1052 },
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001053 {
1054 desc: "Ignore make rules",
1055 mkname: "product.mk",
1056 in: `
1057foo: foo.c
1058 gcc -o $@ $*`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001059 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001060
1061def init(g, handle):
1062 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001063 rblf.mk2rbc_error("product.mk:2", "unsupported line rule: foo: foo.c\n#gcc -o $@ $*")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001064`,
1065 },
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001066 {
1067 desc: "Flag override",
1068 mkname: "product.mk",
1069 in: `
1070override FOO:=`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001071 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001072
1073def init(g, handle):
1074 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001075 rblf.mk2rbc_error("product.mk:2", "cannot handle override directive")
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001076 g["override FOO"] = ""
Sasha Smundak422b6142021-11-11 18:31:59 -08001077`,
1078 },
1079 {
1080 desc: "Bad expression",
1081 mkname: "build/product.mk",
1082 in: `
1083ifeq (,$(call foobar))
1084endif
1085`,
1086 expected: `load("//build/make/core:product_config.rbc", "rblf")
1087
1088def init(g, handle):
1089 cfg = rblf.cfg(handle)
1090 if rblf.mk2rbc_error("build/product.mk:2", "cannot handle invoking foobar"):
1091 pass
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001092`,
1093 },
Cole Faust4eadba72021-12-07 11:54:52 -08001094 {
1095 desc: "if expression",
1096 mkname: "product.mk",
1097 in: `
1098TEST_VAR := foo
1099TEST_VAR_LIST := foo
1100TEST_VAR_LIST += bar
1101TEST_VAR_2 := $(if $(TEST_VAR),bar)
1102TEST_VAR_3 := $(if $(TEST_VAR),bar,baz)
1103TEST_VAR_3 := $(if $(TEST_VAR),$(TEST_VAR_LIST))
1104`,
1105 expected: `load("//build/make/core:product_config.rbc", "rblf")
1106
1107def init(g, handle):
1108 cfg = rblf.cfg(handle)
1109 g["TEST_VAR"] = "foo"
1110 g["TEST_VAR_LIST"] = ["foo"]
1111 g["TEST_VAR_LIST"] += ["bar"]
1112 g["TEST_VAR_2"] = ("bar" if g["TEST_VAR"] else "")
1113 g["TEST_VAR_3"] = ("bar" if g["TEST_VAR"] else "baz")
1114 g["TEST_VAR_3"] = (g["TEST_VAR_LIST"] if g["TEST_VAR"] else [])
1115`,
1116 },
Cole Faustc36c9622021-12-07 15:20:45 -08001117 {
1118 desc: "substitution references",
1119 mkname: "product.mk",
1120 in: `
1121SOURCES := foo.c bar.c
1122OBJECTS := $(SOURCES:.c=.o)
1123OBJECTS2 := $(SOURCES:%.c=%.o)
1124`,
1125 expected: `load("//build/make/core:product_config.rbc", "rblf")
1126
1127def init(g, handle):
1128 cfg = rblf.cfg(handle)
1129 g["SOURCES"] = "foo.c bar.c"
1130 g["OBJECTS"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1131 g["OBJECTS2"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1132`,
1133 },
Cole Faustb0d32ab2021-12-09 14:00:59 -08001134 {
1135 desc: "foreach expressions",
1136 mkname: "product.mk",
1137 in: `
1138BOOT_KERNEL_MODULES := foo.ko bar.ko
1139BOOT_KERNEL_MODULES_FILTER := $(foreach m,$(BOOT_KERNEL_MODULES),%/$(m))
1140BOOT_KERNEL_MODULES_LIST := foo.ko
1141BOOT_KERNEL_MODULES_LIST += bar.ko
1142BOOT_KERNEL_MODULES_FILTER_2 := $(foreach m,$(BOOT_KERNEL_MODULES_LIST),%/$(m))
1143
1144`,
1145 expected: `load("//build/make/core:product_config.rbc", "rblf")
1146
1147def init(g, handle):
1148 cfg = rblf.cfg(handle)
1149 g["BOOT_KERNEL_MODULES"] = "foo.ko bar.ko"
1150 g["BOOT_KERNEL_MODULES_FILTER"] = ["%%/%s" % m for m in rblf.words(g["BOOT_KERNEL_MODULES"])]
1151 g["BOOT_KERNEL_MODULES_LIST"] = ["foo.ko"]
1152 g["BOOT_KERNEL_MODULES_LIST"] += ["bar.ko"]
1153 g["BOOT_KERNEL_MODULES_FILTER_2"] = ["%%/%s" % m for m in g["BOOT_KERNEL_MODULES_LIST"]]
1154`,
1155 },
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001156}
1157
1158var known_variables = []struct {
1159 name string
1160 class varClass
1161 starlarkType
1162}{
Cole Faustf1f44d32021-11-16 14:52:12 -08001163 {"NATIVE_COVERAGE", VarClassSoong, starlarkTypeBool},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001164 {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
1165 {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
1166 {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
1167 {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
1168 {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
1169 {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
1170 {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
1171 {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
1172 {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
1173 {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
1174 {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
1175 {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
1176 {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
1177 {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
1178}
1179
Sasha Smundak6609ba72021-07-22 18:32:56 -07001180type testMakefileFinder struct {
1181 fs fs.FS
1182 root string
1183 files []string
1184}
1185
1186func (t *testMakefileFinder) Find(root string) []string {
1187 if t.files != nil || root == t.root {
1188 return t.files
1189 }
1190 t.files = make([]string, 0)
1191 fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error {
1192 if err != nil {
1193 return err
1194 }
1195 if d.IsDir() {
1196 base := filepath.Base(path)
1197 if base[0] == '.' && len(base) > 1 {
1198 return fs.SkipDir
1199 }
1200 return nil
1201 }
1202 if strings.HasSuffix(path, ".mk") {
1203 t.files = append(t.files, path)
1204 }
1205 return nil
1206 })
1207 return t.files
1208}
1209
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001210func TestGood(t *testing.T) {
1211 for _, v := range known_variables {
1212 KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
1213 }
Sasha Smundak6609ba72021-07-22 18:32:56 -07001214 fs := NewFindMockFS([]string{
1215 "vendor/foo1/cfg.mk",
1216 "vendor/bar/baz/cfg.mk",
1217 "part.mk",
1218 "foo/font.mk",
1219 "bar/font.mk",
1220 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001221 for _, test := range testCases {
1222 t.Run(test.desc,
1223 func(t *testing.T) {
1224 ss, err := Convert(Request{
Sasha Smundak422b6142021-11-11 18:31:59 -08001225 MkFile: test.mkname,
1226 Reader: bytes.NewBufferString(test.in),
1227 RootDir: ".",
1228 OutputSuffix: ".star",
1229 SourceFS: fs,
1230 MakefileFinder: &testMakefileFinder{fs: fs},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001231 })
1232 if err != nil {
1233 t.Error(err)
1234 return
1235 }
1236 got := ss.String()
1237 if got != test.expected {
1238 t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
1239 strings.ReplaceAll(test.expected, "\n", "␤\n"),
1240 strings.ReplaceAll(got, "\n", "␤\n"))
1241 }
1242 })
1243 }
1244}