Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

freezer: change the order to fix bugs with patchelf #2620

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cx_Freeze/freezer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,7 @@ def _post_copy_hook(
target_dir = target.parent
lib_files = self.finder.lib_files
fix_rpath = set()
fix_needed = {}
for dependent in self.get_dependent_files(source):
if not self._should_copy_file(dependent):
continue
Expand Down Expand Up @@ -1317,12 +1318,14 @@ def _post_copy_hook(
dependent_source, dependent_target, copy_dependent_files
)
if dependent.name != dependent_name:
self.replace_needed(target, dependent.name, dependent_name)
fix_needed.setdefault(dependent.name, dependent_name)
if fix_rpath:
has_rpath = self.get_rpath(target)
rpath = ":".join(f"$ORIGIN/{r}" for r in fix_rpath)
if has_rpath != rpath:
self.set_rpath(target, rpath)
for needed_old, needed_new in fix_needed.items():
self.replace_needed(target, needed_old, needed_new)

def _copy_top_dependency(self, source: Path) -> None:
"""Called for copying the top dependencies in _freeze_executable."""
Expand Down
12 changes: 7 additions & 5 deletions cx_Freeze/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def _get_dependent_files(self, filename: Path) -> set[Path]:
continue
if partname in ("not found", "(file not found)"):
partname = Path(parts[0])
for bin_path in self.bin_path_includes:
for bin_path in self._bin_path_includes:
partname = Path(bin_path, partname)
if partname.is_file():
dependent_files.add(partname)
Expand Down Expand Up @@ -314,8 +314,11 @@ def replace_needed(
def set_rpath(self, filename: str | Path, rpath: str) -> None:
"""Sets the rpath of the executable."""
self._set_write_mode(filename)
self.run_patchelf(["--remove-rpath", filename])
self.run_patchelf(["--force-rpath", "--set-rpath", rpath, filename])
try:
self.run_patchelf(["--set-rpath", rpath, filename])
except subprocess.CalledProcessError:
self.run_patchelf(["--remove-rpath", filename])
self.run_patchelf(["--add-rpath", rpath, filename])

def set_soname(self, filename: str | Path, new_so_name: str) -> None:
"""Sets DT_SONAME entry in the dynamic table."""
Expand All @@ -334,8 +337,7 @@ def run_patchelf(self, args: list[str]) -> str:

@staticmethod
def _set_write_mode(filename: str | Path) -> None:
if isinstance(filename, str):
filename = Path(filename)
filename = Path(filename)
mode = filename.stat().st_mode
if mode & stat.S_IWUSR == 0:
filename.chmod(mode | stat.S_IWUSR)
Expand Down
Loading