2023-03-05 18:18:59 +00:00
|
|
|
# typed: true
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
require "dependable"
|
2013-01-26 20:05:39 -06:00
|
|
|
|
|
|
|
# A dependency on another Homebrew formula.
|
2020-08-14 04:06:29 +02:00
|
|
|
#
|
|
|
|
# @api private
|
2013-01-26 20:05:39 -06:00
|
|
|
class Dependency
|
2017-06-26 07:30:28 +02:00
|
|
|
extend Forwardable
|
2013-01-26 20:05:39 -06:00
|
|
|
include Dependable
|
2021-03-19 03:21:27 +00:00
|
|
|
extend Cachable
|
2013-01-26 20:05:39 -06:00
|
|
|
|
2023-06-19 04:37:55 +01:00
|
|
|
attr_reader :name, :env_proc, :option_names, :tap
|
2013-01-26 20:05:39 -06:00
|
|
|
|
2019-04-19 21:46:20 +09:00
|
|
|
DEFAULT_ENV_PROC = proc {}.freeze
|
2020-08-14 04:06:29 +02:00
|
|
|
private_constant :DEFAULT_ENV_PROC
|
2014-02-27 14:22:43 -06:00
|
|
|
|
2023-06-19 04:37:55 +01:00
|
|
|
def initialize(name, tags = [], env_proc = DEFAULT_ENV_PROC, option_names = [name&.split("/")&.last])
|
2018-06-01 19:22:33 +01:00
|
|
|
raise ArgumentError, "Dependency must have a name!" unless name
|
|
|
|
|
2014-02-27 14:22:43 -06:00
|
|
|
@name = name
|
2013-05-06 16:08:50 -05:00
|
|
|
@tags = tags
|
2014-02-27 14:22:43 -06:00
|
|
|
@env_proc = env_proc
|
2015-12-14 19:47:19 +00:00
|
|
|
@option_names = option_names
|
2023-06-19 04:37:55 +01:00
|
|
|
|
|
|
|
@tap = Tap.fetch(Regexp.last_match(1), Regexp.last_match(2)) if name =~ HOMEBREW_TAP_FORMULA_REGEX
|
2013-01-26 20:05:39 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
name
|
|
|
|
end
|
|
|
|
|
|
|
|
def ==(other)
|
2014-11-21 16:08:37 -06:00
|
|
|
instance_of?(other.class) && name == other.name && tags == other.tags
|
2013-01-26 20:05:39 -06:00
|
|
|
end
|
2016-09-23 18:13:48 +02:00
|
|
|
alias eql? ==
|
2013-01-26 20:05:39 -06:00
|
|
|
|
|
|
|
def hash
|
2022-04-23 01:48:15 +01:00
|
|
|
[name, tags].hash
|
2013-01-26 20:05:39 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_formula
|
2014-08-11 17:48:30 -05:00
|
|
|
formula = Formulary.factory(name)
|
|
|
|
formula.build = BuildOptions.new(options, formula.options)
|
|
|
|
formula
|
2013-01-26 20:05:39 -06:00
|
|
|
end
|
|
|
|
|
2019-12-03 11:42:09 +00:00
|
|
|
def installed?
|
|
|
|
to_formula.latest_version_installed?
|
2023-06-19 04:37:55 +01:00
|
|
|
rescue FormulaUnavailableError
|
|
|
|
false
|
2019-12-03 11:42:09 +00:00
|
|
|
end
|
2013-01-26 20:05:39 -06:00
|
|
|
|
2023-05-16 18:45:37 -05:00
|
|
|
def satisfied?(inherited_options = [])
|
2014-02-27 14:22:43 -06:00
|
|
|
installed? && missing_options(inherited_options).empty?
|
2013-01-27 19:40:10 -06:00
|
|
|
end
|
|
|
|
|
2014-08-23 22:41:35 -05:00
|
|
|
def missing_options(inherited_options)
|
2016-12-10 17:58:41 +00:00
|
|
|
formula = to_formula
|
|
|
|
required = options
|
|
|
|
required |= inherited_options
|
|
|
|
required &= formula.options.to_a
|
|
|
|
required -= Tab.for_formula(formula).used_options
|
|
|
|
required
|
2013-01-27 19:40:10 -06:00
|
|
|
end
|
|
|
|
|
2013-06-03 15:08:47 -05:00
|
|
|
def modify_build_environment
|
2017-09-24 19:24:46 +01:00
|
|
|
env_proc&.call
|
2013-06-03 15:08:47 -05:00
|
|
|
end
|
|
|
|
|
2023-06-19 06:03:31 +01:00
|
|
|
sig { overridable.returns(T::Boolean) }
|
|
|
|
def uses_from_macos?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2020-10-20 12:03:48 +02:00
|
|
|
sig { returns(String) }
|
2013-06-07 20:36:13 -05:00
|
|
|
def inspect
|
2014-07-01 15:07:06 -05:00
|
|
|
"#<#{self.class.name}: #{name.inspect} #{tags.inspect}>"
|
2013-06-07 20:36:13 -05:00
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Define marshaling semantics because we cannot serialize @env_proc.
|
2013-09-27 23:23:22 -05:00
|
|
|
def _dump(*)
|
|
|
|
Marshal.dump([name, tags])
|
|
|
|
end
|
|
|
|
|
|
|
|
def self._load(marshaled)
|
2017-10-07 00:31:28 +02:00
|
|
|
new(*Marshal.load(marshaled)) # rubocop:disable Security/MarshalLoad
|
2013-09-27 23:23:22 -05:00
|
|
|
end
|
|
|
|
|
2023-06-28 23:01:31 +01:00
|
|
|
sig { params(formula: Formula).returns(T.self_type) }
|
|
|
|
def dup_with_formula_name(formula)
|
|
|
|
self.class.new(formula.full_name.to_s, tags, env_proc, option_names)
|
|
|
|
end
|
|
|
|
|
2013-05-10 23:45:05 -05:00
|
|
|
class << self
|
2020-11-05 17:17:03 -05:00
|
|
|
# Expand the dependencies of each dependent recursively, optionally yielding
|
2018-10-18 21:42:43 -04:00
|
|
|
# `[dependent, dep]` pairs to allow callers to apply arbitrary filters to
|
2013-05-10 23:45:05 -05:00
|
|
|
# the list.
|
|
|
|
# The default filter, which is applied when a block is not given, omits
|
2023-07-17 12:30:12 -07:00
|
|
|
# optionals and recommends based on what the dependent has asked for
|
2022-06-14 15:03:55 -04:00
|
|
|
def expand(dependent, deps = dependent.deps, cache_key: nil, &block)
|
2016-01-18 08:59:43 +00:00
|
|
|
# Keep track dependencies to avoid infinite cyclic dependency recursion.
|
|
|
|
@expand_stack ||= []
|
|
|
|
@expand_stack.push dependent.name
|
|
|
|
|
2021-03-19 03:21:27 +00:00
|
|
|
if cache_key.present?
|
|
|
|
cache[cache_key] ||= {}
|
2021-05-29 18:08:42 -04:00
|
|
|
return cache[cache_key][cache_id dependent].dup if cache[cache_key][cache_id dependent]
|
2021-03-19 03:21:27 +00:00
|
|
|
end
|
|
|
|
|
2014-02-13 15:38:39 -05:00
|
|
|
expanded_deps = []
|
|
|
|
|
|
|
|
deps.each do |dep|
|
2014-02-27 14:22:42 -06:00
|
|
|
next if dependent.name == dep.name
|
|
|
|
|
2022-06-14 15:03:55 -04:00
|
|
|
case action(dependent, dep, &block)
|
2013-07-22 21:36:11 -05:00
|
|
|
when :prune
|
2014-02-13 15:38:39 -05:00
|
|
|
next
|
2013-07-22 21:36:11 -05:00
|
|
|
when :skip
|
2016-01-18 08:59:43 +00:00
|
|
|
next if @expand_stack.include? dep.name
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2022-06-14 15:03:55 -04:00
|
|
|
expanded_deps.concat(expand(dep.to_formula, cache_key: cache_key, &block))
|
2013-11-13 10:38:14 -06:00
|
|
|
when :keep_but_prune_recursive_deps
|
2014-02-13 15:38:39 -05:00
|
|
|
expanded_deps << dep
|
2013-05-10 23:45:05 -05:00
|
|
|
else
|
2016-01-18 08:59:43 +00:00
|
|
|
next if @expand_stack.include? dep.name
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2023-06-28 23:01:31 +01:00
|
|
|
dep_formula = dep.to_formula
|
|
|
|
expanded_deps.concat(expand(dep_formula, cache_key: cache_key, &block))
|
|
|
|
|
|
|
|
# Fixes names for renamed/aliased formulae.
|
|
|
|
dep = dep.dup_with_formula_name(dep_formula)
|
2014-02-13 15:38:39 -05:00
|
|
|
expanded_deps << dep
|
2013-05-10 23:45:05 -05:00
|
|
|
end
|
2014-02-13 15:38:39 -05:00
|
|
|
end
|
2013-06-07 22:27:30 -05:00
|
|
|
|
2021-03-19 03:21:27 +00:00
|
|
|
expanded_deps = merge_repeats(expanded_deps)
|
2021-05-29 18:08:42 -04:00
|
|
|
cache[cache_key][cache_id dependent] = expanded_deps.dup if cache_key.present?
|
2021-03-19 03:21:27 +00:00
|
|
|
expanded_deps
|
2016-02-06 13:32:12 +08:00
|
|
|
ensure
|
|
|
|
@expand_stack.pop
|
2013-05-10 23:45:05 -05:00
|
|
|
end
|
|
|
|
|
2022-06-14 15:03:55 -04:00
|
|
|
def action(dependent, dep, &block)
|
2013-07-22 21:36:11 -05:00
|
|
|
catch(:action) do
|
2020-11-16 22:18:56 +01:00
|
|
|
if block
|
2013-01-26 20:05:39 -06:00
|
|
|
yield dependent, dep
|
|
|
|
elsif dep.optional? || dep.recommended?
|
2013-12-09 14:36:10 -06:00
|
|
|
prune unless dependent.build.with?(dep)
|
2013-01-26 20:05:39 -06:00
|
|
|
end
|
|
|
|
end
|
2013-05-10 23:45:05 -05:00
|
|
|
end
|
2013-01-26 20:05:39 -06:00
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Prune a dependency and its dependencies recursively.
|
2020-10-20 12:03:48 +02:00
|
|
|
sig { void }
|
2013-05-10 23:45:05 -05:00
|
|
|
def prune
|
2013-07-22 21:36:11 -05:00
|
|
|
throw(:action, :prune)
|
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Prune a single dependency but do not prune its dependencies.
|
2020-10-20 12:03:48 +02:00
|
|
|
sig { void }
|
2013-07-22 21:36:11 -05:00
|
|
|
def skip
|
|
|
|
throw(:action, :skip)
|
2013-05-10 23:45:05 -05:00
|
|
|
end
|
2013-06-07 22:27:30 -05:00
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Keep a dependency, but prune its dependencies.
|
2020-10-20 12:03:48 +02:00
|
|
|
sig { void }
|
2013-11-13 10:38:14 -06:00
|
|
|
def keep_but_prune_recursive_deps
|
|
|
|
throw(:action, :keep_but_prune_recursive_deps)
|
|
|
|
end
|
|
|
|
|
2014-11-21 16:08:37 -06:00
|
|
|
def merge_repeats(all)
|
2014-11-21 22:25:36 -06:00
|
|
|
grouped = all.group_by(&:name)
|
|
|
|
|
|
|
|
all.map(&:name).uniq.map do |name|
|
|
|
|
deps = grouped.fetch(name)
|
2014-11-21 16:08:37 -06:00
|
|
|
dep = deps.first
|
2015-12-15 10:50:38 +01:00
|
|
|
tags = merge_tags(deps)
|
2015-12-14 19:47:19 +00:00
|
|
|
option_names = deps.flat_map(&:option_names).uniq
|
2023-06-19 06:03:31 +01:00
|
|
|
kwargs = {}
|
|
|
|
kwargs[:bounds] = dep.bounds if dep.uses_from_macos?
|
2023-06-19 06:06:15 +01:00
|
|
|
# TODO: simpify to just **kwargs when we require Ruby >= 2.7
|
|
|
|
if kwargs.empty?
|
|
|
|
dep.class.new(name, tags, dep.env_proc, option_names)
|
|
|
|
else
|
|
|
|
dep.class.new(name, tags, dep.env_proc, option_names, **kwargs)
|
|
|
|
end
|
2013-06-07 22:27:30 -05:00
|
|
|
end
|
|
|
|
end
|
2015-12-15 10:50:38 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-05-29 18:08:42 -04:00
|
|
|
def cache_id(dependent)
|
|
|
|
"#{dependent.full_name}_#{dependent.class}"
|
|
|
|
end
|
|
|
|
|
2015-12-15 10:50:38 +01:00
|
|
|
def merge_tags(deps)
|
2018-03-08 09:46:53 +00:00
|
|
|
other_tags = deps.flat_map(&:option_tags).uniq
|
|
|
|
other_tags << :test if deps.flat_map(&:tags).include?(:test)
|
|
|
|
merge_necessity(deps) + merge_temporality(deps) + other_tags
|
2015-12-15 10:50:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def merge_necessity(deps)
|
|
|
|
# Cannot use `deps.any?(&:required?)` here due to its definition.
|
|
|
|
if deps.any? { |dep| !dep.recommended? && !dep.optional? }
|
|
|
|
[] # Means required dependency.
|
|
|
|
elsif deps.any?(&:recommended?)
|
|
|
|
[:recommended]
|
|
|
|
else # deps.all?(&:optional?)
|
|
|
|
[:optional]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_temporality(deps)
|
2023-06-19 06:06:15 +01:00
|
|
|
new_tags = []
|
|
|
|
new_tags << :build if deps.all?(&:build?)
|
|
|
|
new_tags << :implicit if deps.all?(&:implicit?)
|
|
|
|
new_tags
|
2015-12-15 10:50:38 +01:00
|
|
|
end
|
2013-01-26 20:05:39 -06:00
|
|
|
end
|
|
|
|
end
|
2023-06-19 06:03:31 +01:00
|
|
|
|
|
|
|
# A dependency that marked as "installed" on macOS
|
|
|
|
class UsesFromMacOSDependency < Dependency
|
|
|
|
attr_reader :bounds
|
|
|
|
|
|
|
|
def initialize(name, tags = [], env_proc = DEFAULT_ENV_PROC, option_names = [name], bounds:)
|
|
|
|
super(name, tags, env_proc, option_names)
|
|
|
|
|
|
|
|
@bounds = bounds
|
|
|
|
end
|
|
|
|
|
|
|
|
def installed?
|
|
|
|
use_macos_install? || super
|
|
|
|
end
|
|
|
|
|
|
|
|
sig { returns(T::Boolean) }
|
|
|
|
def use_macos_install?
|
|
|
|
# Check whether macOS is new enough for dependency to not be required.
|
|
|
|
if Homebrew::SimulateSystem.simulating_or_running_on_macos?
|
|
|
|
# Assume the oldest macOS version when simulating a generic macOS version
|
|
|
|
return true if Homebrew::SimulateSystem.current_os == :macos && !bounds.key?(:since)
|
|
|
|
|
|
|
|
if Homebrew::SimulateSystem.current_os != :macos
|
|
|
|
current_os = MacOSVersion.from_symbol(Homebrew::SimulateSystem.current_os)
|
|
|
|
since_os = MacOSVersion.from_symbol(bounds[:since]) if bounds.key?(:since)
|
|
|
|
return true if current_os >= since_os
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
sig { override.returns(T::Boolean) }
|
|
|
|
def uses_from_macos?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
sig { override.params(formula: Formula).returns(T.self_type) }
|
|
|
|
def dup_with_formula_name(formula)
|
|
|
|
self.class.new(formula.full_name.to_s, tags, env_proc, option_names, bounds: bounds)
|
|
|
|
end
|
2023-08-28 12:20:15 -04:00
|
|
|
|
|
|
|
sig { returns(String) }
|
|
|
|
def inspect
|
|
|
|
"#<#{self.class.name}: #{name.inspect} #{tags.inspect} #{bounds.inspect}>"
|
|
|
|
end
|
2023-06-19 06:03:31 +01:00
|
|
|
end
|