root / trunk / Rakefile

Revision 169, 7.8 kB (checked in by stepheneb, 3 months ago)

calculation of REV now works with svn and a git svn clone

Line 
1require 'rake'
2require 'rake/clean'
3require 'rake/gempackagetask'
4require 'rake/rdoctask'
5require 'rake/testtask'
6require 'fileutils'
7include FileUtils
8
9RbConfig = Config unless defined?(RbConfig)
10
11NAME = "hpricot"
12REV = `svn info 2>/dev/null`[/Revision: (\d+)/, 1] || `git svn log --limit=1 --oneline 2>/dev/null`[/^r(\d+)/, 1] rescue nil
13VERS = ENV['VERSION'] || "0.6" + (REV ? ".#{REV}" : "")
14PKG = "#{NAME}-#{VERS}"
15BIN = "*.{bundle,jar,so,obj,pdb,lib,def,exp,class}"
16ARCHLIB = "lib/#{::Config::CONFIG['arch']}"
17CLEAN.include ["ext/hpricot_scan/#{BIN}", "ext/fast_xs/#{BIN}", "lib/**/#{BIN}", ARCHLIB,
18               'ext/fast_xs/Makefile', 'ext/hpricot_scan/Makefile',
19               '**/.*.sw?', '*.gem', '.config', 'pkg']
20RDOC_OPTS = ['--quiet', '--title', 'The Hpricot Reference', '--main', 'README', '--inline-source']
21PKG_FILES = %w(CHANGELOG COPYING README Rakefile) +
22      Dir.glob("{bin,doc,test,lib,extras}/**/*") +
23      Dir.glob("ext/**/*.{h,java,c,rb,rl}") +
24      %w[ext/hpricot_scan/hpricot_scan.c ext/hpricot_scan/HpricotScanService.java] # needed because they are generated later
25RAGEL_C_CODE_GENERATION_STYLES = {
26  "table_driven" => 'T0',
27  "faster_table_driven" => 'T1',
28  "flat_table_driven" => 'F0',
29  "faster_flat_table_driven" => 'F1',
30  "goto_driven" => 'G0',
31  "faster_goto_driven" => 'G1',
32  "really_fast goto_driven" => 'G2'
33  # "n_way_split_really_fast_goto_driven" => 'P<N>'
34}
35DEFAULT_RAGEL_C_CODE_GENERATION = "really_fast goto_driven"
36SPEC =
37  Gem::Specification.new do |s|
38    s.name = NAME
39    s.version = VERS
40    s.platform = Gem::Platform::RUBY
41    s.has_rdoc = true
42    s.rdoc_options += RDOC_OPTS
43    s.extra_rdoc_files = ["README", "CHANGELOG", "COPYING"]
44    s.summary = "a swift, liberal HTML parser with a fantastic library"
45    s.description = s.summary
46    s.author = "why the lucky stiff"
47    s.email = 'why@ruby-lang.org'
48    s.homepage = 'http://code.whytheluckystiff.net/hpricot/'
49    s.files = PKG_FILES
50    s.require_paths = [ARCHLIB, "lib"]
51    s.extensions = FileList["ext/**/extconf.rb"].to_a
52    s.bindir = "bin"
53  end
54
55desc "Does a full compile, test run"
56task :default => [:compile, :test]
57
58desc "Packages up Hpricot."
59task :package => [:clean, :ragel]
60
61desc "Releases packages for all Hpricot packages and platforms."
62task :release => [:package, :package_win32, :package_jruby]
63
64desc "Run all the tests"
65Rake::TestTask.new do |t|
66    t.libs << "test" << ARCHLIB
67    t.test_files = FileList['test/test_*.rb']
68    t.verbose = true
69end
70
71Rake::RDocTask.new do |rdoc|
72    rdoc.rdoc_dir = 'doc/rdoc'
73    rdoc.options += RDOC_OPTS
74    rdoc.main = "README"
75    rdoc.rdoc_files.add ['README', 'CHANGELOG', 'COPYING', 'lib/**/*.rb']
76end
77
78Rake::GemPackageTask.new(SPEC) do |p|
79    p.need_tar = true
80    p.gem_spec = SPEC
81end
82
83['hpricot_scan', 'fast_xs'].each do |extension|
84  ext = "ext/#{extension}"
85  ext_so = "#{ext}/#{extension}.#{Config::CONFIG['DLEXT']}"
86  ext_files = FileList[
87    "#{ext}/*.c",
88    "#{ext}/*.h",
89    "#{ext}/*.rl",
90    "#{ext}/extconf.rb",
91    "#{ext}/Makefile",
92    "lib"
93  ]
94
95  desc "Builds just the #{extension} extension"
96  task extension.to_sym => ["#{ext}/Makefile", ext_so ]
97
98  file "#{ext}/Makefile" => ["#{ext}/extconf.rb"] do
99    Dir.chdir(ext) do ruby "extconf.rb" end
100  end
101
102  file ext_so => ext_files do
103    Dir.chdir(ext) do
104      sh(RUBY_PLATFORM =~ /win32/ ? 'nmake' : 'make')
105    end
106    mkdir_p ARCHLIB
107    cp ext_so, ARCHLIB
108  end
109end
110
111task "lib" do
112  directory "lib"
113end
114
115desc "Compiles the Ruby extension"
116task :compile => [:hpricot_scan, :fast_xs] do
117  if Dir.glob(File.join(ARCHLIB,"hpricot_scan.*")).length == 0
118    STDERR.puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
119    STDERR.puts "Gem actually failed to build.  Your system is"
120    STDERR.puts "NOT configured properly to build hpricot."
121    STDERR.puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
122    exit(1)
123  end
124end
125task :hpricot_scan => [:ragel]
126
127desc "Determines the Ragel version and displays it on the console along with the location of the Ragel binary."
128task :ragel_version do
129  @ragel_v = `ragel -v`[/(version )(\S*)/,2].to_f
130  puts "Using ragel version: #{@ragel_v}, location: #{`which ragel`}"
131  @ragel_v
132end
133
134desc "Generates the C scanner code with Ragel."
135task :ragel => [:ragel_version] do
136  if @ragel_v >= 6.1
137    @ragel_c_code_generation_style = RAGEL_C_CODE_GENERATION_STYLES[DEFAULT_RAGEL_C_CODE_GENERATION]
138    sh %{ragel ext/hpricot_scan/hpricot_scan.rl -#{@ragel_c_code_generation_style} -o ext/hpricot_scan/hpricot_scan.c}
139  else
140    STDERR.puts "Ragel 6.1 or greater is required."
141    exit(1)
142  end
143end
144
145# Java only supports the table-driven code
146# generation style at this point.
147desc "Generates the Java scanner code using the Ragel table-driven code generation style."
148task :ragel_java => [:ragel_version] do
149  if @ragel_v >= 6.1
150    puts "compiling with ragel version #{@ragel_v}"
151    sh %{ragel -J -o ext/hpricot_scan/HpricotScanService.java ext/hpricot_scan/hpricot_scan.java.rl}   
152  else
153    STDERR.puts "Ragel 6.1 or greater is required."
154    exit(1)
155  end
156end
157
158### Win32 Packages ###
159
160Win32Spec = SPEC.dup
161Win32Spec.platform = Gem::Platform::CURRENT
162Win32Spec.files = PKG_FILES + ["#{ARCHLIB}/hpricot_scan.so", "#{ARCHLIB}/fast_xs.so"]
163Win32Spec.extensions = []
164 
165WIN32_PKG_DIR = "#{PKG}-mswin32"
166
167desc "Package up the Win32 distribution."
168file WIN32_PKG_DIR => [:package] do
169  sh "tar zxf pkg/#{PKG}.tgz"
170  mv PKG, WIN32_PKG_DIR
171end
172
173desc "Cross-compile the hpricot_scan extension for win32"
174file "hpricot_scan_win32" => [WIN32_PKG_DIR] do
175  cp "extras/mingw-rbconfig.rb", "#{WIN32_PKG_DIR}/ext/hpricot_scan/rbconfig.rb"
176  sh "cd #{WIN32_PKG_DIR}/ext/hpricot_scan/ && ruby -I. extconf.rb && make"
177  mv "#{WIN32_PKG_DIR}/ext/hpricot_scan/hpricot_scan.so", "#{WIN32_PKG_DIR}/#{ARCHLIB}"
178end
179
180desc "Build the binary RubyGems package for win32"
181task :package_win32 => ["hpricot_scan_win32"] do
182  Dir.chdir("#{WIN32_PKG_DIR}") do
183    Gem::Builder.new(Win32Spec).build
184    verbose(true) {
185      mv Dir["*.gem"].first, "../pkg/#{WIN32_PKG_DIR}.gem"
186    }
187  end
188end
189
190CLEAN.include WIN32_PKG_DIR
191
192### JRuby Packages ###
193
194def java_classpath_arg
195  # A myriad of ways to discover the JRuby classpath
196  classpath = begin
197    require 'java'
198    # Already running in a JRuby JVM
199    Java::java.lang.System.getProperty('java.class.path')
200  rescue LoadError
201    ENV['JRUBY_PARENT_CLASSPATH'] || ENV['JRUBY_HOME'] && FileList["#{ENV['JRUBY_HOME']}/lib/*.jar"].join(File::PATH_SEPARATOR)
202  end
203  classpath ? "-cp #{classpath}" : ""
204end
205
206def compile_java(filename, jarname)
207  sh %{javac -source 1.4 -target 1.4 #{java_classpath_arg} #{filename}}
208  sh %{jar cf #{jarname} *.class}
209end
210
211task :hpricot_scan_java => [:ragel_java] do
212  Dir.chdir "ext/hpricot_scan" do
213    compile_java("HpricotScanService.java", "hpricot_scan.jar")
214  end
215end
216
217task :fast_xs_java do
218  Dir.chdir "ext/fast_xs" do
219    compile_java("FastXsService.java", "fast_xs.jar")
220  end
221end
222
223desc "Compiles the JRuby extensions"
224task :hpricot_java => [:hpricot_scan_java, :fast_xs_java] do
225  mkdir_p "#{ARCHLIB}"
226  %w(hpricot_scan fast_xs).each {|ext| mv "ext/#{ext}/#{ext}.jar", "#{ARCHLIB}"}
227end
228
229JRubySpec = SPEC.dup
230JRubySpec.platform = 'jruby'
231JRubySpec.files = PKG_FILES + ["#{ARCHLIB}/hpricot_scan.jar", "#{ARCHLIB}/fast_xs.jar"]
232JRubySpec.extensions = []
233
234JRUBY_PKG_DIR = "#{PKG}-jruby"
235
236desc "Package up the JRuby distribution."
237file JRUBY_PKG_DIR => [:ragel_java, :package] do
238  sh "tar zxf pkg/#{PKG}.tgz"
239  mv PKG, JRUBY_PKG_DIR
240end
241
242desc "Build the RubyGems package for JRuby"
243task :package_jruby => JRUBY_PKG_DIR do
244  Dir.chdir("#{JRUBY_PKG_DIR}") do
245    Rake::Task[:hpricot_java].invoke
246    Gem::Builder.new(JRubySpec).build
247    verbose(true) {
248      mv Dir["*.gem"].first, "../pkg/#{JRUBY_PKG_DIR}.gem"
249    }
250  end
251end
252
253CLEAN.include JRUBY_PKG_DIR
254
255task :install do
256  sh %{rake package}
257  sh %{sudo gem install pkg/#{NAME}-#{VERS}}
258end
259
260task :uninstall => [:clean] do
261  sh %{sudo gem uninstall #{NAME}}
262end
Note: See TracBrowser for help on using the browser.