<commit_before>require "hardware"
require "os/mac/version"
require "os/mac/xcode"
require "os/mac/xquartz"
require "os/mac/pathname"
require "os/mac/sdk"

module OS
  module Mac
    extend self

    ::MacOS = self # compatibility

    # This can be compared to numerics, strings, or symbols
    # using the standard Ruby Comparable methods.
    def version
      return @version ||= Version.new("0") unless OS.mac?
      @version ||= Version.new(full_version.to_s[/10\.\d+/])
    end

    # This can be compared to numerics, strings, or symbols
    # using the standard Ruby Comparable methods.
    def full_version
      return @full_version ||= Version.new("0") unless OS.mac?
      @full_version ||= Version.new(`/usr/bin/sw_vers -productVersion`.chomp)
    end

    def prerelease?
      # TODO: bump version when new OS is released
      version >= "10.12"
    end

    def outdated_release?
      # TODO: bump version when new OS is released
      version < "10.9"
    end

    def cat
      version.to_sym
    end

    def locate(tool)
      # Don't call tools (cc, make, strip, etc.) directly!
      # Give the name of the binary you look for as a string to this method
      # in order to get the full path back as a Pathname.
      (@locate ||= {}).fetch(tool) do |key|
        @locate[key] = if File.executable?(path = "/usr/bin/#{tool}")
          Pathname.new path
        # Homebrew GCCs most frequently; much faster to check this before xcrun
        elsif (path = HOMEBREW_PREFIX/"bin/#{tool}").executable?
          path
        elsif OS.mac?
          path = Utils.popen_read("/usr/bin/xcrun", "-no-cache", "-find", tool).chomp
          Pathname.new(path) if File.executable?(path)
        end
      end
    end

    # Locates a (working) copy of install_name_tool, guaranteed to function
    # whether the user has developer tools installed or not.
    def install_name_tool
      if (path = HOMEBREW_PREFIX/"opt/cctools/bin/install_name_tool").executable?
        path
      else
        locate("install_name_tool")
      end
    end

    # Locates a (working) copy of otool, guaranteed to function whether the user
    # has developer tools installed or not.
    def otool
      if (path = HOMEBREW_PREFIX/"opt/cctools/bin/otool").executable?
        path
      else
        locate("otool")
      end
    end

    # Checks if the user has any developer tools installed, either via Xcode
    # or the CLT. Convenient for guarding against formula builds when building
    # is impossible.
    def has_apple_developer_tools?
      Xcode.installed? || CLT.installed?
    end

    def active_developer_dir
      return "" unless OS.mac?
      @active_developer_dir ||= Utils.popen_read("/usr/bin/xcode-select", "-print-path").strip
    end

    # Returns the requested SDK, if installed.
    # If the requested SDK is not installed returns either:
    # a) The newest SDK (if any SDKs are available), or
    # b) nil
    def sdk(v = version)
      return nil unless OS.mac?
      @locator ||= SDKLocator.new
      begin
        @locator.sdk_for v
      rescue SDKLocator::NoSDKError
        sdk = @locator.latest_sdk
        # don't return an SDK that's older than the OS version
        sdk unless sdk.nil? || sdk.version < version
      end
    end

    # Returns the path to an SDK or nil, following the rules set by #sdk.
    def sdk_path(v = version)
      s = sdk(v)
      s.path unless s.nil?
    end

    def default_cc
      cc = locate "cc"
      cc.realpath.basename.to_s rescue nil
    end

    def default_compiler
      case default_cc
      when /^gcc-4.0/ then :gcc_4_0
      when /^gcc/ then :gcc
      when /^llvm/ then :llvm
      when "clang" then :clang
      else
        # guess :(
        if Xcode.version >= "4.3"
          :clang
        elsif Xcode.version >= "4.2"
          :llvm
        else
          :gcc
        end
      end
    end

    def gcc_40_build_version
      @gcc_40_build_version ||=
        if (path = locate("gcc-4.0"))
          `#{path} --version`[/build (\d{4,})/, 1].to_i
        end
    end
    alias_method :gcc_4_0_build_version, :gcc_40_build_version

    def gcc_42_build_version
      @gcc_42_build_version ||=
        begin
          gcc = MacOS.locate("gcc-4.2") || HOMEBREW_PREFIX.join("opt/apple-gcc42/bin/gcc-4.2")
          if gcc.exist? && gcc.realpath.basename.to_s !~ /^llvm/
            `#{gcc} --version`[/build (\d{4,})/, 1].to_i
          end
        end
    end
    alias_method :gcc_build_version, :gcc_42_build_version

    def llvm_build_version
      @llvm_build_version ||=
        if (path = locate("llvm-gcc")) && path.realpath.basename.to_s !~ /^clang/
          `#{path} --version`[/LLVM build (\d{4,})/, 1].to_i
        end
    end

    def clang_version
      @clang_version ||=
        if (path = locate("clang"))
          `#{path} --version`[/(?:clang|LLVM) version (\d\.\d)/, 1]
        end
    end

    def clang_build_version
      @clang_build_version ||=
        if (path = locate("clang"))
          `#{path} --version`[/clang-(\d{2,})/, 1].to_i
        end
    end

    def non_apple_gcc_version(cc)
      (@non_apple_gcc_version ||= {}).fetch(cc) do
        path = HOMEBREW_PREFIX.join("opt", "gcc", "bin", cc)
        path = locate(cc) unless path.exist?
        path = locate(cc.delete("-.")) if OS.linux? && !path
        version = `#{path} --version`[/gcc(?:-\d(?:\.\d)? \(.+\))? (\d\.\d\.\d)/, 1] if path
        @non_apple_gcc_version[cc] = version
      end
    end

    def clear_version_cache
      @gcc_40_build_version = @gcc_42_build_version = @llvm_build_version = nil
      @clang_version = @clang_build_version = nil
      @non_apple_gcc_version = {}
    end

    # See these issues for some history:
    # https://github.com/Homebrew/homebrew/issues/13
    # https://github.com/Homebrew/homebrew/issues/41
    # https://github.com/Homebrew/homebrew/issues/48
    def macports_or_fink
      paths = []

      # First look in the path because MacPorts is relocatable and Fink<commit_msg>Fix non_apple_gcc_version for Linuxbrew<commit_after>require "hardware"
require "os/mac/version"
require "os/mac/xcode"
require "os/mac/xquartz"
require "os/mac/pathname"
require "os/mac/sdk"

module OS
  module Mac
    extend self

    ::MacOS = self # compatibility

    # This can be compared to numerics, strings, or symbols
    # using the standard Ruby Comparable methods.
    def version
      return @version ||= Version.new("0") unless OS.mac?
      @version ||= Version.new(full_version.to_s[/10\.\d+/])
    end

    # This can be compared to numerics, strings, or symbols
    # using the standard Ruby Comparable methods.
    def full_version
      return @full_version ||= Version.new("0") unless OS.mac?
      @full_version ||= Version.new(`/usr/bin/sw_vers -productVersion`.chomp)
    end

    def prerelease?
      # TODO: bump version when new OS is released
      version >= "10.12"
    end

    def outdated_release?
      # TODO: bump version when new OS is released
      version < "10.9"
    end

    def cat
      version.to_sym
    end

    def locate(tool)
      # Don't call tools (cc, make, strip, etc.) directly!
      # Give the name of the binary you look for as a string to this method
      # in order to get the full path back as a Pathname.
      (@locate ||= {}).fetch(tool) do |key|
        @locate[key] = if File.executable?(path = "/usr/bin/#{tool}")
          Pathname.new path
        # Homebrew GCCs most frequently; much faster to check this before xcrun
        elsif (path = HOMEBREW_PREFIX/"bin/#{tool}").executable?
          path
        elsif OS.mac?
          path = Utils.popen_read("/usr/bin/xcrun", "-no-cache", "-find", tool).chomp
          Pathname.new(path) if File.executable?(path)
        end
      end
    end

    # Locates a (working) copy of install_name_tool, guaranteed to function
    # whether the user has developer tools installed or not.
    def install_name_tool
      if (path = HOMEBREW_PREFIX/"opt/cctools/bin/install_name_tool").executable?
        path
      else
        locate("install_name_tool")
      end
    end

    # Locates a (working) copy of otool, guaranteed to function whether the user
    # has developer tools installed or not.
    def otool
      if (path = HOMEBREW_PREFIX/"opt/cctools/bin/otool").executable?
        path
      else
        locate("otool")
      end
    end

    # Checks if the user has any developer tools installed, either via Xcode
    # or the CLT. Convenient for guarding against formula builds when building
    # is impossible.
    def has_apple_developer_tools?
      Xcode.installed? || CLT.installed?
    end

    def active_developer_dir
      return "" unless OS.mac?
      @active_developer_dir ||= Utils.popen_read("/usr/bin/xcode-select", "-print-path").strip
    end

    # Returns the requested SDK, if installed.
    # If the requested SDK is not installed returns either:
    # a) The newest SDK (if any SDKs are available), or
    # b) nil
    def sdk(v = version)
      return nil unless OS.mac?
      @locator ||= SDKLocator.new
      begin
        @locator.sdk_for v
      rescue SDKLocator::NoSDKError
        sdk = @locator.latest_sdk
        # don't return an SDK that's older than the OS version
        sdk unless sdk.nil? || sdk.version < version
      end
    end

    # Returns the path to an SDK or nil, following the rules set by #sdk.
    def sdk_path(v = version)
      s = sdk(v)
      s.path unless s.nil?
    end

    def default_cc
      cc = locate "cc"
      cc.realpath.basename.to_s rescue nil
    end

    def default_compiler
      case default_cc
      when /^gcc-4.0/ then :gcc_4_0
      when /^gcc/ then :gcc
      when /^llvm/ then :llvm
      when "clang" then :clang
      else
        # guess :(
        if Xcode.version >= "4.3"
          :clang
        elsif Xcode.version >= "4.2"
          :llvm
        else
          :gcc
        end
      end
    end

    def gcc_40_build_version
      @gcc_40_build_version ||=
        if (path = locate("gcc-4.0"))
          `#{path} --version`[/build (\d{4,})/, 1].to_i
        end
    end
    alias_method :gcc_4_0_build_version, :gcc_40_build_version

    def gcc_42_build_version
      @gcc_42_build_version ||=
        begin
          gcc = MacOS.locate("gcc-4.2") || HOMEBREW_PREFIX.join("opt/apple-gcc42/bin/gcc-4.2")
          if gcc.exist? && gcc.realpath.basename.to_s !~ /^llvm/
            `#{gcc} --version`[/build (\d{4,})/, 1].to_i
          end
        end
    end
    alias_method :gcc_build_version, :gcc_42_build_version

    def llvm_build_version
      @llvm_build_version ||=
        if (path = locate("llvm-gcc")) && path.realpath.basename.to_s !~ /^clang/
          `#{path} --version`[/LLVM build (\d{4,})/, 1].to_i
        end
    end

    def clang_version
      @clang_version ||=
        if (path = locate("clang"))
          `#{path} --version`[/(?:clang|LLVM) version (\d\.\d)/, 1]
        end
    end

    def clang_build_version
      @clang_build_version ||=
        if (path = locate("clang"))
          `#{path} --version`[/clang-(\d{2,})/, 1].to_i
        end
    end

    def non_apple_gcc_version(cc)
      (@non_apple_gcc_version ||= {}).fetch(cc) do
        path = HOMEBREW_PREFIX.join("opt", "gcc", "bin", cc)
        path = locate(cc) unless path.exist?
        path = locate(cc.delete("-.")) if OS.linux? && !path
        version = `#{path} --version`[/gcc(?:-\d(?:\.\d)?)? \(.+\) (\d\.\d\.\d)/, 1] if path
        @non_apple_gcc_version[cc] = version
      end
    end

    def clear_version_cache
      @gcc_40_build_version = @gcc_42_build_version = @llvm_build_version = nil
      @clang_version = @clang_build_version = nil
      @non_apple_gcc_version = {}
    end

    # See these issues for some history:
    # https://github.com/Homebrew/homebrew/issues/13
    # https://github.com/Homebrew/homebrew/issues/41
    # https://github.com/Homebrew/homebrew/issues/48
    def macports_or_fink
      paths = []

      # First look in the path because MacPorts is relocatable and Fink
{'fraction_non_alphanumeric': 0.11703187250996015, 'fraction_numerical': 0.010126162018592297, 'mean_word_length': 2.2512142471667564, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3680403', 'n_tokens_mistral': 4108, 'n_tokens_neox': 3899, 'n_words': 1076}