Skip to content

Commit 678698f

Browse files
authored
Merge pull request #7 from python-project-templates/tkp/test
Test importing built library
2 parents b75ad60 + b7cbb99 commit 678698f

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ __pycache__/
44
*$py.class
55

66
# C extensions
7+
*.a
78
*.so
9+
*.obj
10+
*.dll
11+
*.exp
12+
*.lib
813

914
# Distribution / packaging
1015
.Python

hatch_cpp/plugin.py

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def initialize(self, version: str, _: dict[str, t.Any]) -> None:
4949
for command in build_plan.commands:
5050
self._logger.info(command)
5151
build_plan.execute()
52+
build_plan.cleanup()
5253

5354
# build_kwargs = config.build_kwargs
5455
# if version == "editable":

hatch_cpp/structs.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
from dataclasses import dataclass, field
44
from os import environ, system
5-
from sys import platform as sys_platform
5+
from pathlib import Path
6+
from sys import executable, platform as sys_platform
67
from sysconfig import get_path
78
from typing import Literal
89

@@ -80,7 +81,7 @@ def default() -> HatchCppPlatform:
8081
raise Exception(f"Unrecognized toolchain: {CC}, {CXX}")
8182
return HatchCppPlatform(cc=CC, cxx=CXX, platform=platform, toolchain=toolchain)
8283

83-
def get_flags(self, library: HatchCppLibrary) -> str:
84+
def get_compile_flags(self, library: HatchCppLibrary) -> str:
8485
flags = ""
8586
if self.toolchain == "gcc":
8687
flags = f"-I{get_path('include')}"
@@ -109,20 +110,28 @@ def get_flags(self, library: HatchCppLibrary) -> str:
109110
elif self.toolchain == "msvc":
110111
flags = f"/I{get_path('include')} "
111112
flags += " ".join(f"/I{d}" for d in library.include_dirs)
112-
flags += " /LD"
113113
flags += " " + " ".join(library.extra_compile_args)
114114
flags += " " + " ".join(library.extra_link_args)
115115
flags += " " + " ".join(library.extra_objects)
116-
flags += " " + " ".join(f"{lib}.lib" for lib in library.libraries)
117-
flags += " " + " ".join(f"/LIBPATH:{lib}" for lib in library.library_dirs)
118116
flags += " " + " ".join(f"/D{macro}" for macro in library.define_macros)
119117
flags += " " + " ".join(f"/U{macro}" for macro in library.undef_macros)
120-
flags += f" /Fo{library.name}.pyd"
118+
flags += " /EHsc /DWIN32 /LD"
119+
flags += f" /Fo:{library.name}.obj"
120+
flags += f" /Fe:{library.name}.pyd"
121+
flags += " /link /DLL"
122+
if (Path(executable).parent / "libs").exists():
123+
flags += f" /LIBPATH:{str(Path(executable).parent / 'libs')}"
124+
flags += " " + " ".join(f"{lib}.lib" for lib in library.libraries)
125+
flags += " " + " ".join(f"/LIBPATH:{lib}" for lib in library.library_dirs)
121126
# clean
122127
while flags.count(" "):
123128
flags = flags.replace(" ", " ")
124129
return flags
125130

131+
def get_link_flags(self, library: HatchCppLibrary) -> str:
132+
flags = ""
133+
return flags
134+
126135

127136
@dataclass
128137
class HatchCppBuildPlan(object):
@@ -133,11 +142,18 @@ class HatchCppBuildPlan(object):
133142
def generate(self):
134143
self.commands = []
135144
for library in self.libraries:
136-
flags = self.platform.get_flags(library)
145+
flags = self.platform.get_compile_flags(library)
137146
self.commands.append(f"{self.platform.cc} {' '.join(library.sources)} {flags}")
138147
return self.commands
139148

140149
def execute(self):
141150
for command in self.commands:
142151
system(command)
143152
return self.commands
153+
154+
def cleanup(self):
155+
if self.platform.platform == "win32":
156+
for library in self.libraries:
157+
temp_obj = Path(f"{library.name}.obj")
158+
if temp_obj.exists():
159+
temp_obj.unlink()

hatch_cpp/tests/test_project_basic.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from os import listdir
2+
from pathlib import Path
23
from shutil import rmtree
34
from subprocess import check_output
4-
from sys import platform
5+
from sys import path, platform
56

67

78
class TestProject:
@@ -20,3 +21,8 @@ def test_basic(self):
2021
assert "extension.pyd" in listdir("hatch_cpp/tests/test_project_basic/basic_project")
2122
else:
2223
assert "extension.so" in listdir("hatch_cpp/tests/test_project_basic/basic_project")
24+
here = Path(__file__).parent / "test_project_basic"
25+
path.insert(0, str(here))
26+
import basic_project.extension
27+
28+
assert basic_project.extension.hello() == "A string"

0 commit comments

Comments
 (0)