Blame SOURCES/operating_system.rb

d64dc0
module Gem
d64dc0
  class << self
d64dc0
d64dc0
    ##
d64dc0
    # Returns full path of previous but one directory of dir in path
d64dc0
    # E.g. for '/usr/share/ruby', 'ruby', it returns '/usr'
d64dc0
d64dc0
    def previous_but_one_dir_to(path, dir)
d64dc0
      return unless path
d64dc0
d64dc0
      split_path = path.split(File::SEPARATOR)
d64dc0
      File.join(split_path.take_while { |one_dir| one_dir !~ /^#{dir}$/ }[0..-2])
d64dc0
    end
d64dc0
    private :previous_but_one_dir_to
d64dc0
d64dc0
    ##
d64dc0
    # Detects --install-dir option specified on command line.
d64dc0
d64dc0
    def opt_install_dir?
d64dc0
      @opt_install_dir ||= ARGV.include?('--install-dir') || ARGV.include?('-i')
d64dc0
    end
d64dc0
    private :opt_install_dir?
d64dc0
d64dc0
    ##
d64dc0
    # Detects --build-root option specified on command line.
d64dc0
d64dc0
    def opt_build_root?
d64dc0
      @opt_build_root ||= ARGV.include?('--build-root')
d64dc0
    end
d64dc0
    private :opt_build_root?
d64dc0
d64dc0
    ##
d64dc0
    # Tries to detect, if arguments and environment variables suggest that
d64dc0
    # 'gem install' is executed from rpmbuild.
d64dc0
d64dc0
    def rpmbuild?
d64dc0
      @rpmbuild ||= ENV['RPM_PACKAGE_NAME'] && (opt_install_dir? || opt_build_root?)
d64dc0
    end
d64dc0
    private :rpmbuild?
d64dc0
d64dc0
    ##
d64dc0
    # Get enabled SCLs in order of (most) dependent SCL to base SCL
d64dc0
d64dc0
    def x_scls
d64dc0
      @x_scls ||= if ENV['X_SCLS'].kind_of?(String)
d64dc0
        ENV['X_SCLS'].split(' ').reverse!
d64dc0
      else
d64dc0
        []
d64dc0
      end
d64dc0
    end
d64dc0
    private :x_scls
d64dc0
d64dc0
    ##
d64dc0
    # Default gems locations allowed on FHS system (/usr, /usr/share).
d64dc0
    # The locations are derived from directories specified during build
d64dc0
    # configuration.
d64dc0
d64dc0
    def default_locations
d64dc0
      @default_locations ||= {
d64dc0
        :system => previous_but_one_dir_to(RbConfig::CONFIG['vendordir'], RbConfig::CONFIG['RUBY_INSTALL_NAME']),
d64dc0
        :local => previous_but_one_dir_to(RbConfig::CONFIG['sitedir'], RbConfig::CONFIG['RUBY_INSTALL_NAME'])
d64dc0
      }
d64dc0
d64dc0
      # Add additional default locations for enabled software collections
d64dc0
      # Dependent scls needs to add themselves on $GEM_PATH
d64dc0
      if ENV['GEM_PATH']
d64dc0
        gem_paths = ENV['GEM_PATH'].split(':')
d64dc0
d64dc0
        x_scls.each do |scl|
d64dc0
          next if scl == '@SCL@'
d64dc0
d64dc0
          regexp = /#{scl}\/root\/usr\/share\/gems/
d64dc0
          scl_gem_path = gem_paths.grep(regexp)[0]
d64dc0
          if scl_gem_path
d64dc0
            prefix = scl_gem_path.gsub(/\A(.*)#{regexp}\z/, "\\1")
d64dc0
            @default_locations["#{scl}_system".to_sym] = "#{prefix}#{scl}/root/usr"
d64dc0
            @default_locations["#{scl}_local".to_sym] = "#{prefix}#{scl}/root/usr/local"
d64dc0
          end
d64dc0
        end
d64dc0
      end
d64dc0
d64dc0
      @default_locations
d64dc0
    end
d64dc0
d64dc0
    ##
d64dc0
    # For each location provides set of directories for binaries (:bin_dir)
d64dc0
    # platform independent (:gem_dir) and dependent (:ext_dir) files.
d64dc0
d64dc0
    def default_dirs
d64dc0
      @libdir ||= case RUBY_PLATFORM
d64dc0
      when 'java'
d64dc0
        RbConfig::CONFIG['datadir']
d64dc0
      else
d64dc0
        RbConfig::CONFIG['libdir']
d64dc0
      end
d64dc0
d64dc0
      @default_dirs ||= default_locations.inject(Hash.new) do |hash, location|
d64dc0
        destination, path = location
d64dc0
d64dc0
        hash[destination] = if path
d64dc0
          {
d64dc0
            :bin_dir => File.join(path, RbConfig::CONFIG['bindir'].split(File::SEPARATOR).last),
d64dc0
            :gem_dir => File.join(path, RbConfig::CONFIG['datadir'].split(File::SEPARATOR).last, 'gems'),
d64dc0
            :ext_dir => File.join(path, @libdir.split(File::SEPARATOR).last, 'gems')
d64dc0
          }
d64dc0
        else
d64dc0
          {
d64dc0
            :bin_dir => '',
d64dc0
            :gem_dir => '',
d64dc0
            :ext_dir => ''
d64dc0
          }
d64dc0
        end
d64dc0
d64dc0
        hash
d64dc0
      end
d64dc0
    end
d64dc0
d64dc0
    ##
d64dc0
    # Remove methods we are going to override. This avoids "method redefined;"
d64dc0
    # warnings otherwise issued by Ruby.
d64dc0
d64dc0
    remove_method :default_dir if method_defined? :default_dir
d64dc0
    remove_method :default_path if method_defined? :default_path
d64dc0
    remove_method :default_bindir if method_defined? :default_bindir
d64dc0
    remove_method :default_ext_dir_for if method_defined? :default_ext_dir_for
d64dc0
d64dc0
    ##
d64dc0
    # RubyGems default overrides.
d64dc0
d64dc0
    def default_dir
d64dc0
      if opt_build_root?
d64dc0
        scl_prefix = x_scls.detect {|c| c != '@SCL@'}
d64dc0
        scl_prefix = scl_prefix ? scl_prefix + '_': nil
d64dc0
d64dc0
        Gem.default_dirs[:"#{scl_prefix}system"][:gem_dir]
d64dc0
      elsif Process.uid == 0
d64dc0
        Gem.default_dirs[:local][:gem_dir]
d64dc0
      else
d64dc0
        Gem.user_dir
d64dc0
      end
d64dc0
    end
d64dc0
d64dc0
    def default_path
d64dc0
      path = default_dirs.collect {|location, paths| paths[:gem_dir]}
d64dc0
      path.unshift Gem.user_dir if File.exist? Gem.user_home
d64dc0
    end
d64dc0
d64dc0
    def default_bindir
d64dc0
      if opt_build_root?
d64dc0
        scl_prefix = x_scls.detect {|c| c != '@SCL@'}
d64dc0
        scl_prefix = scl_prefix ? scl_prefix + '_': nil
d64dc0
d64dc0
        Gem.default_dirs[:"#{scl_prefix}system"][:bin_dir]
d64dc0
      elsif Process.uid == 0
d64dc0
        Gem.default_dirs[:local][:bin_dir]
d64dc0
      else
d64dc0
        File.join [Dir.home, 'bin']
d64dc0
      end
d64dc0
    end
d64dc0
d64dc0
    def default_ext_dir_for base_dir
d64dc0
      dir = if rpmbuild?
d64dc0
        scl_prefix = x_scls.detect {|c| c != '@SCL@' && base_dir =~ /\/#{c}\//}
d64dc0
        scl_prefix = scl_prefix ? scl_prefix + '_': nil
d64dc0
d64dc0
        build_dir = base_dir.chomp Gem.default_dirs[:"#{scl_prefix}system"][:gem_dir]
d64dc0
        if build_dir != base_dir
d64dc0
          File.join build_dir, Gem.default_dirs[:"#{scl_prefix}system"][:ext_dir]
d64dc0
        end
d64dc0
      else
d64dc0
        dirs = Gem.default_dirs.detect {|location, paths| paths[:gem_dir] == base_dir}
d64dc0
        dirs && dirs.last[:ext_dir]
d64dc0
      end
d64dc0
      dir && File.join(dir, RbConfig::CONFIG['RUBY_INSTALL_NAME'])
d64dc0
    end
d64dc0
d64dc0
    # This method should be available since RubyGems 2.2 until RubyGems 3.0.
d64dc0
    # https://github.com/rubygems/rubygems/issues/749
d64dc0
    if method_defined? :install_extension_in_lib
d64dc0
      remove_method :install_extension_in_lib
d64dc0
d64dc0
      def install_extension_in_lib
d64dc0
        false
d64dc0
      end
d64dc0
    end
d64dc0
  end
d64dc0
end