blob: fc78d474c6f59cb40afecb24a67867e5c859b893 [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)
257$(info this is the info)
258$(error this is the error)
259PRODUCT_NAME:=$(shell echo *)
260`,
261 expected: `load("//build/make/core:product_config.rbc", "rblf")
262
263def init(g, handle):
264 cfg = rblf.cfg(handle)
265 rblf.mkwarning("product.mk", "this is the warning")
266 rblf.mkwarning("product.mk", "")
267 rblf.mkinfo("product.mk", "this is the info")
268 rblf.mkerror("product.mk", "this is the error")
269 cfg["PRODUCT_NAME"] = rblf.shell("echo *")
270`,
271 },
272 {
273 desc: "Empty if",
274 mkname: "product.mk",
275 in: `
276ifdef PRODUCT_NAME
277# Comment
Sasha Smundak6609ba72021-07-22 18:32:56 -0700278else
Sasha Smundak02183cf2021-08-16 13:36:11 -0700279 TARGET_COPY_OUT_RECOVERY := foo
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800280endif
281`,
282 expected: `load("//build/make/core:product_config.rbc", "rblf")
283
284def init(g, handle):
285 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800286 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800287 # Comment
288 pass
Sasha Smundak6609ba72021-07-22 18:32:56 -0700289 else:
Sasha Smundak422b6142021-11-11 18:31:59 -0800290 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 -0800291`,
292 },
293 {
294 desc: "if/else/endif",
295 mkname: "product.mk",
296 in: `
297ifndef PRODUCT_NAME
298 PRODUCT_NAME=gizmo1
299else
300 PRODUCT_NAME=gizmo2
301endif
302`,
303 expected: `load("//build/make/core:product_config.rbc", "rblf")
304
305def init(g, handle):
306 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800307 if not cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800308 cfg["PRODUCT_NAME"] = "gizmo1"
309 else:
310 cfg["PRODUCT_NAME"] = "gizmo2"
311`,
312 },
313 {
314 desc: "else if",
315 mkname: "product.mk",
316 in: `
317ifdef PRODUCT_NAME
318 PRODUCT_NAME = gizmo
319else ifndef PRODUCT_PACKAGES # Comment
320endif
321 `,
322 expected: `load("//build/make/core:product_config.rbc", "rblf")
323
324def init(g, handle):
325 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800326 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800327 cfg["PRODUCT_NAME"] = "gizmo"
Cole Faust71514c02022-01-27 17:21:41 -0800328 elif not cfg.get("PRODUCT_PACKAGES", []):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800329 # Comment
330 pass
331`,
332 },
333 {
334 desc: "ifeq / ifneq",
335 mkname: "product.mk",
336 in: `
337ifeq (aosp_arm, $(TARGET_PRODUCT))
338 PRODUCT_MODEL = pix2
339else
340 PRODUCT_MODEL = pix21
341endif
342ifneq (aosp_x86, $(TARGET_PRODUCT))
343 PRODUCT_MODEL = pix3
344endif
345`,
346 expected: `load("//build/make/core:product_config.rbc", "rblf")
347
348def init(g, handle):
349 cfg = rblf.cfg(handle)
350 if "aosp_arm" == g["TARGET_PRODUCT"]:
351 cfg["PRODUCT_MODEL"] = "pix2"
352 else:
353 cfg["PRODUCT_MODEL"] = "pix21"
354 if "aosp_x86" != g["TARGET_PRODUCT"]:
355 cfg["PRODUCT_MODEL"] = "pix3"
356`,
357 },
358 {
Cole Faustf8320212021-11-10 15:05:07 -0800359 desc: "ifeq with soong_config_get",
360 mkname: "product.mk",
361 in: `
362ifeq (true,$(call soong_config_get,art_module,source_build))
363endif
364`,
365 expected: `load("//build/make/core:product_config.rbc", "rblf")
366
367def init(g, handle):
368 cfg = rblf.cfg(handle)
369 if "true" == rblf.soong_config_get(g, "art_module", "source_build"):
370 pass
371`,
372 },
373 {
Cole Faustf1f44d32021-11-16 14:52:12 -0800374 desc: "ifeq with $(NATIVE_COVERAGE)",
375 mkname: "product.mk",
376 in: `
377ifeq ($(NATIVE_COVERAGE),true)
378endif
379`,
380 expected: `load("//build/make/core:product_config.rbc", "rblf")
381
382def init(g, handle):
383 cfg = rblf.cfg(handle)
384 if g.get("NATIVE_COVERAGE", False):
385 pass
386`,
387 },
388 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800389 desc: "Check filter result",
390 mkname: "product.mk",
391 in: `
392ifeq (,$(filter userdebug eng, $(TARGET_BUILD_VARIANT)))
393endif
394ifneq (,$(filter userdebug,$(TARGET_BUILD_VARIANT))
395endif
396ifneq (,$(filter plaf,$(PLATFORM_LIST)))
397endif
398ifeq ($(TARGET_BUILD_VARIANT), $(filter $(TARGET_BUILD_VARIANT), userdebug eng))
399endif
Cole Faust9932f752022-02-08 11:56:25 -0800400ifneq (, $(filter $(TARGET_BUILD_VARIANT), userdebug eng))
401endif
402ifneq (,$(filter userdebug eng, $(TARGET_BUILD_VARIANT)))
403endif
Sasha Smundak0554d762021-07-08 18:26:12 -0700404ifneq (,$(filter true, $(v1)$(v2)))
405endif
Sasha Smundak5f463be2021-09-15 18:43:36 -0700406ifeq (,$(filter barbet coral%,$(TARGET_PRODUCT)))
407else ifneq (,$(filter barbet%,$(TARGET_PRODUCT)))
408endif
Cole Fausteec0d812021-12-06 16:23:51 -0800409ifeq (,$(filter-out sunfish_kasan, $(TARGET_PRODUCT)))
410endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800411`,
412 expected: `load("//build/make/core:product_config.rbc", "rblf")
413
414def init(g, handle):
415 cfg = rblf.cfg(handle)
Sasha Smundak5f463be2021-09-15 18:43:36 -0700416 if not rblf.filter("userdebug eng", g["TARGET_BUILD_VARIANT"]):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800417 pass
Sasha Smundak5f463be2021-09-15 18:43:36 -0700418 if rblf.filter("userdebug", g["TARGET_BUILD_VARIANT"]):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800419 pass
420 if "plaf" in g.get("PLATFORM_LIST", []):
421 pass
Cole Faust9932f752022-02-08 11:56:25 -0800422 if g["TARGET_BUILD_VARIANT"] == " ".join(rblf.filter(g["TARGET_BUILD_VARIANT"], "userdebug eng")):
423 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800424 if g["TARGET_BUILD_VARIANT"] in ["userdebug", "eng"]:
425 pass
Cole Faust9932f752022-02-08 11:56:25 -0800426 if rblf.filter("userdebug eng", g["TARGET_BUILD_VARIANT"]):
427 pass
Sasha Smundak5f463be2021-09-15 18:43:36 -0700428 if rblf.filter("true", "%s%s" % (_v1, _v2)):
429 pass
430 if not rblf.filter("barbet coral%", g["TARGET_PRODUCT"]):
431 pass
432 elif rblf.filter("barbet%", g["TARGET_PRODUCT"]):
Sasha Smundak0554d762021-07-08 18:26:12 -0700433 pass
Cole Fausteec0d812021-12-06 16:23:51 -0800434 if not rblf.filter_out("sunfish_kasan", g["TARGET_PRODUCT"]):
435 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800436`,
437 },
438 {
439 desc: "Get filter result",
440 mkname: "product.mk",
441 in: `
442PRODUCT_LIST2=$(filter-out %/foo.ko,$(wildcard path/*.ko))
443`,
444 expected: `load("//build/make/core:product_config.rbc", "rblf")
445
446def init(g, handle):
447 cfg = rblf.cfg(handle)
448 cfg["PRODUCT_LIST2"] = rblf.filter_out("%/foo.ko", rblf.expand_wildcard("path/*.ko"))
449`,
450 },
451 {
452 desc: "filter $(VAR), values",
453 mkname: "product.mk",
454 in: `
455ifeq (,$(filter $(TARGET_PRODUCT), yukawa_gms yukawa_sei510_gms)
456 ifneq (,$(filter $(TARGET_PRODUCT), yukawa_gms)
457 endif
458endif
459
460`,
461 expected: `load("//build/make/core:product_config.rbc", "rblf")
462
463def init(g, handle):
464 cfg = rblf.cfg(handle)
465 if g["TARGET_PRODUCT"] not in ["yukawa_gms", "yukawa_sei510_gms"]:
Sasha Smundak0554d762021-07-08 18:26:12 -0700466 if g["TARGET_PRODUCT"] == "yukawa_gms":
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800467 pass
468`,
469 },
470 {
Sasha Smundak0554d762021-07-08 18:26:12 -0700471 desc: "filter $(V1), $(V2)",
472 mkname: "product.mk",
473 in: `
474ifneq (, $(filter $(PRODUCT_LIST), $(TARGET_PRODUCT)))
475endif
476`,
477 expected: `load("//build/make/core:product_config.rbc", "rblf")
478
479def init(g, handle):
480 cfg = rblf.cfg(handle)
Sasha Smundak468e11f2021-08-26 09:10:23 -0700481 if rblf.filter(g.get("PRODUCT_LIST", []), g["TARGET_PRODUCT"]):
Sasha Smundak0554d762021-07-08 18:26:12 -0700482 pass
483`,
484 },
485 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800486 desc: "ifeq",
487 mkname: "product.mk",
488 in: `
489ifeq (aosp, $(TARGET_PRODUCT)) # Comment
490else ifneq (, $(TARGET_PRODUCT))
491endif
492`,
493 expected: `load("//build/make/core:product_config.rbc", "rblf")
494
495def init(g, handle):
496 cfg = rblf.cfg(handle)
497 if "aosp" == g["TARGET_PRODUCT"]:
498 # Comment
499 pass
500 elif g["TARGET_PRODUCT"]:
501 pass
502`,
503 },
504 {
505 desc: "Nested if",
506 mkname: "product.mk",
507 in: `
508ifdef PRODUCT_NAME
509 PRODUCT_PACKAGES = pack-if0
510 ifdef PRODUCT_MODEL
511 PRODUCT_PACKAGES = pack-if-if
512 else ifdef PRODUCT_NAME
513 PRODUCT_PACKAGES = pack-if-elif
514 else
515 PRODUCT_PACKAGES = pack-if-else
516 endif
517 PRODUCT_PACKAGES = pack-if
518else ifneq (,$(TARGET_PRODUCT))
519 PRODUCT_PACKAGES = pack-elif
520else
521 PRODUCT_PACKAGES = pack-else
522endif
523`,
524 expected: `load("//build/make/core:product_config.rbc", "rblf")
525
526def init(g, handle):
527 cfg = rblf.cfg(handle)
Cole Faust71514c02022-01-27 17:21:41 -0800528 if cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800529 cfg["PRODUCT_PACKAGES"] = ["pack-if0"]
Cole Faust71514c02022-01-27 17:21:41 -0800530 if cfg.get("PRODUCT_MODEL", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800531 cfg["PRODUCT_PACKAGES"] = ["pack-if-if"]
Cole Faust71514c02022-01-27 17:21:41 -0800532 elif cfg.get("PRODUCT_NAME", ""):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800533 cfg["PRODUCT_PACKAGES"] = ["pack-if-elif"]
534 else:
535 cfg["PRODUCT_PACKAGES"] = ["pack-if-else"]
536 cfg["PRODUCT_PACKAGES"] = ["pack-if"]
537 elif g["TARGET_PRODUCT"]:
538 cfg["PRODUCT_PACKAGES"] = ["pack-elif"]
539 else:
540 cfg["PRODUCT_PACKAGES"] = ["pack-else"]
541`,
542 },
543 {
544 desc: "Wildcard",
545 mkname: "product.mk",
546 in: `
547ifeq (,$(wildcard foo.mk))
548endif
549ifneq (,$(wildcard foo*.mk))
550endif
551`,
552 expected: `load("//build/make/core:product_config.rbc", "rblf")
553
554def init(g, handle):
555 cfg = rblf.cfg(handle)
556 if not rblf.file_exists("foo.mk"):
557 pass
558 if rblf.file_wildcard_exists("foo*.mk"):
559 pass
560`,
561 },
562 {
Cole Faustf8320212021-11-10 15:05:07 -0800563 desc: "if with interpolation",
564 mkname: "product.mk",
565 in: `
566ifeq ($(VARIABLE1)text$(VARIABLE2),true)
567endif
568`,
569 expected: `load("//build/make/core:product_config.rbc", "rblf")
570
571def init(g, handle):
572 cfg = rblf.cfg(handle)
573 if "%stext%s" % (g.get("VARIABLE1", ""), g.get("VARIABLE2", "")) == "true":
574 pass
575`,
576 },
577 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800578 desc: "ifneq $(X),true",
579 mkname: "product.mk",
580 in: `
581ifneq ($(VARIABLE),true)
582endif
583`,
584 expected: `load("//build/make/core:product_config.rbc", "rblf")
585
586def init(g, handle):
587 cfg = rblf.cfg(handle)
588 if g.get("VARIABLE", "") != "true":
589 pass
590`,
591 },
592 {
593 desc: "Const neq",
594 mkname: "product.mk",
595 in: `
596ifneq (1,0)
597endif
598`,
599 expected: `load("//build/make/core:product_config.rbc", "rblf")
600
601def init(g, handle):
602 cfg = rblf.cfg(handle)
603 if "1" != "0":
604 pass
605`,
606 },
607 {
608 desc: "is-board calls",
609 mkname: "product.mk",
610 in: `
611ifeq ($(call is-board-platform-in-list,msm8998), true)
612else ifneq ($(call is-board-platform,copper),true)
613else ifneq ($(call is-vendor-board-platform,QCOM),true)
614else ifeq ($(call is-product-in-list, $(PLATFORM_LIST)), true)
615endif
616`,
617 expected: `load("//build/make/core:product_config.rbc", "rblf")
618
619def init(g, handle):
620 cfg = rblf.cfg(handle)
Cole Faustb2e0b602022-01-07 15:46:58 -0800621 if rblf.board_platform_in(g, "msm8998"):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800622 pass
Cole Faustb2e0b602022-01-07 15:46:58 -0800623 elif not rblf.board_platform_is(g, "copper"):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800624 pass
625 elif g.get("TARGET_BOARD_PLATFORM", "") not in g["QCOM_BOARD_PLATFORMS"]:
626 pass
627 elif g["TARGET_PRODUCT"] in g.get("PLATFORM_LIST", []):
628 pass
629`,
630 },
631 {
Sasha Smundak3a9b8e82021-08-25 14:11:04 -0700632 desc: "new is-board calls",
633 mkname: "product.mk",
634 in: `
635ifneq (,$(call is-board-platform-in-list2,msm8998 $(X))
636else ifeq (,$(call is-board-platform2,copper)
637else ifneq (,$(call is-vendor-board-qcom))
638endif
639`,
640 expected: `load("//build/make/core:product_config.rbc", "rblf")
641
642def init(g, handle):
643 cfg = rblf.cfg(handle)
644 if rblf.board_platform_in(g, "msm8998 %s" % g.get("X", "")):
645 pass
646 elif not rblf.board_platform_is(g, "copper"):
647 pass
Sasha Smundak4f1f1182021-11-04 17:57:39 -0700648 elif g.get("TARGET_BOARD_PLATFORM", "") in g["QCOM_BOARD_PLATFORMS"]:
Sasha Smundak3a9b8e82021-08-25 14:11:04 -0700649 pass
650`,
651 },
652 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800653 desc: "findstring call",
654 mkname: "product.mk",
655 in: `
Cole Faust0e9418c2021-12-13 16:33:25 -0800656result := $(findstring a,a b c)
657result := $(findstring b,x y z)
658`,
659 expected: `load("//build/make/core:product_config.rbc", "rblf")
660
661def init(g, handle):
662 cfg = rblf.cfg(handle)
663 _result = rblf.findstring("a", "a b c")
664 _result = rblf.findstring("b", "x y z")
665`,
666 },
667 {
668 desc: "findstring in if statement",
669 mkname: "product.mk",
670 in: `
671ifeq ($(findstring foo,$(PRODUCT_PACKAGES)),)
672endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800673ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),)
674endif
Cole Faust0e9418c2021-12-13 16:33:25 -0800675ifeq ($(findstring foo,$(PRODUCT_PACKAGES)),foo)
676endif
677ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),foo)
678endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800679`,
680 expected: `load("//build/make/core:product_config.rbc", "rblf")
681
682def init(g, handle):
683 cfg = rblf.cfg(handle)
Cole Faust0e9418c2021-12-13 16:33:25 -0800684 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") == -1:
685 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800686 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
687 pass
Cole Faust0e9418c2021-12-13 16:33:25 -0800688 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
689 pass
690 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") == -1:
691 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800692`,
693 },
694 {
695 desc: "rhs call",
696 mkname: "product.mk",
697 in: `
698PRODUCT_COPY_FILES = $(call add-to-product-copy-files-if-exists, path:distpath) \
699 $(call find-copy-subdir-files, *, fromdir, todir) $(wildcard foo.*)
700`,
701 expected: `load("//build/make/core:product_config.rbc", "rblf")
702
703def init(g, handle):
704 cfg = rblf.cfg(handle)
705 cfg["PRODUCT_COPY_FILES"] = (rblf.copy_if_exists("path:distpath") +
706 rblf.find_and_copy("*", "fromdir", "todir") +
707 rblf.expand_wildcard("foo.*"))
708`,
709 },
710 {
711 desc: "inferred type",
712 mkname: "product.mk",
713 in: `
714HIKEY_MODS := $(wildcard foo/*.ko)
715BOARD_VENDOR_KERNEL_MODULES += $(HIKEY_MODS)
716`,
717 expected: `load("//build/make/core:product_config.rbc", "rblf")
718
719def init(g, handle):
720 cfg = rblf.cfg(handle)
721 g["HIKEY_MODS"] = rblf.expand_wildcard("foo/*.ko")
722 g.setdefault("BOARD_VENDOR_KERNEL_MODULES", [])
723 g["BOARD_VENDOR_KERNEL_MODULES"] += g["HIKEY_MODS"]
724`,
725 },
726 {
727 desc: "list with vars",
728 mkname: "product.mk",
729 in: `
730PRODUCT_COPY_FILES += path1:$(TARGET_PRODUCT)/path1 $(PRODUCT_MODEL)/path2:$(TARGET_PRODUCT)/path2
731`,
732 expected: `load("//build/make/core:product_config.rbc", "rblf")
733
734def init(g, handle):
735 cfg = rblf.cfg(handle)
736 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
737 cfg["PRODUCT_COPY_FILES"] += (("path1:%s/path1" % g["TARGET_PRODUCT"]).split() +
738 ("%s/path2:%s/path2" % (cfg.get("PRODUCT_MODEL", ""), g["TARGET_PRODUCT"])).split())
739`,
740 },
741 {
742 desc: "misc calls",
743 mkname: "product.mk",
744 in: `
745$(call enforce-product-packages-exist,)
746$(call enforce-product-packages-exist, foo)
747$(call require-artifacts-in-path, foo, bar)
748$(call require-artifacts-in-path-relaxed, foo, bar)
Sasha Smundakd6797852021-11-15 13:01:53 -0800749$(call dist-for-goals, goal, from:to)
Cole Faust1cc08852022-02-28 11:12:08 -0800750$(call add-product-dex-preopt-module-config,MyModule,disable)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800751`,
752 expected: `load("//build/make/core:product_config.rbc", "rblf")
753
754def init(g, handle):
755 cfg = rblf.cfg(handle)
756 rblf.enforce_product_packages_exist("")
757 rblf.enforce_product_packages_exist("foo")
758 rblf.require_artifacts_in_path("foo", "bar")
759 rblf.require_artifacts_in_path_relaxed("foo", "bar")
Sasha Smundakd6797852021-11-15 13:01:53 -0800760 rblf.mkdist_for_goals(g, "goal", "from:to")
Cole Faust1cc08852022-02-28 11:12:08 -0800761 rblf.add_product_dex_preopt_module_config(handle, "MyModule", "disable")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800762`,
763 },
764 {
765 desc: "list with functions",
766 mkname: "product.mk",
767 in: `
768PRODUCT_COPY_FILES := $(call find-copy-subdir-files,*.kl,from1,to1) \
769 $(call find-copy-subdir-files,*.kc,from2,to2) \
770 foo bar
771`,
772 expected: `load("//build/make/core:product_config.rbc", "rblf")
773
774def init(g, handle):
775 cfg = rblf.cfg(handle)
776 cfg["PRODUCT_COPY_FILES"] = (rblf.find_and_copy("*.kl", "from1", "to1") +
777 rblf.find_and_copy("*.kc", "from2", "to2") +
778 [
779 "foo",
780 "bar",
781 ])
782`,
783 },
784 {
785 desc: "Text functions",
786 mkname: "product.mk",
787 in: `
788PRODUCT_COPY_FILES := $(addprefix pfx-,a b c)
789PRODUCT_COPY_FILES := $(addsuffix .sff, a b c)
790PRODUCT_NAME := $(word 1, $(subst ., ,$(TARGET_BOARD_PLATFORM)))
Sasha Smundak35434ed2021-11-05 16:29:56 -0700791$(info $(patsubst %.pub,$(PRODUCT_NAME)%,$(PRODUCT_ADB_KEYS)))
Sasha Smundak16e07732021-07-23 11:38:23 -0700792$(info $(dir foo/bar))
793$(info $(firstword $(PRODUCT_COPY_FILES)))
794$(info $(lastword $(PRODUCT_COPY_FILES)))
795$(info $(dir $(lastword $(MAKEFILE_LIST))))
796$(info $(dir $(lastword $(PRODUCT_COPY_FILES))))
797$(info $(dir $(lastword $(foobar))))
798$(info $(abspath foo/bar))
799$(info $(notdir foo/bar))
Sasha Smundak3deb9682021-07-26 18:42:25 -0700800$(call add_soong_config_namespace,snsconfig)
801$(call add_soong_config_var_value,snsconfig,imagetype,odm_image)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700802$(call soong_config_set, snsconfig, foo, foo_value)
803$(call soong_config_append, snsconfig, bar, bar_value)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700804PRODUCT_COPY_FILES := $(call copy-files,$(wildcard foo*.mk),etc)
Sasha Smundak04453082021-08-17 18:14:41 -0700805PRODUCT_COPY_FILES := $(call product-copy-files-by-pattern,from/%,to/%,a b c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800806`,
807 expected: `load("//build/make/core:product_config.rbc", "rblf")
808
809def init(g, handle):
810 cfg = rblf.cfg(handle)
811 cfg["PRODUCT_COPY_FILES"] = rblf.addprefix("pfx-", "a b c")
812 cfg["PRODUCT_COPY_FILES"] = rblf.addsuffix(".sff", "a b c")
813 cfg["PRODUCT_NAME"] = ((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " ")).split()[0]
Sasha Smundak35434ed2021-11-05 16:29:56 -0700814 rblf.mkinfo("product.mk", rblf.mkpatsubst("%.pub", "%s%%" % cfg["PRODUCT_NAME"], g.get("PRODUCT_ADB_KEYS", "")))
Sasha Smundak16e07732021-07-23 11:38:23 -0700815 rblf.mkinfo("product.mk", rblf.dir("foo/bar"))
816 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][0])
817 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][-1])
818 rblf.mkinfo("product.mk", rblf.dir("product.mk"))
819 rblf.mkinfo("product.mk", rblf.dir(cfg["PRODUCT_COPY_FILES"][-1]))
820 rblf.mkinfo("product.mk", rblf.dir((_foobar).split()[-1]))
821 rblf.mkinfo("product.mk", rblf.abspath("foo/bar"))
822 rblf.mkinfo("product.mk", rblf.notdir("foo/bar"))
Sasha Smundak65b547e2021-09-17 15:35:41 -0700823 rblf.soong_config_namespace(g, "snsconfig")
824 rblf.soong_config_set(g, "snsconfig", "imagetype", "odm_image")
825 rblf.soong_config_set(g, "snsconfig", "foo", "foo_value")
826 rblf.soong_config_append(g, "snsconfig", "bar", "bar_value")
Sasha Smundak3deb9682021-07-26 18:42:25 -0700827 cfg["PRODUCT_COPY_FILES"] = rblf.copy_files(rblf.expand_wildcard("foo*.mk"), "etc")
Sasha Smundak04453082021-08-17 18:14:41 -0700828 cfg["PRODUCT_COPY_FILES"] = rblf.product_copy_files_by_pattern("from/%", "to/%", "a b c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800829`,
830 },
831 {
Sasha Smundak9d011ab2021-07-09 16:00:57 -0700832 desc: "subst in list",
833 mkname: "product.mk",
834 in: `
835files = $(call find-copy-subdir-files,*,from,to)
836PRODUCT_COPY_FILES += $(subst foo,bar,$(files))
837`,
838 expected: `load("//build/make/core:product_config.rbc", "rblf")
839
840def init(g, handle):
841 cfg = rblf.cfg(handle)
842 _files = rblf.find_and_copy("*", "from", "to")
843 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
844 cfg["PRODUCT_COPY_FILES"] += rblf.mksubst("foo", "bar", _files)
845`,
846 },
847 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800848 desc: "assignment flavors",
849 mkname: "product.mk",
850 in: `
851PRODUCT_LIST1 := a
852PRODUCT_LIST2 += a
853PRODUCT_LIST1 += b
854PRODUCT_LIST2 += b
855PRODUCT_LIST3 ?= a
856PRODUCT_LIST1 = c
857PLATFORM_LIST += x
858PRODUCT_PACKAGES := $(PLATFORM_LIST)
859`,
860 expected: `load("//build/make/core:product_config.rbc", "rblf")
861
862def init(g, handle):
863 cfg = rblf.cfg(handle)
864 cfg["PRODUCT_LIST1"] = ["a"]
865 rblf.setdefault(handle, "PRODUCT_LIST2")
866 cfg["PRODUCT_LIST2"] += ["a"]
867 cfg["PRODUCT_LIST1"] += ["b"]
868 cfg["PRODUCT_LIST2"] += ["b"]
869 if cfg.get("PRODUCT_LIST3") == None:
870 cfg["PRODUCT_LIST3"] = ["a"]
871 cfg["PRODUCT_LIST1"] = ["c"]
872 g.setdefault("PLATFORM_LIST", [])
873 g["PLATFORM_LIST"] += ["x"]
874 cfg["PRODUCT_PACKAGES"] = g["PLATFORM_LIST"][:]
875`,
876 },
877 {
878 desc: "assigment flavors2",
879 mkname: "product.mk",
880 in: `
881PRODUCT_LIST1 = a
882ifeq (0,1)
883 PRODUCT_LIST1 += b
884 PRODUCT_LIST2 += b
885endif
886PRODUCT_LIST1 += c
887PRODUCT_LIST2 += c
888`,
889 expected: `load("//build/make/core:product_config.rbc", "rblf")
890
891def init(g, handle):
892 cfg = rblf.cfg(handle)
893 cfg["PRODUCT_LIST1"] = ["a"]
894 if "0" == "1":
895 cfg["PRODUCT_LIST1"] += ["b"]
896 rblf.setdefault(handle, "PRODUCT_LIST2")
897 cfg["PRODUCT_LIST2"] += ["b"]
898 cfg["PRODUCT_LIST1"] += ["c"]
899 rblf.setdefault(handle, "PRODUCT_LIST2")
900 cfg["PRODUCT_LIST2"] += ["c"]
901`,
902 },
903 {
Sasha Smundak3deb9682021-07-26 18:42:25 -0700904 desc: "soong namespace assignments",
905 mkname: "product.mk",
906 in: `
907SOONG_CONFIG_NAMESPACES += cvd
908SOONG_CONFIG_cvd += launch_configs
Sasha Smundak65b547e2021-09-17 15:35:41 -0700909SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
Sasha Smundak3deb9682021-07-26 18:42:25 -0700910SOONG_CONFIG_cvd += grub_config
911SOONG_CONFIG_cvd_grub_config += grub.cfg
Sasha Smundak65b547e2021-09-17 15:35:41 -0700912x := $(SOONG_CONFIG_cvd_grub_config)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700913`,
914 expected: `load("//build/make/core:product_config.rbc", "rblf")
915
916def init(g, handle):
917 cfg = rblf.cfg(handle)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700918 rblf.soong_config_namespace(g, "cvd")
919 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
920 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
Sasha Smundak422b6142021-11-11 18:31:59 -0800921 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 -0700922`,
Cole Faustc00184e2021-11-08 12:08:57 -0800923 }, {
924 desc: "soong namespace accesses",
925 mkname: "product.mk",
926 in: `
927SOONG_CONFIG_NAMESPACES += cvd
928SOONG_CONFIG_cvd += launch_configs
929SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
930SOONG_CONFIG_cvd += grub_config
931SOONG_CONFIG_cvd_grub_config += grub.cfg
932x := $(call soong_config_get,cvd,grub_config)
933`,
934 expected: `load("//build/make/core:product_config.rbc", "rblf")
935
936def init(g, handle):
937 cfg = rblf.cfg(handle)
938 rblf.soong_config_namespace(g, "cvd")
939 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
940 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
941 _x = rblf.soong_config_get(g, "cvd", "grub_config")
942`,
Sasha Smundak3deb9682021-07-26 18:42:25 -0700943 },
944 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800945 desc: "string split",
946 mkname: "product.mk",
947 in: `
948PRODUCT_LIST1 = a
949local = b
950local += c
951FOO = d
952FOO += e
953PRODUCT_LIST1 += $(local)
954PRODUCT_LIST1 += $(FOO)
955`,
956 expected: `load("//build/make/core:product_config.rbc", "rblf")
957
958def init(g, handle):
959 cfg = rblf.cfg(handle)
960 cfg["PRODUCT_LIST1"] = ["a"]
961 _local = "b"
962 _local += " " + "c"
963 g["FOO"] = "d"
964 g["FOO"] += " " + "e"
965 cfg["PRODUCT_LIST1"] += (_local).split()
966 cfg["PRODUCT_LIST1"] += (g["FOO"]).split()
967`,
968 },
969 {
970 desc: "apex_jars",
971 mkname: "product.mk",
972 in: `
973PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex
974`,
975 expected: `load("//build/make/core:product_config.rbc", "rblf")
976
977def init(g, handle):
978 cfg = rblf.cfg(handle)
979 cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) +
980 ["framework-minus-apex"])
981`,
982 },
983 {
984 desc: "strip function",
985 mkname: "product.mk",
986 in: `
987ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),)
988 PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress)
989endif
990`,
991 expected: `load("//build/make/core:product_config.rbc", "rblf")
992
993def init(g, handle):
994 cfg = rblf.cfg(handle)
995 if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []):
Cole Faust816e0802022-03-04 12:04:31 -0800996 rblf.setdefault(handle, "PRODUCT_PACKAGES")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800997 cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split()
998`,
999 },
1000 {
1001 desc: "strip func in condition",
1002 mkname: "product.mk",
1003 in: `
1004ifneq ($(strip $(TARGET_VENDOR)),)
1005endif
1006`,
1007 expected: `load("//build/make/core:product_config.rbc", "rblf")
1008
1009def init(g, handle):
1010 cfg = rblf.cfg(handle)
Sasha Smundak0554d762021-07-08 18:26:12 -07001011 if rblf.mkstrip(g.get("TARGET_VENDOR", "")):
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001012 pass
1013`,
1014 },
1015 {
1016 desc: "ref after set",
1017 mkname: "product.mk",
1018 in: `
1019PRODUCT_ADB_KEYS:=value
1020FOO := $(PRODUCT_ADB_KEYS)
1021ifneq (,$(PRODUCT_ADB_KEYS))
1022endif
1023`,
1024 expected: `load("//build/make/core:product_config.rbc", "rblf")
1025
1026def init(g, handle):
1027 cfg = rblf.cfg(handle)
1028 g["PRODUCT_ADB_KEYS"] = "value"
1029 g["FOO"] = g["PRODUCT_ADB_KEYS"]
1030 if g["PRODUCT_ADB_KEYS"]:
1031 pass
1032`,
1033 },
1034 {
1035 desc: "ref before set",
1036 mkname: "product.mk",
1037 in: `
1038V1 := $(PRODUCT_ADB_KEYS)
1039ifeq (,$(PRODUCT_ADB_KEYS))
1040 V2 := $(PRODUCT_ADB_KEYS)
1041 PRODUCT_ADB_KEYS:=foo
1042 V3 := $(PRODUCT_ADB_KEYS)
1043endif`,
1044 expected: `load("//build/make/core:product_config.rbc", "rblf")
1045
1046def init(g, handle):
1047 cfg = rblf.cfg(handle)
1048 g["V1"] = g.get("PRODUCT_ADB_KEYS", "")
1049 if not g.get("PRODUCT_ADB_KEYS", ""):
1050 g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
1051 g["PRODUCT_ADB_KEYS"] = "foo"
1052 g["V3"] = g["PRODUCT_ADB_KEYS"]
1053`,
1054 },
Sasha Smundak6609ba72021-07-22 18:32:56 -07001055 {
1056 desc: "Dynamic inherit path",
1057 mkname: "product.mk",
1058 in: `
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001059MY_PATH:=foo
Sasha Smundak6609ba72021-07-22 18:32:56 -07001060$(call inherit-product,vendor/$(MY_PATH)/cfg.mk)
1061`,
1062 expected: `load("//build/make/core:product_config.rbc", "rblf")
1063load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1064load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init")
1065
1066def init(g, handle):
1067 cfg = rblf.cfg(handle)
1068 g["MY_PATH"] = "foo"
1069 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001070 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1071 "vendor/bar/baz/cfg.mk": ("vendor/bar/baz/cfg", _cfg1_init),
Sasha Smundak6609ba72021-07-22 18:32:56 -07001072 }.get("vendor/%s/cfg.mk" % g["MY_PATH"])
1073 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1074 if not _varmod_init:
Cole Faust7321b092021-12-21 16:11:16 -08001075 rblf.mkerror("product.mk", "Cannot find %s" % ("vendor/%s/cfg.mk" % g["MY_PATH"]))
Sasha Smundak6609ba72021-07-22 18:32:56 -07001076 rblf.inherit(handle, _varmod, _varmod_init)
1077`,
1078 },
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001079 {
1080 desc: "Dynamic inherit with hint",
1081 mkname: "product.mk",
1082 in: `
1083MY_PATH:=foo
1084#RBC# include_top vendor/foo1
1085$(call inherit-product,$(MY_PATH)/cfg.mk)
1086`,
1087 expected: `load("//build/make/core:product_config.rbc", "rblf")
1088load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1089
1090def init(g, handle):
1091 cfg = rblf.cfg(handle)
1092 g["MY_PATH"] = "foo"
Cole Faust93f8d392022-03-02 13:31:30 -08001093 _entry = {
1094 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1095 }.get("%s/cfg.mk" % g["MY_PATH"])
1096 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1097 if not _varmod_init:
1098 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1099 rblf.inherit(handle, _varmod, _varmod_init)
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001100`,
1101 },
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001102 {
Cole Faustf7ed5342021-12-21 14:15:12 -08001103 desc: "Dynamic inherit with duplicated hint",
1104 mkname: "product.mk",
1105 in: `
1106MY_PATH:=foo
1107#RBC# include_top vendor/foo1
1108$(call inherit-product,$(MY_PATH)/cfg.mk)
1109#RBC# include_top vendor/foo1
Cole Faust7940c6a2022-01-31 15:54:05 -08001110#RBC# include_top vendor/foo1
Cole Faustf7ed5342021-12-21 14:15:12 -08001111$(call inherit-product,$(MY_PATH)/cfg.mk)
1112`,
1113 expected: `load("//build/make/core:product_config.rbc", "rblf")
1114load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1115
1116def init(g, handle):
1117 cfg = rblf.cfg(handle)
1118 g["MY_PATH"] = "foo"
Cole Faust93f8d392022-03-02 13:31:30 -08001119 _entry = {
1120 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1121 }.get("%s/cfg.mk" % g["MY_PATH"])
1122 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1123 if not _varmod_init:
1124 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1125 rblf.inherit(handle, _varmod, _varmod_init)
1126 _entry = {
1127 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1128 }.get("%s/cfg.mk" % g["MY_PATH"])
1129 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1130 if not _varmod_init:
1131 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1132 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faustf7ed5342021-12-21 14:15:12 -08001133`,
1134 },
1135 {
Cole Faust069aba62022-01-26 17:47:33 -08001136 desc: "Dynamic inherit path that lacks hint",
Cole Faust6c934f62022-01-06 15:51:12 -08001137 mkname: "product.mk",
1138 in: `
1139#RBC# include_top foo
1140$(call inherit-product,$(MY_VAR)/font.mk)
1141
1142#RBC# include_top foo
1143
1144# There's some space and even this comment between the include_top and the inherit-product
1145
1146$(call inherit-product,$(MY_VAR)/font.mk)
1147
1148$(call inherit-product,$(MY_VAR)/font.mk)
1149`,
Cole Faust7940c6a2022-01-31 15:54:05 -08001150 expected: `load("//build/make/core:product_config.rbc", "rblf")
Cole Faust6c934f62022-01-06 15:51:12 -08001151load("//foo:font.star|init", _font_init = "init")
Cole Faust069aba62022-01-26 17:47:33 -08001152load("//bar:font.star|init", _font1_init = "init")
Cole Faust6c934f62022-01-06 15:51:12 -08001153
1154def init(g, handle):
1155 cfg = rblf.cfg(handle)
Cole Faust93f8d392022-03-02 13:31:30 -08001156 _entry = {
1157 "foo/font.mk": ("foo/font", _font_init),
1158 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1159 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1160 if not _varmod_init:
1161 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1162 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001163 # There's some space and even this comment between the include_top and the inherit-product
Cole Faust93f8d392022-03-02 13:31:30 -08001164 _entry = {
1165 "foo/font.mk": ("foo/font", _font_init),
1166 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1167 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1168 if not _varmod_init:
1169 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1170 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faustf4e72cf2022-02-08 12:49:37 -08001171 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 -08001172 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001173 "foo/font.mk": ("foo/font", _font_init),
Cole Faust069aba62022-01-26 17:47:33 -08001174 "bar/font.mk": ("bar/font", _font1_init),
Cole Faust6c934f62022-01-06 15:51:12 -08001175 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1176 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1177 if not _varmod_init:
1178 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1179 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001180`,
1181 },
1182 {
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001183 desc: "Ignore make rules",
1184 mkname: "product.mk",
1185 in: `
1186foo: foo.c
1187 gcc -o $@ $*`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001188 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001189
1190def init(g, handle):
1191 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001192 rblf.mk2rbc_error("product.mk:2", "unsupported line rule: foo: foo.c\n#gcc -o $@ $*")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001193`,
1194 },
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001195 {
1196 desc: "Flag override",
1197 mkname: "product.mk",
1198 in: `
1199override FOO:=`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001200 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001201
1202def init(g, handle):
1203 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001204 rblf.mk2rbc_error("product.mk:2", "cannot handle override directive")
Sasha Smundak422b6142021-11-11 18:31:59 -08001205`,
1206 },
1207 {
1208 desc: "Bad expression",
1209 mkname: "build/product.mk",
1210 in: `
1211ifeq (,$(call foobar))
1212endif
1213`,
1214 expected: `load("//build/make/core:product_config.rbc", "rblf")
1215
1216def init(g, handle):
1217 cfg = rblf.cfg(handle)
1218 if rblf.mk2rbc_error("build/product.mk:2", "cannot handle invoking foobar"):
1219 pass
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001220`,
1221 },
Cole Faust4eadba72021-12-07 11:54:52 -08001222 {
1223 desc: "if expression",
1224 mkname: "product.mk",
1225 in: `
1226TEST_VAR := foo
1227TEST_VAR_LIST := foo
1228TEST_VAR_LIST += bar
1229TEST_VAR_2 := $(if $(TEST_VAR),bar)
1230TEST_VAR_3 := $(if $(TEST_VAR),bar,baz)
1231TEST_VAR_3 := $(if $(TEST_VAR),$(TEST_VAR_LIST))
1232`,
1233 expected: `load("//build/make/core:product_config.rbc", "rblf")
1234
1235def init(g, handle):
1236 cfg = rblf.cfg(handle)
1237 g["TEST_VAR"] = "foo"
1238 g["TEST_VAR_LIST"] = ["foo"]
1239 g["TEST_VAR_LIST"] += ["bar"]
1240 g["TEST_VAR_2"] = ("bar" if g["TEST_VAR"] else "")
1241 g["TEST_VAR_3"] = ("bar" if g["TEST_VAR"] else "baz")
1242 g["TEST_VAR_3"] = (g["TEST_VAR_LIST"] if g["TEST_VAR"] else [])
1243`,
1244 },
Cole Faustc36c9622021-12-07 15:20:45 -08001245 {
1246 desc: "substitution references",
1247 mkname: "product.mk",
1248 in: `
1249SOURCES := foo.c bar.c
1250OBJECTS := $(SOURCES:.c=.o)
1251OBJECTS2 := $(SOURCES:%.c=%.o)
1252`,
1253 expected: `load("//build/make/core:product_config.rbc", "rblf")
1254
1255def init(g, handle):
1256 cfg = rblf.cfg(handle)
1257 g["SOURCES"] = "foo.c bar.c"
1258 g["OBJECTS"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1259 g["OBJECTS2"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1260`,
1261 },
Cole Faustb0d32ab2021-12-09 14:00:59 -08001262 {
1263 desc: "foreach expressions",
1264 mkname: "product.mk",
1265 in: `
1266BOOT_KERNEL_MODULES := foo.ko bar.ko
1267BOOT_KERNEL_MODULES_FILTER := $(foreach m,$(BOOT_KERNEL_MODULES),%/$(m))
1268BOOT_KERNEL_MODULES_LIST := foo.ko
1269BOOT_KERNEL_MODULES_LIST += bar.ko
1270BOOT_KERNEL_MODULES_FILTER_2 := $(foreach m,$(BOOT_KERNEL_MODULES_LIST),%/$(m))
1271
Cole Faustb67aa082022-02-28 16:39:59 -08001272FOREACH_WITH_IF := $(foreach module,\
1273 $(BOOT_KERNEL_MODULES_LIST),\
1274 $(if $(filter $(module),foo.ko),,$(error module "$(module)" has an error!)))
Cole Faustb0d32ab2021-12-09 14:00:59 -08001275`,
1276 expected: `load("//build/make/core:product_config.rbc", "rblf")
1277
1278def init(g, handle):
1279 cfg = rblf.cfg(handle)
1280 g["BOOT_KERNEL_MODULES"] = "foo.ko bar.ko"
1281 g["BOOT_KERNEL_MODULES_FILTER"] = ["%%/%s" % m for m in rblf.words(g["BOOT_KERNEL_MODULES"])]
1282 g["BOOT_KERNEL_MODULES_LIST"] = ["foo.ko"]
1283 g["BOOT_KERNEL_MODULES_LIST"] += ["bar.ko"]
1284 g["BOOT_KERNEL_MODULES_FILTER_2"] = ["%%/%s" % m for m in g["BOOT_KERNEL_MODULES_LIST"]]
Cole Faustb67aa082022-02-28 16:39:59 -08001285 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 Faustb0d32ab2021-12-09 14:00:59 -08001286`,
1287 },
Cole Faust0484c232021-12-22 14:08:08 -08001288 {
1289 desc: "List appended to string",
1290 mkname: "product.mk",
1291 in: `
1292NATIVE_BRIDGE_PRODUCT_PACKAGES := \
1293 libnative_bridge_vdso.native_bridge \
1294 native_bridge_guest_app_process.native_bridge \
1295 native_bridge_guest_linker.native_bridge
1296
1297NATIVE_BRIDGE_MODIFIED_GUEST_LIBS := \
1298 libaaudio \
1299 libamidi \
1300 libandroid \
1301 libandroid_runtime
1302
1303NATIVE_BRIDGE_PRODUCT_PACKAGES += \
1304 $(addsuffix .native_bridge,$(NATIVE_BRIDGE_ORIG_GUEST_LIBS))
1305`,
1306 expected: `load("//build/make/core:product_config.rbc", "rblf")
1307
1308def init(g, handle):
1309 cfg = rblf.cfg(handle)
1310 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] = "libnative_bridge_vdso.native_bridge native_bridge_guest_app_process.native_bridge native_bridge_guest_linker.native_bridge"
1311 g["NATIVE_BRIDGE_MODIFIED_GUEST_LIBS"] = "libaaudio libamidi libandroid libandroid_runtime"
1312 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] += " " + " ".join(rblf.addsuffix(".native_bridge", g.get("NATIVE_BRIDGE_ORIG_GUEST_LIBS", "")))
1313`,
1314 },
Cole Faustb1103e22022-01-06 15:22:05 -08001315 {
1316 desc: "Math functions",
1317 mkname: "product.mk",
1318 in: `
1319# Test the math functions defined in build/make/common/math.mk
1320ifeq ($(call math_max,2,5),5)
1321endif
1322ifeq ($(call math_min,2,5),2)
1323endif
1324ifeq ($(call math_gt_or_eq,2,5),true)
1325endif
1326ifeq ($(call math_gt,2,5),true)
1327endif
1328ifeq ($(call math_lt,2,5),true)
1329endif
1330ifeq ($(call math_gt_or_eq,2,5),)
1331endif
1332ifeq ($(call math_gt,2,5),)
1333endif
1334ifeq ($(call math_lt,2,5),)
1335endif
1336ifeq ($(call math_gt_or_eq,$(MY_VAR), 5),true)
1337endif
1338ifeq ($(call math_gt_or_eq,$(MY_VAR),$(MY_OTHER_VAR)),true)
1339endif
1340ifeq ($(call math_gt_or_eq,100$(MY_VAR),10),true)
1341endif
1342`,
1343 expected: `# Test the math functions defined in build/make/common/math.mk
1344load("//build/make/core:product_config.rbc", "rblf")
1345
1346def init(g, handle):
1347 cfg = rblf.cfg(handle)
1348 if max(2, 5) == 5:
1349 pass
1350 if min(2, 5) == 2:
1351 pass
1352 if 2 >= 5:
1353 pass
1354 if 2 > 5:
1355 pass
1356 if 2 < 5:
1357 pass
1358 if 2 < 5:
1359 pass
1360 if 2 <= 5:
1361 pass
1362 if 2 >= 5:
1363 pass
1364 if int(g.get("MY_VAR", "")) >= 5:
1365 pass
1366 if int(g.get("MY_VAR", "")) >= int(g.get("MY_OTHER_VAR", "")):
1367 pass
1368 if int("100%s" % g.get("MY_VAR", "")) >= 10:
1369 pass
1370`,
1371 },
Cole Faustf92c9f22022-03-14 14:35:50 -07001372 {
1373 desc: "Type hints",
1374 mkname: "product.mk",
1375 in: `
1376# Test type hints
1377#RBC# type_hint list MY_VAR MY_VAR_2
1378# Unsupported type
1379#RBC# type_hint bool MY_VAR_3
1380# Invalid syntax
1381#RBC# type_hint list
1382# Duplicated variable
1383#RBC# type_hint list MY_VAR_2
1384#RBC# type_hint list my-local-var-with-dashes
1385
1386MY_VAR := foo
1387MY_VAR_UNHINTED := foo
1388
1389# Vars set after other statements still get the hint
1390MY_VAR_2 := foo
1391
1392# You can't specify a type hint after the first statement
1393#RBC# type_hint list MY_VAR_4
1394MY_VAR_4 := foo
1395
1396my-local-var-with-dashes := foo
1397`,
1398 expected: `# Test type hints
1399# Unsupported type
1400load("//build/make/core:product_config.rbc", "rblf")
1401
1402def init(g, handle):
1403 cfg = rblf.cfg(handle)
1404 rblf.mk2rbc_error("product.mk:5", "Invalid type_hint annotation. Only list/string types are accepted, found bool")
1405 # Invalid syntax
1406 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")
1407 # Duplicated variable
1408 rblf.mk2rbc_error("product.mk:9", "Duplicate type hint for variable MY_VAR_2")
1409 g["MY_VAR"] = ["foo"]
1410 g["MY_VAR_UNHINTED"] = "foo"
1411 # Vars set after other statements still get the hint
1412 g["MY_VAR_2"] = ["foo"]
1413 # You can't specify a type hint after the first statement
1414 rblf.mk2rbc_error("product.mk:19", "type_hint annotations must come before the first Makefile statement")
1415 g["MY_VAR_4"] = "foo"
1416 _my_local_var_with_dashes = ["foo"]
1417`,
1418 },
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001419}
1420
1421var known_variables = []struct {
1422 name string
1423 class varClass
1424 starlarkType
1425}{
Cole Faustf1f44d32021-11-16 14:52:12 -08001426 {"NATIVE_COVERAGE", VarClassSoong, starlarkTypeBool},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001427 {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
1428 {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
1429 {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
1430 {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
1431 {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
1432 {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
1433 {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
1434 {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
1435 {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
1436 {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
1437 {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
1438 {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
1439 {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
1440 {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
1441}
1442
Sasha Smundak6609ba72021-07-22 18:32:56 -07001443type testMakefileFinder struct {
1444 fs fs.FS
1445 root string
1446 files []string
1447}
1448
1449func (t *testMakefileFinder) Find(root string) []string {
1450 if t.files != nil || root == t.root {
1451 return t.files
1452 }
1453 t.files = make([]string, 0)
1454 fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error {
1455 if err != nil {
1456 return err
1457 }
1458 if d.IsDir() {
1459 base := filepath.Base(path)
1460 if base[0] == '.' && len(base) > 1 {
1461 return fs.SkipDir
1462 }
1463 return nil
1464 }
1465 if strings.HasSuffix(path, ".mk") {
1466 t.files = append(t.files, path)
1467 }
1468 return nil
1469 })
1470 return t.files
1471}
1472
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001473func TestGood(t *testing.T) {
1474 for _, v := range known_variables {
1475 KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
1476 }
Sasha Smundak6609ba72021-07-22 18:32:56 -07001477 fs := NewFindMockFS([]string{
1478 "vendor/foo1/cfg.mk",
1479 "vendor/bar/baz/cfg.mk",
1480 "part.mk",
1481 "foo/font.mk",
1482 "bar/font.mk",
1483 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001484 for _, test := range testCases {
1485 t.Run(test.desc,
1486 func(t *testing.T) {
1487 ss, err := Convert(Request{
Sasha Smundak422b6142021-11-11 18:31:59 -08001488 MkFile: test.mkname,
1489 Reader: bytes.NewBufferString(test.in),
Sasha Smundak422b6142021-11-11 18:31:59 -08001490 OutputSuffix: ".star",
1491 SourceFS: fs,
1492 MakefileFinder: &testMakefileFinder{fs: fs},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001493 })
1494 if err != nil {
1495 t.Error(err)
1496 return
1497 }
1498 got := ss.String()
1499 if got != test.expected {
1500 t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
1501 strings.ReplaceAll(test.expected, "\n", "␤\n"),
1502 strings.ReplaceAll(got, "\n", "␤\n"))
1503 }
1504 })
1505 }
1506}