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