55from ctypes import CDLL
66from dataclasses import dataclass
77from glob import glob
8+ from importlib .util import find_spec
89from json import dumps , loads
910from os import environ
1011from os .path import relpath
@@ -93,6 +94,7 @@ class ExternalLibrary:
9394 library_dir : Path
9495 include_dir : Path | None
9596 link_name : str
97+ package : str | None = None
9698
9799
98100WINDOWS_TARGETS = {
@@ -650,6 +652,11 @@ class RustArtifactConfig(BaseModel):
650652 library_root : Path | None = Field (
651653 default = None , alias = "library-root" , description = "Prefix with lib/ and include/ for this library in external mode."
652654 )
655+ python_package : str | None = Field (
656+ default = None ,
657+ alias = "python-package" ,
658+ description = "Installed Python package shipping this library under lib/ and include/; linked from there instead of built." ,
659+ )
653660 import_library_destination : str | None = Field (
654661 default = None ,
655662 alias = "import-library-destination" ,
@@ -736,6 +743,12 @@ def validate_header_symbol_prefix(self) -> RustArtifactConfig:
736743 raise ValueError (f"Artifact '{ self .name } ' must set expected-headers when using header-symbol-prefix." )
737744 return self
738745
746+ @model_validator (mode = "after" )
747+ def validate_python_package (self ) -> RustArtifactConfig :
748+ if self .python_package is not None and (not self .name or self .manifest is not None or self .destination is not None ):
749+ raise ValueError (f"Artifact '{ self .name } ' with python-package must set name and no manifest or destination." )
750+ return self
751+
739752
740753@dataclass (frozen = True )
741754class PlannedArtifact :
@@ -1171,9 +1184,10 @@ def generate(self):
11711184 self ._linked_names = self ._linked_library_names (artifacts )
11721185 self ._external_libraries = {}
11731186 for artifact in artifacts :
1174- if artifact .name in self ._linked_names and self ._resolved_library_source in ("static" , "direct" ):
1187+ library = artifact .python_package is not None or artifact .name in self ._linked_names
1188+ if library and self ._resolved_library_source in ("static" , "direct" ):
11751189 continue
1176- if artifact .name in self . _linked_names and self ._resolved_library_source == "external" :
1190+ if library and ( artifact .python_package is not None or self ._resolved_library_source == "external" ) :
11771191 planned_artifact = self ._plan_external_library (artifact , global_target = global_target )
11781192 elif self ._is_generated_artifact (artifact ):
11791193 planned_artifact = self ._build_command_artifact_plan (artifact , global_target = global_target )
@@ -1242,13 +1256,13 @@ def _library_env(self, artifact: RustArtifactConfig, resolved_target: ResolvedTa
12421256 environment = {}
12431257 for link in artifact .links :
12441258 key = sub (r"[^A-Za-z0-9]" , "_" , link ).upper ()
1245- if self ._resolved_library_source == "bundled" :
1246- environment [f"HATCH_RS_LIBRARY_{ key } _DIR" ] = str (self ._bundled_library_dir (link , resolved_target ))
1247- elif self ._resolved_library_source == "external" :
1248- external = self ._external_libraries [link ]
1259+ external = self ._external_libraries .get (link )
1260+ if external is not None :
12491261 environment [f"HATCH_RS_LIBRARY_{ key } _DIR" ] = str (external .library_dir )
12501262 if external .include_dir is not None :
12511263 environment [f"HATCH_RS_LIBRARY_{ key } _INCLUDE_DIR" ] = str (external .include_dir )
1264+ elif self ._resolved_library_source == "bundled" :
1265+ environment [f"HATCH_RS_LIBRARY_{ key } _DIR" ] = str (self ._bundled_library_dir (link , resolved_target ))
12521266 return environment
12531267
12541268 def _library_rustc_args (self , artifact : RustArtifactConfig , resolved_target : ResolvedTarget ) -> list [str ]:
@@ -1263,8 +1277,10 @@ def _library_rustc_args(self, artifact: RustArtifactConfig, resolved_target: Res
12631277 args .extend (("-C" , f"link-arg=-Wl,-soname,{ shared_library } " ))
12641278 if mode in ("static" , "direct" ):
12651279 return args
1280+ origin = "$ORIGIN" if platform == "linux" else "@loader_path"
12661281 for link in artifact .links :
1267- if mode == "bundled" :
1282+ external = self ._external_libraries .get (link )
1283+ if external is None :
12681284 library_dir = self ._bundled_library_dir (link , resolved_target )
12691285 link_name = f"{ link } .dll" if platform == "win32" else link
12701286 args .extend (("-L" , f"native={ library_dir } " , "-l" , f"dylib={ link_name } " ))
@@ -1273,12 +1289,14 @@ def _library_rustc_args(self, artifact: RustArtifactConfig, resolved_target: Res
12731289 self ._destination_directory (self ._artifact_by_name (link ), resolved_target ),
12741290 self ._destination_directory (artifact , resolved_target ),
12751291 )
1276- origin = "$ORIGIN" if platform == "linux" else "@loader_path"
12771292 rpath = origin if relative == "." else f"{ origin } /{ Path (relative ).as_posix ()} "
12781293 args .extend (("-C" , f"link-arg=-Wl,-rpath,{ rpath } " ))
12791294 else :
1280- external = self ._external_libraries [link ]
12811295 args .extend (("-L" , f"native={ external .library_dir } " , "-l" , f"dylib={ external .link_name } " ))
1296+ if external .package is not None and platform in ("linux" , "darwin" ):
1297+ # both packages sit in one site-packages, so the rpath walks from the extension to the sibling package
1298+ relative = relpath (Path (* external .package .split ("." )) / "lib" , self ._destination_directory (artifact , resolved_target ))
1299+ args .extend (("-C" , f"link-arg=-Wl,-rpath,{ origin } /{ Path (relative ).as_posix ()} " ))
12821300 return args
12831301
12841302 def _pkg_config_variable (self , artifact : RustArtifactConfig , variable : str ) -> Path :
@@ -1298,9 +1316,16 @@ def _resolve_external_library(self, artifact: RustArtifactConfig, *, platform: s
12981316 name = self ._artifact_name (artifact )
12991317 label = self ._artifact_label (artifact )
13001318 root = environ .get ("HATCH_RS_LIBRARY_ROOT" ) or (str (artifact .library_root ) if artifact .library_root is not None else None )
1319+ package = artifact .python_package if self ._resolved_library_source == "bundled" else None
13011320 if root :
13021321 prefix = _resolve_path (root , base = Path (self .path ))
13031322 library_dir , include_dir = prefix / "lib" , prefix / "include"
1323+ elif package :
1324+ spec = find_spec (package )
1325+ if spec is None or not spec .submodule_search_locations :
1326+ raise ValueError (f"Artifact '{ label } ' package '{ package } ' is not installed in the build environment." )
1327+ prefix = Path (next (iter (spec .submodule_search_locations )))
1328+ library_dir , include_dir = prefix / "lib" , prefix / "include"
13041329 elif artifact .pkg_config :
13051330 library_dir = self ._pkg_config_variable (artifact , "libdir" )
13061331 include_dir = self ._pkg_config_variable (artifact , "includedir" )
@@ -1333,6 +1358,7 @@ def _resolve_external_library(self, artifact: RustArtifactConfig, *, platform: s
13331358 library_dir = library_dir ,
13341359 include_dir = include_dir if include_dir .is_dir () else None ,
13351360 link_name = link_name ,
1361+ package = package ,
13361362 )
13371363
13381364 def _plan_external_library (self , artifact : RustArtifactConfig , * , global_target : str | None ) -> PlannedArtifact :
0 commit comments