1
      1
 ?9    #  Phusion Passenger - https://www.phusionpassenger.com/
#  Copyright (c) 2010-2018 Phusion Holding B.V.
#
#  "Passenger", "Phusion Passenger" and "Union Station" are registered
#  trademarks of Phusion Holding B.V.
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#  THE SOFTWARE.

PhusionPassenger.require_passenger_lib 'constants'

module PhusionPassenger

  class NativeSupportLoader
    def self.supported?
      return !defined?(RUBY_ENGINE) || RUBY_ENGINE == "ruby" || RUBY_ENGINE == "rbx"
    end

    def try_load
      if defined?(NativeSupport)
        return true
      else
        load_from_native_support_output_dir ||
        load_from_buildout_dir ||
        load_from_load_path ||
        load_from_home_dir
      end
    end

    def start
      if ENV['PASSENGER_USE_RUBY_NATIVE_SUPPORT'] == '0'
        STDERR.puts " [#{library_name}] will not be used (PASSENGER_USE_RUBY_NATIVE_SUPPORT=0)"
        STDERR.puts "  --> Passenger will still operate normally."
        return false
      elsif try_load
        return true
      elsif compile_and_load || download_binary_and_load
        STDERR.puts " [#{library_name}] successfully loaded."
        return true
      else
        STDERR.puts " [#{library_name}] will not be used (can't compile or download) "
        STDERR.puts "  --> Passenger will still operate normally."
        return false
      end
    end

  private
    def archdir
      @archdir ||= begin
        PhusionPassenger.require_passenger_lib 'platform_info/binary_compatibility'
        PlatformInfo.ruby_extension_binary_compatibility_id
      end
    end

    def libext
      @libext ||= begin
        PhusionPassenger.require_passenger_lib 'platform_info/operating_system'
        PlatformInfo.library_extension
      end
    end

    def library_name
      return "passenger_native_support.#{libext}"
    end

    def extconf_rb
      File.join(PhusionPassenger.ruby_extension_source_dir, "extconf.rb")
    end

    def load_from_native_support_output_dir
      # Quick workaround for people suffering from
      # https://code.google.com/p/phusion-passenger/issues/detail?id=865
      output_dir = ENV['PASSENGER_NATIVE_SUPPORT_OUTPUT_DIR']
      if output_dir && !output_dir.empty?
        begin
          return load_native_extension("#{output_dir}/#{VERSION_STRING}/#{archdir}/#{library_name}")
        rescue LoadError
          return false
        end
      else
        return false
      end
    end

    def load_from_buildout_dir
      if PhusionPassenger.build_system_dir
        begin
          return load_native_extension("#{PhusionPassenger.build_system_dir}/buildout/ruby/#{archdir}/#{library_name}")
        rescue LoadError
          return false
        end
      else
        return false
      end
    end

    def load_from_load_path
      return load_native_extension('passenger_native_support')
    rescue LoadError
      return false
    end

    def load_from_home_dir
      begin
        return load_native_extension("#{PhusionPassenger.home_dir}/#{USER_NAMESPACE_DIRNAME}/native_support/#{VERS