|
| 1 | +# Copyright 2025 The Bazel Authors. All rights reserved. |
| 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 | +"""This module is for implementing PEP508 environment definition. |
| 16 | +""" |
| 17 | + |
| 18 | +_platform_machine_values = { |
| 19 | + "aarch64": "arm64", |
| 20 | + "ppc": "ppc64le", |
| 21 | + "s390x": "s390x", |
| 22 | + "x86_32": "i386", |
| 23 | + "x86_64": "x86_64", |
| 24 | +} |
| 25 | +_platform_system_values = { |
| 26 | + "linux": "Linux", |
| 27 | + "osx": "Darwin", |
| 28 | + "windows": "Windows", |
| 29 | +} |
| 30 | +_sys_platform_values = { |
| 31 | + "linux": "posix", |
| 32 | + "osx": "darwin", |
| 33 | + "windows": "win32", |
| 34 | +} |
| 35 | +_os_name_values = { |
| 36 | + "linux": "posix", |
| 37 | + "osx": "posix", |
| 38 | + "windows": "nt", |
| 39 | +} |
| 40 | + |
| 41 | +def env(target_platform, *, extra = None): |
| 42 | + """Return an env target platform |
| 43 | +
|
| 44 | + Args: |
| 45 | + target_platform: {type}`str` the target platform identifier, e.g. |
| 46 | + `cp33_linux_aarch64` |
| 47 | + extra: {type}`str` the extra value to be added into the env. |
| 48 | +
|
| 49 | + Returns: |
| 50 | + A dict that can be used as `env` in the marker evaluation. |
| 51 | + """ |
| 52 | + |
| 53 | + # TODO @aignas 2025-02-13: consider moving this into config settings. |
| 54 | + |
| 55 | + env = {"extra": extra} if extra != None else {} |
| 56 | + env = env | { |
| 57 | + "implementation_name": "cpython", |
| 58 | + "platform_python_implementation": "CPython", |
| 59 | + "platform_release": "", |
| 60 | + "platform_version": "", |
| 61 | + } |
| 62 | + if target_platform.abi: |
| 63 | + minor_version, _, micro_version = target_platform.abi[3:].partition(".") |
| 64 | + micro_version = micro_version or "0" |
| 65 | + env = env | { |
| 66 | + "implementation_version": "3.{}.{}".format(minor_version, micro_version), |
| 67 | + "python_full_version": "3.{}.{}".format(minor_version, micro_version), |
| 68 | + "python_version": "3.{}".format(minor_version), |
| 69 | + } |
| 70 | + if target_platform.os and target_platform.arch: |
| 71 | + os = target_platform.os |
| 72 | + arch = target_platform.arch |
| 73 | + env = env | { |
| 74 | + "os_name": _os_name_values.get(os, ""), |
| 75 | + "platform_machine": "aarch64" if (os, arch) == ("linux", "aarch64") else _platform_machine_values.get(arch, ""), |
| 76 | + "platform_system": _platform_system_values.get(os, ""), |
| 77 | + "sys_platform": _sys_platform_values.get(os, ""), |
| 78 | + } |
| 79 | + |
| 80 | + # This is split by topic |
| 81 | + return env |
| 82 | + |
| 83 | +def _platform(*, abi = None, os = None, arch = None): |
| 84 | + return struct( |
| 85 | + abi = abi, |
| 86 | + os = os, |
| 87 | + arch = arch, |
| 88 | + ) |
| 89 | + |
| 90 | +def platform_from_str(p, python_version): |
| 91 | + """Return a platform from a string. |
| 92 | +
|
| 93 | + Args: |
| 94 | + p: {type}`str` the actual string. |
| 95 | + python_version: {type}`str` the python version to add to platform if needed. |
| 96 | +
|
| 97 | + Returns: |
| 98 | + A struct that is returned by the `_platform` function. |
| 99 | + """ |
| 100 | + if p.startswith("cp"): |
| 101 | + abi, _, p = p.partition("_") |
| 102 | + elif python_version: |
| 103 | + major, _, tail = python_version.partition(".") |
| 104 | + abi = "cp{}{}".format(major, tail) |
| 105 | + else: |
| 106 | + abi = None |
| 107 | + |
| 108 | + os, _, arch = p.partition("_") |
| 109 | + return _platform(abi = abi, os = os or None, arch = arch or None) |
0 commit comments