C++11 support.
* Add options and ENV method to specify building in C++11 mode.
  - Set C++ compiler flags to enable C++11 mode.
  - To add options to support C++11 mode, a formula can now use
      option :cxx11
    to provide "--c++11" option, and detect and enable C++11 support in
    install method using
      ENV.cxx11 if build.cxx11?
Closes Homebrew/homebrew#22453.
			
			
This commit is contained in:
		
							parent
							
								
									0e6df1c3bf
								
							
						
					
					
						commit
						f2132c47bd
					
				@ -45,13 +45,21 @@ class Cmd
 | 
				
			|||||||
    if @arg0 == 'cpp' or @arg0 == 'ld'
 | 
					    if @arg0 == 'cpp' or @arg0 == 'ld'
 | 
				
			||||||
      @arg0.to_sym
 | 
					      @arg0.to_sym
 | 
				
			||||||
    elsif @args.include? '-c'
 | 
					    elsif @args.include? '-c'
 | 
				
			||||||
 | 
					      if @arg0 =~ /(?:c|g|clang)\+\+/
 | 
				
			||||||
 | 
					        :cxx
 | 
				
			||||||
 | 
					      else
 | 
				
			||||||
        :cc
 | 
					        :cc
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
    elsif @args.include? '-E'
 | 
					    elsif @args.include? '-E'
 | 
				
			||||||
      :ccE
 | 
					      :ccE
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					      if @arg0 =~ /(?:c|g|clang)\+\+/
 | 
				
			||||||
 | 
					        :cxxld
 | 
				
			||||||
      else
 | 
					      else
 | 
				
			||||||
        :ccld
 | 
					        :ccld
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
  def tool
 | 
					  def tool
 | 
				
			||||||
    @tool ||= case @arg0
 | 
					    @tool ||= case @arg0
 | 
				
			||||||
    when 'ld' then 'ld'
 | 
					    when 'ld' then 'ld'
 | 
				
			||||||
@ -83,9 +91,9 @@ class Cmd
 | 
				
			|||||||
      args << "-syslibroot" << $sdkroot
 | 
					      args << "-syslibroot" << $sdkroot
 | 
				
			||||||
    end if nclt?
 | 
					    end if nclt?
 | 
				
			||||||
    allflags = case mode
 | 
					    allflags = case mode
 | 
				
			||||||
    when :ccld
 | 
					    when :ccld, :cxxld
 | 
				
			||||||
      cflags + args + cppflags + ldflags
 | 
					      cflags + args + cppflags + ldflags
 | 
				
			||||||
    when :cc
 | 
					    when :cc, :cxx
 | 
				
			||||||
      cflags + args + cppflags
 | 
					      cflags + args + cppflags
 | 
				
			||||||
    when :ccE
 | 
					    when :ccE
 | 
				
			||||||
      args + cppflags
 | 
					      args + cppflags
 | 
				
			||||||
@ -149,9 +157,15 @@ class Cmd
 | 
				
			|||||||
    args
 | 
					    args
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
  def cflags
 | 
					  def cflags
 | 
				
			||||||
    return [] unless cccfg? 'O'
 | 
					    args = []
 | 
				
			||||||
 | 
					    if mode == :cxx
 | 
				
			||||||
 | 
					      args << '-std=c++11' if cccfg? 'x'
 | 
				
			||||||
 | 
					      args << '-stdlib=libc++' if cccfg? 'g'
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    args = %w{-pipe -w -Os}
 | 
					    return args unless cccfg? 'O'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    args << '-pipe' << '-w' << '-Os'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # When bottling use the oldest supported CPU type.
 | 
					    # When bottling use the oldest supported CPU type.
 | 
				
			||||||
    if cccfg? 'bc'
 | 
					    if cccfg? 'bc'
 | 
				
			||||||
@ -199,6 +213,9 @@ class Cmd
 | 
				
			|||||||
    case mode
 | 
					    case mode
 | 
				
			||||||
    when :ld   then args << '-headerpad_max_install_names'
 | 
					    when :ld   then args << '-headerpad_max_install_names'
 | 
				
			||||||
    when :ccld then args << '-Wl,-headerpad_max_install_names'
 | 
					    when :ccld then args << '-Wl,-headerpad_max_install_names'
 | 
				
			||||||
 | 
					    when :cxxld
 | 
				
			||||||
 | 
					      args << '-Wl,-headerpad_max_install_names'
 | 
				
			||||||
 | 
					      args << '-stdlib=libc++' if cccfg? 'g'
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
    args
 | 
					    args
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
				
			|||||||
@ -7,6 +7,7 @@ class BuildOptions
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  attr_accessor :args
 | 
					  attr_accessor :args
 | 
				
			||||||
  attr_accessor :universal
 | 
					  attr_accessor :universal
 | 
				
			||||||
 | 
					  attr_accessor :cxx11
 | 
				
			||||||
  attr_reader :options
 | 
					  attr_reader :options
 | 
				
			||||||
  protected :options
 | 
					  protected :options
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -25,6 +26,7 @@ class BuildOptions
 | 
				
			|||||||
    description ||= case name.to_s
 | 
					    description ||= case name.to_s
 | 
				
			||||||
      when "universal" then "Build a universal binary"
 | 
					      when "universal" then "Build a universal binary"
 | 
				
			||||||
      when "32-bit" then "Build 32-bit only"
 | 
					      when "32-bit" then "Build 32-bit only"
 | 
				
			||||||
 | 
					      when "c++11" then "Build using C++11 mode"
 | 
				
			||||||
      end.to_s
 | 
					      end.to_s
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @options << Option.new(name, description)
 | 
					    @options << Option.new(name, description)
 | 
				
			||||||
@ -94,6 +96,11 @@ class BuildOptions
 | 
				
			|||||||
    universal || args.include?('--universal') && has_option?('universal')
 | 
					    universal || args.include?('--universal') && has_option?('universal')
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  # True if the user requested to enable C++11 mode.
 | 
				
			||||||
 | 
					  def cxx11?
 | 
				
			||||||
 | 
					    cxx11 || args.include?('--c++11') && has_option?('c++11')
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  # Request a 32-bit only build.
 | 
					  # Request a 32-bit only build.
 | 
				
			||||||
  # This is needed for some use-cases though we prefer to build Universal
 | 
					  # This is needed for some use-cases though we prefer to build Universal
 | 
				
			||||||
  # when a 32-bit version is needed.
 | 
					  # when a 32-bit version is needed.
 | 
				
			||||||
 | 
				
			|||||||
@ -322,6 +322,17 @@ module Stdenv
 | 
				
			|||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def cxx11
 | 
				
			||||||
 | 
					    if compiler == :clang
 | 
				
			||||||
 | 
					      append 'CXX', '-std=c++11'
 | 
				
			||||||
 | 
					      append 'CXX', '-stdlib=libc++'
 | 
				
			||||||
 | 
					    elsif compiler =~ /gcc-4\.(8|9)/
 | 
				
			||||||
 | 
					      append 'CXX', '-std=c++11'
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					      raise "The selected compiler doesn't support C++11: #{compiler}"
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def replace_in_cflags before, after
 | 
					  def replace_in_cflags before, after
 | 
				
			||||||
    CC_FLAG_VARS.each do |key|
 | 
					    CC_FLAG_VARS.each do |key|
 | 
				
			||||||
      self[key] = self[key].sub(before, after) if has_key?(key)
 | 
					      self[key] = self[key].sub(before, after) if has_key?(key)
 | 
				
			||||||
 | 
				
			|||||||
@ -97,6 +97,8 @@ module Superenv
 | 
				
			|||||||
    # A - Installing from a bottle on PPC with Altivec
 | 
					    # A - Installing from a bottle on PPC with Altivec
 | 
				
			||||||
    # O - Enables argument refurbishing. Only active under the
 | 
					    # O - Enables argument refurbishing. Only active under the
 | 
				
			||||||
    #     make/bsdmake wrappers currently.
 | 
					    #     make/bsdmake wrappers currently.
 | 
				
			||||||
 | 
					    # x - Enable C++11 mode.
 | 
				
			||||||
 | 
					    # g - Enable "-stdlib=libc++" for clang.
 | 
				
			||||||
    #
 | 
					    #
 | 
				
			||||||
    # On 10.8 and newer, these flags will also be present:
 | 
					    # On 10.8 and newer, these flags will also be present:
 | 
				
			||||||
    # s - apply fix for sed's Unicode support
 | 
					    # s - apply fix for sed's Unicode support
 | 
				
			||||||
@ -110,6 +112,17 @@ module Superenv
 | 
				
			|||||||
    append 'HOMEBREW_CCCFG', "u", ''
 | 
					    append 'HOMEBREW_CCCFG', "u", ''
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def cxx11
 | 
				
			||||||
 | 
					    if self['HOMEBREW_CC'] == 'clang'
 | 
				
			||||||
 | 
					      append 'HOMEBREW_CCCFG', "x", ''
 | 
				
			||||||
 | 
					      append 'HOMEBREW_CCCFG', "g", ''
 | 
				
			||||||
 | 
					    elsif self['HOMEBREW_CC'] =~ /gcc-4\.(8|9)/
 | 
				
			||||||
 | 
					      append 'HOMEBREW_CCCFG', "x", ''
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					      raise "The selected compiler doesn't support C++11: #{self['HOMEBREW_CC']}"
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  # m32 on superenv does not add any CC flags. It prevents "-m32" from being erased.
 | 
					  # m32 on superenv does not add any CC flags. It prevents "-m32" from being erased.
 | 
				
			||||||
  def m32
 | 
					  def m32
 | 
				
			||||||
    append 'HOMEBREW_CCCFG', "3", ''
 | 
					    append 'HOMEBREW_CCCFG', "3", ''
 | 
				
			||||||
 | 
				
			|||||||
@ -50,6 +50,7 @@ class SoftwareSpec
 | 
				
			|||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def option name, description=nil
 | 
					  def option name, description=nil
 | 
				
			||||||
 | 
					    name = 'c++11' if name == :cxx11
 | 
				
			||||||
    name = name.to_s if Symbol === name
 | 
					    name = name.to_s if Symbol === name
 | 
				
			||||||
    raise "Option name is required." if name.empty?
 | 
					    raise "Option name is required." if name.empty?
 | 
				
			||||||
    raise "Options should not start with dashes." if name[0, 1] == "-"
 | 
					    raise "Options should not start with dashes." if name[0, 1] == "-"
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user