blob: ed9d47b0e273d1fafb4b477f9f7ece41701d1674 [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
571`,
572 expected: `load("//build/make/core:product_config.rbc", "rblf")
573
574def init(g, handle):
575 cfg = rblf.cfg(handle)
576 if not rblf.file_exists("foo.mk"):
577 pass
578 if rblf.file_wildcard_exists("foo*.mk"):
579 pass
580`,
581 },
582 {
Cole Faustf8320212021-11-10 15:05:07 -0800583 desc: "if with interpolation",
584 mkname: "product.mk",
585 in: `
586ifeq ($(VARIABLE1)text$(VARIABLE2),true)
587endif
588`,
589 expected: `load("//build/make/core:product_config.rbc", "rblf")
590
591def init(g, handle):
592 cfg = rblf.cfg(handle)
593 if "%stext%s" % (g.get("VARIABLE1", ""), g.get("VARIABLE2", "")) == "true":
594 pass
595`,
596 },
597 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800598 desc: "ifneq $(X),true",
599 mkname: "product.mk",
600 in: `
601ifneq ($(VARIABLE),true)
602endif
603`,
604 expected: `load("//build/make/core:product_config.rbc", "rblf")
605
606def init(g, handle):
607 cfg = rblf.cfg(handle)
608 if g.get("VARIABLE", "") != "true":
609 pass
610`,
611 },
612 {
613 desc: "Const neq",
614 mkname: "product.mk",
615 in: `
616ifneq (1,0)
617endif
618`,
619 expected: `load("//build/make/core:product_config.rbc", "rblf")
620
621def init(g, handle):
622 cfg = rblf.cfg(handle)
623 if "1" != "0":
624 pass
625`,
626 },
627 {
628 desc: "is-board calls",
629 mkname: "product.mk",
630 in: `
631ifeq ($(call is-board-platform-in-list,msm8998), true)
632else ifneq ($(call is-board-platform,copper),true)
633else ifneq ($(call is-vendor-board-platform,QCOM),true)
634else ifeq ($(call is-product-in-list, $(PLATFORM_LIST)), true)
635endif
636`,
637 expected: `load("//build/make/core:product_config.rbc", "rblf")
638
639def init(g, handle):
640 cfg = rblf.cfg(handle)
Cole Faustb2e0b602022-01-07 15:46:58 -0800641 if rblf.board_platform_in(g, "msm8998"):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800642 pass
Cole Faustb2e0b602022-01-07 15:46:58 -0800643 elif not rblf.board_platform_is(g, "copper"):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800644 pass
Cole Faustf0632662022-04-07 13:59:24 -0700645 elif g.get("TARGET_BOARD_PLATFORM", "") not in g.get("QCOM_BOARD_PLATFORMS", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800646 pass
647 elif g["TARGET_PRODUCT"] in g.get("PLATFORM_LIST", []):
648 pass
649`,
650 },
651 {
Sasha Smundak3a9b8e82021-08-25 14:11:04 -0700652 desc: "new is-board calls",
653 mkname: "product.mk",
654 in: `
655ifneq (,$(call is-board-platform-in-list2,msm8998 $(X))
656else ifeq (,$(call is-board-platform2,copper)
657else ifneq (,$(call is-vendor-board-qcom))
658endif
659`,
660 expected: `load("//build/make/core:product_config.rbc", "rblf")
661
662def init(g, handle):
663 cfg = rblf.cfg(handle)
664 if rblf.board_platform_in(g, "msm8998 %s" % g.get("X", "")):
665 pass
666 elif not rblf.board_platform_is(g, "copper"):
667 pass
Cole Faustf0632662022-04-07 13:59:24 -0700668 elif g.get("TARGET_BOARD_PLATFORM", "") in g.get("QCOM_BOARD_PLATFORMS", ""):
Sasha Smundak3a9b8e82021-08-25 14:11:04 -0700669 pass
670`,
671 },
672 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800673 desc: "findstring call",
674 mkname: "product.mk",
675 in: `
Cole Faust0e9418c2021-12-13 16:33:25 -0800676result := $(findstring a,a b c)
677result := $(findstring b,x y z)
678`,
679 expected: `load("//build/make/core:product_config.rbc", "rblf")
680
681def init(g, handle):
682 cfg = rblf.cfg(handle)
683 _result = rblf.findstring("a", "a b c")
684 _result = rblf.findstring("b", "x y z")
685`,
686 },
687 {
688 desc: "findstring in if statement",
689 mkname: "product.mk",
690 in: `
691ifeq ($(findstring foo,$(PRODUCT_PACKAGES)),)
692endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800693ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),)
694endif
Cole Faust0e9418c2021-12-13 16:33:25 -0800695ifeq ($(findstring foo,$(PRODUCT_PACKAGES)),foo)
696endif
697ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),foo)
698endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800699`,
700 expected: `load("//build/make/core:product_config.rbc", "rblf")
701
702def init(g, handle):
703 cfg = rblf.cfg(handle)
Cole Faust0e9418c2021-12-13 16:33:25 -0800704 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") == -1:
705 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800706 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
707 pass
Cole Faust0e9418c2021-12-13 16:33:25 -0800708 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
709 pass
710 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") == -1:
711 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800712`,
713 },
714 {
715 desc: "rhs call",
716 mkname: "product.mk",
717 in: `
718PRODUCT_COPY_FILES = $(call add-to-product-copy-files-if-exists, path:distpath) \
719 $(call find-copy-subdir-files, *, fromdir, todir) $(wildcard foo.*)
720`,
721 expected: `load("//build/make/core:product_config.rbc", "rblf")
722
723def init(g, handle):
724 cfg = rblf.cfg(handle)
725 cfg["PRODUCT_COPY_FILES"] = (rblf.copy_if_exists("path:distpath") +
726 rblf.find_and_copy("*", "fromdir", "todir") +
727 rblf.expand_wildcard("foo.*"))
728`,
729 },
730 {
731 desc: "inferred type",
732 mkname: "product.mk",
733 in: `
734HIKEY_MODS := $(wildcard foo/*.ko)
735BOARD_VENDOR_KERNEL_MODULES += $(HIKEY_MODS)
736`,
737 expected: `load("//build/make/core:product_config.rbc", "rblf")
738
739def init(g, handle):
740 cfg = rblf.cfg(handle)
741 g["HIKEY_MODS"] = rblf.expand_wildcard("foo/*.ko")
742 g.setdefault("BOARD_VENDOR_KERNEL_MODULES", [])
743 g["BOARD_VENDOR_KERNEL_MODULES"] += g["HIKEY_MODS"]
744`,
745 },
746 {
747 desc: "list with vars",
748 mkname: "product.mk",
749 in: `
750PRODUCT_COPY_FILES += path1:$(TARGET_PRODUCT)/path1 $(PRODUCT_MODEL)/path2:$(TARGET_PRODUCT)/path2
751`,
752 expected: `load("//build/make/core:product_config.rbc", "rblf")
753
754def init(g, handle):
755 cfg = rblf.cfg(handle)
756 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
757 cfg["PRODUCT_COPY_FILES"] += (("path1:%s/path1" % g["TARGET_PRODUCT"]).split() +
758 ("%s/path2:%s/path2" % (cfg.get("PRODUCT_MODEL", ""), g["TARGET_PRODUCT"])).split())
759`,
760 },
761 {
762 desc: "misc calls",
763 mkname: "product.mk",
764 in: `
765$(call enforce-product-packages-exist,)
766$(call enforce-product-packages-exist, foo)
767$(call require-artifacts-in-path, foo, bar)
768$(call require-artifacts-in-path-relaxed, foo, bar)
Sasha Smundakd6797852021-11-15 13:01:53 -0800769$(call dist-for-goals, goal, from:to)
Cole Faust1cc08852022-02-28 11:12:08 -0800770$(call add-product-dex-preopt-module-config,MyModule,disable)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800771`,
772 expected: `load("//build/make/core:product_config.rbc", "rblf")
773
774def init(g, handle):
775 cfg = rblf.cfg(handle)
Cole Faust6c41b8a2022-04-13 13:53:48 -0700776 rblf.enforce_product_packages_exist(handle, "")
777 rblf.enforce_product_packages_exist(handle, "foo")
Cole Faustea9db582022-03-21 17:50:05 -0700778 rblf.require_artifacts_in_path(handle, "foo", "bar")
779 rblf.require_artifacts_in_path_relaxed(handle, "foo", "bar")
Sasha Smundakd6797852021-11-15 13:01:53 -0800780 rblf.mkdist_for_goals(g, "goal", "from:to")
Cole Faust1cc08852022-02-28 11:12:08 -0800781 rblf.add_product_dex_preopt_module_config(handle, "MyModule", "disable")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800782`,
783 },
784 {
785 desc: "list with functions",
786 mkname: "product.mk",
787 in: `
788PRODUCT_COPY_FILES := $(call find-copy-subdir-files,*.kl,from1,to1) \
789 $(call find-copy-subdir-files,*.kc,from2,to2) \
790 foo bar
791`,
792 expected: `load("//build/make/core:product_config.rbc", "rblf")
793
794def init(g, handle):
795 cfg = rblf.cfg(handle)
796 cfg["PRODUCT_COPY_FILES"] = (rblf.find_and_copy("*.kl", "from1", "to1") +
797 rblf.find_and_copy("*.kc", "from2", "to2") +
798 [
799 "foo",
800 "bar",
801 ])
802`,
803 },
804 {
805 desc: "Text functions",
806 mkname: "product.mk",
807 in: `
808PRODUCT_COPY_FILES := $(addprefix pfx-,a b c)
809PRODUCT_COPY_FILES := $(addsuffix .sff, a b c)
810PRODUCT_NAME := $(word 1, $(subst ., ,$(TARGET_BOARD_PLATFORM)))
Cole Faust94c4a9a2022-04-22 17:43:52 -0700811ifeq (1,$(words $(SOME_UNKNOWN_VARIABLE)))
812endif
813ifeq ($(words $(SOME_OTHER_VARIABLE)),$(SOME_INT_VARIABLE))
814endif
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")
Cole Faust94c4a9a2022-04-22 17:43:52 -0700837 cfg["PRODUCT_NAME"] = rblf.words((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " "))[0]
838 if len(rblf.words(g.get("SOME_UNKNOWN_VARIABLE", ""))) == 1:
839 pass
840 if ("%d" % (len(rblf.words(g.get("SOME_OTHER_VARIABLE", ""))))) == g.get("SOME_INT_VARIABLE", ""):
841 pass
Sasha Smundak35434ed2021-11-05 16:29:56 -0700842 rblf.mkinfo("product.mk", rblf.mkpatsubst("%.pub", "%s%%" % cfg["PRODUCT_NAME"], g.get("PRODUCT_ADB_KEYS", "")))
Cole Faust0e2b2562022-04-01 11:46:50 -0700843 rblf.mkinfo("product.mk", "$(dir foo/bar): %s" % rblf.dir("foo/bar"))
Sasha Smundak16e07732021-07-23 11:38:23 -0700844 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][0])
845 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][-1])
846 rblf.mkinfo("product.mk", rblf.dir("product.mk"))
847 rblf.mkinfo("product.mk", rblf.dir(cfg["PRODUCT_COPY_FILES"][-1]))
848 rblf.mkinfo("product.mk", rblf.dir((_foobar).split()[-1]))
849 rblf.mkinfo("product.mk", rblf.abspath("foo/bar"))
850 rblf.mkinfo("product.mk", rblf.notdir("foo/bar"))
Sasha Smundak65b547e2021-09-17 15:35:41 -0700851 rblf.soong_config_namespace(g, "snsconfig")
852 rblf.soong_config_set(g, "snsconfig", "imagetype", "odm_image")
853 rblf.soong_config_set(g, "snsconfig", "foo", "foo_value")
854 rblf.soong_config_append(g, "snsconfig", "bar", "bar_value")
Sasha Smundak3deb9682021-07-26 18:42:25 -0700855 cfg["PRODUCT_COPY_FILES"] = rblf.copy_files(rblf.expand_wildcard("foo*.mk"), "etc")
Sasha Smundak04453082021-08-17 18:14:41 -0700856 cfg["PRODUCT_COPY_FILES"] = rblf.product_copy_files_by_pattern("from/%", "to/%", "a b c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800857`,
858 },
859 {
Sasha Smundak9d011ab2021-07-09 16:00:57 -0700860 desc: "subst in list",
861 mkname: "product.mk",
862 in: `
863files = $(call find-copy-subdir-files,*,from,to)
864PRODUCT_COPY_FILES += $(subst foo,bar,$(files))
865`,
866 expected: `load("//build/make/core:product_config.rbc", "rblf")
867
868def init(g, handle):
869 cfg = rblf.cfg(handle)
870 _files = rblf.find_and_copy("*", "from", "to")
871 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
872 cfg["PRODUCT_COPY_FILES"] += rblf.mksubst("foo", "bar", _files)
873`,
874 },
875 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800876 desc: "assignment flavors",
877 mkname: "product.mk",
878 in: `
879PRODUCT_LIST1 := a
880PRODUCT_LIST2 += a
881PRODUCT_LIST1 += b
882PRODUCT_LIST2 += b
883PRODUCT_LIST3 ?= a
884PRODUCT_LIST1 = c
885PLATFORM_LIST += x
886PRODUCT_PACKAGES := $(PLATFORM_LIST)
887`,
888 expected: `load("//build/make/core:product_config.rbc", "rblf")
889
890def init(g, handle):
891 cfg = rblf.cfg(handle)
892 cfg["PRODUCT_LIST1"] = ["a"]
893 rblf.setdefault(handle, "PRODUCT_LIST2")
894 cfg["PRODUCT_LIST2"] += ["a"]
895 cfg["PRODUCT_LIST1"] += ["b"]
896 cfg["PRODUCT_LIST2"] += ["b"]
897 if cfg.get("PRODUCT_LIST3") == None:
898 cfg["PRODUCT_LIST3"] = ["a"]
899 cfg["PRODUCT_LIST1"] = ["c"]
900 g.setdefault("PLATFORM_LIST", [])
901 g["PLATFORM_LIST"] += ["x"]
902 cfg["PRODUCT_PACKAGES"] = g["PLATFORM_LIST"][:]
903`,
904 },
905 {
906 desc: "assigment flavors2",
907 mkname: "product.mk",
908 in: `
909PRODUCT_LIST1 = a
910ifeq (0,1)
911 PRODUCT_LIST1 += b
912 PRODUCT_LIST2 += b
913endif
914PRODUCT_LIST1 += c
915PRODUCT_LIST2 += c
916`,
917 expected: `load("//build/make/core:product_config.rbc", "rblf")
918
919def init(g, handle):
920 cfg = rblf.cfg(handle)
921 cfg["PRODUCT_LIST1"] = ["a"]
922 if "0" == "1":
923 cfg["PRODUCT_LIST1"] += ["b"]
924 rblf.setdefault(handle, "PRODUCT_LIST2")
925 cfg["PRODUCT_LIST2"] += ["b"]
926 cfg["PRODUCT_LIST1"] += ["c"]
927 rblf.setdefault(handle, "PRODUCT_LIST2")
928 cfg["PRODUCT_LIST2"] += ["c"]
929`,
930 },
931 {
Cole Fauste2a37982022-03-09 16:00:17 -0800932 desc: "assigment setdefaults",
933 mkname: "product.mk",
934 in: `
935# All of these should have a setdefault because they're self-referential and not defined before
936PRODUCT_LIST1 = a $(PRODUCT_LIST1)
937PRODUCT_LIST2 ?= a $(PRODUCT_LIST2)
938PRODUCT_LIST3 += a
939
940# Now doing them again should not have a setdefault because they've already been set
941PRODUCT_LIST1 = a $(PRODUCT_LIST1)
942PRODUCT_LIST2 ?= a $(PRODUCT_LIST2)
943PRODUCT_LIST3 += a
944`,
945 expected: `# All of these should have a setdefault because they're self-referential and not defined before
946load("//build/make/core:product_config.rbc", "rblf")
947
948def init(g, handle):
949 cfg = rblf.cfg(handle)
950 rblf.setdefault(handle, "PRODUCT_LIST1")
951 cfg["PRODUCT_LIST1"] = (["a"] +
952 cfg.get("PRODUCT_LIST1", []))
953 if cfg.get("PRODUCT_LIST2") == None:
954 rblf.setdefault(handle, "PRODUCT_LIST2")
955 cfg["PRODUCT_LIST2"] = (["a"] +
956 cfg.get("PRODUCT_LIST2", []))
957 rblf.setdefault(handle, "PRODUCT_LIST3")
958 cfg["PRODUCT_LIST3"] += ["a"]
959 # Now doing them again should not have a setdefault because they've already been set
960 cfg["PRODUCT_LIST1"] = (["a"] +
961 cfg["PRODUCT_LIST1"])
962 if cfg.get("PRODUCT_LIST2") == None:
963 cfg["PRODUCT_LIST2"] = (["a"] +
964 cfg["PRODUCT_LIST2"])
965 cfg["PRODUCT_LIST3"] += ["a"]
966`,
967 },
968 {
Sasha Smundak3deb9682021-07-26 18:42:25 -0700969 desc: "soong namespace assignments",
970 mkname: "product.mk",
971 in: `
972SOONG_CONFIG_NAMESPACES += cvd
973SOONG_CONFIG_cvd += launch_configs
Sasha Smundak65b547e2021-09-17 15:35:41 -0700974SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
Sasha Smundak3deb9682021-07-26 18:42:25 -0700975SOONG_CONFIG_cvd += grub_config
976SOONG_CONFIG_cvd_grub_config += grub.cfg
Sasha Smundak65b547e2021-09-17 15:35:41 -0700977x := $(SOONG_CONFIG_cvd_grub_config)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700978`,
979 expected: `load("//build/make/core:product_config.rbc", "rblf")
980
981def init(g, handle):
982 cfg = rblf.cfg(handle)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700983 rblf.soong_config_namespace(g, "cvd")
984 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
985 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
Sasha Smundak422b6142021-11-11 18:31:59 -0800986 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 -0700987`,
Cole Faustc00184e2021-11-08 12:08:57 -0800988 }, {
989 desc: "soong namespace accesses",
990 mkname: "product.mk",
991 in: `
992SOONG_CONFIG_NAMESPACES += cvd
993SOONG_CONFIG_cvd += launch_configs
994SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
995SOONG_CONFIG_cvd += grub_config
996SOONG_CONFIG_cvd_grub_config += grub.cfg
997x := $(call soong_config_get,cvd,grub_config)
998`,
999 expected: `load("//build/make/core:product_config.rbc", "rblf")
1000
1001def init(g, handle):
1002 cfg = rblf.cfg(handle)
1003 rblf.soong_config_namespace(g, "cvd")
1004 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
1005 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
1006 _x = rblf.soong_config_get(g, "cvd", "grub_config")
1007`,
Sasha Smundak3deb9682021-07-26 18:42:25 -07001008 },
1009 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001010 desc: "string split",
1011 mkname: "product.mk",
1012 in: `
1013PRODUCT_LIST1 = a
1014local = b
1015local += c
1016FOO = d
1017FOO += e
1018PRODUCT_LIST1 += $(local)
1019PRODUCT_LIST1 += $(FOO)
1020`,
1021 expected: `load("//build/make/core:product_config.rbc", "rblf")
1022
1023def init(g, handle):
1024 cfg = rblf.cfg(handle)
1025 cfg["PRODUCT_LIST1"] = ["a"]
1026 _local = "b"
1027 _local += " " + "c"
1028 g["FOO"] = "d"
1029 g["FOO"] += " " + "e"
1030 cfg["PRODUCT_LIST1"] += (_local).split()
1031 cfg["PRODUCT_LIST1"] += (g["FOO"]).split()
1032`,
1033 },
1034 {
1035 desc: "apex_jars",
1036 mkname: "product.mk",
1037 in: `
1038PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex
1039`,
1040 expected: `load("//build/make/core:product_config.rbc", "rblf")
1041
1042def init(g, handle):
1043 cfg = rblf.cfg(handle)
1044 cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) +
1045 ["framework-minus-apex"])
1046`,
1047 },
1048 {
Cole Faust95b95cb2022-04-05 16:37:39 -07001049 desc: "strip/sort functions",
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001050 mkname: "product.mk",
1051 in: `
1052ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),)
1053 PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress)
1054endif
Cole Faust95b95cb2022-04-05 16:37:39 -07001055MY_VAR := $(sort b a c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001056`,
1057 expected: `load("//build/make/core:product_config.rbc", "rblf")
1058
1059def init(g, handle):
1060 cfg = rblf.cfg(handle)
1061 if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []):
Cole Faust816e0802022-03-04 12:04:31 -08001062 rblf.setdefault(handle, "PRODUCT_PACKAGES")
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001063 cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split()
Cole Faust95b95cb2022-04-05 16:37:39 -07001064 g["MY_VAR"] = rblf.mksort("b a c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001065`,
1066 },
1067 {
1068 desc: "strip func in condition",
1069 mkname: "product.mk",
1070 in: `
1071ifneq ($(strip $(TARGET_VENDOR)),)
1072endif
1073`,
1074 expected: `load("//build/make/core:product_config.rbc", "rblf")
1075
1076def init(g, handle):
1077 cfg = rblf.cfg(handle)
Sasha Smundak0554d762021-07-08 18:26:12 -07001078 if rblf.mkstrip(g.get("TARGET_VENDOR", "")):
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001079 pass
1080`,
1081 },
1082 {
1083 desc: "ref after set",
1084 mkname: "product.mk",
1085 in: `
1086PRODUCT_ADB_KEYS:=value
1087FOO := $(PRODUCT_ADB_KEYS)
1088ifneq (,$(PRODUCT_ADB_KEYS))
1089endif
1090`,
1091 expected: `load("//build/make/core:product_config.rbc", "rblf")
1092
1093def init(g, handle):
1094 cfg = rblf.cfg(handle)
1095 g["PRODUCT_ADB_KEYS"] = "value"
1096 g["FOO"] = g["PRODUCT_ADB_KEYS"]
1097 if g["PRODUCT_ADB_KEYS"]:
1098 pass
1099`,
1100 },
1101 {
1102 desc: "ref before set",
1103 mkname: "product.mk",
1104 in: `
1105V1 := $(PRODUCT_ADB_KEYS)
1106ifeq (,$(PRODUCT_ADB_KEYS))
1107 V2 := $(PRODUCT_ADB_KEYS)
1108 PRODUCT_ADB_KEYS:=foo
1109 V3 := $(PRODUCT_ADB_KEYS)
1110endif`,
1111 expected: `load("//build/make/core:product_config.rbc", "rblf")
1112
1113def init(g, handle):
1114 cfg = rblf.cfg(handle)
1115 g["V1"] = g.get("PRODUCT_ADB_KEYS", "")
1116 if not g.get("PRODUCT_ADB_KEYS", ""):
1117 g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
1118 g["PRODUCT_ADB_KEYS"] = "foo"
1119 g["V3"] = g["PRODUCT_ADB_KEYS"]
1120`,
1121 },
Sasha Smundak6609ba72021-07-22 18:32:56 -07001122 {
1123 desc: "Dynamic inherit path",
1124 mkname: "product.mk",
1125 in: `
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001126MY_PATH:=foo
Sasha Smundak6609ba72021-07-22 18:32:56 -07001127$(call inherit-product,vendor/$(MY_PATH)/cfg.mk)
1128`,
1129 expected: `load("//build/make/core:product_config.rbc", "rblf")
1130load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1131load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init")
1132
1133def init(g, handle):
1134 cfg = rblf.cfg(handle)
1135 g["MY_PATH"] = "foo"
1136 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001137 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1138 "vendor/bar/baz/cfg.mk": ("vendor/bar/baz/cfg", _cfg1_init),
Sasha Smundak6609ba72021-07-22 18:32:56 -07001139 }.get("vendor/%s/cfg.mk" % g["MY_PATH"])
1140 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1141 if not _varmod_init:
Cole Faust7321b092021-12-21 16:11:16 -08001142 rblf.mkerror("product.mk", "Cannot find %s" % ("vendor/%s/cfg.mk" % g["MY_PATH"]))
Sasha Smundak6609ba72021-07-22 18:32:56 -07001143 rblf.inherit(handle, _varmod, _varmod_init)
1144`,
1145 },
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001146 {
1147 desc: "Dynamic inherit with hint",
1148 mkname: "product.mk",
1149 in: `
1150MY_PATH:=foo
1151#RBC# include_top vendor/foo1
1152$(call inherit-product,$(MY_PATH)/cfg.mk)
1153`,
1154 expected: `load("//build/make/core:product_config.rbc", "rblf")
1155load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1156
1157def init(g, handle):
1158 cfg = rblf.cfg(handle)
1159 g["MY_PATH"] = "foo"
Cole Faust93f8d392022-03-02 13:31:30 -08001160 _entry = {
1161 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1162 }.get("%s/cfg.mk" % g["MY_PATH"])
1163 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1164 if not _varmod_init:
1165 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1166 rblf.inherit(handle, _varmod, _varmod_init)
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001167`,
1168 },
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001169 {
Cole Faustf7ed5342021-12-21 14:15:12 -08001170 desc: "Dynamic inherit with duplicated hint",
1171 mkname: "product.mk",
1172 in: `
1173MY_PATH:=foo
1174#RBC# include_top vendor/foo1
1175$(call inherit-product,$(MY_PATH)/cfg.mk)
1176#RBC# include_top vendor/foo1
Cole Faust7940c6a2022-01-31 15:54:05 -08001177#RBC# include_top vendor/foo1
Cole Faustf7ed5342021-12-21 14:15:12 -08001178$(call inherit-product,$(MY_PATH)/cfg.mk)
1179`,
1180 expected: `load("//build/make/core:product_config.rbc", "rblf")
1181load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1182
1183def init(g, handle):
1184 cfg = rblf.cfg(handle)
1185 g["MY_PATH"] = "foo"
Cole Faust93f8d392022-03-02 13:31:30 -08001186 _entry = {
1187 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1188 }.get("%s/cfg.mk" % g["MY_PATH"])
1189 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1190 if not _varmod_init:
1191 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1192 rblf.inherit(handle, _varmod, _varmod_init)
1193 _entry = {
1194 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1195 }.get("%s/cfg.mk" % g["MY_PATH"])
1196 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1197 if not _varmod_init:
1198 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1199 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faustf7ed5342021-12-21 14:15:12 -08001200`,
1201 },
1202 {
Cole Faust069aba62022-01-26 17:47:33 -08001203 desc: "Dynamic inherit path that lacks hint",
Cole Faust6c934f62022-01-06 15:51:12 -08001204 mkname: "product.mk",
1205 in: `
1206#RBC# include_top foo
1207$(call inherit-product,$(MY_VAR)/font.mk)
1208
1209#RBC# include_top foo
1210
1211# There's some space and even this comment between the include_top and the inherit-product
1212
1213$(call inherit-product,$(MY_VAR)/font.mk)
1214
1215$(call inherit-product,$(MY_VAR)/font.mk)
1216`,
Cole Faust7940c6a2022-01-31 15:54:05 -08001217 expected: `load("//build/make/core:product_config.rbc", "rblf")
Cole Faust6c934f62022-01-06 15:51:12 -08001218load("//foo:font.star|init", _font_init = "init")
Cole Faust069aba62022-01-26 17:47:33 -08001219load("//bar:font.star|init", _font1_init = "init")
Cole Faust6c934f62022-01-06 15:51:12 -08001220
1221def init(g, handle):
1222 cfg = rblf.cfg(handle)
Cole Faust93f8d392022-03-02 13:31:30 -08001223 _entry = {
1224 "foo/font.mk": ("foo/font", _font_init),
1225 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1226 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1227 if not _varmod_init:
1228 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1229 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001230 # There's some space and even this comment between the include_top and the inherit-product
Cole Faust93f8d392022-03-02 13:31:30 -08001231 _entry = {
1232 "foo/font.mk": ("foo/font", _font_init),
1233 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1234 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1235 if not _varmod_init:
1236 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1237 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faustf4e72cf2022-02-08 12:49:37 -08001238 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 -08001239 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001240 "foo/font.mk": ("foo/font", _font_init),
Cole Faust069aba62022-01-26 17:47:33 -08001241 "bar/font.mk": ("bar/font", _font1_init),
Cole Faust6c934f62022-01-06 15:51:12 -08001242 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1243 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1244 if not _varmod_init:
1245 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1246 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001247`,
1248 },
1249 {
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001250 desc: "Ignore make rules",
1251 mkname: "product.mk",
1252 in: `
1253foo: foo.c
1254 gcc -o $@ $*`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001255 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001256
1257def init(g, handle):
1258 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001259 rblf.mk2rbc_error("product.mk:2", "unsupported line rule: foo: foo.c\n#gcc -o $@ $*")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001260`,
1261 },
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001262 {
1263 desc: "Flag override",
1264 mkname: "product.mk",
1265 in: `
1266override FOO:=`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001267 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001268
1269def init(g, handle):
1270 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001271 rblf.mk2rbc_error("product.mk:2", "cannot handle override directive")
Sasha Smundak422b6142021-11-11 18:31:59 -08001272`,
1273 },
1274 {
1275 desc: "Bad expression",
1276 mkname: "build/product.mk",
1277 in: `
1278ifeq (,$(call foobar))
1279endif
1280`,
1281 expected: `load("//build/make/core:product_config.rbc", "rblf")
1282
1283def init(g, handle):
1284 cfg = rblf.cfg(handle)
1285 if rblf.mk2rbc_error("build/product.mk:2", "cannot handle invoking foobar"):
1286 pass
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001287`,
1288 },
Cole Faust4eadba72021-12-07 11:54:52 -08001289 {
1290 desc: "if expression",
1291 mkname: "product.mk",
1292 in: `
1293TEST_VAR := foo
1294TEST_VAR_LIST := foo
1295TEST_VAR_LIST += bar
1296TEST_VAR_2 := $(if $(TEST_VAR),bar)
1297TEST_VAR_3 := $(if $(TEST_VAR),bar,baz)
Cole Faust421a1922022-03-16 14:35:45 -07001298TEST_VAR_4 := $(if $(TEST_VAR),$(TEST_VAR_LIST))
Cole Faust4eadba72021-12-07 11:54:52 -08001299`,
1300 expected: `load("//build/make/core:product_config.rbc", "rblf")
1301
1302def init(g, handle):
1303 cfg = rblf.cfg(handle)
1304 g["TEST_VAR"] = "foo"
1305 g["TEST_VAR_LIST"] = ["foo"]
1306 g["TEST_VAR_LIST"] += ["bar"]
1307 g["TEST_VAR_2"] = ("bar" if g["TEST_VAR"] else "")
1308 g["TEST_VAR_3"] = ("bar" if g["TEST_VAR"] else "baz")
Cole Faust421a1922022-03-16 14:35:45 -07001309 g["TEST_VAR_4"] = (g["TEST_VAR_LIST"] if g["TEST_VAR"] else [])
Cole Faust4eadba72021-12-07 11:54:52 -08001310`,
1311 },
Cole Faustc36c9622021-12-07 15:20:45 -08001312 {
1313 desc: "substitution references",
1314 mkname: "product.mk",
1315 in: `
1316SOURCES := foo.c bar.c
1317OBJECTS := $(SOURCES:.c=.o)
1318OBJECTS2 := $(SOURCES:%.c=%.o)
1319`,
1320 expected: `load("//build/make/core:product_config.rbc", "rblf")
1321
1322def init(g, handle):
1323 cfg = rblf.cfg(handle)
1324 g["SOURCES"] = "foo.c bar.c"
1325 g["OBJECTS"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1326 g["OBJECTS2"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1327`,
1328 },
Cole Faustb0d32ab2021-12-09 14:00:59 -08001329 {
1330 desc: "foreach expressions",
1331 mkname: "product.mk",
1332 in: `
1333BOOT_KERNEL_MODULES := foo.ko bar.ko
1334BOOT_KERNEL_MODULES_FILTER := $(foreach m,$(BOOT_KERNEL_MODULES),%/$(m))
1335BOOT_KERNEL_MODULES_LIST := foo.ko
1336BOOT_KERNEL_MODULES_LIST += bar.ko
1337BOOT_KERNEL_MODULES_FILTER_2 := $(foreach m,$(BOOT_KERNEL_MODULES_LIST),%/$(m))
1338
Cole Faustb67aa082022-02-28 16:39:59 -08001339FOREACH_WITH_IF := $(foreach module,\
1340 $(BOOT_KERNEL_MODULES_LIST),\
1341 $(if $(filter $(module),foo.ko),,$(error module "$(module)" has an error!)))
Cole Faustf035d402022-03-28 14:02:50 -07001342
1343# Same as above, but not assigning it to a variable allows it to be converted to statements
1344$(foreach module,\
1345 $(BOOT_KERNEL_MODULES_LIST),\
1346 $(if $(filter $(module),foo.ko),,$(error module "$(module)" has an error!)))
Cole Faustb0d32ab2021-12-09 14:00:59 -08001347`,
1348 expected: `load("//build/make/core:product_config.rbc", "rblf")
1349
1350def init(g, handle):
1351 cfg = rblf.cfg(handle)
1352 g["BOOT_KERNEL_MODULES"] = "foo.ko bar.ko"
1353 g["BOOT_KERNEL_MODULES_FILTER"] = ["%%/%s" % m for m in rblf.words(g["BOOT_KERNEL_MODULES"])]
1354 g["BOOT_KERNEL_MODULES_LIST"] = ["foo.ko"]
1355 g["BOOT_KERNEL_MODULES_LIST"] += ["bar.ko"]
1356 g["BOOT_KERNEL_MODULES_FILTER_2"] = ["%%/%s" % m for m in g["BOOT_KERNEL_MODULES_LIST"]]
Cole Faustb67aa082022-02-28 16:39:59 -08001357 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 -07001358 # Same as above, but not assigning it to a variable allows it to be converted to statements
1359 for module in g["BOOT_KERNEL_MODULES_LIST"]:
1360 if not rblf.filter(module, "foo.ko"):
1361 rblf.mkerror("product.mk", "module \"%s\" has an error!" % module)
Cole Faustb0d32ab2021-12-09 14:00:59 -08001362`,
1363 },
Cole Faust0484c232021-12-22 14:08:08 -08001364 {
1365 desc: "List appended to string",
1366 mkname: "product.mk",
1367 in: `
1368NATIVE_BRIDGE_PRODUCT_PACKAGES := \
1369 libnative_bridge_vdso.native_bridge \
1370 native_bridge_guest_app_process.native_bridge \
1371 native_bridge_guest_linker.native_bridge
1372
1373NATIVE_BRIDGE_MODIFIED_GUEST_LIBS := \
1374 libaaudio \
1375 libamidi \
1376 libandroid \
1377 libandroid_runtime
1378
1379NATIVE_BRIDGE_PRODUCT_PACKAGES += \
1380 $(addsuffix .native_bridge,$(NATIVE_BRIDGE_ORIG_GUEST_LIBS))
1381`,
1382 expected: `load("//build/make/core:product_config.rbc", "rblf")
1383
1384def init(g, handle):
1385 cfg = rblf.cfg(handle)
1386 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] = "libnative_bridge_vdso.native_bridge native_bridge_guest_app_process.native_bridge native_bridge_guest_linker.native_bridge"
1387 g["NATIVE_BRIDGE_MODIFIED_GUEST_LIBS"] = "libaaudio libamidi libandroid libandroid_runtime"
1388 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] += " " + " ".join(rblf.addsuffix(".native_bridge", g.get("NATIVE_BRIDGE_ORIG_GUEST_LIBS", "")))
1389`,
1390 },
Cole Faustb1103e22022-01-06 15:22:05 -08001391 {
1392 desc: "Math functions",
1393 mkname: "product.mk",
1394 in: `
1395# Test the math functions defined in build/make/common/math.mk
1396ifeq ($(call math_max,2,5),5)
1397endif
1398ifeq ($(call math_min,2,5),2)
1399endif
1400ifeq ($(call math_gt_or_eq,2,5),true)
1401endif
1402ifeq ($(call math_gt,2,5),true)
1403endif
1404ifeq ($(call math_lt,2,5),true)
1405endif
1406ifeq ($(call math_gt_or_eq,2,5),)
1407endif
1408ifeq ($(call math_gt,2,5),)
1409endif
1410ifeq ($(call math_lt,2,5),)
1411endif
1412ifeq ($(call math_gt_or_eq,$(MY_VAR), 5),true)
1413endif
1414ifeq ($(call math_gt_or_eq,$(MY_VAR),$(MY_OTHER_VAR)),true)
1415endif
1416ifeq ($(call math_gt_or_eq,100$(MY_VAR),10),true)
1417endif
1418`,
1419 expected: `# Test the math functions defined in build/make/common/math.mk
1420load("//build/make/core:product_config.rbc", "rblf")
1421
1422def init(g, handle):
1423 cfg = rblf.cfg(handle)
1424 if max(2, 5) == 5:
1425 pass
1426 if min(2, 5) == 2:
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 2 <= 5:
1437 pass
1438 if 2 >= 5:
1439 pass
1440 if int(g.get("MY_VAR", "")) >= 5:
1441 pass
1442 if int(g.get("MY_VAR", "")) >= int(g.get("MY_OTHER_VAR", "")):
1443 pass
1444 if int("100%s" % g.get("MY_VAR", "")) >= 10:
1445 pass
1446`,
1447 },
Cole Faustf92c9f22022-03-14 14:35:50 -07001448 {
1449 desc: "Type hints",
1450 mkname: "product.mk",
1451 in: `
1452# Test type hints
1453#RBC# type_hint list MY_VAR MY_VAR_2
1454# Unsupported type
1455#RBC# type_hint bool MY_VAR_3
1456# Invalid syntax
1457#RBC# type_hint list
1458# Duplicated variable
1459#RBC# type_hint list MY_VAR_2
1460#RBC# type_hint list my-local-var-with-dashes
Cole Faust421a1922022-03-16 14:35:45 -07001461#RBC# type_hint string MY_STRING_VAR
Cole Faustf92c9f22022-03-14 14:35:50 -07001462
1463MY_VAR := foo
1464MY_VAR_UNHINTED := foo
1465
1466# Vars set after other statements still get the hint
1467MY_VAR_2 := foo
1468
1469# You can't specify a type hint after the first statement
1470#RBC# type_hint list MY_VAR_4
1471MY_VAR_4 := foo
1472
1473my-local-var-with-dashes := foo
Cole Faust421a1922022-03-16 14:35:45 -07001474
1475MY_STRING_VAR := $(wildcard foo/bar.mk)
Cole Faustf92c9f22022-03-14 14:35:50 -07001476`,
1477 expected: `# Test type hints
1478# Unsupported type
1479load("//build/make/core:product_config.rbc", "rblf")
1480
1481def init(g, handle):
1482 cfg = rblf.cfg(handle)
1483 rblf.mk2rbc_error("product.mk:5", "Invalid type_hint annotation. Only list/string types are accepted, found bool")
1484 # Invalid syntax
1485 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")
1486 # Duplicated variable
1487 rblf.mk2rbc_error("product.mk:9", "Duplicate type hint for variable MY_VAR_2")
1488 g["MY_VAR"] = ["foo"]
1489 g["MY_VAR_UNHINTED"] = "foo"
1490 # Vars set after other statements still get the hint
1491 g["MY_VAR_2"] = ["foo"]
1492 # You can't specify a type hint after the first statement
Cole Faust421a1922022-03-16 14:35:45 -07001493 rblf.mk2rbc_error("product.mk:20", "type_hint annotations must come before the first Makefile statement")
Cole Faustf92c9f22022-03-14 14:35:50 -07001494 g["MY_VAR_4"] = "foo"
1495 _my_local_var_with_dashes = ["foo"]
Cole Faust421a1922022-03-16 14:35:45 -07001496 g["MY_STRING_VAR"] = " ".join(rblf.expand_wildcard("foo/bar.mk"))
Cole Faustf92c9f22022-03-14 14:35:50 -07001497`,
1498 },
Cole Faustf5adedc2022-03-18 14:05:06 -07001499 {
1500 desc: "Set LOCAL_PATH to my-dir",
1501 mkname: "product.mk",
1502 in: `
1503LOCAL_PATH := $(call my-dir)
1504`,
1505 expected: `load("//build/make/core:product_config.rbc", "rblf")
1506
1507def init(g, handle):
1508 cfg = rblf.cfg(handle)
1509
1510`,
1511 },
Cole Faustf035d402022-03-28 14:02:50 -07001512 {
1513 desc: "Evals",
1514 mkname: "product.mk",
1515 in: `
1516$(eval)
1517$(eval MY_VAR := foo)
1518$(eval # This is a test of eval functions)
1519$(eval $(TOO_COMPLICATED) := bar)
1520$(foreach x,$(MY_LIST_VAR), \
1521 $(eval PRODUCT_COPY_FILES += foo/bar/$(x):$(TARGET_COPY_OUT_VENDOR)/etc/$(x)) \
1522 $(if $(MY_OTHER_VAR),$(eval PRODUCT_COPY_FILES += $(MY_OTHER_VAR):foo/bar/$(x))) \
1523)
1524
1525`,
1526 expected: `load("//build/make/core:product_config.rbc", "rblf")
1527
1528def init(g, handle):
1529 cfg = rblf.cfg(handle)
1530 g["MY_VAR"] = "foo"
1531 # This is a test of eval functions
1532 rblf.mk2rbc_error("product.mk:5", "Eval expression too complex; only assignments and comments are supported")
1533 for x in rblf.words(g.get("MY_LIST_VAR", "")):
1534 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
1535 cfg["PRODUCT_COPY_FILES"] += ("foo/bar/%s:%s/etc/%s" % (x, g.get("TARGET_COPY_OUT_VENDOR", ""), x)).split()
1536 if g.get("MY_OTHER_VAR", ""):
1537 cfg["PRODUCT_COPY_FILES"] += ("%s:foo/bar/%s" % (g.get("MY_OTHER_VAR", ""), x)).split()
1538`,
1539 },
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001540}
1541
1542var known_variables = []struct {
1543 name string
1544 class varClass
1545 starlarkType
1546}{
Cole Faustf1f44d32021-11-16 14:52:12 -08001547 {"NATIVE_COVERAGE", VarClassSoong, starlarkTypeBool},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001548 {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
1549 {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
1550 {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
1551 {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
1552 {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
1553 {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
1554 {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
1555 {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
1556 {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
1557 {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
1558 {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
1559 {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
1560 {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
1561 {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
1562}
1563
Sasha Smundak6609ba72021-07-22 18:32:56 -07001564type testMakefileFinder struct {
1565 fs fs.FS
1566 root string
1567 files []string
1568}
1569
1570func (t *testMakefileFinder) Find(root string) []string {
1571 if t.files != nil || root == t.root {
1572 return t.files
1573 }
1574 t.files = make([]string, 0)
1575 fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error {
1576 if err != nil {
1577 return err
1578 }
1579 if d.IsDir() {
1580 base := filepath.Base(path)
1581 if base[0] == '.' && len(base) > 1 {
1582 return fs.SkipDir
1583 }
1584 return nil
1585 }
1586 if strings.HasSuffix(path, ".mk") {
1587 t.files = append(t.files, path)
1588 }
1589 return nil
1590 })
1591 return t.files
1592}
1593
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001594func TestGood(t *testing.T) {
1595 for _, v := range known_variables {
1596 KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
1597 }
Sasha Smundak6609ba72021-07-22 18:32:56 -07001598 fs := NewFindMockFS([]string{
1599 "vendor/foo1/cfg.mk",
1600 "vendor/bar/baz/cfg.mk",
1601 "part.mk",
1602 "foo/font.mk",
1603 "bar/font.mk",
1604 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001605 for _, test := range testCases {
1606 t.Run(test.desc,
1607 func(t *testing.T) {
1608 ss, err := Convert(Request{
Sasha Smundak422b6142021-11-11 18:31:59 -08001609 MkFile: test.mkname,
1610 Reader: bytes.NewBufferString(test.in),
Sasha Smundak422b6142021-11-11 18:31:59 -08001611 OutputSuffix: ".star",
1612 SourceFS: fs,
1613 MakefileFinder: &testMakefileFinder{fs: fs},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001614 })
1615 if err != nil {
1616 t.Error(err)
1617 return
1618 }
1619 got := ss.String()
1620 if got != test.expected {
1621 t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
1622 strings.ReplaceAll(test.expected, "\n", "␤\n"),
1623 strings.ReplaceAll(got, "\n", "␤\n"))
1624 }
1625 })
1626 }
1627}