blob: 7e68026b2169cd6dbcfb122f383f7f2f2ff69613 [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)
Cole Faust1e275862022-04-26 14:28:04 -0700120 cfg["PRODUCT_NAME"] = rblf.mk2rbc_error("product.mk:2", "cannot handle invoking foo1")
121 cfg["PRODUCT_NAME"] = 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
Cole Faust72374fc2022-05-05 11:45:04 -0700582 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)))
Cole Faust94c4a9a2022-04-22 17:43:52 -0700815ifeq (1,$(words $(SOME_UNKNOWN_VARIABLE)))
816endif
817ifeq ($(words $(SOME_OTHER_VARIABLE)),$(SOME_INT_VARIABLE))
818endif
Sasha Smundak35434ed2021-11-05 16:29:56 -0700819$(info $(patsubst %.pub,$(PRODUCT_NAME)%,$(PRODUCT_ADB_KEYS)))
Cole Faust0e2b2562022-04-01 11:46:50 -0700820$(info $$(dir foo/bar): $(dir foo/bar))
Sasha Smundak16e07732021-07-23 11:38:23 -0700821$(info $(firstword $(PRODUCT_COPY_FILES)))
822$(info $(lastword $(PRODUCT_COPY_FILES)))
823$(info $(dir $(lastword $(MAKEFILE_LIST))))
824$(info $(dir $(lastword $(PRODUCT_COPY_FILES))))
825$(info $(dir $(lastword $(foobar))))
826$(info $(abspath foo/bar))
827$(info $(notdir foo/bar))
Sasha Smundak3deb9682021-07-26 18:42:25 -0700828$(call add_soong_config_namespace,snsconfig)
829$(call add_soong_config_var_value,snsconfig,imagetype,odm_image)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700830$(call soong_config_set, snsconfig, foo, foo_value)
831$(call soong_config_append, snsconfig, bar, bar_value)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700832PRODUCT_COPY_FILES := $(call copy-files,$(wildcard foo*.mk),etc)
Sasha Smundak04453082021-08-17 18:14:41 -0700833PRODUCT_COPY_FILES := $(call product-copy-files-by-pattern,from/%,to/%,a b c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800834`,
835 expected: `load("//build/make/core:product_config.rbc", "rblf")
836
837def init(g, handle):
838 cfg = rblf.cfg(handle)
839 cfg["PRODUCT_COPY_FILES"] = rblf.addprefix("pfx-", "a b c")
840 cfg["PRODUCT_COPY_FILES"] = rblf.addsuffix(".sff", "a b c")
Cole Faust94c4a9a2022-04-22 17:43:52 -0700841 cfg["PRODUCT_NAME"] = rblf.words((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " "))[0]
842 if len(rblf.words(g.get("SOME_UNKNOWN_VARIABLE", ""))) == 1:
843 pass
844 if ("%d" % (len(rblf.words(g.get("SOME_OTHER_VARIABLE", ""))))) == g.get("SOME_INT_VARIABLE", ""):
845 pass
Sasha Smundak35434ed2021-11-05 16:29:56 -0700846 rblf.mkinfo("product.mk", rblf.mkpatsubst("%.pub", "%s%%" % cfg["PRODUCT_NAME"], g.get("PRODUCT_ADB_KEYS", "")))
Cole Faust0e2b2562022-04-01 11:46:50 -0700847 rblf.mkinfo("product.mk", "$(dir foo/bar): %s" % rblf.dir("foo/bar"))
Cole Faust5a13aaf2022-04-27 17:49:35 -0700848 rblf.mkinfo("product.mk", rblf.first_word(cfg["PRODUCT_COPY_FILES"]))
849 rblf.mkinfo("product.mk", rblf.last_word(cfg["PRODUCT_COPY_FILES"]))
850 rblf.mkinfo("product.mk", rblf.dir(rblf.last_word("product.mk")))
851 rblf.mkinfo("product.mk", rblf.dir(rblf.last_word(cfg["PRODUCT_COPY_FILES"])))
852 rblf.mkinfo("product.mk", rblf.dir(rblf.last_word(_foobar)))
Sasha Smundak16e07732021-07-23 11:38:23 -0700853 rblf.mkinfo("product.mk", rblf.abspath("foo/bar"))
854 rblf.mkinfo("product.mk", rblf.notdir("foo/bar"))
Sasha Smundak65b547e2021-09-17 15:35:41 -0700855 rblf.soong_config_namespace(g, "snsconfig")
856 rblf.soong_config_set(g, "snsconfig", "imagetype", "odm_image")
857 rblf.soong_config_set(g, "snsconfig", "foo", "foo_value")
858 rblf.soong_config_append(g, "snsconfig", "bar", "bar_value")
Sasha Smundak3deb9682021-07-26 18:42:25 -0700859 cfg["PRODUCT_COPY_FILES"] = rblf.copy_files(rblf.expand_wildcard("foo*.mk"), "etc")
Sasha Smundak04453082021-08-17 18:14:41 -0700860 cfg["PRODUCT_COPY_FILES"] = rblf.product_copy_files_by_pattern("from/%", "to/%", "a b c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800861`,
862 },
863 {
Sasha Smundak9d011ab2021-07-09 16:00:57 -0700864 desc: "subst in list",
865 mkname: "product.mk",
866 in: `
867files = $(call find-copy-subdir-files,*,from,to)
868PRODUCT_COPY_FILES += $(subst foo,bar,$(files))
869`,
870 expected: `load("//build/make/core:product_config.rbc", "rblf")
871
872def init(g, handle):
873 cfg = rblf.cfg(handle)
874 _files = rblf.find_and_copy("*", "from", "to")
875 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
876 cfg["PRODUCT_COPY_FILES"] += rblf.mksubst("foo", "bar", _files)
877`,
878 },
879 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800880 desc: "assignment flavors",
881 mkname: "product.mk",
882 in: `
883PRODUCT_LIST1 := a
884PRODUCT_LIST2 += a
885PRODUCT_LIST1 += b
886PRODUCT_LIST2 += b
887PRODUCT_LIST3 ?= a
888PRODUCT_LIST1 = c
889PLATFORM_LIST += x
890PRODUCT_PACKAGES := $(PLATFORM_LIST)
891`,
892 expected: `load("//build/make/core:product_config.rbc", "rblf")
893
894def init(g, handle):
895 cfg = rblf.cfg(handle)
896 cfg["PRODUCT_LIST1"] = ["a"]
897 rblf.setdefault(handle, "PRODUCT_LIST2")
898 cfg["PRODUCT_LIST2"] += ["a"]
899 cfg["PRODUCT_LIST1"] += ["b"]
900 cfg["PRODUCT_LIST2"] += ["b"]
901 if cfg.get("PRODUCT_LIST3") == None:
902 cfg["PRODUCT_LIST3"] = ["a"]
903 cfg["PRODUCT_LIST1"] = ["c"]
904 g.setdefault("PLATFORM_LIST", [])
905 g["PLATFORM_LIST"] += ["x"]
906 cfg["PRODUCT_PACKAGES"] = g["PLATFORM_LIST"][:]
907`,
908 },
909 {
910 desc: "assigment flavors2",
911 mkname: "product.mk",
912 in: `
913PRODUCT_LIST1 = a
914ifeq (0,1)
915 PRODUCT_LIST1 += b
916 PRODUCT_LIST2 += b
917endif
918PRODUCT_LIST1 += c
919PRODUCT_LIST2 += c
920`,
921 expected: `load("//build/make/core:product_config.rbc", "rblf")
922
923def init(g, handle):
924 cfg = rblf.cfg(handle)
925 cfg["PRODUCT_LIST1"] = ["a"]
926 if "0" == "1":
927 cfg["PRODUCT_LIST1"] += ["b"]
928 rblf.setdefault(handle, "PRODUCT_LIST2")
929 cfg["PRODUCT_LIST2"] += ["b"]
930 cfg["PRODUCT_LIST1"] += ["c"]
931 rblf.setdefault(handle, "PRODUCT_LIST2")
932 cfg["PRODUCT_LIST2"] += ["c"]
933`,
934 },
935 {
Cole Fauste2a37982022-03-09 16:00:17 -0800936 desc: "assigment setdefaults",
937 mkname: "product.mk",
938 in: `
939# All of these should have a setdefault because they're self-referential and not defined before
940PRODUCT_LIST1 = a $(PRODUCT_LIST1)
941PRODUCT_LIST2 ?= a $(PRODUCT_LIST2)
942PRODUCT_LIST3 += a
943
944# Now doing them again should not have a setdefault because they've already been set
945PRODUCT_LIST1 = a $(PRODUCT_LIST1)
946PRODUCT_LIST2 ?= a $(PRODUCT_LIST2)
947PRODUCT_LIST3 += a
948`,
949 expected: `# All of these should have a setdefault because they're self-referential and not defined before
950load("//build/make/core:product_config.rbc", "rblf")
951
952def init(g, handle):
953 cfg = rblf.cfg(handle)
954 rblf.setdefault(handle, "PRODUCT_LIST1")
955 cfg["PRODUCT_LIST1"] = (["a"] +
956 cfg.get("PRODUCT_LIST1", []))
957 if cfg.get("PRODUCT_LIST2") == None:
958 rblf.setdefault(handle, "PRODUCT_LIST2")
959 cfg["PRODUCT_LIST2"] = (["a"] +
960 cfg.get("PRODUCT_LIST2", []))
961 rblf.setdefault(handle, "PRODUCT_LIST3")
962 cfg["PRODUCT_LIST3"] += ["a"]
963 # Now doing them again should not have a setdefault because they've already been set
964 cfg["PRODUCT_LIST1"] = (["a"] +
965 cfg["PRODUCT_LIST1"])
966 if cfg.get("PRODUCT_LIST2") == None:
967 cfg["PRODUCT_LIST2"] = (["a"] +
968 cfg["PRODUCT_LIST2"])
969 cfg["PRODUCT_LIST3"] += ["a"]
970`,
971 },
972 {
Sasha Smundak3deb9682021-07-26 18:42:25 -0700973 desc: "soong namespace assignments",
974 mkname: "product.mk",
975 in: `
976SOONG_CONFIG_NAMESPACES += cvd
977SOONG_CONFIG_cvd += launch_configs
Sasha Smundak65b547e2021-09-17 15:35:41 -0700978SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
Sasha Smundak3deb9682021-07-26 18:42:25 -0700979SOONG_CONFIG_cvd += grub_config
980SOONG_CONFIG_cvd_grub_config += grub.cfg
Sasha Smundak65b547e2021-09-17 15:35:41 -0700981x := $(SOONG_CONFIG_cvd_grub_config)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700982`,
983 expected: `load("//build/make/core:product_config.rbc", "rblf")
984
985def init(g, handle):
986 cfg = rblf.cfg(handle)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700987 rblf.soong_config_namespace(g, "cvd")
988 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
989 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
Cole Faust1e275862022-04-26 14:28:04 -0700990 _x = 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 -0700991`,
Cole Faustc00184e2021-11-08 12:08:57 -0800992 }, {
993 desc: "soong namespace accesses",
994 mkname: "product.mk",
995 in: `
996SOONG_CONFIG_NAMESPACES += cvd
997SOONG_CONFIG_cvd += launch_configs
998SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
999SOONG_CONFIG_cvd += grub_config
1000SOONG_CONFIG_cvd_grub_config += grub.cfg
1001x := $(call soong_config_get,cvd,grub_config)
1002`,
1003 expected: `load("//build/make/core:product_config.rbc", "rblf")
1004
1005def init(g, handle):
1006 cfg = rblf.cfg(handle)
1007 rblf.soong_config_namespace(g, "cvd")
1008 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
1009 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
1010 _x = rblf.soong_config_get(g, "cvd", "grub_config")
1011`,
Sasha Smundak3deb9682021-07-26 18:42:25 -07001012 },
1013 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001014 desc: "string split",
1015 mkname: "product.mk",
1016 in: `
1017PRODUCT_LIST1 = a
1018local = b
1019local += c
1020FOO = d
1021FOO += e
1022PRODUCT_LIST1 += $(local)
1023PRODUCT_LIST1 += $(FOO)
1024`,
1025 expected: `load("//build/make/core:product_config.rbc", "rblf")
1026
1027def init(g, handle):
1028 cfg = rblf.cfg(handle)
1029 cfg["PRODUCT_LIST1"] = ["a"]
1030 _local = "b"
1031 _local += " " + "c"
1032 g["FOO"] = "d"
1033 g["FOO"] += " " + "e"
1034 cfg["PRODUCT_LIST1"] += (_local).split()
1035 cfg["PRODUCT_LIST1"] += (g["FOO"]).split()
1036`,
1037 },
1038 {
1039 desc: "apex_jars",
1040 mkname: "product.mk",
1041 in: `
1042PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex
1043`,
1044 expected: `load("//build/make/core:product_config.rbc", "rblf")
1045
1046def init(g, handle):
1047 cfg = rblf.cfg(handle)
1048 cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) +
1049 ["framework-minus-apex"])
1050`,
1051 },
1052 {
Cole Faust95b95cb2022-04-05 16:37:39 -07001053 desc: "strip/sort functions",
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001054 mkname: "product.mk",
1055 in: `
1056ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),)
1057 PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress)
1058endif
Cole Faust95b95cb2022-04-05 16:37:39 -07001059MY_VAR := $(sort b a c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001060`,
1061 expected: `load("//build/make/core:product_config.rbc", "rblf")
1062
1063def init(g, handle):
1064 cfg = rblf.cfg(handle)
1065 if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []):
Cole Faust816e0802022-03-04 12:04:31 -08001066 rblf.setdefault(handle, "PRODUCT_PACKAGES")
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001067 cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split()
Cole Faust95b95cb2022-04-05 16:37:39 -07001068 g["MY_VAR"] = rblf.mksort("b a c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001069`,
1070 },
1071 {
1072 desc: "strip func in condition",
1073 mkname: "product.mk",
1074 in: `
1075ifneq ($(strip $(TARGET_VENDOR)),)
1076endif
1077`,
1078 expected: `load("//build/make/core:product_config.rbc", "rblf")
1079
1080def init(g, handle):
1081 cfg = rblf.cfg(handle)
Sasha Smundak0554d762021-07-08 18:26:12 -07001082 if rblf.mkstrip(g.get("TARGET_VENDOR", "")):
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001083 pass
1084`,
1085 },
1086 {
1087 desc: "ref after set",
1088 mkname: "product.mk",
1089 in: `
1090PRODUCT_ADB_KEYS:=value
1091FOO := $(PRODUCT_ADB_KEYS)
1092ifneq (,$(PRODUCT_ADB_KEYS))
1093endif
1094`,
1095 expected: `load("//build/make/core:product_config.rbc", "rblf")
1096
1097def init(g, handle):
1098 cfg = rblf.cfg(handle)
1099 g["PRODUCT_ADB_KEYS"] = "value"
1100 g["FOO"] = g["PRODUCT_ADB_KEYS"]
1101 if g["PRODUCT_ADB_KEYS"]:
1102 pass
1103`,
1104 },
1105 {
1106 desc: "ref before set",
1107 mkname: "product.mk",
1108 in: `
1109V1 := $(PRODUCT_ADB_KEYS)
1110ifeq (,$(PRODUCT_ADB_KEYS))
1111 V2 := $(PRODUCT_ADB_KEYS)
1112 PRODUCT_ADB_KEYS:=foo
1113 V3 := $(PRODUCT_ADB_KEYS)
1114endif`,
1115 expected: `load("//build/make/core:product_config.rbc", "rblf")
1116
1117def init(g, handle):
1118 cfg = rblf.cfg(handle)
1119 g["V1"] = g.get("PRODUCT_ADB_KEYS", "")
1120 if not g.get("PRODUCT_ADB_KEYS", ""):
1121 g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
1122 g["PRODUCT_ADB_KEYS"] = "foo"
1123 g["V3"] = g["PRODUCT_ADB_KEYS"]
1124`,
1125 },
Sasha Smundak6609ba72021-07-22 18:32:56 -07001126 {
1127 desc: "Dynamic inherit path",
1128 mkname: "product.mk",
1129 in: `
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001130MY_PATH:=foo
Sasha Smundak6609ba72021-07-22 18:32:56 -07001131$(call inherit-product,vendor/$(MY_PATH)/cfg.mk)
1132`,
1133 expected: `load("//build/make/core:product_config.rbc", "rblf")
1134load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1135load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init")
1136
1137def init(g, handle):
1138 cfg = rblf.cfg(handle)
1139 g["MY_PATH"] = "foo"
1140 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001141 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1142 "vendor/bar/baz/cfg.mk": ("vendor/bar/baz/cfg", _cfg1_init),
Sasha Smundak6609ba72021-07-22 18:32:56 -07001143 }.get("vendor/%s/cfg.mk" % g["MY_PATH"])
1144 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1145 if not _varmod_init:
Cole Faust7321b092021-12-21 16:11:16 -08001146 rblf.mkerror("product.mk", "Cannot find %s" % ("vendor/%s/cfg.mk" % g["MY_PATH"]))
Sasha Smundak6609ba72021-07-22 18:32:56 -07001147 rblf.inherit(handle, _varmod, _varmod_init)
1148`,
1149 },
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001150 {
1151 desc: "Dynamic inherit with hint",
1152 mkname: "product.mk",
1153 in: `
1154MY_PATH:=foo
1155#RBC# include_top vendor/foo1
1156$(call inherit-product,$(MY_PATH)/cfg.mk)
Cole Faust9df1d732022-04-26 16:27:22 -07001157#RBC# include_top vendor/foo1
1158$(call inherit-product,$(MY_OTHER_PATH))
1159#RBC# include_top vendor/foo1
Cole Faust74ac0272022-06-14 12:45:26 -07001160$(call inherit-product,vendor/$(MY_OTHER_PATH))
1161#RBC# include_top vendor/foo1
Cole Faust9df1d732022-04-26 16:27:22 -07001162$(foreach f,$(MY_MAKEFILES), \
1163 $(call inherit-product,$(f)))
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001164`,
1165 expected: `load("//build/make/core:product_config.rbc", "rblf")
1166load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1167
1168def init(g, handle):
1169 cfg = rblf.cfg(handle)
1170 g["MY_PATH"] = "foo"
Cole Faust93f8d392022-03-02 13:31:30 -08001171 _entry = {
1172 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1173 }.get("%s/cfg.mk" % g["MY_PATH"])
1174 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1175 if not _varmod_init:
1176 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1177 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust9df1d732022-04-26 16:27:22 -07001178 _entry = {
1179 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1180 }.get(g.get("MY_OTHER_PATH", ""))
1181 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1182 if not _varmod_init:
1183 rblf.mkerror("product.mk", "Cannot find %s" % (g.get("MY_OTHER_PATH", "")))
1184 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust74ac0272022-06-14 12:45:26 -07001185 _entry = {
1186 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1187 }.get("vendor/%s" % g.get("MY_OTHER_PATH", ""))
1188 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1189 if not _varmod_init:
1190 rblf.mkerror("product.mk", "Cannot find %s" % ("vendor/%s" % g.get("MY_OTHER_PATH", "")))
1191 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust9df1d732022-04-26 16:27:22 -07001192 for f in rblf.words(g.get("MY_MAKEFILES", "")):
1193 _entry = {
1194 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1195 }.get(f)
1196 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1197 if not _varmod_init:
1198 rblf.mkerror("product.mk", "Cannot find %s" % (f))
1199 rblf.inherit(handle, _varmod, _varmod_init)
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001200`,
1201 },
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001202 {
Cole Faustf7ed5342021-12-21 14:15:12 -08001203 desc: "Dynamic inherit with duplicated hint",
1204 mkname: "product.mk",
1205 in: `
1206MY_PATH:=foo
1207#RBC# include_top vendor/foo1
1208$(call inherit-product,$(MY_PATH)/cfg.mk)
1209#RBC# include_top vendor/foo1
Cole Faust7940c6a2022-01-31 15:54:05 -08001210#RBC# include_top vendor/foo1
Cole Faustf7ed5342021-12-21 14:15:12 -08001211$(call inherit-product,$(MY_PATH)/cfg.mk)
1212`,
1213 expected: `load("//build/make/core:product_config.rbc", "rblf")
1214load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1215
1216def init(g, handle):
1217 cfg = rblf.cfg(handle)
1218 g["MY_PATH"] = "foo"
Cole Faust93f8d392022-03-02 13:31:30 -08001219 _entry = {
1220 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1221 }.get("%s/cfg.mk" % g["MY_PATH"])
1222 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1223 if not _varmod_init:
1224 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1225 rblf.inherit(handle, _varmod, _varmod_init)
1226 _entry = {
1227 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1228 }.get("%s/cfg.mk" % g["MY_PATH"])
1229 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1230 if not _varmod_init:
1231 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1232 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faustf7ed5342021-12-21 14:15:12 -08001233`,
1234 },
1235 {
Cole Faust069aba62022-01-26 17:47:33 -08001236 desc: "Dynamic inherit path that lacks hint",
Cole Faust6c934f62022-01-06 15:51:12 -08001237 mkname: "product.mk",
1238 in: `
1239#RBC# include_top foo
1240$(call inherit-product,$(MY_VAR)/font.mk)
1241
1242#RBC# include_top foo
1243
1244# There's some space and even this comment between the include_top and the inherit-product
1245
1246$(call inherit-product,$(MY_VAR)/font.mk)
1247
1248$(call inherit-product,$(MY_VAR)/font.mk)
1249`,
Cole Faust7940c6a2022-01-31 15:54:05 -08001250 expected: `load("//build/make/core:product_config.rbc", "rblf")
Cole Faust6c934f62022-01-06 15:51:12 -08001251load("//foo:font.star|init", _font_init = "init")
Cole Faust069aba62022-01-26 17:47:33 -08001252load("//bar:font.star|init", _font1_init = "init")
Cole Faust6c934f62022-01-06 15:51:12 -08001253
1254def init(g, handle):
1255 cfg = rblf.cfg(handle)
Cole Faust93f8d392022-03-02 13:31:30 -08001256 _entry = {
1257 "foo/font.mk": ("foo/font", _font_init),
1258 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1259 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1260 if not _varmod_init:
1261 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1262 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001263 # There's some space and even this comment between the include_top and the inherit-product
Cole Faust93f8d392022-03-02 13:31:30 -08001264 _entry = {
1265 "foo/font.mk": ("foo/font", _font_init),
1266 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1267 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1268 if not _varmod_init:
1269 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1270 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faustf4e72cf2022-02-08 12:49:37 -08001271 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 -08001272 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001273 "foo/font.mk": ("foo/font", _font_init),
Cole Faust069aba62022-01-26 17:47:33 -08001274 "bar/font.mk": ("bar/font", _font1_init),
Cole Faust6c934f62022-01-06 15:51:12 -08001275 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1276 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1277 if not _varmod_init:
1278 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1279 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001280`,
1281 },
1282 {
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001283 desc: "Ignore make rules",
1284 mkname: "product.mk",
1285 in: `
Cole Faust00afd4f2022-04-26 14:01:56 -07001286foo: PRIVATE_VARIABLE = some_tool $< $@
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001287foo: foo.c
1288 gcc -o $@ $*`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001289 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001290
1291def init(g, handle):
1292 cfg = rblf.cfg(handle)
Cole Faust00afd4f2022-04-26 14:01:56 -07001293 rblf.mk2rbc_error("product.mk:2", "Only simple variables are handled")
1294 rblf.mk2rbc_error("product.mk:3", "unsupported line rule: foo: foo.c\n#gcc -o $@ $*")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001295`,
1296 },
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001297 {
1298 desc: "Flag override",
1299 mkname: "product.mk",
1300 in: `
1301override FOO:=`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001302 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001303
1304def init(g, handle):
1305 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001306 rblf.mk2rbc_error("product.mk:2", "cannot handle override directive")
Sasha Smundak422b6142021-11-11 18:31:59 -08001307`,
1308 },
1309 {
1310 desc: "Bad expression",
1311 mkname: "build/product.mk",
1312 in: `
1313ifeq (,$(call foobar))
1314endif
Cole Faust1e275862022-04-26 14:28:04 -07001315my_sources := $(local-generated-sources-dir)
Sasha Smundak422b6142021-11-11 18:31:59 -08001316`,
1317 expected: `load("//build/make/core:product_config.rbc", "rblf")
1318
1319def init(g, handle):
1320 cfg = rblf.cfg(handle)
1321 if rblf.mk2rbc_error("build/product.mk:2", "cannot handle invoking foobar"):
1322 pass
Cole Faust1e275862022-04-26 14:28:04 -07001323 _my_sources = rblf.mk2rbc_error("build/product.mk:4", "local-generated-sources-dir is not supported")
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001324`,
1325 },
Cole Faust4eadba72021-12-07 11:54:52 -08001326 {
1327 desc: "if expression",
1328 mkname: "product.mk",
1329 in: `
1330TEST_VAR := foo
1331TEST_VAR_LIST := foo
1332TEST_VAR_LIST += bar
1333TEST_VAR_2 := $(if $(TEST_VAR),bar)
1334TEST_VAR_3 := $(if $(TEST_VAR),bar,baz)
Cole Faust421a1922022-03-16 14:35:45 -07001335TEST_VAR_4 := $(if $(TEST_VAR),$(TEST_VAR_LIST))
Cole Faust4eadba72021-12-07 11:54:52 -08001336`,
1337 expected: `load("//build/make/core:product_config.rbc", "rblf")
1338
1339def init(g, handle):
1340 cfg = rblf.cfg(handle)
1341 g["TEST_VAR"] = "foo"
1342 g["TEST_VAR_LIST"] = ["foo"]
1343 g["TEST_VAR_LIST"] += ["bar"]
1344 g["TEST_VAR_2"] = ("bar" if g["TEST_VAR"] else "")
1345 g["TEST_VAR_3"] = ("bar" if g["TEST_VAR"] else "baz")
Cole Faust421a1922022-03-16 14:35:45 -07001346 g["TEST_VAR_4"] = (g["TEST_VAR_LIST"] if g["TEST_VAR"] else [])
Cole Faust4eadba72021-12-07 11:54:52 -08001347`,
1348 },
Cole Faustc36c9622021-12-07 15:20:45 -08001349 {
1350 desc: "substitution references",
1351 mkname: "product.mk",
1352 in: `
1353SOURCES := foo.c bar.c
1354OBJECTS := $(SOURCES:.c=.o)
1355OBJECTS2 := $(SOURCES:%.c=%.o)
1356`,
1357 expected: `load("//build/make/core:product_config.rbc", "rblf")
1358
1359def init(g, handle):
1360 cfg = rblf.cfg(handle)
1361 g["SOURCES"] = "foo.c bar.c"
1362 g["OBJECTS"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1363 g["OBJECTS2"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1364`,
1365 },
Cole Faustb0d32ab2021-12-09 14:00:59 -08001366 {
1367 desc: "foreach expressions",
1368 mkname: "product.mk",
1369 in: `
1370BOOT_KERNEL_MODULES := foo.ko bar.ko
1371BOOT_KERNEL_MODULES_FILTER := $(foreach m,$(BOOT_KERNEL_MODULES),%/$(m))
1372BOOT_KERNEL_MODULES_LIST := foo.ko
1373BOOT_KERNEL_MODULES_LIST += bar.ko
1374BOOT_KERNEL_MODULES_FILTER_2 := $(foreach m,$(BOOT_KERNEL_MODULES_LIST),%/$(m))
Cole Faust72374fc2022-05-05 11:45:04 -07001375NESTED_LISTS := $(foreach m,$(SOME_VAR),$(BOOT_KERNEL_MODULES_LIST))
1376NESTED_LISTS_2 := $(foreach x,$(SOME_VAR),$(foreach y,$(x),prefix$(y)))
Cole Faustb0d32ab2021-12-09 14:00:59 -08001377
Cole Faustb67aa082022-02-28 16:39:59 -08001378FOREACH_WITH_IF := $(foreach module,\
1379 $(BOOT_KERNEL_MODULES_LIST),\
1380 $(if $(filter $(module),foo.ko),,$(error module "$(module)" has an error!)))
Cole Faustf035d402022-03-28 14:02:50 -07001381
1382# Same as above, but not assigning it to a variable allows it to be converted to statements
1383$(foreach module,\
1384 $(BOOT_KERNEL_MODULES_LIST),\
1385 $(if $(filter $(module),foo.ko),,$(error module "$(module)" has an error!)))
Cole Faustb0d32ab2021-12-09 14:00:59 -08001386`,
1387 expected: `load("//build/make/core:product_config.rbc", "rblf")
1388
1389def init(g, handle):
1390 cfg = rblf.cfg(handle)
1391 g["BOOT_KERNEL_MODULES"] = "foo.ko bar.ko"
1392 g["BOOT_KERNEL_MODULES_FILTER"] = ["%%/%s" % m for m in rblf.words(g["BOOT_KERNEL_MODULES"])]
1393 g["BOOT_KERNEL_MODULES_LIST"] = ["foo.ko"]
1394 g["BOOT_KERNEL_MODULES_LIST"] += ["bar.ko"]
1395 g["BOOT_KERNEL_MODULES_FILTER_2"] = ["%%/%s" % m for m in g["BOOT_KERNEL_MODULES_LIST"]]
Cole Faust72374fc2022-05-05 11:45:04 -07001396 g["NESTED_LISTS"] = rblf.flatten_2d_list([g["BOOT_KERNEL_MODULES_LIST"] for m in rblf.words(g.get("SOME_VAR", ""))])
1397 g["NESTED_LISTS_2"] = rblf.flatten_2d_list([["prefix%s" % y for y in rblf.words(x)] for x in rblf.words(g.get("SOME_VAR", ""))])
Cole Faustb67aa082022-02-28 16:39:59 -08001398 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 -07001399 # Same as above, but not assigning it to a variable allows it to be converted to statements
1400 for module in g["BOOT_KERNEL_MODULES_LIST"]:
1401 if not rblf.filter(module, "foo.ko"):
1402 rblf.mkerror("product.mk", "module \"%s\" has an error!" % module)
Cole Faustb0d32ab2021-12-09 14:00:59 -08001403`,
1404 },
Cole Faust0484c232021-12-22 14:08:08 -08001405 {
1406 desc: "List appended to string",
1407 mkname: "product.mk",
1408 in: `
1409NATIVE_BRIDGE_PRODUCT_PACKAGES := \
1410 libnative_bridge_vdso.native_bridge \
1411 native_bridge_guest_app_process.native_bridge \
1412 native_bridge_guest_linker.native_bridge
1413
1414NATIVE_BRIDGE_MODIFIED_GUEST_LIBS := \
1415 libaaudio \
1416 libamidi \
1417 libandroid \
1418 libandroid_runtime
1419
1420NATIVE_BRIDGE_PRODUCT_PACKAGES += \
1421 $(addsuffix .native_bridge,$(NATIVE_BRIDGE_ORIG_GUEST_LIBS))
1422`,
1423 expected: `load("//build/make/core:product_config.rbc", "rblf")
1424
1425def init(g, handle):
1426 cfg = rblf.cfg(handle)
1427 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] = "libnative_bridge_vdso.native_bridge native_bridge_guest_app_process.native_bridge native_bridge_guest_linker.native_bridge"
1428 g["NATIVE_BRIDGE_MODIFIED_GUEST_LIBS"] = "libaaudio libamidi libandroid libandroid_runtime"
1429 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] += " " + " ".join(rblf.addsuffix(".native_bridge", g.get("NATIVE_BRIDGE_ORIG_GUEST_LIBS", "")))
1430`,
1431 },
Cole Faustb1103e22022-01-06 15:22:05 -08001432 {
1433 desc: "Math functions",
1434 mkname: "product.mk",
1435 in: `
1436# Test the math functions defined in build/make/common/math.mk
1437ifeq ($(call math_max,2,5),5)
1438endif
1439ifeq ($(call math_min,2,5),2)
1440endif
1441ifeq ($(call math_gt_or_eq,2,5),true)
1442endif
1443ifeq ($(call math_gt,2,5),true)
1444endif
1445ifeq ($(call math_lt,2,5),true)
1446endif
1447ifeq ($(call math_gt_or_eq,2,5),)
1448endif
1449ifeq ($(call math_gt,2,5),)
1450endif
1451ifeq ($(call math_lt,2,5),)
1452endif
1453ifeq ($(call math_gt_or_eq,$(MY_VAR), 5),true)
1454endif
1455ifeq ($(call math_gt_or_eq,$(MY_VAR),$(MY_OTHER_VAR)),true)
1456endif
1457ifeq ($(call math_gt_or_eq,100$(MY_VAR),10),true)
1458endif
1459`,
1460 expected: `# Test the math functions defined in build/make/common/math.mk
1461load("//build/make/core:product_config.rbc", "rblf")
1462
1463def init(g, handle):
1464 cfg = rblf.cfg(handle)
1465 if max(2, 5) == 5:
1466 pass
1467 if min(2, 5) == 2:
1468 pass
1469 if 2 >= 5:
1470 pass
1471 if 2 > 5:
1472 pass
1473 if 2 < 5:
1474 pass
1475 if 2 < 5:
1476 pass
1477 if 2 <= 5:
1478 pass
1479 if 2 >= 5:
1480 pass
1481 if int(g.get("MY_VAR", "")) >= 5:
1482 pass
1483 if int(g.get("MY_VAR", "")) >= int(g.get("MY_OTHER_VAR", "")):
1484 pass
1485 if int("100%s" % g.get("MY_VAR", "")) >= 10:
1486 pass
1487`,
1488 },
Cole Faustf92c9f22022-03-14 14:35:50 -07001489 {
1490 desc: "Type hints",
1491 mkname: "product.mk",
1492 in: `
1493# Test type hints
1494#RBC# type_hint list MY_VAR MY_VAR_2
1495# Unsupported type
1496#RBC# type_hint bool MY_VAR_3
1497# Invalid syntax
1498#RBC# type_hint list
1499# Duplicated variable
1500#RBC# type_hint list MY_VAR_2
1501#RBC# type_hint list my-local-var-with-dashes
Cole Faust421a1922022-03-16 14:35:45 -07001502#RBC# type_hint string MY_STRING_VAR
Cole Faustf92c9f22022-03-14 14:35:50 -07001503
1504MY_VAR := foo
1505MY_VAR_UNHINTED := foo
1506
1507# Vars set after other statements still get the hint
1508MY_VAR_2 := foo
1509
1510# You can't specify a type hint after the first statement
1511#RBC# type_hint list MY_VAR_4
1512MY_VAR_4 := foo
1513
1514my-local-var-with-dashes := foo
Cole Faust421a1922022-03-16 14:35:45 -07001515
1516MY_STRING_VAR := $(wildcard foo/bar.mk)
Cole Faustf92c9f22022-03-14 14:35:50 -07001517`,
1518 expected: `# Test type hints
1519# Unsupported type
1520load("//build/make/core:product_config.rbc", "rblf")
1521
1522def init(g, handle):
1523 cfg = rblf.cfg(handle)
1524 rblf.mk2rbc_error("product.mk:5", "Invalid type_hint annotation. Only list/string types are accepted, found bool")
1525 # Invalid syntax
1526 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")
1527 # Duplicated variable
1528 rblf.mk2rbc_error("product.mk:9", "Duplicate type hint for variable MY_VAR_2")
1529 g["MY_VAR"] = ["foo"]
1530 g["MY_VAR_UNHINTED"] = "foo"
1531 # Vars set after other statements still get the hint
1532 g["MY_VAR_2"] = ["foo"]
1533 # You can't specify a type hint after the first statement
Cole Faust421a1922022-03-16 14:35:45 -07001534 rblf.mk2rbc_error("product.mk:20", "type_hint annotations must come before the first Makefile statement")
Cole Faustf92c9f22022-03-14 14:35:50 -07001535 g["MY_VAR_4"] = "foo"
1536 _my_local_var_with_dashes = ["foo"]
Cole Faust421a1922022-03-16 14:35:45 -07001537 g["MY_STRING_VAR"] = " ".join(rblf.expand_wildcard("foo/bar.mk"))
Cole Faustf92c9f22022-03-14 14:35:50 -07001538`,
1539 },
Cole Faustf5adedc2022-03-18 14:05:06 -07001540 {
1541 desc: "Set LOCAL_PATH to my-dir",
1542 mkname: "product.mk",
1543 in: `
1544LOCAL_PATH := $(call my-dir)
1545`,
1546 expected: `load("//build/make/core:product_config.rbc", "rblf")
1547
1548def init(g, handle):
1549 cfg = rblf.cfg(handle)
1550
1551`,
1552 },
Cole Faustf035d402022-03-28 14:02:50 -07001553 {
1554 desc: "Evals",
1555 mkname: "product.mk",
1556 in: `
1557$(eval)
1558$(eval MY_VAR := foo)
1559$(eval # This is a test of eval functions)
1560$(eval $(TOO_COMPLICATED) := bar)
Cole Faust20052982022-04-22 14:43:55 -07001561$(eval include foo/font.mk)
1562$(eval $(call inherit-product,vendor/foo1/cfg.mk))
1563
Cole Faustf035d402022-03-28 14:02:50 -07001564$(foreach x,$(MY_LIST_VAR), \
1565 $(eval PRODUCT_COPY_FILES += foo/bar/$(x):$(TARGET_COPY_OUT_VENDOR)/etc/$(x)) \
Cole Faust20052982022-04-22 14:43:55 -07001566 $(if $(MY_OTHER_VAR),$(eval PRODUCT_COPY_FILES += $(MY_OTHER_VAR):foo/bar/$(x))))
Cole Faustf035d402022-03-28 14:02:50 -07001567
Cole Faust20052982022-04-22 14:43:55 -07001568$(foreach x,$(MY_LIST_VAR), \
1569 $(eval include foo/$(x).mk))
Cole Faust73660422023-01-05 11:07:47 -08001570
1571# Check that we get as least close to correct line numbers for errors on statements inside evals
1572$(eval $(call inherit-product,$(A_VAR)))
Cole Faustf035d402022-03-28 14:02:50 -07001573`,
1574 expected: `load("//build/make/core:product_config.rbc", "rblf")
Cole Faust20052982022-04-22 14:43:55 -07001575load("//foo:font.star", _font_init = "init")
1576load("//vendor/foo1:cfg.star", _cfg_init = "init")
Cole Faustf035d402022-03-28 14:02:50 -07001577
1578def init(g, handle):
1579 cfg = rblf.cfg(handle)
1580 g["MY_VAR"] = "foo"
1581 # This is a test of eval functions
Cole Faust20052982022-04-22 14:43:55 -07001582 rblf.mk2rbc_error("product.mk:5", "Eval expression too complex; only assignments, comments, includes, and inherit-products are supported")
1583 _font_init(g, handle)
1584 rblf.inherit(handle, "vendor/foo1/cfg", _cfg_init)
Cole Faustf035d402022-03-28 14:02:50 -07001585 for x in rblf.words(g.get("MY_LIST_VAR", "")):
1586 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
1587 cfg["PRODUCT_COPY_FILES"] += ("foo/bar/%s:%s/etc/%s" % (x, g.get("TARGET_COPY_OUT_VENDOR", ""), x)).split()
1588 if g.get("MY_OTHER_VAR", ""):
1589 cfg["PRODUCT_COPY_FILES"] += ("%s:foo/bar/%s" % (g.get("MY_OTHER_VAR", ""), x)).split()
Cole Faust20052982022-04-22 14:43:55 -07001590 for x in rblf.words(g.get("MY_LIST_VAR", "")):
1591 _entry = {
1592 "foo/font.mk": ("foo/font", _font_init),
Cole Faust72374fc2022-05-05 11:45:04 -07001593 }.get("foo/%s.mk" % x)
Cole Faust20052982022-04-22 14:43:55 -07001594 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1595 if not _varmod_init:
Cole Faust72374fc2022-05-05 11:45:04 -07001596 rblf.mkerror("product.mk", "Cannot find %s" % ("foo/%s.mk" % x))
Cole Faust20052982022-04-22 14:43:55 -07001597 _varmod_init(g, handle)
Cole Faust73660422023-01-05 11:07:47 -08001598 # Check that we get as least close to correct line numbers for errors on statements inside evals
1599 rblf.mk2rbc_error("product.mk:17", "inherit-product/include argument is too complex")
Cole Faustf035d402022-03-28 14:02:50 -07001600`,
1601 },
Cole Faust5d5fcc32022-04-26 18:02:05 -07001602 {
1603 desc: ".KATI_READONLY",
1604 mkname: "product.mk",
1605 in: `
1606MY_VAR := foo
1607.KATI_READONLY := MY_VAR
1608`,
1609 expected: `load("//build/make/core:product_config.rbc", "rblf")
1610
1611def init(g, handle):
1612 cfg = rblf.cfg(handle)
1613 g["MY_VAR"] = "foo"
1614`,
1615 },
Cole Faust13238772022-04-28 14:29:57 -07001616 {
1617 desc: "Complicated variable references",
1618 mkname: "product.mk",
1619 in: `
1620MY_VAR := foo
1621MY_VAR_2 := MY_VAR
1622MY_VAR_3 := $($(MY_VAR_2))
1623MY_VAR_4 := $(foo bar)
1624MY_VAR_5 := $($(MY_VAR_2) bar)
1625`,
1626 expected: `load("//build/make/core:product_config.rbc", "rblf")
1627
1628def init(g, handle):
1629 cfg = rblf.cfg(handle)
1630 g["MY_VAR"] = "foo"
1631 g["MY_VAR_2"] = "MY_VAR"
1632 g["MY_VAR_3"] = (cfg).get(g["MY_VAR_2"], (g).get(g["MY_VAR_2"], ""))
1633 g["MY_VAR_4"] = rblf.mk2rbc_error("product.mk:5", "cannot handle invoking foo")
1634 g["MY_VAR_5"] = rblf.mk2rbc_error("product.mk:6", "reference is too complex: $(MY_VAR_2) bar")
1635`,
1636 },
Cole Faustd2daabf2022-12-12 17:38:01 -08001637 {
1638 desc: "Conditional functions",
1639 mkname: "product.mk",
1640 in: `
1641B := foo
1642X := $(or $(A))
1643X := $(or $(A),$(B))
1644X := $(or $(A),$(B),$(C))
1645X := $(and $(A))
1646X := $(and $(A),$(B))
1647X := $(and $(A),$(B),$(C))
1648X := $(or $(A),$(B)) Y
1649
1650D := $(wildcard *.mk)
1651X := $(or $(B),$(D))
1652`,
1653 expected: `load("//build/make/core:product_config.rbc", "rblf")
1654
1655def init(g, handle):
1656 cfg = rblf.cfg(handle)
1657 g["B"] = "foo"
1658 g["X"] = g.get("A", "")
1659 g["X"] = g.get("A", "") or g["B"]
1660 g["X"] = g.get("A", "") or g["B"] or g.get("C", "")
1661 g["X"] = g.get("A", "")
1662 g["X"] = g.get("A", "") and g["B"]
1663 g["X"] = g.get("A", "") and g["B"] and g.get("C", "")
1664 g["X"] = "%s Y" % g.get("A", "") or g["B"]
1665 g["D"] = rblf.expand_wildcard("*.mk")
1666 g["X"] = rblf.mk2rbc_error("product.mk:12", "Expected all arguments to $(or) or $(and) to have the same type, found \"string\" and \"list\"")
1667`,
1668 },
Cole Faust2dee63d2022-12-12 18:11:00 -08001669 {
1670
1671 desc: "is-lower/is-upper",
1672 mkname: "product.mk",
1673 in: `
1674X := $(call to-lower,aBc)
1675X := $(call to-upper,aBc)
1676X := $(call to-lower,$(VAR))
1677X := $(call to-upper,$(VAR))
1678`,
1679 expected: `load("//build/make/core:product_config.rbc", "rblf")
1680
1681def init(g, handle):
1682 cfg = rblf.cfg(handle)
1683 g["X"] = ("aBc").lower()
1684 g["X"] = ("aBc").upper()
1685 g["X"] = (g.get("VAR", "")).lower()
1686 g["X"] = (g.get("VAR", "")).upper()
1687`,
1688 },
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001689}
1690
1691var known_variables = []struct {
1692 name string
1693 class varClass
1694 starlarkType
1695}{
Cole Faustf1f44d32021-11-16 14:52:12 -08001696 {"NATIVE_COVERAGE", VarClassSoong, starlarkTypeBool},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001697 {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
1698 {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
1699 {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
1700 {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
1701 {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
1702 {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
1703 {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
1704 {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
1705 {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
1706 {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
1707 {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
1708 {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
1709 {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
1710 {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
1711}
1712
Sasha Smundak6609ba72021-07-22 18:32:56 -07001713type testMakefileFinder struct {
1714 fs fs.FS
1715 root string
1716 files []string
1717}
1718
1719func (t *testMakefileFinder) Find(root string) []string {
1720 if t.files != nil || root == t.root {
1721 return t.files
1722 }
1723 t.files = make([]string, 0)
1724 fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error {
1725 if err != nil {
1726 return err
1727 }
1728 if d.IsDir() {
1729 base := filepath.Base(path)
1730 if base[0] == '.' && len(base) > 1 {
1731 return fs.SkipDir
1732 }
1733 return nil
1734 }
1735 if strings.HasSuffix(path, ".mk") {
1736 t.files = append(t.files, path)
1737 }
1738 return nil
1739 })
1740 return t.files
1741}
1742
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001743func TestGood(t *testing.T) {
1744 for _, v := range known_variables {
1745 KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
1746 }
Sasha Smundak6609ba72021-07-22 18:32:56 -07001747 fs := NewFindMockFS([]string{
1748 "vendor/foo1/cfg.mk",
1749 "vendor/bar/baz/cfg.mk",
1750 "part.mk",
1751 "foo/font.mk",
1752 "bar/font.mk",
1753 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001754 for _, test := range testCases {
1755 t.Run(test.desc,
1756 func(t *testing.T) {
1757 ss, err := Convert(Request{
Sasha Smundak422b6142021-11-11 18:31:59 -08001758 MkFile: test.mkname,
1759 Reader: bytes.NewBufferString(test.in),
Sasha Smundak422b6142021-11-11 18:31:59 -08001760 OutputSuffix: ".star",
1761 SourceFS: fs,
1762 MakefileFinder: &testMakefileFinder{fs: fs},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001763 })
1764 if err != nil {
1765 t.Error(err)
1766 return
1767 }
1768 got := ss.String()
1769 if got != test.expected {
1770 t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
1771 strings.ReplaceAll(test.expected, "\n", "␤\n"),
1772 strings.ReplaceAll(got, "\n", "␤\n"))
1773 }
1774 })
1775 }
1776}