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