autoPatchelfHook: more precise dependency ignorance

This commit is contained in:
Samuel Ainsworth 2022-04-11 01:28:55 +00:00
parent f22dc92e9f
commit a7fc2f6392
3 changed files with 32 additions and 19 deletions

View file

@ -1043,7 +1043,7 @@ You can also specify a `runtimeDependencies` variable which lists dependencies t
In certain situations you may want to run the main command (`autoPatchelf`) of the setup hook on a file or a set of directories instead of unconditionally patching all outputs. This can be done by setting the `dontAutoPatchelf` environment variable to a non-empty value. In certain situations you may want to run the main command (`autoPatchelf`) of the setup hook on a file or a set of directories instead of unconditionally patching all outputs. This can be done by setting the `dontAutoPatchelf` environment variable to a non-empty value.
By default `autoPatchelf` will fail as soon as any ELF file requires a dependency which cannot be resolved via the given build inputs. In some situations you might prefer to just leave missing dependencies unpatched and continue to patch the rest. This can be achieved by setting the `autoPatchelfIgnoreMissingDeps` environment variable to a non-empty value. By default `autoPatchelf` will fail as soon as any ELF file requires a dependency which cannot be resolved via the given build inputs. In some situations you might prefer to just leave missing dependencies unpatched and continue to patch the rest. This can be achieved by setting the `autoPatchelfIgnoreMissingDeps` environment variable to a non-empty value. `autoPatchelfIgnoreMissingDeps` can be set to a list like `autoPatchelfIgnoreMissingDeps = [ "libcuda.so.1" "libcudart.so.1" ];` or to simply `[ "*" ]` to ignore all missing dependencies.
The `autoPatchelf` command also recognizes a `--no-recurse` command line flag, which prevents it from recursing into subdirectories. The `autoPatchelf` command also recognizes a `--no-recurse` command line flag, which prevents it from recursing into subdirectories.

View file

@ -1,23 +1,21 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from collections import defaultdict
from contextlib import contextmanager
from dataclasses import dataclass
from elftools.common.exceptions import ELFError # type: ignore
from elftools.elf.dynamic import DynamicSection # type: ignore
from elftools.elf.elffile import ELFFile # type: ignore
from elftools.elf.enums import ENUM_E_TYPE, ENUM_EI_OSABI # type: ignore
from itertools import chain
from pathlib import Path, PurePath
from typing import Tuple, Optional, Iterator, List, DefaultDict, Set
import argparse import argparse
import os import os
import pprint import pprint
import subprocess import subprocess
import sys import sys
from collections import defaultdict
from contextlib import contextmanager
from dataclasses import dataclass
from itertools import chain
from pathlib import Path, PurePath
from typing import DefaultDict, Iterator, List, Optional, Set, Tuple
from elftools.common.exceptions import ELFError # type: ignore
from elftools.elf.dynamic import DynamicSection # type: ignore
from elftools.elf.elffile import ELFFile # type: ignore
from elftools.elf.enums import ENUM_E_TYPE, ENUM_EI_OSABI # type: ignore
@contextmanager @contextmanager
@ -246,7 +244,7 @@ def auto_patchelf(
lib_dirs: List[Path], lib_dirs: List[Path],
runtime_deps: List[Path], runtime_deps: List[Path],
recursive: bool =True, recursive: bool =True,
ignore_missing: bool =False) -> None: ignore_missing: List[str] = []) -> None:
if not paths_to_patch: if not paths_to_patch:
sys.exit("No paths to patch, stopping.") sys.exit("No paths to patch, stopping.")
@ -264,12 +262,19 @@ def auto_patchelf(
missing = [dep for dep in dependencies if not dep.found] missing = [dep for dep in dependencies if not dep.found]
# Print a summary of the missing dependencies at the end # Print a summary of the missing dependencies at the end
print(f"auto-patchelf: {len(missing)} dependencies could not be satisfied")
failure = False
for dep in missing: for dep in missing:
print(f"auto-patchelf could not satisfy dependency {dep.name} wanted by {dep.file}") if dep.name.name in ignore_missing or "*" in ignore_missing:
print(f"warn: auto-patchelf ignoring missing {dep.name} wanted by {dep.file}")
else:
print(f"error: auto-patchelf could not satisfy dependency {dep.name} wanted by {dep.file}")
failure = True
if missing and not ignore_missing: if failure:
sys.exit('auto-patchelf failed to find all the required dependencies.\n' sys.exit('auto-patchelf failed to find all the required dependencies.\n'
'Add the missing dependencies to --libs or use --ignore-missing.') 'Add the missing dependencies to --libs or use '
'`--ignore-missing="foo.so.1 bar.so etc.so"`.')
def main() -> None: def main() -> None:
@ -280,7 +285,8 @@ def main() -> None:
'libraries in the provided paths.') 'libraries in the provided paths.')
parser.add_argument( parser.add_argument(
"--ignore-missing", "--ignore-missing",
action="store_true", nargs="*",
type=str,
help="Do not fail when some dependencies are not found.") help="Do not fail when some dependencies are not found.")
parser.add_argument( parser.add_argument(
"--no-recurse", "--no-recurse",

View file

@ -53,10 +53,17 @@ autoPatchelf() {
esac esac
done done
if [ "${autoPatchelfIgnoreMissingDeps[*]}" == "1" ]; then
echo "autoPatchelf: WARNING: setting 'autoPatchelfIgnoreMissingDeps" \
"= true;' is deprecated and will be removed in a future release." \
"Use 'autoPatchelfIgnoreMissingDeps = [ \"*\" ];' instead." >&2
autoPatchelfIgnoreMissingDeps=( "*" )
fi
local runtimeDependenciesArray=($runtimeDependencies) local runtimeDependenciesArray=($runtimeDependencies)
@pythonInterpreter@ @autoPatchelfScript@ \ @pythonInterpreter@ @autoPatchelfScript@ \
${norecurse:+--no-recurse} \ ${norecurse:+--no-recurse} \
${autoPatchelfIgnoreMissingDeps:+--ignore-missing} \ --ignore-missing "${autoPatchelfIgnoreMissingDeps[@]}" \
--paths "$@" \ --paths "$@" \
--libs "${autoPatchelfLibs[@]}" \ --libs "${autoPatchelfLibs[@]}" \
"${extraAutoPatchelfLibs[@]}" \ "${extraAutoPatchelfLibs[@]}" \