brew/Library/Homebrew/linkage_checker.rb

230 lines
6.5 KiB
Ruby
Raw Normal View History

2016-07-07 20:41:14 +08:00
require "keg"
require "formula"
require "linkage_cache_store"
2016-07-07 20:41:14 +08:00
class LinkageChecker
attr_reader :undeclared_deps
def initialize(keg, formula = nil, cache_db:,
use_cache: !ENV["HOMEBREW_LINKAGE_CACHE"].nil?)
2016-07-07 20:41:14 +08:00
@keg = keg
@formula = formula || resolve_formula(keg)
@store = LinkageCacheStore.new(keg.name, cache_db) if use_cache
@system_dylibs = Set.new
@broken_dylibs = Set.new
@variable_dylibs = Set.new
@brewed_dylibs = Hash.new { |h, k| h[k] = Set.new }
@reverse_links = Hash.new { |h, k| h[k] = Set.new }
@broken_deps = Hash.new { |h, k| h[k] = [] }
@indirect_deps = []
@undeclared_deps = []
@unnecessary_deps = []
check_dylibs
end
def display_normal_output
display_items "System libraries", @system_dylibs
display_items "Homebrew libraries", @brewed_dylibs
display_items "Indirect dependencies with linkage", @indirect_deps
display_items "Variable-referenced libraries", @variable_dylibs
display_items "Missing libraries", @broken_dylibs
display_items "Broken dependencies", @broken_deps
display_items "Undeclared dependencies with linkage", @undeclared_deps
display_items "Dependencies with no linkage", @unnecessary_deps
end
def display_reverse_output
return if @reverse_links.empty?
2018-06-02 20:58:34 +05:30
sorted = @reverse_links.sort
sorted.each do |dylib, files|
puts dylib
files.each do |f|
unprefixed = f.to_s.strip_prefix "#{keg}/"
puts " #{unprefixed}"
end
puts if dylib != sorted.last.first
end
end
def display_test_output(puts_output: true)
display_items "Missing libraries", @broken_dylibs, puts_output: puts_output
display_items "Broken dependencies", @broken_deps, puts_output: puts_output
puts "No broken library linkage" unless broken_library_linkage?
end
def broken_library_linkage?
!@broken_dylibs.empty? || !@broken_deps.empty?
end
private
attr_reader :keg, :formula, :store
def dylib_to_dep(dylib)
2018-03-20 12:30:14 -05:00
dylib =~ %r{#{Regexp.escape(HOMEBREW_PREFIX)}/(opt|Cellar)/([\w+-.@]+)/}
Regexp.last_match(2)
end
def check_dylibs
keg_files_dylibs_was_empty = false
keg_files_dylibs = store&.fetch_type(:keg_files_dylibs)
keg_files_dylibs ||= {}
if keg_files_dylibs.empty?
keg_files_dylibs_was_empty = true
@keg.find do |file|
next if file.symlink? || file.directory?
next if !file.dylib? && !file.binary_executable? && !file.mach_o_bundle?
# weakly loaded dylibs may not actually exist on disk, so skip them
# when checking for broken linkage
keg_files_dylibs[file] =
file.dynamically_linked_libraries(except: :LC_LOAD_WEAK_DYLIB)
end
end
checked_dylibs = Set.new
keg_files_dylibs.each do |file, dylibs|
dylibs.each do |dylib|
2016-07-07 20:41:14 +08:00
@reverse_links[dylib] << file
next if checked_dylibs.include? dylib
checked_dylibs << dylib
2016-07-07 20:41:14 +08:00
if dylib.start_with? "@"
@variable_dylibs << dylib
next
end
begin
owner = Keg.for Pathname.new(dylib)
rescue NotAKegError
@system_dylibs << dylib
rescue Errno::ENOENT
next if harmless_broken_link?(dylib)
if (dep = dylib_to_dep(dylib))
@broken_deps[dep] |= [dylib]
else
@broken_dylibs << dylib
end
2016-07-07 20:41:14 +08:00
else
tap = Tab.for_keg(owner).tap
f = if tap.nil? || tap.core_tap?
owner.name
2016-07-07 20:41:14 +08:00
else
"#{tap}/#{owner.name}"
2016-07-07 20:41:14 +08:00
end
@brewed_dylibs[f] << dylib
2016-07-07 20:41:14 +08:00
end
end
end
2018-05-22 14:46:14 +01:00
if formula
@indirect_deps, @undeclared_deps, @unnecessary_deps =
check_undeclared_deps
end
return unless keg_files_dylibs_was_empty
store&.update!(keg_files_dylibs: keg_files_dylibs)
end
def check_undeclared_deps
filter_out = proc do |dep|
next true if dep.build?
next false unless dep.optional? || dep.recommended?
formula.build.without?(dep)
end
declared_deps_full_names = formula.deps
.reject { |dep| filter_out.call(dep) }
.map(&:name)
declared_deps_names = declared_deps_full_names.map do |dep|
dep.split("/").last
end
recursive_deps = formula.declared_runtime_dependencies.map do |dep|
begin
dep.to_formula.name
rescue FormulaUnavailableError
nil
end
end.compact
indirect_deps = []
undeclared_deps = []
@brewed_dylibs.each_key do |full_name|
name = full_name.split("/").last
next if name == formula.name
if recursive_deps.include?(name)
indirect_deps << full_name unless declared_deps_names.include?(name)
else
undeclared_deps << full_name
end
end
sort_by_formula_full_name!(indirect_deps)
sort_by_formula_full_name!(undeclared_deps)
unnecessary_deps = declared_deps_full_names.reject do |full_name|
next true if Formula[full_name].bin.directory?
name = full_name.split("/").last
@brewed_dylibs.keys.map { |x| x.split("/").last }.include?(name)
end
2018-04-07 15:35:27 -05:00
missing_deps = @broken_deps.values.flatten.map { |d| dylib_to_dep(d) }
unnecessary_deps -= missing_deps
[indirect_deps, undeclared_deps, unnecessary_deps]
end
def sort_by_formula_full_name!(arr)
arr.sort! do |a, b|
if a.include?("/") && !b.include?("/")
1
elsif !a.include?("/") && b.include?("/")
-1
else
a <=> b
2016-07-07 20:41:14 +08:00
end
end
2016-07-07 20:41:14 +08:00
end
# Whether or not dylib is a harmless broken link, meaning that it's
# okay to skip (and not report) as broken.
def harmless_broken_link?(dylib)
# libgcc_s_* is referenced by programs that use the Java Service Wrapper,
# and is harmless on x86(_64) machines
[
"/usr/lib/libgcc_s_ppc64.1.dylib",
"/opt/local/lib/libgcc/libgcc_s.1.dylib",
].include?(dylib)
end
2016-07-07 20:41:14 +08:00
# Display a list of things.
# Things may either be an array, or a hash of (label -> array)
def display_items(label, things, puts_output: true)
2016-07-07 20:41:14 +08:00
return if things.empty?
output = "#{label}:"
2016-07-07 20:41:14 +08:00
if things.is_a? Hash
things.keys.sort.each do |list_label|
things[list_label].sort.each do |item|
output += "\n #{item} (#{list_label})"
2016-07-07 20:41:14 +08:00
end
end
else
things.sort.each do |item|
output += "\n #{item}"
2016-07-07 20:41:14 +08:00
end
end
puts output if puts_output
output
2016-07-07 20:41:14 +08:00
end
def resolve_formula(keg)
2016-07-15 17:45:21 +08:00
Formulary.from_keg(keg)
rescue FormulaUnavailableError
opoo "Formula unavailable: #{keg.name}"
end
2016-07-07 20:41:14 +08:00
end