2020-10-10 14:16:11 +02:00
|
|
|
# typed: true
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
require "dependency"
|
|
|
|
require "dependencies"
|
|
|
|
require "requirement"
|
|
|
|
require "requirements"
|
2017-10-07 00:31:28 +02:00
|
|
|
require "extend/cachable"
|
2013-01-26 20:05:39 -06:00
|
|
|
|
|
|
|
## A dependency is a formula that another formula needs to install.
|
|
|
|
## A requirement is something other than a formula that another formula
|
|
|
|
## needs to be present. This includes external language modules,
|
|
|
|
## command-line tools in the path, or any arbitrary predicate.
|
|
|
|
##
|
|
|
|
## The `depends_on` method in the formula DSL is used to declare
|
|
|
|
## dependencies and requirements.
|
|
|
|
|
|
|
|
# This class is used by `depends_on` in the formula DSL to turn dependency
|
|
|
|
# specifications into the proper kinds of dependencies and requirements.
|
|
|
|
class DependencyCollector
|
2017-10-07 00:31:28 +02:00
|
|
|
extend Cachable
|
|
|
|
|
2013-01-26 20:05:39 -06:00
|
|
|
attr_reader :deps, :requirements
|
|
|
|
|
2020-10-20 12:03:48 +02:00
|
|
|
sig { void }
|
2013-01-26 20:05:39 -06:00
|
|
|
def initialize
|
2022-08-24 23:53:35 +01:00
|
|
|
# Ensure this is synced with `initialize_dup` and `freeze` (excluding simple objects like integers and booleans)
|
2013-01-26 20:05:39 -06:00
|
|
|
@deps = Dependencies.new
|
2014-07-03 14:50:57 -05:00
|
|
|
@requirements = Requirements.new
|
2022-08-25 11:04:37 +01:00
|
|
|
|
|
|
|
init_global_dep_tree_if_needed!
|
2013-01-26 20:05:39 -06:00
|
|
|
end
|
|
|
|
|
2022-08-24 23:53:35 +01:00
|
|
|
def initialize_dup(other)
|
2022-08-23 12:42:02 +01:00
|
|
|
super
|
|
|
|
@deps = @deps.dup
|
|
|
|
@requirements = @requirements.dup
|
|
|
|
end
|
|
|
|
|
2022-08-24 23:53:35 +01:00
|
|
|
def freeze
|
|
|
|
@deps.freeze
|
|
|
|
@requirements.freeze
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2013-01-27 16:53:42 -06:00
|
|
|
def add(spec)
|
2014-02-27 14:22:43 -06:00
|
|
|
case dep = fetch(spec)
|
2023-10-20 12:21:01 -04:00
|
|
|
when Array
|
|
|
|
dep.compact.each { |dep| @deps << dep }
|
2013-01-27 16:53:42 -06:00
|
|
|
when Dependency
|
|
|
|
@deps << dep
|
|
|
|
when Requirement
|
|
|
|
@requirements << dep
|
2022-08-23 12:42:02 +01:00
|
|
|
when nil
|
|
|
|
# no-op when we have a nil value
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
raise ArgumentError, "DependencyCollector#add passed something that isn't a Dependency or Requirement!"
|
2013-01-27 16:53:42 -06:00
|
|
|
end
|
|
|
|
dep
|
|
|
|
end
|
|
|
|
|
2014-02-27 14:22:43 -06:00
|
|
|
def fetch(spec)
|
2017-10-07 00:31:28 +02:00
|
|
|
self.class.cache.fetch(cache_key(spec)) { |key| self.class.cache[key] = build(spec) }
|
2014-02-27 14:22:43 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def cache_key(spec)
|
2023-10-20 12:21:01 -04:00
|
|
|
if spec.is_a?(Resource)
|
|
|
|
if spec.download_strategy <= CurlDownloadStrategy
|
|
|
|
return "#{spec.download_strategy}#{File.extname(spec.url).split("?").first}"
|
|
|
|
end
|
|
|
|
|
|
|
|
return spec.download_strategy
|
2014-02-27 14:22:43 -06:00
|
|
|
end
|
2023-10-20 12:21:01 -04:00
|
|
|
spec
|
2014-02-27 14:22:43 -06:00
|
|
|
end
|
|
|
|
|
2013-01-27 16:53:42 -06:00
|
|
|
def build(spec)
|
2016-09-20 22:03:08 +02:00
|
|
|
spec, tags = spec.is_a?(Hash) ? spec.first : spec
|
2013-05-06 16:08:50 -05:00
|
|
|
parse_spec(spec, Array(tags))
|
2013-01-26 20:05:39 -06:00
|
|
|
end
|
|
|
|
|
2022-08-25 11:04:37 +01:00
|
|
|
sig { params(related_formula_names: T::Array[String]).returns(T.nilable(Dependency)) }
|
|
|
|
def gcc_dep_if_needed(related_formula_names); end
|
|
|
|
|
|
|
|
sig { params(related_formula_names: T::Array[String]).returns(T.nilable(Dependency)) }
|
|
|
|
def glibc_dep_if_needed(related_formula_names); end
|
|
|
|
|
2018-01-10 13:19:45 +00:00
|
|
|
def git_dep_if_needed(tags)
|
2020-08-23 06:32:26 +02:00
|
|
|
return if Utils::Git.available?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2023-06-19 06:06:15 +01:00
|
|
|
Dependency.new("git", [*tags, :implicit])
|
2018-01-10 13:19:45 +00:00
|
|
|
end
|
|
|
|
|
2022-08-23 12:42:02 +01:00
|
|
|
def curl_dep_if_needed(tags)
|
2023-06-19 06:06:15 +01:00
|
|
|
Dependency.new("curl", [*tags, :implicit])
|
2021-07-26 12:39:25 +02:00
|
|
|
end
|
|
|
|
|
2018-01-10 13:19:45 +00:00
|
|
|
def subversion_dep_if_needed(tags)
|
2020-08-23 06:00:17 +02:00
|
|
|
return if Utils::Svn.available?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2023-06-19 06:06:15 +01:00
|
|
|
Dependency.new("subversion", [*tags, :implicit])
|
2018-01-10 13:19:45 +00:00
|
|
|
end
|
|
|
|
|
2017-12-23 16:38:06 +00:00
|
|
|
def cvs_dep_if_needed(tags)
|
2023-06-19 06:06:15 +01:00
|
|
|
Dependency.new("cvs", [*tags, :implicit]) unless which("cvs")
|
2017-12-23 16:38:06 +00:00
|
|
|
end
|
|
|
|
|
2017-12-23 16:36:33 +00:00
|
|
|
def xz_dep_if_needed(tags)
|
2023-06-19 06:06:15 +01:00
|
|
|
Dependency.new("xz", [*tags, :implicit]) unless which("xz")
|
2016-11-28 03:04:50 +00:00
|
|
|
end
|
|
|
|
|
2021-09-16 15:56:31 +01:00
|
|
|
def zstd_dep_if_needed(tags)
|
2023-06-19 06:06:15 +01:00
|
|
|
Dependency.new("zstd", [*tags, :implicit]) unless which("zstd")
|
2021-09-16 15:56:31 +01:00
|
|
|
end
|
|
|
|
|
2018-03-04 11:51:06 -08:00
|
|
|
def unzip_dep_if_needed(tags)
|
2023-06-19 06:06:15 +01:00
|
|
|
Dependency.new("unzip", [*tags, :implicit]) unless which("unzip")
|
2018-02-19 06:49:42 +00:00
|
|
|
end
|
2018-02-16 10:41:14 -06:00
|
|
|
|
2018-02-19 06:49:42 +00:00
|
|
|
def bzip2_dep_if_needed(tags)
|
2023-06-19 06:06:15 +01:00
|
|
|
Dependency.new("bzip2", [*tags, :implicit]) unless which("bzip2")
|
2018-02-19 06:49:42 +00:00
|
|
|
end
|
2018-02-16 10:41:14 -06:00
|
|
|
|
2016-10-15 13:10:35 +01:00
|
|
|
def self.tar_needs_xz_dependency?
|
2017-12-23 16:36:33 +00:00
|
|
|
!new.xz_dep_if_needed([]).nil?
|
2016-10-15 13:10:35 +01:00
|
|
|
end
|
|
|
|
|
2013-04-07 00:49:56 -05:00
|
|
|
private
|
2013-01-26 20:05:39 -06:00
|
|
|
|
2022-08-25 11:04:37 +01:00
|
|
|
sig { void }
|
|
|
|
def init_global_dep_tree_if_needed!; end
|
|
|
|
|
2023-09-01 19:37:32 +01:00
|
|
|
sig {
|
|
|
|
params(spec: T.any(String, Resource, Symbol, Requirement, Dependency, Class),
|
2023-10-20 12:21:01 -04:00
|
|
|
tags: T::Array[Symbol]).returns(T.any(Dependency, Requirement, Array, NilClass))
|
2023-09-01 19:37:32 +01:00
|
|
|
}
|
2013-05-06 16:08:50 -05:00
|
|
|
def parse_spec(spec, tags)
|
2023-06-19 06:06:15 +01:00
|
|
|
raise ArgumentError, "Implicit dependencies cannot be manually specified" if tags.include?(:implicit)
|
|
|
|
|
2013-01-26 20:05:39 -06:00
|
|
|
case spec
|
|
|
|
when String
|
2013-05-06 16:08:50 -05:00
|
|
|
parse_string_spec(spec, tags)
|
2013-09-28 16:37:05 -05:00
|
|
|
when Resource
|
|
|
|
resource_dep(spec, tags)
|
2013-04-17 14:05:47 -05:00
|
|
|
when Symbol
|
2013-05-06 16:08:50 -05:00
|
|
|
parse_symbol_spec(spec, tags)
|
2013-05-06 16:08:49 -05:00
|
|
|
when Requirement, Dependency
|
2013-01-26 20:05:39 -06:00
|
|
|
spec
|
|
|
|
when Class
|
2013-05-06 16:08:50 -05:00
|
|
|
parse_class_spec(spec, tags)
|
2013-01-26 20:05:39 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-06 16:08:50 -05:00
|
|
|
def parse_string_spec(spec, tags)
|
2023-06-19 04:37:55 +01:00
|
|
|
Dependency.new(spec, tags)
|
2013-05-06 16:08:49 -05:00
|
|
|
end
|
|
|
|
|
2013-05-06 16:08:50 -05:00
|
|
|
def parse_symbol_spec(spec, tags)
|
2023-02-14 02:03:58 +00:00
|
|
|
# When modifying this list of supported requirements, consider
|
|
|
|
# whether Formulary::API_SUPPORTED_REQUIREMENTS should also be changed.
|
2013-01-26 20:05:39 -06:00
|
|
|
case spec
|
2018-10-19 16:38:41 +01:00
|
|
|
when :arch then ArchRequirement.new(tags)
|
|
|
|
when :codesign then CodesignRequirement.new(tags)
|
|
|
|
when :linux then LinuxRequirement.new(tags)
|
|
|
|
when :macos then MacOSRequirement.new(tags)
|
2019-08-15 07:53:28 +02:00
|
|
|
when :maximum_macos then MacOSRequirement.new(tags, comparator: "<=")
|
2018-10-19 16:38:41 +01:00
|
|
|
when :xcode then XcodeRequirement.new(tags)
|
2013-01-26 20:05:39 -06:00
|
|
|
else
|
2014-03-22 10:31:00 +00:00
|
|
|
raise ArgumentError, "Unsupported special dependency #{spec.inspect}"
|
2013-01-26 20:05:39 -06:00
|
|
|
end
|
|
|
|
end
|
2013-02-10 19:25:22 -06:00
|
|
|
|
2013-05-06 16:08:50 -05:00
|
|
|
def parse_class_spec(spec, tags)
|
2019-02-19 13:11:32 +00:00
|
|
|
raise TypeError, "#{spec.inspect} is not a Requirement subclass" unless spec < Requirement
|
2016-09-23 22:02:23 +02:00
|
|
|
|
|
|
|
spec.new(tags)
|
2013-05-06 16:08:49 -05:00
|
|
|
end
|
|
|
|
|
2013-09-28 16:37:05 -05:00
|
|
|
def resource_dep(spec, tags)
|
2020-03-04 21:52:46 +01:00
|
|
|
tags << :build << :test
|
2013-09-28 16:37:05 -05:00
|
|
|
strategy = spec.download_strategy
|
|
|
|
|
2021-07-26 12:39:25 +02:00
|
|
|
if strategy <= HomebrewCurlDownloadStrategy
|
2023-10-20 12:21:01 -04:00
|
|
|
[curl_dep_if_needed(tags), parse_url_spec(spec.url, tags)]
|
2023-05-17 16:30:00 -05:00
|
|
|
elsif strategy <= NoUnzipCurlDownloadStrategy
|
|
|
|
# ensure NoUnzip never adds any dependencies
|
2021-07-26 12:39:25 +02:00
|
|
|
elsif strategy <= CurlDownloadStrategy
|
2013-09-28 16:37:05 -05:00
|
|
|
parse_url_spec(spec.url, tags)
|
2016-09-17 15:17:27 +01:00
|
|
|
elsif strategy <= GitDownloadStrategy
|
2018-01-10 13:19:45 +00:00
|
|
|
git_dep_if_needed(tags)
|
2017-12-23 16:38:06 +00:00
|
|
|
elsif strategy <= SubversionDownloadStrategy
|
2018-01-10 13:19:45 +00:00
|
|
|
subversion_dep_if_needed(tags)
|
2016-09-17 15:17:27 +01:00
|
|
|
elsif strategy <= MercurialDownloadStrategy
|
2023-06-19 06:06:15 +01:00
|
|
|
Dependency.new("mercurial", [*tags, :implicit])
|
2016-09-17 15:17:27 +01:00
|
|
|
elsif strategy <= FossilDownloadStrategy
|
2023-06-19 06:06:15 +01:00
|
|
|
Dependency.new("fossil", [*tags, :implicit])
|
2016-09-17 15:17:27 +01:00
|
|
|
elsif strategy <= BazaarDownloadStrategy
|
2023-06-19 06:06:15 +01:00
|
|
|
Dependency.new("breezy", [*tags, :implicit])
|
2016-09-17 15:17:27 +01:00
|
|
|
elsif strategy <= CVSDownloadStrategy
|
2017-12-23 16:38:06 +00:00
|
|
|
cvs_dep_if_needed(tags)
|
2016-09-17 15:17:27 +01:00
|
|
|
elsif strategy < AbstractDownloadStrategy
|
2013-09-28 16:37:05 -05:00
|
|
|
# allow unknown strategies to pass through
|
|
|
|
else
|
2023-05-17 16:30:00 -05:00
|
|
|
raise TypeError, "#{strategy.inspect} is not an AbstractDownloadStrategy subclass"
|
2013-09-28 16:37:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_url_spec(url, tags)
|
|
|
|
case File.extname(url)
|
2017-12-23 16:36:33 +00:00
|
|
|
when ".xz" then xz_dep_if_needed(tags)
|
2021-09-16 15:56:31 +01:00
|
|
|
when ".zst" then zstd_dep_if_needed(tags)
|
2018-03-04 11:51:06 -08:00
|
|
|
when ".zip" then unzip_dep_if_needed(tags)
|
2018-02-19 09:54:36 +00:00
|
|
|
when ".bz2" then bzip2_dep_if_needed(tags)
|
2023-06-19 06:06:15 +01:00
|
|
|
when ".lha", ".lzh" then Dependency.new("lha", [*tags, :implicit])
|
|
|
|
when ".lz" then Dependency.new("lzip", [*tags, :implicit])
|
|
|
|
when ".rar" then Dependency.new("libarchive", [*tags, :implicit])
|
|
|
|
when ".7z" then Dependency.new("p7zip", [*tags, :implicit])
|
2013-09-28 16:37:05 -05:00
|
|
|
end
|
|
|
|
end
|
2013-01-26 20:05:39 -06:00
|
|
|
end
|
2016-09-18 16:23:36 -04:00
|
|
|
|
|
|
|
require "extend/os/dependency_collector"
|