Skip to content

Commit 86a6121

Browse files
authored
Merge pull request #113 from anlambert/main
Use better qualified name for cpython extension module shared library
2 parents 924c143 + b9165b7 commit 86a6121

5 files changed

Lines changed: 27 additions & 28 deletions

File tree

hatch_cpp/tests/test_project_cmake/CMakeLists.txt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,11 @@ endif()
7171

7272

7373
find_package(Python ${CSP_PYTHON_VERSION} EXACT REQUIRED COMPONENTS Interpreter Development.Module)
74-
link_directories(${Python_LIBRARY_DIRS})
75-
include_directories(${Python_INCLUDE_DIRS})
76-
77-
set(CMAKE_SHARED_LIBRARY_PREFIX "")
78-
if(NOT WIN32)
79-
set(CMAKE_SHARED_LIBRARY_SUFFIX .so)
80-
else()
81-
set(CMAKE_SHARED_LIBRARY_SUFFIX .pyd)
82-
endif()
8374

8475
include_directories("${CMAKE_SOURCE_DIR}/cpp")
8576

86-
add_library(extension SHARED cpp/project/basic.cpp)
77+
python_add_library(extension MODULE WITH_SOABI cpp/project/basic.cpp)
78+
8779
set_target_properties(extension PROPERTIES PUBLIC_HEADER cpp/project/basic.hpp)
8880
install(TARGETS extension
8981
PUBLIC_HEADER DESTINATION project/include/project

hatch_cpp/tests/test_project_cmake_vcpkg/CMakeLists.txt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,11 @@ endif()
7171

7272

7373
find_package(Python ${CSP_PYTHON_VERSION} EXACT REQUIRED COMPONENTS Interpreter Development.Module)
74-
link_directories(${Python_LIBRARY_DIRS})
75-
include_directories(${Python_INCLUDE_DIRS})
76-
77-
set(CMAKE_SHARED_LIBRARY_PREFIX "")
78-
if(NOT WIN32)
79-
set(CMAKE_SHARED_LIBRARY_SUFFIX .so)
80-
else()
81-
set(CMAKE_SHARED_LIBRARY_SUFFIX .pyd)
82-
endif()
8374

8475
include_directories("${CMAKE_SOURCE_DIR}/cpp")
8576

86-
add_library(extension SHARED cpp/project/basic.cpp)
77+
python_add_library(extension MODULE WITH_SOABI cpp/project/basic.cpp)
78+
8779
set_target_properties(extension PROPERTIES PUBLIC_HEADER cpp/project/basic.hpp)
8880
install(TARGETS extension
8981
PUBLIC_HEADER DESTINATION project/include/project

hatch_cpp/tests/test_projects.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from os import listdir
1+
import contextlib
2+
from os import listdir, remove
23
from pathlib import Path
3-
from shutil import rmtree
44
from subprocess import check_call
55
from sys import modules, path, platform
6+
from sysconfig import get_config_var
67

78
import pytest
89

@@ -24,9 +25,17 @@ class TestProject:
2425
],
2526
)
2627
def test_basic(self, project):
28+
29+
suffix_ext = get_config_var("EXT_SUFFIX")
30+
2731
# cleanup
28-
rmtree(f"hatch_cpp/tests/{project}/project/extension.so", ignore_errors=True)
29-
rmtree(f"hatch_cpp/tests/{project}/project/extension.pyd", ignore_errors=True)
32+
with contextlib.suppress(FileNotFoundError):
33+
remove(f"hatch_cpp/tests/{project}/project/extension.so")
34+
with contextlib.suppress(FileNotFoundError):
35+
remove(f"hatch_cpp/tests/{project}/project/extension.pyd")
36+
with contextlib.suppress(FileNotFoundError):
37+
remove(f"hatch_cpp/tests/{project}/project/extension{suffix_ext}")
38+
3039
modules.pop("project", None)
3140
modules.pop("project.extension", None)
3241

@@ -40,14 +49,14 @@ def test_basic(self, project):
4049
)
4150

4251
# assert built
43-
52+
project_dir_content = listdir(f"hatch_cpp/tests/{project}/project")
4453
if project == "test_project_limited_api" and platform != "win32":
45-
assert "extension.abi3.so" in listdir(f"hatch_cpp/tests/{project}/project")
54+
assert "extension.abi3.so" in project_dir_content
4655
else:
4756
if platform == "win32":
48-
assert "extension.pyd" in listdir(f"hatch_cpp/tests/{project}/project")
57+
assert "extension.pyd" in project_dir_content or f"extension{suffix_ext}" in project_dir_content
4958
else:
50-
assert "extension.so" in listdir(f"hatch_cpp/tests/{project}/project")
59+
assert "extension.so" in project_dir_content or f"extension{suffix_ext}" in project_dir_content
5160

5261
# import
5362
here = Path(__file__).parent / project

hatch_cpp/toolchains/common.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ def check_py_limited_api(cls, value: Any) -> Any:
101101
return value
102102

103103
def get_qualified_name(self, platform):
104+
if self.binding == "cpython" and not self.py_limited_api:
105+
suffix = get_config_var("EXT_SUFFIX")
106+
return f"{self.name}{suffix}"
104107
if platform == "win32":
105108
suffix = "dll" if self.binding == "generic" else "pyd"
106109
elif platform == "darwin":

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ branch = false
100100
omit = [
101101
"hatch_cpp/tests/integration/",
102102
]
103+
patch = ["subprocess"]
104+
103105

104106
[tool.coverage.report]
105107
exclude_also = [
@@ -132,6 +134,7 @@ addopts = [
132134
"--junitxml=junit.xml",
133135
]
134136
testpaths = "hatch_cpp/tests"
137+
norecursedirs = "vcpkg"
135138

136139
[tool.ruff]
137140
line-length = 150

0 commit comments

Comments
 (0)