Files
cibuildwheel/test/utils.py
T

373 lines
12 KiB
Python
Raw Normal View History

2021-05-03 11:45:43 -04:00
"""
Utility functions used by the cibuildwheel tests.
This file is added to the PYTHONPATH in the test runner at bin/run_test.py.
2021-05-03 11:45:43 -04:00
"""
2019-11-12 23:51:27 +00:00
import os
2020-02-20 16:12:54 -05:00
import platform as pm
2019-11-12 23:51:27 +00:00
import subprocess
import sys
2024-10-22 10:16:59 -04:00
from collections.abc import Mapping, Sequence
from pathlib import Path
2021-02-14 20:44:20 +01:00
from tempfile import TemporaryDirectory
2024-10-22 10:16:59 -04:00
from typing import Any, Final
2024-05-28 05:31:36 -07:00
import pytest
from cibuildwheel.architecture import Architecture
from cibuildwheel.ci import CIProvider, detect_ci_provider
2025-01-27 20:35:56 +01:00
from cibuildwheel.util.file import CIBW_CACHE_PATH
EMULATED_ARCHS: Final[list[str]] = sorted(
arch.value for arch in (Architecture.all_archs("linux") - Architecture.auto_archs("linux"))
)
2024-05-26 12:41:20 +02:00
SINGLE_PYTHON_VERSION: Final[tuple[int, int]] = (3, 12)
_AARCH64_CAN_RUN_ARMV7: Final[bool] = Architecture.aarch64.value not in EMULATED_ARCHS and {
None: Architecture.armv7l.value not in EMULATED_ARCHS,
CIProvider.travis_ci: False,
CIProvider.cirrus_ci: False,
}.get(detect_ci_provider(), True)
2024-08-14 15:56:06 -04:00
platform = os.environ.get("CIBW_PLATFORM", "")
if platform:
pass
2021-05-03 11:45:43 -04:00
elif sys.platform.startswith("linux"):
platform = "linux"
elif sys.platform.startswith("darwin"):
platform = "macos"
2024-08-14 15:56:06 -04:00
elif sys.platform.startswith(("win32", "cygwin")):
2021-05-03 11:45:43 -04:00
platform = "windows"
else:
2022-09-05 13:11:46 -04:00
msg = f"Unsupported platform {sys.platform!r}"
raise Exception(msg)
2019-04-27 12:12:02 +01:00
2024-10-22 10:16:59 -04:00
def cibuildwheel_get_build_identifiers(
project_path: Path, env: dict[str, str] | None = None, *, prerelease_pythons: bool = False
) -> list[str]:
2021-05-03 11:45:43 -04:00
"""
Returns the list of build identifiers that cibuildwheel will try to build
for the current platform.
2021-05-03 11:45:43 -04:00
"""
cmd = [sys.executable, "-m", "cibuildwheel", "--print-build-identifiers", str(project_path)]
if env is None:
env = os.environ.copy()
env["CIBW_ENABLE"] = "cpython-freethreading pypy"
if prerelease_pythons:
env["CIBW_ENABLE"] += " cpython-prerelease"
2021-02-14 12:56:33 -05:00
cmd_output = subprocess.run(
cmd,
text=True,
env=env,
2021-02-14 12:56:33 -05:00
check=True,
stdout=subprocess.PIPE,
).stdout
2021-05-03 11:45:43 -04:00
return cmd_output.strip().split("\n")
def _update_pip_cache_dir(env: dict[str, str]) -> None:
# Fix for pip concurrency bug https://github.com/pypa/pip/issues/11340
# See https://github.com/pypa/cibuildwheel/issues/1254 for discussion.
if platform == "linux":
return
if "PIP_CACHE_DIR" in env:
return
worker_id = os.environ.get("PYTEST_XDIST_WORKER")
if worker_id is None or worker_id == "gw0":
return
pip_cache_dir = CIBW_CACHE_PATH / "test_cache" / f"pip_cache_dir_{worker_id}"
env["PIP_CACHE_DIR"] = str(pip_cache_dir)
def cibuildwheel_run(
2024-10-22 10:16:59 -04:00
project_path: str | Path,
package_dir: str | Path = ".",
env: dict[str, str] | None = None,
add_env: Mapping[str, str] | None = None,
output_dir: Path | None = None,
add_args: Sequence[str] | None = None,
single_python: bool = False,
) -> list[str]:
2021-05-03 11:45:43 -04:00
"""
2019-09-28 19:24:16 +02:00
Runs cibuildwheel as a subprocess, building the project at project_path.
Uses the current Python interpreter.
:param project_path: path of the project to be built.
:param package_dir: path of the package to be built. Can be absolute, or
relative to project_path.
:param env: full environment to be used, os.environ if None
:param add_env: environment used to update env
:param output_dir: directory where wheels are saved. If None, a temporary
directory will be used for the duration of the command.
:param add_args: Additional command-line arguments to pass to cibuildwheel.
:return: list of built wheels (file names).
2021-05-03 11:45:43 -04:00
"""
if env is None:
env = os.environ.copy()
# If present in the host environment, remove the MACOSX_DEPLOYMENT_TARGET for consistency
2021-05-03 11:45:43 -04:00
env.pop("MACOSX_DEPLOYMENT_TARGET", None)
2019-09-28 19:24:16 +02:00
if add_args is None:
add_args = []
if add_env is not None:
env.update(add_env)
_update_pip_cache_dir(env)
env["CIBW_ENABLE"] = "cpython-prerelease cpython-freethreading pypy"
2024-05-26 12:41:20 +02:00
if single_python:
env["CIBW_BUILD"] = "cp{}{}-*".format(*SINGLE_PYTHON_VERSION)
2021-02-14 20:44:20 +01:00
with TemporaryDirectory() as tmp_output_dir:
2021-02-14 12:56:33 -05:00
subprocess.run(
2021-04-30 17:56:34 -04:00
[
sys.executable,
2021-05-03 11:45:43 -04:00
"-m",
"cibuildwheel",
"--output-dir",
2021-02-14 20:44:20 +01:00
str(output_dir or tmp_output_dir),
2021-04-30 17:56:34 -04:00
str(package_dir),
*add_args,
],
env=env,
cwd=project_path,
2021-02-14 12:56:33 -05:00
check=True,
)
2021-02-14 20:44:20 +01:00
wheels = os.listdir(output_dir or tmp_output_dir)
return wheels
def _floor_macosx(*args: str) -> str:
2021-05-03 11:45:43 -04:00
"""
Make sure a deployment target is not less than some value.
2021-05-03 11:45:43 -04:00
"""
return max(args, key=lambda x: tuple(map(int, x.split("."))))
2021-01-08 14:59:16 +00:00
2021-04-30 17:56:34 -04:00
def expected_wheels(
2024-10-22 10:16:59 -04:00
package_name: str,
package_version: str,
manylinux_versions: list[str] | None = None,
musllinux_versions: list[str] | None = None,
macosx_deployment_target: str = "10.9",
machine_arch: str | None = None,
python_abi_tags: list[str] | None = None,
include_universal2: bool = False,
single_python: bool = False,
single_arch: bool = False,
) -> list[str]:
2021-05-03 11:45:43 -04:00
"""
Returns a list of expected wheels from a run of cibuildwheel.
2021-05-03 11:45:43 -04:00
"""
2019-12-02 11:07:56 +01:00
# per PEP 425 (https://www.python.org/dev/peps/pep-0425/), wheel files shall have name of the form
# {distribution}-{version}(-{build tag})?-{python tag}-{abi tag}-{platform tag}.whl
# {python tag} and {abi tag} are closely related to the python interpreter used to build the wheel
# so we'll merge them below as python_abi_tag
2020-02-20 16:12:54 -05:00
2020-12-21 11:06:25 +00:00
if machine_arch is None:
machine_arch = pm.machine()
if platform == "linux" and machine_arch.lower() == "arm64":
# we're running linux tests from macOS/Windows arm64, override platform
machine_arch = "aarch64"
2020-12-21 11:06:25 +00:00
2020-04-06 23:10:19 +01:00
if manylinux_versions is None:
if machine_arch in ("armv7l", "aarch64"):
2024-11-16 01:07:24 +01:00
manylinux_versions = ["manylinux_2_17", "manylinux2014", "manylinux_2_31"]
2024-10-01 16:33:57 +02:00
elif machine_arch == "x86_64":
manylinux_versions = [
"manylinux_2_5",
"manylinux1",
"manylinux_2_17",
"manylinux2014",
]
2020-02-20 16:12:54 -05:00
else:
manylinux_versions = ["manylinux_2_17", "manylinux2014"]
2020-04-06 23:10:19 +01:00
if musllinux_versions is None:
musllinux_versions = ["musllinux_1_2"]
2024-05-28 05:31:36 -07:00
if platform == "pyodide" and python_abi_tags is None:
python_abi_tags = ["cp312-cp312"]
if python_abi_tags is None:
python_abi_tags = [
"cp38-cp38",
"cp39-cp39",
"cp310-cp310",
"cp311-cp311",
2023-05-26 21:14:55 -07:00
"cp312-cp312",
2024-05-12 02:26:18 +02:00
"cp313-cp313",
"cp313-cp313t",
]
2020-04-06 23:10:19 +01:00
if machine_arch in ["x86_64", "AMD64", "x86", "aarch64"]:
2023-06-17 12:00:27 +02:00
python_abi_tags += [
"pp38-pypy38_pp73",
"pp39-pypy39_pp73",
"pp310-pypy310_pp73",
2025-02-12 16:51:33 +01:00
"pp311-pypy311_pp73",
2023-06-17 12:00:27 +02:00
]
2020-04-06 23:10:19 +01:00
if platform == "macos" and machine_arch == "arm64":
# arm64 macs are only supported by cp38+
2023-01-14 09:48:01 +01:00
python_abi_tags = [
"cp38-cp38",
"cp39-cp39",
"cp310-cp310",
"cp311-cp311",
2023-05-26 21:14:55 -07:00
"cp312-cp312",
2024-05-12 02:26:18 +02:00
"cp313-cp313",
"cp313-cp313t",
2023-01-14 09:48:01 +01:00
"pp38-pypy38_pp73",
"pp39-pypy39_pp73",
2023-06-17 12:00:27 +02:00
"pp310-pypy310_pp73",
2025-02-12 16:51:33 +01:00
"pp311-pypy311_pp73",
2023-01-14 09:48:01 +01:00
]
2021-01-08 14:59:16 +00:00
2024-05-26 12:41:20 +02:00
if single_python:
python_tag = "cp{}{}-".format(*SINGLE_PYTHON_VERSION)
python_abi_tags = [
next(
tag
for tag in python_abi_tags
if tag.startswith(python_tag) and not tag.endswith("t")
)
]
2020-04-06 23:10:19 +01:00
wheels = []
2024-05-28 05:31:36 -07:00
if platform == "pyodide":
assert len(python_abi_tags) == 1
python_abi_tag = python_abi_tags[0]
platform_tag = "pyodide_2024_0_wasm32"
return [f"{package_name}-{package_version}-{python_abi_tag}-{platform_tag}.whl"]
2020-04-06 23:10:19 +01:00
for python_abi_tag in python_abi_tags:
platform_tags = []
2021-05-03 11:45:43 -04:00
if platform == "linux":
architectures = [arch_name_for_linux(machine_arch)]
2020-04-06 23:10:19 +01:00
if not single_arch:
if machine_arch == "x86_64":
architectures.append("i686")
elif (
machine_arch == "aarch64"
and sys.platform.startswith("linux")
and not python_abi_tag.startswith("pp")
and _AARCH64_CAN_RUN_ARMV7
):
architectures.append("armv7l")
2020-04-06 23:10:19 +01:00
2023-08-07 18:01:10 +02:00
if len(manylinux_versions) > 0:
platform_tags = [
".".join(
f"{manylinux_version}_{architecture}"
for manylinux_version in manylinux_versions
if (manylinux_version, architecture) != ("manylinux_2_31", "aarch64")
2023-08-07 18:01:10 +02:00
)
for architecture in architectures
]
if len(musllinux_versions) > 0 and not python_abi_tag.startswith("pp"):
platform_tags.extend(
[
".".join(
f"{musllinux_version}_{architecture}"
for musllinux_version in musllinux_versions
)
for architecture in architectures
]
)
2021-05-03 11:45:43 -04:00
elif platform == "windows":
if python_abi_tag.startswith("pp"):
platform_tags = ["win_amd64"]
else:
platform_tags = ["win32", "win_amd64"]
2021-05-03 11:45:43 -04:00
elif platform == "macos":
2024-09-12 15:32:22 -04:00
if python_abi_tag.startswith("pp"):
2025-02-25 20:23:01 +01:00
if python_abi_tag.startswith("pp38"):
2024-09-12 15:32:22 -04:00
min_macosx = macosx_deployment_target
else:
min_macosx = _floor_macosx(macosx_deployment_target, "10.15")
elif python_abi_tag.startswith("cp"):
2025-02-25 20:23:01 +01:00
if python_abi_tag.startswith(("cp38", "cp39", "cp310", "cp311")):
2024-09-12 15:32:22 -04:00
min_macosx = macosx_deployment_target
else:
min_macosx = _floor_macosx(macosx_deployment_target, "10.13")
else:
min_macosx = macosx_deployment_target
if machine_arch == "arm64":
2024-09-12 15:32:22 -04:00
arm64_macosx = _floor_macosx(min_macosx, "11.0")
platform_tags = [f"macosx_{arm64_macosx.replace('.', '_')}_arm64"]
2021-01-01 18:56:36 +00:00
else:
platform_tags = [f"macosx_{min_macosx.replace('.', '_')}_x86_64"]
if include_universal2:
platform_tags.append(f"macosx_{min_macosx.replace('.', '_')}_universal2")
2020-04-06 23:10:19 +01:00
else:
2022-09-05 13:11:46 -04:00
msg = f"Unsupported platform {platform!r}"
raise Exception(msg)
2020-02-20 16:17:51 -05:00
2020-04-06 23:10:19 +01:00
for platform_tag in platform_tags:
2021-05-03 11:45:43 -04:00
wheels.append(f"{package_name}-{package_version}-{python_abi_tag}-{platform_tag}.whl")
2019-12-02 11:07:56 +01:00
2020-04-06 23:10:19 +01:00
return wheels
2019-12-02 11:07:56 +01:00
2024-10-22 10:16:59 -04:00
def get_macos_version() -> tuple[int, int]:
2021-05-03 11:45:43 -04:00
"""
Returns the macOS major/minor version, as a tuple, e.g. (10, 15) or (11, 0)
These tuples can be used in comparisons, e.g.
(10, 14) <= (11, 0) == True
(11, 2) <= (11, 0) != True
2021-05-03 11:45:43 -04:00
"""
version_str, _, _ = pm.mac_ver()
2024-10-22 10:16:59 -04:00
return tuple(map(int, version_str.split(".")[:2])) # type: ignore[return-value]
def get_xcode_version() -> tuple[int, int]:
"""Calls `xcodebuild -version` to retrieve the Xcode version as a 2-tuple."""
output = subprocess.run(
["xcodebuild", "-version"],
text=True,
check=True,
stdout=subprocess.PIPE,
).stdout
lines = output.splitlines()
_, version_str = lines[0].split()
version_parts = version_str.split(".")
return (int(version_parts[0]), int(version_parts[1]))
2024-10-22 10:16:59 -04:00
def skip_if_pyodide(reason: str) -> Any:
2024-05-28 05:31:36 -07:00
return pytest.mark.skipif(platform == "pyodide", reason=reason)
def invoke_pytest() -> str:
# see https://github.com/pyodide/pyodide/issues/4802
if platform == "pyodide" and sys.platform.startswith("darwin"):
return "python -m pytest"
return "pytest"
2024-10-22 10:16:59 -04:00
def arch_name_for_linux(arch: str) -> str:
"""
Archs have different names on different platforms, but it's useful to be
able to run linux tests on dev machines. This function translates between
the different names.
"""
if arch == "arm64":
return "aarch64"
return arch