Skip to content

Test importing built library #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 10, 2025
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ __pycache__/
*$py.class

# C extensions
*.a
*.so
*.obj
*.dll
*.exp
*.lib

# Distribution / packaging
.Python
Expand Down
1 change: 1 addition & 0 deletions hatch_cpp/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def initialize(self, version: str, _: dict[str, t.Any]) -> None:
for command in build_plan.commands:
self._logger.info(command)
build_plan.execute()
build_plan.cleanup()

# build_kwargs = config.build_kwargs
# if version == "editable":
Expand Down
30 changes: 23 additions & 7 deletions hatch_cpp/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from dataclasses import dataclass, field
from os import environ, system
from sys import platform as sys_platform
from pathlib import Path
from sys import executable, platform as sys_platform
from sysconfig import get_path
from typing import Literal

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

def get_flags(self, library: HatchCppLibrary) -> str:
def get_compile_flags(self, library: HatchCppLibrary) -> str:
flags = ""
if self.toolchain == "gcc":
flags = f"-I{get_path('include')}"
Expand Down Expand Up @@ -109,20 +110,28 @@ def get_flags(self, library: HatchCppLibrary) -> str:
elif self.toolchain == "msvc":
flags = f"/I{get_path('include')} "
flags += " ".join(f"/I{d}" for d in library.include_dirs)
flags += " /LD"
flags += " " + " ".join(library.extra_compile_args)
flags += " " + " ".join(library.extra_link_args)
flags += " " + " ".join(library.extra_objects)
flags += " " + " ".join(f"{lib}.lib" for lib in library.libraries)
flags += " " + " ".join(f"/LIBPATH:{lib}" for lib in library.library_dirs)
flags += " " + " ".join(f"/D{macro}" for macro in library.define_macros)
flags += " " + " ".join(f"/U{macro}" for macro in library.undef_macros)
flags += f" /Fo{library.name}.pyd"
flags += " /EHsc /DWIN32 /LD"
flags += f" /Fo:{library.name}.obj"
flags += f" /Fe:{library.name}.pyd"
flags += " /link /DLL"
if (Path(executable).parent / "libs").exists():
flags += f" /LIBPATH:{str(Path(executable).parent / 'libs')}"
flags += " " + " ".join(f"{lib}.lib" for lib in library.libraries)
flags += " " + " ".join(f"/LIBPATH:{lib}" for lib in library.library_dirs)
# clean
while flags.count(" "):
flags = flags.replace(" ", " ")
return flags

def get_link_flags(self, library: HatchCppLibrary) -> str:
flags = ""
return flags


@dataclass
class HatchCppBuildPlan(object):
Expand All @@ -133,11 +142,18 @@ class HatchCppBuildPlan(object):
def generate(self):
self.commands = []
for library in self.libraries:
flags = self.platform.get_flags(library)
flags = self.platform.get_compile_flags(library)
self.commands.append(f"{self.platform.cc} {' '.join(library.sources)} {flags}")
return self.commands

def execute(self):
for command in self.commands:
system(command)
return self.commands

def cleanup(self):
if self.platform.platform == "win32":
for library in self.libraries:
temp_obj = Path(f"{library.name}.obj")
if temp_obj.exists():
temp_obj.unlink()
8 changes: 7 additions & 1 deletion hatch_cpp/tests/test_project_basic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from os import listdir
from pathlib import Path
from shutil import rmtree
from subprocess import check_output
from sys import platform
from sys import path, platform


class TestProject:
Expand All @@ -20,3 +21,8 @@ def test_basic(self):
assert "extension.pyd" in listdir("hatch_cpp/tests/test_project_basic/basic_project")
else:
assert "extension.so" in listdir("hatch_cpp/tests/test_project_basic/basic_project")
here = Path(__file__).parent / "test_project_basic"
path.insert(0, str(here))
import basic_project.extension

assert basic_project.extension.hello() == "A string"
Loading