|
| 1 | +"""Implement a flag for matching the dependency specifiers at analysis time.""" |
| 2 | + |
| 3 | +load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") |
| 4 | +load("//python/private:toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE") |
| 5 | +load( |
| 6 | + ":pep508_env.bzl", |
| 7 | + "env_aliases", |
| 8 | + "os_name_select_map", |
| 9 | + "platform_machine_select_map", |
| 10 | + "platform_system_select_map", |
| 11 | + "sys_platform_select_map", |
| 12 | +) |
| 13 | +load(":pep508_evaluate.bzl", "evaluate") |
| 14 | + |
| 15 | +# Use capitals to hint its not an actual boolean type. |
| 16 | +_ENV_MARKER_TRUE = "TRUE" |
| 17 | +_ENV_MARKER_FALSE = "FALSE" |
| 18 | + |
| 19 | +def env_marker_setting(*, name, expression, **kwargs): |
| 20 | + """Creates an env_marker setting. |
| 21 | +
|
| 22 | + Generated targets: |
| 23 | +
|
| 24 | + * `is_{name}_true`: config_setting that matches when the expression is true. |
| 25 | + * `{name}`: env marker target that evalutes the expression. |
| 26 | +
|
| 27 | + Args: |
| 28 | + name: {type}`str` target name |
| 29 | + expression: {type}`str` the environment marker string to evaluate |
| 30 | + **kwargs: {type}`dict` additional common kwargs. |
| 31 | + """ |
| 32 | + native.config_setting( |
| 33 | + name = "is_{}_true".format(name), |
| 34 | + flag_values = { |
| 35 | + ":{}".format(name): _ENV_MARKER_TRUE, |
| 36 | + }, |
| 37 | + **kwargs |
| 38 | + ) |
| 39 | + _env_marker_setting( |
| 40 | + name = name, |
| 41 | + expression = expression, |
| 42 | + os_name = select(os_name_select_map), |
| 43 | + sys_platform = select(sys_platform_select_map), |
| 44 | + platform_machine = select(platform_machine_select_map), |
| 45 | + platform_system = select(platform_system_select_map), |
| 46 | + platform_release = select({ |
| 47 | + "@platforms//os:osx": "USE_OSX_VERSION_FLAG", |
| 48 | + "//conditions:default": "", |
| 49 | + }), |
| 50 | + **kwargs |
| 51 | + ) |
| 52 | + |
| 53 | +def _env_marker_setting_impl(ctx): |
| 54 | + env = {} |
| 55 | + |
| 56 | + runtime = ctx.toolchains[TARGET_TOOLCHAIN_TYPE].py3_runtime |
| 57 | + if runtime.interpreter_version_info: |
| 58 | + version_info = runtime.interpreter_version_info |
| 59 | + env["python_version"] = "{major}.{minor}".format( |
| 60 | + major = version_info.major, |
| 61 | + minor = version_info.minor, |
| 62 | + ) |
| 63 | + full_version = _format_full_version(version_info) |
| 64 | + env["python_full_version"] = full_version |
| 65 | + env["implementation_version"] = full_version |
| 66 | + else: |
| 67 | + env["python_version"] = _get_flag(ctx.attr._python_version_major_minor_flag) |
| 68 | + full_version = _get_flag(ctx.attr._python_full_version_flag) |
| 69 | + env["python_full_version"] = full_version |
| 70 | + env["implementation_version"] = full_version |
| 71 | + |
| 72 | + # We assume cpython if the toolchain doesn't specify because it's most |
| 73 | + # likely to be true. |
| 74 | + env["implementation_name"] = runtime.implementation_name or "cpython" |
| 75 | + env["os_name"] = ctx.attr.os_name |
| 76 | + env["sys_platform"] = ctx.attr.sys_platform |
| 77 | + env["platform_machine"] = ctx.attr.platform_machine |
| 78 | + |
| 79 | + # The `platform_python_implementation` marker value is supposed to come |
| 80 | + # from `platform.python_implementation()`, however, PEP 421 introduced |
| 81 | + # `sys.implementation.name` and the `implementation_name` env marker to |
| 82 | + # replace it. Per the platform.python_implementation docs, there's now |
| 83 | + # essentially just two possible "registered" values: CPython or PyPy. |
| 84 | + # Rather than add a field to the toolchain, we just special case the value |
| 85 | + # from `sys.implementation.name` to handle the two documented values. |
| 86 | + platform_python_impl = runtime.implementation_name |
| 87 | + if platform_python_impl == "cpython": |
| 88 | + platform_python_impl = "CPython" |
| 89 | + elif platform_python_impl == "pypy": |
| 90 | + platform_python_impl = "PyPy" |
| 91 | + env["platform_python_implementation"] = platform_python_impl |
| 92 | + |
| 93 | + # NOTE: Platform release for Android will be Android version: |
| 94 | + # https://peps.python.org/pep-0738/#platform |
| 95 | + # Similar for iOS: |
| 96 | + # https://peps.python.org/pep-0730/#platform |
| 97 | + platform_release = ctx.attr.platform_release |
| 98 | + if platform_release == "USE_OSX_VERSION_FLAG": |
| 99 | + platform_release = _get_flag(ctx.attr._pip_whl_osx_version_flag) |
| 100 | + env["platform_release"] = platform_release |
| 101 | + env["platform_system"] = ctx.attr.platform_system |
| 102 | + |
| 103 | + # For lack of a better option, just use an empty string for now. |
| 104 | + env["platform_version"] = "" |
| 105 | + |
| 106 | + env.update(env_aliases()) |
| 107 | + |
| 108 | + if evaluate(ctx.attr.expression, env = env): |
| 109 | + value = _ENV_MARKER_TRUE |
| 110 | + else: |
| 111 | + value = _ENV_MARKER_FALSE |
| 112 | + return [config_common.FeatureFlagInfo(value = value)] |
| 113 | + |
| 114 | +_env_marker_setting = rule( |
| 115 | + doc = """ |
| 116 | +Evaluates an environment marker expression using target configuration info. |
| 117 | +
|
| 118 | +See |
| 119 | +https://packaging.python.org/en/latest/specifications/dependency-specifiers |
| 120 | +for the specification of behavior. |
| 121 | +""", |
| 122 | + implementation = _env_marker_setting_impl, |
| 123 | + attrs = { |
| 124 | + "expression": attr.string( |
| 125 | + mandatory = True, |
| 126 | + doc = "Environment marker expression to evaluate.", |
| 127 | + ), |
| 128 | + "os_name": attr.string(), |
| 129 | + "platform_machine": attr.string(), |
| 130 | + "platform_release": attr.string(), |
| 131 | + "platform_system": attr.string(), |
| 132 | + "sys_platform": attr.string(), |
| 133 | + "_pip_whl_osx_version_flag": attr.label( |
| 134 | + default = "//python/config_settings:pip_whl_osx_version", |
| 135 | + providers = [[BuildSettingInfo], [config_common.FeatureFlagInfo]], |
| 136 | + ), |
| 137 | + "_python_full_version_flag": attr.label( |
| 138 | + default = "//python/config_settings:python_version", |
| 139 | + providers = [config_common.FeatureFlagInfo], |
| 140 | + ), |
| 141 | + "_python_version_major_minor_flag": attr.label( |
| 142 | + default = "//python/config_settings:python_version_major_minor", |
| 143 | + providers = [config_common.FeatureFlagInfo], |
| 144 | + ), |
| 145 | + }, |
| 146 | + provides = [config_common.FeatureFlagInfo], |
| 147 | + toolchains = [ |
| 148 | + TARGET_TOOLCHAIN_TYPE, |
| 149 | + ], |
| 150 | +) |
| 151 | + |
| 152 | +def _format_full_version(info): |
| 153 | + """Format the full python interpreter version. |
| 154 | +
|
| 155 | + Adapted from spec code at: |
| 156 | + https://packaging.python.org/en/latest/specifications/dependency-specifiers/#environment-markers |
| 157 | +
|
| 158 | + Args: |
| 159 | + info: The provider from the Python runtime. |
| 160 | +
|
| 161 | + Returns: |
| 162 | + a {type}`str` with the version |
| 163 | + """ |
| 164 | + kind = info.releaselevel |
| 165 | + if kind == "final": |
| 166 | + kind = "" |
| 167 | + serial = "" |
| 168 | + else: |
| 169 | + kind = kind[0] if kind else "" |
| 170 | + serial = str(info.serial) if info.serial else "" |
| 171 | + |
| 172 | + return "{major}.{minor}.{micro}{kind}{serial}".format( |
| 173 | + v = info, |
| 174 | + major = info.major, |
| 175 | + minor = info.minor, |
| 176 | + micro = info.micro, |
| 177 | + kind = kind, |
| 178 | + serial = serial, |
| 179 | + ) |
| 180 | + |
| 181 | +def _get_flag(t): |
| 182 | + if config_common.FeatureFlagInfo in t: |
| 183 | + return t[config_common.FeatureFlagInfo].value |
| 184 | + if BuildSettingInfo in t: |
| 185 | + return t[BuildSettingInfo].value |
| 186 | + fail("Should not occur: {} does not have necessary providers") |
0 commit comments