Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/scikit_build_core/builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from ..resources import find_python
from ..settings.skbuild_model import ScikitBuildSettings
from .generator import set_environment_for_gen
from .sysconfig import get_python_include_dir, get_python_library
from .sysconfig import get_platform, get_python_include_dir, get_python_library

__all__: list[str] = ["Builder"]

Expand Down Expand Up @@ -45,6 +45,9 @@ def get_archs(self) -> list[str]:
if sys.platform.startswith("darwin"):
archs = re.findall(r"-arch (\S+)", self.config.env.get("ARCHFLAGS", ""))
return archs
if sys.platform.startswith("win"):
if get_platform(self.config.env) == "win-arm64":
return ["win_arm64"]

return []

Expand Down Expand Up @@ -94,9 +97,13 @@ def configure(
if version is not None:
cache_config["SKBUILD_PROJECT_VERSION"] = str(version)

# Classic Find Python
python_library = get_python_library()
if limited_abi is None:
limited_abi = self.settings.wheel.py_api.startswith("cp3")

python_library = get_python_library(self.config.env, abi3=limited_abi)
python_include_dir = get_python_include_dir()

# Classic Find Python
cache_config["PYTHON_EXECUTABLE"] = sys.executable
cache_config["PYTHON_INCLUDE_DIR"] = python_include_dir
if python_library:
Expand All @@ -109,9 +116,6 @@ def configure(
cache_config[f"{prefix}_INCLUDE_DIR"] = python_include_dir
cache_config[f"{prefix}_FIND_REGISTRY"] = "NEVER"

if limited_abi is None:
limited_abi = self.settings.wheel.py_api.startswith("cp3")

if limited_abi:
cache_config["SKBUILD_SOABI"] = (
"" if sys.platform.startswith("win") else "abi3"
Expand Down
21 changes: 18 additions & 3 deletions src/scikit_build_core/builder/sysconfig.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import configparser
import os
import sys
import sysconfig
Expand Down Expand Up @@ -30,7 +31,18 @@ def __dir__() -> list[str]:
return __all__


def get_python_library() -> Path | None:
def get_python_library(env: Mapping[str, str], *, abi3: bool = False) -> Path | None:
# When cross-compiling, check DIST_EXTRA_CONFIG first
config_file = env.get("DIST_EXTRA_CONFIG", None)
if config_file and Path(config_file).is_file():
cp = configparser.ConfigParser()
cp.read(config_file)
result = cp.get("build_ext", "library_dirs", fallback="")
if result:
logger.info("Reading DIST_EXTRA_CONFIG:build_ext.library_dirs={}", result)
minor = "" if abi3 else sys.version_info[1]
return Path(result) / f"python3{minor}.lib"

libdirstr = sysconfig.get_config_var("LIBDIR")
ldlibrarystr = sysconfig.get_config_var("LDLIBRARY")
libdir: Path | None = libdirstr and Path(libdirstr)
Expand Down Expand Up @@ -96,8 +108,11 @@ def get_platform(env: Mapping[str, str] | None = None) -> str:
"""
if env is None:
env = os.environ
if os.name == "nt" and "VSCMD_ARG_TGT_ARCH" in env:
return TARGET_TO_PLAT.get(env["VSCMD_ARG_TGT_ARCH"]) or get_host_platform()
if sys.platform.startswith("win"):
if "VSCMD_ARG_TGT_ARCH" in env:
return TARGET_TO_PLAT.get(env["VSCMD_ARG_TGT_ARCH"]) or get_host_platform()
if "arm64" in env.get("SETUPTOOLS_EXT_SUFFIX", "").lower():
return "win-arm64"
return get_host_platform()


Expand Down
23 changes: 22 additions & 1 deletion tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import sysconfig
import typing
from pathlib import Path
from types import SimpleNamespace

import pytest
Expand Down Expand Up @@ -54,14 +55,34 @@ def test_get_python_include_dir():
def test_get_python_library():
pprint.pprint(sysconfig.get_config_vars())

lib = get_python_library()
lib = get_python_library({})
if sys.platform.startswith("win"):
assert lib is None
else:
assert lib
assert lib.is_file()


@pytest.mark.skipif(not sys.platform.startswith("win"), reason="Windows only")
def test_get_python_library_xcompile(tmp_path):
config_path = tmp_path / "tmp.cfg"
config_path.write_text(
"""\
[build_ext]
library_dirs=C:\\Python\\libs
"""
)
env = {"DIST_EXTRA_CONFIG": str(config_path)}
lib = get_python_library(env)
assert lib
assert lib.parent == Path("C:\\Python\\libs")
assert lib.parent != Path("C:\\Python\\libs\\python3.lib")

lib2 = get_python_library(env, abi3=True)
assert lib2
assert lib2 == Path("C:\\Python\\libs\\python3.lib")


@pytest.mark.parametrize("archs", (["x86_64"], ["arm64", "universal2"]))
def test_builder_macos_arch(monkeypatch, archs):
archflags = " ".join(f"-arch {arch}" for arch in archs)
Expand Down