Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package mk2rbc |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame^] | 19 | "io/fs" |
| 20 | "path/filepath" |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 21 | "strings" |
| 22 | "testing" |
| 23 | ) |
| 24 | |
| 25 | var 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 |
| 42 | load("//build/make/core:product_config.rbc", "rblf") |
| 43 | |
| 44 | def 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 |
| 55 | load("//build/make/core:product_config.rbc", "rblf") |
| 56 | |
| 57 | def init(g, handle): |
| 58 | cfg = rblf.cfg(handle) |
| 59 | `, |
| 60 | }, |
| 61 | { |
| 62 | desc: "Item variable", |
| 63 | mkname: "pixel3.mk", |
| 64 | in: ` |
| 65 | PRODUCT_NAME := Pixel 3 |
| 66 | PRODUCT_MODEL := |
| 67 | local_var = foo |
| 68 | `, |
| 69 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 70 | |
| 71 | def 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: ` |
| 82 | PRODUCT_PACKAGES = package1 package2 |
| 83 | PRODUCT_COPY_FILES += file2:target |
| 84 | PRODUCT_PACKAGES += package3 |
| 85 | PRODUCT_COPY_FILES = |
| 86 | `, |
| 87 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 88 | |
| 89 | def 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 Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame^] | 105 | PRODUCT_NAME := $(call foo1, bar) |
| 106 | PRODUCT_NAME := $(call foo0) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 107 | `, |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame^] | 108 | expected: `# MK2RBC TRANSLATION ERROR: cannot handle invoking foo1 |
| 109 | # PRODUCT_NAME := $(call foo1, bar) |
| 110 | # MK2RBC TRANSLATION ERROR: cannot handle invoking foo0 |
| 111 | # PRODUCT_NAME := $(call foo0) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 112 | load("//build/make/core:product_config.rbc", "rblf") |
| 113 | |
| 114 | def init(g, handle): |
| 115 | cfg = rblf.cfg(handle) |
| 116 | rblf.warning("product.mk", "partially successful conversion") |
| 117 | `, |
| 118 | }, |
| 119 | { |
| 120 | desc: "Inherit configuration always", |
| 121 | mkname: "product.mk", |
| 122 | in: ` |
| 123 | ifdef PRODUCT_NAME |
| 124 | $(call inherit-product, part.mk) |
| 125 | else # Comment |
| 126 | $(call inherit-product, $(LOCAL_PATH)/part.mk) |
| 127 | endif |
| 128 | `, |
| 129 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 130 | load(":part.star", _part_init = "init") |
| 131 | |
| 132 | def init(g, handle): |
| 133 | cfg = rblf.cfg(handle) |
| 134 | if g.get("PRODUCT_NAME") != None: |
| 135 | rblf.inherit(handle, "part", _part_init) |
| 136 | else: |
| 137 | # Comment |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame^] | 138 | rblf.inherit(handle, "part", _part_init) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 139 | `, |
| 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") |
| 148 | load(":part.star|init", _part_init = "init") |
| 149 | |
| 150 | def init(g, handle): |
| 151 | cfg = rblf.cfg(handle) |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame^] | 152 | if _part_init: |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 153 | rblf.inherit(handle, "part", _part_init) |
| 154 | `, |
| 155 | }, |
| 156 | |
| 157 | { |
| 158 | desc: "Include configuration", |
| 159 | mkname: "product.mk", |
| 160 | in: ` |
| 161 | ifdef PRODUCT_NAME |
| 162 | include part.mk |
| 163 | else |
| 164 | -include $(LOCAL_PATH)/part.mk) |
| 165 | endif |
| 166 | `, |
| 167 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame^] | 168 | load(":part.star|init", _part_init = "init") |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 169 | |
| 170 | def init(g, handle): |
| 171 | cfg = rblf.cfg(handle) |
| 172 | if g.get("PRODUCT_NAME") != None: |
| 173 | _part_init(g, handle) |
| 174 | else: |
| 175 | if _part_init != None: |
| 176 | _part_init(g, handle) |
| 177 | `, |
| 178 | }, |
| 179 | |
| 180 | { |
| 181 | desc: "Synonymous inherited configurations", |
| 182 | mkname: "path/product.mk", |
| 183 | in: ` |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame^] | 184 | $(call inherit-product, */font.mk) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 185 | `, |
| 186 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 187 | load("//foo:font.star", _font_init = "init") |
| 188 | load("//bar:font.star", _font1_init = "init") |
| 189 | |
| 190 | def init(g, handle): |
| 191 | cfg = rblf.cfg(handle) |
| 192 | rblf.inherit(handle, "foo/font", _font_init) |
| 193 | rblf.inherit(handle, "bar/font", _font1_init) |
| 194 | `, |
| 195 | }, |
| 196 | { |
| 197 | desc: "Directive define", |
| 198 | mkname: "product.mk", |
| 199 | in: ` |
| 200 | define some-macro |
| 201 | $(info foo) |
| 202 | endef |
| 203 | `, |
| 204 | expected: `# MK2RBC TRANSLATION ERROR: define is not supported: some-macro |
| 205 | # define some-macro |
| 206 | # $(info foo) |
| 207 | # endef |
| 208 | load("//build/make/core:product_config.rbc", "rblf") |
| 209 | |
| 210 | def init(g, handle): |
| 211 | cfg = rblf.cfg(handle) |
| 212 | rblf.warning("product.mk", "partially successful conversion") |
| 213 | `, |
| 214 | }, |
| 215 | { |
| 216 | desc: "Ifdef", |
| 217 | mkname: "product.mk", |
| 218 | in: ` |
| 219 | ifdef PRODUCT_NAME |
| 220 | PRODUCT_NAME = gizmo |
| 221 | else |
| 222 | endif |
| 223 | `, |
| 224 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 225 | |
| 226 | def init(g, handle): |
| 227 | cfg = rblf.cfg(handle) |
| 228 | if g.get("PRODUCT_NAME") != None: |
| 229 | cfg["PRODUCT_NAME"] = "gizmo" |
| 230 | else: |
| 231 | pass |
| 232 | `, |
| 233 | }, |
| 234 | { |
| 235 | desc: "Simple functions", |
| 236 | mkname: "product.mk", |
| 237 | in: ` |
| 238 | $(warning this is the warning) |
| 239 | $(warning) |
| 240 | $(info this is the info) |
| 241 | $(error this is the error) |
| 242 | PRODUCT_NAME:=$(shell echo *) |
| 243 | `, |
| 244 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 245 | |
| 246 | def init(g, handle): |
| 247 | cfg = rblf.cfg(handle) |
| 248 | rblf.mkwarning("product.mk", "this is the warning") |
| 249 | rblf.mkwarning("product.mk", "") |
| 250 | rblf.mkinfo("product.mk", "this is the info") |
| 251 | rblf.mkerror("product.mk", "this is the error") |
| 252 | cfg["PRODUCT_NAME"] = rblf.shell("echo *") |
| 253 | `, |
| 254 | }, |
| 255 | { |
| 256 | desc: "Empty if", |
| 257 | mkname: "product.mk", |
| 258 | in: ` |
| 259 | ifdef PRODUCT_NAME |
| 260 | # Comment |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame^] | 261 | else |
| 262 | TARGET_COPY_OUT_VENDOR := foo |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 263 | endif |
| 264 | `, |
| 265 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 266 | |
| 267 | def init(g, handle): |
| 268 | cfg = rblf.cfg(handle) |
| 269 | if g.get("PRODUCT_NAME") != None: |
| 270 | # Comment |
| 271 | pass |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame^] | 272 | else: |
| 273 | # MK2RBC TRANSLATION ERROR: cannot set predefined variable TARGET_COPY_OUT_VENDOR to "foo", its value should be "||VENDOR-PATH-PH||" |
| 274 | pass |
| 275 | rblf.warning("product.mk", "partially successful conversion") |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 276 | `, |
| 277 | }, |
| 278 | { |
| 279 | desc: "if/else/endif", |
| 280 | mkname: "product.mk", |
| 281 | in: ` |
| 282 | ifndef PRODUCT_NAME |
| 283 | PRODUCT_NAME=gizmo1 |
| 284 | else |
| 285 | PRODUCT_NAME=gizmo2 |
| 286 | endif |
| 287 | `, |
| 288 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 289 | |
| 290 | def init(g, handle): |
| 291 | cfg = rblf.cfg(handle) |
| 292 | if not g.get("PRODUCT_NAME") != None: |
| 293 | cfg["PRODUCT_NAME"] = "gizmo1" |
| 294 | else: |
| 295 | cfg["PRODUCT_NAME"] = "gizmo2" |
| 296 | `, |
| 297 | }, |
| 298 | { |
| 299 | desc: "else if", |
| 300 | mkname: "product.mk", |
| 301 | in: ` |
| 302 | ifdef PRODUCT_NAME |
| 303 | PRODUCT_NAME = gizmo |
| 304 | else ifndef PRODUCT_PACKAGES # Comment |
| 305 | endif |
| 306 | `, |
| 307 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 308 | |
| 309 | def init(g, handle): |
| 310 | cfg = rblf.cfg(handle) |
| 311 | if g.get("PRODUCT_NAME") != None: |
| 312 | cfg["PRODUCT_NAME"] = "gizmo" |
| 313 | elif not g.get("PRODUCT_PACKAGES") != None: |
| 314 | # Comment |
| 315 | pass |
| 316 | `, |
| 317 | }, |
| 318 | { |
| 319 | desc: "ifeq / ifneq", |
| 320 | mkname: "product.mk", |
| 321 | in: ` |
| 322 | ifeq (aosp_arm, $(TARGET_PRODUCT)) |
| 323 | PRODUCT_MODEL = pix2 |
| 324 | else |
| 325 | PRODUCT_MODEL = pix21 |
| 326 | endif |
| 327 | ifneq (aosp_x86, $(TARGET_PRODUCT)) |
| 328 | PRODUCT_MODEL = pix3 |
| 329 | endif |
| 330 | `, |
| 331 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 332 | |
| 333 | def init(g, handle): |
| 334 | cfg = rblf.cfg(handle) |
| 335 | if "aosp_arm" == g["TARGET_PRODUCT"]: |
| 336 | cfg["PRODUCT_MODEL"] = "pix2" |
| 337 | else: |
| 338 | cfg["PRODUCT_MODEL"] = "pix21" |
| 339 | if "aosp_x86" != g["TARGET_PRODUCT"]: |
| 340 | cfg["PRODUCT_MODEL"] = "pix3" |
| 341 | `, |
| 342 | }, |
| 343 | { |
| 344 | desc: "Check filter result", |
| 345 | mkname: "product.mk", |
| 346 | in: ` |
| 347 | ifeq (,$(filter userdebug eng, $(TARGET_BUILD_VARIANT))) |
| 348 | endif |
| 349 | ifneq (,$(filter userdebug,$(TARGET_BUILD_VARIANT)) |
| 350 | endif |
| 351 | ifneq (,$(filter plaf,$(PLATFORM_LIST))) |
| 352 | endif |
| 353 | ifeq ($(TARGET_BUILD_VARIANT), $(filter $(TARGET_BUILD_VARIANT), userdebug eng)) |
| 354 | endif |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame] | 355 | ifneq (,$(filter true, $(v1)$(v2))) |
| 356 | endif |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 357 | `, |
| 358 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 359 | |
| 360 | def init(g, handle): |
| 361 | cfg = rblf.cfg(handle) |
| 362 | if g["TARGET_BUILD_VARIANT"] not in ["userdebug", "eng"]: |
| 363 | pass |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame] | 364 | if g["TARGET_BUILD_VARIANT"] == "userdebug": |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 365 | pass |
| 366 | if "plaf" in g.get("PLATFORM_LIST", []): |
| 367 | pass |
| 368 | if g["TARGET_BUILD_VARIANT"] in ["userdebug", "eng"]: |
| 369 | pass |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame] | 370 | if "%s%s" % (_v1, _v2) == "true": |
| 371 | pass |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 372 | `, |
| 373 | }, |
| 374 | { |
| 375 | desc: "Get filter result", |
| 376 | mkname: "product.mk", |
| 377 | in: ` |
| 378 | PRODUCT_LIST2=$(filter-out %/foo.ko,$(wildcard path/*.ko)) |
| 379 | `, |
| 380 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 381 | |
| 382 | def init(g, handle): |
| 383 | cfg = rblf.cfg(handle) |
| 384 | cfg["PRODUCT_LIST2"] = rblf.filter_out("%/foo.ko", rblf.expand_wildcard("path/*.ko")) |
| 385 | `, |
| 386 | }, |
| 387 | { |
| 388 | desc: "filter $(VAR), values", |
| 389 | mkname: "product.mk", |
| 390 | in: ` |
| 391 | ifeq (,$(filter $(TARGET_PRODUCT), yukawa_gms yukawa_sei510_gms) |
| 392 | ifneq (,$(filter $(TARGET_PRODUCT), yukawa_gms) |
| 393 | endif |
| 394 | endif |
| 395 | |
| 396 | `, |
| 397 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 398 | |
| 399 | def init(g, handle): |
| 400 | cfg = rblf.cfg(handle) |
| 401 | if g["TARGET_PRODUCT"] not in ["yukawa_gms", "yukawa_sei510_gms"]: |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame] | 402 | if g["TARGET_PRODUCT"] == "yukawa_gms": |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 403 | pass |
| 404 | `, |
| 405 | }, |
| 406 | { |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame] | 407 | desc: "filter $(V1), $(V2)", |
| 408 | mkname: "product.mk", |
| 409 | in: ` |
| 410 | ifneq (, $(filter $(PRODUCT_LIST), $(TARGET_PRODUCT))) |
| 411 | endif |
| 412 | `, |
| 413 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 414 | |
| 415 | def init(g, handle): |
| 416 | cfg = rblf.cfg(handle) |
| 417 | if rblf.filter(g.get("PRODUCT_LIST", ""), g["TARGET_PRODUCT"]): |
| 418 | pass |
| 419 | `, |
| 420 | }, |
| 421 | { |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 422 | desc: "ifeq", |
| 423 | mkname: "product.mk", |
| 424 | in: ` |
| 425 | ifeq (aosp, $(TARGET_PRODUCT)) # Comment |
| 426 | else ifneq (, $(TARGET_PRODUCT)) |
| 427 | endif |
| 428 | `, |
| 429 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 430 | |
| 431 | def init(g, handle): |
| 432 | cfg = rblf.cfg(handle) |
| 433 | if "aosp" == g["TARGET_PRODUCT"]: |
| 434 | # Comment |
| 435 | pass |
| 436 | elif g["TARGET_PRODUCT"]: |
| 437 | pass |
| 438 | `, |
| 439 | }, |
| 440 | { |
| 441 | desc: "Nested if", |
| 442 | mkname: "product.mk", |
| 443 | in: ` |
| 444 | ifdef PRODUCT_NAME |
| 445 | PRODUCT_PACKAGES = pack-if0 |
| 446 | ifdef PRODUCT_MODEL |
| 447 | PRODUCT_PACKAGES = pack-if-if |
| 448 | else ifdef PRODUCT_NAME |
| 449 | PRODUCT_PACKAGES = pack-if-elif |
| 450 | else |
| 451 | PRODUCT_PACKAGES = pack-if-else |
| 452 | endif |
| 453 | PRODUCT_PACKAGES = pack-if |
| 454 | else ifneq (,$(TARGET_PRODUCT)) |
| 455 | PRODUCT_PACKAGES = pack-elif |
| 456 | else |
| 457 | PRODUCT_PACKAGES = pack-else |
| 458 | endif |
| 459 | `, |
| 460 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 461 | |
| 462 | def init(g, handle): |
| 463 | cfg = rblf.cfg(handle) |
| 464 | if g.get("PRODUCT_NAME") != None: |
| 465 | cfg["PRODUCT_PACKAGES"] = ["pack-if0"] |
| 466 | if g.get("PRODUCT_MODEL") != None: |
| 467 | cfg["PRODUCT_PACKAGES"] = ["pack-if-if"] |
| 468 | elif g.get("PRODUCT_NAME") != None: |
| 469 | cfg["PRODUCT_PACKAGES"] = ["pack-if-elif"] |
| 470 | else: |
| 471 | cfg["PRODUCT_PACKAGES"] = ["pack-if-else"] |
| 472 | cfg["PRODUCT_PACKAGES"] = ["pack-if"] |
| 473 | elif g["TARGET_PRODUCT"]: |
| 474 | cfg["PRODUCT_PACKAGES"] = ["pack-elif"] |
| 475 | else: |
| 476 | cfg["PRODUCT_PACKAGES"] = ["pack-else"] |
| 477 | `, |
| 478 | }, |
| 479 | { |
| 480 | desc: "Wildcard", |
| 481 | mkname: "product.mk", |
| 482 | in: ` |
| 483 | ifeq (,$(wildcard foo.mk)) |
| 484 | endif |
| 485 | ifneq (,$(wildcard foo*.mk)) |
| 486 | endif |
| 487 | `, |
| 488 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 489 | |
| 490 | def init(g, handle): |
| 491 | cfg = rblf.cfg(handle) |
| 492 | if not rblf.file_exists("foo.mk"): |
| 493 | pass |
| 494 | if rblf.file_wildcard_exists("foo*.mk"): |
| 495 | pass |
| 496 | `, |
| 497 | }, |
| 498 | { |
| 499 | desc: "ifneq $(X),true", |
| 500 | mkname: "product.mk", |
| 501 | in: ` |
| 502 | ifneq ($(VARIABLE),true) |
| 503 | endif |
| 504 | `, |
| 505 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 506 | |
| 507 | def init(g, handle): |
| 508 | cfg = rblf.cfg(handle) |
| 509 | if g.get("VARIABLE", "") != "true": |
| 510 | pass |
| 511 | `, |
| 512 | }, |
| 513 | { |
| 514 | desc: "Const neq", |
| 515 | mkname: "product.mk", |
| 516 | in: ` |
| 517 | ifneq (1,0) |
| 518 | endif |
| 519 | `, |
| 520 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 521 | |
| 522 | def init(g, handle): |
| 523 | cfg = rblf.cfg(handle) |
| 524 | if "1" != "0": |
| 525 | pass |
| 526 | `, |
| 527 | }, |
| 528 | { |
| 529 | desc: "is-board calls", |
| 530 | mkname: "product.mk", |
| 531 | in: ` |
| 532 | ifeq ($(call is-board-platform-in-list,msm8998), true) |
| 533 | else ifneq ($(call is-board-platform,copper),true) |
| 534 | else ifneq ($(call is-vendor-board-platform,QCOM),true) |
| 535 | else ifeq ($(call is-product-in-list, $(PLATFORM_LIST)), true) |
| 536 | endif |
| 537 | `, |
| 538 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 539 | |
| 540 | def init(g, handle): |
| 541 | cfg = rblf.cfg(handle) |
| 542 | if g.get("TARGET_BOARD_PLATFORM", "") in ["msm8998"]: |
| 543 | pass |
| 544 | elif g.get("TARGET_BOARD_PLATFORM", "") != "copper": |
| 545 | pass |
| 546 | elif g.get("TARGET_BOARD_PLATFORM", "") not in g["QCOM_BOARD_PLATFORMS"]: |
| 547 | pass |
| 548 | elif g["TARGET_PRODUCT"] in g.get("PLATFORM_LIST", []): |
| 549 | pass |
| 550 | `, |
| 551 | }, |
| 552 | { |
| 553 | desc: "findstring call", |
| 554 | mkname: "product.mk", |
| 555 | in: ` |
| 556 | ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),) |
| 557 | endif |
| 558 | `, |
| 559 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 560 | |
| 561 | def init(g, handle): |
| 562 | cfg = rblf.cfg(handle) |
| 563 | if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1: |
| 564 | pass |
| 565 | `, |
| 566 | }, |
| 567 | { |
| 568 | desc: "rhs call", |
| 569 | mkname: "product.mk", |
| 570 | in: ` |
| 571 | PRODUCT_COPY_FILES = $(call add-to-product-copy-files-if-exists, path:distpath) \ |
| 572 | $(call find-copy-subdir-files, *, fromdir, todir) $(wildcard foo.*) |
| 573 | `, |
| 574 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 575 | |
| 576 | def init(g, handle): |
| 577 | cfg = rblf.cfg(handle) |
| 578 | cfg["PRODUCT_COPY_FILES"] = (rblf.copy_if_exists("path:distpath") + |
| 579 | rblf.find_and_copy("*", "fromdir", "todir") + |
| 580 | rblf.expand_wildcard("foo.*")) |
| 581 | `, |
| 582 | }, |
| 583 | { |
| 584 | desc: "inferred type", |
| 585 | mkname: "product.mk", |
| 586 | in: ` |
| 587 | HIKEY_MODS := $(wildcard foo/*.ko) |
| 588 | BOARD_VENDOR_KERNEL_MODULES += $(HIKEY_MODS) |
| 589 | `, |
| 590 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 591 | |
| 592 | def init(g, handle): |
| 593 | cfg = rblf.cfg(handle) |
| 594 | g["HIKEY_MODS"] = rblf.expand_wildcard("foo/*.ko") |
| 595 | g.setdefault("BOARD_VENDOR_KERNEL_MODULES", []) |
| 596 | g["BOARD_VENDOR_KERNEL_MODULES"] += g["HIKEY_MODS"] |
| 597 | `, |
| 598 | }, |
| 599 | { |
| 600 | desc: "list with vars", |
| 601 | mkname: "product.mk", |
| 602 | in: ` |
| 603 | PRODUCT_COPY_FILES += path1:$(TARGET_PRODUCT)/path1 $(PRODUCT_MODEL)/path2:$(TARGET_PRODUCT)/path2 |
| 604 | `, |
| 605 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 606 | |
| 607 | def init(g, handle): |
| 608 | cfg = rblf.cfg(handle) |
| 609 | rblf.setdefault(handle, "PRODUCT_COPY_FILES") |
| 610 | cfg["PRODUCT_COPY_FILES"] += (("path1:%s/path1" % g["TARGET_PRODUCT"]).split() + |
| 611 | ("%s/path2:%s/path2" % (cfg.get("PRODUCT_MODEL", ""), g["TARGET_PRODUCT"])).split()) |
| 612 | `, |
| 613 | }, |
| 614 | { |
| 615 | desc: "misc calls", |
| 616 | mkname: "product.mk", |
| 617 | in: ` |
| 618 | $(call enforce-product-packages-exist,) |
| 619 | $(call enforce-product-packages-exist, foo) |
| 620 | $(call require-artifacts-in-path, foo, bar) |
| 621 | $(call require-artifacts-in-path-relaxed, foo, bar) |
| 622 | `, |
| 623 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 624 | |
| 625 | def init(g, handle): |
| 626 | cfg = rblf.cfg(handle) |
| 627 | rblf.enforce_product_packages_exist("") |
| 628 | rblf.enforce_product_packages_exist("foo") |
| 629 | rblf.require_artifacts_in_path("foo", "bar") |
| 630 | rblf.require_artifacts_in_path_relaxed("foo", "bar") |
| 631 | `, |
| 632 | }, |
| 633 | { |
| 634 | desc: "list with functions", |
| 635 | mkname: "product.mk", |
| 636 | in: ` |
| 637 | PRODUCT_COPY_FILES := $(call find-copy-subdir-files,*.kl,from1,to1) \ |
| 638 | $(call find-copy-subdir-files,*.kc,from2,to2) \ |
| 639 | foo bar |
| 640 | `, |
| 641 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 642 | |
| 643 | def init(g, handle): |
| 644 | cfg = rblf.cfg(handle) |
| 645 | cfg["PRODUCT_COPY_FILES"] = (rblf.find_and_copy("*.kl", "from1", "to1") + |
| 646 | rblf.find_and_copy("*.kc", "from2", "to2") + |
| 647 | [ |
| 648 | "foo", |
| 649 | "bar", |
| 650 | ]) |
| 651 | `, |
| 652 | }, |
| 653 | { |
| 654 | desc: "Text functions", |
| 655 | mkname: "product.mk", |
| 656 | in: ` |
| 657 | PRODUCT_COPY_FILES := $(addprefix pfx-,a b c) |
| 658 | PRODUCT_COPY_FILES := $(addsuffix .sff, a b c) |
| 659 | PRODUCT_NAME := $(word 1, $(subst ., ,$(TARGET_BOARD_PLATFORM))) |
Sasha Smundak | 94b41c7 | 2021-07-12 18:30:42 -0700 | [diff] [blame] | 660 | $(info $(patsubst %.pub,%,$(PRODUCT_ADB_KEYS))) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 661 | |
| 662 | `, |
| 663 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 664 | |
| 665 | def init(g, handle): |
| 666 | cfg = rblf.cfg(handle) |
| 667 | cfg["PRODUCT_COPY_FILES"] = rblf.addprefix("pfx-", "a b c") |
| 668 | cfg["PRODUCT_COPY_FILES"] = rblf.addsuffix(".sff", "a b c") |
| 669 | cfg["PRODUCT_NAME"] = ((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " ")).split()[0] |
Sasha Smundak | 94b41c7 | 2021-07-12 18:30:42 -0700 | [diff] [blame] | 670 | rblf.mkinfo("product.mk", rblf.mkpatsubst("%.pub", "%", g.get("PRODUCT_ADB_KEYS", ""))) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 671 | `, |
| 672 | }, |
| 673 | { |
Sasha Smundak | 9d011ab | 2021-07-09 16:00:57 -0700 | [diff] [blame] | 674 | desc: "subst in list", |
| 675 | mkname: "product.mk", |
| 676 | in: ` |
| 677 | files = $(call find-copy-subdir-files,*,from,to) |
| 678 | PRODUCT_COPY_FILES += $(subst foo,bar,$(files)) |
| 679 | `, |
| 680 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 681 | |
| 682 | def init(g, handle): |
| 683 | cfg = rblf.cfg(handle) |
| 684 | _files = rblf.find_and_copy("*", "from", "to") |
| 685 | rblf.setdefault(handle, "PRODUCT_COPY_FILES") |
| 686 | cfg["PRODUCT_COPY_FILES"] += rblf.mksubst("foo", "bar", _files) |
| 687 | `, |
| 688 | }, |
| 689 | { |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 690 | desc: "assignment flavors", |
| 691 | mkname: "product.mk", |
| 692 | in: ` |
| 693 | PRODUCT_LIST1 := a |
| 694 | PRODUCT_LIST2 += a |
| 695 | PRODUCT_LIST1 += b |
| 696 | PRODUCT_LIST2 += b |
| 697 | PRODUCT_LIST3 ?= a |
| 698 | PRODUCT_LIST1 = c |
| 699 | PLATFORM_LIST += x |
| 700 | PRODUCT_PACKAGES := $(PLATFORM_LIST) |
| 701 | `, |
| 702 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 703 | |
| 704 | def init(g, handle): |
| 705 | cfg = rblf.cfg(handle) |
| 706 | cfg["PRODUCT_LIST1"] = ["a"] |
| 707 | rblf.setdefault(handle, "PRODUCT_LIST2") |
| 708 | cfg["PRODUCT_LIST2"] += ["a"] |
| 709 | cfg["PRODUCT_LIST1"] += ["b"] |
| 710 | cfg["PRODUCT_LIST2"] += ["b"] |
| 711 | if cfg.get("PRODUCT_LIST3") == None: |
| 712 | cfg["PRODUCT_LIST3"] = ["a"] |
| 713 | cfg["PRODUCT_LIST1"] = ["c"] |
| 714 | g.setdefault("PLATFORM_LIST", []) |
| 715 | g["PLATFORM_LIST"] += ["x"] |
| 716 | cfg["PRODUCT_PACKAGES"] = g["PLATFORM_LIST"][:] |
| 717 | `, |
| 718 | }, |
| 719 | { |
| 720 | desc: "assigment flavors2", |
| 721 | mkname: "product.mk", |
| 722 | in: ` |
| 723 | PRODUCT_LIST1 = a |
| 724 | ifeq (0,1) |
| 725 | PRODUCT_LIST1 += b |
| 726 | PRODUCT_LIST2 += b |
| 727 | endif |
| 728 | PRODUCT_LIST1 += c |
| 729 | PRODUCT_LIST2 += c |
| 730 | `, |
| 731 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 732 | |
| 733 | def init(g, handle): |
| 734 | cfg = rblf.cfg(handle) |
| 735 | cfg["PRODUCT_LIST1"] = ["a"] |
| 736 | if "0" == "1": |
| 737 | cfg["PRODUCT_LIST1"] += ["b"] |
| 738 | rblf.setdefault(handle, "PRODUCT_LIST2") |
| 739 | cfg["PRODUCT_LIST2"] += ["b"] |
| 740 | cfg["PRODUCT_LIST1"] += ["c"] |
| 741 | rblf.setdefault(handle, "PRODUCT_LIST2") |
| 742 | cfg["PRODUCT_LIST2"] += ["c"] |
| 743 | `, |
| 744 | }, |
| 745 | { |
| 746 | desc: "string split", |
| 747 | mkname: "product.mk", |
| 748 | in: ` |
| 749 | PRODUCT_LIST1 = a |
| 750 | local = b |
| 751 | local += c |
| 752 | FOO = d |
| 753 | FOO += e |
| 754 | PRODUCT_LIST1 += $(local) |
| 755 | PRODUCT_LIST1 += $(FOO) |
| 756 | `, |
| 757 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 758 | |
| 759 | def init(g, handle): |
| 760 | cfg = rblf.cfg(handle) |
| 761 | cfg["PRODUCT_LIST1"] = ["a"] |
| 762 | _local = "b" |
| 763 | _local += " " + "c" |
| 764 | g["FOO"] = "d" |
| 765 | g["FOO"] += " " + "e" |
| 766 | cfg["PRODUCT_LIST1"] += (_local).split() |
| 767 | cfg["PRODUCT_LIST1"] += (g["FOO"]).split() |
| 768 | `, |
| 769 | }, |
| 770 | { |
| 771 | desc: "apex_jars", |
| 772 | mkname: "product.mk", |
| 773 | in: ` |
| 774 | PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex |
| 775 | `, |
| 776 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 777 | |
| 778 | def init(g, handle): |
| 779 | cfg = rblf.cfg(handle) |
| 780 | cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) + |
| 781 | ["framework-minus-apex"]) |
| 782 | `, |
| 783 | }, |
| 784 | { |
| 785 | desc: "strip function", |
| 786 | mkname: "product.mk", |
| 787 | in: ` |
| 788 | ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),) |
| 789 | PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress) |
| 790 | endif |
| 791 | `, |
| 792 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 793 | |
| 794 | def init(g, handle): |
| 795 | cfg = rblf.cfg(handle) |
| 796 | if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []): |
| 797 | cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split() |
| 798 | `, |
| 799 | }, |
| 800 | { |
| 801 | desc: "strip func in condition", |
| 802 | mkname: "product.mk", |
| 803 | in: ` |
| 804 | ifneq ($(strip $(TARGET_VENDOR)),) |
| 805 | endif |
| 806 | `, |
| 807 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 808 | |
| 809 | def init(g, handle): |
| 810 | cfg = rblf.cfg(handle) |
Sasha Smundak | 0554d76 | 2021-07-08 18:26:12 -0700 | [diff] [blame] | 811 | if rblf.mkstrip(g.get("TARGET_VENDOR", "")): |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 812 | pass |
| 813 | `, |
| 814 | }, |
| 815 | { |
| 816 | desc: "ref after set", |
| 817 | mkname: "product.mk", |
| 818 | in: ` |
| 819 | PRODUCT_ADB_KEYS:=value |
| 820 | FOO := $(PRODUCT_ADB_KEYS) |
| 821 | ifneq (,$(PRODUCT_ADB_KEYS)) |
| 822 | endif |
| 823 | `, |
| 824 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 825 | |
| 826 | def init(g, handle): |
| 827 | cfg = rblf.cfg(handle) |
| 828 | g["PRODUCT_ADB_KEYS"] = "value" |
| 829 | g["FOO"] = g["PRODUCT_ADB_KEYS"] |
| 830 | if g["PRODUCT_ADB_KEYS"]: |
| 831 | pass |
| 832 | `, |
| 833 | }, |
| 834 | { |
| 835 | desc: "ref before set", |
| 836 | mkname: "product.mk", |
| 837 | in: ` |
| 838 | V1 := $(PRODUCT_ADB_KEYS) |
| 839 | ifeq (,$(PRODUCT_ADB_KEYS)) |
| 840 | V2 := $(PRODUCT_ADB_KEYS) |
| 841 | PRODUCT_ADB_KEYS:=foo |
| 842 | V3 := $(PRODUCT_ADB_KEYS) |
| 843 | endif`, |
| 844 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 845 | |
| 846 | def init(g, handle): |
| 847 | cfg = rblf.cfg(handle) |
| 848 | g["V1"] = g.get("PRODUCT_ADB_KEYS", "") |
| 849 | if not g.get("PRODUCT_ADB_KEYS", ""): |
| 850 | g["V2"] = g.get("PRODUCT_ADB_KEYS", "") |
| 851 | g["PRODUCT_ADB_KEYS"] = "foo" |
| 852 | g["V3"] = g["PRODUCT_ADB_KEYS"] |
| 853 | `, |
| 854 | }, |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame^] | 855 | { |
| 856 | desc: "Dynamic inherit path", |
| 857 | mkname: "product.mk", |
| 858 | in: ` |
| 859 | MY_PATH=foo |
| 860 | $(call inherit-product,vendor/$(MY_PATH)/cfg.mk) |
| 861 | `, |
| 862 | expected: `load("//build/make/core:product_config.rbc", "rblf") |
| 863 | load("//vendor/foo1:cfg.star|init", _cfg_init = "init") |
| 864 | load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init") |
| 865 | |
| 866 | def init(g, handle): |
| 867 | cfg = rblf.cfg(handle) |
| 868 | g["MY_PATH"] = "foo" |
| 869 | _entry = { |
| 870 | "vendor/foo1/cfg.mk": ("_cfg", _cfg_init), |
| 871 | "vendor/bar/baz/cfg.mk": ("_cfg1", _cfg1_init), |
| 872 | }.get("vendor/%s/cfg.mk" % g["MY_PATH"]) |
| 873 | (_varmod, _varmod_init) = _entry if _entry else (None, None) |
| 874 | if not _varmod_init: |
| 875 | rblf.mkerror("cannot") |
| 876 | rblf.inherit(handle, _varmod, _varmod_init) |
| 877 | `, |
| 878 | }, |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | var known_variables = []struct { |
| 882 | name string |
| 883 | class varClass |
| 884 | starlarkType |
| 885 | }{ |
| 886 | {"PRODUCT_NAME", VarClassConfig, starlarkTypeString}, |
| 887 | {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString}, |
| 888 | {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList}, |
| 889 | {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList}, |
| 890 | {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList}, |
| 891 | {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString}, |
| 892 | {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList}, |
| 893 | {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList}, |
| 894 | {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList}, |
| 895 | {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString}, |
| 896 | {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString}, |
| 897 | {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString}, |
| 898 | {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString}, |
| 899 | {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong |
| 900 | } |
| 901 | |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame^] | 902 | type testMakefileFinder struct { |
| 903 | fs fs.FS |
| 904 | root string |
| 905 | files []string |
| 906 | } |
| 907 | |
| 908 | func (t *testMakefileFinder) Find(root string) []string { |
| 909 | if t.files != nil || root == t.root { |
| 910 | return t.files |
| 911 | } |
| 912 | t.files = make([]string, 0) |
| 913 | fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error { |
| 914 | if err != nil { |
| 915 | return err |
| 916 | } |
| 917 | if d.IsDir() { |
| 918 | base := filepath.Base(path) |
| 919 | if base[0] == '.' && len(base) > 1 { |
| 920 | return fs.SkipDir |
| 921 | } |
| 922 | return nil |
| 923 | } |
| 924 | if strings.HasSuffix(path, ".mk") { |
| 925 | t.files = append(t.files, path) |
| 926 | } |
| 927 | return nil |
| 928 | }) |
| 929 | return t.files |
| 930 | } |
| 931 | |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 932 | func TestGood(t *testing.T) { |
| 933 | for _, v := range known_variables { |
| 934 | KnownVariables.NewVariable(v.name, v.class, v.starlarkType) |
| 935 | } |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame^] | 936 | fs := NewFindMockFS([]string{ |
| 937 | "vendor/foo1/cfg.mk", |
| 938 | "vendor/bar/baz/cfg.mk", |
| 939 | "part.mk", |
| 940 | "foo/font.mk", |
| 941 | "bar/font.mk", |
| 942 | }) |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 943 | for _, test := range testCases { |
| 944 | t.Run(test.desc, |
| 945 | func(t *testing.T) { |
| 946 | ss, err := Convert(Request{ |
| 947 | MkFile: test.mkname, |
| 948 | Reader: bytes.NewBufferString(test.in), |
| 949 | RootDir: ".", |
| 950 | OutputSuffix: ".star", |
| 951 | WarnPartialSuccess: true, |
Sasha Smundak | 6609ba7 | 2021-07-22 18:32:56 -0700 | [diff] [blame^] | 952 | SourceFS: fs, |
| 953 | MakefileFinder: &testMakefileFinder{fs: fs}, |
Sasha Smundak | b051c4e | 2020-11-05 20:45:07 -0800 | [diff] [blame] | 954 | }) |
| 955 | if err != nil { |
| 956 | t.Error(err) |
| 957 | return |
| 958 | } |
| 959 | got := ss.String() |
| 960 | if got != test.expected { |
| 961 | t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc, |
| 962 | strings.ReplaceAll(test.expected, "\n", "\n"), |
| 963 | strings.ReplaceAll(got, "\n", "\n")) |
| 964 | } |
| 965 | }) |
| 966 | } |
| 967 | } |