99from os import environ
1010from pathlib import Path
1111from platform import machine as platform_machine
12- from re import sub
12+ from re import DOTALL , MULTILINE , escape , findall , sub
1313from shlex import join as shell_join
1414from shutil import copy2
1515from subprocess import run
3131 "RustArtifactConfig" ,
3232 "ValidationCommandConfig" ,
3333 "executable_name" ,
34+ "header_symbols" ,
3435 "python_extension_name" ,
3536 "resolve_target_triple" ,
3637 "shared_library_name" ,
@@ -431,6 +432,23 @@ def _cargo_artifact_stem(path: Path) -> str:
431432 return stem
432433
433434
435+ def header_symbols (text : str , prefix : str ) -> list [str ]:
436+ """Functions declared in a C header whose names start with `prefix`.
437+
438+ Comments, preprocessor lines and `static inline` definitions are skipped,
439+ since none of them is exported by the library. Declarations are found with
440+ regular expressions rather than a C parser, which works for a hand-written
441+ or generated ABI header.
442+ """
443+ text = sub (r"/\*.*?\*/" , " " , text , flags = DOTALL )
444+ text = sub (r"//[^\n]*" , " " , text )
445+ text = sub (r"^\s*#.*?(?<!\\)$" , " " , text , flags = MULTILINE | DOTALL )
446+ text = sub (r"\bstatic\s+inline\b[^;{]*\{" , "static inline {" , text )
447+ declared = findall (rf"\b({ escape (prefix )} \w*)\s*\(" , text )
448+ inline = set (findall (rf"\bstatic\s+(?:inline\s+)?[^;{{(]*?\b({ escape (prefix )} \w*)\s*\(" , text ))
449+ return sorted (set (declared ) - inline )
450+
451+
434452def _resolve_path (path : Path | str , * , base : Path ) -> Path :
435453 path = Path (path )
436454 if path .is_absolute ():
@@ -583,6 +601,11 @@ class RustArtifactConfig(BaseModel):
583601 cpp_compat : bool = Field (default = False , alias = "cpp-compat" , description = "Reserved for cbindgen C++ compatibility presets." )
584602 depfile : Path | None = Field (default = None , description = "Reserved depfile path for generated headers." )
585603 expected_symbols : list [str ] = Field (default_factory = list , alias = "expected-symbols" , description = "Exported symbols expected in a copied library." )
604+ header_symbol_prefix : str | None = Field (
605+ default = None ,
606+ alias = "header-symbol-prefix" ,
607+ description = "Expect every function in expected-headers whose name starts with this prefix to be exported." ,
608+ )
586609 expected_headers : list [Path ] = Field (default_factory = list , alias = "expected-headers" , description = "Header files expected to exist after build." )
587610 expected_abi_strings : list [str ] = Field (
588611 default_factory = list ,
@@ -680,6 +703,12 @@ def validate_optional_path(cls, path: Path | None) -> Path | None:
680703 path = Path (path )
681704 return path
682705
706+ @model_validator (mode = "after" )
707+ def validate_header_symbol_prefix (self ) -> RustArtifactConfig :
708+ if self .header_symbol_prefix is not None and not self .expected_headers :
709+ raise ValueError (f"Artifact '{ self .name } ' must set expected-headers when using header-symbol-prefix." )
710+ return self
711+
683712
684713@dataclass (frozen = True )
685714class PlannedArtifact :
@@ -1592,9 +1621,22 @@ def _validate_expected_headers(self, artifact: RustArtifactConfig) -> list[Path]
15921621 raise RuntimeError (f"Artifact '{ self ._artifact_label (artifact )} ' expected ABI string was not found in headers: { expected } " )
15931622 return headers
15941623
1595- def _validate_library_exports (self , planned_artifact : PlannedArtifact , copied_artifacts : list [CopiedArtifact ]) -> None :
1624+ def _header_symbols (self , artifact : RustArtifactConfig , headers : list [Path ]) -> list [str ]:
1625+ if artifact .header_symbol_prefix is None :
1626+ return []
1627+ symbols = sorted (
1628+ {symbol for header in headers for symbol in header_symbols (header .read_text (errors = "replace" ), artifact .header_symbol_prefix )}
1629+ )
1630+ if not symbols :
1631+ raise RuntimeError (
1632+ f"Artifact '{ self ._artifact_label (artifact )} ' expected headers declare no functions starting with '{ artifact .header_symbol_prefix } '."
1633+ )
1634+ return symbols
1635+
1636+ def _validate_library_exports (self , planned_artifact : PlannedArtifact , copied_artifacts : list [CopiedArtifact ], headers : list [Path ]) -> None :
15961637 artifact = planned_artifact .artifact
1597- if not artifact .validate_artifact and not artifact .expected_symbols :
1638+ expected = list (dict .fromkeys ([* artifact .expected_symbols , * self ._header_symbols (artifact , headers )]))
1639+ if not artifact .validate_artifact and not expected :
15981640 return
15991641 if not copied_artifacts :
16001642 raise RuntimeError (f"Artifact '{ self ._artifact_label (artifact )} ' has no copied library to validate." )
@@ -1607,11 +1649,14 @@ def _validate_library_exports(self, planned_artifact: PlannedArtifact, copied_ar
16071649 f"Artifact '{ self ._artifact_label (artifact )} ' failed validation load check for '{ copied_library .distribution_path } ': { error } "
16081650 ) from error
16091651
1610- for symbol in artifact .expected_symbols :
1652+ missing = []
1653+ for symbol in expected :
16111654 try :
16121655 getattr (library , symbol )
1613- except AttributeError as error :
1614- raise RuntimeError (f"Artifact '{ self ._artifact_label (artifact )} ' expected symbol was not exported: { symbol } " ) from error
1656+ except AttributeError :
1657+ missing .append (symbol )
1658+ if missing :
1659+ raise RuntimeError (f"Artifact '{ self ._artifact_label (artifact )} ' expected symbol was not exported: { ', ' .join (missing )} " )
16151660
16161661 def _validation_tokens (self , planned_artifact : PlannedArtifact , copied_artifacts : list [CopiedArtifact ], headers : list [Path ]) -> dict [str , str ]:
16171662 artifact = planned_artifact .artifact
@@ -1675,7 +1720,7 @@ def _run_validation_commands(
16751720
16761721 def _validate_artifact (self , planned_artifact : PlannedArtifact , * , build_root : Path , copied_artifacts : list [CopiedArtifact ]) -> None :
16771722 headers = self ._validate_expected_headers (planned_artifact .artifact )
1678- self ._validate_library_exports (planned_artifact , copied_artifacts )
1723+ self ._validate_library_exports (planned_artifact , copied_artifacts , headers )
16791724 self ._run_validation_commands (planned_artifact , build_root = build_root , copied_artifacts = copied_artifacts , headers = headers )
16801725
16811726 def _format_artifact_manifest_destination (self ) -> Path :
0 commit comments