summaryrefslogtreecommitdiff
path: root/lib/rake/baseextensiontask.rb
blob: 7b294adabf3e29be565073478aaa1292ef9ad310 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require 'rake'
require 'rake/clean'
require 'rake/tasklib'
require 'rbconfig'

require 'pathname'

require_relative "compiler_config"

module Rake
  class BaseExtensionTask < TaskLib

    attr_accessor :name
    attr_accessor :gem_spec
    attr_accessor :tmp_dir
    attr_accessor :ext_dir
    attr_accessor :lib_dir
    attr_accessor :config_options
    attr_accessor :source_pattern
    attr_accessor :extra_options
    attr_accessor :extra_sources
    attr_writer :platform

    def platform
      @platform ||= RUBY_PLATFORM
    end

    def initialize(name = nil, gem_spec = nil)
      init(name, gem_spec)
      yield self if block_given?
      define
    end

    def init(name = nil, gem_spec = nil)
      @name = name
      @gem_spec = gem_spec
      @tmp_dir = 'tmp'
      @ext_dir = "ext/#{@name}"
      @lib_dir = 'lib'
      if @name and File.dirname(@name.to_s) != "."
        @lib_dir += "/#{File.dirname(@name.to_s)}"
      end
      @config_options = []
      @extra_options = ARGV.select { |i| i =~ /\A--?/ }
      @extra_sources = FileList[]
    end

    def define
      fail "Extension name must be provided." if @name.nil?
      @name = @name.to_s

      define_compile_tasks
    end

    private

    def define_compile_tasks
      raise NotImplementedError
    end

    def binary(platform = nil)
      ext = case platform
        when /darwin/
          'bundle'
        when /mingw|mswin|linux/
          'so'
        when /java/
          'jar'
        else
          RbConfig::CONFIG['DLEXT']
      end
      "#{@name}.#{ext}"
    end

    def source_files
      FileList["#{@ext_dir}/#{@source_pattern}"] + @extra_sources
    end

    def warn_once(message)
      @@already_warned ||= false
      return if @@already_warned
      @@already_warned = true
      warn message
    end

    def windows?
      Rake.application.windows?
    end
  end
end