Class: Recipe

Inherits:
Rake::Task show all
Includes:
Rake::DSL
Defined in:
lib/rake-plus/recipe.rb

Constant Summary collapse

BUILD_SYSTEM =
`uname -sm`.strip.gsub(' ', '-').downcase
@@defaults =
{}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rake::Task

#file_missing?, #out_of_date?

Class Method Details

.var(name, &default) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rake-plus/recipe.rb', line 11

def var(name, &default)
  if block_given?
    @@defaults[name.to_s] = default

    class_eval <<-CODE
    def #{name}(val=nil)
      if !val.nil?
        @#{name} = val
      elsif @#{name}.nil?
        @#{name} = instance_eval(&@@defaults["#{name}"])
      end
      @#{name}
    end
    CODE
  else
    class_eval <<-CODE
    def #{name}(val=nil)
      @#{name} = val unless val.nil?
      @#{name}
    end
    CODE
  end
  self
end

Instance Method Details

#bdeps(val = nil) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/rake-plus/recipe.rb', line 60

def bdeps(val=nil)
  unless val.nil?
    val = [val] unless val.kind_of? Array
    @bdeps = val.map{|x| x.kind_of?(Recipe) ? x : recipe(x)}
  end
  @bdeps || []
end

#define(&block) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/rake-plus/recipe.rb', line 144

def define(&block)
  raise "Already defined" if @defined
  instance_eval(&block)
  raise "Missing @url or @git" unless url_or_git

  directory build_dir
  directory src_dir

  patches.each{|p| file(p) }


  file(build_dir / "src.done" => [url_or_git, build_dir, src_dir] + patches) do |t|
    sh "rm -rf #{src_dir}/*"
    url_or_git.unpack_to src_dir

    # Apply patches
    Dir.chdir(src_dir / unpack_dir) do
      patches.each do |p|
        sh "patch -b -f -N -p0 < #{RakePlus.top / p}"
      end
    end

    touch t.name
  end

  self.class.define_task(self.name.sub(/.*:/,'') => bdeps + [build_dir / "src.done"] + Dir[src_dir/'**/*'] )

  @defined = true
  self
end

#execute(args = nil) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/rake-plus/recipe.rb', line 175

def execute(args=nil)
  raise "Not defined" unless @defined

  # Make build-dependencies available
  if bdeps.any?
    rm_rf dep_dir
    mkdir_p dep_dir
    bdeps.each{|dep| recipe(dep).install_to(dep_dir) }
  end

  with_env(
    :LDFLAGS => "-L#{dep_dir}/lib",
    :CFLAGS  => "-I#{dep_dir}/include",
    :PKG_CONFIG_PATH => "#{dep_dir}/lib/pkgconfig",
    :PATH    => "#{dep_dir}/bin:#{ENV['PATH']}"
  ) do
    Dir.chdir(src_dir / unpack_dir, &install)
  end

  # remove unused stuff
  rm_rf prefix / "man"
  rm_rf prefix / "share" / "man"
  sh "find #{prefix} -type d -empty -delete"

  # done
  touch build_dir / "install.done"
  super
end

#flags_to_features(*flags) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/rake-plus/recipe.rb', line 129

def flags_to_features(*flags)
  flags.flatten.map do |x|
    case x
    when /^\+/
      x.gsub(/^\+/,'--enable-')
    when /^-/
      x.gsub(/^-/,'--disable-')
    when /^\/\//, /^\s*$/
      nil
    else
      raise ArgumentError, "unknown flag: #{x}"
    end
  end.compact.join(' ')
end

#git(val = nil) ⇒ Object



84
85
86
87
88
89
# File 'lib/rake-plus/recipe.rb', line 84

def git(val=nil)
  unless val.nil?
    @git = git_dep(val)
  end
  @git
end

#install(&block) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/rake-plus/recipe.rb', line 68

def install(&block)
  @install ||= block
  @install ||= proc do
    mkdir_p(prefix)
    sh "./configure --prefix=#{prefix}#{static_install ? " --enable-static --disable-shared" : ''}"
    sh "make"
    sh "make install"
  end
  @install
end

#install_to(dir) ⇒ Object



105
106
107
# File 'lib/rake-plus/recipe.rb', line 105

def install_to(dir)
  sh "cp -r #{prefix}/* #{dir}"
end

#needed?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/rake-plus/recipe.rb', line 109

def needed?
  file_missing?(build_dir / "install.done") || out_of_date?(timestamp)
end

#opts_to_string(opts) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/rake-plus/recipe.rb', line 121

def opts_to_string(opts)
  ary = []
  opts.each_pair do |k,v|
    ary << "--#{k}=#{v}"
  end
  ary.join(' ')
end

#patch(val) ⇒ Object



101
102
103
# File 'lib/rake-plus/recipe.rb', line 101

def patch(val)
  patches.push(val)
end

#patchesObject



98
99
100
# File 'lib/rake-plus/recipe.rb', line 98

def patches
  @patches ||= []
end

#svn(val = nil) ⇒ Object



91
92
93
94
95
96
# File 'lib/rake-plus/recipe.rb', line 91

def svn(val=nil)
  unless val.nil?
    @svn = svn_dep(val)
  end
  @svn
end

#timestampObject



113
114
115
116
117
118
119
# File 'lib/rake-plus/recipe.rb', line 113

def timestamp
  if File.exist?(build_dir / "install.done")
    File.mtime(build_dir / "install.done")
  else
    Rake::EARLY
  end
end

#url(val = nil) ⇒ Object



78
79
80
81
82
83
# File 'lib/rake-plus/recipe.rb', line 78

def url(val=nil)
  unless val.nil?
    @url = remote_package(val)
  end
  @url
end

#url_or_gitObject

Variables for the DSL



38
39
40
# File 'lib/rake-plus/recipe.rb', line 38

def url_or_git
  @url || @git || @svn
end