| 
									
										
										
										
											2024-08-12 10:30:59 +01:00
										 |  |  | # typed: true # rubocop:todo Sorbet/StrictSigil | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 20:41:14 +08:00
										 |  |  | require "keg" | 
					
						
							|  |  |  | require "formula" | 
					
						
							| 
									
										
										
										
											2018-03-06 12:07:57 -05:00
										 |  |  | require "linkage_cache_store" | 
					
						
							| 
									
										
										
										
											2020-06-24 16:24:39 +01:00
										 |  |  | require "fiddle" | 
					
						
							| 
									
										
										
										
											2025-08-20 19:20:19 +01:00
										 |  |  | require "utils/output" | 
					
						
							| 
									
										
										
										
											2016-07-07 20:41:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 19:56:24 +02:00
										 |  |  | # Check for broken/missing linkage in a formula's keg. | 
					
						
							| 
									
										
										
										
											2016-07-07 20:41:14 +08:00
										 |  |  | class LinkageChecker | 
					
						
							| 
									
										
										
										
											2025-08-20 19:20:19 +01:00
										 |  |  |   include Utils::Output::Mixin | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-07 11:29:33 +01:00
										 |  |  |   attr_reader :undeclared_deps, :keg, :formula, :store | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-29 19:57:39 +01:00
										 |  |  |   def initialize(keg, formula = nil, cache_db:, rebuild_cache: false) | 
					
						
							| 
									
										
										
										
											2016-07-07 20:41:14 +08:00
										 |  |  |     @keg = keg | 
					
						
							| 
									
										
										
										
											2016-07-14 13:14:03 +08:00
										 |  |  |     @formula = formula || resolve_formula(keg) | 
					
						
							| 
									
										
										
										
											2018-06-29 19:57:39 +01:00
										 |  |  |     @store = LinkageCacheStore.new(keg.to_s, cache_db) | 
					
						
							| 
									
										
										
										
											2018-05-21 16:15:52 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01: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 } | 
					
						
							|  |  |  |     @broken_deps      = Hash.new { |h, k| h[k] = [] } | 
					
						
							|  |  |  |     @indirect_deps    = [] | 
					
						
							|  |  |  |     @undeclared_deps  = [] | 
					
						
							|  |  |  |     @unnecessary_deps = [] | 
					
						
							| 
									
										
										
										
											2018-09-05 15:29:42 -07:00
										 |  |  |     @unwanted_system_dylibs = [] | 
					
						
							| 
									
										
										
										
											2018-08-27 10:12:02 +01:00
										 |  |  |     @version_conflict_deps = [] | 
					
						
							| 
									
										
										
										
											2022-02-11 21:22:40 -08:00
										 |  |  |     @files_missing_rpaths = [] | 
					
						
							| 
									
										
										
										
											2023-06-20 16:42:13 -03:00
										 |  |  |     @executable_path_dylibs = [] | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |     check_dylibs(rebuild_cache:) | 
					
						
							| 
									
										
										
										
											2018-01-16 17:37:59 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-24 09:52:51 +01:00
										 |  |  |   def display_normal_output | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  |     display_items "System libraries", @system_dylibs | 
					
						
							|  |  |  |     display_items "Homebrew libraries", @brewed_dylibs | 
					
						
							|  |  |  |     display_items "Indirect dependencies with linkage", @indirect_deps | 
					
						
							| 
									
										
										
										
											2023-06-20 16:42:13 -03:00
										 |  |  |     display_items "@rpath-referenced libraries", @variable_dylibs | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  |     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-09-05 15:29:42 -07:00
										 |  |  |     display_items "Unwanted system libraries", @unwanted_system_dylibs | 
					
						
							| 
									
										
										
										
											2022-02-11 21:22:40 -08:00
										 |  |  |     display_items "Files with missing rpath", @files_missing_rpaths | 
					
						
							| 
									
										
										
										
											2023-06-20 16:42:13 -03:00
										 |  |  |     display_items "@executable_path references in libraries", @executable_path_dylibs | 
					
						
							| 
									
										
										
										
											2018-04-24 09:52:51 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def display_reverse_output | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  |     return if @reverse_links.empty? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-02 20:58:34 +05:30
										 |  |  |     sorted = @reverse_links.sort | 
					
						
							|  |  |  |     sorted.each do |dylib, files| | 
					
						
							| 
									
										
										
										
											2018-04-24 09:52:51 +01:00
										 |  |  |       puts dylib | 
					
						
							|  |  |  |       files.each do |f| | 
					
						
							| 
									
										
										
										
											2018-09-15 00:04:01 +02:00
										 |  |  |         unprefixed = f.to_s.delete_prefix "#{keg}/" | 
					
						
							| 
									
										
										
										
											2018-04-24 09:52:51 +01:00
										 |  |  |         puts "  #{unprefixed}" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  |       puts if dylib != sorted.last.first | 
					
						
							| 
									
										
										
										
											2018-04-24 09:52:51 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 17:42:17 +08:00
										 |  |  |   def display_test_output(puts_output: true, strict: false) | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |     display_items("Missing libraries", @broken_dylibs, puts_output:) | 
					
						
							|  |  |  |     display_items("Broken dependencies", @broken_deps, puts_output:) | 
					
						
							|  |  |  |     display_items("Unwanted system libraries", @unwanted_system_dylibs, puts_output:) | 
					
						
							|  |  |  |     display_items("Conflicting libraries", @version_conflict_deps, puts_output:) | 
					
						
							| 
									
										
										
										
											2022-03-10 07:09:47 +08:00
										 |  |  |     return unless strict | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-13 07:49:34 +01:00
										 |  |  |     display_items("Indirect dependencies with linkage", @indirect_deps, puts_output:) | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |     display_items("Undeclared dependencies with linkage", @undeclared_deps, puts_output:) | 
					
						
							|  |  |  |     display_items("Files with missing rpath", @files_missing_rpaths, puts_output:) | 
					
						
							|  |  |  |     display_items "@executable_path references in libraries", @executable_path_dylibs, puts_output: | 
					
						
							| 
									
										
										
										
											2018-04-24 09:52:51 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 09:38:52 +01:00
										 |  |  |   sig { params(test: T::Boolean, strict: T::Boolean).returns(T::Boolean) } | 
					
						
							|  |  |  |   def broken_library_linkage?(test: false, strict: false) | 
					
						
							|  |  |  |     raise ArgumentError, "Strict linkage checking requires test mode to be enabled." if strict && !test | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-07 22:58:54 +00:00
										 |  |  |     issues = [@broken_deps, @broken_dylibs] | 
					
						
							| 
									
										
										
										
											2022-08-23 09:38:52 +01:00
										 |  |  |     if test | 
					
						
							| 
									
										
										
										
											2023-12-07 22:58:54 +00:00
										 |  |  |       issues += [@unwanted_system_dylibs, @version_conflict_deps] | 
					
						
							| 
									
										
										
										
											2024-05-13 07:49:34 +01:00
										 |  |  |       issues += [@indirect_deps, @undeclared_deps, @files_missing_rpaths, @executable_path_dylibs] if strict | 
					
						
							| 
									
										
										
										
											2022-08-23 09:38:52 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |     issues.any?(&:present?) | 
					
						
							| 
									
										
										
										
											2020-07-10 21:20:46 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-24 09:52:51 +01:00
										 |  |  |   private | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-18 18:52:12 -05:00
										 |  |  |   def dylib_to_dep(dylib) | 
					
						
							| 
									
										
										
										
											2020-11-16 22:18:56 +01:00
										 |  |  |     dylib =~ %r{#{Regexp.escape(HOMEBREW_PREFIX)}/(opt|Cellar)/([\w+-.@]+)/}o | 
					
						
							| 
									
										
										
										
											2018-03-20 12:30:14 -05:00
										 |  |  |     Regexp.last_match(2) | 
					
						
							| 
									
										
										
										
											2018-03-18 18:52:12 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-02 22:42:08 -04:00
										 |  |  |   sig { params(file: String).returns(T::Boolean) } | 
					
						
							|  |  |  |   def broken_dylibs_allowed?(file) | 
					
						
							|  |  |  |     return false if formula.name != "julia" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     file.start_with?("#{formula.prefix.realpath}/share/julia/compiled/") | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-06 13:27:59 +01:00
										 |  |  |   def check_dylibs(rebuild_cache:) | 
					
						
							|  |  |  |     keg_files_dylibs = nil | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if rebuild_cache | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |       store&.delete! | 
					
						
							| 
									
										
										
										
											2018-06-06 13:27:59 +01:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |       keg_files_dylibs = store&.fetch(:keg_files_dylibs) | 
					
						
							| 
									
										
										
										
											2018-06-06 13:27:59 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  |     keg_files_dylibs_was_empty = false | 
					
						
							|  |  |  |     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? | 
					
						
							| 
									
										
										
										
											2024-09-29 05:15:36 +08:00
										 |  |  |         next unless file.arch_compatible?(Hardware::CPU.arch) | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  |         # weakly loaded dylibs may not actually exist on disk, so skip them | 
					
						
							|  |  |  |         # when checking for broken linkage | 
					
						
							|  |  |  |         keg_files_dylibs[file] = | 
					
						
							| 
									
										
										
										
											2024-06-10 18:56:50 +01:00
										 |  |  |           file.dynamically_linked_libraries(except: :DYLIB_USE_WEAK_LINK) | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-01-16 17:37:59 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-17 00:15:51 -05:00
										 |  |  |     checked_dylibs = Set.new | 
					
						
							| 
									
										
										
										
											2016-11-07 19:37:52 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  |     keg_files_dylibs.each do |file, dylibs| | 
					
						
							| 
									
										
										
										
											2021-12-17 08:14:44 -08:00
										 |  |  |       file_has_any_rpath_dylibs = T.let(false, T::Boolean) | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  |       dylibs.each do |dylib| | 
					
						
							| 
									
										
										
										
											2016-07-07 20:41:14 +08:00
										 |  |  |         @reverse_links[dylib] << file | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-11 21:22:40 -08:00
										 |  |  |         # Files that link @rpath-prefixed dylibs must include at | 
					
						
							| 
									
										
										
										
											2021-12-17 08:14:44 -08:00
										 |  |  |         # least one rpath in order to resolve it. | 
					
						
							|  |  |  |         if !file_has_any_rpath_dylibs && (dylib.start_with? "@rpath/") | 
					
						
							|  |  |  |           file_has_any_rpath_dylibs = true | 
					
						
							|  |  |  |           pathname = Pathname(file) | 
					
						
							| 
									
										
										
										
											2024-09-02 22:42:08 -04:00
										 |  |  |           @files_missing_rpaths << file if pathname.rpaths.empty? && !broken_dylibs_allowed?(file.to_s) | 
					
						
							| 
									
										
										
										
											2021-12-17 08:14:44 -08:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-17 00:15:51 -05:00
										 |  |  |         next if checked_dylibs.include? dylib | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  |         checked_dylibs << dylib | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-20 16:42:13 -03:00
										 |  |  |         if dylib.start_with? "@rpath" | 
					
						
							| 
									
										
										
										
											2016-07-07 20:41:14 +08:00
										 |  |  |           @variable_dylibs << dylib | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  |           next | 
					
						
							| 
									
										
										
										
											2023-06-20 16:42:13 -03:00
										 |  |  |         elsif dylib.start_with?("@executable_path") && !Pathname(file).binary_executable? | 
					
						
							|  |  |  |           @executable_path_dylibs << dylib | 
					
						
							|  |  |  |           next | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         begin | 
					
						
							| 
									
										
										
										
											2024-04-28 03:23:21 +02:00
										 |  |  |           owner = Keg.for(Pathname(dylib)) | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  |         rescue NotAKegError | 
					
						
							|  |  |  |           @system_dylibs << dylib | 
					
						
							|  |  |  |         rescue Errno::ENOENT | 
					
						
							|  |  |  |           next if harmless_broken_link?(dylib) | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  |           if (dep = dylib_to_dep(dylib)) | 
					
						
							|  |  |  |             @broken_deps[dep] |= [dylib] | 
					
						
							| 
									
										
										
										
											2024-08-04 05:54:50 +08:00
										 |  |  |           elsif system_libraries_exist_in_cache? && dylib_found_in_shared_cache?(dylib) | 
					
						
							| 
									
										
										
										
											2020-06-24 16:24:39 +01:00
										 |  |  |             # If we cannot associate the dylib with a dependency, then it may be a system library. | 
					
						
							| 
									
										
										
										
											2024-08-04 05:54:50 +08:00
										 |  |  |             # Check the dylib shared cache for the library to verify this. | 
					
						
							| 
									
										
										
										
											2020-06-24 16:24:39 +01:00
										 |  |  |             @system_dylibs << dylib | 
					
						
							| 
									
										
										
										
											2024-09-02 22:42:08 -04:00
										 |  |  |           elsif !system_framework?(dylib) && !broken_dylibs_allowed?(file.to_s) | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  |             @broken_dylibs << dylib | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2016-07-07 20:41:14 +08:00
										 |  |  |         else | 
					
						
							| 
									
										
										
										
											2024-04-28 03:23:21 +02:00
										 |  |  |           tap = owner.tab.tap | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  |           f = if tap.nil? || tap.core_tap? | 
					
						
							|  |  |  |             owner.name | 
					
						
							| 
									
										
										
										
											2016-07-07 20:41:14 +08:00
										 |  |  |           else | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  |             "#{tap}/#{owner.name}" | 
					
						
							| 
									
										
										
										
											2016-07-07 20:41:14 +08:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  |           @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 | 
					
						
							| 
									
										
										
										
											2018-08-27 10:12:02 +01:00
										 |  |  |       @indirect_deps, @undeclared_deps, @unnecessary_deps, | 
					
						
							|  |  |  |         @version_conflict_deps = check_formula_deps | 
					
						
							| 
									
										
										
										
											2018-05-22 14:46:14 +01:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-06-01 13:26:45 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return unless keg_files_dylibs_was_empty | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |     store&.update!(keg_files_dylibs:) | 
					
						
							| 
									
										
										
										
											2016-07-14 13:14:03 +08:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-15 19:52:21 +00:00
										 |  |  |   def system_libraries_exist_in_cache? | 
					
						
							|  |  |  |     false | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-04 05:54:50 +08:00
										 |  |  |   def dylib_found_in_shared_cache?(dylib) | 
					
						
							|  |  |  |     @dyld_shared_cache_contains_path ||= begin | 
					
						
							|  |  |  |       libc = Fiddle.dlopen("/usr/lib/libSystem.B.dylib") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       Fiddle::Function.new( | 
					
						
							|  |  |  |         libc["_dyld_shared_cache_contains_path"], | 
					
						
							|  |  |  |         [Fiddle::TYPE_CONST_STRING], | 
					
						
							|  |  |  |         Fiddle::TYPE_BOOL, | 
					
						
							|  |  |  |       ) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @dyld_shared_cache_contains_path.call(dylib) | 
					
						
							| 
									
										
										
										
											2020-06-24 16:24:39 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-27 10:12:02 +01:00
										 |  |  |   def check_formula_deps | 
					
						
							| 
									
										
										
										
											2016-09-11 17:49:27 +01:00
										 |  |  |     filter_out = proc do |dep| | 
					
						
							| 
									
										
										
										
											2024-09-07 13:16:50 -04:00
										 |  |  |       next true if dep.build? || dep.test? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-07 13:49:05 -08:00
										 |  |  |       (dep.optional? || dep.recommended?) && formula.build.without?(dep) | 
					
						
							| 
									
										
										
										
											2016-09-11 17:49:27 +01:00
										 |  |  |     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-07-16 16:46:39 +01:00
										 |  |  |     recursive_deps = formula.runtime_formula_dependencies(undeclared: false) | 
					
						
							|  |  |  |                             .map(&:name) | 
					
						
							| 
									
										
										
										
											2018-04-25 10:26:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 08:34:23 -05:00
										 |  |  |       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-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 08:34:23 -05:00
										 |  |  |       name = full_name.split("/").last | 
					
						
							| 
									
										
										
										
											2018-08-27 10:12:02 +01:00
										 |  |  |       @brewed_dylibs.keys.map { |l| l.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-08-27 10:12:02 +01:00
										 |  |  |     version_hash = {} | 
					
						
							|  |  |  |     version_conflict_deps = Set.new | 
					
						
							| 
									
										
										
										
											2020-02-19 11:18:40 +00:00
										 |  |  |     @brewed_dylibs.each_key do |l| | 
					
						
							| 
									
										
										
										
											2018-08-27 10:12:02 +01:00
										 |  |  |       name = l.split("/").last | 
					
						
							|  |  |  |       unversioned_name, = name.split("@") | 
					
						
							|  |  |  |       version_hash[unversioned_name] ||= Set.new | 
					
						
							|  |  |  |       version_hash[unversioned_name] << name | 
					
						
							|  |  |  |       next if version_hash[unversioned_name].length < 2
 | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-27 10:12:02 +01:00
										 |  |  |       version_conflict_deps += version_hash[unversioned_name] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     [indirect_deps, undeclared_deps, | 
					
						
							|  |  |  |      unnecessary_deps, version_conflict_deps.to_a] | 
					
						
							| 
									
										
										
										
											2018-02-10 08:34:23 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def sort_by_formula_full_name!(arr) | 
					
						
							|  |  |  |     arr.sort! do |a, b| | 
					
						
							| 
									
										
										
										
											2020-12-01 17:04:59 +00:00
										 |  |  |       if a.include?("/") && b.exclude?("/") | 
					
						
							| 
									
										
										
										
											2016-09-11 17:49:27 +01:00
										 |  |  |         1
 | 
					
						
							| 
									
										
										
										
											2020-12-01 17:04:59 +00:00
										 |  |  |       elsif a.exclude?("/") && b.include?("/") | 
					
						
							| 
									
										
										
										
											2016-09-11 17:49:27 +01:00
										 |  |  |         -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 | 
					
						
							| 
									
										
										
										
											2022-09-19 12:37:32 +08:00
										 |  |  |     # dyld will fall back to Apple libc++ if LLVM's is not available. | 
					
						
							| 
									
										
										
										
											2022-07-18 11:57:21 +08:00
										 |  |  |     [ | 
					
						
							| 
									
										
										
										
											2017-07-22 15:39:53 -04:00
										 |  |  |       "/usr/lib/libgcc_s_ppc64.1.dylib", | 
					
						
							|  |  |  |       "/opt/local/lib/libgcc/libgcc_s.1.dylib", | 
					
						
							| 
									
										
										
										
											2022-09-19 12:37:32 +08:00
										 |  |  |       # TODO: Report linkage with `/usr/lib/libc++.1.dylib` when this link is broken. | 
					
						
							|  |  |  |       "#{HOMEBREW_PREFIX}/opt/llvm/lib/libc++.1.dylib", | 
					
						
							| 
									
										
										
										
											2017-07-22 15:39:53 -04:00
										 |  |  |     ].include?(dylib) | 
					
						
							| 
									
										
										
										
											2022-07-18 11:57:21 +08:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2020-11-30 14:23:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-18 11:57:21 +08:00
										 |  |  |   def system_framework?(dylib) | 
					
						
							| 
									
										
										
										
											2020-11-30 14:23:59 +00:00
										 |  |  |     dylib.start_with?("/System/Library/Frameworks/") | 
					
						
							| 
									
										
										
										
											2017-06-19 22:48:01 -04:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 20:41:14 +08:00
										 |  |  |   # Display a list of things. | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |   # 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-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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| | 
					
						
							| 
									
										
										
										
											2020-07-21 19:56:53 +00:00
										 |  |  |         output += if item.is_a? Regexp | 
					
						
							| 
									
										
										
										
											2020-07-23 08:38:42 -05:00
										 |  |  |           "\n  #{item.inspect}" | 
					
						
							| 
									
										
										
										
											2020-07-21 19:56:53 +00:00
										 |  |  |         else | 
					
						
							|  |  |  |           "\n  #{item}" | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2016-07-07 20:41:14 +08:00
										 |  |  | end | 
					
						
							| 
									
										
										
										
											2018-09-05 15:29:42 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | require "extend/os/linkage_checker" |