blob: de7512927a79b7f91d4d6da13616308c8dc78741 [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)))
Sasha Smundak35434ed2021-11-05 16:29:56 -0700811$(info $(patsubst %.pub,$(PRODUCT_NAME)%,$(PRODUCT_ADB_KEYS)))
Cole Faust0e2b2562022-04-01 11:46:50 -0700812$(info $$(dir foo/bar): $(dir foo/bar))
Sasha Smundak16e07732021-07-23 11:38:23 -0700813$(info $(firstword $(PRODUCT_COPY_FILES)))
814$(info $(lastword $(PRODUCT_COPY_FILES)))
815$(info $(dir $(lastword $(MAKEFILE_LIST))))
816$(info $(dir $(lastword $(PRODUCT_COPY_FILES))))
817$(info $(dir $(lastword $(foobar))))
818$(info $(abspath foo/bar))
819$(info $(notdir foo/bar))
Sasha Smundak3deb9682021-07-26 18:42:25 -0700820$(call add_soong_config_namespace,snsconfig)
821$(call add_soong_config_var_value,snsconfig,imagetype,odm_image)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700822$(call soong_config_set, snsconfig, foo, foo_value)
823$(call soong_config_append, snsconfig, bar, bar_value)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700824PRODUCT_COPY_FILES := $(call copy-files,$(wildcard foo*.mk),etc)
Sasha Smundak04453082021-08-17 18:14:41 -0700825PRODUCT_COPY_FILES := $(call product-copy-files-by-pattern,from/%,to/%,a b c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800826`,
827 expected: `load("//build/make/core:product_config.rbc", "rblf")
828
829def init(g, handle):
830 cfg = rblf.cfg(handle)
831 cfg["PRODUCT_COPY_FILES"] = rblf.addprefix("pfx-", "a b c")
832 cfg["PRODUCT_COPY_FILES"] = rblf.addsuffix(".sff", "a b c")
833 cfg["PRODUCT_NAME"] = ((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " ")).split()[0]
Sasha Smundak35434ed2021-11-05 16:29:56 -0700834 rblf.mkinfo("product.mk", rblf.mkpatsubst("%.pub", "%s%%" % cfg["PRODUCT_NAME"], g.get("PRODUCT_ADB_KEYS", "")))
Cole Faust0e2b2562022-04-01 11:46:50 -0700835 rblf.mkinfo("product.mk", "$(dir foo/bar): %s" % rblf.dir("foo/bar"))
Sasha Smundak16e07732021-07-23 11:38:23 -0700836 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][0])
837 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][-1])
838 rblf.mkinfo("product.mk", rblf.dir("product.mk"))
839 rblf.mkinfo("product.mk", rblf.dir(cfg["PRODUCT_COPY_FILES"][-1]))
840 rblf.mkinfo("product.mk", rblf.dir((_foobar).split()[-1]))
841 rblf.mkinfo("product.mk", rblf.abspath("foo/bar"))
842 rblf.mkinfo("product.mk", rblf.notdir("foo/bar"))
Sasha Smundak65b547e2021-09-17 15:35:41 -0700843 rblf.soong_config_namespace(g, "snsconfig")
844 rblf.soong_config_set(g, "snsconfig", "imagetype", "odm_image")
845 rblf.soong_config_set(g, "snsconfig", "foo", "foo_value")
846 rblf.soong_config_append(g, "snsconfig", "bar", "bar_value")
Sasha Smundak3deb9682021-07-26 18:42:25 -0700847 cfg["PRODUCT_COPY_FILES"] = rblf.copy_files(rblf.expand_wildcard("foo*.mk"), "etc")
Sasha Smundak04453082021-08-17 18:14:41 -0700848 cfg["PRODUCT_COPY_FILES"] = rblf.product_copy_files_by_pattern("from/%", "to/%", "a b c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800849`,
850 },
851 {
Sasha Smundak9d011ab2021-07-09 16:00:57 -0700852 desc: "subst in list",
853 mkname: "product.mk",
854 in: `
855files = $(call find-copy-subdir-files,*,from,to)
856PRODUCT_COPY_FILES += $(subst foo,bar,$(files))
857`,
858 expected: `load("//build/make/core:product_config.rbc", "rblf")
859
860def init(g, handle):
861 cfg = rblf.cfg(handle)
862 _files = rblf.find_and_copy("*", "from", "to")
863 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
864 cfg["PRODUCT_COPY_FILES"] += rblf.mksubst("foo", "bar", _files)
865`,
866 },
867 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800868 desc: "assignment flavors",
869 mkname: "product.mk",
870 in: `
871PRODUCT_LIST1 := a
872PRODUCT_LIST2 += a
873PRODUCT_LIST1 += b
874PRODUCT_LIST2 += b
875PRODUCT_LIST3 ?= a
876PRODUCT_LIST1 = c
877PLATFORM_LIST += x
878PRODUCT_PACKAGES := $(PLATFORM_LIST)
879`,
880 expected: `load("//build/make/core:product_config.rbc", "rblf")
881
882def init(g, handle):
883 cfg = rblf.cfg(handle)
884 cfg["PRODUCT_LIST1"] = ["a"]
885 rblf.setdefault(handle, "PRODUCT_LIST2")
886 cfg["PRODUCT_LIST2"] += ["a"]
887 cfg["PRODUCT_LIST1"] += ["b"]
888 cfg["PRODUCT_LIST2"] += ["b"]
889 if cfg.get("PRODUCT_LIST3") == None:
890 cfg["PRODUCT_LIST3"] = ["a"]
891 cfg["PRODUCT_LIST1"] = ["c"]
892 g.setdefault("PLATFORM_LIST", [])
893 g["PLATFORM_LIST"] += ["x"]
894 cfg["PRODUCT_PACKAGES"] = g["PLATFORM_LIST"][:]
895`,
896 },
897 {
898 desc: "assigment flavors2",
899 mkname: "product.mk",
900 in: `
901PRODUCT_LIST1 = a
902ifeq (0,1)
903 PRODUCT_LIST1 += b
904 PRODUCT_LIST2 += b
905endif
906PRODUCT_LIST1 += c
907PRODUCT_LIST2 += c
908`,
909 expected: `load("//build/make/core:product_config.rbc", "rblf")
910
911def init(g, handle):
912 cfg = rblf.cfg(handle)
913 cfg["PRODUCT_LIST1"] = ["a"]
914 if "0" == "1":
915 cfg["PRODUCT_LIST1"] += ["b"]
916 rblf.setdefault(handle, "PRODUCT_LIST2")
917 cfg["PRODUCT_LIST2"] += ["b"]
918 cfg["PRODUCT_LIST1"] += ["c"]
919 rblf.setdefault(handle, "PRODUCT_LIST2")
920 cfg["PRODUCT_LIST2"] += ["c"]
921`,
922 },
923 {
Cole Fauste2a37982022-03-09 16:00:17 -0800924 desc: "assigment setdefaults",
925 mkname: "product.mk",
926 in: `
927# All of these should have a setdefault because they're self-referential and not defined before
928PRODUCT_LIST1 = a $(PRODUCT_LIST1)
929PRODUCT_LIST2 ?= a $(PRODUCT_LIST2)
930PRODUCT_LIST3 += a
931
932# Now doing them again should not have a setdefault because they've already been set
933PRODUCT_LIST1 = a $(PRODUCT_LIST1)
934PRODUCT_LIST2 ?= a $(PRODUCT_LIST2)
935PRODUCT_LIST3 += a
936`,
937 expected: `# All of these should have a setdefault because they're self-referential and not defined before
938load("//build/make/core:product_config.rbc", "rblf")
939
940def init(g, handle):
941 cfg = rblf.cfg(handle)
942 rblf.setdefault(handle, "PRODUCT_LIST1")
943 cfg["PRODUCT_LIST1"] = (["a"] +
944 cfg.get("PRODUCT_LIST1", []))
945 if cfg.get("PRODUCT_LIST2") == None:
946 rblf.setdefault(handle, "PRODUCT_LIST2")
947 cfg["PRODUCT_LIST2"] = (["a"] +
948 cfg.get("PRODUCT_LIST2", []))
949 rblf.setdefault(handle, "PRODUCT_LIST3")
950 cfg["PRODUCT_LIST3"] += ["a"]
951 # Now doing them again should not have a setdefault because they've already been set
952 cfg["PRODUCT_LIST1"] = (["a"] +
953 cfg["PRODUCT_LIST1"])
954 if cfg.get("PRODUCT_LIST2") == None:
955 cfg["PRODUCT_LIST2"] = (["a"] +
956 cfg["PRODUCT_LIST2"])
957 cfg["PRODUCT_LIST3"] += ["a"]
958`,
959 },
960 {
Sasha Smundak3deb9682021-07-26 18:42:25 -0700961 desc: "soong namespace assignments",
962 mkname: "product.mk",
963 in: `
964SOONG_CONFIG_NAMESPACES += cvd
965SOONG_CONFIG_cvd += launch_configs
Sasha Smundak65b547e2021-09-17 15:35:41 -0700966SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
Sasha Smundak3deb9682021-07-26 18:42:25 -0700967SOONG_CONFIG_cvd += grub_config
968SOONG_CONFIG_cvd_grub_config += grub.cfg
Sasha Smundak65b547e2021-09-17 15:35:41 -0700969x := $(SOONG_CONFIG_cvd_grub_config)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700970`,
971 expected: `load("//build/make/core:product_config.rbc", "rblf")
972
973def init(g, handle):
974 cfg = rblf.cfg(handle)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700975 rblf.soong_config_namespace(g, "cvd")
976 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
977 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
Sasha Smundak422b6142021-11-11 18:31:59 -0800978 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 -0700979`,
Cole Faustc00184e2021-11-08 12:08:57 -0800980 }, {
981 desc: "soong namespace accesses",
982 mkname: "product.mk",
983 in: `
984SOONG_CONFIG_NAMESPACES += cvd
985SOONG_CONFIG_cvd += launch_configs
986SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
987SOONG_CONFIG_cvd += grub_config
988SOONG_CONFIG_cvd_grub_config += grub.cfg
989x := $(call soong_config_get,cvd,grub_config)
990`,
991 expected: `load("//build/make/core:product_config.rbc", "rblf")
992
993def init(g, handle):
994 cfg = rblf.cfg(handle)
995 rblf.soong_config_namespace(g, "cvd")
996 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
997 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
998 _x = rblf.soong_config_get(g, "cvd", "grub_config")
999`,
Sasha Smundak3deb9682021-07-26 18:42:25 -07001000 },
1001 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001002 desc: "string split",
1003 mkname: "product.mk",
1004 in: `
1005PRODUCT_LIST1 = a
1006local = b
1007local += c
1008FOO = d
1009FOO += e
1010PRODUCT_LIST1 += $(local)
1011PRODUCT_LIST1 += $(FOO)
1012`,
1013 expected: `load("//build/make/core:product_config.rbc", "rblf")
1014
1015def init(g, handle):
1016 cfg = rblf.cfg(handle)
1017 cfg["PRODUCT_LIST1"] = ["a"]
1018 _local = "b"
1019 _local += " " + "c"
1020 g["FOO"] = "d"
1021 g["FOO"] += " " + "e"
1022 cfg["PRODUCT_LIST1"] += (_local).split()
1023 cfg["PRODUCT_LIST1"] += (g["FOO"]).split()
1024`,
1025 },
1026 {
1027 desc: "apex_jars",
1028 mkname: "product.mk",
1029 in: `
1030PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex
1031`,
1032 expected: `load("//build/make/core:product_config.rbc", "rblf")
1033
1034def init(g, handle):
1035 cfg = rblf.cfg(handle)
1036 cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) +
1037 ["framework-minus-apex"])
1038`,
1039 },
1040 {
Cole Faust95b95cb2022-04-05 16:37:39 -07001041 desc: "strip/sort functions",
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001042 mkname: "product.mk",
1043 in: `
1044ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),)
1045 PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress)
1046endif
Cole Faust95b95cb2022-04-05 16:37:39 -07001047MY_VAR := $(sort b a c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001048`,
1049 expected: `load("//build/make/core:product_config.rbc", "rblf")
1050
1051def init(g, handle):
1052 cfg = rblf.cfg(handle)
1053 if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []):
Cole Faust816e0802022-03-04 12:04:31 -08001054 rblf.setdefault(handle, "PRODUCT_PACKAGES")
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001055 cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split()
Cole Faust95b95cb2022-04-05 16:37:39 -07001056 g["MY_VAR"] = rblf.mksort("b a c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001057`,
1058 },
1059 {
1060 desc: "strip func in condition",
1061 mkname: "product.mk",
1062 in: `
1063ifneq ($(strip $(TARGET_VENDOR)),)
1064endif
1065`,
1066 expected: `load("//build/make/core:product_config.rbc", "rblf")
1067
1068def init(g, handle):
1069 cfg = rblf.cfg(handle)
Sasha Smundak0554d762021-07-08 18:26:12 -07001070 if rblf.mkstrip(g.get("TARGET_VENDOR", "")):
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001071 pass
1072`,
1073 },
1074 {
1075 desc: "ref after set",
1076 mkname: "product.mk",
1077 in: `
1078PRODUCT_ADB_KEYS:=value
1079FOO := $(PRODUCT_ADB_KEYS)
1080ifneq (,$(PRODUCT_ADB_KEYS))
1081endif
1082`,
1083 expected: `load("//build/make/core:product_config.rbc", "rblf")
1084
1085def init(g, handle):
1086 cfg = rblf.cfg(handle)
1087 g["PRODUCT_ADB_KEYS"] = "value"
1088 g["FOO"] = g["PRODUCT_ADB_KEYS"]
1089 if g["PRODUCT_ADB_KEYS"]:
1090 pass
1091`,
1092 },
1093 {
1094 desc: "ref before set",
1095 mkname: "product.mk",
1096 in: `
1097V1 := $(PRODUCT_ADB_KEYS)
1098ifeq (,$(PRODUCT_ADB_KEYS))
1099 V2 := $(PRODUCT_ADB_KEYS)
1100 PRODUCT_ADB_KEYS:=foo
1101 V3 := $(PRODUCT_ADB_KEYS)
1102endif`,
1103 expected: `load("//build/make/core:product_config.rbc", "rblf")
1104
1105def init(g, handle):
1106 cfg = rblf.cfg(handle)
1107 g["V1"] = g.get("PRODUCT_ADB_KEYS", "")
1108 if not g.get("PRODUCT_ADB_KEYS", ""):
1109 g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
1110 g["PRODUCT_ADB_KEYS"] = "foo"
1111 g["V3"] = g["PRODUCT_ADB_KEYS"]
1112`,
1113 },
Sasha Smundak6609ba72021-07-22 18:32:56 -07001114 {
1115 desc: "Dynamic inherit path",
1116 mkname: "product.mk",
1117 in: `
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001118MY_PATH:=foo
Sasha Smundak6609ba72021-07-22 18:32:56 -07001119$(call inherit-product,vendor/$(MY_PATH)/cfg.mk)
1120`,
1121 expected: `load("//build/make/core:product_config.rbc", "rblf")
1122load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1123load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init")
1124
1125def init(g, handle):
1126 cfg = rblf.cfg(handle)
1127 g["MY_PATH"] = "foo"
1128 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001129 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1130 "vendor/bar/baz/cfg.mk": ("vendor/bar/baz/cfg", _cfg1_init),
Sasha Smundak6609ba72021-07-22 18:32:56 -07001131 }.get("vendor/%s/cfg.mk" % g["MY_PATH"])
1132 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1133 if not _varmod_init:
Cole Faust7321b092021-12-21 16:11:16 -08001134 rblf.mkerror("product.mk", "Cannot find %s" % ("vendor/%s/cfg.mk" % g["MY_PATH"]))
Sasha Smundak6609ba72021-07-22 18:32:56 -07001135 rblf.inherit(handle, _varmod, _varmod_init)
1136`,
1137 },
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001138 {
1139 desc: "Dynamic inherit with hint",
1140 mkname: "product.mk",
1141 in: `
1142MY_PATH:=foo
1143#RBC# include_top vendor/foo1
1144$(call inherit-product,$(MY_PATH)/cfg.mk)
1145`,
1146 expected: `load("//build/make/core:product_config.rbc", "rblf")
1147load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1148
1149def init(g, handle):
1150 cfg = rblf.cfg(handle)
1151 g["MY_PATH"] = "foo"
Cole Faust93f8d392022-03-02 13:31:30 -08001152 _entry = {
1153 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1154 }.get("%s/cfg.mk" % g["MY_PATH"])
1155 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1156 if not _varmod_init:
1157 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1158 rblf.inherit(handle, _varmod, _varmod_init)
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001159`,
1160 },
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001161 {
Cole Faustf7ed5342021-12-21 14:15:12 -08001162 desc: "Dynamic inherit with duplicated hint",
1163 mkname: "product.mk",
1164 in: `
1165MY_PATH:=foo
1166#RBC# include_top vendor/foo1
1167$(call inherit-product,$(MY_PATH)/cfg.mk)
1168#RBC# include_top vendor/foo1
Cole Faust7940c6a2022-01-31 15:54:05 -08001169#RBC# include_top vendor/foo1
Cole Faustf7ed5342021-12-21 14:15:12 -08001170$(call inherit-product,$(MY_PATH)/cfg.mk)
1171`,
1172 expected: `load("//build/make/core:product_config.rbc", "rblf")
1173load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1174
1175def init(g, handle):
1176 cfg = rblf.cfg(handle)
1177 g["MY_PATH"] = "foo"
Cole Faust93f8d392022-03-02 13:31:30 -08001178 _entry = {
1179 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1180 }.get("%s/cfg.mk" % g["MY_PATH"])
1181 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1182 if not _varmod_init:
1183 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1184 rblf.inherit(handle, _varmod, _varmod_init)
1185 _entry = {
1186 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1187 }.get("%s/cfg.mk" % g["MY_PATH"])
1188 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1189 if not _varmod_init:
1190 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1191 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faustf7ed5342021-12-21 14:15:12 -08001192`,
1193 },
1194 {
Cole Faust069aba62022-01-26 17:47:33 -08001195 desc: "Dynamic inherit path that lacks hint",
Cole Faust6c934f62022-01-06 15:51:12 -08001196 mkname: "product.mk",
1197 in: `
1198#RBC# include_top foo
1199$(call inherit-product,$(MY_VAR)/font.mk)
1200
1201#RBC# include_top foo
1202
1203# There's some space and even this comment between the include_top and the inherit-product
1204
1205$(call inherit-product,$(MY_VAR)/font.mk)
1206
1207$(call inherit-product,$(MY_VAR)/font.mk)
1208`,
Cole Faust7940c6a2022-01-31 15:54:05 -08001209 expected: `load("//build/make/core:product_config.rbc", "rblf")
Cole Faust6c934f62022-01-06 15:51:12 -08001210load("//foo:font.star|init", _font_init = "init")
Cole Faust069aba62022-01-26 17:47:33 -08001211load("//bar:font.star|init", _font1_init = "init")
Cole Faust6c934f62022-01-06 15:51:12 -08001212
1213def init(g, handle):
1214 cfg = rblf.cfg(handle)
Cole Faust93f8d392022-03-02 13:31:30 -08001215 _entry = {
1216 "foo/font.mk": ("foo/font", _font_init),
1217 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1218 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1219 if not _varmod_init:
1220 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1221 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001222 # There's some space and even this comment between the include_top and the inherit-product
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 Faustf4e72cf2022-02-08 12:49:37 -08001230 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 -08001231 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001232 "foo/font.mk": ("foo/font", _font_init),
Cole Faust069aba62022-01-26 17:47:33 -08001233 "bar/font.mk": ("bar/font", _font1_init),
Cole Faust6c934f62022-01-06 15:51:12 -08001234 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1235 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1236 if not _varmod_init:
1237 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1238 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001239`,
1240 },
1241 {
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001242 desc: "Ignore make rules",
1243 mkname: "product.mk",
1244 in: `
1245foo: foo.c
1246 gcc -o $@ $*`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001247 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001248
1249def init(g, handle):
1250 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001251 rblf.mk2rbc_error("product.mk:2", "unsupported line rule: foo: foo.c\n#gcc -o $@ $*")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001252`,
1253 },
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001254 {
1255 desc: "Flag override",
1256 mkname: "product.mk",
1257 in: `
1258override FOO:=`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001259 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001260
1261def init(g, handle):
1262 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001263 rblf.mk2rbc_error("product.mk:2", "cannot handle override directive")
Sasha Smundak422b6142021-11-11 18:31:59 -08001264`,
1265 },
1266 {
1267 desc: "Bad expression",
1268 mkname: "build/product.mk",
1269 in: `
1270ifeq (,$(call foobar))
1271endif
1272`,
1273 expected: `load("//build/make/core:product_config.rbc", "rblf")
1274
1275def init(g, handle):
1276 cfg = rblf.cfg(handle)
1277 if rblf.mk2rbc_error("build/product.mk:2", "cannot handle invoking foobar"):
1278 pass
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001279`,
1280 },
Cole Faust4eadba72021-12-07 11:54:52 -08001281 {
1282 desc: "if expression",
1283 mkname: "product.mk",
1284 in: `
1285TEST_VAR := foo
1286TEST_VAR_LIST := foo
1287TEST_VAR_LIST += bar
1288TEST_VAR_2 := $(if $(TEST_VAR),bar)
1289TEST_VAR_3 := $(if $(TEST_VAR),bar,baz)
Cole Faust421a1922022-03-16 14:35:45 -07001290TEST_VAR_4 := $(if $(TEST_VAR),$(TEST_VAR_LIST))
Cole Faust4eadba72021-12-07 11:54:52 -08001291`,
1292 expected: `load("//build/make/core:product_config.rbc", "rblf")
1293
1294def init(g, handle):
1295 cfg = rblf.cfg(handle)
1296 g["TEST_VAR"] = "foo"
1297 g["TEST_VAR_LIST"] = ["foo"]
1298 g["TEST_VAR_LIST"] += ["bar"]
1299 g["TEST_VAR_2"] = ("bar" if g["TEST_VAR"] else "")
1300 g["TEST_VAR_3"] = ("bar" if g["TEST_VAR"] else "baz")
Cole Faust421a1922022-03-16 14:35:45 -07001301 g["TEST_VAR_4"] = (g["TEST_VAR_LIST"] if g["TEST_VAR"] else [])
Cole Faust4eadba72021-12-07 11:54:52 -08001302`,
1303 },
Cole Faustc36c9622021-12-07 15:20:45 -08001304 {
1305 desc: "substitution references",
1306 mkname: "product.mk",
1307 in: `
1308SOURCES := foo.c bar.c
1309OBJECTS := $(SOURCES:.c=.o)
1310OBJECTS2 := $(SOURCES:%.c=%.o)
1311`,
1312 expected: `load("//build/make/core:product_config.rbc", "rblf")
1313
1314def init(g, handle):
1315 cfg = rblf.cfg(handle)
1316 g["SOURCES"] = "foo.c bar.c"
1317 g["OBJECTS"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1318 g["OBJECTS2"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1319`,
1320 },
Cole Faustb0d32ab2021-12-09 14:00:59 -08001321 {
1322 desc: "foreach expressions",
1323 mkname: "product.mk",
1324 in: `
1325BOOT_KERNEL_MODULES := foo.ko bar.ko
1326BOOT_KERNEL_MODULES_FILTER := $(foreach m,$(BOOT_KERNEL_MODULES),%/$(m))
1327BOOT_KERNEL_MODULES_LIST := foo.ko
1328BOOT_KERNEL_MODULES_LIST += bar.ko
1329BOOT_KERNEL_MODULES_FILTER_2 := $(foreach m,$(BOOT_KERNEL_MODULES_LIST),%/$(m))
1330
Cole Faustb67aa082022-02-28 16:39:59 -08001331FOREACH_WITH_IF := $(foreach module,\
1332 $(BOOT_KERNEL_MODULES_LIST),\
1333 $(if $(filter $(module),foo.ko),,$(error module "$(module)" has an error!)))
Cole Faustf035d402022-03-28 14:02:50 -07001334
1335# Same as above, but not assigning it to a variable allows it to be converted to statements
1336$(foreach module,\
1337 $(BOOT_KERNEL_MODULES_LIST),\
1338 $(if $(filter $(module),foo.ko),,$(error module "$(module)" has an error!)))
Cole Faustb0d32ab2021-12-09 14:00:59 -08001339`,
1340 expected: `load("//build/make/core:product_config.rbc", "rblf")
1341
1342def init(g, handle):
1343 cfg = rblf.cfg(handle)
1344 g["BOOT_KERNEL_MODULES"] = "foo.ko bar.ko"
1345 g["BOOT_KERNEL_MODULES_FILTER"] = ["%%/%s" % m for m in rblf.words(g["BOOT_KERNEL_MODULES"])]
1346 g["BOOT_KERNEL_MODULES_LIST"] = ["foo.ko"]
1347 g["BOOT_KERNEL_MODULES_LIST"] += ["bar.ko"]
1348 g["BOOT_KERNEL_MODULES_FILTER_2"] = ["%%/%s" % m for m in g["BOOT_KERNEL_MODULES_LIST"]]
Cole Faustb67aa082022-02-28 16:39:59 -08001349 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 -07001350 # Same as above, but not assigning it to a variable allows it to be converted to statements
1351 for module in g["BOOT_KERNEL_MODULES_LIST"]:
1352 if not rblf.filter(module, "foo.ko"):
1353 rblf.mkerror("product.mk", "module \"%s\" has an error!" % module)
Cole Faustb0d32ab2021-12-09 14:00:59 -08001354`,
1355 },
Cole Faust0484c232021-12-22 14:08:08 -08001356 {
1357 desc: "List appended to string",
1358 mkname: "product.mk",
1359 in: `
1360NATIVE_BRIDGE_PRODUCT_PACKAGES := \
1361 libnative_bridge_vdso.native_bridge \
1362 native_bridge_guest_app_process.native_bridge \
1363 native_bridge_guest_linker.native_bridge
1364
1365NATIVE_BRIDGE_MODIFIED_GUEST_LIBS := \
1366 libaaudio \
1367 libamidi \
1368 libandroid \
1369 libandroid_runtime
1370
1371NATIVE_BRIDGE_PRODUCT_PACKAGES += \
1372 $(addsuffix .native_bridge,$(NATIVE_BRIDGE_ORIG_GUEST_LIBS))
1373`,
1374 expected: `load("//build/make/core:product_config.rbc", "rblf")
1375
1376def init(g, handle):
1377 cfg = rblf.cfg(handle)
1378 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] = "libnative_bridge_vdso.native_bridge native_bridge_guest_app_process.native_bridge native_bridge_guest_linker.native_bridge"
1379 g["NATIVE_BRIDGE_MODIFIED_GUEST_LIBS"] = "libaaudio libamidi libandroid libandroid_runtime"
1380 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] += " " + " ".join(rblf.addsuffix(".native_bridge", g.get("NATIVE_BRIDGE_ORIG_GUEST_LIBS", "")))
1381`,
1382 },
Cole Faustb1103e22022-01-06 15:22:05 -08001383 {
1384 desc: "Math functions",
1385 mkname: "product.mk",
1386 in: `
1387# Test the math functions defined in build/make/common/math.mk
1388ifeq ($(call math_max,2,5),5)
1389endif
1390ifeq ($(call math_min,2,5),2)
1391endif
1392ifeq ($(call math_gt_or_eq,2,5),true)
1393endif
1394ifeq ($(call math_gt,2,5),true)
1395endif
1396ifeq ($(call math_lt,2,5),true)
1397endif
1398ifeq ($(call math_gt_or_eq,2,5),)
1399endif
1400ifeq ($(call math_gt,2,5),)
1401endif
1402ifeq ($(call math_lt,2,5),)
1403endif
1404ifeq ($(call math_gt_or_eq,$(MY_VAR), 5),true)
1405endif
1406ifeq ($(call math_gt_or_eq,$(MY_VAR),$(MY_OTHER_VAR)),true)
1407endif
1408ifeq ($(call math_gt_or_eq,100$(MY_VAR),10),true)
1409endif
1410`,
1411 expected: `# Test the math functions defined in build/make/common/math.mk
1412load("//build/make/core:product_config.rbc", "rblf")
1413
1414def init(g, handle):
1415 cfg = rblf.cfg(handle)
1416 if max(2, 5) == 5:
1417 pass
1418 if min(2, 5) == 2:
1419 pass
1420 if 2 >= 5:
1421 pass
1422 if 2 > 5:
1423 pass
1424 if 2 < 5:
1425 pass
1426 if 2 < 5:
1427 pass
1428 if 2 <= 5:
1429 pass
1430 if 2 >= 5:
1431 pass
1432 if int(g.get("MY_VAR", "")) >= 5:
1433 pass
1434 if int(g.get("MY_VAR", "")) >= int(g.get("MY_OTHER_VAR", "")):
1435 pass
1436 if int("100%s" % g.get("MY_VAR", "")) >= 10:
1437 pass
1438`,
1439 },
Cole Faustf92c9f22022-03-14 14:35:50 -07001440 {
1441 desc: "Type hints",
1442 mkname: "product.mk",
1443 in: `
1444# Test type hints
1445#RBC# type_hint list MY_VAR MY_VAR_2
1446# Unsupported type
1447#RBC# type_hint bool MY_VAR_3
1448# Invalid syntax
1449#RBC# type_hint list
1450# Duplicated variable
1451#RBC# type_hint list MY_VAR_2
1452#RBC# type_hint list my-local-var-with-dashes
Cole Faust421a1922022-03-16 14:35:45 -07001453#RBC# type_hint string MY_STRING_VAR
Cole Faustf92c9f22022-03-14 14:35:50 -07001454
1455MY_VAR := foo
1456MY_VAR_UNHINTED := foo
1457
1458# Vars set after other statements still get the hint
1459MY_VAR_2 := foo
1460
1461# You can't specify a type hint after the first statement
1462#RBC# type_hint list MY_VAR_4
1463MY_VAR_4 := foo
1464
1465my-local-var-with-dashes := foo
Cole Faust421a1922022-03-16 14:35:45 -07001466
1467MY_STRING_VAR := $(wildcard foo/bar.mk)
Cole Faustf92c9f22022-03-14 14:35:50 -07001468`,
1469 expected: `# Test type hints
1470# Unsupported type
1471load("//build/make/core:product_config.rbc", "rblf")
1472
1473def init(g, handle):
1474 cfg = rblf.cfg(handle)
1475 rblf.mk2rbc_error("product.mk:5", "Invalid type_hint annotation. Only list/string types are accepted, found bool")
1476 # Invalid syntax
1477 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")
1478 # Duplicated variable
1479 rblf.mk2rbc_error("product.mk:9", "Duplicate type hint for variable MY_VAR_2")
1480 g["MY_VAR"] = ["foo"]
1481 g["MY_VAR_UNHINTED"] = "foo"
1482 # Vars set after other statements still get the hint
1483 g["MY_VAR_2"] = ["foo"]
1484 # You can't specify a type hint after the first statement
Cole Faust421a1922022-03-16 14:35:45 -07001485 rblf.mk2rbc_error("product.mk:20", "type_hint annotations must come before the first Makefile statement")
Cole Faustf92c9f22022-03-14 14:35:50 -07001486 g["MY_VAR_4"] = "foo"
1487 _my_local_var_with_dashes = ["foo"]
Cole Faust421a1922022-03-16 14:35:45 -07001488 g["MY_STRING_VAR"] = " ".join(rblf.expand_wildcard("foo/bar.mk"))
Cole Faustf92c9f22022-03-14 14:35:50 -07001489`,
1490 },
Cole Faustf5adedc2022-03-18 14:05:06 -07001491 {
1492 desc: "Set LOCAL_PATH to my-dir",
1493 mkname: "product.mk",
1494 in: `
1495LOCAL_PATH := $(call my-dir)
1496`,
1497 expected: `load("//build/make/core:product_config.rbc", "rblf")
1498
1499def init(g, handle):
1500 cfg = rblf.cfg(handle)
1501
1502`,
1503 },
Cole Faustf035d402022-03-28 14:02:50 -07001504 {
1505 desc: "Evals",
1506 mkname: "product.mk",
1507 in: `
1508$(eval)
1509$(eval MY_VAR := foo)
1510$(eval # This is a test of eval functions)
1511$(eval $(TOO_COMPLICATED) := bar)
1512$(foreach x,$(MY_LIST_VAR), \
1513 $(eval PRODUCT_COPY_FILES += foo/bar/$(x):$(TARGET_COPY_OUT_VENDOR)/etc/$(x)) \
1514 $(if $(MY_OTHER_VAR),$(eval PRODUCT_COPY_FILES += $(MY_OTHER_VAR):foo/bar/$(x))) \
1515)
1516
1517`,
1518 expected: `load("//build/make/core:product_config.rbc", "rblf")
1519
1520def init(g, handle):
1521 cfg = rblf.cfg(handle)
1522 g["MY_VAR"] = "foo"
1523 # This is a test of eval functions
1524 rblf.mk2rbc_error("product.mk:5", "Eval expression too complex; only assignments and comments are supported")
1525 for x in rblf.words(g.get("MY_LIST_VAR", "")):
1526 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
1527 cfg["PRODUCT_COPY_FILES"] += ("foo/bar/%s:%s/etc/%s" % (x, g.get("TARGET_COPY_OUT_VENDOR", ""), x)).split()
1528 if g.get("MY_OTHER_VAR", ""):
1529 cfg["PRODUCT_COPY_FILES"] += ("%s:foo/bar/%s" % (g.get("MY_OTHER_VAR", ""), x)).split()
1530`,
1531 },
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001532}
1533
1534var known_variables = []struct {
1535 name string
1536 class varClass
1537 starlarkType
1538}{
Cole Faustf1f44d32021-11-16 14:52:12 -08001539 {"NATIVE_COVERAGE", VarClassSoong, starlarkTypeBool},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001540 {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
1541 {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
1542 {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
1543 {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
1544 {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
1545 {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
1546 {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
1547 {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
1548 {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
1549 {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
1550 {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
1551 {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
1552 {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
1553 {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
1554}
1555
Sasha Smundak6609ba72021-07-22 18:32:56 -07001556type testMakefileFinder struct {
1557 fs fs.FS
1558 root string
1559 files []string
1560}
1561
1562func (t *testMakefileFinder) Find(root string) []string {
1563 if t.files != nil || root == t.root {
1564 return t.files
1565 }
1566 t.files = make([]string, 0)
1567 fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error {
1568 if err != nil {
1569 return err
1570 }
1571 if d.IsDir() {
1572 base := filepath.Base(path)
1573 if base[0] == '.' && len(base) > 1 {
1574 return fs.SkipDir
1575 }
1576 return nil
1577 }
1578 if strings.HasSuffix(path, ".mk") {
1579 t.files = append(t.files, path)
1580 }
1581 return nil
1582 })
1583 return t.files
1584}
1585
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001586func TestGood(t *testing.T) {
1587 for _, v := range known_variables {
1588 KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
1589 }
Sasha Smundak6609ba72021-07-22 18:32:56 -07001590 fs := NewFindMockFS([]string{
1591 "vendor/foo1/cfg.mk",
1592 "vendor/bar/baz/cfg.mk",
1593 "part.mk",
1594 "foo/font.mk",
1595 "bar/font.mk",
1596 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001597 for _, test := range testCases {
1598 t.Run(test.desc,
1599 func(t *testing.T) {
1600 ss, err := Convert(Request{
Sasha Smundak422b6142021-11-11 18:31:59 -08001601 MkFile: test.mkname,
1602 Reader: bytes.NewBufferString(test.in),
Sasha Smundak422b6142021-11-11 18:31:59 -08001603 OutputSuffix: ".star",
1604 SourceFS: fs,
1605 MakefileFinder: &testMakefileFinder{fs: fs},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001606 })
1607 if err != nil {
1608 t.Error(err)
1609 return
1610 }
1611 got := ss.String()
1612 if got != test.expected {
1613 t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
1614 strings.ReplaceAll(test.expected, "\n", "␤\n"),
1615 strings.ReplaceAll(got, "\n", "␤\n"))
1616 }
1617 })
1618 }
1619}