2016-07-07 20:41:14 +08:00
|
|
|
require "keg"
|
|
|
|
require "formula"
|
2018-03-06 12:07:57 -05:00
|
|
|
require "linkage_cache_store"
|
2016-07-07 20:41:14 +08:00
|
|
|
|
|
|
|
class LinkageChecker
|
2018-05-22 14:46:14 +01:00
|
|
|
def initialize(keg, formula = nil, use_cache: false, cache_db:)
|
2016-07-07 20:41:14 +08:00
|
|
|
@keg = keg
|
2016-07-14 13:14:03 +08:00
|
|
|
@formula = formula || resolve_formula(keg)
|
2018-05-21 16:15:52 -04:00
|
|
|
|
|
|
|
if use_cache
|
2018-05-22 14:46:14 +01:00
|
|
|
@store = LinkageCacheStore.new(keg.name, cache_db)
|
2018-05-22 12:54:54 -04:00
|
|
|
flush_cache_and_check_dylibs unless @store.keg_exists?
|
2018-05-21 16:15:52 -04:00
|
|
|
else
|
|
|
|
flush_cache_and_check_dylibs
|
|
|
|
end
|
2018-01-16 17:37:59 -05:00
|
|
|
end
|
|
|
|
|
2018-04-24 09:52:51 +01:00
|
|
|
def display_normal_output
|
2018-04-24 16:49:51 -04:00
|
|
|
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
|
2018-04-24 09:52:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def display_reverse_output
|
2018-04-24 16:49:51 -04:00
|
|
|
return if reverse_links.empty?
|
|
|
|
sorted = reverse_links.sort
|
2018-04-24 09:52:51 +01:00
|
|
|
sorted.each do |dylib, files|
|
|
|
|
puts dylib
|
|
|
|
files.each do |f|
|
2018-04-24 16:49:51 -04:00
|
|
|
unprefixed = f.to_s.strip_prefix "#{keg}/"
|
2018-04-24 09:52:51 +01:00
|
|
|
puts " #{unprefixed}"
|
|
|
|
end
|
|
|
|
puts unless dylib == sorted.last[0]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def display_test_output(puts_output: true)
|
2018-05-24 14:51:28 +01:00
|
|
|
display_items "Missing libraries", broken_dylibs, puts_output: puts_output
|
|
|
|
display_items "Broken dependencies", broken_deps, puts_output: puts_output
|
2018-04-24 09:52:51 +01:00
|
|
|
puts "No broken library linkage" unless broken_library_linkage?
|
|
|
|
end
|
|
|
|
|
|
|
|
def broken_library_linkage?
|
2018-05-24 14:51:28 +01:00
|
|
|
!broken_dylibs.empty? || !broken_deps.empty?
|
2018-04-24 09:52:51 +01:00
|
|
|
end
|
|
|
|
|
2018-05-06 15:55:33 -04:00
|
|
|
def undeclared_deps
|
2018-05-22 14:46:14 +01:00
|
|
|
@undeclared_deps ||= store.fetch_type(:undeclared_deps)
|
2018-05-06 15:55:33 -04:00
|
|
|
end
|
|
|
|
|
2018-04-24 09:52:51 +01:00
|
|
|
private
|
|
|
|
|
2018-04-24 16:49:51 -04:00
|
|
|
attr_reader :keg, :formula, :store
|
2018-04-24 09:52:51 +01:00
|
|
|
|
2018-05-06 15:55:33 -04:00
|
|
|
# 'Hash-type' cache values
|
|
|
|
|
|
|
|
def brewed_dylibs
|
2018-05-22 14:46:14 +01:00
|
|
|
@brewed_dylibs ||= store.fetch_type(:brewed_dylibs)
|
2018-05-06 15:55:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reverse_links
|
2018-05-22 14:46:14 +01:00
|
|
|
@reverse_links ||= store.fetch_type(:reverse_links)
|
2018-05-06 15:55:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def broken_deps
|
2018-05-22 14:46:14 +01:00
|
|
|
@broken_deps ||= store.fetch_type(:broken_deps)
|
2018-05-06 15:55:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# 'Path-type' cached values
|
|
|
|
|
|
|
|
def system_dylibs
|
2018-05-22 14:46:14 +01:00
|
|
|
@system_dylibs ||= store.fetch_type(:system_dylibs)
|
2018-05-06 15:55:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def broken_dylibs
|
2018-05-22 14:46:14 +01:00
|
|
|
@broken_dylibs ||= store.fetch_type(:broken_dylibs)
|
2018-05-06 15:55:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def variable_dylibs
|
2018-05-22 14:46:14 +01:00
|
|
|
@variable_dylibs ||= store.fetch_type(:variable_dylibs)
|
2018-05-06 15:55:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def indirect_deps
|
2018-05-22 14:46:14 +01:00
|
|
|
@indirect_deps ||= store.fetch_type(:indirect_deps)
|
2018-05-06 15:55:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def unnecessary_deps
|
2018-05-22 14:46:14 +01:00
|
|
|
@unnecessary_deps ||= store.fetch_type(:unnecessary_deps)
|
2018-05-06 15:55:33 -04:00
|
|
|
end
|
|
|
|
|
2018-03-18 18:52:12 -05:00
|
|
|
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)
|
2018-03-18 18:52:12 -05:00
|
|
|
end
|
|
|
|
|
2018-01-16 17:37:59 -05:00
|
|
|
def flush_cache_and_check_dylibs
|
|
|
|
reset_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?
|
2018-04-25 12:08:33 +01:00
|
|
|
next if !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
|
2018-05-22 14:46:14 +01:00
|
|
|
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-04-07 13:28:15 -05:00
|
|
|
if (dep = dylib_to_dep(dylib))
|
2018-04-07 15:35:27 -05:00
|
|
|
@broken_deps[dep] |= [dylib]
|
2018-04-07 14:46:31 -05:00
|
|
|
else
|
|
|
|
@broken_dylibs << dylib
|
2018-03-30 15:55:14 -05:00
|
|
|
end
|
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-05-22 14:46:14 +01:00
|
|
|
if formula
|
|
|
|
@indirect_deps, @undeclared_deps, @unnecessary_deps =
|
|
|
|
check_undeclared_deps
|
|
|
|
end
|
2018-01-16 17:37:59 -05:00
|
|
|
store_dylibs!
|
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
|
2018-04-25 10:26:02 +01:00
|
|
|
|
2018-04-28 17:24:43 +01:00
|
|
|
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
|
2018-04-25 10:26:02 +01:00
|
|
|
recursive_deps = formula.declared_runtime_dependencies.map do |dep|
|
|
|
|
begin
|
|
|
|
dep.to_formula.name
|
|
|
|
rescue FormulaUnavailableError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end.compact
|
|
|
|
|
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)
|
2018-04-28 17:24:43 +01:00
|
|
|
indirect_deps << full_name unless declared_deps_names.include?(name)
|
2018-02-10 08:34:23 -05:00
|
|
|
else
|
|
|
|
undeclared_deps << full_name
|
|
|
|
end
|
|
|
|
end
|
2018-04-25 10:26:02 +01:00
|
|
|
|
2018-02-10 08:34:23 -05:00
|
|
|
sort_by_formula_full_name!(indirect_deps)
|
|
|
|
sort_by_formula_full_name!(undeclared_deps)
|
2018-04-25 10:26:02 +01:00
|
|
|
|
2018-04-28 17:24:43 +01:00
|
|
|
unnecessary_deps = declared_deps_full_names.reject do |full_name|
|
|
|
|
next true if Formula[full_name].bin.directory?
|
2018-02-10 08:34:23 -05:00
|
|
|
name = full_name.split("/").last
|
|
|
|
@brewed_dylibs.keys.map { |x| x.split("/").last }.include?(name)
|
2016-09-11 17:49:27 +01:00
|
|
|
end
|
2018-04-25 10:26:02 +01:00
|
|
|
|
2018-04-07 15:35:27 -05:00
|
|
|
missing_deps = @broken_deps.values.flatten.map { |d| dylib_to_dep(d) }
|
2018-03-21 13:10:23 -05:00
|
|
|
unnecessary_deps -= missing_deps
|
2018-04-25 11:44:29 +01:00
|
|
|
|
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
|
|
|
|
|
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)
|
2018-04-24 09:52:51 +01:00
|
|
|
def display_items(label, things, puts_output: true)
|
2016-07-07 20:41:14 +08:00
|
|
|
return if things.empty?
|
2018-04-24 09:52:51 +01:00
|
|
|
output = "#{label}:"
|
2016-07-07 20:41:14 +08:00
|
|
|
if things.is_a? Hash
|
2018-04-07 13:33:10 -05:00
|
|
|
things.keys.sort.each do |list_label|
|
|
|
|
things[list_label].sort.each do |item|
|
2018-04-24 09:52:51 +01:00
|
|
|
output += "\n #{item} (#{list_label})"
|
2016-07-07 20:41:14 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
things.sort.each do |item|
|
2018-04-24 09:52:51 +01:00
|
|
|
output += "\n #{item}"
|
2016-07-07 20:41:14 +08:00
|
|
|
end
|
|
|
|
end
|
2018-04-24 09:52:51 +01:00
|
|
|
puts output if puts_output
|
|
|
|
output
|
2016-07-07 20:41:14 +08:00
|
|
|
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
|
2018-01-16 17:37:59 -05:00
|
|
|
|
2018-05-21 16:15:52 -04:00
|
|
|
# Helper function to reset dylib values
|
2018-01-16 17:37:59 -05:00
|
|
|
def reset_dylibs!
|
2018-05-21 16:15:52 -04:00
|
|
|
store&.flush_cache!
|
2018-01-16 17:37:59 -05:00
|
|
|
@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 }
|
2018-04-09 14:00:48 -04:00
|
|
|
@broken_deps = Hash.new { |h, k| h[k] = [] }
|
2018-01-16 17:37:59 -05:00
|
|
|
@indirect_deps = []
|
|
|
|
@undeclared_deps = []
|
|
|
|
@unnecessary_deps = []
|
|
|
|
end
|
|
|
|
|
|
|
|
# Updates data store with package path values
|
|
|
|
def store_dylibs!
|
2018-05-21 16:15:52 -04:00
|
|
|
store&.update!(
|
2018-04-09 13:49:58 -04:00
|
|
|
system_dylibs: system_dylibs,
|
|
|
|
variable_dylibs: variable_dylibs,
|
|
|
|
broken_dylibs: broken_dylibs,
|
|
|
|
indirect_deps: indirect_deps,
|
2018-04-09 14:00:48 -04:00
|
|
|
broken_deps: broken_deps,
|
2018-04-09 13:49:58 -04:00
|
|
|
undeclared_deps: undeclared_deps,
|
|
|
|
unnecessary_deps: unnecessary_deps,
|
|
|
|
brewed_dylibs: brewed_dylibs,
|
|
|
|
reverse_links: reverse_links,
|
2018-01-16 17:37:59 -05:00
|
|
|
)
|
|
|
|
end
|
2016-07-07 20:41:14 +08:00
|
|
|
end
|