brew/Library/Homebrew/software_spec.rb

288 lines
7.7 KiB
Ruby
Raw Normal View History

rubocop: Use `Sorbet/StrictSigil` as it's better than comments - Previously I thought that comments were fine to discourage people from wasting their time trying to bump things that used `undef` that Sorbet didn't support. But RuboCop is better at this since it'll complain if the comments are unnecessary. - Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501. - I've gone for a mixture of `rubocop:disable` for the files that can't be `typed: strict` (use of undef, required before everything else, etc) and `rubocop:todo` for everything else that should be tried to make strictly typed. There's no functional difference between the two as `rubocop:todo` is `rubocop:disable` with a different name. - And I entirely disabled the cop for the docs/ directory since `typed: strict` isn't going to gain us anything for some Markdown linting config files. - This means that now it's easier to track what needs to be done rather than relying on checklists of files in our big Sorbet issue: ```shell $ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l 268 ``` - And this is confirmed working for new files: ```shell $ git status On branch use-rubocop-for-sorbet-strict-sigils Untracked files: (use "git add <file>..." to include in what will be committed) Library/Homebrew/bad.rb Library/Homebrew/good.rb nothing added to commit but untracked files present (use "git add" to track) $ brew style Offenses: bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true. ^^^^^^^^^^^^^ 1340 files inspected, 1 offense detected ```
2024-08-12 10:30:59 +01:00
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
require "resource"
require "download_strategy"
require "checksum"
require "version"
require "options"
require "build_options"
require "dependency_collector"
2016-04-25 17:57:51 +01:00
require "utils/bottles"
require "patch"
require "compilers"
require "macos_version"
require "extend/on_system"
2013-09-14 10:16:52 -05:00
2024-07-14 21:03:08 -04:00
class SoftwareSpec
include Downloadable
extend Forwardable
include OnSystem::MacOSAndLinux
2013-09-14 10:16:52 -05:00
PREDEFINED_OPTIONS = {
universal: Option.new("universal", "Build a universal binary"),
cxx11: Option.new("c++11", "Build using C++11 mode"),
}.freeze
2020-07-07 11:29:33 +01:00
attr_reader :name, :full_name, :owner, :build, :resources, :patches, :options, :deprecated_flags,
:deprecated_options, :dependency_collector, :bottle_specification, :compiler_failures
2013-09-17 21:25:39 -05:00
2020-07-07 11:29:33 +01:00
def_delegators :@resource, :stage, :fetch, :verify_download_integrity, :source_modified_time, :download_name,
:cached_download, :clear_cache, :checksum, :mirrors, :specs, :using, :version, :mirror,
2020-11-24 15:46:47 +01:00
:downloader
def_delegators :@resource, :sha256
2013-09-14 10:16:52 -05:00
def initialize(flags: [])
2024-07-14 11:42:22 -04:00
super()
# Ensure this is synced with `initialize_dup` and `freeze` (excluding simple objects like integers and booleans)
2024-07-14 11:42:22 -04:00
@resource = Resource::Formula.new
2013-09-17 21:25:39 -05:00
@resources = {}
2013-09-21 19:27:24 -05:00
@dependency_collector = DependencyCollector.new
@bottle_specification = BottleSpecification.new
@patches = []
@options = Options.new
@flags = flags
@deprecated_flags = []
@deprecated_options = []
@build = BuildOptions.new(Options.create(@flags), options)
@compiler_failures = []
2013-09-17 21:25:39 -05:00
end
def initialize_dup(other)
super
@resource = @resource.dup
@resources = @resources.dup
@dependency_collector = @dependency_collector.dup
@bottle_specification = @bottle_specification.dup
@patches = @patches.dup
@options = @options.dup
@flags = @flags.dup
@deprecated_flags = @deprecated_flags.dup
@deprecated_options = @deprecated_options.dup
@build = @build.dup
@compiler_failures = @compiler_failures.dup
end
def freeze
@resource.freeze
@resources.freeze
@dependency_collector.freeze
@bottle_specification.freeze
@patches.freeze
@options.freeze
@flags.freeze
@deprecated_flags.freeze
@deprecated_options.freeze
@build.freeze
@compiler_failures.freeze
super
end
2024-07-14 11:42:22 -04:00
sig { override.returns(String) }
def download_type
"formula"
end
def owner=(owner)
2013-09-23 21:39:19 -05:00
@name = owner.name
2015-05-27 22:15:35 +08:00
@full_name = owner.full_name
@bottle_specification.tap = owner.tap
2014-02-27 14:50:22 -06:00
@owner = owner
@resource.owner = self
resources.each_value do |r|
r.owner = self
next if r.version
next if version.nil?
r.version(version.head? ? Version.new("HEAD") : version.dup)
end
patches.each { |p| p.owner = self }
2013-09-17 21:25:39 -05:00
end
sig { override.params(val: T.nilable(String), specs: T::Hash[Symbol, T.anything]).returns(T.nilable(String)) }
def url(val = nil, specs = {})
if val
@resource.url(val, **specs)
dependency_collector.add(@resource)
end
@resource.url
end
2015-11-01 20:33:24 +08:00
def bottle_defined?
!bottle_specification.collector.tags.empty?
2015-11-01 20:33:24 +08:00
end
def bottle_tag?(tag = nil)
bottle_specification.tag?(Utils::Bottles.tag(tag))
end
def bottled?(tag = nil)
2023-04-04 15:37:24 +01:00
bottle_tag?(tag) &&
(tag.present? || bottle_specification.compatible_locations? || owner.force_bottle)
end
def bottle(&block)
bottle_specification.instance_eval(&block)
end
def resource_defined?(name)
resources.key?(name)
end
2024-05-03 21:47:49 +02:00
sig {
params(name: String, klass: T.class_of(Resource), block: T.nilable(T.proc.bind(Resource).void))
.returns(T.nilable(Resource))
}
2024-07-14 11:42:22 -04:00
def resource(name = T.unsafe(nil), klass = Resource, &block)
2020-11-16 22:18:56 +01:00
if block
2024-07-14 11:42:22 -04:00
raise ArgumentError, "Resource must have a name." if name.nil?
raise DuplicateResourceError, name if resource_defined?(name)
2018-09-17 02:45:00 +02:00
res = klass.new(name, &block)
software_spec: do not add empty resources Empty resources are allowed to exist under the following form: resource "filelock" do on_linux do url "https://files.pythonhosted.org/packages/14/ec/6ee2168387ce0154632f856d5cc5592328e9cf93127c5c9aeca92c8c16cb/filelock-3.0.12.tar.gz" sha256 "18d82244ee114f543149c66a6e0c14e9c4f8a1044b5cdaadd0f82159d6a6ff59" end end In this case (or for the on_macos only resource case), just ignore the resource block. Fixes: /usr/local/Homebrew/Library/Homebrew/dependency_collector.rb:148:in `resource_dep' /usr/local/Homebrew/Library/Homebrew/dependency_collector.rb:99:in `parse_spec' /usr/local/Homebrew/Library/Homebrew/dependency_collector.rb:53:in `build' /usr/local/Homebrew/Library/Homebrew/dependency_collector.rb:40:in `block in fetch' /usr/local/Homebrew/Library/Homebrew/dependency_collector.rb:40:in `fetch' /usr/local/Homebrew/Library/Homebrew/dependency_collector.rb:40:in `fetch' /usr/local/Homebrew/Library/Homebrew/dependency_collector.rb:30:in `add' /usr/local/Homebrew/Library/Homebrew/software_spec.rb:120:in `resource' /usr/local/Homebrew/Library/Homebrew/formula.rb:2439:in `block in resource' /usr/local/Homebrew/Library/Homebrew/formula.rb:2438:in `each' /usr/local/Homebrew/Library/Homebrew/formula.rb:2438:in `resource' /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/aws-google-auth.rb:149:in `<class:AwsGoogleAuth>' /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/aws-google-auth.rb:1:in `load_formula' /usr/local/Homebrew/Library/Homebrew/formulary.rb:38:in `rescue in load_formula' /usr/local/Homebrew/Library/Homebrew/formulary.rb:35:in `load_formula' /usr/local/Homebrew/Library/Homebrew/formulary.rb:56:in `load_formula_from_path' /usr/local/Homebrew/Library/Homebrew/formulary.rb:138:in `load_file' /usr/local/Homebrew/Library/Homebrew/formulary.rb:128:in `klass' /usr/local/Homebrew/Library/Homebrew/formulary.rb:124:in `get_formula' /usr/local/Homebrew/Library/Homebrew/formulary.rb:333:in `factory' /usr/local/Homebrew/Library/Homebrew/cli/args.rb:87:in `block in formulae' /usr/local/Homebrew/Library/Homebrew/cli/args.rb:86:in `map' /usr/local/Homebrew/Library/Homebrew/cli/args.rb:86:in `formulae' /usr/local/Homebrew/Library/Homebrew/cmd/install.rb:133:in `install' /usr/local/Homebrew/Library/Homebrew/brew.rb:111:in `<main>'
2020-06-25 22:24:46 +02:00
return unless res.url
resources[name] = res
dependency_collector.add(res)
2024-05-03 21:47:49 +02:00
res
2013-09-17 21:25:39 -05:00
else
2024-07-14 11:42:22 -04:00
return @resource if name.nil?
2013-09-17 21:25:39 -05:00
resources.fetch(name) { raise ResourceMissingError.new(owner, name) }
end
2013-09-14 10:16:52 -05:00
end
2013-09-21 19:27:24 -05:00
def go_resource(name, &block)
odisabled "`SoftwareSpec#go_resource`", "Go modules"
resource name, Resource::Go, &block
end
2014-07-31 19:37:39 -05:00
def option_defined?(name)
options.include?(name)
end
def option(name, description = "")
opt = PREDEFINED_OPTIONS.fetch(name) do
2016-09-20 22:03:08 +02:00
unless name.is_a?(String)
raise ArgumentError, "option name must be string or symbol; got a #{name.class}: #{name}"
end
raise ArgumentError, "option name is required" if name.empty?
2023-04-18 15:06:50 -07:00
raise ArgumentError, "option name must be longer than one character: #{name}" if name.length <= 1
raise ArgumentError, "option name must not start with dashes: #{name}" if name.start_with?("-")
2018-09-17 02:45:00 +02:00
Option.new(name, description)
end
options << opt
2013-09-21 19:27:24 -05:00
end
2013-09-21 19:27:24 -05:00
def deprecated_option(hash)
raise ArgumentError, "deprecated_option hash must not be empty" if hash.empty?
2018-09-17 02:45:00 +02:00
hash.each do |old_options, new_options|
Array(old_options).each do |old_option|
Array(new_options).each do |new_option|
deprecated_option = DeprecatedOption.new(old_option, new_option)
deprecated_options << deprecated_option
old_flag = deprecated_option.old_flag
new_flag = deprecated_option.current_flag
next unless @flags.include? old_flag
2018-09-17 02:45:00 +02:00
@flags -= [old_flag]
@flags |= [new_flag]
@deprecated_flags << deprecated_option
end
end
end
@build = BuildOptions.new(Options.create(@flags), options)
end
def depends_on(spec)
2013-09-21 19:27:24 -05:00
dep = dependency_collector.add(spec)
add_dep_option(dep) if dep
2013-09-21 19:27:24 -05:00
end
sig {
params(
2024-02-13 00:42:54 +01:00
dep: T.any(String, T::Hash[T.any(String, Symbol), T.any(Symbol, T::Array[Symbol])]),
bounds: T::Hash[Symbol, Symbol],
).void
}
2024-02-13 00:42:54 +01:00
def uses_from_macos(dep, bounds = {})
if dep.is_a?(Hash)
bounds = dep.dup
dep, tags = bounds.shift
2024-02-13 00:42:54 +01:00
dep = T.cast(dep, String)
tags = [*tags]
2024-02-13 00:42:54 +01:00
bounds = T.cast(bounds, T::Hash[Symbol, Symbol])
else
tags = []
end
2024-03-07 16:20:20 +00:00
depends_on UsesFromMacOSDependency.new(dep, tags, bounds:)
end
2013-09-21 19:27:24 -05:00
def deps
dependency_collector.deps.dup_without_system_deps
end
def declared_deps
2013-09-21 19:27:24 -05:00
dependency_collector.deps
end
def recursive_dependencies
deps_f = []
recursive_dependencies = deps.filter_map do |dep|
deps_f << dep.to_formula
dep
rescue TapFormulaUnavailableError
# Don't complain about missing cross-tap dependencies
next
end.uniq
deps_f.compact.each do |f|
f.recursive_dependencies.each do |dep|
recursive_dependencies << dep unless recursive_dependencies.include?(dep)
end
end
recursive_dependencies
end
2013-09-21 19:27:24 -05:00
def requirements
dependency_collector.requirements
end
def recursive_requirements
Requirement.expand(self)
end
def patch(strip = :p1, src = nil, &block)
p = Patch.create(strip, src, &block)
2022-07-15 18:25:57 +02:00
return if p.is_a?(ExternalPatch) && p.url.blank?
dependency_collector.add(p.resource) if p.is_a? ExternalPatch
patches << p
end
def fails_with(compiler, &block)
compiler_failures << CompilerFailure.create(compiler, &block)
end
def needs(*standards)
standards.each do |standard|
compiler_failures.concat CompilerFailure.for_standard(standard)
end
end
def add_dep_option(dep)
dep.option_names.each do |name|
if dep.optional? && !option_defined?("with-#{name}")
options << Option.new("with-#{name}", "Build with #{name} support")
elsif dep.recommended? && !option_defined?("without-#{name}")
options << Option.new("without-#{name}", "Build without #{name} support")
end
end
end
2013-09-14 10:16:52 -05:00
end