Skip to content

Commit d0efeec

Browse files
authored
Merge pull request #84 from Point72/tkp/paths
Tweak paths
2 parents 3ad3652 + 1f210fa commit d0efeec

File tree

2 files changed

+90
-3
lines changed

2 files changed

+90
-3
lines changed

hatch_cpp/tests/test_platform_specific.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,82 @@ def test_base_list_not_mutated(self):
454454
# Base list should not be modified
455455
assert library.include_dirs == ["common"]
456456
assert library.include_dirs_linux == ["linux"]
457+
458+
459+
class TestMSVCPythonLibsPath:
460+
"""Tests for MSVC Python libs path discovery."""
461+
462+
def test_msvc_link_flags_include_libpath(self):
463+
"""Test that MSVC link flags include /LIBPATH for Python libs."""
464+
library = HatchCppLibrary(
465+
name="test",
466+
sources=["test.cpp"],
467+
binding="generic", # Skip Python.h include
468+
)
469+
470+
platform = HatchCppPlatform(
471+
cc="cl",
472+
cxx="cl",
473+
ld="link",
474+
platform="win32",
475+
toolchain="msvc",
476+
disable_ccache=True,
477+
)
478+
479+
flags = platform.get_link_flags(library)
480+
# Should have /link /DLL flags
481+
assert "/link" in flags
482+
assert "/DLL" in flags
483+
# Should have output file
484+
assert "/Fe:" in flags
485+
486+
def test_msvc_link_flags_with_libraries(self):
487+
"""Test that MSVC link flags properly format library names."""
488+
library = HatchCppLibrary(
489+
name="test",
490+
sources=["test.cpp"],
491+
binding="generic",
492+
libraries=["mylib"],
493+
library_dirs=["path/to/libs"],
494+
)
495+
496+
platform = HatchCppPlatform(
497+
cc="cl",
498+
cxx="cl",
499+
ld="link",
500+
platform="win32",
501+
toolchain="msvc",
502+
disable_ccache=True,
503+
)
504+
505+
flags = platform.get_link_flags(library)
506+
# Libraries should have .lib suffix on Windows
507+
assert "mylib.lib" in flags
508+
# Library dirs should use /LIBPATH:
509+
assert "/LIBPATH:path/to/libs" in flags
510+
511+
def test_msvc_link_flags_with_platform_specific_libraries(self):
512+
"""Test that MSVC uses win32-specific libraries."""
513+
library = HatchCppLibrary(
514+
name="test",
515+
sources=["test.cpp"],
516+
binding="generic",
517+
libraries=["common"],
518+
libraries_win32=["kernel32", "user32"],
519+
library_dirs_win32=["C:/Windows/System32"],
520+
)
521+
522+
platform = HatchCppPlatform(
523+
cc="cl",
524+
cxx="cl",
525+
ld="link",
526+
platform="win32",
527+
toolchain="msvc",
528+
disable_ccache=True,
529+
)
530+
531+
flags = platform.get_link_flags(library)
532+
assert "common.lib" in flags
533+
assert "kernel32.lib" in flags
534+
assert "user32.lib" in flags
535+
assert "/LIBPATH:C:/Windows/System32" in flags

hatch_cpp/toolchains/common.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from re import match
66
from shutil import which
77
from sys import executable, platform as sys_platform
8-
from sysconfig import get_path
8+
from sysconfig import get_config_var, get_path
99
from typing import Any, List, Literal, Optional
1010

1111
from pydantic import AliasChoices, BaseModel, Field, field_validator, model_validator
@@ -368,8 +368,16 @@ def get_link_flags(self, library: HatchCppLibrary, build_type: BuildType = "rele
368368
flags += " /LD"
369369
flags += f" /Fe:{library.get_qualified_name(self.platform)}"
370370
flags += " /link /DLL"
371-
if (Path(executable).parent / "libs").exists():
372-
flags += f" /LIBPATH:{str(Path(executable).parent / 'libs')}"
371+
# Add Python libs directory - check multiple possible locations
372+
python_libs_paths = [
373+
Path(executable).parent / "libs", # Standard Python install
374+
Path(executable).parent.parent / "libs", # Some virtualenv layouts
375+
Path(get_config_var("installed_base") or "") / "libs", # sysconfig approach
376+
]
377+
for libs_path in python_libs_paths:
378+
if libs_path.exists():
379+
flags += f" /LIBPATH:{str(libs_path)}"
380+
break
373381
flags += " " + " ".join(f"{lib}.lib" for lib in effective_libraries)
374382
flags += " " + " ".join(f"/LIBPATH:{lib}" for lib in effective_library_dirs)
375383
# clean

0 commit comments

Comments
 (0)