@@ -32,7 +32,7 @@ def run_cmd(
32
32
return result
33
33
34
34
35
- def patch_rpath (file : Path , rpath : List [str ]):
35
+ def patch_rpath (file : Path , rpath : List [str ]) -> Optional [ subprocess . CompletedProcess ] :
36
36
"""Set the RPATH of an executable or library.
37
37
38
38
This will fail silently if the file is not an ELF executable.
@@ -41,26 +41,24 @@ def patch_rpath(file: Path, rpath: List[str]):
41
41
assert file .exists ()
42
42
43
43
# Get original RPATH of file, or fail.
44
+ cmd = ["patchelf" , "--print-rpath" , str (file )]
44
45
result = subprocess .run (
45
- ["patchelf" , "--print-rpath" , str (file )],
46
- stdout = subprocess .PIPE ,
47
- stderr = subprocess .PIPE ,
48
- check = False ,
46
+ cmd , stdout = subprocess .PIPE , stderr = subprocess .PIPE , check = False
49
47
)
50
- if result .stderr .decode ().strip () == "not an ELF executable" :
51
- # Silently ignore binaries that don't apply.
52
- return
48
+ if result .returncode != 0 :
49
+ if result .stderr .decode ().strip () == "not an ELF executable" :
50
+ # Silently ignore binaries that don't apply.
51
+ return None
52
+ logging .error (f"Error running: { ' ' .join (cmd )} " )
53
+ logging .error (result .stderr )
53
54
original = result .stdout .decode ().strip ()
54
55
if original != "" :
55
56
rpath .append (original )
56
57
57
58
file_arg = str (file )
58
59
rpath_arg = ":" .join (rpath )
59
60
logging .debug (f"Setting RPATH: { file_arg } -> { rpath_arg } " )
60
- subprocess .run (
61
- ["patchelf" , "--set-rpath" , rpath_arg , file_arg ],
62
- check = True ,
63
- )
61
+ return run_cmd (["patchelf" , "--set-rpath" , rpath_arg , file_arg ], must_succeed = False )
64
62
65
63
66
64
def find_binary_files (cwd : Optional [Path ] = None ) -> List [Path ]:
@@ -75,12 +73,10 @@ def find_binary_files(cwd: Optional[Path] = None) -> List[Path]:
75
73
)
76
74
return [Path (x ) for x in result .stdout .decode ().splitlines ()]
77
75
76
+
78
77
def patch_binary_files_rpath (src : Path , rpath : List [str ]):
79
78
"""Patch RPATH all binary files found in src directory."""
80
79
for file in find_binary_files (src ):
81
80
file = src / file
82
- try :
83
- logging .info (f"Patching RPATH: { file } " )
84
- patch_rpath (file , rpath )
85
- except :
86
- logging .warning (f"Error: cannot set RPATH of { file } " )
81
+ logging .info (f"Patching RPATH: { file } " )
82
+ patch_rpath (file , rpath )
0 commit comments