blob: ce1a1f9b1553a730afe8684db2517682585ea0ac [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 Smundak6bc132a2022-01-10 17:02:16 -0800124$(call inherit-product, $(LOCAL_PATH)/part.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)
Cole Faust71514c02022-01-27 17:21:41 -0800134 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800135 if not _part1_init:
136 rblf.mkerror("product.mk", "Cannot find %s" % (":part1.star"))
Sasha Smundak868c5e32021-09-23 16:20:58 -0700137 rblf.inherit(handle, "part1", _part1_init)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800138 else:
139 # Comment
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800140 rblf.inherit(handle, "part", _part_init)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800141`,
142 },
143 {
144 desc: "Inherit configuration if it exists",
145 mkname: "product.mk",
146 in: `
147$(call inherit-product-if-exists, part.mk)
148`,
149 expected: `load("//build/make/core:product_config.rbc", "rblf")
150load(":part.star|init", _part_init = "init")
151
152def init(g, handle):
153 cfg = rblf.cfg(handle)
Sasha Smundak6609ba72021-07-22 18:32:56 -0700154 if _part_init:
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800155 rblf.inherit(handle, "part", _part_init)
156`,
157 },
158
159 {
160 desc: "Include configuration",
161 mkname: "product.mk",
162 in: `
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800163include part.mk
Sasha Smundak868c5e32021-09-23 16:20:58 -0700164ifdef PRODUCT_NAME
165include part1.mk
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800166else
Sasha Smundak868c5e32021-09-23 16:20:58 -0700167-include $(LOCAL_PATH)/part1.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800168endif
169`,
170 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundak868c5e32021-09-23 16:20:58 -0700171load(":part.star", _part_init = "init")
172load(":part1.star|init", _part1_init = "init")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800173
174def init(g, handle):
175 cfg = rblf.cfg(handle)
Sasha Smundak868c5e32021-09-23 16:20:58 -0700176 _part_init(g, handle)
Cole Faust71514c02022-01-27 17:21:41 -0800177 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800178 if not _part1_init:
179 rblf.mkerror("product.mk", "Cannot find %s" % (":part1.star"))
Sasha Smundak868c5e32021-09-23 16:20:58 -0700180 _part1_init(g, handle)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800181 else:
Sasha Smundak868c5e32021-09-23 16:20:58 -0700182 if _part1_init != None:
183 _part1_init(g, handle)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800184`,
185 },
186
187 {
188 desc: "Synonymous inherited configurations",
189 mkname: "path/product.mk",
190 in: `
Sasha Smundak6609ba72021-07-22 18:32:56 -0700191$(call inherit-product, */font.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800192`,
193 expected: `load("//build/make/core:product_config.rbc", "rblf")
194load("//foo:font.star", _font_init = "init")
195load("//bar:font.star", _font1_init = "init")
196
197def init(g, handle):
198 cfg = rblf.cfg(handle)
199 rblf.inherit(handle, "foo/font", _font_init)
200 rblf.inherit(handle, "bar/font", _font1_init)
201`,
202 },
203 {
204 desc: "Directive define",
205 mkname: "product.mk",
206 in: `
207define some-macro
208 $(info foo)
209endef
210`,
Sasha Smundak422b6142021-11-11 18:31:59 -0800211 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800212
213def init(g, handle):
214 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -0800215 rblf.mk2rbc_error("product.mk:2", "define is not supported: some-macro")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800216`,
217 },
218 {
219 desc: "Ifdef",
220 mkname: "product.mk",
221 in: `
222ifdef PRODUCT_NAME
223 PRODUCT_NAME = gizmo
224else
225endif
Sasha Smundakc4fa93e2021-11-05 14:38:46 -0700226local_var :=
227ifdef local_var
228endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800229`,
230 expected: `load("//build/make/core:product_config.rbc", "rblf")
231
232def init(g, handle):
233 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800234 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800235 cfg["PRODUCT_NAME"] = "gizmo"
236 else:
237 pass
Sasha Smundakc4fa93e2021-11-05 14:38:46 -0700238 _local_var = ""
239 if _local_var:
240 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800241`,
242 },
243 {
244 desc: "Simple functions",
245 mkname: "product.mk",
246 in: `
247$(warning this is the warning)
248$(warning)
249$(info this is the info)
250$(error this is the error)
251PRODUCT_NAME:=$(shell echo *)
252`,
253 expected: `load("//build/make/core:product_config.rbc", "rblf")
254
255def init(g, handle):
256 cfg = rblf.cfg(handle)
257 rblf.mkwarning("product.mk", "this is the warning")
258 rblf.mkwarning("product.mk", "")
259 rblf.mkinfo("product.mk", "this is the info")
260 rblf.mkerror("product.mk", "this is the error")
261 cfg["PRODUCT_NAME"] = rblf.shell("echo *")
262`,
263 },
264 {
265 desc: "Empty if",
266 mkname: "product.mk",
267 in: `
268ifdef PRODUCT_NAME
269# Comment
Sasha Smundak6609ba72021-07-22 18:32:56 -0700270else
Sasha Smundak02183cf2021-08-16 13:36:11 -0700271 TARGET_COPY_OUT_RECOVERY := foo
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800272endif
273`,
274 expected: `load("//build/make/core:product_config.rbc", "rblf")
275
276def init(g, handle):
277 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800278 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800279 # Comment
280 pass
Sasha Smundak6609ba72021-07-22 18:32:56 -0700281 else:
Sasha Smundak422b6142021-11-11 18:31:59 -0800282 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 -0800283`,
284 },
285 {
286 desc: "if/else/endif",
287 mkname: "product.mk",
288 in: `
289ifndef PRODUCT_NAME
290 PRODUCT_NAME=gizmo1
291else
292 PRODUCT_NAME=gizmo2
293endif
294`,
295 expected: `load("//build/make/core:product_config.rbc", "rblf")
296
297def init(g, handle):
298 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800299 if not cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800300 cfg["PRODUCT_NAME"] = "gizmo1"
301 else:
302 cfg["PRODUCT_NAME"] = "gizmo2"
303`,
304 },
305 {
306 desc: "else if",
307 mkname: "product.mk",
308 in: `
309ifdef PRODUCT_NAME
310 PRODUCT_NAME = gizmo
311else ifndef PRODUCT_PACKAGES # Comment
312endif
313 `,
314 expected: `load("//build/make/core:product_config.rbc", "rblf")
315
316def init(g, handle):
317 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800318 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800319 cfg["PRODUCT_NAME"] = "gizmo"
Cole Faust71514c02022-01-27 17:21:41 -0800320 elif not cfg.get("PRODUCT_PACKAGES", []):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800321 # Comment
322 pass
323`,
324 },
325 {
326 desc: "ifeq / ifneq",
327 mkname: "product.mk",
328 in: `
329ifeq (aosp_arm, $(TARGET_PRODUCT))
330 PRODUCT_MODEL = pix2
331else
332 PRODUCT_MODEL = pix21
333endif
334ifneq (aosp_x86, $(TARGET_PRODUCT))
335 PRODUCT_MODEL = pix3
336endif
337`,
338 expected: `load("//build/make/core:product_config.rbc", "rblf")
339
340def init(g, handle):
341 cfg = rblf.cfg(handle)
342 if "aosp_arm" == g["TARGET_PRODUCT"]:
343 cfg["PRODUCT_MODEL"] = "pix2"
344 else:
345 cfg["PRODUCT_MODEL"] = "pix21"
346 if "aosp_x86" != g["TARGET_PRODUCT"]:
347 cfg["PRODUCT_MODEL"] = "pix3"
348`,
349 },
350 {
Cole Faustf8320212021-11-10 15:05:07 -0800351 desc: "ifeq with soong_config_get",
352 mkname: "product.mk",
353 in: `
354ifeq (true,$(call soong_config_get,art_module,source_build))
355endif
356`,
357 expected: `load("//build/make/core:product_config.rbc", "rblf")
358
359def init(g, handle):
360 cfg = rblf.cfg(handle)
361 if "true" == rblf.soong_config_get(g, "art_module", "source_build"):
362 pass
363`,
364 },
365 {
Cole Faustf1f44d32021-11-16 14:52:12 -0800366 desc: "ifeq with $(NATIVE_COVERAGE)",
367 mkname: "product.mk",
368 in: `
369ifeq ($(NATIVE_COVERAGE),true)
370endif
371`,
372 expected: `load("//build/make/core:product_config.rbc", "rblf")
373
374def init(g, handle):
375 cfg = rblf.cfg(handle)
376 if g.get("NATIVE_COVERAGE", False):
377 pass
378`,
379 },
380 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800381 desc: "Check filter result",
382 mkname: "product.mk",
383 in: `
384ifeq (,$(filter userdebug eng, $(TARGET_BUILD_VARIANT)))
385endif
386ifneq (,$(filter userdebug,$(TARGET_BUILD_VARIANT))
387endif
388ifneq (,$(filter plaf,$(PLATFORM_LIST)))
389endif
390ifeq ($(TARGET_BUILD_VARIANT), $(filter $(TARGET_BUILD_VARIANT), userdebug eng))
391endif
Cole Faust9932f752022-02-08 11:56:25 -0800392ifneq (, $(filter $(TARGET_BUILD_VARIANT), userdebug eng))
393endif
394ifneq (,$(filter userdebug eng, $(TARGET_BUILD_VARIANT)))
395endif
Sasha Smundak0554d762021-07-08 18:26:12 -0700396ifneq (,$(filter true, $(v1)$(v2)))
397endif
Sasha Smundak5f463be2021-09-15 18:43:36 -0700398ifeq (,$(filter barbet coral%,$(TARGET_PRODUCT)))
399else ifneq (,$(filter barbet%,$(TARGET_PRODUCT)))
400endif
Cole Fausteec0d812021-12-06 16:23:51 -0800401ifeq (,$(filter-out sunfish_kasan, $(TARGET_PRODUCT)))
402endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800403`,
404 expected: `load("//build/make/core:product_config.rbc", "rblf")
405
406def init(g, handle):
407 cfg = rblf.cfg(handle)
Sasha Smundak5f463be2021-09-15 18:43:36 -0700408 if not rblf.filter("userdebug eng", g["TARGET_BUILD_VARIANT"]):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800409 pass
Sasha Smundak5f463be2021-09-15 18:43:36 -0700410 if rblf.filter("userdebug", g["TARGET_BUILD_VARIANT"]):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800411 pass
412 if "plaf" in g.get("PLATFORM_LIST", []):
413 pass
Cole Faust9932f752022-02-08 11:56:25 -0800414 if g["TARGET_BUILD_VARIANT"] == " ".join(rblf.filter(g["TARGET_BUILD_VARIANT"], "userdebug eng")):
415 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800416 if g["TARGET_BUILD_VARIANT"] in ["userdebug", "eng"]:
417 pass
Cole Faust9932f752022-02-08 11:56:25 -0800418 if rblf.filter("userdebug eng", g["TARGET_BUILD_VARIANT"]):
419 pass
Sasha Smundak5f463be2021-09-15 18:43:36 -0700420 if rblf.filter("true", "%s%s" % (_v1, _v2)):
421 pass
422 if not rblf.filter("barbet coral%", g["TARGET_PRODUCT"]):
423 pass
424 elif rblf.filter("barbet%", g["TARGET_PRODUCT"]):
Sasha Smundak0554d762021-07-08 18:26:12 -0700425 pass
Cole Fausteec0d812021-12-06 16:23:51 -0800426 if not rblf.filter_out("sunfish_kasan", g["TARGET_PRODUCT"]):
427 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800428`,
429 },
430 {
431 desc: "Get filter result",
432 mkname: "product.mk",
433 in: `
434PRODUCT_LIST2=$(filter-out %/foo.ko,$(wildcard path/*.ko))
435`,
436 expected: `load("//build/make/core:product_config.rbc", "rblf")
437
438def init(g, handle):
439 cfg = rblf.cfg(handle)
440 cfg["PRODUCT_LIST2"] = rblf.filter_out("%/foo.ko", rblf.expand_wildcard("path/*.ko"))
441`,
442 },
443 {
444 desc: "filter $(VAR), values",
445 mkname: "product.mk",
446 in: `
447ifeq (,$(filter $(TARGET_PRODUCT), yukawa_gms yukawa_sei510_gms)
448 ifneq (,$(filter $(TARGET_PRODUCT), yukawa_gms)
449 endif
450endif
451
452`,
453 expected: `load("//build/make/core:product_config.rbc", "rblf")
454
455def init(g, handle):
456 cfg = rblf.cfg(handle)
457 if g["TARGET_PRODUCT"] not in ["yukawa_gms", "yukawa_sei510_gms"]:
Sasha Smundak0554d762021-07-08 18:26:12 -0700458 if g["TARGET_PRODUCT"] == "yukawa_gms":
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800459 pass
460`,
461 },
462 {
Sasha Smundak0554d762021-07-08 18:26:12 -0700463 desc: "filter $(V1), $(V2)",
464 mkname: "product.mk",
465 in: `
466ifneq (, $(filter $(PRODUCT_LIST), $(TARGET_PRODUCT)))
467endif
468`,
469 expected: `load("//build/make/core:product_config.rbc", "rblf")
470
471def init(g, handle):
472 cfg = rblf.cfg(handle)
Sasha Smundak468e11f2021-08-26 09:10:23 -0700473 if rblf.filter(g.get("PRODUCT_LIST", []), g["TARGET_PRODUCT"]):
Sasha Smundak0554d762021-07-08 18:26:12 -0700474 pass
475`,
476 },
477 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800478 desc: "ifeq",
479 mkname: "product.mk",
480 in: `
481ifeq (aosp, $(TARGET_PRODUCT)) # Comment
482else ifneq (, $(TARGET_PRODUCT))
483endif
484`,
485 expected: `load("//build/make/core:product_config.rbc", "rblf")
486
487def init(g, handle):
488 cfg = rblf.cfg(handle)
489 if "aosp" == g["TARGET_PRODUCT"]:
490 # Comment
491 pass
492 elif g["TARGET_PRODUCT"]:
493 pass
494`,
495 },
496 {
497 desc: "Nested if",
498 mkname: "product.mk",
499 in: `
500ifdef PRODUCT_NAME
501 PRODUCT_PACKAGES = pack-if0
502 ifdef PRODUCT_MODEL
503 PRODUCT_PACKAGES = pack-if-if
504 else ifdef PRODUCT_NAME
505 PRODUCT_PACKAGES = pack-if-elif
506 else
507 PRODUCT_PACKAGES = pack-if-else
508 endif
509 PRODUCT_PACKAGES = pack-if
510else ifneq (,$(TARGET_PRODUCT))
511 PRODUCT_PACKAGES = pack-elif
512else
513 PRODUCT_PACKAGES = pack-else
514endif
515`,
516 expected: `load("//build/make/core:product_config.rbc", "rblf")
517
518def init(g, handle):
519 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800520 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800521 cfg["PRODUCT_PACKAGES"] = ["pack-if0"]
Cole Faust71514c02022-01-27 17:21:41 -0800522 if cfg.get("PRODUCT_MODEL", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800523 cfg["PRODUCT_PACKAGES"] = ["pack-if-if"]
Cole Faust71514c02022-01-27 17:21:41 -0800524 elif cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800525 cfg["PRODUCT_PACKAGES"] = ["pack-if-elif"]
526 else:
527 cfg["PRODUCT_PACKAGES"] = ["pack-if-else"]
528 cfg["PRODUCT_PACKAGES"] = ["pack-if"]
529 elif g["TARGET_PRODUCT"]:
530 cfg["PRODUCT_PACKAGES"] = ["pack-elif"]
531 else:
532 cfg["PRODUCT_PACKAGES"] = ["pack-else"]
533`,
534 },
535 {
536 desc: "Wildcard",
537 mkname: "product.mk",
538 in: `
539ifeq (,$(wildcard foo.mk))
540endif
541ifneq (,$(wildcard foo*.mk))
542endif
543`,
544 expected: `load("//build/make/core:product_config.rbc", "rblf")
545
546def init(g, handle):
547 cfg = rblf.cfg(handle)
548 if not rblf.file_exists("foo.mk"):
549 pass
550 if rblf.file_wildcard_exists("foo*.mk"):
551 pass
552`,
553 },
554 {
Cole Faustf8320212021-11-10 15:05:07 -0800555 desc: "if with interpolation",
556 mkname: "product.mk",
557 in: `
558ifeq ($(VARIABLE1)text$(VARIABLE2),true)
559endif
560`,
561 expected: `load("//build/make/core:product_config.rbc", "rblf")
562
563def init(g, handle):
564 cfg = rblf.cfg(handle)
565 if "%stext%s" % (g.get("VARIABLE1", ""), g.get("VARIABLE2", "")) == "true":
566 pass
567`,
568 },
569 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800570 desc: "ifneq $(X),true",
571 mkname: "product.mk",
572 in: `
573ifneq ($(VARIABLE),true)
574endif
575`,
576 expected: `load("//build/make/core:product_config.rbc", "rblf")
577
578def init(g, handle):
579 cfg = rblf.cfg(handle)
580 if g.get("VARIABLE", "") != "true":
581 pass
582`,
583 },
584 {
585 desc: "Const neq",
586 mkname: "product.mk",
587 in: `
588ifneq (1,0)
589endif
590`,
591 expected: `load("//build/make/core:product_config.rbc", "rblf")
592
593def init(g, handle):
594 cfg = rblf.cfg(handle)
595 if "1" != "0":
596 pass
597`,
598 },
599 {
600 desc: "is-board calls",
601 mkname: "product.mk",
602 in: `
603ifeq ($(call is-board-platform-in-list,msm8998), true)
604else ifneq ($(call is-board-platform,copper),true)
605else ifneq ($(call is-vendor-board-platform,QCOM),true)
606else ifeq ($(call is-product-in-list, $(PLATFORM_LIST)), true)
607endif
608`,
609 expected: `load("//build/make/core:product_config.rbc", "rblf")
610
611def init(g, handle):
612 cfg = rblf.cfg(handle)
Cole Faustb2e0b602022-01-07 15:46:58 -0800613 if rblf.board_platform_in(g, "msm8998"):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800614 pass
Cole Faustb2e0b602022-01-07 15:46:58 -0800615 elif not rblf.board_platform_is(g, "copper"):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800616 pass
617 elif g.get("TARGET_BOARD_PLATFORM", "") not in g["QCOM_BOARD_PLATFORMS"]:
618 pass
619 elif g["TARGET_PRODUCT"] in g.get("PLATFORM_LIST", []):
620 pass
621`,
622 },
623 {
Sasha Smundak3a9b8e82021-08-25 14:11:04 -0700624 desc: "new is-board calls",
625 mkname: "product.mk",
626 in: `
627ifneq (,$(call is-board-platform-in-list2,msm8998 $(X))
628else ifeq (,$(call is-board-platform2,copper)
629else ifneq (,$(call is-vendor-board-qcom))
630endif
631`,
632 expected: `load("//build/make/core:product_config.rbc", "rblf")
633
634def init(g, handle):
635 cfg = rblf.cfg(handle)
636 if rblf.board_platform_in(g, "msm8998 %s" % g.get("X", "")):
637 pass
638 elif not rblf.board_platform_is(g, "copper"):
639 pass
Sasha Smundak4f1f1182021-11-04 17:57:39 -0700640 elif g.get("TARGET_BOARD_PLATFORM", "") in g["QCOM_BOARD_PLATFORMS"]:
Sasha Smundak3a9b8e82021-08-25 14:11:04 -0700641 pass
642`,
643 },
644 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800645 desc: "findstring call",
646 mkname: "product.mk",
647 in: `
Cole Faust0e9418c2021-12-13 16:33:25 -0800648result := $(findstring a,a b c)
649result := $(findstring b,x y z)
650`,
651 expected: `load("//build/make/core:product_config.rbc", "rblf")
652
653def init(g, handle):
654 cfg = rblf.cfg(handle)
655 _result = rblf.findstring("a", "a b c")
656 _result = rblf.findstring("b", "x y z")
657`,
658 },
659 {
660 desc: "findstring in if statement",
661 mkname: "product.mk",
662 in: `
663ifeq ($(findstring foo,$(PRODUCT_PACKAGES)),)
664endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800665ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),)
666endif
Cole Faust0e9418c2021-12-13 16:33:25 -0800667ifeq ($(findstring foo,$(PRODUCT_PACKAGES)),foo)
668endif
669ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),foo)
670endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800671`,
672 expected: `load("//build/make/core:product_config.rbc", "rblf")
673
674def init(g, handle):
675 cfg = rblf.cfg(handle)
Cole Faust0e9418c2021-12-13 16:33:25 -0800676 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") == -1:
677 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800678 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
679 pass
Cole Faust0e9418c2021-12-13 16:33:25 -0800680 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
681 pass
682 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") == -1:
683 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800684`,
685 },
686 {
687 desc: "rhs call",
688 mkname: "product.mk",
689 in: `
690PRODUCT_COPY_FILES = $(call add-to-product-copy-files-if-exists, path:distpath) \
691 $(call find-copy-subdir-files, *, fromdir, todir) $(wildcard foo.*)
692`,
693 expected: `load("//build/make/core:product_config.rbc", "rblf")
694
695def init(g, handle):
696 cfg = rblf.cfg(handle)
697 cfg["PRODUCT_COPY_FILES"] = (rblf.copy_if_exists("path:distpath") +
698 rblf.find_and_copy("*", "fromdir", "todir") +
699 rblf.expand_wildcard("foo.*"))
700`,
701 },
702 {
703 desc: "inferred type",
704 mkname: "product.mk",
705 in: `
706HIKEY_MODS := $(wildcard foo/*.ko)
707BOARD_VENDOR_KERNEL_MODULES += $(HIKEY_MODS)
708`,
709 expected: `load("//build/make/core:product_config.rbc", "rblf")
710
711def init(g, handle):
712 cfg = rblf.cfg(handle)
713 g["HIKEY_MODS"] = rblf.expand_wildcard("foo/*.ko")
714 g.setdefault("BOARD_VENDOR_KERNEL_MODULES", [])
715 g["BOARD_VENDOR_KERNEL_MODULES"] += g["HIKEY_MODS"]
716`,
717 },
718 {
719 desc: "list with vars",
720 mkname: "product.mk",
721 in: `
722PRODUCT_COPY_FILES += path1:$(TARGET_PRODUCT)/path1 $(PRODUCT_MODEL)/path2:$(TARGET_PRODUCT)/path2
723`,
724 expected: `load("//build/make/core:product_config.rbc", "rblf")
725
726def init(g, handle):
727 cfg = rblf.cfg(handle)
728 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
729 cfg["PRODUCT_COPY_FILES"] += (("path1:%s/path1" % g["TARGET_PRODUCT"]).split() +
730 ("%s/path2:%s/path2" % (cfg.get("PRODUCT_MODEL", ""), g["TARGET_PRODUCT"])).split())
731`,
732 },
733 {
734 desc: "misc calls",
735 mkname: "product.mk",
736 in: `
737$(call enforce-product-packages-exist,)
738$(call enforce-product-packages-exist, foo)
739$(call require-artifacts-in-path, foo, bar)
740$(call require-artifacts-in-path-relaxed, foo, bar)
Sasha Smundakd6797852021-11-15 13:01:53 -0800741$(call dist-for-goals, goal, from:to)
Cole Faust1cc08852022-02-28 11:12:08 -0800742$(call add-product-dex-preopt-module-config,MyModule,disable)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800743`,
744 expected: `load("//build/make/core:product_config.rbc", "rblf")
745
746def init(g, handle):
747 cfg = rblf.cfg(handle)
748 rblf.enforce_product_packages_exist("")
749 rblf.enforce_product_packages_exist("foo")
750 rblf.require_artifacts_in_path("foo", "bar")
751 rblf.require_artifacts_in_path_relaxed("foo", "bar")
Sasha Smundakd6797852021-11-15 13:01:53 -0800752 rblf.mkdist_for_goals(g, "goal", "from:to")
Cole Faust1cc08852022-02-28 11:12:08 -0800753 rblf.add_product_dex_preopt_module_config(handle, "MyModule", "disable")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800754`,
755 },
756 {
757 desc: "list with functions",
758 mkname: "product.mk",
759 in: `
760PRODUCT_COPY_FILES := $(call find-copy-subdir-files,*.kl,from1,to1) \
761 $(call find-copy-subdir-files,*.kc,from2,to2) \
762 foo bar
763`,
764 expected: `load("//build/make/core:product_config.rbc", "rblf")
765
766def init(g, handle):
767 cfg = rblf.cfg(handle)
768 cfg["PRODUCT_COPY_FILES"] = (rblf.find_and_copy("*.kl", "from1", "to1") +
769 rblf.find_and_copy("*.kc", "from2", "to2") +
770 [
771 "foo",
772 "bar",
773 ])
774`,
775 },
776 {
777 desc: "Text functions",
778 mkname: "product.mk",
779 in: `
780PRODUCT_COPY_FILES := $(addprefix pfx-,a b c)
781PRODUCT_COPY_FILES := $(addsuffix .sff, a b c)
782PRODUCT_NAME := $(word 1, $(subst ., ,$(TARGET_BOARD_PLATFORM)))
Sasha Smundak35434ed2021-11-05 16:29:56 -0700783$(info $(patsubst %.pub,$(PRODUCT_NAME)%,$(PRODUCT_ADB_KEYS)))
Sasha Smundak16e07732021-07-23 11:38:23 -0700784$(info $(dir foo/bar))
785$(info $(firstword $(PRODUCT_COPY_FILES)))
786$(info $(lastword $(PRODUCT_COPY_FILES)))
787$(info $(dir $(lastword $(MAKEFILE_LIST))))
788$(info $(dir $(lastword $(PRODUCT_COPY_FILES))))
789$(info $(dir $(lastword $(foobar))))
790$(info $(abspath foo/bar))
791$(info $(notdir foo/bar))
Sasha Smundak3deb9682021-07-26 18:42:25 -0700792$(call add_soong_config_namespace,snsconfig)
793$(call add_soong_config_var_value,snsconfig,imagetype,odm_image)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700794$(call soong_config_set, snsconfig, foo, foo_value)
795$(call soong_config_append, snsconfig, bar, bar_value)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700796PRODUCT_COPY_FILES := $(call copy-files,$(wildcard foo*.mk),etc)
Sasha Smundak04453082021-08-17 18:14:41 -0700797PRODUCT_COPY_FILES := $(call product-copy-files-by-pattern,from/%,to/%,a b c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800798`,
799 expected: `load("//build/make/core:product_config.rbc", "rblf")
800
801def init(g, handle):
802 cfg = rblf.cfg(handle)
803 cfg["PRODUCT_COPY_FILES"] = rblf.addprefix("pfx-", "a b c")
804 cfg["PRODUCT_COPY_FILES"] = rblf.addsuffix(".sff", "a b c")
805 cfg["PRODUCT_NAME"] = ((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " ")).split()[0]
Sasha Smundak35434ed2021-11-05 16:29:56 -0700806 rblf.mkinfo("product.mk", rblf.mkpatsubst("%.pub", "%s%%" % cfg["PRODUCT_NAME"], g.get("PRODUCT_ADB_KEYS", "")))
Sasha Smundak16e07732021-07-23 11:38:23 -0700807 rblf.mkinfo("product.mk", rblf.dir("foo/bar"))
808 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][0])
809 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][-1])
810 rblf.mkinfo("product.mk", rblf.dir("product.mk"))
811 rblf.mkinfo("product.mk", rblf.dir(cfg["PRODUCT_COPY_FILES"][-1]))
812 rblf.mkinfo("product.mk", rblf.dir((_foobar).split()[-1]))
813 rblf.mkinfo("product.mk", rblf.abspath("foo/bar"))
814 rblf.mkinfo("product.mk", rblf.notdir("foo/bar"))
Sasha Smundak65b547e2021-09-17 15:35:41 -0700815 rblf.soong_config_namespace(g, "snsconfig")
816 rblf.soong_config_set(g, "snsconfig", "imagetype", "odm_image")
817 rblf.soong_config_set(g, "snsconfig", "foo", "foo_value")
818 rblf.soong_config_append(g, "snsconfig", "bar", "bar_value")
Sasha Smundak3deb9682021-07-26 18:42:25 -0700819 cfg["PRODUCT_COPY_FILES"] = rblf.copy_files(rblf.expand_wildcard("foo*.mk"), "etc")
Sasha Smundak04453082021-08-17 18:14:41 -0700820 cfg["PRODUCT_COPY_FILES"] = rblf.product_copy_files_by_pattern("from/%", "to/%", "a b c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800821`,
822 },
823 {
Sasha Smundak9d011ab2021-07-09 16:00:57 -0700824 desc: "subst in list",
825 mkname: "product.mk",
826 in: `
827files = $(call find-copy-subdir-files,*,from,to)
828PRODUCT_COPY_FILES += $(subst foo,bar,$(files))
829`,
830 expected: `load("//build/make/core:product_config.rbc", "rblf")
831
832def init(g, handle):
833 cfg = rblf.cfg(handle)
834 _files = rblf.find_and_copy("*", "from", "to")
835 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
836 cfg["PRODUCT_COPY_FILES"] += rblf.mksubst("foo", "bar", _files)
837`,
838 },
839 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800840 desc: "assignment flavors",
841 mkname: "product.mk",
842 in: `
843PRODUCT_LIST1 := a
844PRODUCT_LIST2 += a
845PRODUCT_LIST1 += b
846PRODUCT_LIST2 += b
847PRODUCT_LIST3 ?= a
848PRODUCT_LIST1 = c
849PLATFORM_LIST += x
850PRODUCT_PACKAGES := $(PLATFORM_LIST)
851`,
852 expected: `load("//build/make/core:product_config.rbc", "rblf")
853
854def init(g, handle):
855 cfg = rblf.cfg(handle)
856 cfg["PRODUCT_LIST1"] = ["a"]
857 rblf.setdefault(handle, "PRODUCT_LIST2")
858 cfg["PRODUCT_LIST2"] += ["a"]
859 cfg["PRODUCT_LIST1"] += ["b"]
860 cfg["PRODUCT_LIST2"] += ["b"]
861 if cfg.get("PRODUCT_LIST3") == None:
862 cfg["PRODUCT_LIST3"] = ["a"]
863 cfg["PRODUCT_LIST1"] = ["c"]
864 g.setdefault("PLATFORM_LIST", [])
865 g["PLATFORM_LIST"] += ["x"]
866 cfg["PRODUCT_PACKAGES"] = g["PLATFORM_LIST"][:]
867`,
868 },
869 {
870 desc: "assigment flavors2",
871 mkname: "product.mk",
872 in: `
873PRODUCT_LIST1 = a
874ifeq (0,1)
875 PRODUCT_LIST1 += b
876 PRODUCT_LIST2 += b
877endif
878PRODUCT_LIST1 += c
879PRODUCT_LIST2 += c
880`,
881 expected: `load("//build/make/core:product_config.rbc", "rblf")
882
883def init(g, handle):
884 cfg = rblf.cfg(handle)
885 cfg["PRODUCT_LIST1"] = ["a"]
886 if "0" == "1":
887 cfg["PRODUCT_LIST1"] += ["b"]
888 rblf.setdefault(handle, "PRODUCT_LIST2")
889 cfg["PRODUCT_LIST2"] += ["b"]
890 cfg["PRODUCT_LIST1"] += ["c"]
891 rblf.setdefault(handle, "PRODUCT_LIST2")
892 cfg["PRODUCT_LIST2"] += ["c"]
893`,
894 },
895 {
Sasha Smundak3deb9682021-07-26 18:42:25 -0700896 desc: "soong namespace assignments",
897 mkname: "product.mk",
898 in: `
899SOONG_CONFIG_NAMESPACES += cvd
900SOONG_CONFIG_cvd += launch_configs
Sasha Smundak65b547e2021-09-17 15:35:41 -0700901SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
Sasha Smundak3deb9682021-07-26 18:42:25 -0700902SOONG_CONFIG_cvd += grub_config
903SOONG_CONFIG_cvd_grub_config += grub.cfg
Sasha Smundak65b547e2021-09-17 15:35:41 -0700904x := $(SOONG_CONFIG_cvd_grub_config)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700905`,
906 expected: `load("//build/make/core:product_config.rbc", "rblf")
907
908def init(g, handle):
909 cfg = rblf.cfg(handle)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700910 rblf.soong_config_namespace(g, "cvd")
911 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
912 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
Sasha Smundak422b6142021-11-11 18:31:59 -0800913 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 -0700914`,
Cole Faustc00184e2021-11-08 12:08:57 -0800915 }, {
916 desc: "soong namespace accesses",
917 mkname: "product.mk",
918 in: `
919SOONG_CONFIG_NAMESPACES += cvd
920SOONG_CONFIG_cvd += launch_configs
921SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
922SOONG_CONFIG_cvd += grub_config
923SOONG_CONFIG_cvd_grub_config += grub.cfg
924x := $(call soong_config_get,cvd,grub_config)
925`,
926 expected: `load("//build/make/core:product_config.rbc", "rblf")
927
928def init(g, handle):
929 cfg = rblf.cfg(handle)
930 rblf.soong_config_namespace(g, "cvd")
931 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
932 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
933 _x = rblf.soong_config_get(g, "cvd", "grub_config")
934`,
Sasha Smundak3deb9682021-07-26 18:42:25 -0700935 },
936 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800937 desc: "string split",
938 mkname: "product.mk",
939 in: `
940PRODUCT_LIST1 = a
941local = b
942local += c
943FOO = d
944FOO += e
945PRODUCT_LIST1 += $(local)
946PRODUCT_LIST1 += $(FOO)
947`,
948 expected: `load("//build/make/core:product_config.rbc", "rblf")
949
950def init(g, handle):
951 cfg = rblf.cfg(handle)
952 cfg["PRODUCT_LIST1"] = ["a"]
953 _local = "b"
954 _local += " " + "c"
955 g["FOO"] = "d"
956 g["FOO"] += " " + "e"
957 cfg["PRODUCT_LIST1"] += (_local).split()
958 cfg["PRODUCT_LIST1"] += (g["FOO"]).split()
959`,
960 },
961 {
962 desc: "apex_jars",
963 mkname: "product.mk",
964 in: `
965PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex
966`,
967 expected: `load("//build/make/core:product_config.rbc", "rblf")
968
969def init(g, handle):
970 cfg = rblf.cfg(handle)
971 cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) +
972 ["framework-minus-apex"])
973`,
974 },
975 {
976 desc: "strip function",
977 mkname: "product.mk",
978 in: `
979ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),)
980 PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress)
981endif
982`,
983 expected: `load("//build/make/core:product_config.rbc", "rblf")
984
985def init(g, handle):
986 cfg = rblf.cfg(handle)
987 if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []):
Cole Faust816e0802022-03-04 12:04:31 -0800988 rblf.setdefault(handle, "PRODUCT_PACKAGES")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800989 cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split()
990`,
991 },
992 {
993 desc: "strip func in condition",
994 mkname: "product.mk",
995 in: `
996ifneq ($(strip $(TARGET_VENDOR)),)
997endif
998`,
999 expected: `load("//build/make/core:product_config.rbc", "rblf")
1000
1001def init(g, handle):
1002 cfg = rblf.cfg(handle)
Sasha Smundak0554d762021-07-08 18:26:12 -07001003 if rblf.mkstrip(g.get("TARGET_VENDOR", "")):
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001004 pass
1005`,
1006 },
1007 {
1008 desc: "ref after set",
1009 mkname: "product.mk",
1010 in: `
1011PRODUCT_ADB_KEYS:=value
1012FOO := $(PRODUCT_ADB_KEYS)
1013ifneq (,$(PRODUCT_ADB_KEYS))
1014endif
1015`,
1016 expected: `load("//build/make/core:product_config.rbc", "rblf")
1017
1018def init(g, handle):
1019 cfg = rblf.cfg(handle)
1020 g["PRODUCT_ADB_KEYS"] = "value"
1021 g["FOO"] = g["PRODUCT_ADB_KEYS"]
1022 if g["PRODUCT_ADB_KEYS"]:
1023 pass
1024`,
1025 },
1026 {
1027 desc: "ref before set",
1028 mkname: "product.mk",
1029 in: `
1030V1 := $(PRODUCT_ADB_KEYS)
1031ifeq (,$(PRODUCT_ADB_KEYS))
1032 V2 := $(PRODUCT_ADB_KEYS)
1033 PRODUCT_ADB_KEYS:=foo
1034 V3 := $(PRODUCT_ADB_KEYS)
1035endif`,
1036 expected: `load("//build/make/core:product_config.rbc", "rblf")
1037
1038def init(g, handle):
1039 cfg = rblf.cfg(handle)
1040 g["V1"] = g.get("PRODUCT_ADB_KEYS", "")
1041 if not g.get("PRODUCT_ADB_KEYS", ""):
1042 g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
1043 g["PRODUCT_ADB_KEYS"] = "foo"
1044 g["V3"] = g["PRODUCT_ADB_KEYS"]
1045`,
1046 },
Sasha Smundak6609ba72021-07-22 18:32:56 -07001047 {
1048 desc: "Dynamic inherit path",
1049 mkname: "product.mk",
1050 in: `
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001051MY_PATH:=foo
Sasha Smundak6609ba72021-07-22 18:32:56 -07001052$(call inherit-product,vendor/$(MY_PATH)/cfg.mk)
1053`,
1054 expected: `load("//build/make/core:product_config.rbc", "rblf")
1055load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1056load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init")
1057
1058def init(g, handle):
1059 cfg = rblf.cfg(handle)
1060 g["MY_PATH"] = "foo"
1061 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001062 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1063 "vendor/bar/baz/cfg.mk": ("vendor/bar/baz/cfg", _cfg1_init),
Sasha Smundak6609ba72021-07-22 18:32:56 -07001064 }.get("vendor/%s/cfg.mk" % g["MY_PATH"])
1065 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1066 if not _varmod_init:
Cole Faust7321b092021-12-21 16:11:16 -08001067 rblf.mkerror("product.mk", "Cannot find %s" % ("vendor/%s/cfg.mk" % g["MY_PATH"]))
Sasha Smundak6609ba72021-07-22 18:32:56 -07001068 rblf.inherit(handle, _varmod, _varmod_init)
1069`,
1070 },
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001071 {
1072 desc: "Dynamic inherit with hint",
1073 mkname: "product.mk",
1074 in: `
1075MY_PATH:=foo
1076#RBC# include_top vendor/foo1
1077$(call inherit-product,$(MY_PATH)/cfg.mk)
1078`,
1079 expected: `load("//build/make/core:product_config.rbc", "rblf")
1080load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1081
1082def init(g, handle):
1083 cfg = rblf.cfg(handle)
1084 g["MY_PATH"] = "foo"
Cole Faust069aba62022-01-26 17:47:33 -08001085 rblf.inherit(handle, "vendor/foo1/cfg", _cfg_init)
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001086`,
1087 },
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001088 {
Cole Faustf7ed5342021-12-21 14:15:12 -08001089 desc: "Dynamic inherit with duplicated hint",
1090 mkname: "product.mk",
1091 in: `
1092MY_PATH:=foo
1093#RBC# include_top vendor/foo1
1094$(call inherit-product,$(MY_PATH)/cfg.mk)
1095#RBC# include_top vendor/foo1
Cole Faust7940c6a2022-01-31 15:54:05 -08001096#RBC# include_top vendor/foo1
Cole Faustf7ed5342021-12-21 14:15:12 -08001097$(call inherit-product,$(MY_PATH)/cfg.mk)
1098`,
1099 expected: `load("//build/make/core:product_config.rbc", "rblf")
1100load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1101
1102def init(g, handle):
1103 cfg = rblf.cfg(handle)
1104 g["MY_PATH"] = "foo"
Cole Faust069aba62022-01-26 17:47:33 -08001105 rblf.inherit(handle, "vendor/foo1/cfg", _cfg_init)
Cole Faust069aba62022-01-26 17:47:33 -08001106 rblf.inherit(handle, "vendor/foo1/cfg", _cfg_init)
Cole Faustf7ed5342021-12-21 14:15:12 -08001107`,
1108 },
1109 {
Cole Faust069aba62022-01-26 17:47:33 -08001110 desc: "Dynamic inherit path that lacks hint",
Cole Faust6c934f62022-01-06 15:51:12 -08001111 mkname: "product.mk",
1112 in: `
1113#RBC# include_top foo
1114$(call inherit-product,$(MY_VAR)/font.mk)
1115
1116#RBC# include_top foo
1117
1118# There's some space and even this comment between the include_top and the inherit-product
1119
1120$(call inherit-product,$(MY_VAR)/font.mk)
1121
1122$(call inherit-product,$(MY_VAR)/font.mk)
1123`,
Cole Faust7940c6a2022-01-31 15:54:05 -08001124 expected: `load("//build/make/core:product_config.rbc", "rblf")
Cole Faust6c934f62022-01-06 15:51:12 -08001125load("//foo:font.star|init", _font_init = "init")
Cole Faust069aba62022-01-26 17:47:33 -08001126load("//bar:font.star|init", _font1_init = "init")
Cole Faust6c934f62022-01-06 15:51:12 -08001127
1128def init(g, handle):
1129 cfg = rblf.cfg(handle)
Cole Faust069aba62022-01-26 17:47:33 -08001130 rblf.inherit(handle, "foo/font", _font_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001131 # There's some space and even this comment between the include_top and the inherit-product
Cole Faust069aba62022-01-26 17:47:33 -08001132 rblf.inherit(handle, "foo/font", _font_init)
Cole Faustf4e72cf2022-02-08 12:49:37 -08001133 rblf.mkwarning("product.mk:11", "Please avoid starting an include path with a variable. See https://source.android.com/setup/build/bazel/product_config/issues/includes for details.")
Cole Faust6c934f62022-01-06 15:51:12 -08001134 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001135 "foo/font.mk": ("foo/font", _font_init),
Cole Faust069aba62022-01-26 17:47:33 -08001136 "bar/font.mk": ("bar/font", _font1_init),
Cole Faust6c934f62022-01-06 15:51:12 -08001137 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1138 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1139 if not _varmod_init:
1140 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1141 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001142`,
1143 },
1144 {
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001145 desc: "Ignore make rules",
1146 mkname: "product.mk",
1147 in: `
1148foo: foo.c
1149 gcc -o $@ $*`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001150 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001151
1152def init(g, handle):
1153 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001154 rblf.mk2rbc_error("product.mk:2", "unsupported line rule: foo: foo.c\n#gcc -o $@ $*")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001155`,
1156 },
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001157 {
1158 desc: "Flag override",
1159 mkname: "product.mk",
1160 in: `
1161override FOO:=`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001162 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001163
1164def init(g, handle):
1165 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001166 rblf.mk2rbc_error("product.mk:2", "cannot handle override directive")
Sasha Smundak422b6142021-11-11 18:31:59 -08001167`,
1168 },
1169 {
1170 desc: "Bad expression",
1171 mkname: "build/product.mk",
1172 in: `
1173ifeq (,$(call foobar))
1174endif
1175`,
1176 expected: `load("//build/make/core:product_config.rbc", "rblf")
1177
1178def init(g, handle):
1179 cfg = rblf.cfg(handle)
1180 if rblf.mk2rbc_error("build/product.mk:2", "cannot handle invoking foobar"):
1181 pass
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001182`,
1183 },
Cole Faust4eadba72021-12-07 11:54:52 -08001184 {
1185 desc: "if expression",
1186 mkname: "product.mk",
1187 in: `
1188TEST_VAR := foo
1189TEST_VAR_LIST := foo
1190TEST_VAR_LIST += bar
1191TEST_VAR_2 := $(if $(TEST_VAR),bar)
1192TEST_VAR_3 := $(if $(TEST_VAR),bar,baz)
1193TEST_VAR_3 := $(if $(TEST_VAR),$(TEST_VAR_LIST))
1194`,
1195 expected: `load("//build/make/core:product_config.rbc", "rblf")
1196
1197def init(g, handle):
1198 cfg = rblf.cfg(handle)
1199 g["TEST_VAR"] = "foo"
1200 g["TEST_VAR_LIST"] = ["foo"]
1201 g["TEST_VAR_LIST"] += ["bar"]
1202 g["TEST_VAR_2"] = ("bar" if g["TEST_VAR"] else "")
1203 g["TEST_VAR_3"] = ("bar" if g["TEST_VAR"] else "baz")
1204 g["TEST_VAR_3"] = (g["TEST_VAR_LIST"] if g["TEST_VAR"] else [])
1205`,
1206 },
Cole Faustc36c9622021-12-07 15:20:45 -08001207 {
1208 desc: "substitution references",
1209 mkname: "product.mk",
1210 in: `
1211SOURCES := foo.c bar.c
1212OBJECTS := $(SOURCES:.c=.o)
1213OBJECTS2 := $(SOURCES:%.c=%.o)
1214`,
1215 expected: `load("//build/make/core:product_config.rbc", "rblf")
1216
1217def init(g, handle):
1218 cfg = rblf.cfg(handle)
1219 g["SOURCES"] = "foo.c bar.c"
1220 g["OBJECTS"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1221 g["OBJECTS2"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1222`,
1223 },
Cole Faustb0d32ab2021-12-09 14:00:59 -08001224 {
1225 desc: "foreach expressions",
1226 mkname: "product.mk",
1227 in: `
1228BOOT_KERNEL_MODULES := foo.ko bar.ko
1229BOOT_KERNEL_MODULES_FILTER := $(foreach m,$(BOOT_KERNEL_MODULES),%/$(m))
1230BOOT_KERNEL_MODULES_LIST := foo.ko
1231BOOT_KERNEL_MODULES_LIST += bar.ko
1232BOOT_KERNEL_MODULES_FILTER_2 := $(foreach m,$(BOOT_KERNEL_MODULES_LIST),%/$(m))
1233
Cole Faustb67aa082022-02-28 16:39:59 -08001234FOREACH_WITH_IF := $(foreach module,\
1235 $(BOOT_KERNEL_MODULES_LIST),\
1236 $(if $(filter $(module),foo.ko),,$(error module "$(module)" has an error!)))
Cole Faustb0d32ab2021-12-09 14:00:59 -08001237`,
1238 expected: `load("//build/make/core:product_config.rbc", "rblf")
1239
1240def init(g, handle):
1241 cfg = rblf.cfg(handle)
1242 g["BOOT_KERNEL_MODULES"] = "foo.ko bar.ko"
1243 g["BOOT_KERNEL_MODULES_FILTER"] = ["%%/%s" % m for m in rblf.words(g["BOOT_KERNEL_MODULES"])]
1244 g["BOOT_KERNEL_MODULES_LIST"] = ["foo.ko"]
1245 g["BOOT_KERNEL_MODULES_LIST"] += ["bar.ko"]
1246 g["BOOT_KERNEL_MODULES_FILTER_2"] = ["%%/%s" % m for m in g["BOOT_KERNEL_MODULES_LIST"]]
Cole Faustb67aa082022-02-28 16:39:59 -08001247 g["FOREACH_WITH_IF"] = [("" if rblf.filter(module, "foo.ko") else rblf.mkerror("product.mk", "module \"%s\" has an error!" % module)) for module in g["BOOT_KERNEL_MODULES_LIST"]]
Cole Faustb0d32ab2021-12-09 14:00:59 -08001248`,
1249 },
Cole Faust0484c232021-12-22 14:08:08 -08001250 {
1251 desc: "List appended to string",
1252 mkname: "product.mk",
1253 in: `
1254NATIVE_BRIDGE_PRODUCT_PACKAGES := \
1255 libnative_bridge_vdso.native_bridge \
1256 native_bridge_guest_app_process.native_bridge \
1257 native_bridge_guest_linker.native_bridge
1258
1259NATIVE_BRIDGE_MODIFIED_GUEST_LIBS := \
1260 libaaudio \
1261 libamidi \
1262 libandroid \
1263 libandroid_runtime
1264
1265NATIVE_BRIDGE_PRODUCT_PACKAGES += \
1266 $(addsuffix .native_bridge,$(NATIVE_BRIDGE_ORIG_GUEST_LIBS))
1267`,
1268 expected: `load("//build/make/core:product_config.rbc", "rblf")
1269
1270def init(g, handle):
1271 cfg = rblf.cfg(handle)
1272 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] = "libnative_bridge_vdso.native_bridge native_bridge_guest_app_process.native_bridge native_bridge_guest_linker.native_bridge"
1273 g["NATIVE_BRIDGE_MODIFIED_GUEST_LIBS"] = "libaaudio libamidi libandroid libandroid_runtime"
1274 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] += " " + " ".join(rblf.addsuffix(".native_bridge", g.get("NATIVE_BRIDGE_ORIG_GUEST_LIBS", "")))
1275`,
1276 },
Cole Faustb1103e22022-01-06 15:22:05 -08001277 {
1278 desc: "Math functions",
1279 mkname: "product.mk",
1280 in: `
1281# Test the math functions defined in build/make/common/math.mk
1282ifeq ($(call math_max,2,5),5)
1283endif
1284ifeq ($(call math_min,2,5),2)
1285endif
1286ifeq ($(call math_gt_or_eq,2,5),true)
1287endif
1288ifeq ($(call math_gt,2,5),true)
1289endif
1290ifeq ($(call math_lt,2,5),true)
1291endif
1292ifeq ($(call math_gt_or_eq,2,5),)
1293endif
1294ifeq ($(call math_gt,2,5),)
1295endif
1296ifeq ($(call math_lt,2,5),)
1297endif
1298ifeq ($(call math_gt_or_eq,$(MY_VAR), 5),true)
1299endif
1300ifeq ($(call math_gt_or_eq,$(MY_VAR),$(MY_OTHER_VAR)),true)
1301endif
1302ifeq ($(call math_gt_or_eq,100$(MY_VAR),10),true)
1303endif
1304`,
1305 expected: `# Test the math functions defined in build/make/common/math.mk
1306load("//build/make/core:product_config.rbc", "rblf")
1307
1308def init(g, handle):
1309 cfg = rblf.cfg(handle)
1310 if max(2, 5) == 5:
1311 pass
1312 if min(2, 5) == 2:
1313 pass
1314 if 2 >= 5:
1315 pass
1316 if 2 > 5:
1317 pass
1318 if 2 < 5:
1319 pass
1320 if 2 < 5:
1321 pass
1322 if 2 <= 5:
1323 pass
1324 if 2 >= 5:
1325 pass
1326 if int(g.get("MY_VAR", "")) >= 5:
1327 pass
1328 if int(g.get("MY_VAR", "")) >= int(g.get("MY_OTHER_VAR", "")):
1329 pass
1330 if int("100%s" % g.get("MY_VAR", "")) >= 10:
1331 pass
1332`,
1333 },
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001334}
1335
1336var known_variables = []struct {
1337 name string
1338 class varClass
1339 starlarkType
1340}{
Cole Faustf1f44d32021-11-16 14:52:12 -08001341 {"NATIVE_COVERAGE", VarClassSoong, starlarkTypeBool},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001342 {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
1343 {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
1344 {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
1345 {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
1346 {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
1347 {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
1348 {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
1349 {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
1350 {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
1351 {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
1352 {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
1353 {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
1354 {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
1355 {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
1356}
1357
Sasha Smundak6609ba72021-07-22 18:32:56 -07001358type testMakefileFinder struct {
1359 fs fs.FS
1360 root string
1361 files []string
1362}
1363
1364func (t *testMakefileFinder) Find(root string) []string {
1365 if t.files != nil || root == t.root {
1366 return t.files
1367 }
1368 t.files = make([]string, 0)
1369 fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error {
1370 if err != nil {
1371 return err
1372 }
1373 if d.IsDir() {
1374 base := filepath.Base(path)
1375 if base[0] == '.' && len(base) > 1 {
1376 return fs.SkipDir
1377 }
1378 return nil
1379 }
1380 if strings.HasSuffix(path, ".mk") {
1381 t.files = append(t.files, path)
1382 }
1383 return nil
1384 })
1385 return t.files
1386}
1387
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001388func TestGood(t *testing.T) {
1389 for _, v := range known_variables {
1390 KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
1391 }
Sasha Smundak6609ba72021-07-22 18:32:56 -07001392 fs := NewFindMockFS([]string{
1393 "vendor/foo1/cfg.mk",
1394 "vendor/bar/baz/cfg.mk",
1395 "part.mk",
1396 "foo/font.mk",
1397 "bar/font.mk",
1398 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001399 for _, test := range testCases {
1400 t.Run(test.desc,
1401 func(t *testing.T) {
1402 ss, err := Convert(Request{
Sasha Smundak422b6142021-11-11 18:31:59 -08001403 MkFile: test.mkname,
1404 Reader: bytes.NewBufferString(test.in),
Sasha Smundak422b6142021-11-11 18:31:59 -08001405 OutputSuffix: ".star",
1406 SourceFS: fs,
1407 MakefileFinder: &testMakefileFinder{fs: fs},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001408 })
1409 if err != nil {
1410 t.Error(err)
1411 return
1412 }
1413 got := ss.String()
1414 if got != test.expected {
1415 t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
1416 strings.ReplaceAll(test.expected, "\n", "␤\n"),
1417 strings.ReplaceAll(got, "\n", "␤\n"))
1418 }
1419 })
1420 }
1421}