|
| 1 | +# Copyright 2022-2025 Broadcom. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# mypy: ignore-errors |
| 4 | +# flake8: noqa |
| 5 | +""" |
| 6 | +Template for sysconfigdata module generated at build time. |
| 7 | +
|
| 8 | +This file is used as a template to generate the _sysconfigdata module |
| 9 | +that CPython uses at runtime. It is copied verbatim (after the header comments) |
| 10 | +into the generated sysconfigdata file. |
| 11 | +
|
| 12 | +The _build_time_vars dictionary is written before this content. |
| 13 | +
|
| 14 | +Note: mypy and flake8 errors are ignored for this template file as it contains |
| 15 | +code that is valid only in the context of the generated sysconfigdata module |
| 16 | +(e.g., _build_time_vars is injected, RelenvException is in generated context). |
| 17 | +""" |
| 18 | + |
| 19 | +import pathlib |
| 20 | +import sys |
| 21 | +import platform |
| 22 | +import os |
| 23 | +import logging |
| 24 | + |
| 25 | +log = logging.getLogger(__name__) |
| 26 | + |
| 27 | + |
| 28 | +def build_arch(): |
| 29 | + machine = platform.machine() |
| 30 | + return machine.lower() |
| 31 | + |
| 32 | + |
| 33 | +def get_triplet(machine=None, plat=None): |
| 34 | + if not plat: |
| 35 | + plat = sys.platform |
| 36 | + if not machine: |
| 37 | + machine = build_arch() |
| 38 | + if plat == "darwin": |
| 39 | + return f"{machine}-macos" |
| 40 | + elif plat == "win32": |
| 41 | + return f"{machine}-win" |
| 42 | + elif plat == "linux": |
| 43 | + return f"{machine}-linux-gnu" |
| 44 | + else: |
| 45 | + raise RelenvException("Unknown platform {}".format(platform)) |
| 46 | + |
| 47 | + |
| 48 | +pydir = pathlib.Path(__file__).resolve().parent |
| 49 | +if sys.platform == "win32": |
| 50 | + DEFAULT_DATA_DIR = pathlib.Path.home() / "AppData" / "Local" / "relenv" |
| 51 | +else: |
| 52 | + DEFAULT_DATA_DIR = pathlib.Path.home() / ".local" / "relenv" |
| 53 | + |
| 54 | +if "RELENV_DATA" in os.environ: |
| 55 | + DATA_DIR = pathlib.Path(os.environ["RELENV_DATA"]).resolve() |
| 56 | +else: |
| 57 | + DATA_DIR = DEFAULT_DATA_DIR |
| 58 | + |
| 59 | +buildroot = pydir.parent.parent |
| 60 | + |
| 61 | +toolchain = DATA_DIR / "toolchain" / get_triplet() |
| 62 | + |
| 63 | +build_time_vars = {} |
| 64 | +for key in _build_time_vars: |
| 65 | + val = _build_time_vars[key] |
| 66 | + orig = val |
| 67 | + if isinstance(val, str): |
| 68 | + val = val.format( |
| 69 | + BUILDROOT=buildroot, |
| 70 | + TOOLCHAIN=toolchain, |
| 71 | + ) |
| 72 | + build_time_vars[key] = val |
0 commit comments