blob: 193e6357c6ed8c3bd34f42776dc9cfedce04d13a [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
Cole Faust3c4fc992022-02-28 16:05:01 -080068local-var-with-dashes := bar
69$(warning local-var-with-dashes: $(local-var-with-dashes))
70GLOBAL-VAR-WITH-DASHES := baz
71$(warning GLOBAL-VAR-WITH-DASHES: $(GLOBAL-VAR-WITH-DASHES))
Sasha Smundakb051c4e2020-11-05 20:45:07 -080072`,
73 expected: `load("//build/make/core:product_config.rbc", "rblf")
74
75def init(g, handle):
76 cfg = rblf.cfg(handle)
77 cfg["PRODUCT_NAME"] = "Pixel 3"
78 cfg["PRODUCT_MODEL"] = ""
79 _local_var = "foo"
Cole Faust3c4fc992022-02-28 16:05:01 -080080 _local_var_with_dashes = "bar"
81 rblf.mkwarning("pixel3.mk", "local-var-with-dashes: %s" % _local_var_with_dashes)
82 g["GLOBAL-VAR-WITH-DASHES"] = "baz"
83 rblf.mkwarning("pixel3.mk", "GLOBAL-VAR-WITH-DASHES: %s" % g["GLOBAL-VAR-WITH-DASHES"])
Sasha Smundakb051c4e2020-11-05 20:45:07 -080084`,
85 },
86 {
87 desc: "List variable",
88 mkname: "pixel4.mk",
89 in: `
90PRODUCT_PACKAGES = package1 package2
91PRODUCT_COPY_FILES += file2:target
92PRODUCT_PACKAGES += package3
93PRODUCT_COPY_FILES =
94`,
95 expected: `load("//build/make/core:product_config.rbc", "rblf")
96
97def init(g, handle):
98 cfg = rblf.cfg(handle)
99 cfg["PRODUCT_PACKAGES"] = [
100 "package1",
101 "package2",
102 ]
103 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
104 cfg["PRODUCT_COPY_FILES"] += ["file2:target"]
105 cfg["PRODUCT_PACKAGES"] += ["package3"]
106 cfg["PRODUCT_COPY_FILES"] = []
107`,
108 },
109 {
110 desc: "Unknown function",
111 mkname: "product.mk",
112 in: `
Sasha Smundak6609ba72021-07-22 18:32:56 -0700113PRODUCT_NAME := $(call foo1, bar)
114PRODUCT_NAME := $(call foo0)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800115`,
Sasha Smundak422b6142021-11-11 18:31:59 -0800116 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800117
118def init(g, handle):
119 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -0800120 rblf.mk2rbc_error("product.mk:2", "cannot handle invoking foo1")
121 rblf.mk2rbc_error("product.mk:3", "cannot handle invoking foo0")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800122`,
123 },
124 {
125 desc: "Inherit configuration always",
126 mkname: "product.mk",
127 in: `
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800128$(call inherit-product, part.mk)
Sasha Smundak868c5e32021-09-23 16:20:58 -0700129ifdef PRODUCT_NAME
130$(call inherit-product, part1.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800131else # Comment
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800132$(call inherit-product, $(LOCAL_PATH)/part.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800133endif
134`,
135 expected: `load("//build/make/core:product_config.rbc", "rblf")
136load(":part.star", _part_init = "init")
Sasha Smundak868c5e32021-09-23 16:20:58 -0700137load(":part1.star|init", _part1_init = "init")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800138
139def init(g, handle):
140 cfg = rblf.cfg(handle)
Sasha Smundak868c5e32021-09-23 16:20:58 -0700141 rblf.inherit(handle, "part", _part_init)
Cole Faust71514c02022-01-27 17:21:41 -0800142 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800143 if not _part1_init:
144 rblf.mkerror("product.mk", "Cannot find %s" % (":part1.star"))
Sasha Smundak868c5e32021-09-23 16:20:58 -0700145 rblf.inherit(handle, "part1", _part1_init)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800146 else:
147 # Comment
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800148 rblf.inherit(handle, "part", _part_init)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800149`,
150 },
151 {
152 desc: "Inherit configuration if it exists",
153 mkname: "product.mk",
154 in: `
155$(call inherit-product-if-exists, part.mk)
156`,
157 expected: `load("//build/make/core:product_config.rbc", "rblf")
158load(":part.star|init", _part_init = "init")
159
160def init(g, handle):
161 cfg = rblf.cfg(handle)
Sasha Smundak6609ba72021-07-22 18:32:56 -0700162 if _part_init:
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800163 rblf.inherit(handle, "part", _part_init)
164`,
165 },
166
167 {
168 desc: "Include configuration",
169 mkname: "product.mk",
170 in: `
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800171include part.mk
Sasha Smundak868c5e32021-09-23 16:20:58 -0700172ifdef PRODUCT_NAME
173include part1.mk
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800174else
Sasha Smundak868c5e32021-09-23 16:20:58 -0700175-include $(LOCAL_PATH)/part1.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800176endif
177`,
178 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundak868c5e32021-09-23 16:20:58 -0700179load(":part.star", _part_init = "init")
180load(":part1.star|init", _part1_init = "init")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800181
182def init(g, handle):
183 cfg = rblf.cfg(handle)
Sasha Smundak868c5e32021-09-23 16:20:58 -0700184 _part_init(g, handle)
Cole Faust71514c02022-01-27 17:21:41 -0800185 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundak6bc132a2022-01-10 17:02:16 -0800186 if not _part1_init:
187 rblf.mkerror("product.mk", "Cannot find %s" % (":part1.star"))
Sasha Smundak868c5e32021-09-23 16:20:58 -0700188 _part1_init(g, handle)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800189 else:
Sasha Smundak868c5e32021-09-23 16:20:58 -0700190 if _part1_init != None:
191 _part1_init(g, handle)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800192`,
193 },
194
195 {
196 desc: "Synonymous inherited configurations",
197 mkname: "path/product.mk",
198 in: `
Sasha Smundak6609ba72021-07-22 18:32:56 -0700199$(call inherit-product, */font.mk)
Cole Faust62e05112022-04-05 17:56:11 -0700200$(call inherit-product, $(sort $(wildcard */font.mk)))
201$(call inherit-product, $(wildcard */font.mk))
202
203include */font.mk
204include $(sort $(wildcard */font.mk))
205include $(wildcard */font.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800206`,
207 expected: `load("//build/make/core:product_config.rbc", "rblf")
Cole Faust62e05112022-04-05 17:56:11 -0700208load("//bar:font.star", _font_init = "init")
209load("//foo:font.star", _font1_init = "init")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800210
211def init(g, handle):
212 cfg = rblf.cfg(handle)
Cole Faust62e05112022-04-05 17:56:11 -0700213 rblf.inherit(handle, "bar/font", _font_init)
214 rblf.inherit(handle, "foo/font", _font1_init)
215 rblf.inherit(handle, "bar/font", _font_init)
216 rblf.inherit(handle, "foo/font", _font1_init)
217 rblf.inherit(handle, "bar/font", _font_init)
218 rblf.inherit(handle, "foo/font", _font1_init)
219 _font_init(g, handle)
220 _font1_init(g, handle)
221 _font_init(g, handle)
222 _font1_init(g, handle)
223 _font_init(g, handle)
224 _font1_init(g, handle)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800225`,
226 },
227 {
228 desc: "Directive define",
229 mkname: "product.mk",
230 in: `
231define some-macro
232 $(info foo)
233endef
234`,
Sasha Smundak422b6142021-11-11 18:31:59 -0800235 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800236
237def init(g, handle):
238 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -0800239 rblf.mk2rbc_error("product.mk:2", "define is not supported: some-macro")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800240`,
241 },
242 {
243 desc: "Ifdef",
244 mkname: "product.mk",
245 in: `
246ifdef PRODUCT_NAME
247 PRODUCT_NAME = gizmo
248else
249endif
Sasha Smundakc4fa93e2021-11-05 14:38:46 -0700250local_var :=
251ifdef local_var
252endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800253`,
254 expected: `load("//build/make/core:product_config.rbc", "rblf")
255
256def init(g, handle):
257 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800258 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800259 cfg["PRODUCT_NAME"] = "gizmo"
260 else:
261 pass
Sasha Smundakc4fa93e2021-11-05 14:38:46 -0700262 _local_var = ""
263 if _local_var:
264 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800265`,
266 },
267 {
268 desc: "Simple functions",
269 mkname: "product.mk",
270 in: `
271$(warning this is the warning)
272$(warning)
Cole Fauste309a912022-03-16 13:42:34 -0700273$(warning # this warning starts with a pound)
274$(warning this warning has a # in the middle)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800275$(info this is the info)
276$(error this is the error)
277PRODUCT_NAME:=$(shell echo *)
278`,
279 expected: `load("//build/make/core:product_config.rbc", "rblf")
280
281def init(g, handle):
282 cfg = rblf.cfg(handle)
283 rblf.mkwarning("product.mk", "this is the warning")
284 rblf.mkwarning("product.mk", "")
Cole Fauste309a912022-03-16 13:42:34 -0700285 rblf.mkwarning("product.mk", "# this warning starts with a pound")
286 rblf.mkwarning("product.mk", "this warning has a # in the middle")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800287 rblf.mkinfo("product.mk", "this is the info")
288 rblf.mkerror("product.mk", "this is the error")
289 cfg["PRODUCT_NAME"] = rblf.shell("echo *")
290`,
291 },
292 {
293 desc: "Empty if",
294 mkname: "product.mk",
295 in: `
296ifdef PRODUCT_NAME
297# Comment
Sasha Smundak6609ba72021-07-22 18:32:56 -0700298else
Sasha Smundak02183cf2021-08-16 13:36:11 -0700299 TARGET_COPY_OUT_RECOVERY := foo
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800300endif
301`,
302 expected: `load("//build/make/core:product_config.rbc", "rblf")
303
304def init(g, handle):
305 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800306 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800307 # Comment
308 pass
Sasha Smundak6609ba72021-07-22 18:32:56 -0700309 else:
Sasha Smundak422b6142021-11-11 18:31:59 -0800310 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 -0800311`,
312 },
313 {
314 desc: "if/else/endif",
315 mkname: "product.mk",
316 in: `
317ifndef PRODUCT_NAME
318 PRODUCT_NAME=gizmo1
319else
320 PRODUCT_NAME=gizmo2
321endif
322`,
323 expected: `load("//build/make/core:product_config.rbc", "rblf")
324
325def init(g, handle):
326 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800327 if not cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800328 cfg["PRODUCT_NAME"] = "gizmo1"
329 else:
330 cfg["PRODUCT_NAME"] = "gizmo2"
331`,
332 },
333 {
334 desc: "else if",
335 mkname: "product.mk",
336 in: `
337ifdef PRODUCT_NAME
338 PRODUCT_NAME = gizmo
339else ifndef PRODUCT_PACKAGES # Comment
340endif
341 `,
342 expected: `load("//build/make/core:product_config.rbc", "rblf")
343
344def init(g, handle):
345 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800346 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800347 cfg["PRODUCT_NAME"] = "gizmo"
Cole Faust71514c02022-01-27 17:21:41 -0800348 elif not cfg.get("PRODUCT_PACKAGES", []):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800349 # Comment
350 pass
351`,
352 },
353 {
354 desc: "ifeq / ifneq",
355 mkname: "product.mk",
356 in: `
357ifeq (aosp_arm, $(TARGET_PRODUCT))
358 PRODUCT_MODEL = pix2
359else
360 PRODUCT_MODEL = pix21
361endif
362ifneq (aosp_x86, $(TARGET_PRODUCT))
363 PRODUCT_MODEL = pix3
364endif
365`,
366 expected: `load("//build/make/core:product_config.rbc", "rblf")
367
368def init(g, handle):
369 cfg = rblf.cfg(handle)
370 if "aosp_arm" == g["TARGET_PRODUCT"]:
371 cfg["PRODUCT_MODEL"] = "pix2"
372 else:
373 cfg["PRODUCT_MODEL"] = "pix21"
374 if "aosp_x86" != g["TARGET_PRODUCT"]:
375 cfg["PRODUCT_MODEL"] = "pix3"
376`,
377 },
378 {
Cole Faustf8320212021-11-10 15:05:07 -0800379 desc: "ifeq with soong_config_get",
380 mkname: "product.mk",
381 in: `
382ifeq (true,$(call soong_config_get,art_module,source_build))
383endif
384`,
385 expected: `load("//build/make/core:product_config.rbc", "rblf")
386
387def init(g, handle):
388 cfg = rblf.cfg(handle)
389 if "true" == rblf.soong_config_get(g, "art_module", "source_build"):
390 pass
391`,
392 },
393 {
Cole Faustf1f44d32021-11-16 14:52:12 -0800394 desc: "ifeq with $(NATIVE_COVERAGE)",
395 mkname: "product.mk",
396 in: `
397ifeq ($(NATIVE_COVERAGE),true)
398endif
399`,
400 expected: `load("//build/make/core:product_config.rbc", "rblf")
401
402def init(g, handle):
403 cfg = rblf.cfg(handle)
404 if g.get("NATIVE_COVERAGE", False):
405 pass
406`,
407 },
408 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800409 desc: "Check filter result",
410 mkname: "product.mk",
411 in: `
412ifeq (,$(filter userdebug eng, $(TARGET_BUILD_VARIANT)))
413endif
414ifneq (,$(filter userdebug,$(TARGET_BUILD_VARIANT))
415endif
416ifneq (,$(filter plaf,$(PLATFORM_LIST)))
417endif
418ifeq ($(TARGET_BUILD_VARIANT), $(filter $(TARGET_BUILD_VARIANT), userdebug eng))
419endif
Cole Faust9932f752022-02-08 11:56:25 -0800420ifneq (, $(filter $(TARGET_BUILD_VARIANT), userdebug eng))
421endif
422ifneq (,$(filter userdebug eng, $(TARGET_BUILD_VARIANT)))
423endif
Sasha Smundak0554d762021-07-08 18:26:12 -0700424ifneq (,$(filter true, $(v1)$(v2)))
425endif
Sasha Smundak5f463be2021-09-15 18:43:36 -0700426ifeq (,$(filter barbet coral%,$(TARGET_PRODUCT)))
427else ifneq (,$(filter barbet%,$(TARGET_PRODUCT)))
428endif
Cole Fausteec0d812021-12-06 16:23:51 -0800429ifeq (,$(filter-out sunfish_kasan, $(TARGET_PRODUCT)))
430endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800431`,
432 expected: `load("//build/make/core:product_config.rbc", "rblf")
433
434def init(g, handle):
435 cfg = rblf.cfg(handle)
Sasha Smundak5f463be2021-09-15 18:43:36 -0700436 if not rblf.filter("userdebug eng", g["TARGET_BUILD_VARIANT"]):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800437 pass
Sasha Smundak5f463be2021-09-15 18:43:36 -0700438 if rblf.filter("userdebug", g["TARGET_BUILD_VARIANT"]):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800439 pass
440 if "plaf" in g.get("PLATFORM_LIST", []):
441 pass
Cole Faust9932f752022-02-08 11:56:25 -0800442 if g["TARGET_BUILD_VARIANT"] == " ".join(rblf.filter(g["TARGET_BUILD_VARIANT"], "userdebug eng")):
443 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800444 if g["TARGET_BUILD_VARIANT"] in ["userdebug", "eng"]:
445 pass
Cole Faust9932f752022-02-08 11:56:25 -0800446 if rblf.filter("userdebug eng", g["TARGET_BUILD_VARIANT"]):
447 pass
Sasha Smundak5f463be2021-09-15 18:43:36 -0700448 if rblf.filter("true", "%s%s" % (_v1, _v2)):
449 pass
450 if not rblf.filter("barbet coral%", g["TARGET_PRODUCT"]):
451 pass
452 elif rblf.filter("barbet%", g["TARGET_PRODUCT"]):
Sasha Smundak0554d762021-07-08 18:26:12 -0700453 pass
Cole Fausteec0d812021-12-06 16:23:51 -0800454 if not rblf.filter_out("sunfish_kasan", g["TARGET_PRODUCT"]):
455 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800456`,
457 },
458 {
459 desc: "Get filter result",
460 mkname: "product.mk",
461 in: `
462PRODUCT_LIST2=$(filter-out %/foo.ko,$(wildcard path/*.ko))
463`,
464 expected: `load("//build/make/core:product_config.rbc", "rblf")
465
466def init(g, handle):
467 cfg = rblf.cfg(handle)
468 cfg["PRODUCT_LIST2"] = rblf.filter_out("%/foo.ko", rblf.expand_wildcard("path/*.ko"))
469`,
470 },
471 {
472 desc: "filter $(VAR), values",
473 mkname: "product.mk",
474 in: `
475ifeq (,$(filter $(TARGET_PRODUCT), yukawa_gms yukawa_sei510_gms)
476 ifneq (,$(filter $(TARGET_PRODUCT), yukawa_gms)
477 endif
478endif
479
480`,
481 expected: `load("//build/make/core:product_config.rbc", "rblf")
482
483def init(g, handle):
484 cfg = rblf.cfg(handle)
485 if g["TARGET_PRODUCT"] not in ["yukawa_gms", "yukawa_sei510_gms"]:
Sasha Smundak0554d762021-07-08 18:26:12 -0700486 if g["TARGET_PRODUCT"] == "yukawa_gms":
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800487 pass
488`,
489 },
490 {
Sasha Smundak0554d762021-07-08 18:26:12 -0700491 desc: "filter $(V1), $(V2)",
492 mkname: "product.mk",
493 in: `
494ifneq (, $(filter $(PRODUCT_LIST), $(TARGET_PRODUCT)))
495endif
496`,
497 expected: `load("//build/make/core:product_config.rbc", "rblf")
498
499def init(g, handle):
500 cfg = rblf.cfg(handle)
Sasha Smundak468e11f2021-08-26 09:10:23 -0700501 if rblf.filter(g.get("PRODUCT_LIST", []), g["TARGET_PRODUCT"]):
Sasha Smundak0554d762021-07-08 18:26:12 -0700502 pass
503`,
504 },
505 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800506 desc: "ifeq",
507 mkname: "product.mk",
508 in: `
509ifeq (aosp, $(TARGET_PRODUCT)) # Comment
510else ifneq (, $(TARGET_PRODUCT))
511endif
512`,
513 expected: `load("//build/make/core:product_config.rbc", "rblf")
514
515def init(g, handle):
516 cfg = rblf.cfg(handle)
517 if "aosp" == g["TARGET_PRODUCT"]:
518 # Comment
519 pass
520 elif g["TARGET_PRODUCT"]:
521 pass
522`,
523 },
524 {
525 desc: "Nested if",
526 mkname: "product.mk",
527 in: `
528ifdef PRODUCT_NAME
529 PRODUCT_PACKAGES = pack-if0
530 ifdef PRODUCT_MODEL
531 PRODUCT_PACKAGES = pack-if-if
532 else ifdef PRODUCT_NAME
533 PRODUCT_PACKAGES = pack-if-elif
534 else
535 PRODUCT_PACKAGES = pack-if-else
536 endif
537 PRODUCT_PACKAGES = pack-if
538else ifneq (,$(TARGET_PRODUCT))
539 PRODUCT_PACKAGES = pack-elif
540else
541 PRODUCT_PACKAGES = pack-else
542endif
543`,
544 expected: `load("//build/make/core:product_config.rbc", "rblf")
545
546def init(g, handle):
547 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800548 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800549 cfg["PRODUCT_PACKAGES"] = ["pack-if0"]
Cole Faust71514c02022-01-27 17:21:41 -0800550 if cfg.get("PRODUCT_MODEL", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800551 cfg["PRODUCT_PACKAGES"] = ["pack-if-if"]
Cole Faust71514c02022-01-27 17:21:41 -0800552 elif cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800553 cfg["PRODUCT_PACKAGES"] = ["pack-if-elif"]
554 else:
555 cfg["PRODUCT_PACKAGES"] = ["pack-if-else"]
556 cfg["PRODUCT_PACKAGES"] = ["pack-if"]
557 elif g["TARGET_PRODUCT"]:
558 cfg["PRODUCT_PACKAGES"] = ["pack-elif"]
559 else:
560 cfg["PRODUCT_PACKAGES"] = ["pack-else"]
561`,
562 },
563 {
564 desc: "Wildcard",
565 mkname: "product.mk",
566 in: `
567ifeq (,$(wildcard foo.mk))
568endif
569ifneq (,$(wildcard foo*.mk))
570endif
Cole Fausta99afdf2022-04-26 12:06:49 -0700571ifeq (foo1.mk foo2.mk barxyz.mk,$(wildcard foo*.mk bar*.mk))
572endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800573`,
574 expected: `load("//build/make/core:product_config.rbc", "rblf")
575
576def init(g, handle):
577 cfg = rblf.cfg(handle)
Cole Fausta99afdf2022-04-26 12:06:49 -0700578 if not rblf.expand_wildcard("foo.mk"):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800579 pass
Cole Fausta99afdf2022-04-26 12:06:49 -0700580 if rblf.expand_wildcard("foo*.mk"):
581 pass
582 if rblf.expand_wildcard("foo*.mk bar*.mk") == ["foo1.mk", "foo2.mk", "barxyz.mk"]:␤
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800583 pass
584`,
585 },
586 {
Cole Faustf8320212021-11-10 15:05:07 -0800587 desc: "if with interpolation",
588 mkname: "product.mk",
589 in: `
590ifeq ($(VARIABLE1)text$(VARIABLE2),true)
591endif
592`,
593 expected: `load("//build/make/core:product_config.rbc", "rblf")
594
595def init(g, handle):
596 cfg = rblf.cfg(handle)
597 if "%stext%s" % (g.get("VARIABLE1", ""), g.get("VARIABLE2", "")) == "true":
598 pass
599`,
600 },
601 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800602 desc: "ifneq $(X),true",
603 mkname: "product.mk",
604 in: `
605ifneq ($(VARIABLE),true)
606endif
607`,
608 expected: `load("//build/make/core:product_config.rbc", "rblf")
609
610def init(g, handle):
611 cfg = rblf.cfg(handle)
612 if g.get("VARIABLE", "") != "true":
613 pass
614`,
615 },
616 {
617 desc: "Const neq",
618 mkname: "product.mk",
619 in: `
620ifneq (1,0)
621endif
622`,
623 expected: `load("//build/make/core:product_config.rbc", "rblf")
624
625def init(g, handle):
626 cfg = rblf.cfg(handle)
627 if "1" != "0":
628 pass
629`,
630 },
631 {
632 desc: "is-board calls",
633 mkname: "product.mk",
634 in: `
635ifeq ($(call is-board-platform-in-list,msm8998), true)
636else ifneq ($(call is-board-platform,copper),true)
637else ifneq ($(call is-vendor-board-platform,QCOM),true)
638else ifeq ($(call is-product-in-list, $(PLATFORM_LIST)), true)
639endif
640`,
641 expected: `load("//build/make/core:product_config.rbc", "rblf")
642
643def init(g, handle):
644 cfg = rblf.cfg(handle)
Cole Faustb2e0b602022-01-07 15:46:58 -0800645 if rblf.board_platform_in(g, "msm8998"):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800646 pass
Cole Faustb2e0b602022-01-07 15:46:58 -0800647 elif not rblf.board_platform_is(g, "copper"):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800648 pass
Cole Faustf0632662022-04-07 13:59:24 -0700649 elif g.get("TARGET_BOARD_PLATFORM", "") not in g.get("QCOM_BOARD_PLATFORMS", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800650 pass
651 elif g["TARGET_PRODUCT"] in g.get("PLATFORM_LIST", []):
652 pass
653`,
654 },
655 {
Sasha Smundak3a9b8e82021-08-25 14:11:04 -0700656 desc: "new is-board calls",
657 mkname: "product.mk",
658 in: `
659ifneq (,$(call is-board-platform-in-list2,msm8998 $(X))
660else ifeq (,$(call is-board-platform2,copper)
661else ifneq (,$(call is-vendor-board-qcom))
662endif
663`,
664 expected: `load("//build/make/core:product_config.rbc", "rblf")
665
666def init(g, handle):
667 cfg = rblf.cfg(handle)
668 if rblf.board_platform_in(g, "msm8998 %s" % g.get("X", "")):
669 pass
670 elif not rblf.board_platform_is(g, "copper"):
671 pass
Cole Faustf0632662022-04-07 13:59:24 -0700672 elif g.get("TARGET_BOARD_PLATFORM", "") in g.get("QCOM_BOARD_PLATFORMS", ""):
Sasha Smundak3a9b8e82021-08-25 14:11:04 -0700673 pass
674`,
675 },
676 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800677 desc: "findstring call",
678 mkname: "product.mk",
679 in: `
Cole Faust0e9418c2021-12-13 16:33:25 -0800680result := $(findstring a,a b c)
681result := $(findstring b,x y z)
682`,
683 expected: `load("//build/make/core:product_config.rbc", "rblf")
684
685def init(g, handle):
686 cfg = rblf.cfg(handle)
687 _result = rblf.findstring("a", "a b c")
688 _result = rblf.findstring("b", "x y z")
689`,
690 },
691 {
692 desc: "findstring in if statement",
693 mkname: "product.mk",
694 in: `
695ifeq ($(findstring foo,$(PRODUCT_PACKAGES)),)
696endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800697ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),)
698endif
Cole Faust0e9418c2021-12-13 16:33:25 -0800699ifeq ($(findstring foo,$(PRODUCT_PACKAGES)),foo)
700endif
701ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),foo)
702endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800703`,
704 expected: `load("//build/make/core:product_config.rbc", "rblf")
705
706def init(g, handle):
707 cfg = rblf.cfg(handle)
Cole Faust0e9418c2021-12-13 16:33:25 -0800708 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") == -1:
709 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800710 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
711 pass
Cole Faust0e9418c2021-12-13 16:33:25 -0800712 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
713 pass
714 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") == -1:
715 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800716`,
717 },
718 {
719 desc: "rhs call",
720 mkname: "product.mk",
721 in: `
722PRODUCT_COPY_FILES = $(call add-to-product-copy-files-if-exists, path:distpath) \
723 $(call find-copy-subdir-files, *, fromdir, todir) $(wildcard foo.*)
724`,
725 expected: `load("//build/make/core:product_config.rbc", "rblf")
726
727def init(g, handle):
728 cfg = rblf.cfg(handle)
729 cfg["PRODUCT_COPY_FILES"] = (rblf.copy_if_exists("path:distpath") +
730 rblf.find_and_copy("*", "fromdir", "todir") +
731 rblf.expand_wildcard("foo.*"))
732`,
733 },
734 {
735 desc: "inferred type",
736 mkname: "product.mk",
737 in: `
738HIKEY_MODS := $(wildcard foo/*.ko)
739BOARD_VENDOR_KERNEL_MODULES += $(HIKEY_MODS)
740`,
741 expected: `load("//build/make/core:product_config.rbc", "rblf")
742
743def init(g, handle):
744 cfg = rblf.cfg(handle)
745 g["HIKEY_MODS"] = rblf.expand_wildcard("foo/*.ko")
746 g.setdefault("BOARD_VENDOR_KERNEL_MODULES", [])
747 g["BOARD_VENDOR_KERNEL_MODULES"] += g["HIKEY_MODS"]
748`,
749 },
750 {
751 desc: "list with vars",
752 mkname: "product.mk",
753 in: `
754PRODUCT_COPY_FILES += path1:$(TARGET_PRODUCT)/path1 $(PRODUCT_MODEL)/path2:$(TARGET_PRODUCT)/path2
755`,
756 expected: `load("//build/make/core:product_config.rbc", "rblf")
757
758def init(g, handle):
759 cfg = rblf.cfg(handle)
760 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
761 cfg["PRODUCT_COPY_FILES"] += (("path1:%s/path1" % g["TARGET_PRODUCT"]).split() +
762 ("%s/path2:%s/path2" % (cfg.get("PRODUCT_MODEL", ""), g["TARGET_PRODUCT"])).split())
763`,
764 },
765 {
766 desc: "misc calls",
767 mkname: "product.mk",
768 in: `
769$(call enforce-product-packages-exist,)
770$(call enforce-product-packages-exist, foo)
771$(call require-artifacts-in-path, foo, bar)
772$(call require-artifacts-in-path-relaxed, foo, bar)
Sasha Smundakd6797852021-11-15 13:01:53 -0800773$(call dist-for-goals, goal, from:to)
Cole Faust1cc08852022-02-28 11:12:08 -0800774$(call add-product-dex-preopt-module-config,MyModule,disable)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800775`,
776 expected: `load("//build/make/core:product_config.rbc", "rblf")
777
778def init(g, handle):
779 cfg = rblf.cfg(handle)
Cole Faust6c41b8a2022-04-13 13:53:48 -0700780 rblf.enforce_product_packages_exist(handle, "")
781 rblf.enforce_product_packages_exist(handle, "foo")
Cole Faustea9db582022-03-21 17:50:05 -0700782 rblf.require_artifacts_in_path(handle, "foo", "bar")
783 rblf.require_artifacts_in_path_relaxed(handle, "foo", "bar")
Sasha Smundakd6797852021-11-15 13:01:53 -0800784 rblf.mkdist_for_goals(g, "goal", "from:to")
Cole Faust1cc08852022-02-28 11:12:08 -0800785 rblf.add_product_dex_preopt_module_config(handle, "MyModule", "disable")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800786`,
787 },
788 {
789 desc: "list with functions",
790 mkname: "product.mk",
791 in: `
792PRODUCT_COPY_FILES := $(call find-copy-subdir-files,*.kl,from1,to1) \
793 $(call find-copy-subdir-files,*.kc,from2,to2) \
794 foo bar
795`,
796 expected: `load("//build/make/core:product_config.rbc", "rblf")
797
798def init(g, handle):
799 cfg = rblf.cfg(handle)
800 cfg["PRODUCT_COPY_FILES"] = (rblf.find_and_copy("*.kl", "from1", "to1") +
801 rblf.find_and_copy("*.kc", "from2", "to2") +
802 [
803 "foo",
804 "bar",
805 ])
806`,
807 },
808 {
809 desc: "Text functions",
810 mkname: "product.mk",
811 in: `
812PRODUCT_COPY_FILES := $(addprefix pfx-,a b c)
813PRODUCT_COPY_FILES := $(addsuffix .sff, a b c)
814PRODUCT_NAME := $(word 1, $(subst ., ,$(TARGET_BOARD_PLATFORM)))
Sasha Smundak35434ed2021-11-05 16:29:56 -0700815$(info $(patsubst %.pub,$(PRODUCT_NAME)%,$(PRODUCT_ADB_KEYS)))
Cole Faust0e2b2562022-04-01 11:46:50 -0700816$(info $$(dir foo/bar): $(dir foo/bar))
Sasha Smundak16e07732021-07-23 11:38:23 -0700817$(info $(firstword $(PRODUCT_COPY_FILES)))
818$(info $(lastword $(PRODUCT_COPY_FILES)))
819$(info $(dir $(lastword $(MAKEFILE_LIST))))
820$(info $(dir $(lastword $(PRODUCT_COPY_FILES))))
821$(info $(dir $(lastword $(foobar))))
822$(info $(abspath foo/bar))
823$(info $(notdir foo/bar))
Sasha Smundak3deb9682021-07-26 18:42:25 -0700824$(call add_soong_config_namespace,snsconfig)
825$(call add_soong_config_var_value,snsconfig,imagetype,odm_image)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700826$(call soong_config_set, snsconfig, foo, foo_value)
827$(call soong_config_append, snsconfig, bar, bar_value)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700828PRODUCT_COPY_FILES := $(call copy-files,$(wildcard foo*.mk),etc)
Sasha Smundak04453082021-08-17 18:14:41 -0700829PRODUCT_COPY_FILES := $(call product-copy-files-by-pattern,from/%,to/%,a b c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800830`,
831 expected: `load("//build/make/core:product_config.rbc", "rblf")
832
833def init(g, handle):
834 cfg = rblf.cfg(handle)
835 cfg["PRODUCT_COPY_FILES"] = rblf.addprefix("pfx-", "a b c")
836 cfg["PRODUCT_COPY_FILES"] = rblf.addsuffix(".sff", "a b c")
837 cfg["PRODUCT_NAME"] = ((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " ")).split()[0]
Sasha Smundak35434ed2021-11-05 16:29:56 -0700838 rblf.mkinfo("product.mk", rblf.mkpatsubst("%.pub", "%s%%" % cfg["PRODUCT_NAME"], g.get("PRODUCT_ADB_KEYS", "")))
Cole Faust0e2b2562022-04-01 11:46:50 -0700839 rblf.mkinfo("product.mk", "$(dir foo/bar): %s" % rblf.dir("foo/bar"))
Sasha Smundak16e07732021-07-23 11:38:23 -0700840 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][0])
841 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][-1])
842 rblf.mkinfo("product.mk", rblf.dir("product.mk"))
843 rblf.mkinfo("product.mk", rblf.dir(cfg["PRODUCT_COPY_FILES"][-1]))
844 rblf.mkinfo("product.mk", rblf.dir((_foobar).split()[-1]))
845 rblf.mkinfo("product.mk", rblf.abspath("foo/bar"))
846 rblf.mkinfo("product.mk", rblf.notdir("foo/bar"))
Sasha Smundak65b547e2021-09-17 15:35:41 -0700847 rblf.soong_config_namespace(g, "snsconfig")
848 rblf.soong_config_set(g, "snsconfig", "imagetype", "odm_image")
849 rblf.soong_config_set(g, "snsconfig", "foo", "foo_value")
850 rblf.soong_config_append(g, "snsconfig", "bar", "bar_value")
Sasha Smundak3deb9682021-07-26 18:42:25 -0700851 cfg["PRODUCT_COPY_FILES"] = rblf.copy_files(rblf.expand_wildcard("foo*.mk"), "etc")
Sasha Smundak04453082021-08-17 18:14:41 -0700852 cfg["PRODUCT_COPY_FILES"] = rblf.product_copy_files_by_pattern("from/%", "to/%", "a b c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800853`,
854 },
855 {
Sasha Smundak9d011ab2021-07-09 16:00:57 -0700856 desc: "subst in list",
857 mkname: "product.mk",
858 in: `
859files = $(call find-copy-subdir-files,*,from,to)
860PRODUCT_COPY_FILES += $(subst foo,bar,$(files))
861`,
862 expected: `load("//build/make/core:product_config.rbc", "rblf")
863
864def init(g, handle):
865 cfg = rblf.cfg(handle)
866 _files = rblf.find_and_copy("*", "from", "to")
867 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
868 cfg["PRODUCT_COPY_FILES"] += rblf.mksubst("foo", "bar", _files)
869`,
870 },
871 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800872 desc: "assignment flavors",
873 mkname: "product.mk",
874 in: `
875PRODUCT_LIST1 := a
876PRODUCT_LIST2 += a
877PRODUCT_LIST1 += b
878PRODUCT_LIST2 += b
879PRODUCT_LIST3 ?= a
880PRODUCT_LIST1 = c
881PLATFORM_LIST += x
882PRODUCT_PACKAGES := $(PLATFORM_LIST)
883`,
884 expected: `load("//build/make/core:product_config.rbc", "rblf")
885
886def init(g, handle):
887 cfg = rblf.cfg(handle)
888 cfg["PRODUCT_LIST1"] = ["a"]
889 rblf.setdefault(handle, "PRODUCT_LIST2")
890 cfg["PRODUCT_LIST2"] += ["a"]
891 cfg["PRODUCT_LIST1"] += ["b"]
892 cfg["PRODUCT_LIST2"] += ["b"]
893 if cfg.get("PRODUCT_LIST3") == None:
894 cfg["PRODUCT_LIST3"] = ["a"]
895 cfg["PRODUCT_LIST1"] = ["c"]
896 g.setdefault("PLATFORM_LIST", [])
897 g["PLATFORM_LIST"] += ["x"]
898 cfg["PRODUCT_PACKAGES"] = g["PLATFORM_LIST"][:]
899`,
900 },
901 {
902 desc: "assigment flavors2",
903 mkname: "product.mk",
904 in: `
905PRODUCT_LIST1 = a
906ifeq (0,1)
907 PRODUCT_LIST1 += b
908 PRODUCT_LIST2 += b
909endif
910PRODUCT_LIST1 += c
911PRODUCT_LIST2 += c
912`,
913 expected: `load("//build/make/core:product_config.rbc", "rblf")
914
915def init(g, handle):
916 cfg = rblf.cfg(handle)
917 cfg["PRODUCT_LIST1"] = ["a"]
918 if "0" == "1":
919 cfg["PRODUCT_LIST1"] += ["b"]
920 rblf.setdefault(handle, "PRODUCT_LIST2")
921 cfg["PRODUCT_LIST2"] += ["b"]
922 cfg["PRODUCT_LIST1"] += ["c"]
923 rblf.setdefault(handle, "PRODUCT_LIST2")
924 cfg["PRODUCT_LIST2"] += ["c"]
925`,
926 },
927 {
Cole Fauste2a37982022-03-09 16:00:17 -0800928 desc: "assigment setdefaults",
929 mkname: "product.mk",
930 in: `
931# All of these should have a setdefault because they're self-referential and not defined before
932PRODUCT_LIST1 = a $(PRODUCT_LIST1)
933PRODUCT_LIST2 ?= a $(PRODUCT_LIST2)
934PRODUCT_LIST3 += a
935
936# Now doing them again should not have a setdefault because they've already been set
937PRODUCT_LIST1 = a $(PRODUCT_LIST1)
938PRODUCT_LIST2 ?= a $(PRODUCT_LIST2)
939PRODUCT_LIST3 += a
940`,
941 expected: `# All of these should have a setdefault because they're self-referential and not defined before
942load("//build/make/core:product_config.rbc", "rblf")
943
944def init(g, handle):
945 cfg = rblf.cfg(handle)
946 rblf.setdefault(handle, "PRODUCT_LIST1")
947 cfg["PRODUCT_LIST1"] = (["a"] +
948 cfg.get("PRODUCT_LIST1", []))
949 if cfg.get("PRODUCT_LIST2") == None:
950 rblf.setdefault(handle, "PRODUCT_LIST2")
951 cfg["PRODUCT_LIST2"] = (["a"] +
952 cfg.get("PRODUCT_LIST2", []))
953 rblf.setdefault(handle, "PRODUCT_LIST3")
954 cfg["PRODUCT_LIST3"] += ["a"]
955 # Now doing them again should not have a setdefault because they've already been set
956 cfg["PRODUCT_LIST1"] = (["a"] +
957 cfg["PRODUCT_LIST1"])
958 if cfg.get("PRODUCT_LIST2") == None:
959 cfg["PRODUCT_LIST2"] = (["a"] +
960 cfg["PRODUCT_LIST2"])
961 cfg["PRODUCT_LIST3"] += ["a"]
962`,
963 },
964 {
Sasha Smundak3deb9682021-07-26 18:42:25 -0700965 desc: "soong namespace assignments",
966 mkname: "product.mk",
967 in: `
968SOONG_CONFIG_NAMESPACES += cvd
969SOONG_CONFIG_cvd += launch_configs
Sasha Smundak65b547e2021-09-17 15:35:41 -0700970SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
Sasha Smundak3deb9682021-07-26 18:42:25 -0700971SOONG_CONFIG_cvd += grub_config
972SOONG_CONFIG_cvd_grub_config += grub.cfg
Sasha Smundak65b547e2021-09-17 15:35:41 -0700973x := $(SOONG_CONFIG_cvd_grub_config)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700974`,
975 expected: `load("//build/make/core:product_config.rbc", "rblf")
976
977def init(g, handle):
978 cfg = rblf.cfg(handle)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700979 rblf.soong_config_namespace(g, "cvd")
980 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
981 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
Sasha Smundak422b6142021-11-11 18:31:59 -0800982 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 -0700983`,
Cole Faustc00184e2021-11-08 12:08:57 -0800984 }, {
985 desc: "soong namespace accesses",
986 mkname: "product.mk",
987 in: `
988SOONG_CONFIG_NAMESPACES += cvd
989SOONG_CONFIG_cvd += launch_configs
990SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
991SOONG_CONFIG_cvd += grub_config
992SOONG_CONFIG_cvd_grub_config += grub.cfg
993x := $(call soong_config_get,cvd,grub_config)
994`,
995 expected: `load("//build/make/core:product_config.rbc", "rblf")
996
997def init(g, handle):
998 cfg = rblf.cfg(handle)
999 rblf.soong_config_namespace(g, "cvd")
1000 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
1001 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
1002 _x = rblf.soong_config_get(g, "cvd", "grub_config")
1003`,
Sasha Smundak3deb9682021-07-26 18:42:25 -07001004 },
1005 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001006 desc: "string split",
1007 mkname: "product.mk",
1008 in: `
1009PRODUCT_LIST1 = a
1010local = b
1011local += c
1012FOO = d
1013FOO += e
1014PRODUCT_LIST1 += $(local)
1015PRODUCT_LIST1 += $(FOO)
1016`,
1017 expected: `load("//build/make/core:product_config.rbc", "rblf")
1018
1019def init(g, handle):
1020 cfg = rblf.cfg(handle)
1021 cfg["PRODUCT_LIST1"] = ["a"]
1022 _local = "b"
1023 _local += " " + "c"
1024 g["FOO"] = "d"
1025 g["FOO"] += " " + "e"
1026 cfg["PRODUCT_LIST1"] += (_local).split()
1027 cfg["PRODUCT_LIST1"] += (g["FOO"]).split()
1028`,
1029 },
1030 {
1031 desc: "apex_jars",
1032 mkname: "product.mk",
1033 in: `
1034PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex
1035`,
1036 expected: `load("//build/make/core:product_config.rbc", "rblf")
1037
1038def init(g, handle):
1039 cfg = rblf.cfg(handle)
1040 cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) +
1041 ["framework-minus-apex"])
1042`,
1043 },
1044 {
Cole Faust95b95cb2022-04-05 16:37:39 -07001045 desc: "strip/sort functions",
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001046 mkname: "product.mk",
1047 in: `
1048ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),)
1049 PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress)
1050endif
Cole Faust95b95cb2022-04-05 16:37:39 -07001051MY_VAR := $(sort b a c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001052`,
1053 expected: `load("//build/make/core:product_config.rbc", "rblf")
1054
1055def init(g, handle):
1056 cfg = rblf.cfg(handle)
1057 if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []):
Cole Faust816e0802022-03-04 12:04:31 -08001058 rblf.setdefault(handle, "PRODUCT_PACKAGES")
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001059 cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split()
Cole Faust95b95cb2022-04-05 16:37:39 -07001060 g["MY_VAR"] = rblf.mksort("b a c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001061`,
1062 },
1063 {
1064 desc: "strip func in condition",
1065 mkname: "product.mk",
1066 in: `
1067ifneq ($(strip $(TARGET_VENDOR)),)
1068endif
1069`,
1070 expected: `load("//build/make/core:product_config.rbc", "rblf")
1071
1072def init(g, handle):
1073 cfg = rblf.cfg(handle)
Sasha Smundak0554d762021-07-08 18:26:12 -07001074 if rblf.mkstrip(g.get("TARGET_VENDOR", "")):
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001075 pass
1076`,
1077 },
1078 {
1079 desc: "ref after set",
1080 mkname: "product.mk",
1081 in: `
1082PRODUCT_ADB_KEYS:=value
1083FOO := $(PRODUCT_ADB_KEYS)
1084ifneq (,$(PRODUCT_ADB_KEYS))
1085endif
1086`,
1087 expected: `load("//build/make/core:product_config.rbc", "rblf")
1088
1089def init(g, handle):
1090 cfg = rblf.cfg(handle)
1091 g["PRODUCT_ADB_KEYS"] = "value"
1092 g["FOO"] = g["PRODUCT_ADB_KEYS"]
1093 if g["PRODUCT_ADB_KEYS"]:
1094 pass
1095`,
1096 },
1097 {
1098 desc: "ref before set",
1099 mkname: "product.mk",
1100 in: `
1101V1 := $(PRODUCT_ADB_KEYS)
1102ifeq (,$(PRODUCT_ADB_KEYS))
1103 V2 := $(PRODUCT_ADB_KEYS)
1104 PRODUCT_ADB_KEYS:=foo
1105 V3 := $(PRODUCT_ADB_KEYS)
1106endif`,
1107 expected: `load("//build/make/core:product_config.rbc", "rblf")
1108
1109def init(g, handle):
1110 cfg = rblf.cfg(handle)
1111 g["V1"] = g.get("PRODUCT_ADB_KEYS", "")
1112 if not g.get("PRODUCT_ADB_KEYS", ""):
1113 g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
1114 g["PRODUCT_ADB_KEYS"] = "foo"
1115 g["V3"] = g["PRODUCT_ADB_KEYS"]
1116`,
1117 },
Sasha Smundak6609ba72021-07-22 18:32:56 -07001118 {
1119 desc: "Dynamic inherit path",
1120 mkname: "product.mk",
1121 in: `
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001122MY_PATH:=foo
Sasha Smundak6609ba72021-07-22 18:32:56 -07001123$(call inherit-product,vendor/$(MY_PATH)/cfg.mk)
1124`,
1125 expected: `load("//build/make/core:product_config.rbc", "rblf")
1126load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1127load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init")
1128
1129def init(g, handle):
1130 cfg = rblf.cfg(handle)
1131 g["MY_PATH"] = "foo"
1132 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001133 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1134 "vendor/bar/baz/cfg.mk": ("vendor/bar/baz/cfg", _cfg1_init),
Sasha Smundak6609ba72021-07-22 18:32:56 -07001135 }.get("vendor/%s/cfg.mk" % g["MY_PATH"])
1136 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1137 if not _varmod_init:
Cole Faust7321b092021-12-21 16:11:16 -08001138 rblf.mkerror("product.mk", "Cannot find %s" % ("vendor/%s/cfg.mk" % g["MY_PATH"]))
Sasha Smundak6609ba72021-07-22 18:32:56 -07001139 rblf.inherit(handle, _varmod, _varmod_init)
1140`,
1141 },
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001142 {
1143 desc: "Dynamic inherit with hint",
1144 mkname: "product.mk",
1145 in: `
1146MY_PATH:=foo
1147#RBC# include_top vendor/foo1
1148$(call inherit-product,$(MY_PATH)/cfg.mk)
1149`,
1150 expected: `load("//build/make/core:product_config.rbc", "rblf")
1151load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1152
1153def init(g, handle):
1154 cfg = rblf.cfg(handle)
1155 g["MY_PATH"] = "foo"
Cole Faust93f8d392022-03-02 13:31:30 -08001156 _entry = {
1157 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1158 }.get("%s/cfg.mk" % g["MY_PATH"])
1159 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1160 if not _varmod_init:
1161 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1162 rblf.inherit(handle, _varmod, _varmod_init)
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001163`,
1164 },
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001165 {
Cole Faustf7ed5342021-12-21 14:15:12 -08001166 desc: "Dynamic inherit with duplicated hint",
1167 mkname: "product.mk",
1168 in: `
1169MY_PATH:=foo
1170#RBC# include_top vendor/foo1
1171$(call inherit-product,$(MY_PATH)/cfg.mk)
1172#RBC# include_top vendor/foo1
Cole Faust7940c6a2022-01-31 15:54:05 -08001173#RBC# include_top vendor/foo1
Cole Faustf7ed5342021-12-21 14:15:12 -08001174$(call inherit-product,$(MY_PATH)/cfg.mk)
1175`,
1176 expected: `load("//build/make/core:product_config.rbc", "rblf")
1177load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1178
1179def init(g, handle):
1180 cfg = rblf.cfg(handle)
1181 g["MY_PATH"] = "foo"
Cole Faust93f8d392022-03-02 13:31:30 -08001182 _entry = {
1183 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1184 }.get("%s/cfg.mk" % g["MY_PATH"])
1185 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1186 if not _varmod_init:
1187 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1188 rblf.inherit(handle, _varmod, _varmod_init)
1189 _entry = {
1190 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1191 }.get("%s/cfg.mk" % g["MY_PATH"])
1192 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1193 if not _varmod_init:
1194 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1195 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faustf7ed5342021-12-21 14:15:12 -08001196`,
1197 },
1198 {
Cole Faust069aba62022-01-26 17:47:33 -08001199 desc: "Dynamic inherit path that lacks hint",
Cole Faust6c934f62022-01-06 15:51:12 -08001200 mkname: "product.mk",
1201 in: `
1202#RBC# include_top foo
1203$(call inherit-product,$(MY_VAR)/font.mk)
1204
1205#RBC# include_top foo
1206
1207# There's some space and even this comment between the include_top and the inherit-product
1208
1209$(call inherit-product,$(MY_VAR)/font.mk)
1210
1211$(call inherit-product,$(MY_VAR)/font.mk)
1212`,
Cole Faust7940c6a2022-01-31 15:54:05 -08001213 expected: `load("//build/make/core:product_config.rbc", "rblf")
Cole Faust6c934f62022-01-06 15:51:12 -08001214load("//foo:font.star|init", _font_init = "init")
Cole Faust069aba62022-01-26 17:47:33 -08001215load("//bar:font.star|init", _font1_init = "init")
Cole Faust6c934f62022-01-06 15:51:12 -08001216
1217def init(g, handle):
1218 cfg = rblf.cfg(handle)
Cole Faust93f8d392022-03-02 13:31:30 -08001219 _entry = {
1220 "foo/font.mk": ("foo/font", _font_init),
1221 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1222 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1223 if not _varmod_init:
1224 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1225 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001226 # There's some space and even this comment between the include_top and the inherit-product
Cole Faust93f8d392022-03-02 13:31:30 -08001227 _entry = {
1228 "foo/font.mk": ("foo/font", _font_init),
1229 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1230 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1231 if not _varmod_init:
1232 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1233 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faustf4e72cf2022-02-08 12:49:37 -08001234 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 -08001235 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001236 "foo/font.mk": ("foo/font", _font_init),
Cole Faust069aba62022-01-26 17:47:33 -08001237 "bar/font.mk": ("bar/font", _font1_init),
Cole Faust6c934f62022-01-06 15:51:12 -08001238 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1239 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1240 if not _varmod_init:
1241 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1242 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001243`,
1244 },
1245 {
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001246 desc: "Ignore make rules",
1247 mkname: "product.mk",
1248 in: `
1249foo: foo.c
1250 gcc -o $@ $*`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001251 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001252
1253def init(g, handle):
1254 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001255 rblf.mk2rbc_error("product.mk:2", "unsupported line rule: foo: foo.c\n#gcc -o $@ $*")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001256`,
1257 },
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001258 {
1259 desc: "Flag override",
1260 mkname: "product.mk",
1261 in: `
1262override FOO:=`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001263 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001264
1265def init(g, handle):
1266 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001267 rblf.mk2rbc_error("product.mk:2", "cannot handle override directive")
Sasha Smundak422b6142021-11-11 18:31:59 -08001268`,
1269 },
1270 {
1271 desc: "Bad expression",
1272 mkname: "build/product.mk",
1273 in: `
1274ifeq (,$(call foobar))
1275endif
1276`,
1277 expected: `load("//build/make/core:product_config.rbc", "rblf")
1278
1279def init(g, handle):
1280 cfg = rblf.cfg(handle)
1281 if rblf.mk2rbc_error("build/product.mk:2", "cannot handle invoking foobar"):
1282 pass
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001283`,
1284 },
Cole Faust4eadba72021-12-07 11:54:52 -08001285 {
1286 desc: "if expression",
1287 mkname: "product.mk",
1288 in: `
1289TEST_VAR := foo
1290TEST_VAR_LIST := foo
1291TEST_VAR_LIST += bar
1292TEST_VAR_2 := $(if $(TEST_VAR),bar)
1293TEST_VAR_3 := $(if $(TEST_VAR),bar,baz)
Cole Faust421a1922022-03-16 14:35:45 -07001294TEST_VAR_4 := $(if $(TEST_VAR),$(TEST_VAR_LIST))
Cole Faust4eadba72021-12-07 11:54:52 -08001295`,
1296 expected: `load("//build/make/core:product_config.rbc", "rblf")
1297
1298def init(g, handle):
1299 cfg = rblf.cfg(handle)
1300 g["TEST_VAR"] = "foo"
1301 g["TEST_VAR_LIST"] = ["foo"]
1302 g["TEST_VAR_LIST"] += ["bar"]
1303 g["TEST_VAR_2"] = ("bar" if g["TEST_VAR"] else "")
1304 g["TEST_VAR_3"] = ("bar" if g["TEST_VAR"] else "baz")
Cole Faust421a1922022-03-16 14:35:45 -07001305 g["TEST_VAR_4"] = (g["TEST_VAR_LIST"] if g["TEST_VAR"] else [])
Cole Faust4eadba72021-12-07 11:54:52 -08001306`,
1307 },
Cole Faustc36c9622021-12-07 15:20:45 -08001308 {
1309 desc: "substitution references",
1310 mkname: "product.mk",
1311 in: `
1312SOURCES := foo.c bar.c
1313OBJECTS := $(SOURCES:.c=.o)
1314OBJECTS2 := $(SOURCES:%.c=%.o)
1315`,
1316 expected: `load("//build/make/core:product_config.rbc", "rblf")
1317
1318def init(g, handle):
1319 cfg = rblf.cfg(handle)
1320 g["SOURCES"] = "foo.c bar.c"
1321 g["OBJECTS"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1322 g["OBJECTS2"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1323`,
1324 },
Cole Faustb0d32ab2021-12-09 14:00:59 -08001325 {
1326 desc: "foreach expressions",
1327 mkname: "product.mk",
1328 in: `
1329BOOT_KERNEL_MODULES := foo.ko bar.ko
1330BOOT_KERNEL_MODULES_FILTER := $(foreach m,$(BOOT_KERNEL_MODULES),%/$(m))
1331BOOT_KERNEL_MODULES_LIST := foo.ko
1332BOOT_KERNEL_MODULES_LIST += bar.ko
1333BOOT_KERNEL_MODULES_FILTER_2 := $(foreach m,$(BOOT_KERNEL_MODULES_LIST),%/$(m))
1334
Cole Faustb67aa082022-02-28 16:39:59 -08001335FOREACH_WITH_IF := $(foreach module,\
1336 $(BOOT_KERNEL_MODULES_LIST),\
1337 $(if $(filter $(module),foo.ko),,$(error module "$(module)" has an error!)))
Cole Faustf035d402022-03-28 14:02:50 -07001338
1339# Same as above, but not assigning it to a variable allows it to be converted to statements
1340$(foreach module,\
1341 $(BOOT_KERNEL_MODULES_LIST),\
1342 $(if $(filter $(module),foo.ko),,$(error module "$(module)" has an error!)))
Cole Faustb0d32ab2021-12-09 14:00:59 -08001343`,
1344 expected: `load("//build/make/core:product_config.rbc", "rblf")
1345
1346def init(g, handle):
1347 cfg = rblf.cfg(handle)
1348 g["BOOT_KERNEL_MODULES"] = "foo.ko bar.ko"
1349 g["BOOT_KERNEL_MODULES_FILTER"] = ["%%/%s" % m for m in rblf.words(g["BOOT_KERNEL_MODULES"])]
1350 g["BOOT_KERNEL_MODULES_LIST"] = ["foo.ko"]
1351 g["BOOT_KERNEL_MODULES_LIST"] += ["bar.ko"]
1352 g["BOOT_KERNEL_MODULES_FILTER_2"] = ["%%/%s" % m for m in g["BOOT_KERNEL_MODULES_LIST"]]
Cole Faustb67aa082022-02-28 16:39:59 -08001353 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 Faustf035d402022-03-28 14:02:50 -07001354 # Same as above, but not assigning it to a variable allows it to be converted to statements
1355 for module in g["BOOT_KERNEL_MODULES_LIST"]:
1356 if not rblf.filter(module, "foo.ko"):
1357 rblf.mkerror("product.mk", "module \"%s\" has an error!" % module)
Cole Faustb0d32ab2021-12-09 14:00:59 -08001358`,
1359 },
Cole Faust0484c232021-12-22 14:08:08 -08001360 {
1361 desc: "List appended to string",
1362 mkname: "product.mk",
1363 in: `
1364NATIVE_BRIDGE_PRODUCT_PACKAGES := \
1365 libnative_bridge_vdso.native_bridge \
1366 native_bridge_guest_app_process.native_bridge \
1367 native_bridge_guest_linker.native_bridge
1368
1369NATIVE_BRIDGE_MODIFIED_GUEST_LIBS := \
1370 libaaudio \
1371 libamidi \
1372 libandroid \
1373 libandroid_runtime
1374
1375NATIVE_BRIDGE_PRODUCT_PACKAGES += \
1376 $(addsuffix .native_bridge,$(NATIVE_BRIDGE_ORIG_GUEST_LIBS))
1377`,
1378 expected: `load("//build/make/core:product_config.rbc", "rblf")
1379
1380def init(g, handle):
1381 cfg = rblf.cfg(handle)
1382 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] = "libnative_bridge_vdso.native_bridge native_bridge_guest_app_process.native_bridge native_bridge_guest_linker.native_bridge"
1383 g["NATIVE_BRIDGE_MODIFIED_GUEST_LIBS"] = "libaaudio libamidi libandroid libandroid_runtime"
1384 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] += " " + " ".join(rblf.addsuffix(".native_bridge", g.get("NATIVE_BRIDGE_ORIG_GUEST_LIBS", "")))
1385`,
1386 },
Cole Faustb1103e22022-01-06 15:22:05 -08001387 {
1388 desc: "Math functions",
1389 mkname: "product.mk",
1390 in: `
1391# Test the math functions defined in build/make/common/math.mk
1392ifeq ($(call math_max,2,5),5)
1393endif
1394ifeq ($(call math_min,2,5),2)
1395endif
1396ifeq ($(call math_gt_or_eq,2,5),true)
1397endif
1398ifeq ($(call math_gt,2,5),true)
1399endif
1400ifeq ($(call math_lt,2,5),true)
1401endif
1402ifeq ($(call math_gt_or_eq,2,5),)
1403endif
1404ifeq ($(call math_gt,2,5),)
1405endif
1406ifeq ($(call math_lt,2,5),)
1407endif
1408ifeq ($(call math_gt_or_eq,$(MY_VAR), 5),true)
1409endif
1410ifeq ($(call math_gt_or_eq,$(MY_VAR),$(MY_OTHER_VAR)),true)
1411endif
1412ifeq ($(call math_gt_or_eq,100$(MY_VAR),10),true)
1413endif
1414`,
1415 expected: `# Test the math functions defined in build/make/common/math.mk
1416load("//build/make/core:product_config.rbc", "rblf")
1417
1418def init(g, handle):
1419 cfg = rblf.cfg(handle)
1420 if max(2, 5) == 5:
1421 pass
1422 if min(2, 5) == 2:
1423 pass
1424 if 2 >= 5:
1425 pass
1426 if 2 > 5:
1427 pass
1428 if 2 < 5:
1429 pass
1430 if 2 < 5:
1431 pass
1432 if 2 <= 5:
1433 pass
1434 if 2 >= 5:
1435 pass
1436 if int(g.get("MY_VAR", "")) >= 5:
1437 pass
1438 if int(g.get("MY_VAR", "")) >= int(g.get("MY_OTHER_VAR", "")):
1439 pass
1440 if int("100%s" % g.get("MY_VAR", "")) >= 10:
1441 pass
1442`,
1443 },
Cole Faustf92c9f22022-03-14 14:35:50 -07001444 {
1445 desc: "Type hints",
1446 mkname: "product.mk",
1447 in: `
1448# Test type hints
1449#RBC# type_hint list MY_VAR MY_VAR_2
1450# Unsupported type
1451#RBC# type_hint bool MY_VAR_3
1452# Invalid syntax
1453#RBC# type_hint list
1454# Duplicated variable
1455#RBC# type_hint list MY_VAR_2
1456#RBC# type_hint list my-local-var-with-dashes
Cole Faust421a1922022-03-16 14:35:45 -07001457#RBC# type_hint string MY_STRING_VAR
Cole Faustf92c9f22022-03-14 14:35:50 -07001458
1459MY_VAR := foo
1460MY_VAR_UNHINTED := foo
1461
1462# Vars set after other statements still get the hint
1463MY_VAR_2 := foo
1464
1465# You can't specify a type hint after the first statement
1466#RBC# type_hint list MY_VAR_4
1467MY_VAR_4 := foo
1468
1469my-local-var-with-dashes := foo
Cole Faust421a1922022-03-16 14:35:45 -07001470
1471MY_STRING_VAR := $(wildcard foo/bar.mk)
Cole Faustf92c9f22022-03-14 14:35:50 -07001472`,
1473 expected: `# Test type hints
1474# Unsupported type
1475load("//build/make/core:product_config.rbc", "rblf")
1476
1477def init(g, handle):
1478 cfg = rblf.cfg(handle)
1479 rblf.mk2rbc_error("product.mk:5", "Invalid type_hint annotation. Only list/string types are accepted, found bool")
1480 # Invalid syntax
1481 rblf.mk2rbc_error("product.mk:7", "Invalid type_hint annotation: list. Must be a variable type followed by a list of variables of that type")
1482 # Duplicated variable
1483 rblf.mk2rbc_error("product.mk:9", "Duplicate type hint for variable MY_VAR_2")
1484 g["MY_VAR"] = ["foo"]
1485 g["MY_VAR_UNHINTED"] = "foo"
1486 # Vars set after other statements still get the hint
1487 g["MY_VAR_2"] = ["foo"]
1488 # You can't specify a type hint after the first statement
Cole Faust421a1922022-03-16 14:35:45 -07001489 rblf.mk2rbc_error("product.mk:20", "type_hint annotations must come before the first Makefile statement")
Cole Faustf92c9f22022-03-14 14:35:50 -07001490 g["MY_VAR_4"] = "foo"
1491 _my_local_var_with_dashes = ["foo"]
Cole Faust421a1922022-03-16 14:35:45 -07001492 g["MY_STRING_VAR"] = " ".join(rblf.expand_wildcard("foo/bar.mk"))
Cole Faustf92c9f22022-03-14 14:35:50 -07001493`,
1494 },
Cole Faustf5adedc2022-03-18 14:05:06 -07001495 {
1496 desc: "Set LOCAL_PATH to my-dir",
1497 mkname: "product.mk",
1498 in: `
1499LOCAL_PATH := $(call my-dir)
1500`,
1501 expected: `load("//build/make/core:product_config.rbc", "rblf")
1502
1503def init(g, handle):
1504 cfg = rblf.cfg(handle)
1505
1506`,
1507 },
Cole Faustf035d402022-03-28 14:02:50 -07001508 {
1509 desc: "Evals",
1510 mkname: "product.mk",
1511 in: `
1512$(eval)
1513$(eval MY_VAR := foo)
1514$(eval # This is a test of eval functions)
1515$(eval $(TOO_COMPLICATED) := bar)
1516$(foreach x,$(MY_LIST_VAR), \
1517 $(eval PRODUCT_COPY_FILES += foo/bar/$(x):$(TARGET_COPY_OUT_VENDOR)/etc/$(x)) \
1518 $(if $(MY_OTHER_VAR),$(eval PRODUCT_COPY_FILES += $(MY_OTHER_VAR):foo/bar/$(x))) \
1519)
1520
1521`,
1522 expected: `load("//build/make/core:product_config.rbc", "rblf")
1523
1524def init(g, handle):
1525 cfg = rblf.cfg(handle)
1526 g["MY_VAR"] = "foo"
1527 # This is a test of eval functions
1528 rblf.mk2rbc_error("product.mk:5", "Eval expression too complex; only assignments and comments are supported")
1529 for x in rblf.words(g.get("MY_LIST_VAR", "")):
1530 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
1531 cfg["PRODUCT_COPY_FILES"] += ("foo/bar/%s:%s/etc/%s" % (x, g.get("TARGET_COPY_OUT_VENDOR", ""), x)).split()
1532 if g.get("MY_OTHER_VAR", ""):
1533 cfg["PRODUCT_COPY_FILES"] += ("%s:foo/bar/%s" % (g.get("MY_OTHER_VAR", ""), x)).split()
1534`,
1535 },
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001536}
1537
1538var known_variables = []struct {
1539 name string
1540 class varClass
1541 starlarkType
1542}{
Cole Faustf1f44d32021-11-16 14:52:12 -08001543 {"NATIVE_COVERAGE", VarClassSoong, starlarkTypeBool},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001544 {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
1545 {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
1546 {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
1547 {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
1548 {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
1549 {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
1550 {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
1551 {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
1552 {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
1553 {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
1554 {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
1555 {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
1556 {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
1557 {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
1558}
1559
Sasha Smundak6609ba72021-07-22 18:32:56 -07001560type testMakefileFinder struct {
1561 fs fs.FS
1562 root string
1563 files []string
1564}
1565
1566func (t *testMakefileFinder) Find(root string) []string {
1567 if t.files != nil || root == t.root {
1568 return t.files
1569 }
1570 t.files = make([]string, 0)
1571 fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error {
1572 if err != nil {
1573 return err
1574 }
1575 if d.IsDir() {
1576 base := filepath.Base(path)
1577 if base[0] == '.' && len(base) > 1 {
1578 return fs.SkipDir
1579 }
1580 return nil
1581 }
1582 if strings.HasSuffix(path, ".mk") {
1583 t.files = append(t.files, path)
1584 }
1585 return nil
1586 })
1587 return t.files
1588}
1589
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001590func TestGood(t *testing.T) {
1591 for _, v := range known_variables {
1592 KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
1593 }
Sasha Smundak6609ba72021-07-22 18:32:56 -07001594 fs := NewFindMockFS([]string{
1595 "vendor/foo1/cfg.mk",
1596 "vendor/bar/baz/cfg.mk",
1597 "part.mk",
1598 "foo/font.mk",
1599 "bar/font.mk",
1600 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001601 for _, test := range testCases {
1602 t.Run(test.desc,
1603 func(t *testing.T) {
1604 ss, err := Convert(Request{
Sasha Smundak422b6142021-11-11 18:31:59 -08001605 MkFile: test.mkname,
1606 Reader: bytes.NewBufferString(test.in),
Sasha Smundak422b6142021-11-11 18:31:59 -08001607 OutputSuffix: ".star",
1608 SourceFS: fs,
1609 MakefileFinder: &testMakefileFinder{fs: fs},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001610 })
1611 if err != nil {
1612 t.Error(err)
1613 return
1614 }
1615 got := ss.String()
1616 if got != test.expected {
1617 t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
1618 strings.ReplaceAll(test.expected, "\n", "␤\n"),
1619 strings.ReplaceAll(got, "\n", "␤\n"))
1620 }
1621 })
1622 }
1623}