blob: 35c54d268d2043e903bc9a7f76854bea85ba92a2 [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 {
Cole Fauste2a37982022-03-09 16:00:17 -0800904 desc: "assigment setdefaults",
905 mkname: "product.mk",
906 in: `
907# All of these should have a setdefault because they're self-referential and not defined before
908PRODUCT_LIST1 = a $(PRODUCT_LIST1)
909PRODUCT_LIST2 ?= a $(PRODUCT_LIST2)
910PRODUCT_LIST3 += a
911
912# Now doing them again should not have a setdefault because they've already been set
913PRODUCT_LIST1 = a $(PRODUCT_LIST1)
914PRODUCT_LIST2 ?= a $(PRODUCT_LIST2)
915PRODUCT_LIST3 += a
916`,
917 expected: `# All of these should have a setdefault because they're self-referential and not defined before
918load("//build/make/core:product_config.rbc", "rblf")
919
920def init(g, handle):
921 cfg = rblf.cfg(handle)
922 rblf.setdefault(handle, "PRODUCT_LIST1")
923 cfg["PRODUCT_LIST1"] = (["a"] +
924 cfg.get("PRODUCT_LIST1", []))
925 if cfg.get("PRODUCT_LIST2") == None:
926 rblf.setdefault(handle, "PRODUCT_LIST2")
927 cfg["PRODUCT_LIST2"] = (["a"] +
928 cfg.get("PRODUCT_LIST2", []))
929 rblf.setdefault(handle, "PRODUCT_LIST3")
930 cfg["PRODUCT_LIST3"] += ["a"]
931 # Now doing them again should not have a setdefault because they've already been set
932 cfg["PRODUCT_LIST1"] = (["a"] +
933 cfg["PRODUCT_LIST1"])
934 if cfg.get("PRODUCT_LIST2") == None:
935 cfg["PRODUCT_LIST2"] = (["a"] +
936 cfg["PRODUCT_LIST2"])
937 cfg["PRODUCT_LIST3"] += ["a"]
938`,
939 },
940 {
Sasha Smundak3deb9682021-07-26 18:42:25 -0700941 desc: "soong namespace assignments",
942 mkname: "product.mk",
943 in: `
944SOONG_CONFIG_NAMESPACES += cvd
945SOONG_CONFIG_cvd += launch_configs
Sasha Smundak65b547e2021-09-17 15:35:41 -0700946SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
Sasha Smundak3deb9682021-07-26 18:42:25 -0700947SOONG_CONFIG_cvd += grub_config
948SOONG_CONFIG_cvd_grub_config += grub.cfg
Sasha Smundak65b547e2021-09-17 15:35:41 -0700949x := $(SOONG_CONFIG_cvd_grub_config)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700950`,
951 expected: `load("//build/make/core:product_config.rbc", "rblf")
952
953def init(g, handle):
954 cfg = rblf.cfg(handle)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700955 rblf.soong_config_namespace(g, "cvd")
956 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
957 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
Sasha Smundak422b6142021-11-11 18:31:59 -0800958 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 -0700959`,
Cole Faustc00184e2021-11-08 12:08:57 -0800960 }, {
961 desc: "soong namespace accesses",
962 mkname: "product.mk",
963 in: `
964SOONG_CONFIG_NAMESPACES += cvd
965SOONG_CONFIG_cvd += launch_configs
966SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
967SOONG_CONFIG_cvd += grub_config
968SOONG_CONFIG_cvd_grub_config += grub.cfg
969x := $(call soong_config_get,cvd,grub_config)
970`,
971 expected: `load("//build/make/core:product_config.rbc", "rblf")
972
973def init(g, handle):
974 cfg = rblf.cfg(handle)
975 rblf.soong_config_namespace(g, "cvd")
976 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
977 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
978 _x = rblf.soong_config_get(g, "cvd", "grub_config")
979`,
Sasha Smundak3deb9682021-07-26 18:42:25 -0700980 },
981 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800982 desc: "string split",
983 mkname: "product.mk",
984 in: `
985PRODUCT_LIST1 = a
986local = b
987local += c
988FOO = d
989FOO += e
990PRODUCT_LIST1 += $(local)
991PRODUCT_LIST1 += $(FOO)
992`,
993 expected: `load("//build/make/core:product_config.rbc", "rblf")
994
995def init(g, handle):
996 cfg = rblf.cfg(handle)
997 cfg["PRODUCT_LIST1"] = ["a"]
998 _local = "b"
999 _local += " " + "c"
1000 g["FOO"] = "d"
1001 g["FOO"] += " " + "e"
1002 cfg["PRODUCT_LIST1"] += (_local).split()
1003 cfg["PRODUCT_LIST1"] += (g["FOO"]).split()
1004`,
1005 },
1006 {
1007 desc: "apex_jars",
1008 mkname: "product.mk",
1009 in: `
1010PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex
1011`,
1012 expected: `load("//build/make/core:product_config.rbc", "rblf")
1013
1014def init(g, handle):
1015 cfg = rblf.cfg(handle)
1016 cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) +
1017 ["framework-minus-apex"])
1018`,
1019 },
1020 {
1021 desc: "strip function",
1022 mkname: "product.mk",
1023 in: `
1024ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),)
1025 PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress)
1026endif
1027`,
1028 expected: `load("//build/make/core:product_config.rbc", "rblf")
1029
1030def init(g, handle):
1031 cfg = rblf.cfg(handle)
1032 if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []):
Cole Faust816e0802022-03-04 12:04:31 -08001033 rblf.setdefault(handle, "PRODUCT_PACKAGES")
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001034 cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split()
1035`,
1036 },
1037 {
1038 desc: "strip func in condition",
1039 mkname: "product.mk",
1040 in: `
1041ifneq ($(strip $(TARGET_VENDOR)),)
1042endif
1043`,
1044 expected: `load("//build/make/core:product_config.rbc", "rblf")
1045
1046def init(g, handle):
1047 cfg = rblf.cfg(handle)
Sasha Smundak0554d762021-07-08 18:26:12 -07001048 if rblf.mkstrip(g.get("TARGET_VENDOR", "")):
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001049 pass
1050`,
1051 },
1052 {
1053 desc: "ref after set",
1054 mkname: "product.mk",
1055 in: `
1056PRODUCT_ADB_KEYS:=value
1057FOO := $(PRODUCT_ADB_KEYS)
1058ifneq (,$(PRODUCT_ADB_KEYS))
1059endif
1060`,
1061 expected: `load("//build/make/core:product_config.rbc", "rblf")
1062
1063def init(g, handle):
1064 cfg = rblf.cfg(handle)
1065 g["PRODUCT_ADB_KEYS"] = "value"
1066 g["FOO"] = g["PRODUCT_ADB_KEYS"]
1067 if g["PRODUCT_ADB_KEYS"]:
1068 pass
1069`,
1070 },
1071 {
1072 desc: "ref before set",
1073 mkname: "product.mk",
1074 in: `
1075V1 := $(PRODUCT_ADB_KEYS)
1076ifeq (,$(PRODUCT_ADB_KEYS))
1077 V2 := $(PRODUCT_ADB_KEYS)
1078 PRODUCT_ADB_KEYS:=foo
1079 V3 := $(PRODUCT_ADB_KEYS)
1080endif`,
1081 expected: `load("//build/make/core:product_config.rbc", "rblf")
1082
1083def init(g, handle):
1084 cfg = rblf.cfg(handle)
1085 g["V1"] = g.get("PRODUCT_ADB_KEYS", "")
1086 if not g.get("PRODUCT_ADB_KEYS", ""):
1087 g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
1088 g["PRODUCT_ADB_KEYS"] = "foo"
1089 g["V3"] = g["PRODUCT_ADB_KEYS"]
1090`,
1091 },
Sasha Smundak6609ba72021-07-22 18:32:56 -07001092 {
1093 desc: "Dynamic inherit path",
1094 mkname: "product.mk",
1095 in: `
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001096MY_PATH:=foo
Sasha Smundak6609ba72021-07-22 18:32:56 -07001097$(call inherit-product,vendor/$(MY_PATH)/cfg.mk)
1098`,
1099 expected: `load("//build/make/core:product_config.rbc", "rblf")
1100load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1101load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init")
1102
1103def init(g, handle):
1104 cfg = rblf.cfg(handle)
1105 g["MY_PATH"] = "foo"
1106 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001107 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1108 "vendor/bar/baz/cfg.mk": ("vendor/bar/baz/cfg", _cfg1_init),
Sasha Smundak6609ba72021-07-22 18:32:56 -07001109 }.get("vendor/%s/cfg.mk" % g["MY_PATH"])
1110 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1111 if not _varmod_init:
Cole Faust7321b092021-12-21 16:11:16 -08001112 rblf.mkerror("product.mk", "Cannot find %s" % ("vendor/%s/cfg.mk" % g["MY_PATH"]))
Sasha Smundak6609ba72021-07-22 18:32:56 -07001113 rblf.inherit(handle, _varmod, _varmod_init)
1114`,
1115 },
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001116 {
1117 desc: "Dynamic inherit with hint",
1118 mkname: "product.mk",
1119 in: `
1120MY_PATH:=foo
1121#RBC# include_top vendor/foo1
1122$(call inherit-product,$(MY_PATH)/cfg.mk)
1123`,
1124 expected: `load("//build/make/core:product_config.rbc", "rblf")
1125load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1126
1127def init(g, handle):
1128 cfg = rblf.cfg(handle)
1129 g["MY_PATH"] = "foo"
Cole Faust93f8d392022-03-02 13:31:30 -08001130 _entry = {
1131 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1132 }.get("%s/cfg.mk" % g["MY_PATH"])
1133 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1134 if not _varmod_init:
1135 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1136 rblf.inherit(handle, _varmod, _varmod_init)
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001137`,
1138 },
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001139 {
Cole Faustf7ed5342021-12-21 14:15:12 -08001140 desc: "Dynamic inherit with duplicated hint",
1141 mkname: "product.mk",
1142 in: `
1143MY_PATH:=foo
1144#RBC# include_top vendor/foo1
1145$(call inherit-product,$(MY_PATH)/cfg.mk)
1146#RBC# include_top vendor/foo1
Cole Faust7940c6a2022-01-31 15:54:05 -08001147#RBC# include_top vendor/foo1
Cole Faustf7ed5342021-12-21 14:15:12 -08001148$(call inherit-product,$(MY_PATH)/cfg.mk)
1149`,
1150 expected: `load("//build/make/core:product_config.rbc", "rblf")
1151load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1152
1153def init(g, handle):
1154 cfg = rblf.cfg(handle)
1155 g["MY_PATH"] = "foo"
Cole Faust93f8d392022-03-02 13:31:30 -08001156 _entry = {
1157 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1158 }.get("%s/cfg.mk" % g["MY_PATH"])
1159 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1160 if not _varmod_init:
1161 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1162 rblf.inherit(handle, _varmod, _varmod_init)
1163 _entry = {
1164 "vendor/foo1/cfg.mk": ("vendor/foo1/cfg", _cfg_init),
1165 }.get("%s/cfg.mk" % g["MY_PATH"])
1166 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1167 if not _varmod_init:
1168 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/cfg.mk" % g["MY_PATH"]))
1169 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faustf7ed5342021-12-21 14:15:12 -08001170`,
1171 },
1172 {
Cole Faust069aba62022-01-26 17:47:33 -08001173 desc: "Dynamic inherit path that lacks hint",
Cole Faust6c934f62022-01-06 15:51:12 -08001174 mkname: "product.mk",
1175 in: `
1176#RBC# include_top foo
1177$(call inherit-product,$(MY_VAR)/font.mk)
1178
1179#RBC# include_top foo
1180
1181# There's some space and even this comment between the include_top and the inherit-product
1182
1183$(call inherit-product,$(MY_VAR)/font.mk)
1184
1185$(call inherit-product,$(MY_VAR)/font.mk)
1186`,
Cole Faust7940c6a2022-01-31 15:54:05 -08001187 expected: `load("//build/make/core:product_config.rbc", "rblf")
Cole Faust6c934f62022-01-06 15:51:12 -08001188load("//foo:font.star|init", _font_init = "init")
Cole Faust069aba62022-01-26 17:47:33 -08001189load("//bar:font.star|init", _font1_init = "init")
Cole Faust6c934f62022-01-06 15:51:12 -08001190
1191def init(g, handle):
1192 cfg = rblf.cfg(handle)
Cole Faust93f8d392022-03-02 13:31:30 -08001193 _entry = {
1194 "foo/font.mk": ("foo/font", _font_init),
1195 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1196 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1197 if not _varmod_init:
1198 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1199 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001200 # There's some space and even this comment between the include_top and the inherit-product
Cole Faust93f8d392022-03-02 13:31:30 -08001201 _entry = {
1202 "foo/font.mk": ("foo/font", _font_init),
1203 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1204 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1205 if not _varmod_init:
1206 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1207 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faustf4e72cf2022-02-08 12:49:37 -08001208 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 -08001209 _entry = {
Sasha Smundak845cb292022-01-18 10:31:14 -08001210 "foo/font.mk": ("foo/font", _font_init),
Cole Faust069aba62022-01-26 17:47:33 -08001211 "bar/font.mk": ("bar/font", _font1_init),
Cole Faust6c934f62022-01-06 15:51:12 -08001212 }.get("%s/font.mk" % g.get("MY_VAR", ""))
1213 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1214 if not _varmod_init:
1215 rblf.mkerror("product.mk", "Cannot find %s" % ("%s/font.mk" % g.get("MY_VAR", "")))
1216 rblf.inherit(handle, _varmod, _varmod_init)
Cole Faust6c934f62022-01-06 15:51:12 -08001217`,
1218 },
1219 {
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001220 desc: "Ignore make rules",
1221 mkname: "product.mk",
1222 in: `
1223foo: foo.c
1224 gcc -o $@ $*`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001225 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001226
1227def init(g, handle):
1228 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001229 rblf.mk2rbc_error("product.mk:2", "unsupported line rule: foo: foo.c\n#gcc -o $@ $*")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001230`,
1231 },
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001232 {
1233 desc: "Flag override",
1234 mkname: "product.mk",
1235 in: `
1236override FOO:=`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001237 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001238
1239def init(g, handle):
1240 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001241 rblf.mk2rbc_error("product.mk:2", "cannot handle override directive")
Sasha Smundak422b6142021-11-11 18:31:59 -08001242`,
1243 },
1244 {
1245 desc: "Bad expression",
1246 mkname: "build/product.mk",
1247 in: `
1248ifeq (,$(call foobar))
1249endif
1250`,
1251 expected: `load("//build/make/core:product_config.rbc", "rblf")
1252
1253def init(g, handle):
1254 cfg = rblf.cfg(handle)
1255 if rblf.mk2rbc_error("build/product.mk:2", "cannot handle invoking foobar"):
1256 pass
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001257`,
1258 },
Cole Faust4eadba72021-12-07 11:54:52 -08001259 {
1260 desc: "if expression",
1261 mkname: "product.mk",
1262 in: `
1263TEST_VAR := foo
1264TEST_VAR_LIST := foo
1265TEST_VAR_LIST += bar
1266TEST_VAR_2 := $(if $(TEST_VAR),bar)
1267TEST_VAR_3 := $(if $(TEST_VAR),bar,baz)
1268TEST_VAR_3 := $(if $(TEST_VAR),$(TEST_VAR_LIST))
1269`,
1270 expected: `load("//build/make/core:product_config.rbc", "rblf")
1271
1272def init(g, handle):
1273 cfg = rblf.cfg(handle)
1274 g["TEST_VAR"] = "foo"
1275 g["TEST_VAR_LIST"] = ["foo"]
1276 g["TEST_VAR_LIST"] += ["bar"]
1277 g["TEST_VAR_2"] = ("bar" if g["TEST_VAR"] else "")
1278 g["TEST_VAR_3"] = ("bar" if g["TEST_VAR"] else "baz")
1279 g["TEST_VAR_3"] = (g["TEST_VAR_LIST"] if g["TEST_VAR"] else [])
1280`,
1281 },
Cole Faustc36c9622021-12-07 15:20:45 -08001282 {
1283 desc: "substitution references",
1284 mkname: "product.mk",
1285 in: `
1286SOURCES := foo.c bar.c
1287OBJECTS := $(SOURCES:.c=.o)
1288OBJECTS2 := $(SOURCES:%.c=%.o)
1289`,
1290 expected: `load("//build/make/core:product_config.rbc", "rblf")
1291
1292def init(g, handle):
1293 cfg = rblf.cfg(handle)
1294 g["SOURCES"] = "foo.c bar.c"
1295 g["OBJECTS"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1296 g["OBJECTS2"] = rblf.mkpatsubst("%.c", "%.o", g["SOURCES"])
1297`,
1298 },
Cole Faustb0d32ab2021-12-09 14:00:59 -08001299 {
1300 desc: "foreach expressions",
1301 mkname: "product.mk",
1302 in: `
1303BOOT_KERNEL_MODULES := foo.ko bar.ko
1304BOOT_KERNEL_MODULES_FILTER := $(foreach m,$(BOOT_KERNEL_MODULES),%/$(m))
1305BOOT_KERNEL_MODULES_LIST := foo.ko
1306BOOT_KERNEL_MODULES_LIST += bar.ko
1307BOOT_KERNEL_MODULES_FILTER_2 := $(foreach m,$(BOOT_KERNEL_MODULES_LIST),%/$(m))
1308
Cole Faustb67aa082022-02-28 16:39:59 -08001309FOREACH_WITH_IF := $(foreach module,\
1310 $(BOOT_KERNEL_MODULES_LIST),\
1311 $(if $(filter $(module),foo.ko),,$(error module "$(module)" has an error!)))
Cole Faustb0d32ab2021-12-09 14:00:59 -08001312`,
1313 expected: `load("//build/make/core:product_config.rbc", "rblf")
1314
1315def init(g, handle):
1316 cfg = rblf.cfg(handle)
1317 g["BOOT_KERNEL_MODULES"] = "foo.ko bar.ko"
1318 g["BOOT_KERNEL_MODULES_FILTER"] = ["%%/%s" % m for m in rblf.words(g["BOOT_KERNEL_MODULES"])]
1319 g["BOOT_KERNEL_MODULES_LIST"] = ["foo.ko"]
1320 g["BOOT_KERNEL_MODULES_LIST"] += ["bar.ko"]
1321 g["BOOT_KERNEL_MODULES_FILTER_2"] = ["%%/%s" % m for m in g["BOOT_KERNEL_MODULES_LIST"]]
Cole Faustb67aa082022-02-28 16:39:59 -08001322 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 -08001323`,
1324 },
Cole Faust0484c232021-12-22 14:08:08 -08001325 {
1326 desc: "List appended to string",
1327 mkname: "product.mk",
1328 in: `
1329NATIVE_BRIDGE_PRODUCT_PACKAGES := \
1330 libnative_bridge_vdso.native_bridge \
1331 native_bridge_guest_app_process.native_bridge \
1332 native_bridge_guest_linker.native_bridge
1333
1334NATIVE_BRIDGE_MODIFIED_GUEST_LIBS := \
1335 libaaudio \
1336 libamidi \
1337 libandroid \
1338 libandroid_runtime
1339
1340NATIVE_BRIDGE_PRODUCT_PACKAGES += \
1341 $(addsuffix .native_bridge,$(NATIVE_BRIDGE_ORIG_GUEST_LIBS))
1342`,
1343 expected: `load("//build/make/core:product_config.rbc", "rblf")
1344
1345def init(g, handle):
1346 cfg = rblf.cfg(handle)
1347 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] = "libnative_bridge_vdso.native_bridge native_bridge_guest_app_process.native_bridge native_bridge_guest_linker.native_bridge"
1348 g["NATIVE_BRIDGE_MODIFIED_GUEST_LIBS"] = "libaaudio libamidi libandroid libandroid_runtime"
1349 g["NATIVE_BRIDGE_PRODUCT_PACKAGES"] += " " + " ".join(rblf.addsuffix(".native_bridge", g.get("NATIVE_BRIDGE_ORIG_GUEST_LIBS", "")))
1350`,
1351 },
Cole Faustb1103e22022-01-06 15:22:05 -08001352 {
1353 desc: "Math functions",
1354 mkname: "product.mk",
1355 in: `
1356# Test the math functions defined in build/make/common/math.mk
1357ifeq ($(call math_max,2,5),5)
1358endif
1359ifeq ($(call math_min,2,5),2)
1360endif
1361ifeq ($(call math_gt_or_eq,2,5),true)
1362endif
1363ifeq ($(call math_gt,2,5),true)
1364endif
1365ifeq ($(call math_lt,2,5),true)
1366endif
1367ifeq ($(call math_gt_or_eq,2,5),)
1368endif
1369ifeq ($(call math_gt,2,5),)
1370endif
1371ifeq ($(call math_lt,2,5),)
1372endif
1373ifeq ($(call math_gt_or_eq,$(MY_VAR), 5),true)
1374endif
1375ifeq ($(call math_gt_or_eq,$(MY_VAR),$(MY_OTHER_VAR)),true)
1376endif
1377ifeq ($(call math_gt_or_eq,100$(MY_VAR),10),true)
1378endif
1379`,
1380 expected: `# Test the math functions defined in build/make/common/math.mk
1381load("//build/make/core:product_config.rbc", "rblf")
1382
1383def init(g, handle):
1384 cfg = rblf.cfg(handle)
1385 if max(2, 5) == 5:
1386 pass
1387 if min(2, 5) == 2:
1388 pass
1389 if 2 >= 5:
1390 pass
1391 if 2 > 5:
1392 pass
1393 if 2 < 5:
1394 pass
1395 if 2 < 5:
1396 pass
1397 if 2 <= 5:
1398 pass
1399 if 2 >= 5:
1400 pass
1401 if int(g.get("MY_VAR", "")) >= 5:
1402 pass
1403 if int(g.get("MY_VAR", "")) >= int(g.get("MY_OTHER_VAR", "")):
1404 pass
1405 if int("100%s" % g.get("MY_VAR", "")) >= 10:
1406 pass
1407`,
1408 },
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001409}
1410
1411var known_variables = []struct {
1412 name string
1413 class varClass
1414 starlarkType
1415}{
Cole Faustf1f44d32021-11-16 14:52:12 -08001416 {"NATIVE_COVERAGE", VarClassSoong, starlarkTypeBool},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001417 {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
1418 {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
1419 {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
1420 {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
1421 {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
1422 {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
1423 {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
1424 {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
1425 {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
1426 {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
1427 {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
1428 {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
1429 {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
1430 {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
1431}
1432
Sasha Smundak6609ba72021-07-22 18:32:56 -07001433type testMakefileFinder struct {
1434 fs fs.FS
1435 root string
1436 files []string
1437}
1438
1439func (t *testMakefileFinder) Find(root string) []string {
1440 if t.files != nil || root == t.root {
1441 return t.files
1442 }
1443 t.files = make([]string, 0)
1444 fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error {
1445 if err != nil {
1446 return err
1447 }
1448 if d.IsDir() {
1449 base := filepath.Base(path)
1450 if base[0] == '.' && len(base) > 1 {
1451 return fs.SkipDir
1452 }
1453 return nil
1454 }
1455 if strings.HasSuffix(path, ".mk") {
1456 t.files = append(t.files, path)
1457 }
1458 return nil
1459 })
1460 return t.files
1461}
1462
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001463func TestGood(t *testing.T) {
1464 for _, v := range known_variables {
1465 KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
1466 }
Sasha Smundak6609ba72021-07-22 18:32:56 -07001467 fs := NewFindMockFS([]string{
1468 "vendor/foo1/cfg.mk",
1469 "vendor/bar/baz/cfg.mk",
1470 "part.mk",
1471 "foo/font.mk",
1472 "bar/font.mk",
1473 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001474 for _, test := range testCases {
1475 t.Run(test.desc,
1476 func(t *testing.T) {
1477 ss, err := Convert(Request{
Sasha Smundak422b6142021-11-11 18:31:59 -08001478 MkFile: test.mkname,
1479 Reader: bytes.NewBufferString(test.in),
Sasha Smundak422b6142021-11-11 18:31:59 -08001480 OutputSuffix: ".star",
1481 SourceFS: fs,
1482 MakefileFinder: &testMakefileFinder{fs: fs},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001483 })
1484 if err != nil {
1485 t.Error(err)
1486 return
1487 }
1488 got := ss.String()
1489 if got != test.expected {
1490 t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
1491 strings.ReplaceAll(test.expected, "\n", "␤\n"),
1492 strings.ReplaceAll(got, "\n", "␤\n"))
1493 }
1494 })
1495 }
1496}