forked from sparklemotion/nokogiri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
336 lines (283 loc) · 9.23 KB
/
Rakefile
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
174
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# -*- ruby -*-
require 'rubygems'
require 'shellwords'
gem 'hoe'
require 'hoe'
Hoe.plugin :debugging
Hoe.plugin :git
Hoe.plugin :gemspec
Hoe.plugin :bundler
GENERATED_PARSER = "lib/nokogiri/css/parser.rb"
GENERATED_TOKENIZER = "lib/nokogiri/css/tokenizer.rb"
def java?
/java/ === RUBY_PLATFORM
end
ENV['LANG'] = "en_US.UTF-8" # UBUNTU 10.04, Y U NO DEFAULT TO UTF-8?
CrossRuby = Struct.new(:version, :host) {
def ver
@ver ||= version[/\A[^-]+/]
end
def minor_ver
@minor_ver ||= ver[/\A\d\.\d(?=\.)/]
end
def api_ver_suffix
case minor_ver
when nil
raise "unsupported version: #{ver}"
else
minor_ver.delete('.') << '0'
end
end
def platform
@platform ||=
case host
when /\Ax86_64-/
'x64-mingw32'
when /\Ai[3-6]86-/
'x86-mingw32'
else
raise "unsupported host: #{host}"
end
end
def tool(name)
(@binutils_prefix ||=
case platform
when 'x64-mingw32'
'x86_64-w64-mingw32-'
when 'x86-mingw32'
'i686-w64-mingw32-'
end) + name
end
def target
case platform
when 'x64-mingw32'
'pei-x86-64'
when 'x86-mingw32'
'pei-i386'
end
end
def libruby_dll
case platform
when 'x64-mingw32'
"x64-msvcrt-ruby#{api_ver_suffix}.dll"
when 'x86-mingw32'
"msvcrt-ruby#{api_ver_suffix}.dll"
end
end
def dlls
[
'kernel32.dll',
'msvcrt.dll',
'ws2_32.dll',
*(case
when ver >= '2.0.0'
'user32.dll'
end),
libruby_dll
]
end
}
CROSS_RUBIES = File.read('.cross_rubies').lines.flat_map { |line|
case line
when /\A([^#]+):([^#]+)/
CrossRuby.new($1, $2)
else
[]
end
}
ENV['RUBY_CC_VERSION'] ||= CROSS_RUBIES.map(&:ver).uniq.join(":")
HOE = Hoe.spec 'nokogiri' do
developer 'Aaron Patterson', 'aaronp@rubyforge.org'
developer 'Mike Dalessio', 'mike.dalessio@gmail.com'
developer 'Yoko Harada', 'yokolet@gmail.com'
developer 'Tim Elliott', 'tle@holymonkey.com'
developer 'Akinori MUSHA', 'knu@idaemons.org'
developer 'John Shahid', 'jvshahid@gmail.com'
developer 'Lars Kanis', 'lars@greiz-reinsdorf.de'
license "MIT"
self.readme_file = "README.md"
self.history_file = "CHANGELOG.md"
self.extra_rdoc_files = FileList['ext/nokogiri/*.c']
self.clean_globs += [
'nokogiri.gemspec',
'lib/nokogiri/nokogiri.{bundle,jar,rb,so}',
'lib/nokogiri/[0-9].[0-9]',
'concourse/images/*.generated'
]
self.clean_globs += Dir.glob("ports/*").reject { |d| d =~ %r{/archives$} }
unless java?
self.extra_deps += [
["mini_portile2", "~> 2.4.0"], # keep version in sync with extconf.rb
]
end
self.extra_dev_deps += [
["concourse", "~> 0.24"],
["hoe-bundler", "~> 1.2"],
["hoe-debugging", "~> 2.0"],
["hoe-gemspec", "~> 1.0"],
["hoe-git", "~> 1.6"],
["minitest", "~> 5.8"],
["racc", "~> 1.4.14"],
["rake", "~> 12.0"],
["rake-compiler", "~> 1.0.3"],
["rake-compiler-dock", "~> 0.7.0"],
["rexical", "~> 1.0.5"],
["simplecov", "~> 0.16"],
]
self.spec_extras = {
:extensions => ["ext/nokogiri/extconf.rb"],
:required_ruby_version => '>= 2.3.0'
}
self.testlib = :minitest
self.test_prelude = 'require "helper"' # ensure simplecov gets loaded before anything else
end
# ----------------------------------------
def add_file_to_gem relative_source_path
dest_path = File.join(gem_build_path, relative_source_path)
dest_dir = File.dirname(dest_path)
mkdir_p dest_dir unless Dir.exist?(dest_dir)
rm_f dest_path if File.exist?(dest_path)
safe_ln relative_source_path, dest_path
HOE.spec.files << relative_source_path
end
def gem_build_path
File.join 'pkg', HOE.spec.full_name
end
if java?
# TODO: clean this section up.
require "rake/javaextensiontask"
Rake::JavaExtensionTask.new("nokogiri", HOE.spec) do |ext|
jruby_home = RbConfig::CONFIG['prefix']
ext.ext_dir = 'ext/java'
ext.lib_dir = 'lib/nokogiri'
ext.source_version = '1.6'
ext.target_version = '1.6'
jars = ["#{jruby_home}/lib/jruby.jar"] + FileList['lib/*.jar']
ext.classpath = jars.map { |x| File.expand_path x }.join ':'
ext.debug = true if ENV['JAVA_DEBUG']
end
task gem_build_path => [:compile] do
add_file_to_gem 'lib/nokogiri/nokogiri.jar'
end
else
begin
require 'rake/extensioncompiler'
# Ensure mingw compiler is installed
Rake::ExtensionCompiler.mingw_host
mingw_available = true
rescue
mingw_available = false
end
require "rake/extensiontask"
HOE.spec.files.reject! { |f| f =~ %r{\.(java|jar)$} }
dependencies = YAML.load_file("dependencies.yml")
task gem_build_path do
%w[libxml2 libxslt].each do |lib|
version = dependencies[lib]["version"]
archive = File.join("ports", "archives", "#{lib}-#{version}.tar.gz")
add_file_to_gem archive
patchesdir = File.join("patches", lib)
patches = `#{['git', 'ls-files', patchesdir].shelljoin}`.split("\n").grep(/\.patch\z/)
patches.each { |patch|
add_file_to_gem patch
}
(untracked = Dir[File.join(patchesdir, '*.patch')] - patches).empty? or
at_exit {
untracked.each { |patch|
puts "** WARNING: untracked patch file not added to gem: #{patch}"
}
}
end
end
Rake::ExtensionTask.new("nokogiri", HOE.spec) do |ext|
ext.lib_dir = File.join(*['lib', 'nokogiri', ENV['FAT_DIR']].compact)
ext.config_options << ENV['EXTOPTS']
if mingw_available
ext.cross_compile = true
ext.cross_platform = CROSS_RUBIES.map(&:platform).uniq
ext.cross_config_options << "--enable-cross-build"
ext.cross_compiling do |spec|
libs = dependencies.map { |name, dep| "#{name}-#{dep["version"]}" }.join(', ')
spec.post_install_message = <<-EOS
Nokogiri is built with the packaged libraries: #{libs}.
EOS
spec.files.reject! { |path| File.fnmatch?('ports/*', path) }
end
end
end
end
# ----------------------------------------
desc "Generate css/parser.rb and css/tokenizer.rex"
task 'generate' => [GENERATED_PARSER, GENERATED_TOKENIZER]
task 'gem:spec' => 'generate' if Rake::Task.task_defined?("gem:spec")
[:compile, :check_manifest].each do |task_name|
Rake::Task[task_name].prerequisites << GENERATED_PARSER
Rake::Task[task_name].prerequisites << GENERATED_TOKENIZER
end
file GENERATED_PARSER => "lib/nokogiri/css/parser.y" do |t|
sh "racc -l -o #{t.name} #{t.prerequisites.first}"
end
file GENERATED_TOKENIZER => "lib/nokogiri/css/tokenizer.rex" do |t|
sh "rex --independent -o #{t.name} #{t.prerequisites.first}"
end
# ----------------------------------------
desc "set environment variables to build and/or test with debug options"
task :debug do
ENV['NOKOGIRI_DEBUG'] = "true"
ENV['CFLAGS'] ||= ""
ENV['CFLAGS'] += " -DDEBUG"
end
task :java_debug do
ENV['JRUBY_OPTS'] = "#{ENV['JRUBY_OPTS']} --debug --dev"
ENV['JAVA_OPTS'] = '-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y' if ENV['JAVA_DEBUG']
end
Rake::Task[:test].prerequisites << :java_debug
if Hoe.plugins.include?(:debugging)
['valgrind', 'valgrind:mem', 'valgrind:mem0'].each do |task_name|
Rake::Task["test:#{task_name}"].prerequisites << :compile
end
end
require 'concourse'
Concourse.new("nokogiri", fly_target: "ci") do |c|
c.add_pipeline "nokogiri", "nokogiri.yml"
c.add_pipeline "nokogiri-pr", "nokogiri-pr.yml"
end
# ----------------------------------------
def verify_dll(dll, cross_ruby)
dll_imports = cross_ruby.dlls
dump = `#{['env', 'LANG=C', cross_ruby.tool('objdump'), '-p', dll].shelljoin}`
raise "unexpected file format for generated dll #{dll}" unless /file format #{Regexp.quote(cross_ruby.target)}\s/ === dump
raise "export function Init_nokogiri not in dll #{dll}" unless /Table.*\sInit_nokogiri\s/mi === dump
# Verify that the expected DLL dependencies match the actual dependencies
# and that no further dependencies exist.
dll_imports_is = dump.scan(/DLL Name: (.*)$/).map(&:first).map(&:downcase).uniq
if dll_imports_is.sort != dll_imports.sort
raise "unexpected dll imports #{dll_imports_is.inspect} in #{dll}"
end
puts "#{dll}: Looks good!"
end
task :cross do
rake_compiler_config_path = File.expand_path("~/.rake-compiler/config.yml")
unless File.exists? rake_compiler_config_path
raise "rake-compiler has not installed any cross rubies. Use rake-compiler-dock or 'rake gem:windows' for building binary windows gems."
end
CROSS_RUBIES.each do |cross_ruby|
task "tmp/#{cross_ruby.platform}/nokogiri/#{cross_ruby.ver}/nokogiri.so" do |t|
# To reduce the gem file size strip mingw32 dlls before packaging
sh [cross_ruby.tool('strip'), '-S', t.name].shelljoin
verify_dll t.name, cross_ruby
end
end
end
desc "build a windows gem without all the ceremony"
task "gem:windows" do
require "rake_compiler_dock"
RakeCompilerDock.sh "bundle && rake cross native gem MAKE='nice make -j`nproc`' RUBY_CC_VERSION=#{ENV['RUBY_CC_VERSION']}"
end
desc "build a jruby gem with docker"
task "gem:jruby" do
require "rake_compiler_dock"
RakeCompilerDock.sh "bundle && rake java gem", rubyvm: 'jruby'
end
require_relative "tasks/docker"
# vim: syntax=Ruby