brew/Library/Homebrew/dependency_collector.rb

142 lines
4.1 KiB
Ruby
Raw Normal View History

require 'dependency'
require 'dependencies'
require 'requirement'
require 'requirements'
require 'requirements/ld64_dependency'
require 'set'
## 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
# Define the languages that we can handle as external dependencies.
LANGUAGE_MODULES = Set[
Python module deps, ext. python fix and 10.6 fix * Fixes Homebrew/homebrew#20572 by tweaking the logic that decides which python is used by the `python` object inside a formula. There was a bug when on 10.6 there is no Python 2.7 but a :recommended Python was still treated as being available. * Use the user's PATH when looking for an external Python. Until now only brewed or OS X system's python have been found by `depends_on :python`. But now we support any Python in PATH (e.g. pyenv's python). * Further, instead of handling python modules and import tests in LanguageModuleDependency, these are now handled by: depends_on :python => 'numpy' # for example The old style depends_on 'numpy' => :python is still supported and is only an alias for the newer style (only for :python, the other languages are not altered by this commit). The reasoning is that if a formula requires a python module, it basically also needs python itself - and further that specific version of python has to provide the module. So the `PythonInstalled` is the natural place to check for the availability of a python module. Using a python module and other tags like :optional or :recommended is done like so: depends_on :python => [:optional, 'numpy'] Specifying another PyPi (Python Package index) name than the module import name is seldom used but supported, too: depends_on :python => ['enchant'=>'pyenchant'] A last note: For clarity, you can define multiple depends_on statements with different modules to be importable.`
2013-06-17 09:06:38 +02:00
:chicken, :jruby, :lua, :node, :ocaml, :perl, :python, :rbx, :ruby
].freeze
attr_reader :deps, :requirements
def initialize
@deps = Dependencies.new
@requirements = ComparableSet.new
end
def add(spec)
case dep = build(spec)
when Dependency
@deps << dep
when Requirement
@requirements << dep
end
dep
end
def build(spec)
spec, tags = case spec
when Hash then destructure_spec_hash(spec)
else spec
end
parse_spec(spec, Array(tags))
end
private
def destructure_spec_hash(spec)
spec.each { |o| return o }
end
def parse_spec(spec, tags)
case spec
when String
parse_string_spec(spec, tags)
2013-04-17 14:05:47 -05:00
when Symbol
parse_symbol_spec(spec, tags)
when Requirement, Dependency
spec
when Class
parse_class_spec(spec, tags)
else
raise TypeError, "Unsupported type #{spec.class} for #{spec.inspect}"
end
end
def parse_string_spec(spec, tags)
if tags.empty?
Dependency.new(spec, tags)
elsif (tag = tags.first) && LANGUAGE_MODULES.include?(tag)
Python module deps, ext. python fix and 10.6 fix * Fixes Homebrew/homebrew#20572 by tweaking the logic that decides which python is used by the `python` object inside a formula. There was a bug when on 10.6 there is no Python 2.7 but a :recommended Python was still treated as being available. * Use the user's PATH when looking for an external Python. Until now only brewed or OS X system's python have been found by `depends_on :python`. But now we support any Python in PATH (e.g. pyenv's python). * Further, instead of handling python modules and import tests in LanguageModuleDependency, these are now handled by: depends_on :python => 'numpy' # for example The old style depends_on 'numpy' => :python is still supported and is only an alias for the newer style (only for :python, the other languages are not altered by this commit). The reasoning is that if a formula requires a python module, it basically also needs python itself - and further that specific version of python has to provide the module. So the `PythonInstalled` is the natural place to check for the availability of a python module. Using a python module and other tags like :optional or :recommended is done like so: depends_on :python => [:optional, 'numpy'] Specifying another PyPi (Python Package index) name than the module import name is seldom used but supported, too: depends_on :python => ['enchant'=>'pyenchant'] A last note: For clarity, you can define multiple depends_on statements with different modules to be importable.`
2013-06-17 09:06:38 +02:00
# Next line only for legacy support of `depends_on 'module' => :python`
# It should be replaced by `depends_on :python => 'module'`
return PythonDependency.new("2", spec) if tag == :python
LanguageModuleDependency.new(tag, spec)
else
Dependency.new(spec, tags)
end
end
def parse_symbol_spec(spec, tags)
case spec
when :autoconf, :automake, :bsdmake, :libtool, :libltdl
# Xcode no longer provides autotools or some other build tools
autotools_dep(spec, tags)
when :x11 then X11Dependency.new(spec.to_s, tags)
when *X11Dependency::Proxy::PACKAGES
x11_dep(spec, tags)
when :cairo, :pixman
# We no longer use X11 psuedo-deps for cairo or pixman,
# so just return a standard formula dependency.
Dependency.new(spec.to_s, tags)
when :xcode then XcodeDependency.new(tags)
when :macos then MinimumMacOSRequirement.new(tags)
when :mysql then MysqlDependency.new(tags)
when :postgresql then PostgresqlDependency.new(tags)
when :fortran then FortranDependency.new(tags)
when :mpi then MPIDependency.new(*tags)
when :tex then TeXDependency.new(tags)
when :clt then CLTDependency.new(tags)
when :arch then ArchRequirement.new(tags)
when :hg then MercurialDependency.new(tags)
when :python, :python2 then PythonDependency.new("2", tags)
when :python3 then PythonDependency.new("3", tags)
# Tiger's ld is too old to properly link some software
when :ld64 then LD64Dependency.new if MacOS.version < :leopard
else
raise "Unsupported special dependency #{spec.inspect}"
end
end
def parse_class_spec(spec, tags)
if spec < Requirement
spec.new(tags)
else
raise TypeError, "#{spec.inspect} is not a Requirement subclass"
end
end
def x11_dep(spec, tags)
if MacOS.version >= :mountain_lion
Dependency.new(spec.to_s, tags)
else
X11Dependency::Proxy.for(spec.to_s, tags)
end
end
def autotools_dep(spec, tags)
unless MacOS::Xcode.provides_autotools?
case spec
when :libltdl then spec = :libtool
else tags << :build
end
Dependency.new(spec.to_s, tags)
end
end
end