blob: f94aa18f5a62b1d92fbffdc3ee6e072b71af5159 [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)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800200`,
201 expected: `load("//build/make/core:product_config.rbc", "rblf")
202load("//foo:font.star", _font_init = "init")
203load("//bar:font.star", _font1_init = "init")
204
205def init(g, handle):
206 cfg = rblf.cfg(handle)
207 rblf.inherit(handle, "foo/font", _font_init)
208 rblf.inherit(handle, "bar/font", _font1_init)
209`,
210 },
211 {
212 desc: "Directive define",
213 mkname: "product.mk",
214 in: `
215define some-macro
216 $(info foo)
217endef
218`,
Sasha Smundak422b6142021-11-11 18:31:59 -0800219 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800220
221def init(g, handle):
222 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -0800223 rblf.mk2rbc_error("product.mk:2", "define is not supported: some-macro")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800224`,
225 },
226 {
227 desc: "Ifdef",
228 mkname: "product.mk",
229 in: `
230ifdef PRODUCT_NAME
231 PRODUCT_NAME = gizmo
232else
233endif
Sasha Smundakc4fa93e2021-11-05 14:38:46 -0700234local_var :=
235ifdef local_var
236endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800237`,
238 expected: `load("//build/make/core:product_config.rbc", "rblf")
239
240def init(g, handle):
241 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800242 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800243 cfg["PRODUCT_NAME"] = "gizmo"
244 else:
245 pass
Sasha Smundakc4fa93e2021-11-05 14:38:46 -0700246 _local_var = ""
247 if _local_var:
248 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800249`,
250 },
251 {
252 desc: "Simple functions",
253 mkname: "product.mk",
254 in: `
255$(warning this is the warning)
256$(warning)
Cole Fauste309a912022-03-16 13:42:34 -0700257$(warning # this warning starts with a pound)
258$(warning this warning has a # in the middle)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800259$(info this is the info)
260$(error this is the error)
261PRODUCT_NAME:=$(shell echo *)
262`,
263 expected: `load("//build/make/core:product_config.rbc", "rblf")
264
265def init(g, handle):
266 cfg = rblf.cfg(handle)
267 rblf.mkwarning("product.mk", "this is the warning")
268 rblf.mkwarning("product.mk", "")
Cole Fauste309a912022-03-16 13:42:34 -0700269 rblf.mkwarning("product.mk", "# this warning starts with a pound")
270 rblf.mkwarning("product.mk", "this warning has a # in the middle")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800271 rblf.mkinfo("product.mk", "this is the info")
272 rblf.mkerror("product.mk", "this is the error")
273 cfg["PRODUCT_NAME"] = rblf.shell("echo *")
274`,
275 },
276 {
277 desc: "Empty if",
278 mkname: "product.mk",
279 in: `
280ifdef PRODUCT_NAME
281# Comment
Sasha Smundak6609ba72021-07-22 18:32:56 -0700282else
Sasha Smundak02183cf2021-08-16 13:36:11 -0700283 TARGET_COPY_OUT_RECOVERY := foo
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800284endif
285`,
286 expected: `load("//build/make/core:product_config.rbc", "rblf")
287
288def init(g, handle):
289 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800290 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800291 # Comment
292 pass
Sasha Smundak6609ba72021-07-22 18:32:56 -0700293 else:
Sasha Smundak422b6142021-11-11 18:31:59 -0800294 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 -0800295`,
296 },
297 {
298 desc: "if/else/endif",
299 mkname: "product.mk",
300 in: `
301ifndef PRODUCT_NAME
302 PRODUCT_NAME=gizmo1
303else
304 PRODUCT_NAME=gizmo2
305endif
306`,
307 expected: `load("//build/make/core:product_config.rbc", "rblf")
308
309def init(g, handle):
310 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800311 if not cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800312 cfg["PRODUCT_NAME"] = "gizmo1"
313 else:
314 cfg["PRODUCT_NAME"] = "gizmo2"
315`,
316 },
317 {
318 desc: "else if",
319 mkname: "product.mk",
320 in: `
321ifdef PRODUCT_NAME
322 PRODUCT_NAME = gizmo
323else ifndef PRODUCT_PACKAGES # Comment
324endif
325 `,
326 expected: `load("//build/make/core:product_config.rbc", "rblf")
327
328def init(g, handle):
329 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800330 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800331 cfg["PRODUCT_NAME"] = "gizmo"
Cole Faust71514c02022-01-27 17:21:41 -0800332 elif not cfg.get("PRODUCT_PACKAGES", []):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800333 # Comment
334 pass
335`,
336 },
337 {
338 desc: "ifeq / ifneq",
339 mkname: "product.mk",
340 in: `
341ifeq (aosp_arm, $(TARGET_PRODUCT))
342 PRODUCT_MODEL = pix2
343else
344 PRODUCT_MODEL = pix21
345endif
346ifneq (aosp_x86, $(TARGET_PRODUCT))
347 PRODUCT_MODEL = pix3
348endif
349`,
350 expected: `load("//build/make/core:product_config.rbc", "rblf")
351
352def init(g, handle):
353 cfg = rblf.cfg(handle)
354 if "aosp_arm" == g["TARGET_PRODUCT"]:
355 cfg["PRODUCT_MODEL"] = "pix2"
356 else:
357 cfg["PRODUCT_MODEL"] = "pix21"
358 if "aosp_x86" != g["TARGET_PRODUCT"]:
359 cfg["PRODUCT_MODEL"] = "pix3"
360`,
361 },
362 {
Cole Faustf8320212021-11-10 15:05:07 -0800363 desc: "ifeq with soong_config_get",
364 mkname: "product.mk",
365 in: `
366ifeq (true,$(call soong_config_get,art_module,source_build))
367endif
368`,
369 expected: `load("//build/make/core:product_config.rbc", "rblf")
370
371def init(g, handle):
372 cfg = rblf.cfg(handle)
373 if "true" == rblf.soong_config_get(g, "art_module", "source_build"):
374 pass
375`,
376 },
377 {
Cole Faustf1f44d32021-11-16 14:52:12 -0800378 desc: "ifeq with $(NATIVE_COVERAGE)",
379 mkname: "product.mk",
380 in: `
381ifeq ($(NATIVE_COVERAGE),true)
382endif
383`,
384 expected: `load("//build/make/core:product_config.rbc", "rblf")
385
386def init(g, handle):
387 cfg = rblf.cfg(handle)
388 if g.get("NATIVE_COVERAGE", False):
389 pass
390`,
391 },
392 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800393 desc: "Check filter result",
394 mkname: "product.mk",
395 in: `
396ifeq (,$(filter userdebug eng, $(TARGET_BUILD_VARIANT)))
397endif
398ifneq (,$(filter userdebug,$(TARGET_BUILD_VARIANT))
399endif
400ifneq (,$(filter plaf,$(PLATFORM_LIST)))
401endif
402ifeq ($(TARGET_BUILD_VARIANT), $(filter $(TARGET_BUILD_VARIANT), userdebug eng))
403endif
Cole Faust9932f752022-02-08 11:56:25 -0800404ifneq (, $(filter $(TARGET_BUILD_VARIANT), userdebug eng))
405endif
406ifneq (,$(filter userdebug eng, $(TARGET_BUILD_VARIANT)))
407endif
Sasha Smundak0554d762021-07-08 18:26:12 -0700408ifneq (,$(filter true, $(v1)$(v2)))
409endif
Sasha Smundak5f463be2021-09-15 18:43:36 -0700410ifeq (,$(filter barbet coral%,$(TARGET_PRODUCT)))
411else ifneq (,$(filter barbet%,$(TARGET_PRODUCT)))
412endif
Cole Fausteec0d812021-12-06 16:23:51 -0800413ifeq (,$(filter-out sunfish_kasan, $(TARGET_PRODUCT)))
414endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800415`,
416 expected: `load("//build/make/core:product_config.rbc", "rblf")
417
418def init(g, handle):
419 cfg = rblf.cfg(handle)
Sasha Smundak5f463be2021-09-15 18:43:36 -0700420 if not rblf.filter("userdebug eng", g["TARGET_BUILD_VARIANT"]):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800421 pass
Sasha Smundak5f463be2021-09-15 18:43:36 -0700422 if rblf.filter("userdebug", g["TARGET_BUILD_VARIANT"]):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800423 pass
424 if "plaf" in g.get("PLATFORM_LIST", []):
425 pass
Cole Faust9932f752022-02-08 11:56:25 -0800426 if g["TARGET_BUILD_VARIANT"] == " ".join(rblf.filter(g["TARGET_BUILD_VARIANT"], "userdebug eng")):
427 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800428 if g["TARGET_BUILD_VARIANT"] in ["userdebug", "eng"]:
429 pass
Cole Faust9932f752022-02-08 11:56:25 -0800430 if rblf.filter("userdebug eng", g["TARGET_BUILD_VARIANT"]):
431 pass
Sasha Smundak5f463be2021-09-15 18:43:36 -0700432 if rblf.filter("true", "%s%s" % (_v1, _v2)):
433 pass
434 if not rblf.filter("barbet coral%", g["TARGET_PRODUCT"]):
435 pass
436 elif rblf.filter("barbet%", g["TARGET_PRODUCT"]):
Sasha Smundak0554d762021-07-08 18:26:12 -0700437 pass
Cole Fausteec0d812021-12-06 16:23:51 -0800438 if not rblf.filter_out("sunfish_kasan", g["TARGET_PRODUCT"]):
439 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800440`,
441 },
442 {
443 desc: "Get filter result",
444 mkname: "product.mk",
445 in: `
446PRODUCT_LIST2=$(filter-out %/foo.ko,$(wildcard path/*.ko))
447`,
448 expected: `load("//build/make/core:product_config.rbc", "rblf")
449
450def init(g, handle):
451 cfg = rblf.cfg(handle)
452 cfg["PRODUCT_LIST2"] = rblf.filter_out("%/foo.ko", rblf.expand_wildcard("path/*.ko"))
453`,
454 },
455 {
456 desc: "filter $(VAR), values",
457 mkname: "product.mk",
458 in: `
459ifeq (,$(filter $(TARGET_PRODUCT), yukawa_gms yukawa_sei510_gms)
460 ifneq (,$(filter $(TARGET_PRODUCT), yukawa_gms)
461 endif
462endif
463
464`,
465 expected: `load("//build/make/core:product_config.rbc", "rblf")
466
467def init(g, handle):
468 cfg = rblf.cfg(handle)
469 if g["TARGET_PRODUCT"] not in ["yukawa_gms", "yukawa_sei510_gms"]:
Sasha Smundak0554d762021-07-08 18:26:12 -0700470 if g["TARGET_PRODUCT"] == "yukawa_gms":
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800471 pass
472`,
473 },
474 {
Sasha Smundak0554d762021-07-08 18:26:12 -0700475 desc: "filter $(V1), $(V2)",
476 mkname: "product.mk",
477 in: `
478ifneq (, $(filter $(PRODUCT_LIST), $(TARGET_PRODUCT)))
479endif
480`,
481 expected: `load("//build/make/core:product_config.rbc", "rblf")
482
483def init(g, handle):
484 cfg = rblf.cfg(handle)
Sasha Smundak468e11f2021-08-26 09:10:23 -0700485 if rblf.filter(g.get("PRODUCT_LIST", []), g["TARGET_PRODUCT"]):
Sasha Smundak0554d762021-07-08 18:26:12 -0700486 pass
487`,
488 },
489 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800490 desc: "ifeq",
491 mkname: "product.mk",
492 in: `
493ifeq (aosp, $(TARGET_PRODUCT)) # Comment
494else ifneq (, $(TARGET_PRODUCT))
495endif
496`,
497 expected: `load("//build/make/core:product_config.rbc", "rblf")
498
499def init(g, handle):
500 cfg = rblf.cfg(handle)
501 if "aosp" == g["TARGET_PRODUCT"]:
502 # Comment
503 pass
504 elif g["TARGET_PRODUCT"]:
505 pass
506`,
507 },
508 {
509 desc: "Nested if",
510 mkname: "product.mk",
511 in: `
512ifdef PRODUCT_NAME
513 PRODUCT_PACKAGES = pack-if0
514 ifdef PRODUCT_MODEL
515 PRODUCT_PACKAGES = pack-if-if
516 else ifdef PRODUCT_NAME
517 PRODUCT_PACKAGES = pack-if-elif
518 else
519 PRODUCT_PACKAGES = pack-if-else
520 endif
521 PRODUCT_PACKAGES = pack-if
522else ifneq (,$(TARGET_PRODUCT))
523 PRODUCT_PACKAGES = pack-elif
524else
525 PRODUCT_PACKAGES = pack-else
526endif
527`,
528 expected: `load("//build/make/core:product_config.rbc", "rblf")
529
530def init(g, handle):
531 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800532 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800533 cfg["PRODUCT_PACKAGES"] = ["pack-if0"]
Cole Faust71514c02022-01-27 17:21:41 -0800534 if cfg.get("PRODUCT_MODEL", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800535 cfg["PRODUCT_PACKAGES"] = ["pack-if-if"]
Cole Faust71514c02022-01-27 17:21:41 -0800536 elif cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800537 cfg["PRODUCT_PACKAGES"] = ["pack-if-elif"]
538 else:
539 cfg["PRODUCT_PACKAGES"] = ["pack-if-else"]
540 cfg["PRODUCT_PACKAGES"] = ["pack-if"]
541 elif g["TARGET_PRODUCT"]:
542 cfg["PRODUCT_PACKAGES"] = ["pack-elif"]
543 else:
544 cfg["PRODUCT_PACKAGES"] = ["pack-else"]
545`,
546 },
547 {
548 desc: "Wildcard",
549 mkname: "product.mk",
550 in: `
551ifeq (,$(wildcard foo.mk))
552endif
553ifneq (,$(wildcard foo*.mk))
554endif
555`,
556 expected: `load("//build/make/core:product_config.rbc", "rblf")
557
558def init(g, handle):
559 cfg = rblf.cfg(handle)
560 if not rblf.file_exists("foo.mk"):
561 pass
562 if rblf.file_wildcard_exists("foo*.mk"):
563 pass
564`,
565 },
566 {
Cole Faustf8320212021-11-10 15:05:07 -0800567 desc: "if with interpolation",
568 mkname: "product.mk",
569 in: `
570ifeq ($(VARIABLE1)text$(VARIABLE2),true)
571endif
572`,
573 expected: `load("//build/make/core:product_config.rbc", "rblf")
574
575def init(g, handle):
576 cfg = rblf.cfg(handle)
577 if "%stext%s" % (g.get("VARIABLE1", ""), g.get("VARIABLE2", "")) == "true":
578 pass
579`,
580 },
581 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800582 desc: "ifneq $(X),true",
583 mkname: "product.mk",
584 in: `
585ifneq ($(VARIABLE),true)
586endif
587`,
588 expected: `load("//build/make/core:product_config.rbc", "rblf")
589
590def init(g, handle):
591 cfg = rblf.cfg(handle)
592 if g.get("VARIABLE", "") != "true":
593 pass
594`,
595 },
596 {
597 desc: "Const neq",
598 mkname: "product.mk",
599 in: `
600ifneq (1,0)
601endif
602`,
603 expected: `load("//build/make/core:product_config.rbc", "rblf")
604
605def init(g, handle):
606 cfg = rblf.cfg(handle)
607 if "1" != "0":
608 pass
609`,
610 },
611 {
612 desc: "is-board calls",
613 mkname: "product.mk",
614 in: `
615ifeq ($(call is-board-platform-in-list,msm8998), true)
616else ifneq ($(call is-board-platform,copper),true)
617else ifneq ($(call is-vendor-board-platform,QCOM),true)
618else ifeq ($(call is-product-in-list, $(PLATFORM_LIST)), true)
619endif
620`,
621 expected: `load("//build/make/core:product_config.rbc", "rblf")
622
623def init(g, handle):
624 cfg = rblf.cfg(handle)
Cole Faustb2e0b602022-01-07 15:46:58 -0800625 if rblf.board_platform_in(g, "msm8998"):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800626 pass
Cole Faustb2e0b602022-01-07 15:46:58 -0800627 elif not rblf.board_platform_is(g, "copper"):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800628 pass
629 elif g.get("TARGET_BOARD_PLATFORM", "") not in g["QCOM_BOARD_PLATFORMS"]:
630 pass
631 elif g["TARGET_PRODUCT"] in g.get("PLATFORM_LIST", []):
632 pass
633`,
634 },
635 {
Sasha Smundak3a9b8e82021-08-25 14:11:04 -0700636 desc: "new is-board calls",
637 mkname: "product.mk",
638 in: `
639ifneq (,$(call is-board-platform-in-list2,msm8998 $(X))
640else ifeq (,$(call is-board-platform2,copper)
641else ifneq (,$(call is-vendor-board-qcom))
642endif
643`,
644 expected: `load("//build/make/core:product_config.rbc", "rblf")
645
646def init(g, handle):
647 cfg = rblf.cfg(handle)
648 if rblf.board_platform_in(g, "msm8998 %s" % g.get("X", "")):
649 pass
650 elif not rblf.board_platform_is(g, "copper"):
651 pass
Sasha Smundak4f1f1182021-11-04 17:57:39 -0700652 elif g.get("TARGET_BOARD_PLATFORM", "") in g["QCOM_BOARD_PLATFORMS"]:
Sasha Smundak3a9b8e82021-08-25 14:11:04 -0700653 pass
654`,
655 },
656 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800657 desc: "findstring call",
658 mkname: "product.mk",
659 in: `
Cole Faust0e9418c2021-12-13 16:33:25 -0800660result := $(findstring a,a b c)
661result := $(findstring b,x y z)
662`,
663 expected: `load("//build/make/core:product_config.rbc", "rblf")
664
665def init(g, handle):
666 cfg = rblf.cfg(handle)
667 _result = rblf.findstring("a", "a b c")
668 _result = rblf.findstring("b", "x y z")
669`,
670 },
671 {
672 desc: "findstring in if statement",
673 mkname: "product.mk",
674 in: `
675ifeq ($(findstring foo,$(PRODUCT_PACKAGES)),)
676endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800677ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),)
678endif
Cole Faust0e9418c2021-12-13 16:33:25 -0800679ifeq ($(findstring foo,$(PRODUCT_PACKAGES)),foo)
680endif
681ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),foo)
682endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800683`,
684 expected: `load("//build/make/core:product_config.rbc", "rblf")
685
686def init(g, handle):
687 cfg = rblf.cfg(handle)
Cole Faust0e9418c2021-12-13 16:33:25 -0800688 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") == -1:
689 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800690 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
691 pass
Cole Faust0e9418c2021-12-13 16:33:25 -0800692 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
693 pass
694 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") == -1:
695 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800696`,
697 },
698 {
699 desc: "rhs call",
700 mkname: "product.mk",
701 in: `
702PRODUCT_COPY_FILES = $(call add-to-product-copy-files-if-exists, path:distpath) \
703 $(call find-copy-subdir-files, *, fromdir, todir) $(wildcard foo.*)
704`,
705 expected: `load("//build/make/core:product_config.rbc", "rblf")
706
707def init(g, handle):
708 cfg = rblf.cfg(handle)
709 cfg["PRODUCT_COPY_FILES"] = (rblf.copy_if_exists("path:distpath") +
710 rblf.find_and_copy("*", "fromdir", "todir") +
711 rblf.expand_wildcard("foo.*"))
712`,
713 },
714 {
715 desc: "inferred type",
716 mkname: "product.mk",
717 in: `
718HIKEY_MODS := $(wildcard foo/*.ko)
719BOARD_VENDOR_KERNEL_MODULES += $(HIKEY_MODS)
720`,
721 expected: `load("//build/make/core:product_config.rbc", "rblf")
722
723def init(g, handle):
724 cfg = rblf.cfg(handle)
725 g["HIKEY_MODS"] = rblf.expand_wildcard("foo/*.ko")
726 g.setdefault("BOARD_VENDOR_KERNEL_MODULES", [])
727 g["BOARD_VENDOR_KERNEL_MODULES"] += g["HIKEY_MODS"]
728`,
729 },
730 {
731 desc: "list with vars",
732 mkname: "product.mk",
733 in: `
734PRODUCT_COPY_FILES += path1:$(TARGET_PRODUCT)/path1 $(PRODUCT_MODEL)/path2:$(TARGET_PRODUCT)/path2
735`,
736 expected: `load("//build/make/core:product_config.rbc", "rblf")
737
738def init(g, handle):
739 cfg = rblf.cfg(handle)
740 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
741 cfg["PRODUCT_COPY_FILES"] += (("path1:%s/path1" % g["TARGET_PRODUCT"]).split() +
742 ("%s/path2:%s/path2" % (cfg.get("PRODUCT_MODEL", ""), g["TARGET_PRODUCT"])).split())
743`,
744 },
745 {
746 desc: "misc calls",
747 mkname: "product.mk",
748 in: `
749$(call enforce-product-packages-exist,)
750$(call enforce-product-packages-exist, foo)
751$(call require-artifacts-in-path, foo, bar)
752$(call require-artifacts-in-path-relaxed, foo, bar)
Sasha Smundakd6797852021-11-15 13:01:53 -0800753$(call dist-for-goals, goal, from:to)
Cole Faust1cc08852022-02-28 11:12:08 -0800754$(call add-product-dex-preopt-module-config,MyModule,disable)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800755`,
756 expected: `load("//build/make/core:product_config.rbc", "rblf")
757
758def init(g, handle):
759 cfg = rblf.cfg(handle)
760 rblf.enforce_product_packages_exist("")
761 rblf.enforce_product_packages_exist("foo")
762 rblf.require_artifacts_in_path("foo", "bar")
763 rblf.require_artifacts_in_path_relaxed("foo", "bar")
Sasha Smundakd6797852021-11-15 13:01:53 -0800764 rblf.mkdist_for_goals(g, "goal", "from:to")
Cole Faust1cc08852022-02-28 11:12:08 -0800765 rblf.add_product_dex_preopt_module_config(handle, "MyModule", "disable")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800766`,
767 },
768 {
769 desc: "list with functions",
770 mkname: "product.mk",
771 in: `
772PRODUCT_COPY_FILES := $(call find-copy-subdir-files,*.kl,from1,to1) \
773 $(call find-copy-subdir-files,*.kc,from2,to2) \
774 foo bar
775`,
776 expected: `load("//build/make/core:product_config.rbc", "rblf")
777
778def init(g, handle):
779 cfg = rblf.cfg(handle)
780 cfg["PRODUCT_COPY_FILES"] = (rblf.find_and_copy("*.kl", "from1", "to1") +
781 rblf.find_and_copy("*.kc", "from2", "to2") +
782 [
783 "foo",
784 "bar",
785 ])
786`,
787 },
788 {
789 desc: "Text functions",
790 mkname: "product.mk",
791 in: `
792PRODUCT_COPY_FILES := $(addprefix pfx-,a b c)
793PRODUCT_COPY_FILES := $(addsuffix .sff, a b c)
794PRODUCT_NAME := $(word 1, $(subst ., ,$(TARGET_BOARD_PLATFORM)))
Sasha Smundak35434ed2021-11-05 16:29:56 -0700795$(info $(patsubst %.pub,$(PRODUCT_NAME)%,$(PRODUCT_ADB_KEYS)))
Cole Faust0e2b2562022-04-01 11:46:50 -0700796$(info $$(dir foo/bar): $(dir foo/bar))
Sasha Smundak16e07732021-07-23 11:38:23 -0700797$(info $(firstword $(PRODUCT_COPY_FILES)))
798$(info $(lastword $(PRODUCT_COPY_FILES)))
799$(info $(dir $(lastword $(MAKEFILE_LIST))))
800$(info $(dir $(lastword $(PRODUCT_COPY_FILES))))
801$(info $(dir $(lastword $(foobar))))
802$(info $(abspath foo/bar))
803$(info $(notdir foo/bar))
Sasha Smundak3deb9682021-07-26 18:42:25 -0700804$(call add_soong_config_namespace,snsconfig)
805$(call add_soong_config_var_value,snsconfig,imagetype,odm_image)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700806$(call soong_config_set, snsconfig, foo, foo_value)
807$(call soong_config_append, snsconfig, bar, bar_value)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700808PRODUCT_COPY_FILES := $(call copy-files,$(wildcard foo*.mk),etc)
Sasha Smundak04453082021-08-17 18:14:41 -0700809PRODUCT_COPY_FILES := $(call product-copy-files-by-pattern,from/%,to/%,a b c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800810`,
811 expected: `load("//build/make/core:product_config.rbc", "rblf")
812
813def init(g, handle):
814 cfg = rblf.cfg(handle)
815 cfg["PRODUCT_COPY_FILES"] = rblf.addprefix("pfx-", "a b c")
816 cfg["PRODUCT_COPY_FILES"] = rblf.addsuffix(".sff", "a b c")
817 cfg["PRODUCT_NAME"] = ((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " ")).split()[0]
Sasha Smundak35434ed2021-11-05 16:29:56 -0700818 rblf.mkinfo("product.mk", rblf.mkpatsubst("%.pub", "%s%%" % cfg["PRODUCT_NAME"], g.get("PRODUCT_ADB_KEYS", "")))
Cole Faust0e2b2562022-04-01 11:46:50 -0700819 rblf.mkinfo("product.mk", "$(dir foo/bar): %s" % rblf.dir("foo/bar"))
Sasha Smundak16e07732021-07-23 11:38:23 -0700820 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][0])
821 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][-1])
822 rblf.mkinfo("product.mk", rblf.dir("product.mk"))
823 rblf.mkinfo("product.mk", rblf.dir(cfg["PRODUCT_COPY_FILES"][-1]))
824 rblf.mkinfo("product.mk", rblf.dir((_foobar).split()[-1]))
825 rblf.mkinfo("product.mk", rblf.abspath("foo/bar"))
826 rblf.mkinfo("product.mk", rblf.notdir("foo/bar"))
Sasha Smundak65b547e2021-09-17 15:35:41 -0700827 rblf.soong_config_namespace(g, "snsconfig")
828 rblf.soong_config_set(g, "snsconfig", "imagetype", "odm_image")
829 rblf.soong_config_set(g, "snsconfig", "foo", "foo_value")
830 rblf.soong_config_append(g, "snsconfig", "bar", "bar_value")
Sasha Smundak3deb9682021-07-26 18:42:25 -0700831 cfg["PRODUCT_COPY_FILES"] = rblf.copy_files(rblf.expand_wildcard("foo*.mk"), "etc")
Sasha Smundak04453082021-08-17 18:14:41 -0700832 cfg["PRODUCT_COPY_FILES"] = rblf.product_copy_files_by_pattern("from/%", "to/%", "a b c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800833`,
834 },
835 {
Sasha Smundak9d011ab2021-07-09 16:00:57 -0700836 desc: "subst in list",
837 mkname: "product.mk",
838 in: `
839files = $(call find-copy-subdir-files,*,from,to)
840PRODUCT_COPY_FILES += $(subst foo,bar,$(files))
841`,
842 expected: `load("//build/make/core:product_config.rbc", "rblf")
843
844def init(g, handle):
845 cfg = rblf.cfg(handle)
846 _files = rblf.find_and_copy("*", "from", "to")
847 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
848 cfg["PRODUCT_COPY_FILES"] += rblf.mksubst("foo", "bar", _files)
849`,
850 },
851 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800852 desc: "assignment flavors",
853 mkname: "product.mk",
854 in: `
855PRODUCT_LIST1 := a
856PRODUCT_LIST2 += a
857PRODUCT_LIST1 += b
858PRODUCT_LIST2 += b
859PRODUCT_LIST3 ?= a
860PRODUCT_LIST1 = c
861PLATFORM_LIST += x
862PRODUCT_PACKAGES := $(PLATFORM_LIST)
863`,
864 expected: `load("//build/make/core:product_config.rbc", "rblf")
865
866def init(g, handle):
867 cfg = rblf.cfg(handle)
868 cfg["PRODUCT_LIST1"] = ["a"]
869 rblf.setdefault(handle, "PRODUCT_LIST2")
870 cfg["PRODUCT_LIST2"] += ["a"]
871 cfg["PRODUCT_LIST1"] += ["b"]
872 cfg["PRODUCT_LIST2"] += ["b"]
873 if cfg.get("PRODUCT_LIST3") == None:
874 cfg["PRODUCT_LIST3"] = ["a"]
875 cfg["PRODUCT_LIST1"] = ["c"]
876 g.setdefault("PLATFORM_LIST", [])
877 g["PLATFORM_LIST"] += ["x"]
878 cfg["PRODUCT_PACKAGES"] = g["PLATFORM_LIST"][:]
879`,
880 },
881 {
882 desc: "assigment flavors2",
883 mkname: "product.mk",
884 in: `
885PRODUCT_LIST1 = a
886ifeq (0,1)
887 PRODUCT_LIST1 += b
888 PRODUCT_LIST2 += b
889endif
890PRODUCT_LIST1 += c
891PRODUCT_LIST2 += c
892`,
893 expected: `load("//build/make/core:product_config.rbc", "rblf")
894
895def init(g, handle):
896 cfg = rblf.cfg(handle)
897 cfg["PRODUCT_LIST1"] = ["a"]
898 if "0" == "1":
899 cfg["PRODUCT_LIST1"] += ["b"]
900 rblf.setdefault(handle, "PRODUCT_LIST2")
901 cfg["PRODUCT_LIST2"] += ["b"]
902 cfg["PRODUCT_LIST1"] += ["c"]
903 rblf.setdefault(handle, "PRODUCT_LIST2")
904 cfg["PRODUCT_LIST2"] += ["c"]
905`,
906 },
907 {
Cole Fauste2a37982022-03-09 16:00:17 -0800908 desc: "assigment setdefaults",
909 mkname: "product.mk",
910 in: `
911# All of these should have a setdefault because they're self-referential and not defined before
912PRODUCT_LIST1 = a $(PRODUCT_LIST1)
913PRODUCT_LIST2 ?= a $(PRODUCT_LIST2)
914PRODUCT_LIST3 += a
915
916# Now doing them again should not have a setdefault because they've already been set
917PRODUCT_LIST1 = a $(PRODUCT_LIST1)
918PRODUCT_LIST2 ?= a $(PRODUCT_LIST2)
919PRODUCT_LIST3 += a
920`,
921 expected: `# All of these should have a setdefault because they're self-referential and not defined before
922load("//build/make/core:product_config.rbc", "rblf")
923
924def init(g, handle):
925 cfg = rblf.cfg(handle)
926 rblf.setdefault(handle, "PRODUCT_LIST1")
927 cfg["PRODUCT_LIST1"] = (["a"] +
928 cfg.get("PRODUCT_LIST1", []))
929 if cfg.get("PRODUCT_LIST2") == None:
930 rblf.setdefault(handle, "PRODUCT_LIST2")
931 cfg["PRODUCT_LIST2"] = (["a"] +
932 cfg.get("PRODUCT_LIST2", []))
933 rblf.setdefault(handle, "PRODUCT_LIST3")
934 cfg["PRODUCT_LIST3"] += ["a"]
935 # Now doing them again should not have a setdefault because they've already been set
936 cfg["PRODUCT_LIST1"] = (["a"] +
937 cfg["PRODUCT_LIST1"])
938 if cfg.get("PRODUCT_LIST2") == None:
939 cfg["PRODUCT_LIST2"] = (["a"] +
940 cfg["PRODUCT_LIST2"])
941 cfg["PRODUCT_LIST3"] += ["a"]
942`,
943 },
944 {
Sasha Smundak3deb9682021-07-26 18:42:25 -0700945 desc: "soong namespace assignments",
946 mkname: "product.mk",
947 in: `
948SOONG_CONFIG_NAMESPACES += cvd
949SOONG_CONFIG_cvd += launch_configs
Sasha Smundak65b547e2021-09-17 15:35:41 -0700950SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
Sasha Smundak3deb9682021-07-26 18:42:25 -0700951SOONG_CONFIG_cvd += grub_config
952SOONG_CONFIG_cvd_grub_config += grub.cfg
Sasha Smundak65b547e2021-09-17 15:35:41 -0700953x := $(SOONG_CONFIG_cvd_grub_config)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700954`,
955 expected: `load("//build/make/core:product_config.rbc", "rblf")
956
957def init(g, handle):
958 cfg = rblf.cfg(handle)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700959 rblf.soong_config_namespace(g, "cvd")
960 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
961 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
Sasha Smundak422b6142021-11-11 18:31:59 -0800962 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 -0700963`,
Cole Faustc00184e2021-11-08 12:08:57 -0800964 }, {
965 desc: "soong namespace accesses",
966 mkname: "product.mk",
967 in: `
968SOONG_CONFIG_NAMESPACES += cvd
969SOONG_CONFIG_cvd += launch_configs
970SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
971SOONG_CONFIG_cvd += grub_config
972SOONG_CONFIG_cvd_grub_config += grub.cfg
973x := $(call soong_config_get,cvd,grub_config)
974`,
975 expected: `load("//build/make/core:product_config.rbc", "rblf")
976
977def init(g, handle):
978 cfg = rblf.cfg(handle)
979 rblf.soong_config_namespace(g, "cvd")
980 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
981 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
982 _x = rblf.soong_config_get(g, "cvd", "grub_config")
983`,
Sasha Smundak3deb9682021-07-26 18:42:25 -0700984 },
985 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800986 desc: "string split",
987 mkname: "product.mk",
988 in: `
989PRODUCT_LIST1 = a
990local = b
991local += c
992FOO = d
993FOO += e
994PRODUCT_LIST1 += $(local)
995PRODUCT_LIST1 += $(FOO)
996`,
997 expected: `load("//build/make/core:product_config.rbc", "rblf")
998
999def init(g, handle):
1000 cfg = rblf.cfg(handle)
1001 cfg["PRODUCT_LIST1"] = ["a"]
1002 _local = "b"
1003 _local += " " + "c"
1004 g["FOO"] = "d"
1005 g["FOO"] += " " + "e"
1006 cfg["PRODUCT_LIST1"] += (_local).split()
1007 cfg["PRODUCT_LIST1"] += (g["FOO"]).split()
1008`,
1009 },
1010 {
1011 desc: "apex_jars",
1012 mkname: "product.mk",
1013 in: `
1014PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex
1015`,
1016 expected: `load("//build/make/core:product_config.rbc", "rblf")
1017
1018def init(g, handle):
1019 cfg = rblf.cfg(handle)
1020 cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) +
1021 ["framework-minus-apex"])
1022`,
1023 },
1024 {
Cole Faust95b95cb2022-04-05 16:37:39 -07001025 desc: "strip/sort functions",
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001026 mkname: "product.mk",
1027 in: `
1028ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),)
1029 PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress)
1030endif
Cole Faust95b95cb2022-04-05 16:37:39 -07001031MY_VAR := $(sort b a c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001032`,
1033 expected: `load("//build/make/core:product_config.rbc", "rblf")
1034
1035def init(g, handle):
1036 cfg = rblf.cfg(handle)
1037 if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []):
Cole Faust816e0802022-03-04 12:04:31 -08001038 rblf.setdefault(handle, "PRODUCT_PACKAGES")
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001039 cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split()
Cole Faust95b95cb2022-04-05 16:37:39 -07001040 g["MY_VAR"] = rblf.mksort("b a c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001041`,
1042 },
1043 {
1044 desc: "strip func in condition",
1045 mkname: "product.mk",
1046 in: `
1047ifneq ($(strip $(TARGET_VENDOR)),)
1048endif
1049`,
1050 expected: `load("//build/make/core:product_config.rbc", "rblf")
1051
1052def init(g, handle):
1053 cfg = rblf.cfg(handle)
Sasha Smundak0554d762021-07-08 18:26:12 -07001054 if rblf.mkstrip(g.get("TARGET_VENDOR", "")):
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001055 pass
1056`,
1057 },
1058 {
1059 desc: "ref after set",
1060 mkname: "product.mk",
1061 in: `
1062PRODUCT_ADB_KEYS:=value
1063FOO := $(PRODUCT_ADB_KEYS)
1064ifneq (,$(PRODUCT_ADB_KEYS))
1065endif
1066`,
1067 expected: `load("//build/make/core:product_config.rbc", "rblf")
1068
1069def init(g, handle):
1070 cfg = rblf.cfg(handle)
1071 g["PRODUCT_ADB_KEYS"] = "value"
1072 g["FOO"] = g["PRODUCT_ADB_KEYS"]
1073 if g["PRODUCT_ADB_KEYS"]:
1074 pass
1075`,
1076 },
1077 {
1078 desc: "ref before set",
1079 mkname: "product.mk",
1080 in: `
1081V1 := $(PRODUCT_ADB_KEYS)
1082ifeq (,$(PRODUCT_ADB_KEYS))
1083 V2 := $(PRODUCT_ADB_KEYS)
1084 PRODUCT_ADB_KEYS:=foo
1085 V3 := $(PRODUCT_ADB_KEYS)
1086endif`,
1087 expected: `load("//build/make/core:product_config.rbc", "rblf")
1088
1089def init(g, handle):
1090 cfg = rblf.cfg(handle)
1091 g["V1"] = g.get("PRODUCT_ADB_KEYS", "")
1092 if not g.get("PRODUCT_ADB_KEYS", ""):
1093 g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
1094 g["PRODUCT_ADB_KEYS"] = "foo"
1095 g["V3"] = g["PRODUCT_ADB_KEYS"]
1096`,
1097 },
Sasha Smundak6609ba72021-07-22 18:32:56 -07001098 {
1099 desc: "Dynamic inherit path",
1100 mkname: "product.mk",
1101 in: `
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001102MY_PATH:=foo
Sasha Smundak6609ba72021-07-22 18:32:56 -07001103$(call inherit-product,vendor/$(MY_PATH)/cfg.mk)
1104`,
1105 expected: `load("//build/make/core:product_config.rbc", "rblf")
1106load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1107load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init")
1108
1109def init(g, handle):
1110 cfg = rblf.cfg(handle)
1111 g["MY_PATH"] = "foo"
1112 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001113 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1114 "vendor/bar/baz/cfg.mk": ("vendor/bar/baz/cfg", _cfg1_init),
Sasha Smundak6609ba72021-07-22 18:32:56 -07001115 }.get("vendor/%s/cfg.mk" % g["MY_PATH"])
1116 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1117 if not _varmod_init:
Cole Faust7321b092021-12-21 16:11:16 -08001118 rblf.mkerror("product.mk", "Cannot find %s" % ("vendor/%s/cfg.mk" % g["MY_PATH"]))
Sasha Smundak6609ba72021-07-22 18:32:56 -07001119 rblf.inherit(handle, _varmod, _varmod_init)
1120`,
1121 },
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001122 {
1123 desc: "Dynamic inherit with hint",
1124 mkname: "product.mk",
1125 in: `
1126MY_PATH:=foo
1127#RBC# include_top vendor/foo1
1128$(call inherit-product,$(MY_PATH)/cfg.mk)
1129`,
1130 expected: `load("//build/make/core:product_config.rbc", "rblf")
1131load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1132
1133def init(g, handle):
1134 cfg = rblf.cfg(handle)
1135 g["MY_PATH"] = "foo"
Cole Faust93f8d392022-03-02 13:31:30 -08001136 _entry = {
1137 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1138 }.get("%s/cfg.mk" % g["MY_PATH"])
1139 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1140 if not _varmod_init:
1141 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1142 rblf.inherit(handle, _varmod, _varmod_init)
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001143`,
1144 },
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001145 {
Cole Faustf7ed5342021-12-21 14:15:12 -08001146 desc: "Dynamic inherit with duplicated hint",
1147 mkname: "product.mk",
1148 in: `
1149MY_PATH:=foo
1150#RBC# include_top vendor/foo1
1151$(call inherit-product,$(MY_PATH)/cfg.mk)
1152#RBC# include_top vendor/foo1
Cole Faust7940c6a2022-01-31 15:54:05 -08001153#RBC# include_top vendor/foo1
Cole Faustf7ed5342021-12-21 14:15:12 -08001154$(call inherit-product,$(MY_PATH)/cfg.mk)
1155`,
1156 expected: `load("//build/make/core:product_config.rbc", "rblf")
1157load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1158
1159def init(g, handle):
1160 cfg = rblf.cfg(handle)
1161 g["MY_PATH"] = "foo"
Cole Faust93f8d392022-03-02 13:31:30 -08001162 _entry = {
1163 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1164 }.get("%s/cfg.mk" % g["MY_PATH"])
1165 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1166 if not _varmod_init:
1167 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1168 rblf.inherit(handle, _varmod, _varmod_init)
1169 _entry = {
1170 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1171 }.get("%s/cfg.mk" % g["MY_PATH"])
1172 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1173 if not _varmod_init:
1174 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1175 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faustf7ed5342021-12-21 14:15:12 -08001176`,
1177 },
1178 {
Cole Faust069aba62022-01-26 17:47:33 -08001179 desc: "Dynamic inherit path that lacks hint",
Cole Faust6c934f62022-01-06 15:51:12 -08001180 mkname: "product.mk",
1181 in: `
1182#RBC# include_top foo
1183$(call inherit-product,$(MY_VAR)/font.mk)
1184
1185#RBC# include_top foo
1186
1187# There's some space and even this comment between the include_top and the inherit-product
1188
1189$(call inherit-product,$(MY_VAR)/font.mk)
1190
1191$(call inherit-product,$(MY_VAR)/font.mk)
1192`,
Cole Faust7940c6a2022-01-31 15:54:05 -08001193 expected: `load("//build/make/core:product_config.rbc", "rblf")
Cole Faust6c934f62022-01-06 15:51:12 -08001194load("//foo:font.star|init", _font_init = "init")
Cole Faust069aba62022-01-26 17:47:33 -08001195load("//bar:font.star|init", _font1_init = "init")
Cole Faust6c934f62022-01-06 15:51:12 -08001196
1197def init(g, handle):
1198 cfg = rblf.cfg(handle)
Cole Faust93f8d392022-03-02 13:31:30 -08001199 _entry = {
1200 "foo/font.mk": ("foo/font", _font_init),
1201 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1202 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1203 if not _varmod_init:
1204 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1205 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001206 # There's some space and even this comment between the include_top and the inherit-product
Cole Faust93f8d392022-03-02 13:31:30 -08001207 _entry = {
1208 "foo/font.mk": ("foo/font", _font_init),
1209 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1210 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1211 if not _varmod_init:
1212 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1213 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faustf4e72cf2022-02-08 12:49:37 -08001214 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 -08001215 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001216 "foo/font.mk": ("foo/font", _font_init),
Cole Faust069aba62022-01-26 17:47:33 -08001217 "bar/font.mk": ("bar/font", _font1_init),
Cole Faust6c934f62022-01-06 15:51:12 -08001218 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1219 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1220 if not _varmod_init:
1221 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1222 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001223`,
1224 },
1225 {
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001226 desc: "Ignore make rules",
1227 mkname: "product.mk",
1228 in: `
1229foo: foo.c
1230 gcc -o $@ $*`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001231 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001232
1233def init(g, handle):
1234 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001235 rblf.mk2rbc_error("product.mk:2", "unsupported line rule: foo: foo.c\n#gcc -o $@ $*")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001236`,
1237 },
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001238 {
1239 desc: "Flag override",
1240 mkname: "product.mk",
1241 in: `
1242override FOO:=`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001243 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001244
1245def init(g, handle):
1246 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001247 rblf.mk2rbc_error("product.mk:2", "cannot handle override directive")
Sasha Smundak422b6142021-11-11 18:31:59 -08001248`,
1249 },
1250 {
1251 desc: "Bad expression",
1252 mkname: "build/product.mk",
1253 in: `
1254ifeq (,$(call foobar))
1255endif
1256`,
1257 expected: `load("//build/make/core:product_config.rbc", "rblf")
1258
1259def init(g, handle):
1260 cfg = rblf.cfg(handle)
1261 if rblf.mk2rbc_error("build/product.mk:2", "cannot handle invoking foobar"):
1262 pass
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001263`,
1264 },
Cole Faust4eadba72021-12-07 11:54:52 -08001265 {
1266 desc: "if expression",
1267 mkname: "product.mk",
1268 in: `
1269TEST_VAR := foo
1270TEST_VAR_LIST := foo
1271TEST_VAR_LIST += bar
1272TEST_VAR_2 := $(if $(TEST_VAR),bar)
1273TEST_VAR_3 := $(if $(TEST_VAR),bar,baz)
Cole Faust421a1922022-03-16 14:35:45 -07001274TEST_VAR_4 := $(if $(TEST_VAR),$(TEST_VAR_LIST))
Cole Faust4eadba72021-12-07 11:54:52 -08001275`,
1276 expected: `load("//build/make/core:product_config.rbc", "rblf")
1277
1278def init(g, handle):
1279 cfg = rblf.cfg(handle)
1280 g["TEST_VAR"] = "foo"
1281 g["TEST_VAR_LIST"] = ["foo"]
1282 g["TEST_VAR_LIST"] += ["bar"]
1283 g["TEST_VAR_2"] = ("bar" if g["TEST_VAR"] else "")
1284 g["TEST_VAR_3"] = ("bar" if g["TEST_VAR"] else "baz")
Cole Faust421a1922022-03-16 14:35:45 -07001285 g["TEST_VAR_4"] = (g["TEST_VAR_LIST"] if g["TEST_VAR"] else [])
Cole Faust4eadba72021-12-07 11:54:52 -08001286`,
1287 },
Cole Faustc36c9622021-12-07 15:20:45 -08001288 {
1289 desc: "substitution references",
1290 mkname: "product.mk",
1291 in: `
1292SOURCES := foo.c bar.c
1293OBJECTS := $(SOURCES:.c=.o)
1294OBJECTS2 := $(SOURCES:%.c=%.o)
1295`,
1296 expected: `load("//build/make/core:product_config.rbc", "rblf")
1297
1298def init(g, handle):
1299 cfg = rblf.cfg(handle)
1300 g["SOURCES"] = "foo.c bar.c"
1301 g["OBJECTS"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1302 g["OBJECTS2"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1303`,
1304 },
Cole Faustb0d32ab2021-12-09 14:00:59 -08001305 {
1306 desc: "foreach expressions",
1307 mkname: "product.mk",
1308 in: `
1309BOOT_KERNEL_MODULES := foo.ko bar.ko
1310BOOT_KERNEL_MODULES_FILTER := $(foreach m,$(BOOT_KERNEL_MODULES),%/$(m))
1311BOOT_KERNEL_MODULES_LIST := foo.ko
1312BOOT_KERNEL_MODULES_LIST += bar.ko
1313BOOT_KERNEL_MODULES_FILTER_2 := $(foreach m,$(BOOT_KERNEL_MODULES_LIST),%/$(m))
1314
Cole Faustb67aa082022-02-28 16:39:59 -08001315FOREACH_WITH_IF := $(foreach module,\
1316 $(BOOT_KERNEL_MODULES_LIST),\
1317 $(if $(filter $(module),foo.ko),,$(error module "$(module)" has an error!)))
Cole Faustf035d402022-03-28 14:02:50 -07001318
1319# Same as above, but not assigning it to a variable allows it to be converted to statements
1320$(foreach module,\
1321 $(BOOT_KERNEL_MODULES_LIST),\
1322 $(if $(filter $(module),foo.ko),,$(error module "$(module)" has an error!)))
Cole Faustb0d32ab2021-12-09 14:00:59 -08001323`,
1324 expected: `load("//build/make/core:product_config.rbc", "rblf")
1325
1326def init(g, handle):
1327 cfg = rblf.cfg(handle)
1328 g["BOOT_KERNEL_MODULES"] = "foo.ko bar.ko"
1329 g["BOOT_KERNEL_MODULES_FILTER"] = ["%%/%s" % m for m in rblf.words(g["BOOT_KERNEL_MODULES"])]
1330 g["BOOT_KERNEL_MODULES_LIST"] = ["foo.ko"]
1331 g["BOOT_KERNEL_MODULES_LIST"] += ["bar.ko"]
1332 g["BOOT_KERNEL_MODULES_FILTER_2"] = ["%%/%s" % m for m in g["BOOT_KERNEL_MODULES_LIST"]]
Cole Faustb67aa082022-02-28 16:39:59 -08001333 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 -07001334 # Same as above, but not assigning it to a variable allows it to be converted to statements
1335 for module in g["BOOT_KERNEL_MODULES_LIST"]:
1336 if not rblf.filter(module, "foo.ko"):
1337 rblf.mkerror("product.mk", "module \"%s\" has an error!" % module)
Cole Faustb0d32ab2021-12-09 14:00:59 -08001338`,
1339 },
Cole Faust0484c232021-12-22 14:08:08 -08001340 {
1341 desc: "List appended to string",
1342 mkname: "product.mk",
1343 in: `
1344NATIVE_BRIDGE_PRODUCT_PACKAGES := \
1345 libnative_bridge_vdso.native_bridge \
1346 native_bridge_guest_app_process.native_bridge \
1347 native_bridge_guest_linker.native_bridge
1348
1349NATIVE_BRIDGE_MODIFIED_GUEST_LIBS := \
1350 libaaudio \
1351 libamidi \
1352 libandroid \
1353 libandroid_runtime
1354
1355NATIVE_BRIDGE_PRODUCT_PACKAGES += \
1356 $(addsuffix .native_bridge,$(NATIVE_BRIDGE_ORIG_GUEST_LIBS))
1357`,
1358 expected: `load("//build/make/core:product_config.rbc", "rblf")
1359
1360def init(g, handle):
1361 cfg = rblf.cfg(handle)
1362 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] = "libnative_bridge_vdso.native_bridge native_bridge_guest_app_process.native_bridge native_bridge_guest_linker.native_bridge"
1363 g["NATIVE_BRIDGE_MODIFIED_GUEST_LIBS"] = "libaaudio libamidi libandroid libandroid_runtime"
1364 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] += " " + " ".join(rblf.addsuffix(".native_bridge", g.get("NATIVE_BRIDGE_ORIG_GUEST_LIBS", "")))
1365`,
1366 },
Cole Faustb1103e22022-01-06 15:22:05 -08001367 {
1368 desc: "Math functions",
1369 mkname: "product.mk",
1370 in: `
1371# Test the math functions defined in build/make/common/math.mk
1372ifeq ($(call math_max,2,5),5)
1373endif
1374ifeq ($(call math_min,2,5),2)
1375endif
1376ifeq ($(call math_gt_or_eq,2,5),true)
1377endif
1378ifeq ($(call math_gt,2,5),true)
1379endif
1380ifeq ($(call math_lt,2,5),true)
1381endif
1382ifeq ($(call math_gt_or_eq,2,5),)
1383endif
1384ifeq ($(call math_gt,2,5),)
1385endif
1386ifeq ($(call math_lt,2,5),)
1387endif
1388ifeq ($(call math_gt_or_eq,$(MY_VAR), 5),true)
1389endif
1390ifeq ($(call math_gt_or_eq,$(MY_VAR),$(MY_OTHER_VAR)),true)
1391endif
1392ifeq ($(call math_gt_or_eq,100$(MY_VAR),10),true)
1393endif
1394`,
1395 expected: `# Test the math functions defined in build/make/common/math.mk
1396load("//build/make/core:product_config.rbc", "rblf")
1397
1398def init(g, handle):
1399 cfg = rblf.cfg(handle)
1400 if max(2, 5) == 5:
1401 pass
1402 if min(2, 5) == 2:
1403 pass
1404 if 2 >= 5:
1405 pass
1406 if 2 > 5:
1407 pass
1408 if 2 < 5:
1409 pass
1410 if 2 < 5:
1411 pass
1412 if 2 <= 5:
1413 pass
1414 if 2 >= 5:
1415 pass
1416 if int(g.get("MY_VAR", "")) >= 5:
1417 pass
1418 if int(g.get("MY_VAR", "")) >= int(g.get("MY_OTHER_VAR", "")):
1419 pass
1420 if int("100%s" % g.get("MY_VAR", "")) >= 10:
1421 pass
1422`,
1423 },
Cole Faustf92c9f22022-03-14 14:35:50 -07001424 {
1425 desc: "Type hints",
1426 mkname: "product.mk",
1427 in: `
1428# Test type hints
1429#RBC# type_hint list MY_VAR MY_VAR_2
1430# Unsupported type
1431#RBC# type_hint bool MY_VAR_3
1432# Invalid syntax
1433#RBC# type_hint list
1434# Duplicated variable
1435#RBC# type_hint list MY_VAR_2
1436#RBC# type_hint list my-local-var-with-dashes
Cole Faust421a1922022-03-16 14:35:45 -07001437#RBC# type_hint string MY_STRING_VAR
Cole Faustf92c9f22022-03-14 14:35:50 -07001438
1439MY_VAR := foo
1440MY_VAR_UNHINTED := foo
1441
1442# Vars set after other statements still get the hint
1443MY_VAR_2 := foo
1444
1445# You can't specify a type hint after the first statement
1446#RBC# type_hint list MY_VAR_4
1447MY_VAR_4 := foo
1448
1449my-local-var-with-dashes := foo
Cole Faust421a1922022-03-16 14:35:45 -07001450
1451MY_STRING_VAR := $(wildcard foo/bar.mk)
Cole Faustf92c9f22022-03-14 14:35:50 -07001452`,
1453 expected: `# Test type hints
1454# Unsupported type
1455load("//build/make/core:product_config.rbc", "rblf")
1456
1457def init(g, handle):
1458 cfg = rblf.cfg(handle)
1459 rblf.mk2rbc_error("product.mk:5", "Invalid type_hint annotation. Only list/string types are accepted, found bool")
1460 # Invalid syntax
1461 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")
1462 # Duplicated variable
1463 rblf.mk2rbc_error("product.mk:9", "Duplicate type hint for variable MY_VAR_2")
1464 g["MY_VAR"] = ["foo"]
1465 g["MY_VAR_UNHINTED"] = "foo"
1466 # Vars set after other statements still get the hint
1467 g["MY_VAR_2"] = ["foo"]
1468 # You can't specify a type hint after the first statement
Cole Faust421a1922022-03-16 14:35:45 -07001469 rblf.mk2rbc_error("product.mk:20", "type_hint annotations must come before the first Makefile statement")
Cole Faustf92c9f22022-03-14 14:35:50 -07001470 g["MY_VAR_4"] = "foo"
1471 _my_local_var_with_dashes = ["foo"]
Cole Faust421a1922022-03-16 14:35:45 -07001472 g["MY_STRING_VAR"] = " ".join(rblf.expand_wildcard("foo/bar.mk"))
Cole Faustf92c9f22022-03-14 14:35:50 -07001473`,
1474 },
Cole Faustf5adedc2022-03-18 14:05:06 -07001475 {
1476 desc: "Set LOCAL_PATH to my-dir",
1477 mkname: "product.mk",
1478 in: `
1479LOCAL_PATH := $(call my-dir)
1480`,
1481 expected: `load("//build/make/core:product_config.rbc", "rblf")
1482
1483def init(g, handle):
1484 cfg = rblf.cfg(handle)
1485
1486`,
1487 },
Cole Faustf035d402022-03-28 14:02:50 -07001488 {
1489 desc: "Evals",
1490 mkname: "product.mk",
1491 in: `
1492$(eval)
1493$(eval MY_VAR := foo)
1494$(eval # This is a test of eval functions)
1495$(eval $(TOO_COMPLICATED) := bar)
1496$(foreach x,$(MY_LIST_VAR), \
1497 $(eval PRODUCT_COPY_FILES += foo/bar/$(x):$(TARGET_COPY_OUT_VENDOR)/etc/$(x)) \
1498 $(if $(MY_OTHER_VAR),$(eval PRODUCT_COPY_FILES += $(MY_OTHER_VAR):foo/bar/$(x))) \
1499)
1500
1501`,
1502 expected: `load("//build/make/core:product_config.rbc", "rblf")
1503
1504def init(g, handle):
1505 cfg = rblf.cfg(handle)
1506 g["MY_VAR"] = "foo"
1507 # This is a test of eval functions
1508 rblf.mk2rbc_error("product.mk:5", "Eval expression too complex; only assignments and comments are supported")
1509 for x in rblf.words(g.get("MY_LIST_VAR", "")):
1510 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
1511 cfg["PRODUCT_COPY_FILES"] += ("foo/bar/%s:%s/etc/%s" % (x, g.get("TARGET_COPY_OUT_VENDOR", ""), x)).split()
1512 if g.get("MY_OTHER_VAR", ""):
1513 cfg["PRODUCT_COPY_FILES"] += ("%s:foo/bar/%s" % (g.get("MY_OTHER_VAR", ""), x)).split()
1514`,
1515 },
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001516}
1517
1518var known_variables = []struct {
1519 name string
1520 class varClass
1521 starlarkType
1522}{
Cole Faustf1f44d32021-11-16 14:52:12 -08001523 {"NATIVE_COVERAGE", VarClassSoong, starlarkTypeBool},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001524 {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
1525 {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
1526 {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
1527 {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
1528 {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
1529 {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
1530 {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
1531 {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
1532 {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
1533 {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
1534 {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
1535 {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
1536 {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
1537 {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
1538}
1539
Sasha Smundak6609ba72021-07-22 18:32:56 -07001540type testMakefileFinder struct {
1541 fs fs.FS
1542 root string
1543 files []string
1544}
1545
1546func (t *testMakefileFinder) Find(root string) []string {
1547 if t.files != nil || root == t.root {
1548 return t.files
1549 }
1550 t.files = make([]string, 0)
1551 fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error {
1552 if err != nil {
1553 return err
1554 }
1555 if d.IsDir() {
1556 base := filepath.Base(path)
1557 if base[0] == '.' && len(base) > 1 {
1558 return fs.SkipDir
1559 }
1560 return nil
1561 }
1562 if strings.HasSuffix(path, ".mk") {
1563 t.files = append(t.files, path)
1564 }
1565 return nil
1566 })
1567 return t.files
1568}
1569
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001570func TestGood(t *testing.T) {
1571 for _, v := range known_variables {
1572 KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
1573 }
Sasha Smundak6609ba72021-07-22 18:32:56 -07001574 fs := NewFindMockFS([]string{
1575 "vendor/foo1/cfg.mk",
1576 "vendor/bar/baz/cfg.mk",
1577 "part.mk",
1578 "foo/font.mk",
1579 "bar/font.mk",
1580 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001581 for _, test := range testCases {
1582 t.Run(test.desc,
1583 func(t *testing.T) {
1584 ss, err := Convert(Request{
Sasha Smundak422b6142021-11-11 18:31:59 -08001585 MkFile: test.mkname,
1586 Reader: bytes.NewBufferString(test.in),
Sasha Smundak422b6142021-11-11 18:31:59 -08001587 OutputSuffix: ".star",
1588 SourceFS: fs,
1589 MakefileFinder: &testMakefileFinder{fs: fs},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001590 })
1591 if err != nil {
1592 t.Error(err)
1593 return
1594 }
1595 got := ss.String()
1596 if got != test.expected {
1597 t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
1598 strings.ReplaceAll(test.expected, "\n", "␤\n"),
1599 strings.ReplaceAll(got, "\n", "␤\n"))
1600 }
1601 })
1602 }
1603}