|
| 1 | +# Copyright lowRISC contributors (OpenTitan project). |
| 2 | +# Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +load("@rules_pkg//pkg:providers.bzl", "PackageDirsInfo", "PackageFilegroupInfo", "PackageFilesInfo", "PackageSymlinkInfo") |
| 6 | + |
| 7 | +_KNOWN_PROVIDERS = [ |
| 8 | + PackageDirsInfo, |
| 9 | + PackageFilegroupInfo, |
| 10 | + PackageFilesInfo, |
| 11 | + PackageSymlinkInfo, |
| 12 | + OutputGroupInfo, |
| 13 | +] |
| 14 | + |
| 15 | +def _bool(v): |
| 16 | + if v in ("True", "true"): |
| 17 | + return True |
| 18 | + if v in ("False", "false"): |
| 19 | + return False |
| 20 | + fail("Boolean value must be 'True' or 'False'") |
| 21 | + |
| 22 | +_FLAG_CONVERSIONS = { |
| 23 | + # TODO: this needs to be the superset of all flags you might want to |
| 24 | + # modify during the build and an appropriate type conversion function. |
| 25 | + "@lowrisc_opentitan//rules:static_link_host_tools": _bool, |
| 26 | +} |
| 27 | + |
| 28 | +def _flags_transition_impl(settings, attr): |
| 29 | + result = {} |
| 30 | + for label, value in attr.flags.items(): |
| 31 | + label = str(label) |
| 32 | + if label.startswith("@@//"): |
| 33 | + label = "@lowrisc_opentitan" + label[2:] |
| 34 | + result[label] = _FLAG_CONVERSIONS[label](value) |
| 35 | + return result |
| 36 | + |
| 37 | +flags_transition = transition( |
| 38 | + implementation = _flags_transition_impl, |
| 39 | + inputs = [], |
| 40 | + outputs = _FLAG_CONVERSIONS.keys(), |
| 41 | +) |
| 42 | + |
| 43 | +def _build_with_flags_impl(ctx): |
| 44 | + # Start with DefaultInfo and then forward on any other providers we know about. |
| 45 | + result = [ctx.attr.target[DefaultInfo]] |
| 46 | + for p in _KNOWN_PROVIDERS: |
| 47 | + if p in ctx.attr.target: |
| 48 | + result.append(ctx.attr.target[p]) |
| 49 | + return result |
| 50 | + |
| 51 | +build_with_flags = rule( |
| 52 | + implementation = _build_with_flags_impl, |
| 53 | + cfg = flags_transition, |
| 54 | + attrs = { |
| 55 | + "target": attr.label(doc = "Target to build with flags"), |
| 56 | + "flags": attr.label_keyed_string_dict(doc = "Mapping of flag labels to values"), |
| 57 | + "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"), |
| 58 | + }, |
| 59 | +) |
0 commit comments