diff --git a/Library/Homebrew/os/linux/elf.rb b/Library/Homebrew/os/linux/elf.rb index efd855db952ca3..dbda663e1df7ad 100644 --- a/Library/Homebrew/os/linux/elf.rb +++ b/Library/Homebrew/os/linux/elf.rb @@ -130,19 +130,7 @@ def initialize(path) @dylib_id, needed = needed_libraries path return if needed.empty? - ldd = DevelopmentTools.locate "ldd" - ldd_output = Utils.popen_read(ldd, path.expand_path.to_s).split("\n") - return unless $CHILD_STATUS.success? - - ldd_paths = ldd_output.filter_map do |line| - match = line.match(/\t.+ => (.+) \(.+\)|\t(.+) => not found/) - next unless match - - match.captures.compact.first - end - @dylibs = ldd_paths.select do |ldd_path| - needed.include? File.basename(ldd_path) - end + @dylibs = needed.map { |lib| find_full_lib_path(lib).to_s } end private @@ -157,6 +145,49 @@ def needed_libraries_using_patchelf_rb(path) patcher = path.patchelf_patcher [patcher.soname, patcher.needed] end + + def ld_so_conf(conf_path = "/etc/ld.so.conf") + conf_file = Pathname(conf_path) + paths = Set.new + directory = conf_file.realpath.dirname + + conf_file.readlines.each do |line| + line.rstrip! + if line.start_with?(/\s*include\s+/) + include_path = Pathname(line.sub(/^\s*include\s+/, "")).expand_path + wildcard = include_path.absolute? ? include_path : directory/include_path + + Dir.glob(wildcard.to_s).each do |include_file| + paths += ld_so_conf(include_file) + end + elsif line.start_with?(/\s*#/) || line.empty? + next + end + + paths << Pathname(line) + end + + paths.to_a + end + + def find_full_lib_path(basename) + local_paths = (path.patchelf_patcher.runpath || path.patchelf_patcher.rpath)&.split(":")&.map do |p| + Pathname(p) + end + + local_paths&.each do |local_path| + candidate = local_path/basename + return candidate if candidate.exist? && candidate.elf? + end + + global_paths = ld_so_conf + global_paths.each do |global_path| + candidate = global_path/basename + return candidate if candidate.exist? && candidate.elf? + end + + basename + end end private_constant :Metadata