brew/Library/Homebrew/build_options.rb
Jack Nagel 046d802d09 FormulaInstaller: allow formulae to pass options to deps
Formulae can now pass build options to dependencies. The following
syntax is supported:

  depends_on 'foo' => 'with-bar'
  depends_on 'foo' => ['with-bar', 'with-baz']

If a dependency is already installed but lacks the required build
options, an exception is raised. Eventually we may be able to just stash
the existing keg and reinstall it with the combined set of used_options
and passed options, but enabling that is left for another day.
2013-01-26 12:14:46 -06:00

89 lines
1.7 KiB
Ruby

require 'options'
# This class holds the build-time options defined for a Formula,
# and provides named access to those options during install.
class BuildOptions
attr_accessor :args
include Enumerable
def initialize args
@args = Array.new(args).extend(HomebrewArgvExtension)
@options = Options.new
end
def add name, description=nil
description ||= case name.to_s
when "universal" then "Build a universal binary"
when "32-bit" then "Build 32-bit only"
end.to_s
@options << Option.new(name, description)
end
def has_option? name
any? { |opt| opt.name == name }
end
def empty?
@options.empty?
end
def each(*args, &block)
@options.each(*args, &block)
end
def as_flags
@options.as_flags
end
def include? name
@args.include? '--' + name
end
def with? name
if has_option? "with-#{name}"
include? "with-#{name}"
elsif has_option? "without-#{name}"
not include? "without-#{name}"
else
false
end
end
def without? name
not with? name
end
def head?
@args.flag? '--HEAD'
end
def devel?
@args.include? '--devel'
end
def stable?
not (head? or devel?)
end
# True if the user requested a universal build.
def universal?
@args.include?('--universal') && has_option?('universal')
end
# Request a 32-bit only build.
# This is needed for some use-cases though we prefer to build Universal
# when a 32-bit version is needed.
def build_32_bit?
@args.include?('--32-bit') && has_option?('32-bit')
end
def used_options
Options.new((as_flags & @args.options_only).map { |o| Option.new(o) })
end
def unused_options
Options.new((as_flags - @args.options_only).map { |o| Option.new(o) })
end
end