Merge pull request #10815 from Bo98/fi-more-perf

formula_installer: further reduce dependency calculations
This commit is contained in:
Bo Anderson 2021-03-15 18:20:03 +00:00 committed by GitHub
commit acfe9d24d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 121 additions and 60 deletions

View File

@ -288,49 +288,51 @@ class FormulaInstaller
return if ignore_deps? return if ignore_deps?
recursive_deps = formula.recursive_dependencies if Homebrew::EnvConfig.developer?
recursive_formulae = recursive_deps.map(&:to_formula) # `recursive_dependencies` trims cyclic dependencies, so we do one level and take the recursive deps of that.
# Mapping direct dependencies to deeper dependencies in a hash is also useful for the cyclic output below.
recursive_dep_map = formula.deps.to_h { |dep| [dep, dep.to_formula.recursive_dependencies] }
recursive_dependencies = [] cyclic_dependencies = []
invalid_arch_dependencies = [] recursive_dep_map.each do |dep, recursive_deps|
recursive_formulae.each do |dep| if [formula.name, formula.full_name].include?(dep.name)
dep_recursive_dependencies = dep.recursive_dependencies.map(&:to_s) cyclic_dependencies << "#{formula.full_name} depends on itself directly"
if dep_recursive_dependencies.include?(formula.name) elsif recursive_deps.any? { |rdep| [formula.name, formula.full_name].include?(rdep.name) }
recursive_dependencies << "#{formula.full_name} depends on #{dep.full_name}" cyclic_dependencies << "#{formula.full_name} depends on itself via #{dep.name}"
recursive_dependencies << "#{dep.full_name} depends on #{formula.full_name}"
end
if (tab = Tab.for_formula(dep)) && tab.arch.present? && tab.arch.to_s != Hardware::CPU.arch.to_s
invalid_arch_dependencies << "#{dep} was built for #{tab.arch}"
end end
end end
unless recursive_dependencies.empty? if cyclic_dependencies.present?
raise CannotInstallFormulaError, <<~EOS raise CannotInstallFormulaError, <<~EOS
#{formula.full_name} contains a recursive dependency on itself: #{formula.full_name} contains a recursive dependency on itself:
#{recursive_dependencies.join("\n ")} #{cyclic_dependencies.join("\n ")}
EOS EOS
end end
if recursive_formulae.flat_map(&:recursive_dependencies) # Merge into one list
.map(&:to_s) recursive_deps = recursive_dep_map.flat_map { |dep, rdeps| [dep] + rdeps }
.include?(formula.name) Dependency.merge_repeats(recursive_deps)
raise CannotInstallFormulaError, <<~EOS else
#{formula.full_name} contains a recursive dependency on itself! recursive_deps = formula.recursive_dependencies
EOS
end end
unless invalid_arch_dependencies.empty? invalid_arch_dependencies = []
pinned_unsatisfied_deps = []
recursive_deps.each do |dep|
if (tab = Tab.for_formula(dep.to_formula)) && tab.arch.present? && tab.arch.to_s != Hardware::CPU.arch.to_s
invalid_arch_dependencies << "#{dep} was built for #{tab.arch}"
end
pinned_unsatisfied_deps << dep if dep.to_formula.pinned? && !dep.satisfied?(inherited_options_for(dep))
end
if invalid_arch_dependencies.present?
raise CannotInstallFormulaError, <<~EOS raise CannotInstallFormulaError, <<~EOS
#{formula.full_name} dependencies not built for the #{Hardware::CPU.arch} CPU architecture: #{formula.full_name} dependencies not built for the #{Hardware::CPU.arch} CPU architecture:
#{invalid_arch_dependencies.join("\n ")} #{invalid_arch_dependencies.join("\n ")}
EOS EOS
end end
pinned_unsatisfied_deps = recursive_deps.select do |dep|
dep.to_formula.pinned? && !dep.satisfied?(inherited_options_for(dep))
end
return if pinned_unsatisfied_deps.empty? return if pinned_unsatisfied_deps.empty?
raise CannotInstallFormulaError, raise CannotInstallFormulaError,
@ -1153,10 +1155,12 @@ class FormulaInstaller
tab = Tab.for_keg(keg) tab = Tab.for_keg(keg)
unless ignore_deps?
CxxStdlib.check_compatibility( CxxStdlib.check_compatibility(
formula, formula.recursive_dependencies, formula, formula.recursive_dependencies,
Keg.new(formula.prefix), tab.compiler Keg.new(formula.prefix), tab.compiler
) )
end
tab.tap = formula.tap tab.tap = formula.tap
tab.poured_from_bottle = true tab.poured_from_bottle = true

View File

@ -96,8 +96,64 @@ describe FormulaInstaller do
end end
end end
specify "check installation sanity pinned dependency" do describe "#check_install_sanity" do
dep_name = "dependency" it "raises on direct cyclic dependency" do
ENV["HOMEBREW_DEVELOPER"] = "1"
dep_name = "homebrew-test-cyclic"
dep_path = CoreTap.new.formula_dir/"#{dep_name}.rb"
dep_path.write <<~RUBY
class #{Formulary.class_s(dep_name)} < Formula
url "foo"
version "0.1"
depends_on "#{dep_name}"
end
RUBY
Formulary.cache.delete(dep_path)
f = Formulary.factory(dep_name)
fi = described_class.new(f)
expect {
fi.check_install_sanity
}.to raise_error(CannotInstallFormulaError)
end
it "raises on indirect cyclic dependency" do
ENV["HOMEBREW_DEVELOPER"] = "1"
formula1_name = "homebrew-test-formula1"
formula2_name = "homebrew-test-formula2"
formula1_path = CoreTap.new.formula_dir/"#{formula1_name}.rb"
formula1_path.write <<~RUBY
class #{Formulary.class_s(formula1_name)} < Formula
url "foo"
version "0.1"
depends_on "#{formula2_name}"
end
RUBY
Formulary.cache.delete(formula1_path)
formula1 = Formulary.factory(formula1_name)
formula2_path = CoreTap.new.formula_dir/"#{formula2_name}.rb"
formula2_path.write <<~RUBY
class #{Formulary.class_s(formula2_name)} < Formula
url "foo"
version "0.1"
depends_on "#{formula1_name}"
end
RUBY
Formulary.cache.delete(formula2_path)
fi = described_class.new(formula1)
expect {
fi.check_install_sanity
}.to raise_error(CannotInstallFormulaError)
end
it "raises on pinned dependency" do
dep_name = "homebrew-test-dependency"
dep_path = CoreTap.new.formula_dir/"#{dep_name}.rb" dep_path = CoreTap.new.formula_dir/"#{dep_name}.rb"
dep_path.write <<~RUBY dep_path.write <<~RUBY
class #{Formulary.class_s(dep_name)} < Formula class #{Formulary.class_s(dep_name)} < Formula
@ -131,6 +187,7 @@ describe FormulaInstaller do
fi.check_install_sanity fi.check_install_sanity
}.to raise_error(CannotInstallFormulaError) }.to raise_error(CannotInstallFormulaError)
end end
end
specify "install fails with BuildError when a system() call fails" do specify "install fails with BuildError when a system() call fails" do
ENV["HOMEBREW_TEST_NO_EXIT_CLEANUP"] = "1" ENV["HOMEBREW_TEST_NO_EXIT_CLEANUP"] = "1"