@@ -262,8 +262,8 @@ def modify_macho_object(cur_path, rpaths, deps, idpath,
262262 args .extend (['-id' , new_idpath ])
263263
264264 for orig , new in zip (deps , new_deps ):
265- args . extend ([ '-change' , orig , new ])
266-
265+ if not orig == new :
266+ args . extend ([ '-change' , orig , new ])
267267 for orig in list (set (rpaths )):
268268 args .extend (['-delete_rpath' , orig ])
269269 for new in list (set (new_rpaths )):
@@ -285,6 +285,8 @@ def modify_object_machotools(cur_path, rpaths, deps, idpath,
285285 The old install dir in LC_RPATH is replaced with the new install dir using
286286 using py-machotools
287287 """
288+ if cur_path .endswith ('.o' ): return
289+ tty .debug ('Running machotools on %s\n ' % cur_path )
288290 try :
289291 import machotools
290292 except ImportError as e :
@@ -293,8 +295,9 @@ def modify_object_machotools(cur_path, rpaths, deps, idpath,
293295 if machotools .detect .is_dylib (cur_path ):
294296 rewriter .install_name = new_idpath
295297 for orig , new in zip (deps , new_deps ):
296- rewriter .change_dependency (orig , new )
297- for orig , new in zip (rpaths , new_rpaths ):
298+ if not orig == new :
299+ rewriter .change_dependency (orig , new )
300+ for new in list (set (new_rpaths )):
298301 rewriter .append_rpath (new )
299302 rewriter .commit ()
300303 return
@@ -393,15 +396,13 @@ def replace(match):
393396 f .truncate ()
394397
395398
396- def relocate_macho_binaries (path_names , old_dir , new_dir , allow_root ):
399+ def relocate_macho_binaries (path_names , rpaths , old_dir , new_dir , allow_root ):
397400 """
398401 Change old_dir to new_dir in RPATHs of elf or mach-o files
399402 Account for the case where old_dir is now a placeholder
400403 """
401404 placeholder = set_placeholder (old_dir )
402-
403405 for path_name in path_names :
404- rpaths = set ()
405406 deps = set ()
406407 idpath = ""
407408 if platform .system ().lower () == 'darwin' :
@@ -431,17 +432,20 @@ def relocate_macho_binaries(path_names, old_dir, new_dir, allow_root):
431432 modify_macho_object (path_name ,
432433 rpaths , deps , idpath ,
433434 new_rpaths , new_deps , new_idpath )
435+ # only try binary string replacement in mach-o binaries
436+ # on macOS
437+ if len (new_dir ) <= len (old_dir ):
438+ replace_prefix_bin (path_name , old_dir , new_dir )
439+ else :
440+ tty .warn ('Cannot do a binary string replacement'
441+ ' with padding for %s'
442+ ' because %s is longer than %s' %
443+ (path_name , new_dir , old_dir ))
434444 else :
435445 modify_object_machotools (path_name ,
436446 rpaths , deps , idpath ,
437447 new_rpaths , new_deps , new_idpath )
438- if len (new_dir ) <= len (old_dir ):
439- replace_prefix_bin (path_name , old_dir , new_dir )
440- else :
441- tty .warn ('Cannot do a binary string replacement'
442- ' with padding for %s'
443- ' because %s is longer than %s' %
444- (path_name , new_dir , old_dir ))
448+
445449
446450
447451def relocate_elf_binaries (path_names , old_dir , new_dir , allow_root ):
@@ -481,7 +485,7 @@ def make_link_relative(cur_path_names, orig_path_names):
481485 os .symlink (new_src , cur_path )
482486
483487
484- def make_macho_binary_relative (cur_path_names , orig_path_names , old_dir ,
488+ def make_macho_binaries_relative (cur_path_names , orig_path_names , old_dir ,
485489 allow_root ):
486490 """
487491 Replace old RPATHs with paths relative to old_dir in binary files
@@ -508,11 +512,34 @@ def make_macho_binary_relative(cur_path_names, orig_path_names, old_dir,
508512 new_rpaths , new_deps , new_idpath )
509513
510514 if (not allow_root and
511- not file_is_relocatable (cur_path , old_dir )):
515+ not file_is_relocatable (cur_path )):
512516 raise InstallRootStringException (cur_path , old_dir )
513517
518+ def make_macho_binaries_clean (cur_path_names , orig_path_names , old_dir ,
519+ allow_root ):
520+ """
521+ Remove RPATHs in mach-o binary files for later replacement
522+ """
523+ for cur_path in cur_path_names :
524+ rpaths = set ()
525+ deps = set ()
526+ idpath = ""
527+ if platform .system ().lower () == 'darwin' :
528+ (rpaths , deps , idpath ) = macho_get_paths (cur_path )
529+ modify_macho_object (cur_path ,
530+ rpaths , deps , idpath ,
531+ list (), deps , idpath )
532+ else :
533+ (rpaths , deps , idpath ) = machotools_get_paths (cur_path )
534+ modify_object_machotools (cur_path ,
535+ rpaths , deps , idpath ,
536+ list (), deps , idpath )
537+
538+ if (not allow_root and
539+ not file_is_relocatable (cur_path )):
540+ raise InstallRootStringException (cur_path , old_dir )
514541
515- def make_elf_binary_relative (cur_path_names , orig_path_names , old_dir ,
542+ def make_elf_binaries_relative (cur_path_names , orig_path_names , old_dir ,
516543 allow_root ):
517544 """
518545 Replace old RPATHs with paths relative to old_dir in binary files
@@ -524,9 +551,25 @@ def make_elf_binary_relative(cur_path_names, orig_path_names, old_dir,
524551 orig_rpaths )
525552 modify_elf_object (cur_path , new_rpaths )
526553 if (not allow_root and
527- not file_is_relocatable (cur_path , old_dir )):
554+ not file_is_relocatable (cur_path )):
528555 raise InstallRootStringException (cur_path , old_dir )
529556
557+ def make_elf_binaries_clean (cur_path_names , orig_path_names , old_dir ,
558+ allow_root ):
559+ """
560+ Remove RPATHS in elf binary files for later replacement
561+ """
562+ for cur_path in cur_path_names :
563+ orig_rpaths = get_existing_elf_rpaths (cur_path )
564+ new_rpaths = set ()
565+ if orig_rpaths :
566+ for rpath in orig_rpaths :
567+ if not old_dir in rpath :
568+ new_rpaths .add (rpath )
569+ modify_elf_object (cur_path , new_rpaths )
570+ if (not allow_root and
571+ not file_is_relocatable (cur_path )):
572+ raise InstallRootStringException (cur_path , old_dir )
530573
531574def check_files_relocatable (cur_path_names , allow_root ):
532575 """
0 commit comments