blob: a56815f4513b6187c07a71d9731119f841d6839a [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
Sasha Smundak0554d762021-07-08 18:26:12 -0700392ifneq (,$(filter true, $(v1)$(v2)))
393endif
Sasha Smundak5f463be2021-09-15 18:43:36 -0700394ifeq (,$(filter barbet coral%,$(TARGET_PRODUCT)))
395else ifneq (,$(filter barbet%,$(TARGET_PRODUCT)))
396endif
Cole Fausteec0d812021-12-06 16:23:51 -0800397ifeq (,$(filter-out sunfish_kasan, $(TARGET_PRODUCT)))
398endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800399`,
400 expected: `load("//build/make/core:product_config.rbc", "rblf")
401
402def init(g, handle):
403 cfg = rblf.cfg(handle)
Sasha Smundak5f463be2021-09-15 18:43:36 -0700404 if not rblf.filter("userdebug eng", g["TARGET_BUILD_VARIANT"]):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800405 pass
Sasha Smundak5f463be2021-09-15 18:43:36 -0700406 if rblf.filter("userdebug", g["TARGET_BUILD_VARIANT"]):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800407 pass
408 if "plaf" in g.get("PLATFORM_LIST", []):
409 pass
410 if g["TARGET_BUILD_VARIANT"] in ["userdebug", "eng"]:
411 pass
Sasha Smundak5f463be2021-09-15 18:43:36 -0700412 if rblf.filter("true", "%s%s" % (_v1, _v2)):
413 pass
414 if not rblf.filter("barbet coral%", g["TARGET_PRODUCT"]):
415 pass
416 elif rblf.filter("barbet%", g["TARGET_PRODUCT"]):
Sasha Smundak0554d762021-07-08 18:26:12 -0700417 pass
Cole Fausteec0d812021-12-06 16:23:51 -0800418 if not rblf.filter_out("sunfish_kasan", g["TARGET_PRODUCT"]):
419 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800420`,
421 },
422 {
423 desc: "Get filter result",
424 mkname: "product.mk",
425 in: `
426PRODUCT_LIST2=$(filter-out %/foo.ko,$(wildcard path/*.ko))
427`,
428 expected: `load("//build/make/core:product_config.rbc", "rblf")
429
430def init(g, handle):
431 cfg = rblf.cfg(handle)
432 cfg["PRODUCT_LIST2"] = rblf.filter_out("%/foo.ko", rblf.expand_wildcard("path/*.ko"))
433`,
434 },
435 {
436 desc: "filter $(VAR), values",
437 mkname: "product.mk",
438 in: `
439ifeq (,$(filter $(TARGET_PRODUCT), yukawa_gms yukawa_sei510_gms)
440 ifneq (,$(filter $(TARGET_PRODUCT), yukawa_gms)
441 endif
442endif
443
444`,
445 expected: `load("//build/make/core:product_config.rbc", "rblf")
446
447def init(g, handle):
448 cfg = rblf.cfg(handle)
449 if g["TARGET_PRODUCT"] not in ["yukawa_gms", "yukawa_sei510_gms"]:
Sasha Smundak0554d762021-07-08 18:26:12 -0700450 if g["TARGET_PRODUCT"] == "yukawa_gms":
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800451 pass
452`,
453 },
454 {
Sasha Smundak0554d762021-07-08 18:26:12 -0700455 desc: "filter $(V1), $(V2)",
456 mkname: "product.mk",
457 in: `
458ifneq (, $(filter $(PRODUCT_LIST), $(TARGET_PRODUCT)))
459endif
460`,
461 expected: `load("//build/make/core:product_config.rbc", "rblf")
462
463def init(g, handle):
464 cfg = rblf.cfg(handle)
Sasha Smundak468e11f2021-08-26 09:10:23 -0700465 if rblf.filter(g.get("PRODUCT_LIST", []), g["TARGET_PRODUCT"]):
Sasha Smundak0554d762021-07-08 18:26:12 -0700466 pass
467`,
468 },
469 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800470 desc: "ifeq",
471 mkname: "product.mk",
472 in: `
473ifeq (aosp, $(TARGET_PRODUCT)) # Comment
474else ifneq (, $(TARGET_PRODUCT))
475endif
476`,
477 expected: `load("//build/make/core:product_config.rbc", "rblf")
478
479def init(g, handle):
480 cfg = rblf.cfg(handle)
481 if "aosp" == g["TARGET_PRODUCT"]:
482 # Comment
483 pass
484 elif g["TARGET_PRODUCT"]:
485 pass
486`,
487 },
488 {
489 desc: "Nested if",
490 mkname: "product.mk",
491 in: `
492ifdef PRODUCT_NAME
493 PRODUCT_PACKAGES = pack-if0
494 ifdef PRODUCT_MODEL
495 PRODUCT_PACKAGES = pack-if-if
496 else ifdef PRODUCT_NAME
497 PRODUCT_PACKAGES = pack-if-elif
498 else
499 PRODUCT_PACKAGES = pack-if-else
500 endif
501 PRODUCT_PACKAGES = pack-if
502else ifneq (,$(TARGET_PRODUCT))
503 PRODUCT_PACKAGES = pack-elif
504else
505 PRODUCT_PACKAGES = pack-else
506endif
507`,
508 expected: `load("//build/make/core:product_config.rbc", "rblf")
509
510def init(g, handle):
511 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800512 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800513 cfg["PRODUCT_PACKAGES"] = ["pack-if0"]
Cole Faust71514c02022-01-27 17:21:41 -0800514 if cfg.get("PRODUCT_MODEL", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800515 cfg["PRODUCT_PACKAGES"] = ["pack-if-if"]
Cole Faust71514c02022-01-27 17:21:41 -0800516 elif cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800517 cfg["PRODUCT_PACKAGES"] = ["pack-if-elif"]
518 else:
519 cfg["PRODUCT_PACKAGES"] = ["pack-if-else"]
520 cfg["PRODUCT_PACKAGES"] = ["pack-if"]
521 elif g["TARGET_PRODUCT"]:
522 cfg["PRODUCT_PACKAGES"] = ["pack-elif"]
523 else:
524 cfg["PRODUCT_PACKAGES"] = ["pack-else"]
525`,
526 },
527 {
528 desc: "Wildcard",
529 mkname: "product.mk",
530 in: `
531ifeq (,$(wildcard foo.mk))
532endif
533ifneq (,$(wildcard foo*.mk))
534endif
535`,
536 expected: `load("//build/make/core:product_config.rbc", "rblf")
537
538def init(g, handle):
539 cfg = rblf.cfg(handle)
540 if not rblf.file_exists("foo.mk"):
541 pass
542 if rblf.file_wildcard_exists("foo*.mk"):
543 pass
544`,
545 },
546 {
Cole Faustf8320212021-11-10 15:05:07 -0800547 desc: "if with interpolation",
548 mkname: "product.mk",
549 in: `
550ifeq ($(VARIABLE1)text$(VARIABLE2),true)
551endif
552`,
553 expected: `load("//build/make/core:product_config.rbc", "rblf")
554
555def init(g, handle):
556 cfg = rblf.cfg(handle)
557 if "%stext%s" % (g.get("VARIABLE1", ""), g.get("VARIABLE2", "")) == "true":
558 pass
559`,
560 },
561 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800562 desc: "ifneq $(X),true",
563 mkname: "product.mk",
564 in: `
565ifneq ($(VARIABLE),true)
566endif
567`,
568 expected: `load("//build/make/core:product_config.rbc", "rblf")
569
570def init(g, handle):
571 cfg = rblf.cfg(handle)
572 if g.get("VARIABLE", "") != "true":
573 pass
574`,
575 },
576 {
577 desc: "Const neq",
578 mkname: "product.mk",
579 in: `
580ifneq (1,0)
581endif
582`,
583 expected: `load("//build/make/core:product_config.rbc", "rblf")
584
585def init(g, handle):
586 cfg = rblf.cfg(handle)
587 if "1" != "0":
588 pass
589`,
590 },
591 {
592 desc: "is-board calls",
593 mkname: "product.mk",
594 in: `
595ifeq ($(call is-board-platform-in-list,msm8998), true)
596else ifneq ($(call is-board-platform,copper),true)
597else ifneq ($(call is-vendor-board-platform,QCOM),true)
598else ifeq ($(call is-product-in-list, $(PLATFORM_LIST)), true)
599endif
600`,
601 expected: `load("//build/make/core:product_config.rbc", "rblf")
602
603def init(g, handle):
604 cfg = rblf.cfg(handle)
Cole Faustb2e0b602022-01-07 15:46:58 -0800605 if rblf.board_platform_in(g, "msm8998"):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800606 pass
Cole Faustb2e0b602022-01-07 15:46:58 -0800607 elif not rblf.board_platform_is(g, "copper"):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800608 pass
609 elif g.get("TARGET_BOARD_PLATFORM", "") not in g["QCOM_BOARD_PLATFORMS"]:
610 pass
611 elif g["TARGET_PRODUCT"] in g.get("PLATFORM_LIST", []):
612 pass
613`,
614 },
615 {
Sasha Smundak3a9b8e82021-08-25 14:11:04 -0700616 desc: "new is-board calls",
617 mkname: "product.mk",
618 in: `
619ifneq (,$(call is-board-platform-in-list2,msm8998 $(X))
620else ifeq (,$(call is-board-platform2,copper)
621else ifneq (,$(call is-vendor-board-qcom))
622endif
623`,
624 expected: `load("//build/make/core:product_config.rbc", "rblf")
625
626def init(g, handle):
627 cfg = rblf.cfg(handle)
628 if rblf.board_platform_in(g, "msm8998 %s" % g.get("X", "")):
629 pass
630 elif not rblf.board_platform_is(g, "copper"):
631 pass
Sasha Smundak4f1f1182021-11-04 17:57:39 -0700632 elif g.get("TARGET_BOARD_PLATFORM", "") in g["QCOM_BOARD_PLATFORMS"]:
Sasha Smundak3a9b8e82021-08-25 14:11:04 -0700633 pass
634`,
635 },
636 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800637 desc: "findstring call",
638 mkname: "product.mk",
639 in: `
Cole Faust0e9418c2021-12-13 16:33:25 -0800640result := $(findstring a,a b c)
641result := $(findstring b,x y z)
642`,
643 expected: `load("//build/make/core:product_config.rbc", "rblf")
644
645def init(g, handle):
646 cfg = rblf.cfg(handle)
647 _result = rblf.findstring("a", "a b c")
648 _result = rblf.findstring("b", "x y z")
649`,
650 },
651 {
652 desc: "findstring in if statement",
653 mkname: "product.mk",
654 in: `
655ifeq ($(findstring foo,$(PRODUCT_PACKAGES)),)
656endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800657ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),)
658endif
Cole Faust0e9418c2021-12-13 16:33:25 -0800659ifeq ($(findstring foo,$(PRODUCT_PACKAGES)),foo)
660endif
661ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),foo)
662endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800663`,
664 expected: `load("//build/make/core:product_config.rbc", "rblf")
665
666def init(g, handle):
667 cfg = rblf.cfg(handle)
Cole Faust0e9418c2021-12-13 16:33:25 -0800668 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") == -1:
669 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800670 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
671 pass
Cole Faust0e9418c2021-12-13 16:33:25 -0800672 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
673 pass
674 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") == -1:
675 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800676`,
677 },
678 {
679 desc: "rhs call",
680 mkname: "product.mk",
681 in: `
682PRODUCT_COPY_FILES = $(call add-to-product-copy-files-if-exists, path:distpath) \
683 $(call find-copy-subdir-files, *, fromdir, todir) $(wildcard foo.*)
684`,
685 expected: `load("//build/make/core:product_config.rbc", "rblf")
686
687def init(g, handle):
688 cfg = rblf.cfg(handle)
689 cfg["PRODUCT_COPY_FILES"] = (rblf.copy_if_exists("path:distpath") +
690 rblf.find_and_copy("*", "fromdir", "todir") +
691 rblf.expand_wildcard("foo.*"))
692`,
693 },
694 {
695 desc: "inferred type",
696 mkname: "product.mk",
697 in: `
698HIKEY_MODS := $(wildcard foo/*.ko)
699BOARD_VENDOR_KERNEL_MODULES += $(HIKEY_MODS)
700`,
701 expected: `load("//build/make/core:product_config.rbc", "rblf")
702
703def init(g, handle):
704 cfg = rblf.cfg(handle)
705 g["HIKEY_MODS"] = rblf.expand_wildcard("foo/*.ko")
706 g.setdefault("BOARD_VENDOR_KERNEL_MODULES", [])
707 g["BOARD_VENDOR_KERNEL_MODULES"] += g["HIKEY_MODS"]
708`,
709 },
710 {
711 desc: "list with vars",
712 mkname: "product.mk",
713 in: `
714PRODUCT_COPY_FILES += path1:$(TARGET_PRODUCT)/path1 $(PRODUCT_MODEL)/path2:$(TARGET_PRODUCT)/path2
715`,
716 expected: `load("//build/make/core:product_config.rbc", "rblf")
717
718def init(g, handle):
719 cfg = rblf.cfg(handle)
720 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
721 cfg["PRODUCT_COPY_FILES"] += (("path1:%s/path1" % g["TARGET_PRODUCT"]).split() +
722 ("%s/path2:%s/path2" % (cfg.get("PRODUCT_MODEL", ""), g["TARGET_PRODUCT"])).split())
723`,
724 },
725 {
726 desc: "misc calls",
727 mkname: "product.mk",
728 in: `
729$(call enforce-product-packages-exist,)
730$(call enforce-product-packages-exist, foo)
731$(call require-artifacts-in-path, foo, bar)
732$(call require-artifacts-in-path-relaxed, foo, bar)
Sasha Smundakd6797852021-11-15 13:01:53 -0800733$(call dist-for-goals, goal, from:to)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800734`,
735 expected: `load("//build/make/core:product_config.rbc", "rblf")
736
737def init(g, handle):
738 cfg = rblf.cfg(handle)
739 rblf.enforce_product_packages_exist("")
740 rblf.enforce_product_packages_exist("foo")
741 rblf.require_artifacts_in_path("foo", "bar")
742 rblf.require_artifacts_in_path_relaxed("foo", "bar")
Sasha Smundakd6797852021-11-15 13:01:53 -0800743 rblf.mkdist_for_goals(g, "goal", "from:to")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800744`,
745 },
746 {
747 desc: "list with functions",
748 mkname: "product.mk",
749 in: `
750PRODUCT_COPY_FILES := $(call find-copy-subdir-files,*.kl,from1,to1) \
751 $(call find-copy-subdir-files,*.kc,from2,to2) \
752 foo bar
753`,
754 expected: `load("//build/make/core:product_config.rbc", "rblf")
755
756def init(g, handle):
757 cfg = rblf.cfg(handle)
758 cfg["PRODUCT_COPY_FILES"] = (rblf.find_and_copy("*.kl", "from1", "to1") +
759 rblf.find_and_copy("*.kc", "from2", "to2") +
760 [
761 "foo",
762 "bar",
763 ])
764`,
765 },
766 {
767 desc: "Text functions",
768 mkname: "product.mk",
769 in: `
770PRODUCT_COPY_FILES := $(addprefix pfx-,a b c)
771PRODUCT_COPY_FILES := $(addsuffix .sff, a b c)
772PRODUCT_NAME := $(word 1, $(subst ., ,$(TARGET_BOARD_PLATFORM)))
Sasha Smundak35434ed2021-11-05 16:29:56 -0700773$(info $(patsubst %.pub,$(PRODUCT_NAME)%,$(PRODUCT_ADB_KEYS)))
Sasha Smundak16e07732021-07-23 11:38:23 -0700774$(info $(dir foo/bar))
775$(info $(firstword $(PRODUCT_COPY_FILES)))
776$(info $(lastword $(PRODUCT_COPY_FILES)))
777$(info $(dir $(lastword $(MAKEFILE_LIST))))
778$(info $(dir $(lastword $(PRODUCT_COPY_FILES))))
779$(info $(dir $(lastword $(foobar))))
780$(info $(abspath foo/bar))
781$(info $(notdir foo/bar))
Sasha Smundak3deb9682021-07-26 18:42:25 -0700782$(call add_soong_config_namespace,snsconfig)
783$(call add_soong_config_var_value,snsconfig,imagetype,odm_image)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700784$(call soong_config_set, snsconfig, foo, foo_value)
785$(call soong_config_append, snsconfig, bar, bar_value)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700786PRODUCT_COPY_FILES := $(call copy-files,$(wildcard foo*.mk),etc)
Sasha Smundak04453082021-08-17 18:14:41 -0700787PRODUCT_COPY_FILES := $(call product-copy-files-by-pattern,from/%,to/%,a b c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800788`,
789 expected: `load("//build/make/core:product_config.rbc", "rblf")
790
791def init(g, handle):
792 cfg = rblf.cfg(handle)
793 cfg["PRODUCT_COPY_FILES"] = rblf.addprefix("pfx-", "a b c")
794 cfg["PRODUCT_COPY_FILES"] = rblf.addsuffix(".sff", "a b c")
795 cfg["PRODUCT_NAME"] = ((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " ")).split()[0]
Sasha Smundak35434ed2021-11-05 16:29:56 -0700796 rblf.mkinfo("product.mk", rblf.mkpatsubst("%.pub", "%s%%" % cfg["PRODUCT_NAME"], g.get("PRODUCT_ADB_KEYS", "")))
Sasha Smundak16e07732021-07-23 11:38:23 -0700797 rblf.mkinfo("product.mk", rblf.dir("foo/bar"))
798 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][0])
799 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][-1])
800 rblf.mkinfo("product.mk", rblf.dir("product.mk"))
801 rblf.mkinfo("product.mk", rblf.dir(cfg["PRODUCT_COPY_FILES"][-1]))
802 rblf.mkinfo("product.mk", rblf.dir((_foobar).split()[-1]))
803 rblf.mkinfo("product.mk", rblf.abspath("foo/bar"))
804 rblf.mkinfo("product.mk", rblf.notdir("foo/bar"))
Sasha Smundak65b547e2021-09-17 15:35:41 -0700805 rblf.soong_config_namespace(g, "snsconfig")
806 rblf.soong_config_set(g, "snsconfig", "imagetype", "odm_image")
807 rblf.soong_config_set(g, "snsconfig", "foo", "foo_value")
808 rblf.soong_config_append(g, "snsconfig", "bar", "bar_value")
Sasha Smundak3deb9682021-07-26 18:42:25 -0700809 cfg["PRODUCT_COPY_FILES"] = rblf.copy_files(rblf.expand_wildcard("foo*.mk"), "etc")
Sasha Smundak04453082021-08-17 18:14:41 -0700810 cfg["PRODUCT_COPY_FILES"] = rblf.product_copy_files_by_pattern("from/%", "to/%", "a b c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800811`,
812 },
813 {
Sasha Smundak9d011ab2021-07-09 16:00:57 -0700814 desc: "subst in list",
815 mkname: "product.mk",
816 in: `
817files = $(call find-copy-subdir-files,*,from,to)
818PRODUCT_COPY_FILES += $(subst foo,bar,$(files))
819`,
820 expected: `load("//build/make/core:product_config.rbc", "rblf")
821
822def init(g, handle):
823 cfg = rblf.cfg(handle)
824 _files = rblf.find_and_copy("*", "from", "to")
825 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
826 cfg["PRODUCT_COPY_FILES"] += rblf.mksubst("foo", "bar", _files)
827`,
828 },
829 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800830 desc: "assignment flavors",
831 mkname: "product.mk",
832 in: `
833PRODUCT_LIST1 := a
834PRODUCT_LIST2 += a
835PRODUCT_LIST1 += b
836PRODUCT_LIST2 += b
837PRODUCT_LIST3 ?= a
838PRODUCT_LIST1 = c
839PLATFORM_LIST += x
840PRODUCT_PACKAGES := $(PLATFORM_LIST)
841`,
842 expected: `load("//build/make/core:product_config.rbc", "rblf")
843
844def init(g, handle):
845 cfg = rblf.cfg(handle)
846 cfg["PRODUCT_LIST1"] = ["a"]
847 rblf.setdefault(handle, "PRODUCT_LIST2")
848 cfg["PRODUCT_LIST2"] += ["a"]
849 cfg["PRODUCT_LIST1"] += ["b"]
850 cfg["PRODUCT_LIST2"] += ["b"]
851 if cfg.get("PRODUCT_LIST3") == None:
852 cfg["PRODUCT_LIST3"] = ["a"]
853 cfg["PRODUCT_LIST1"] = ["c"]
854 g.setdefault("PLATFORM_LIST", [])
855 g["PLATFORM_LIST"] += ["x"]
856 cfg["PRODUCT_PACKAGES"] = g["PLATFORM_LIST"][:]
857`,
858 },
859 {
860 desc: "assigment flavors2",
861 mkname: "product.mk",
862 in: `
863PRODUCT_LIST1 = a
864ifeq (0,1)
865 PRODUCT_LIST1 += b
866 PRODUCT_LIST2 += b
867endif
868PRODUCT_LIST1 += c
869PRODUCT_LIST2 += c
870`,
871 expected: `load("//build/make/core:product_config.rbc", "rblf")
872
873def init(g, handle):
874 cfg = rblf.cfg(handle)
875 cfg["PRODUCT_LIST1"] = ["a"]
876 if "0" == "1":
877 cfg["PRODUCT_LIST1"] += ["b"]
878 rblf.setdefault(handle, "PRODUCT_LIST2")
879 cfg["PRODUCT_LIST2"] += ["b"]
880 cfg["PRODUCT_LIST1"] += ["c"]
881 rblf.setdefault(handle, "PRODUCT_LIST2")
882 cfg["PRODUCT_LIST2"] += ["c"]
883`,
884 },
885 {
Sasha Smundak3deb9682021-07-26 18:42:25 -0700886 desc: "soong namespace assignments",
887 mkname: "product.mk",
888 in: `
889SOONG_CONFIG_NAMESPACES += cvd
890SOONG_CONFIG_cvd += launch_configs
Sasha Smundak65b547e2021-09-17 15:35:41 -0700891SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
Sasha Smundak3deb9682021-07-26 18:42:25 -0700892SOONG_CONFIG_cvd += grub_config
893SOONG_CONFIG_cvd_grub_config += grub.cfg
Sasha Smundak65b547e2021-09-17 15:35:41 -0700894x := $(SOONG_CONFIG_cvd_grub_config)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700895`,
896 expected: `load("//build/make/core:product_config.rbc", "rblf")
897
898def init(g, handle):
899 cfg = rblf.cfg(handle)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700900 rblf.soong_config_namespace(g, "cvd")
901 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
902 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
Sasha Smundak422b6142021-11-11 18:31:59 -0800903 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 -0700904`,
Cole Faustc00184e2021-11-08 12:08:57 -0800905 }, {
906 desc: "soong namespace accesses",
907 mkname: "product.mk",
908 in: `
909SOONG_CONFIG_NAMESPACES += cvd
910SOONG_CONFIG_cvd += launch_configs
911SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
912SOONG_CONFIG_cvd += grub_config
913SOONG_CONFIG_cvd_grub_config += grub.cfg
914x := $(call soong_config_get,cvd,grub_config)
915`,
916 expected: `load("//build/make/core:product_config.rbc", "rblf")
917
918def init(g, handle):
919 cfg = rblf.cfg(handle)
920 rblf.soong_config_namespace(g, "cvd")
921 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
922 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
923 _x = rblf.soong_config_get(g, "cvd", "grub_config")
924`,
Sasha Smundak3deb9682021-07-26 18:42:25 -0700925 },
926 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800927 desc: "string split",
928 mkname: "product.mk",
929 in: `
930PRODUCT_LIST1 = a
931local = b
932local += c
933FOO = d
934FOO += e
935PRODUCT_LIST1 += $(local)
936PRODUCT_LIST1 += $(FOO)
937`,
938 expected: `load("//build/make/core:product_config.rbc", "rblf")
939
940def init(g, handle):
941 cfg = rblf.cfg(handle)
942 cfg["PRODUCT_LIST1"] = ["a"]
943 _local = "b"
944 _local += " " + "c"
945 g["FOO"] = "d"
946 g["FOO"] += " " + "e"
947 cfg["PRODUCT_LIST1"] += (_local).split()
948 cfg["PRODUCT_LIST1"] += (g["FOO"]).split()
949`,
950 },
951 {
952 desc: "apex_jars",
953 mkname: "product.mk",
954 in: `
955PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex
956`,
957 expected: `load("//build/make/core:product_config.rbc", "rblf")
958
959def init(g, handle):
960 cfg = rblf.cfg(handle)
961 cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) +
962 ["framework-minus-apex"])
963`,
964 },
965 {
966 desc: "strip function",
967 mkname: "product.mk",
968 in: `
969ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),)
970 PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress)
971endif
972`,
973 expected: `load("//build/make/core:product_config.rbc", "rblf")
974
975def init(g, handle):
976 cfg = rblf.cfg(handle)
977 if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []):
978 cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split()
979`,
980 },
981 {
982 desc: "strip func in condition",
983 mkname: "product.mk",
984 in: `
985ifneq ($(strip $(TARGET_VENDOR)),)
986endif
987`,
988 expected: `load("//build/make/core:product_config.rbc", "rblf")
989
990def init(g, handle):
991 cfg = rblf.cfg(handle)
Sasha Smundak0554d762021-07-08 18:26:12 -0700992 if rblf.mkstrip(g.get("TARGET_VENDOR", "")):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800993 pass
994`,
995 },
996 {
997 desc: "ref after set",
998 mkname: "product.mk",
999 in: `
1000PRODUCT_ADB_KEYS:=value
1001FOO := $(PRODUCT_ADB_KEYS)
1002ifneq (,$(PRODUCT_ADB_KEYS))
1003endif
1004`,
1005 expected: `load("//build/make/core:product_config.rbc", "rblf")
1006
1007def init(g, handle):
1008 cfg = rblf.cfg(handle)
1009 g["PRODUCT_ADB_KEYS"] = "value"
1010 g["FOO"] = g["PRODUCT_ADB_KEYS"]
1011 if g["PRODUCT_ADB_KEYS"]:
1012 pass
1013`,
1014 },
1015 {
1016 desc: "ref before set",
1017 mkname: "product.mk",
1018 in: `
1019V1 := $(PRODUCT_ADB_KEYS)
1020ifeq (,$(PRODUCT_ADB_KEYS))
1021 V2 := $(PRODUCT_ADB_KEYS)
1022 PRODUCT_ADB_KEYS:=foo
1023 V3 := $(PRODUCT_ADB_KEYS)
1024endif`,
1025 expected: `load("//build/make/core:product_config.rbc", "rblf")
1026
1027def init(g, handle):
1028 cfg = rblf.cfg(handle)
1029 g["V1"] = g.get("PRODUCT_ADB_KEYS", "")
1030 if not g.get("PRODUCT_ADB_KEYS", ""):
1031 g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
1032 g["PRODUCT_ADB_KEYS"] = "foo"
1033 g["V3"] = g["PRODUCT_ADB_KEYS"]
1034`,
1035 },
Sasha Smundak6609ba72021-07-22 18:32:56 -07001036 {
1037 desc: "Dynamic inherit path",
1038 mkname: "product.mk",
1039 in: `
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001040MY_PATH:=foo
Sasha Smundak6609ba72021-07-22 18:32:56 -07001041$(call inherit-product,vendor/$(MY_PATH)/cfg.mk)
1042`,
1043 expected: `load("//build/make/core:product_config.rbc", "rblf")
1044load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1045load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init")
1046
1047def init(g, handle):
1048 cfg = rblf.cfg(handle)
1049 g["MY_PATH"] = "foo"
1050 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001051 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1052 "vendor/bar/baz/cfg.mk": ("vendor/bar/baz/cfg", _cfg1_init),
Sasha Smundak6609ba72021-07-22 18:32:56 -07001053 }.get("vendor/%s/cfg.mk" % g["MY_PATH"])
1054 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1055 if not _varmod_init:
Cole Faust7321b092021-12-21 16:11:16 -08001056 rblf.mkerror("product.mk", "Cannot find %s" % ("vendor/%s/cfg.mk" % g["MY_PATH"]))
Sasha Smundak6609ba72021-07-22 18:32:56 -07001057 rblf.inherit(handle, _varmod, _varmod_init)
1058`,
1059 },
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001060 {
1061 desc: "Dynamic inherit with hint",
1062 mkname: "product.mk",
1063 in: `
1064MY_PATH:=foo
1065#RBC# include_top vendor/foo1
1066$(call inherit-product,$(MY_PATH)/cfg.mk)
1067`,
1068 expected: `load("//build/make/core:product_config.rbc", "rblf")
1069load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1070
1071def init(g, handle):
1072 cfg = rblf.cfg(handle)
1073 g["MY_PATH"] = "foo"
Cole Faust069aba62022-01-26 17:47:33 -08001074 rblf.inherit(handle, "vendor/foo1/cfg", _cfg_init)
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001075`,
1076 },
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001077 {
Cole Faustf7ed5342021-12-21 14:15:12 -08001078 desc: "Dynamic inherit with duplicated hint",
1079 mkname: "product.mk",
1080 in: `
1081MY_PATH:=foo
1082#RBC# include_top vendor/foo1
1083$(call inherit-product,$(MY_PATH)/cfg.mk)
1084#RBC# include_top vendor/foo1
Cole Faust7940c6a2022-01-31 15:54:05 -08001085#RBC# include_top vendor/foo1
Cole Faustf7ed5342021-12-21 14:15:12 -08001086$(call inherit-product,$(MY_PATH)/cfg.mk)
1087`,
1088 expected: `load("//build/make/core:product_config.rbc", "rblf")
1089load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1090
1091def init(g, handle):
1092 cfg = rblf.cfg(handle)
1093 g["MY_PATH"] = "foo"
Cole Faust069aba62022-01-26 17:47:33 -08001094 rblf.inherit(handle, "vendor/foo1/cfg", _cfg_init)
Cole Faust069aba62022-01-26 17:47:33 -08001095 rblf.inherit(handle, "vendor/foo1/cfg", _cfg_init)
Cole Faustf7ed5342021-12-21 14:15:12 -08001096`,
1097 },
1098 {
Cole Faust069aba62022-01-26 17:47:33 -08001099 desc: "Dynamic inherit path that lacks hint",
Cole Faust6c934f62022-01-06 15:51:12 -08001100 mkname: "product.mk",
1101 in: `
1102#RBC# include_top foo
1103$(call inherit-product,$(MY_VAR)/font.mk)
1104
1105#RBC# include_top foo
1106
1107# There's some space and even this comment between the include_top and the inherit-product
1108
1109$(call inherit-product,$(MY_VAR)/font.mk)
1110
1111$(call inherit-product,$(MY_VAR)/font.mk)
1112`,
Cole Faust7940c6a2022-01-31 15:54:05 -08001113 expected: `load("//build/make/core:product_config.rbc", "rblf")
Cole Faust6c934f62022-01-06 15:51:12 -08001114load("//foo:font.star|init", _font_init = "init")
Cole Faust069aba62022-01-26 17:47:33 -08001115load("//bar:font.star|init", _font1_init = "init")
Cole Faust6c934f62022-01-06 15:51:12 -08001116
1117def init(g, handle):
1118 cfg = rblf.cfg(handle)
Cole Faust069aba62022-01-26 17:47:33 -08001119 rblf.inherit(handle, "foo/font", _font_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001120 # There's some space and even this comment between the include_top and the inherit-product
Cole Faust069aba62022-01-26 17:47:33 -08001121 rblf.inherit(handle, "foo/font", _font_init)
Cole Faustf4e72cf2022-02-08 12:49:37 -08001122 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 -08001123 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001124 "foo/font.mk": ("foo/font", _font_init),
Cole Faust069aba62022-01-26 17:47:33 -08001125 "bar/font.mk": ("bar/font", _font1_init),
Cole Faust6c934f62022-01-06 15:51:12 -08001126 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1127 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1128 if not _varmod_init:
1129 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1130 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001131`,
1132 },
1133 {
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001134 desc: "Ignore make rules",
1135 mkname: "product.mk",
1136 in: `
1137foo: foo.c
1138 gcc -o $@ $*`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001139 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001140
1141def init(g, handle):
1142 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001143 rblf.mk2rbc_error("product.mk:2", "unsupported line rule: foo: foo.c\n#gcc -o $@ $*")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001144`,
1145 },
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001146 {
1147 desc: "Flag override",
1148 mkname: "product.mk",
1149 in: `
1150override FOO:=`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001151 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001152
1153def init(g, handle):
1154 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001155 rblf.mk2rbc_error("product.mk:2", "cannot handle override directive")
Sasha Smundak422b6142021-11-11 18:31:59 -08001156`,
1157 },
1158 {
1159 desc: "Bad expression",
1160 mkname: "build/product.mk",
1161 in: `
1162ifeq (,$(call foobar))
1163endif
1164`,
1165 expected: `load("//build/make/core:product_config.rbc", "rblf")
1166
1167def init(g, handle):
1168 cfg = rblf.cfg(handle)
1169 if rblf.mk2rbc_error("build/product.mk:2", "cannot handle invoking foobar"):
1170 pass
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001171`,
1172 },
Cole Faust4eadba72021-12-07 11:54:52 -08001173 {
1174 desc: "if expression",
1175 mkname: "product.mk",
1176 in: `
1177TEST_VAR := foo
1178TEST_VAR_LIST := foo
1179TEST_VAR_LIST += bar
1180TEST_VAR_2 := $(if $(TEST_VAR),bar)
1181TEST_VAR_3 := $(if $(TEST_VAR),bar,baz)
1182TEST_VAR_3 := $(if $(TEST_VAR),$(TEST_VAR_LIST))
1183`,
1184 expected: `load("//build/make/core:product_config.rbc", "rblf")
1185
1186def init(g, handle):
1187 cfg = rblf.cfg(handle)
1188 g["TEST_VAR"] = "foo"
1189 g["TEST_VAR_LIST"] = ["foo"]
1190 g["TEST_VAR_LIST"] += ["bar"]
1191 g["TEST_VAR_2"] = ("bar" if g["TEST_VAR"] else "")
1192 g["TEST_VAR_3"] = ("bar" if g["TEST_VAR"] else "baz")
1193 g["TEST_VAR_3"] = (g["TEST_VAR_LIST"] if g["TEST_VAR"] else [])
1194`,
1195 },
Cole Faustc36c9622021-12-07 15:20:45 -08001196 {
1197 desc: "substitution references",
1198 mkname: "product.mk",
1199 in: `
1200SOURCES := foo.c bar.c
1201OBJECTS := $(SOURCES:.c=.o)
1202OBJECTS2 := $(SOURCES:%.c=%.o)
1203`,
1204 expected: `load("//build/make/core:product_config.rbc", "rblf")
1205
1206def init(g, handle):
1207 cfg = rblf.cfg(handle)
1208 g["SOURCES"] = "foo.c bar.c"
1209 g["OBJECTS"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1210 g["OBJECTS2"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1211`,
1212 },
Cole Faustb0d32ab2021-12-09 14:00:59 -08001213 {
1214 desc: "foreach expressions",
1215 mkname: "product.mk",
1216 in: `
1217BOOT_KERNEL_MODULES := foo.ko bar.ko
1218BOOT_KERNEL_MODULES_FILTER := $(foreach m,$(BOOT_KERNEL_MODULES),%/$(m))
1219BOOT_KERNEL_MODULES_LIST := foo.ko
1220BOOT_KERNEL_MODULES_LIST += bar.ko
1221BOOT_KERNEL_MODULES_FILTER_2 := $(foreach m,$(BOOT_KERNEL_MODULES_LIST),%/$(m))
1222
1223`,
1224 expected: `load("//build/make/core:product_config.rbc", "rblf")
1225
1226def init(g, handle):
1227 cfg = rblf.cfg(handle)
1228 g["BOOT_KERNEL_MODULES"] = "foo.ko bar.ko"
1229 g["BOOT_KERNEL_MODULES_FILTER"] = ["%%/%s" % m for m in rblf.words(g["BOOT_KERNEL_MODULES"])]
1230 g["BOOT_KERNEL_MODULES_LIST"] = ["foo.ko"]
1231 g["BOOT_KERNEL_MODULES_LIST"] += ["bar.ko"]
1232 g["BOOT_KERNEL_MODULES_FILTER_2"] = ["%%/%s" % m for m in g["BOOT_KERNEL_MODULES_LIST"]]
1233`,
1234 },
Cole Faust0484c232021-12-22 14:08:08 -08001235 {
1236 desc: "List appended to string",
1237 mkname: "product.mk",
1238 in: `
1239NATIVE_BRIDGE_PRODUCT_PACKAGES := \
1240 libnative_bridge_vdso.native_bridge \
1241 native_bridge_guest_app_process.native_bridge \
1242 native_bridge_guest_linker.native_bridge
1243
1244NATIVE_BRIDGE_MODIFIED_GUEST_LIBS := \
1245 libaaudio \
1246 libamidi \
1247 libandroid \
1248 libandroid_runtime
1249
1250NATIVE_BRIDGE_PRODUCT_PACKAGES += \
1251 $(addsuffix .native_bridge,$(NATIVE_BRIDGE_ORIG_GUEST_LIBS))
1252`,
1253 expected: `load("//build/make/core:product_config.rbc", "rblf")
1254
1255def init(g, handle):
1256 cfg = rblf.cfg(handle)
1257 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] = "libnative_bridge_vdso.native_bridge native_bridge_guest_app_process.native_bridge native_bridge_guest_linker.native_bridge"
1258 g["NATIVE_BRIDGE_MODIFIED_GUEST_LIBS"] = "libaaudio libamidi libandroid libandroid_runtime"
1259 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] += " " + " ".join(rblf.addsuffix(".native_bridge", g.get("NATIVE_BRIDGE_ORIG_GUEST_LIBS", "")))
1260`,
1261 },
Cole Faustb1103e22022-01-06 15:22:05 -08001262 {
1263 desc: "Math functions",
1264 mkname: "product.mk",
1265 in: `
1266# Test the math functions defined in build/make/common/math.mk
1267ifeq ($(call math_max,2,5),5)
1268endif
1269ifeq ($(call math_min,2,5),2)
1270endif
1271ifeq ($(call math_gt_or_eq,2,5),true)
1272endif
1273ifeq ($(call math_gt,2,5),true)
1274endif
1275ifeq ($(call math_lt,2,5),true)
1276endif
1277ifeq ($(call math_gt_or_eq,2,5),)
1278endif
1279ifeq ($(call math_gt,2,5),)
1280endif
1281ifeq ($(call math_lt,2,5),)
1282endif
1283ifeq ($(call math_gt_or_eq,$(MY_VAR), 5),true)
1284endif
1285ifeq ($(call math_gt_or_eq,$(MY_VAR),$(MY_OTHER_VAR)),true)
1286endif
1287ifeq ($(call math_gt_or_eq,100$(MY_VAR),10),true)
1288endif
1289`,
1290 expected: `# Test the math functions defined in build/make/common/math.mk
1291load("//build/make/core:product_config.rbc", "rblf")
1292
1293def init(g, handle):
1294 cfg = rblf.cfg(handle)
1295 if max(2, 5) == 5:
1296 pass
1297 if min(2, 5) == 2:
1298 pass
1299 if 2 >= 5:
1300 pass
1301 if 2 > 5:
1302 pass
1303 if 2 < 5:
1304 pass
1305 if 2 < 5:
1306 pass
1307 if 2 <= 5:
1308 pass
1309 if 2 >= 5:
1310 pass
1311 if int(g.get("MY_VAR", "")) >= 5:
1312 pass
1313 if int(g.get("MY_VAR", "")) >= int(g.get("MY_OTHER_VAR", "")):
1314 pass
1315 if int("100%s" % g.get("MY_VAR", "")) >= 10:
1316 pass
1317`,
1318 },
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001319}
1320
1321var known_variables = []struct {
1322 name string
1323 class varClass
1324 starlarkType
1325}{
Cole Faustf1f44d32021-11-16 14:52:12 -08001326 {"NATIVE_COVERAGE", VarClassSoong, starlarkTypeBool},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001327 {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
1328 {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
1329 {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
1330 {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
1331 {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
1332 {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
1333 {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
1334 {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
1335 {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
1336 {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
1337 {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
1338 {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
1339 {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
1340 {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
1341}
1342
Sasha Smundak6609ba72021-07-22 18:32:56 -07001343type testMakefileFinder struct {
1344 fs fs.FS
1345 root string
1346 files []string
1347}
1348
1349func (t *testMakefileFinder) Find(root string) []string {
1350 if t.files != nil || root == t.root {
1351 return t.files
1352 }
1353 t.files = make([]string, 0)
1354 fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error {
1355 if err != nil {
1356 return err
1357 }
1358 if d.IsDir() {
1359 base := filepath.Base(path)
1360 if base[0] == '.' && len(base) > 1 {
1361 return fs.SkipDir
1362 }
1363 return nil
1364 }
1365 if strings.HasSuffix(path, ".mk") {
1366 t.files = append(t.files, path)
1367 }
1368 return nil
1369 })
1370 return t.files
1371}
1372
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001373func TestGood(t *testing.T) {
1374 for _, v := range known_variables {
1375 KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
1376 }
Sasha Smundak6609ba72021-07-22 18:32:56 -07001377 fs := NewFindMockFS([]string{
1378 "vendor/foo1/cfg.mk",
1379 "vendor/bar/baz/cfg.mk",
1380 "part.mk",
1381 "foo/font.mk",
1382 "bar/font.mk",
1383 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001384 for _, test := range testCases {
1385 t.Run(test.desc,
1386 func(t *testing.T) {
1387 ss, err := Convert(Request{
Sasha Smundak422b6142021-11-11 18:31:59 -08001388 MkFile: test.mkname,
1389 Reader: bytes.NewBufferString(test.in),
Sasha Smundak422b6142021-11-11 18:31:59 -08001390 OutputSuffix: ".star",
1391 SourceFS: fs,
1392 MakefileFinder: &testMakefileFinder{fs: fs},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001393 })
1394 if err != nil {
1395 t.Error(err)
1396 return
1397 }
1398 got := ss.String()
1399 if got != test.expected {
1400 t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
1401 strings.ReplaceAll(test.expected, "\n", "␤\n"),
1402 strings.ReplaceAll(got, "\n", "␤\n"))
1403 }
1404 })
1405 }
1406}