blob: 22490a2f57fde42fe8c38b34ecfdc4f6a7193b02 [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
68`,
69 expected: `load("//build/make/core:product_config.rbc", "rblf")
70
71def init(g, handle):
72 cfg = rblf.cfg(handle)
73 cfg["PRODUCT_NAME"] = "Pixel 3"
74 cfg["PRODUCT_MODEL"] = ""
75 _local_var = "foo"
76`,
77 },
78 {
79 desc: "List variable",
80 mkname: "pixel4.mk",
81 in: `
82PRODUCT_PACKAGES = package1 package2
83PRODUCT_COPY_FILES += file2:target
84PRODUCT_PACKAGES += package3
85PRODUCT_COPY_FILES =
86`,
87 expected: `load("//build/make/core:product_config.rbc", "rblf")
88
89def init(g, handle):
90 cfg = rblf.cfg(handle)
91 cfg["PRODUCT_PACKAGES"] = [
92 "package1",
93 "package2",
94 ]
95 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
96 cfg["PRODUCT_COPY_FILES"] += ["file2:target"]
97 cfg["PRODUCT_PACKAGES"] += ["package3"]
98 cfg["PRODUCT_COPY_FILES"] = []
99`,
100 },
101 {
102 desc: "Unknown function",
103 mkname: "product.mk",
104 in: `
Sasha Smundak6609ba72021-07-22 18:32:56 -0700105PRODUCT_NAME := $(call foo1, bar)
106PRODUCT_NAME := $(call foo0)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800107`,
Sasha Smundak422b6142021-11-11 18:31:59 -0800108 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800109
110def init(g, handle):
111 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -0800112 rblf.mk2rbc_error("product.mk:2", "cannot handle invoking foo1")
113 rblf.mk2rbc_error("product.mk:3", "cannot handle invoking foo0")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800114`,
115 },
116 {
117 desc: "Inherit configuration always",
118 mkname: "product.mk",
119 in: `
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800120$(call inherit-product, part.mk)
Sasha Smundak868c5e32021-09-23 16:20:58 -0700121ifdef PRODUCT_NAME
122$(call inherit-product, part1.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800123else # Comment
Sasha Smundak868c5e32021-09-23 16:20:58 -0700124$(call inherit-product, $(LOCAL_PATH)/part1.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800125endif
126`,
127 expected: `load("//build/make/core:product_config.rbc", "rblf")
128load(":part.star", _part_init = "init")
Sasha Smundak868c5e32021-09-23 16:20:58 -0700129load(":part1.star|init", _part1_init = "init")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800130
131def init(g, handle):
132 cfg = rblf.cfg(handle)
Sasha Smundak868c5e32021-09-23 16:20:58 -0700133 rblf.inherit(handle, "part", _part_init)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800134 if g.get("PRODUCT_NAME") != None:
Sasha Smundak868c5e32021-09-23 16:20:58 -0700135 rblf.inherit(handle, "part1", _part1_init)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800136 else:
137 # Comment
Sasha Smundak868c5e32021-09-23 16:20:58 -0700138 rblf.inherit(handle, "part1", _part1_init)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800139`,
140 },
141 {
142 desc: "Inherit configuration if it exists",
143 mkname: "product.mk",
144 in: `
145$(call inherit-product-if-exists, part.mk)
146`,
147 expected: `load("//build/make/core:product_config.rbc", "rblf")
148load(":part.star|init", _part_init = "init")
149
150def init(g, handle):
151 cfg = rblf.cfg(handle)
Sasha Smundak6609ba72021-07-22 18:32:56 -0700152 if _part_init:
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800153 rblf.inherit(handle, "part", _part_init)
154`,
155 },
156
157 {
158 desc: "Include configuration",
159 mkname: "product.mk",
160 in: `
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800161include part.mk
Sasha Smundak868c5e32021-09-23 16:20:58 -0700162ifdef PRODUCT_NAME
163include part1.mk
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800164else
Sasha Smundak868c5e32021-09-23 16:20:58 -0700165-include $(LOCAL_PATH)/part1.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800166endif
167`,
168 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundak868c5e32021-09-23 16:20:58 -0700169load(":part.star", _part_init = "init")
170load(":part1.star|init", _part1_init = "init")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800171
172def init(g, handle):
173 cfg = rblf.cfg(handle)
Sasha Smundak868c5e32021-09-23 16:20:58 -0700174 _part_init(g, handle)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800175 if g.get("PRODUCT_NAME") != None:
Sasha Smundak868c5e32021-09-23 16:20:58 -0700176 _part1_init(g, handle)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800177 else:
Sasha Smundak868c5e32021-09-23 16:20:58 -0700178 if _part1_init != None:
179 _part1_init(g, handle)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800180`,
181 },
182
183 {
184 desc: "Synonymous inherited configurations",
185 mkname: "path/product.mk",
186 in: `
Sasha Smundak6609ba72021-07-22 18:32:56 -0700187$(call inherit-product, */font.mk)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800188`,
189 expected: `load("//build/make/core:product_config.rbc", "rblf")
190load("//foo:font.star", _font_init = "init")
191load("//bar:font.star", _font1_init = "init")
192
193def init(g, handle):
194 cfg = rblf.cfg(handle)
195 rblf.inherit(handle, "foo/font", _font_init)
196 rblf.inherit(handle, "bar/font", _font1_init)
197`,
198 },
199 {
200 desc: "Directive define",
201 mkname: "product.mk",
202 in: `
203define some-macro
204 $(info foo)
205endef
206`,
Sasha Smundak422b6142021-11-11 18:31:59 -0800207 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800208
209def init(g, handle):
210 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -0800211 rblf.mk2rbc_error("product.mk:2", "define is not supported: some-macro")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800212`,
213 },
214 {
215 desc: "Ifdef",
216 mkname: "product.mk",
217 in: `
218ifdef PRODUCT_NAME
219 PRODUCT_NAME = gizmo
220else
221endif
Sasha Smundakc4fa93e2021-11-05 14:38:46 -0700222local_var :=
223ifdef local_var
224endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800225`,
226 expected: `load("//build/make/core:product_config.rbc", "rblf")
227
228def init(g, handle):
229 cfg = rblf.cfg(handle)
230 if g.get("PRODUCT_NAME") != None:
231 cfg["PRODUCT_NAME"] = "gizmo"
232 else:
233 pass
Sasha Smundakc4fa93e2021-11-05 14:38:46 -0700234 _local_var = ""
235 if _local_var:
236 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800237`,
238 },
239 {
240 desc: "Simple functions",
241 mkname: "product.mk",
242 in: `
243$(warning this is the warning)
244$(warning)
245$(info this is the info)
246$(error this is the error)
247PRODUCT_NAME:=$(shell echo *)
248`,
249 expected: `load("//build/make/core:product_config.rbc", "rblf")
250
251def init(g, handle):
252 cfg = rblf.cfg(handle)
253 rblf.mkwarning("product.mk", "this is the warning")
254 rblf.mkwarning("product.mk", "")
255 rblf.mkinfo("product.mk", "this is the info")
256 rblf.mkerror("product.mk", "this is the error")
257 cfg["PRODUCT_NAME"] = rblf.shell("echo *")
258`,
259 },
260 {
261 desc: "Empty if",
262 mkname: "product.mk",
263 in: `
264ifdef PRODUCT_NAME
265# Comment
Sasha Smundak6609ba72021-07-22 18:32:56 -0700266else
Sasha Smundak02183cf2021-08-16 13:36:11 -0700267 TARGET_COPY_OUT_RECOVERY := foo
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800268endif
269`,
270 expected: `load("//build/make/core:product_config.rbc", "rblf")
271
272def init(g, handle):
273 cfg = rblf.cfg(handle)
274 if g.get("PRODUCT_NAME") != None:
275 # Comment
276 pass
Sasha Smundak6609ba72021-07-22 18:32:56 -0700277 else:
Sasha Smundak422b6142021-11-11 18:31:59 -0800278 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 -0800279`,
280 },
281 {
282 desc: "if/else/endif",
283 mkname: "product.mk",
284 in: `
285ifndef PRODUCT_NAME
286 PRODUCT_NAME=gizmo1
287else
288 PRODUCT_NAME=gizmo2
289endif
290`,
291 expected: `load("//build/make/core:product_config.rbc", "rblf")
292
293def init(g, handle):
294 cfg = rblf.cfg(handle)
295 if not g.get("PRODUCT_NAME") != None:
296 cfg["PRODUCT_NAME"] = "gizmo1"
297 else:
298 cfg["PRODUCT_NAME"] = "gizmo2"
299`,
300 },
301 {
302 desc: "else if",
303 mkname: "product.mk",
304 in: `
305ifdef PRODUCT_NAME
306 PRODUCT_NAME = gizmo
307else ifndef PRODUCT_PACKAGES # Comment
308endif
309 `,
310 expected: `load("//build/make/core:product_config.rbc", "rblf")
311
312def init(g, handle):
313 cfg = rblf.cfg(handle)
314 if g.get("PRODUCT_NAME") != None:
315 cfg["PRODUCT_NAME"] = "gizmo"
316 elif not g.get("PRODUCT_PACKAGES") != None:
317 # Comment
318 pass
319`,
320 },
321 {
322 desc: "ifeq / ifneq",
323 mkname: "product.mk",
324 in: `
325ifeq (aosp_arm, $(TARGET_PRODUCT))
326 PRODUCT_MODEL = pix2
327else
328 PRODUCT_MODEL = pix21
329endif
330ifneq (aosp_x86, $(TARGET_PRODUCT))
331 PRODUCT_MODEL = pix3
332endif
333`,
334 expected: `load("//build/make/core:product_config.rbc", "rblf")
335
336def init(g, handle):
337 cfg = rblf.cfg(handle)
338 if "aosp_arm" == g["TARGET_PRODUCT"]:
339 cfg["PRODUCT_MODEL"] = "pix2"
340 else:
341 cfg["PRODUCT_MODEL"] = "pix21"
342 if "aosp_x86" != g["TARGET_PRODUCT"]:
343 cfg["PRODUCT_MODEL"] = "pix3"
344`,
345 },
346 {
Cole Faustf8320212021-11-10 15:05:07 -0800347 desc: "ifeq with soong_config_get",
348 mkname: "product.mk",
349 in: `
350ifeq (true,$(call soong_config_get,art_module,source_build))
351endif
352`,
353 expected: `load("//build/make/core:product_config.rbc", "rblf")
354
355def init(g, handle):
356 cfg = rblf.cfg(handle)
357 if "true" == rblf.soong_config_get(g, "art_module", "source_build"):
358 pass
359`,
360 },
361 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800362 desc: "Check filter result",
363 mkname: "product.mk",
364 in: `
365ifeq (,$(filter userdebug eng, $(TARGET_BUILD_VARIANT)))
366endif
367ifneq (,$(filter userdebug,$(TARGET_BUILD_VARIANT))
368endif
369ifneq (,$(filter plaf,$(PLATFORM_LIST)))
370endif
371ifeq ($(TARGET_BUILD_VARIANT), $(filter $(TARGET_BUILD_VARIANT), userdebug eng))
372endif
Sasha Smundak0554d762021-07-08 18:26:12 -0700373ifneq (,$(filter true, $(v1)$(v2)))
374endif
Sasha Smundak5f463be2021-09-15 18:43:36 -0700375ifeq (,$(filter barbet coral%,$(TARGET_PRODUCT)))
376else ifneq (,$(filter barbet%,$(TARGET_PRODUCT)))
377endif
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800378`,
379 expected: `load("//build/make/core:product_config.rbc", "rblf")
380
381def init(g, handle):
382 cfg = rblf.cfg(handle)
Sasha Smundak5f463be2021-09-15 18:43:36 -0700383 if not rblf.filter("userdebug eng", g["TARGET_BUILD_VARIANT"]):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800384 pass
Sasha Smundak5f463be2021-09-15 18:43:36 -0700385 if rblf.filter("userdebug", g["TARGET_BUILD_VARIANT"]):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800386 pass
387 if "plaf" in g.get("PLATFORM_LIST", []):
388 pass
389 if g["TARGET_BUILD_VARIANT"] in ["userdebug", "eng"]:
390 pass
Sasha Smundak5f463be2021-09-15 18:43:36 -0700391 if rblf.filter("true", "%s%s" % (_v1, _v2)):
392 pass
393 if not rblf.filter("barbet coral%", g["TARGET_PRODUCT"]):
394 pass
395 elif rblf.filter("barbet%", g["TARGET_PRODUCT"]):
Sasha Smundak0554d762021-07-08 18:26:12 -0700396 pass
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800397`,
398 },
399 {
400 desc: "Get filter result",
401 mkname: "product.mk",
402 in: `
403PRODUCT_LIST2=$(filter-out %/foo.ko,$(wildcard path/*.ko))
404`,
405 expected: `load("//build/make/core:product_config.rbc", "rblf")
406
407def init(g, handle):
408 cfg = rblf.cfg(handle)
409 cfg["PRODUCT_LIST2"] = rblf.filter_out("%/foo.ko", rblf.expand_wildcard("path/*.ko"))
410`,
411 },
412 {
413 desc: "filter $(VAR), values",
414 mkname: "product.mk",
415 in: `
416ifeq (,$(filter $(TARGET_PRODUCT), yukawa_gms yukawa_sei510_gms)
417 ifneq (,$(filter $(TARGET_PRODUCT), yukawa_gms)
418 endif
419endif
420
421`,
422 expected: `load("//build/make/core:product_config.rbc", "rblf")
423
424def init(g, handle):
425 cfg = rblf.cfg(handle)
426 if g["TARGET_PRODUCT"] not in ["yukawa_gms", "yukawa_sei510_gms"]:
Sasha Smundak0554d762021-07-08 18:26:12 -0700427 if g["TARGET_PRODUCT"] == "yukawa_gms":
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800428 pass
429`,
430 },
431 {
Sasha Smundak0554d762021-07-08 18:26:12 -0700432 desc: "filter $(V1), $(V2)",
433 mkname: "product.mk",
434 in: `
435ifneq (, $(filter $(PRODUCT_LIST), $(TARGET_PRODUCT)))
436endif
437`,
438 expected: `load("//build/make/core:product_config.rbc", "rblf")
439
440def init(g, handle):
441 cfg = rblf.cfg(handle)
Sasha Smundak468e11f2021-08-26 09:10:23 -0700442 if rblf.filter(g.get("PRODUCT_LIST", []), g["TARGET_PRODUCT"]):
Sasha Smundak0554d762021-07-08 18:26:12 -0700443 pass
444`,
445 },
446 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800447 desc: "ifeq",
448 mkname: "product.mk",
449 in: `
450ifeq (aosp, $(TARGET_PRODUCT)) # Comment
451else ifneq (, $(TARGET_PRODUCT))
452endif
453`,
454 expected: `load("//build/make/core:product_config.rbc", "rblf")
455
456def init(g, handle):
457 cfg = rblf.cfg(handle)
458 if "aosp" == g["TARGET_PRODUCT"]:
459 # Comment
460 pass
461 elif g["TARGET_PRODUCT"]:
462 pass
463`,
464 },
465 {
466 desc: "Nested if",
467 mkname: "product.mk",
468 in: `
469ifdef PRODUCT_NAME
470 PRODUCT_PACKAGES = pack-if0
471 ifdef PRODUCT_MODEL
472 PRODUCT_PACKAGES = pack-if-if
473 else ifdef PRODUCT_NAME
474 PRODUCT_PACKAGES = pack-if-elif
475 else
476 PRODUCT_PACKAGES = pack-if-else
477 endif
478 PRODUCT_PACKAGES = pack-if
479else ifneq (,$(TARGET_PRODUCT))
480 PRODUCT_PACKAGES = pack-elif
481else
482 PRODUCT_PACKAGES = pack-else
483endif
484`,
485 expected: `load("//build/make/core:product_config.rbc", "rblf")
486
487def init(g, handle):
488 cfg = rblf.cfg(handle)
489 if g.get("PRODUCT_NAME") != None:
490 cfg["PRODUCT_PACKAGES"] = ["pack-if0"]
491 if g.get("PRODUCT_MODEL") != None:
492 cfg["PRODUCT_PACKAGES"] = ["pack-if-if"]
493 elif g.get("PRODUCT_NAME") != None:
494 cfg["PRODUCT_PACKAGES"] = ["pack-if-elif"]
495 else:
496 cfg["PRODUCT_PACKAGES"] = ["pack-if-else"]
497 cfg["PRODUCT_PACKAGES"] = ["pack-if"]
498 elif g["TARGET_PRODUCT"]:
499 cfg["PRODUCT_PACKAGES"] = ["pack-elif"]
500 else:
501 cfg["PRODUCT_PACKAGES"] = ["pack-else"]
502`,
503 },
504 {
505 desc: "Wildcard",
506 mkname: "product.mk",
507 in: `
508ifeq (,$(wildcard foo.mk))
509endif
510ifneq (,$(wildcard foo*.mk))
511endif
512`,
513 expected: `load("//build/make/core:product_config.rbc", "rblf")
514
515def init(g, handle):
516 cfg = rblf.cfg(handle)
517 if not rblf.file_exists("foo.mk"):
518 pass
519 if rblf.file_wildcard_exists("foo*.mk"):
520 pass
521`,
522 },
523 {
Cole Faustf8320212021-11-10 15:05:07 -0800524 desc: "if with interpolation",
525 mkname: "product.mk",
526 in: `
527ifeq ($(VARIABLE1)text$(VARIABLE2),true)
528endif
529`,
530 expected: `load("//build/make/core:product_config.rbc", "rblf")
531
532def init(g, handle):
533 cfg = rblf.cfg(handle)
534 if "%stext%s" % (g.get("VARIABLE1", ""), g.get("VARIABLE2", "")) == "true":
535 pass
536`,
537 },
538 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800539 desc: "ifneq $(X),true",
540 mkname: "product.mk",
541 in: `
542ifneq ($(VARIABLE),true)
543endif
544`,
545 expected: `load("//build/make/core:product_config.rbc", "rblf")
546
547def init(g, handle):
548 cfg = rblf.cfg(handle)
549 if g.get("VARIABLE", "") != "true":
550 pass
551`,
552 },
553 {
554 desc: "Const neq",
555 mkname: "product.mk",
556 in: `
557ifneq (1,0)
558endif
559`,
560 expected: `load("//build/make/core:product_config.rbc", "rblf")
561
562def init(g, handle):
563 cfg = rblf.cfg(handle)
564 if "1" != "0":
565 pass
566`,
567 },
568 {
569 desc: "is-board calls",
570 mkname: "product.mk",
571 in: `
572ifeq ($(call is-board-platform-in-list,msm8998), true)
573else ifneq ($(call is-board-platform,copper),true)
574else ifneq ($(call is-vendor-board-platform,QCOM),true)
575else ifeq ($(call is-product-in-list, $(PLATFORM_LIST)), true)
576endif
577`,
578 expected: `load("//build/make/core:product_config.rbc", "rblf")
579
580def init(g, handle):
581 cfg = rblf.cfg(handle)
582 if g.get("TARGET_BOARD_PLATFORM", "") in ["msm8998"]:
583 pass
584 elif g.get("TARGET_BOARD_PLATFORM", "") != "copper":
585 pass
586 elif g.get("TARGET_BOARD_PLATFORM", "") not in g["QCOM_BOARD_PLATFORMS"]:
587 pass
588 elif g["TARGET_PRODUCT"] in g.get("PLATFORM_LIST", []):
589 pass
590`,
591 },
592 {
Sasha Smundak3a9b8e82021-08-25 14:11:04 -0700593 desc: "new is-board calls",
594 mkname: "product.mk",
595 in: `
596ifneq (,$(call is-board-platform-in-list2,msm8998 $(X))
597else ifeq (,$(call is-board-platform2,copper)
598else ifneq (,$(call is-vendor-board-qcom))
599endif
600`,
601 expected: `load("//build/make/core:product_config.rbc", "rblf")
602
603def init(g, handle):
604 cfg = rblf.cfg(handle)
605 if rblf.board_platform_in(g, "msm8998 %s" % g.get("X", "")):
606 pass
607 elif not rblf.board_platform_is(g, "copper"):
608 pass
Sasha Smundak4f1f1182021-11-04 17:57:39 -0700609 elif g.get("TARGET_BOARD_PLATFORM", "") in g["QCOM_BOARD_PLATFORMS"]:
Sasha Smundak3a9b8e82021-08-25 14:11:04 -0700610 pass
611`,
612 },
613 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800614 desc: "findstring call",
615 mkname: "product.mk",
616 in: `
617ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),)
618endif
619`,
620 expected: `load("//build/make/core:product_config.rbc", "rblf")
621
622def init(g, handle):
623 cfg = rblf.cfg(handle)
624 if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
625 pass
626`,
627 },
628 {
629 desc: "rhs call",
630 mkname: "product.mk",
631 in: `
632PRODUCT_COPY_FILES = $(call add-to-product-copy-files-if-exists, path:distpath) \
633 $(call find-copy-subdir-files, *, fromdir, todir) $(wildcard foo.*)
634`,
635 expected: `load("//build/make/core:product_config.rbc", "rblf")
636
637def init(g, handle):
638 cfg = rblf.cfg(handle)
639 cfg["PRODUCT_COPY_FILES"] = (rblf.copy_if_exists("path:distpath") +
640 rblf.find_and_copy("*", "fromdir", "todir") +
641 rblf.expand_wildcard("foo.*"))
642`,
643 },
644 {
645 desc: "inferred type",
646 mkname: "product.mk",
647 in: `
648HIKEY_MODS := $(wildcard foo/*.ko)
649BOARD_VENDOR_KERNEL_MODULES += $(HIKEY_MODS)
650`,
651 expected: `load("//build/make/core:product_config.rbc", "rblf")
652
653def init(g, handle):
654 cfg = rblf.cfg(handle)
655 g["HIKEY_MODS"] = rblf.expand_wildcard("foo/*.ko")
656 g.setdefault("BOARD_VENDOR_KERNEL_MODULES", [])
657 g["BOARD_VENDOR_KERNEL_MODULES"] += g["HIKEY_MODS"]
658`,
659 },
660 {
661 desc: "list with vars",
662 mkname: "product.mk",
663 in: `
664PRODUCT_COPY_FILES += path1:$(TARGET_PRODUCT)/path1 $(PRODUCT_MODEL)/path2:$(TARGET_PRODUCT)/path2
665`,
666 expected: `load("//build/make/core:product_config.rbc", "rblf")
667
668def init(g, handle):
669 cfg = rblf.cfg(handle)
670 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
671 cfg["PRODUCT_COPY_FILES"] += (("path1:%s/path1" % g["TARGET_PRODUCT"]).split() +
672 ("%s/path2:%s/path2" % (cfg.get("PRODUCT_MODEL", ""), g["TARGET_PRODUCT"])).split())
673`,
674 },
675 {
676 desc: "misc calls",
677 mkname: "product.mk",
678 in: `
679$(call enforce-product-packages-exist,)
680$(call enforce-product-packages-exist, foo)
681$(call require-artifacts-in-path, foo, bar)
682$(call require-artifacts-in-path-relaxed, foo, bar)
Sasha Smundakd6797852021-11-15 13:01:53 -0800683$(call dist-for-goals, goal, from:to)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800684`,
685 expected: `load("//build/make/core:product_config.rbc", "rblf")
686
687def init(g, handle):
688 cfg = rblf.cfg(handle)
689 rblf.enforce_product_packages_exist("")
690 rblf.enforce_product_packages_exist("foo")
691 rblf.require_artifacts_in_path("foo", "bar")
692 rblf.require_artifacts_in_path_relaxed("foo", "bar")
Sasha Smundakd6797852021-11-15 13:01:53 -0800693 rblf.mkdist_for_goals(g, "goal", "from:to")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800694`,
695 },
696 {
697 desc: "list with functions",
698 mkname: "product.mk",
699 in: `
700PRODUCT_COPY_FILES := $(call find-copy-subdir-files,*.kl,from1,to1) \
701 $(call find-copy-subdir-files,*.kc,from2,to2) \
702 foo bar
703`,
704 expected: `load("//build/make/core:product_config.rbc", "rblf")
705
706def init(g, handle):
707 cfg = rblf.cfg(handle)
708 cfg["PRODUCT_COPY_FILES"] = (rblf.find_and_copy("*.kl", "from1", "to1") +
709 rblf.find_and_copy("*.kc", "from2", "to2") +
710 [
711 "foo",
712 "bar",
713 ])
714`,
715 },
716 {
717 desc: "Text functions",
718 mkname: "product.mk",
719 in: `
720PRODUCT_COPY_FILES := $(addprefix pfx-,a b c)
721PRODUCT_COPY_FILES := $(addsuffix .sff, a b c)
722PRODUCT_NAME := $(word 1, $(subst ., ,$(TARGET_BOARD_PLATFORM)))
Sasha Smundak35434ed2021-11-05 16:29:56 -0700723$(info $(patsubst %.pub,$(PRODUCT_NAME)%,$(PRODUCT_ADB_KEYS)))
Sasha Smundak16e07732021-07-23 11:38:23 -0700724$(info $(dir foo/bar))
725$(info $(firstword $(PRODUCT_COPY_FILES)))
726$(info $(lastword $(PRODUCT_COPY_FILES)))
727$(info $(dir $(lastword $(MAKEFILE_LIST))))
728$(info $(dir $(lastword $(PRODUCT_COPY_FILES))))
729$(info $(dir $(lastword $(foobar))))
730$(info $(abspath foo/bar))
731$(info $(notdir foo/bar))
Sasha Smundak3deb9682021-07-26 18:42:25 -0700732$(call add_soong_config_namespace,snsconfig)
733$(call add_soong_config_var_value,snsconfig,imagetype,odm_image)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700734$(call soong_config_set, snsconfig, foo, foo_value)
735$(call soong_config_append, snsconfig, bar, bar_value)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700736PRODUCT_COPY_FILES := $(call copy-files,$(wildcard foo*.mk),etc)
Sasha Smundak04453082021-08-17 18:14:41 -0700737PRODUCT_COPY_FILES := $(call product-copy-files-by-pattern,from/%,to/%,a b c)
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800738`,
739 expected: `load("//build/make/core:product_config.rbc", "rblf")
740
741def init(g, handle):
742 cfg = rblf.cfg(handle)
743 cfg["PRODUCT_COPY_FILES"] = rblf.addprefix("pfx-", "a b c")
744 cfg["PRODUCT_COPY_FILES"] = rblf.addsuffix(".sff", "a b c")
745 cfg["PRODUCT_NAME"] = ((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " ")).split()[0]
Sasha Smundak35434ed2021-11-05 16:29:56 -0700746 rblf.mkinfo("product.mk", rblf.mkpatsubst("%.pub", "%s%%" % cfg["PRODUCT_NAME"], g.get("PRODUCT_ADB_KEYS", "")))
Sasha Smundak16e07732021-07-23 11:38:23 -0700747 rblf.mkinfo("product.mk", rblf.dir("foo/bar"))
748 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][0])
749 rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][-1])
750 rblf.mkinfo("product.mk", rblf.dir("product.mk"))
751 rblf.mkinfo("product.mk", rblf.dir(cfg["PRODUCT_COPY_FILES"][-1]))
752 rblf.mkinfo("product.mk", rblf.dir((_foobar).split()[-1]))
753 rblf.mkinfo("product.mk", rblf.abspath("foo/bar"))
754 rblf.mkinfo("product.mk", rblf.notdir("foo/bar"))
Sasha Smundak65b547e2021-09-17 15:35:41 -0700755 rblf.soong_config_namespace(g, "snsconfig")
756 rblf.soong_config_set(g, "snsconfig", "imagetype", "odm_image")
757 rblf.soong_config_set(g, "snsconfig", "foo", "foo_value")
758 rblf.soong_config_append(g, "snsconfig", "bar", "bar_value")
Sasha Smundak3deb9682021-07-26 18:42:25 -0700759 cfg["PRODUCT_COPY_FILES"] = rblf.copy_files(rblf.expand_wildcard("foo*.mk"), "etc")
Sasha Smundak04453082021-08-17 18:14:41 -0700760 cfg["PRODUCT_COPY_FILES"] = rblf.product_copy_files_by_pattern("from/%", "to/%", "a b c")
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800761`,
762 },
763 {
Sasha Smundak9d011ab2021-07-09 16:00:57 -0700764 desc: "subst in list",
765 mkname: "product.mk",
766 in: `
767files = $(call find-copy-subdir-files,*,from,to)
768PRODUCT_COPY_FILES += $(subst foo,bar,$(files))
769`,
770 expected: `load("//build/make/core:product_config.rbc", "rblf")
771
772def init(g, handle):
773 cfg = rblf.cfg(handle)
774 _files = rblf.find_and_copy("*", "from", "to")
775 rblf.setdefault(handle, "PRODUCT_COPY_FILES")
776 cfg["PRODUCT_COPY_FILES"] += rblf.mksubst("foo", "bar", _files)
777`,
778 },
779 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800780 desc: "assignment flavors",
781 mkname: "product.mk",
782 in: `
783PRODUCT_LIST1 := a
784PRODUCT_LIST2 += a
785PRODUCT_LIST1 += b
786PRODUCT_LIST2 += b
787PRODUCT_LIST3 ?= a
788PRODUCT_LIST1 = c
789PLATFORM_LIST += x
790PRODUCT_PACKAGES := $(PLATFORM_LIST)
791`,
792 expected: `load("//build/make/core:product_config.rbc", "rblf")
793
794def init(g, handle):
795 cfg = rblf.cfg(handle)
796 cfg["PRODUCT_LIST1"] = ["a"]
797 rblf.setdefault(handle, "PRODUCT_LIST2")
798 cfg["PRODUCT_LIST2"] += ["a"]
799 cfg["PRODUCT_LIST1"] += ["b"]
800 cfg["PRODUCT_LIST2"] += ["b"]
801 if cfg.get("PRODUCT_LIST3") == None:
802 cfg["PRODUCT_LIST3"] = ["a"]
803 cfg["PRODUCT_LIST1"] = ["c"]
804 g.setdefault("PLATFORM_LIST", [])
805 g["PLATFORM_LIST"] += ["x"]
806 cfg["PRODUCT_PACKAGES"] = g["PLATFORM_LIST"][:]
807`,
808 },
809 {
810 desc: "assigment flavors2",
811 mkname: "product.mk",
812 in: `
813PRODUCT_LIST1 = a
814ifeq (0,1)
815 PRODUCT_LIST1 += b
816 PRODUCT_LIST2 += b
817endif
818PRODUCT_LIST1 += c
819PRODUCT_LIST2 += c
820`,
821 expected: `load("//build/make/core:product_config.rbc", "rblf")
822
823def init(g, handle):
824 cfg = rblf.cfg(handle)
825 cfg["PRODUCT_LIST1"] = ["a"]
826 if "0" == "1":
827 cfg["PRODUCT_LIST1"] += ["b"]
828 rblf.setdefault(handle, "PRODUCT_LIST2")
829 cfg["PRODUCT_LIST2"] += ["b"]
830 cfg["PRODUCT_LIST1"] += ["c"]
831 rblf.setdefault(handle, "PRODUCT_LIST2")
832 cfg["PRODUCT_LIST2"] += ["c"]
833`,
834 },
835 {
Sasha Smundak3deb9682021-07-26 18:42:25 -0700836 desc: "soong namespace assignments",
837 mkname: "product.mk",
838 in: `
839SOONG_CONFIG_NAMESPACES += cvd
840SOONG_CONFIG_cvd += launch_configs
Sasha Smundak65b547e2021-09-17 15:35:41 -0700841SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
Sasha Smundak3deb9682021-07-26 18:42:25 -0700842SOONG_CONFIG_cvd += grub_config
843SOONG_CONFIG_cvd_grub_config += grub.cfg
Sasha Smundak65b547e2021-09-17 15:35:41 -0700844x := $(SOONG_CONFIG_cvd_grub_config)
Sasha Smundak3deb9682021-07-26 18:42:25 -0700845`,
846 expected: `load("//build/make/core:product_config.rbc", "rblf")
847
848def init(g, handle):
849 cfg = rblf.cfg(handle)
Sasha Smundak65b547e2021-09-17 15:35:41 -0700850 rblf.soong_config_namespace(g, "cvd")
851 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
852 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
Sasha Smundak422b6142021-11-11 18:31:59 -0800853 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 -0700854`,
Cole Faustc00184e2021-11-08 12:08:57 -0800855 }, {
856 desc: "soong namespace accesses",
857 mkname: "product.mk",
858 in: `
859SOONG_CONFIG_NAMESPACES += cvd
860SOONG_CONFIG_cvd += launch_configs
861SOONG_CONFIG_cvd_launch_configs = cvd_config_auto.json
862SOONG_CONFIG_cvd += grub_config
863SOONG_CONFIG_cvd_grub_config += grub.cfg
864x := $(call soong_config_get,cvd,grub_config)
865`,
866 expected: `load("//build/make/core:product_config.rbc", "rblf")
867
868def init(g, handle):
869 cfg = rblf.cfg(handle)
870 rblf.soong_config_namespace(g, "cvd")
871 rblf.soong_config_set(g, "cvd", "launch_configs", "cvd_config_auto.json")
872 rblf.soong_config_append(g, "cvd", "grub_config", "grub.cfg")
873 _x = rblf.soong_config_get(g, "cvd", "grub_config")
874`,
Sasha Smundak3deb9682021-07-26 18:42:25 -0700875 },
876 {
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800877 desc: "string split",
878 mkname: "product.mk",
879 in: `
880PRODUCT_LIST1 = a
881local = b
882local += c
883FOO = d
884FOO += e
885PRODUCT_LIST1 += $(local)
886PRODUCT_LIST1 += $(FOO)
887`,
888 expected: `load("//build/make/core:product_config.rbc", "rblf")
889
890def init(g, handle):
891 cfg = rblf.cfg(handle)
892 cfg["PRODUCT_LIST1"] = ["a"]
893 _local = "b"
894 _local += " " + "c"
895 g["FOO"] = "d"
896 g["FOO"] += " " + "e"
897 cfg["PRODUCT_LIST1"] += (_local).split()
898 cfg["PRODUCT_LIST1"] += (g["FOO"]).split()
899`,
900 },
901 {
902 desc: "apex_jars",
903 mkname: "product.mk",
904 in: `
905PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex
906`,
907 expected: `load("//build/make/core:product_config.rbc", "rblf")
908
909def init(g, handle):
910 cfg = rblf.cfg(handle)
911 cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) +
912 ["framework-minus-apex"])
913`,
914 },
915 {
916 desc: "strip function",
917 mkname: "product.mk",
918 in: `
919ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),)
920 PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress)
921endif
922`,
923 expected: `load("//build/make/core:product_config.rbc", "rblf")
924
925def init(g, handle):
926 cfg = rblf.cfg(handle)
927 if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []):
928 cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split()
929`,
930 },
931 {
932 desc: "strip func in condition",
933 mkname: "product.mk",
934 in: `
935ifneq ($(strip $(TARGET_VENDOR)),)
936endif
937`,
938 expected: `load("//build/make/core:product_config.rbc", "rblf")
939
940def init(g, handle):
941 cfg = rblf.cfg(handle)
Sasha Smundak0554d762021-07-08 18:26:12 -0700942 if rblf.mkstrip(g.get("TARGET_VENDOR", "")):
Sasha Smundakb051c4e2020-11-05 20:45:07 -0800943 pass
944`,
945 },
946 {
947 desc: "ref after set",
948 mkname: "product.mk",
949 in: `
950PRODUCT_ADB_KEYS:=value
951FOO := $(PRODUCT_ADB_KEYS)
952ifneq (,$(PRODUCT_ADB_KEYS))
953endif
954`,
955 expected: `load("//build/make/core:product_config.rbc", "rblf")
956
957def init(g, handle):
958 cfg = rblf.cfg(handle)
959 g["PRODUCT_ADB_KEYS"] = "value"
960 g["FOO"] = g["PRODUCT_ADB_KEYS"]
961 if g["PRODUCT_ADB_KEYS"]:
962 pass
963`,
964 },
965 {
966 desc: "ref before set",
967 mkname: "product.mk",
968 in: `
969V1 := $(PRODUCT_ADB_KEYS)
970ifeq (,$(PRODUCT_ADB_KEYS))
971 V2 := $(PRODUCT_ADB_KEYS)
972 PRODUCT_ADB_KEYS:=foo
973 V3 := $(PRODUCT_ADB_KEYS)
974endif`,
975 expected: `load("//build/make/core:product_config.rbc", "rblf")
976
977def init(g, handle):
978 cfg = rblf.cfg(handle)
979 g["V1"] = g.get("PRODUCT_ADB_KEYS", "")
980 if not g.get("PRODUCT_ADB_KEYS", ""):
981 g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
982 g["PRODUCT_ADB_KEYS"] = "foo"
983 g["V3"] = g["PRODUCT_ADB_KEYS"]
984`,
985 },
Sasha Smundak6609ba72021-07-22 18:32:56 -0700986 {
987 desc: "Dynamic inherit path",
988 mkname: "product.mk",
989 in: `
Sasha Smundak6d852dd2021-09-27 20:34:39 -0700990MY_PATH:=foo
Sasha Smundak6609ba72021-07-22 18:32:56 -0700991$(call inherit-product,vendor/$(MY_PATH)/cfg.mk)
992`,
993 expected: `load("//build/make/core:product_config.rbc", "rblf")
994load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
995load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init")
996
997def init(g, handle):
998 cfg = rblf.cfg(handle)
999 g["MY_PATH"] = "foo"
1000 _entry = {
1001 "vendor/foo1/cfg.mk": ("_cfg", _cfg_init),
1002 "vendor/bar/baz/cfg.mk": ("_cfg1", _cfg1_init),
1003 }.get("vendor/%s/cfg.mk" % g["MY_PATH"])
1004 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1005 if not _varmod_init:
1006 rblf.mkerror("cannot")
1007 rblf.inherit(handle, _varmod, _varmod_init)
1008`,
1009 },
Sasha Smundak6d852dd2021-09-27 20:34:39 -07001010 {
1011 desc: "Dynamic inherit with hint",
1012 mkname: "product.mk",
1013 in: `
1014MY_PATH:=foo
1015#RBC# include_top vendor/foo1
1016$(call inherit-product,$(MY_PATH)/cfg.mk)
1017`,
1018 expected: `load("//build/make/core:product_config.rbc", "rblf")
1019load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
1020
1021def init(g, handle):
1022 cfg = rblf.cfg(handle)
1023 g["MY_PATH"] = "foo"
1024 #RBC# include_top vendor/foo1
1025 _entry = {
1026 "vendor/foo1/cfg.mk": ("_cfg", _cfg_init),
1027 }.get("%s/cfg.mk" % g["MY_PATH"])
1028 (_varmod, _varmod_init) = _entry if _entry else (None, None)
1029 if not _varmod_init:
1030 rblf.mkerror("cannot")
1031 rblf.inherit(handle, _varmod, _varmod_init)
1032`,
1033 },
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001034 {
1035 desc: "Ignore make rules",
1036 mkname: "product.mk",
1037 in: `
1038foo: foo.c
1039 gcc -o $@ $*`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001040 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001041
1042def init(g, handle):
1043 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001044 rblf.mk2rbc_error("product.mk:2", "unsupported line rule: foo: foo.c\n#gcc -o $@ $*")
Sasha Smundak2afb9d72021-10-24 15:16:59 -07001045`,
1046 },
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001047 {
1048 desc: "Flag override",
1049 mkname: "product.mk",
1050 in: `
1051override FOO:=`,
Sasha Smundak422b6142021-11-11 18:31:59 -08001052 expected: `load("//build/make/core:product_config.rbc", "rblf")
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001053
1054def init(g, handle):
1055 cfg = rblf.cfg(handle)
Sasha Smundak422b6142021-11-11 18:31:59 -08001056 rblf.mk2rbc_error("product.mk:2", "cannot handle override directive")
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001057 g["override FOO"] = ""
Sasha Smundak422b6142021-11-11 18:31:59 -08001058`,
1059 },
1060 {
1061 desc: "Bad expression",
1062 mkname: "build/product.mk",
1063 in: `
1064ifeq (,$(call foobar))
1065endif
1066`,
1067 expected: `load("//build/make/core:product_config.rbc", "rblf")
1068
1069def init(g, handle):
1070 cfg = rblf.cfg(handle)
1071 if rblf.mk2rbc_error("build/product.mk:2", "cannot handle invoking foobar"):
1072 pass
Sasha Smundakea3bc3a2021-11-10 13:06:42 -08001073`,
1074 },
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001075}
1076
1077var known_variables = []struct {
1078 name string
1079 class varClass
1080 starlarkType
1081}{
1082 {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
1083 {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
1084 {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
1085 {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
1086 {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
1087 {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
1088 {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
1089 {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
1090 {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
1091 {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
1092 {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
1093 {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
1094 {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
1095 {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
1096}
1097
Sasha Smundak6609ba72021-07-22 18:32:56 -07001098type testMakefileFinder struct {
1099 fs fs.FS
1100 root string
1101 files []string
1102}
1103
1104func (t *testMakefileFinder) Find(root string) []string {
1105 if t.files != nil || root == t.root {
1106 return t.files
1107 }
1108 t.files = make([]string, 0)
1109 fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error {
1110 if err != nil {
1111 return err
1112 }
1113 if d.IsDir() {
1114 base := filepath.Base(path)
1115 if base[0] == '.' && len(base) > 1 {
1116 return fs.SkipDir
1117 }
1118 return nil
1119 }
1120 if strings.HasSuffix(path, ".mk") {
1121 t.files = append(t.files, path)
1122 }
1123 return nil
1124 })
1125 return t.files
1126}
1127
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001128func TestGood(t *testing.T) {
1129 for _, v := range known_variables {
1130 KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
1131 }
Sasha Smundak6609ba72021-07-22 18:32:56 -07001132 fs := NewFindMockFS([]string{
1133 "vendor/foo1/cfg.mk",
1134 "vendor/bar/baz/cfg.mk",
1135 "part.mk",
1136 "foo/font.mk",
1137 "bar/font.mk",
1138 })
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001139 for _, test := range testCases {
1140 t.Run(test.desc,
1141 func(t *testing.T) {
1142 ss, err := Convert(Request{
Sasha Smundak422b6142021-11-11 18:31:59 -08001143 MkFile: test.mkname,
1144 Reader: bytes.NewBufferString(test.in),
1145 RootDir: ".",
1146 OutputSuffix: ".star",
1147 SourceFS: fs,
1148 MakefileFinder: &testMakefileFinder{fs: fs},
Sasha Smundakb051c4e2020-11-05 20:45:07 -08001149 })
1150 if err != nil {
1151 t.Error(err)
1152 return
1153 }
1154 got := ss.String()
1155 if got != test.expected {
1156 t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
1157 strings.ReplaceAll(test.expected, "\n", "␤\n"),
1158 strings.ReplaceAll(got, "\n", "␤\n"))
1159 }
1160 })
1161 }
1162}