|
| 1 | +"""A small utility module dedicated to detecting whether or not the `--enable_runfiles` and `--windows_enable_symlinks` flag are enabled |
| 2 | +""" |
| 3 | + |
| 4 | +load("@bazel_skylib//lib:selects.bzl", "selects") |
| 5 | +load("@bazel_skylib//rules:common_settings.bzl", "bool_setting") |
| 6 | + |
| 7 | +RunfilesEnabledInfo = provider( |
| 8 | + doc = "A singleton provider that contains the raw value of a build setting", |
| 9 | + fields = { |
| 10 | + "value": "The value of the build setting in the current configuration. " + |
| 11 | + "This value may come from the command line or an upstream transition, " + |
| 12 | + "or else it will be the build setting's default.", |
| 13 | + }, |
| 14 | +) |
| 15 | + |
| 16 | +def _runfiles_enabled_setting_impl(ctx): |
| 17 | + return RunfilesEnabledInfo(value = ctx.attr.value) |
| 18 | + |
| 19 | +runfiles_enabled_setting = rule( |
| 20 | + implementation = _runfiles_enabled_setting_impl, |
| 21 | + doc = "A bool-typed build setting that cannot be set on the command line", |
| 22 | + attrs = { |
| 23 | + "value": attr.bool( |
| 24 | + doc = "A boolean value", |
| 25 | + mandatory = True, |
| 26 | + ), |
| 27 | + }, |
| 28 | +) |
| 29 | + |
| 30 | +_RUNFILES_ENABLED_ATTR_NAME = "_runfiles_enabled" |
| 31 | + |
| 32 | +def runfiles_enabled_attr(default): |
| 33 | + return { |
| 34 | + _RUNFILES_ENABLED_ATTR_NAME: attr.label( |
| 35 | + doc = "A flag representing whether or not runfiles are enabled.", |
| 36 | + providers = [RunfilesEnabledInfo], |
| 37 | + default = default, |
| 38 | + cfg = "exec", |
| 39 | + ), |
| 40 | + } |
| 41 | + |
| 42 | +def runfiles_enabled_build_setting(name, **kwargs): |
| 43 | + """Define a build setting identifying if runfiles are enabled. |
| 44 | +
|
| 45 | + Args: |
| 46 | + name (str): The name of the build setting |
| 47 | + **kwargs: Additional keyword arguments for the target. |
| 48 | + """ |
| 49 | + native.config_setting( |
| 50 | + name = "{}__enable_runfiles".format(name), |
| 51 | + values = {"enable_runfiles": "true"}, |
| 52 | + ) |
| 53 | + |
| 54 | + native.config_setting( |
| 55 | + name = "{}__disable_runfiles".format(name), |
| 56 | + values = {"enable_runfiles": "false"}, |
| 57 | + ) |
| 58 | + |
| 59 | + bool_setting( |
| 60 | + name = "{}__always_true".format(name), |
| 61 | + build_setting_default = True, |
| 62 | + ) |
| 63 | + |
| 64 | + native.config_setting( |
| 65 | + name = "{}__always_true_setting".format(name), |
| 66 | + flag_values = {":{}__always_true".format(name): "True"}, |
| 67 | + ) |
| 68 | + |
| 69 | + native.config_setting( |
| 70 | + name = "{}__always_false_setting".format(name), |
| 71 | + flag_values = {":{}__always_true".format(name): "False"}, |
| 72 | + ) |
| 73 | + |
| 74 | + # There is no way to query a setting that is unset. By utilizing constant |
| 75 | + # settings, we can filter to a fallback setting where no known value is |
| 76 | + # defined. |
| 77 | + native.alias( |
| 78 | + name = "{}__unset_runfiles".format(name), |
| 79 | + actual = select({ |
| 80 | + ":{}__disable_runfiles".format(name): ":{}__always_false_setting".format(name), |
| 81 | + ":{}__enable_runfiles".format(name): ":{}__always_false_setting".format(name), |
| 82 | + "//conditions:default": ":{}__always_true_setting".format(name), |
| 83 | + }), |
| 84 | + ) |
| 85 | + |
| 86 | + selects.config_setting_group( |
| 87 | + name = "{}__windows_enable_runfiles".format(name), |
| 88 | + match_all = [ |
| 89 | + ":{}__enable_runfiles".format(name), |
| 90 | + "@platforms//os:windows", |
| 91 | + ], |
| 92 | + ) |
| 93 | + |
| 94 | + selects.config_setting_group( |
| 95 | + name = "{}__windows_disable_runfiles".format(name), |
| 96 | + match_all = [ |
| 97 | + ":{}__disable_runfiles".format(name), |
| 98 | + "@platforms//os:windows", |
| 99 | + ], |
| 100 | + ) |
| 101 | + |
| 102 | + selects.config_setting_group( |
| 103 | + name = "{}__windows_unset_runfiles".format(name), |
| 104 | + match_all = [ |
| 105 | + ":{}__unset_runfiles".format(name), |
| 106 | + "@platforms//os:windows", |
| 107 | + ], |
| 108 | + ) |
| 109 | + |
| 110 | + native.alias( |
| 111 | + name = "{}__unix".format(name), |
| 112 | + actual = select({ |
| 113 | + "@platforms//os:windows": ":{}__always_false_setting".format(name), |
| 114 | + "//conditions:default": ":{}__always_true_setting".format(name), |
| 115 | + }), |
| 116 | + ) |
| 117 | + |
| 118 | + selects.config_setting_group( |
| 119 | + name = "{}__unix_enable_runfiles".format(name), |
| 120 | + match_all = [ |
| 121 | + ":{}__enable_runfiles".format(name), |
| 122 | + ":{}__unix".format(name), |
| 123 | + ], |
| 124 | + ) |
| 125 | + |
| 126 | + selects.config_setting_group( |
| 127 | + name = "{}__unix_disable_runfiles".format(name), |
| 128 | + match_all = [ |
| 129 | + ":{}__disable_runfiles".format(name), |
| 130 | + ":{}__unix".format(name), |
| 131 | + ], |
| 132 | + ) |
| 133 | + |
| 134 | + selects.config_setting_group( |
| 135 | + name = "{}__unix_unset_runfiles".format(name), |
| 136 | + match_all = [ |
| 137 | + ":{}__unset_runfiles".format(name), |
| 138 | + ":{}__unix".format(name), |
| 139 | + ], |
| 140 | + ) |
| 141 | + |
| 142 | + runfiles_enabled_setting( |
| 143 | + name = name, |
| 144 | + value = select({ |
| 145 | + ":{}__windows_enable_runfiles".format(name): True, |
| 146 | + ":{}__windows_disable_runfiles".format(name): False, |
| 147 | + ":{}__windows_unset_runfiles".format(name): False, |
| 148 | + ":{}__unix_enable_runfiles".format(name): True, |
| 149 | + ":{}__unix_disable_runfiles".format(name): False, |
| 150 | + ":{}__unix_unset_runfiles".format(name): True, |
| 151 | + "//conditions:default": True, |
| 152 | + }), |
| 153 | + **kwargs |
| 154 | + ) |
| 155 | + |
| 156 | +def is_runfiles_enabled(attr): |
| 157 | + """Determine whether or not runfiles are enabled. |
| 158 | +
|
| 159 | + Args: |
| 160 | + attr (struct): A rule's struct of attributes (`ctx.attr`) |
| 161 | + Returns: |
| 162 | + bool: The enable_runfiles value. |
| 163 | + """ |
| 164 | + |
| 165 | + runfiles_enabled = getattr(attr, _RUNFILES_ENABLED_ATTR_NAME, None) |
| 166 | + |
| 167 | + return runfiles_enabled[RunfilesEnabledInfo].value if runfiles_enabled else True |
0 commit comments