lib/rake/extensiontask.rb in luislavena-rake-compiler-0.3.1 vs lib/rake/extensiontask.rb in luislavena-rake-compiler-0.4.0

- old
+ new

@@ -5,14 +5,14 @@ require 'rake' require 'rake/clean' require 'rake/tasklib' require 'rbconfig' +require 'yaml' module Rake autoload :GemPackageTask, 'rake/gempackagetask' - autoload :YAML, 'yaml' class ExtensionTask < TaskLib attr_accessor :name attr_accessor :gem_spec attr_accessor :config_script @@ -35,11 +35,11 @@ def init(name = nil, gem_spec = nil) @name = name @gem_spec = gem_spec @config_script = 'extconf.rb' @tmp_dir = 'tmp' - @ext_dir = 'ext' + @ext_dir = "ext/#{@name}" @lib_dir = 'lib' @source_pattern = "*.c" @config_options = [] @cross_compile = false @cross_config_options = [] @@ -100,21 +100,34 @@ # makefile depends of tmp_dir and config_script # tmp/extension_name/Makefile file "#{tmp_path}/Makefile" => [tmp_path, extconf] do |t| options = @config_options.dup + # include current directory + cmd = ['-I.'] + + # if fake.rb is present, add to the command line + if t.prerequisites.include?("#{tmp_path}/fake.rb") then + cmd << '-rfake' + end + + # now add the extconf script + cmd << File.join(Dir.pwd, extconf) + # rbconfig.rb will be present if we are cross compiling if t.prerequisites.include?("#{tmp_path}/rbconfig.rb") then options.push(*@cross_config_options) end - parent = Dir.pwd + # add options to command + cmd.push(*options) + chdir tmp_path do # FIXME: Rake is broken for multiple arguments system() calls. # Add current directory to the search path of Ruby # Also, include additional parameters supplied. - ruby ['-I.', File.join(parent, extconf), *options].join(' ') + ruby cmd.join(' ') end end # compile tasks unless Rake::Task.task_defined?('compile') then @@ -126,11 +139,11 @@ unless Rake::Task.task_defined?("compile:#{@name}") then desc "Compile #{@name}" task "compile:#{@name}" end - # Allow segmented compilation by platfrom (open door for 'cross compile') + # Allow segmented compilation by platform (open door for 'cross compile') task "compile:#{@name}:#{platf}" => ["copy:#{@name}:#{platf}"] task "compile:#{platf}" => ["compile:#{@name}:#{platf}"] # Only add this extension to the compile chain if current # platform matches the indicated one. @@ -155,11 +168,11 @@ # FIXME: truly duplicate the Gem::Specification # workaround the lack of #dup for Gem::Specification spec = Gem::Specification.from_yaml(gem_spec.to_yaml) # adjust to specified platform - spec.platform = platf + spec.platform = Gem::Platform.new(platf) # clear the extensions defined in the specs spec.extensions.clear # add the binaries that this task depends on @@ -200,11 +213,11 @@ end end def define_cross_platform_tasks config_path = File.expand_path("~/.rake-compiler/config.yml") - major_ver = (ENV['RUBY_CC_VERSION'] || RUBY_VERSION).match(/(\d+.\d+)/)[1] + ruby_ver = ENV['RUBY_CC_VERSION'] || RUBY_VERSION # warn the user about the need of configuration to use cross compilation. unless File.exist?(config_path) warn "rake-compiler must be configured first to enable cross-compilation" return @@ -213,25 +226,32 @@ config_file = YAML.load_file(config_path) # tmp_path tmp_path = "#{@tmp_dir}/#{cross_platform}/#{@name}" - unless rbconfig_file = config_file["rbconfig-#{major_ver}"] then - fail "no configuration section for this version of Ruby (rbconfig-#{major_ver})" + unless rbconfig_file = config_file["rbconfig-#{ruby_ver}"] then + fail "no configuration section for specified version of Ruby (rbconfig-#{ruby_ver})" end # define compilation tasks for cross platfrom! define_compile_tasks(cross_platform) - # chain rbconfig.rb to Makefile generation - file "#{tmp_path}/Makefile" => ["#{tmp_path}/rbconfig.rb"] + # chain fake.rb and rbconfig.rb to Makefile generation + file "#{tmp_path}/Makefile" => ["#{tmp_path}/fake.rb", "#{tmp_path}/rbconfig.rb"] # copy the file from the cross-ruby location file "#{tmp_path}/rbconfig.rb" => [rbconfig_file] do |t| cp t.prerequisites.first, t.name end + # genearte fake.rb for different ruby versions + file "#{tmp_path}/fake.rb" do |t| + File.open(t.name, 'w') do |f| + f.write fake_rb(ruby_ver) + end + end + # now define native tasks for cross compiled files define_native_tasks(cross_platform) if @gem_spec && @gem_spec.platform == 'ruby' # create cross task task 'cross' do @@ -255,11 +275,11 @@ end end end def extconf - "#{@ext_dir}/#{@name}/#{@config_script}" + "#{@ext_dir}/#{@config_script}" end def make RUBY_PLATFORM =~ /mswin/ ? 'nmake' : 'make' end @@ -275,9 +295,20 @@ end "#{@name}.#{ext}" end def source_files - @source_files ||= FileList["#{@ext_dir}/#{@name}/#{@source_pattern}"] + @source_files ||= FileList["#{@ext_dir}/#{@source_pattern}"] + end + + def fake_rb(version) + <<-FAKE_RB + class Object + remove_const :RUBY_PLATFORM + remove_const :RUBY_VERSION + RUBY_PLATFORM = "i386-mingw32" + RUBY_VERSION = "#{version}" + end +FAKE_RB end end end