2016-07-07 20:41:14 +08:00
|
|
|
require "set"
|
|
|
|
require "keg"
|
|
|
|
require "formula"
|
|
|
|
|
|
|
|
class LinkageChecker
|
2016-07-14 13:14:03 +08:00
|
|
|
attr_reader :keg, :formula
|
2016-07-07 20:41:14 +08:00
|
|
|
attr_reader :brewed_dylibs, :system_dylibs, :broken_dylibs, :variable_dylibs
|
2017-09-05 15:59:26 -05:00
|
|
|
attr_reader :undeclared_deps, :unnecessary_deps, :reverse_links
|
2016-07-07 20:41:14 +08:00
|
|
|
|
2016-07-14 13:14:03 +08:00
|
|
|
def initialize(keg, formula = nil)
|
2016-07-07 20:41:14 +08:00
|
|
|
@keg = keg
|
2016-07-14 13:14:03 +08:00
|
|
|
@formula = formula || resolve_formula(keg)
|
2016-07-07 20:41:14 +08:00
|
|
|
@brewed_dylibs = Hash.new { |h, k| h[k] = Set.new }
|
|
|
|
@system_dylibs = Set.new
|
2018-03-18 18:52:12 -05:00
|
|
|
@broken_dylibs = Hash.new { |h, k| h[k] = Set.new }
|
2016-07-07 20:41:14 +08:00
|
|
|
@variable_dylibs = Set.new
|
2018-02-10 08:34:23 -05:00
|
|
|
@indirect_deps = []
|
2016-07-07 20:41:14 +08:00
|
|
|
@undeclared_deps = []
|
|
|
|
@reverse_links = Hash.new { |h, k| h[k] = Set.new }
|
2017-09-05 15:59:26 -05:00
|
|
|
@unnecessary_deps = []
|
2016-07-07 20:41:14 +08:00
|
|
|
check_dylibs
|
|
|
|
end
|
|
|
|
|
2018-03-18 18:52:12 -05:00
|
|
|
def dylib_to_dep(dylib)
|
|
|
|
dylib.sub("#{HOMEBREW_PREFIX}/opt/", "").sub("#{HOMEBREW_CELLAR}/", "").split("/")[0]
|
|
|
|
end
|
|
|
|
|
2016-07-07 20:41:14 +08:00
|
|
|
def check_dylibs
|
2018-03-17 00:15:51 -05:00
|
|
|
checked_dylibs = Set.new
|
2016-07-07 20:41:14 +08:00
|
|
|
@keg.find do |file|
|
|
|
|
next if file.symlink? || file.directory?
|
2017-12-01 16:43:00 -08:00
|
|
|
next unless file.dylib? || file.binary_executable? || file.mach_o_bundle?
|
2016-11-07 19:37:52 -05:00
|
|
|
|
|
|
|
# weakly loaded dylibs may not actually exist on disk, so skip them
|
|
|
|
# when checking for broken linkage
|
|
|
|
file.dynamically_linked_libraries(except: :LC_LOAD_WEAK_DYLIB).each do |dylib|
|
2016-07-07 20:41:14 +08:00
|
|
|
@reverse_links[dylib] << file
|
2018-03-17 00:15:51 -05:00
|
|
|
next if checked_dylibs.include? dylib
|
2016-07-07 20:41:14 +08:00
|
|
|
if dylib.start_with? "@"
|
|
|
|
@variable_dylibs << dylib
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
owner = Keg.for Pathname.new(dylib)
|
|
|
|
rescue NotAKegError
|
|
|
|
@system_dylibs << dylib
|
|
|
|
rescue Errno::ENOENT
|
2017-06-19 22:48:01 -04:00
|
|
|
next if harmless_broken_link?(dylib)
|
2018-03-18 18:52:12 -05:00
|
|
|
@broken_dylibs[dylib_to_dep(dylib)] << dylib
|
2016-07-07 20:41:14 +08:00
|
|
|
else
|
2016-07-14 13:14:03 +08:00
|
|
|
tap = Tab.for_keg(owner).tap
|
|
|
|
f = if tap.nil? || tap.core_tap?
|
|
|
|
owner.name
|
|
|
|
else
|
|
|
|
"#{tap}/#{owner.name}"
|
|
|
|
end
|
|
|
|
@brewed_dylibs[f] << dylib
|
2016-07-07 20:41:14 +08:00
|
|
|
end
|
|
|
|
end
|
2018-03-17 00:15:51 -05:00
|
|
|
checked_dylibs << dylib
|
2016-07-07 20:41:14 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-10 08:34:23 -05:00
|
|
|
@indirect_deps, @undeclared_deps, @unnecessary_deps = check_undeclared_deps if formula
|
2016-07-14 13:14:03 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_undeclared_deps
|
2016-09-11 17:49:27 +01:00
|
|
|
filter_out = proc do |dep|
|
|
|
|
next true if dep.build?
|
|
|
|
next false unless dep.optional? || dep.recommended?
|
|
|
|
formula.build.without?(dep)
|
|
|
|
end
|
|
|
|
declared_deps = formula.deps.reject { |dep| filter_out.call(dep) }.map(&:name)
|
2018-03-05 09:51:27 -08:00
|
|
|
recursive_deps = keg.to_formula.runtime_dependencies.map { |dep| dep.to_formula.name }
|
2018-01-14 13:27:43 +00:00
|
|
|
declared_dep_names = declared_deps.map { |dep| dep.split("/").last }
|
2018-02-10 08:34:23 -05:00
|
|
|
indirect_deps = []
|
|
|
|
undeclared_deps = []
|
|
|
|
@brewed_dylibs.each_key do |full_name|
|
2016-09-11 17:49:27 +01:00
|
|
|
name = full_name.split("/").last
|
2018-02-10 08:34:23 -05:00
|
|
|
next if name == formula.name
|
|
|
|
if recursive_deps.include?(name)
|
|
|
|
indirect_deps << full_name unless declared_dep_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_dep_names.reject do |full_name|
|
|
|
|
name = full_name.split("/").last
|
|
|
|
next true if Formula[name].bin.directory?
|
|
|
|
@brewed_dylibs.keys.map { |x| x.split("/").last }.include?(name)
|
2016-09-11 17:49:27 +01:00
|
|
|
end
|
2018-03-18 18:52:12 -05:00
|
|
|
missing = Set.new
|
2018-03-18 19:03:31 -05:00
|
|
|
@broken_dylibs.each_value do |value|
|
|
|
|
value.each do |str|
|
|
|
|
missing << dylib_to_dep(str) if str.start_with?("#{HOMEBREW_PREFIX}/opt", HOMEBREW_CELLAR)
|
|
|
|
end
|
2018-03-17 00:15:51 -05:00
|
|
|
end
|
2018-03-18 18:52:12 -05:00
|
|
|
unnecessary_deps -= missing.to_a
|
2018-02-10 08:34:23 -05:00
|
|
|
[indirect_deps, undeclared_deps, unnecessary_deps]
|
|
|
|
end
|
|
|
|
|
|
|
|
def sort_by_formula_full_name!(arr)
|
|
|
|
arr.sort! do |a, b|
|
2016-09-11 17:49:27 +01:00
|
|
|
if a.include?("/") && !b.include?("/")
|
|
|
|
1
|
|
|
|
elsif !a.include?("/") && b.include?("/")
|
|
|
|
-1
|
|
|
|
else
|
|
|
|
a <=> b
|
2016-07-07 20:41:14 +08:00
|
|
|
end
|
2016-09-11 17:49:27 +01:00
|
|
|
end
|
2016-07-07 20:41:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def display_normal_output
|
|
|
|
display_items "System libraries", @system_dylibs
|
|
|
|
display_items "Homebrew libraries", @brewed_dylibs
|
2018-02-10 08:34:23 -05:00
|
|
|
display_items "Indirect dependencies with linkage", @indirect_deps
|
2016-07-07 20:41:14 +08:00
|
|
|
display_items "Variable-referenced libraries", @variable_dylibs
|
|
|
|
display_items "Missing libraries", @broken_dylibs
|
2017-09-19 10:54:50 +01:00
|
|
|
display_items "Undeclared dependencies with linkage", @undeclared_deps
|
|
|
|
display_items "Dependencies with no linkage", @unnecessary_deps
|
2016-07-07 20:41:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def display_reverse_output
|
|
|
|
return if @reverse_links.empty?
|
|
|
|
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 unless dylib == sorted.last[0]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def display_test_output
|
|
|
|
display_items "Missing libraries", @broken_dylibs
|
2018-03-17 00:15:51 -05:00
|
|
|
display_items "Dependencies with no linkage", @unnecessary_deps
|
2016-07-07 20:41:14 +08:00
|
|
|
puts "No broken dylib links" if @broken_dylibs.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def broken_dylibs?
|
|
|
|
!@broken_dylibs.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def undeclared_deps?
|
|
|
|
!@undeclared_deps.empty?
|
|
|
|
end
|
|
|
|
|
2017-09-05 15:59:26 -05:00
|
|
|
def unnecessary_deps?
|
|
|
|
!@unnecessary_deps.empty?
|
2017-08-29 11:04:00 -05:00
|
|
|
end
|
|
|
|
|
2016-07-07 20:41:14 +08:00
|
|
|
private
|
|
|
|
|
2017-06-19 22:48:01 -04:00
|
|
|
# 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)
|
2017-07-22 15:39:53 -04:00
|
|
|
# libgcc_s_* is referenced by programs that use the Java Service Wrapper,
|
2017-06-19 22:48:01 -04:00
|
|
|
# and is harmless on x86(_64) machines
|
2017-07-22 15:39:53 -04:00
|
|
|
[
|
|
|
|
"/usr/lib/libgcc_s_ppc64.1.dylib",
|
|
|
|
"/opt/local/lib/libgcc/libgcc_s.1.dylib",
|
|
|
|
].include?(dylib)
|
2017-06-19 22:48:01 -04:00
|
|
|
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)
|
|
|
|
return if things.empty?
|
|
|
|
puts "#{label}:"
|
|
|
|
if things.is_a? Hash
|
|
|
|
things.sort.each do |list_label, list|
|
|
|
|
list.sort.each do |item|
|
|
|
|
puts " #{item} (#{list_label})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
things.sort.each do |item|
|
|
|
|
puts " #{item}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-07-14 13:14:03 +08:00
|
|
|
|
|
|
|
def resolve_formula(keg)
|
2016-07-15 17:45:21 +08:00
|
|
|
Formulary.from_keg(keg)
|
2016-07-14 13:14:03 +08:00
|
|
|
rescue FormulaUnavailableError
|
|
|
|
opoo "Formula unavailable: #{keg.name}"
|
|
|
|
end
|
2016-07-07 20:41:14 +08:00
|
|
|
end
|