Top Level Namespace

Defined Under Namespace

Modules: Nokogiri, XSD

Constant Summary collapse

ROOT =
File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))

Instance Method Summary collapse

Instance Method Details

#add_cflags(flags) ⇒ Object


159
160
161
162
163
164
165
166
167
168
169
170
# File 'ext/nokogiri/extconf.rb', line 159

def add_cflags(flags)
  print "checking if the C compiler accepts #{flags}... "
  with_cflags("#{$CFLAGS} #{flags}") do
    if nokogiri_try_compile
      puts 'yes'
      true
    else
      puts 'no'
      false
    end
  end
end

#asplode(lib) ⇒ Object


186
187
188
# File 'ext/nokogiri/extconf.rb', line 186

def asplode(lib)
  abort "-----\n#{lib} is missing.  Please locate mkmf.log to investigate how it is failing.\n-----"
end

#chdir_for_buildObject

When using rake-compiler-dock on Windows, the underlying Virtualbox shared folders don't support symlinks, but libiconv expects it for a build on Linux. We work around this limitation by using the temp dir for cooking.


254
255
256
257
258
259
# File 'ext/nokogiri/extconf.rb', line 254

def chdir_for_build
  build_dir = ENV['RCD_HOST_RUBY_PLATFORM'].to_s =~ /mingw|mswin|cygwin/ ? '/tmp' : '.'
  Dir.chdir(build_dir) do
    yield
  end
end

#check_libxml_version(version = nil) ⇒ Object


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'ext/nokogiri/extconf.rb', line 141

def check_libxml_version version=nil
  source = if version.nil?
             "#include <libxml/xmlversion.h>\n"
           else
             version_int = sprintf "%d%2.2d%2.2d", *(version.split("."))
             "#include <libxml/xmlversion.h>\n#if LIBXML_VERSION < \#{version_int}\n#error libxml2 is older than \#{version}\n#endif\n"
           end

  try_cpp source
end

#darwin?Boolean

Returns:

  • (Boolean)

19
20
21
# File 'ext/nokogiri/extconf.rb', line 19

def darwin?
  RbConfig::CONFIG['target_os'] =~ /darwin/
end

#do_cleanObject


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
# File 'ext/nokogiri/extconf.rb', line 83

def do_clean
  require 'pathname'
  require 'fileutils'

  root = Pathname(ROOT)
  pwd  = Pathname(Dir.pwd)

  # Skip if this is a development work tree
  unless (root + '.git').exist?
    message "Cleaning files only used during build.\n"

    # (root + 'tmp') cannot be removed at this stage because
    # nokogiri.so is yet to be copied to lib.

    # clean the ports build directory
    Pathname.glob(pwd.join('tmp', '*', 'ports')) do |dir|
      FileUtils.rm_rf(dir, verbose: true)
    end

    if enable_config('static')
      # ports installation can be safely removed if statically linked.
      FileUtils.rm_rf(root + 'ports', verbose: true)
    else
      FileUtils.rm_rf(root + 'ports' + 'archives', verbose: true)
    end
  end

  exit! 0
end

#do_helpObject


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
# File 'ext/nokogiri/extconf.rb', line 52

def do_help
  print "usage: ruby \#{$0} [options]\n\n    --disable-clean\n        Do not clean out intermediate files after successful build.\n\n    --disable-static\n        Do not statically link bundled libraries.\n\n    --with-iconv-dir=DIR\n        Use the iconv library placed under DIR.\n\n    --with-zlib-dir=DIR\n        Use the zlib library placed under DIR.\n\n    --use-system-libraries\n        Use system libraries instead of building and using the bundled\n        libraries.\n\n    --with-xml2-dir=DIR / --with-xml2-config=CONFIG\n    --with-xslt-dir=DIR / --with-xslt-config=CONFIG\n    --with-exslt-dir=DIR / --with-exslt-config=CONFIG\n        Use libxml2/libxslt/libexslt as specified.\n\n    --enable-cross-build\n        Do cross-build.\n"
  exit! 0
end

#have_iconv?(using = nil) ⇒ Boolean

Returns:

  • (Boolean)

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'ext/nokogiri/extconf.rb', line 190

def have_iconv?(using = nil)
  checking_for(using ? "iconv using #{using}" : 'iconv') do
    ['', '-liconv'].any? do |opt|
      preserving_globals do
        yield if block_given?

        try_link("#include <stdlib.h>\n#include <iconv.h>\n\nint main(void)\n{\niconv_t cd = iconv_open(\"\", \"\");\niconv(cd, NULL, NULL, NULL, NULL);\nreturn EXIT_SUCCESS;\n}\n", opt)
      end
    end
  end
end

#iconv_configure_flagsObject


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
# File 'ext/nokogiri/extconf.rb', line 212

def iconv_configure_flags
  # If --with-iconv-dir or --with-opt-dir is given, it should be
  # the first priority
  %w[iconv opt].each do |name|
    if (config = preserving_globals { dir_config(name) }).any? &&
        have_iconv?("--with-#{name}-* flags") { dir_config(name) }
      idirs, ldirs = config.map do |dirs|
        Array(dirs).flat_map do |dir|
          dir.split(File::PATH_SEPARATOR)
        end if dirs
      end

      return [
        '--with-iconv=yes',
        *("CPPFLAGS=#{idirs.map { |dir| '-I' + dir }.join(' ')}" if idirs),
        *("LDFLAGS=#{ldirs.map { |dir| '-L' + dir }.join(' ')}" if ldirs),
      ]
    end
  end

  if have_iconv?
    return ['--with-iconv=yes']
  end

  if (config = preserving_globals { package_config('libiconv') }) &&
     have_iconv?('pkg-config libiconv') { package_config('libiconv') }
    cflags, ldflags, libs = config

    return [
      '--with-iconv=yes',
      "CPPFLAGS=#{cflags}",
      "LDFLAGS=#{ldflags}",
      "LIBS=#{libs}",
    ]
  end

  asplode "libiconv"
end

#lib_a(ldflag) ⇒ Object


373
374
375
376
377
378
# File 'ext/nokogiri/extconf.rb', line 373

def lib_a(ldflag)
  case ldflag
  when /\A-l(.+)/
    "lib#{$1}.#{$LIBEXT}"
  end
end

#nix?Boolean

Returns:

  • (Boolean)

27
28
29
# File 'ext/nokogiri/extconf.rb', line 27

def nix?
  ! (windows? || solaris? || darwin?)
end

#Nokogiri(*args, &block) ⇒ Object

Parser a document contained in args. Nokogiri will try to guess what type of document you are attempting to parse. For more information, see Nokogiri.parse

To specify the type of document, use Nokogiri.XML or Nokogiri.HTML.


138
139
140
141
142
143
144
# File 'lib/nokogiri.rb', line 138

def Nokogiri(*args, &block)
  if block_given?
    Nokogiri::HTML::Builder.new(&block).doc.root
  else
    Nokogiri.parse(*args)
  end
end

#nokogiri_try_compileObject


137
138
139
# File 'ext/nokogiri/extconf.rb', line 137

def nokogiri_try_compile
  try_compile "int main() {return 0;}", "", {werror: true}
end

#openbsd?Boolean

Returns:

  • (Boolean)

23
24
25
# File 'ext/nokogiri/extconf.rb', line 23

def openbsd?
  RbConfig::CONFIG['target_os'] =~ /openbsd/
end

#package_config(pkg, options = {}) ⇒ Object


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'ext/nokogiri/extconf.rb', line 113

def package_config pkg, options={}
  package = pkg_config(pkg)
  return package if package

  begin
    require 'rubygems'
    gem 'pkg-config', (gem_ver='~> 1.1')
    require 'pkg-config' and message("Using pkg-config gem version #{PKGConfig::VERSION}\n")
  rescue LoadError
    message "pkg-config could not be used to find #{pkg}\nPlease install either `pkg-config` or the pkg-config gem per\n\n    gem install pkg-config -v #{gem_ver.inspect}\n\n"
  else
    return nil unless PKGConfig.have_package(pkg)

    cflags  = PKGConfig.cflags(pkg)
    ldflags = PKGConfig.libs_only_L(pkg)
    libs    = PKGConfig.libs_only_l(pkg)

    Logging::message "PKGConfig package configuration for %s\n", pkg
    Logging::message "cflags: %s\nldflags: %s\nlibs: %s\n\n", cflags, ldflags, libs

    [cflags, ldflags, libs]
  end
end

#preserving_globalsObject


172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'ext/nokogiri/extconf.rb', line 172

def preserving_globals
  values = [
    $arg_config,
    $CFLAGS, $CPPFLAGS,
    $LDFLAGS, $LIBPATH, $libs
  ].map(&:dup)
  yield
ensure
  $arg_config,
  $CFLAGS, $CPPFLAGS,
  $LDFLAGS, $LIBPATH, $libs =
    values
end

#process_recipe(name, version, static_p, cross_p) ⇒ Object


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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'ext/nokogiri/extconf.rb', line 261

def process_recipe(name, version, static_p, cross_p)
  MiniPortile.new(name, version).tap do |recipe|
    recipe.target = File.join(ROOT, "ports")
    # Prefer host_alias over host in order to use i586-mingw32msvc as
    # correct compiler prefix for cross build, but use host if not set.
    recipe.host = RbConfig::CONFIG["host_alias"].empty? ? RbConfig::CONFIG["host"] : RbConfig::CONFIG["host_alias"]
    recipe.patch_files = Dir[File.join(ROOT, "patches", name, "*.patch")].sort
    recipe.configure_options << "--libdir=#{File.join(recipe.path, "lib")}"

    yield recipe

    env = Hash.new do |hash, key|
      hash[key] = "#{ENV[key]}"  # (ENV[key].dup rescue '')
    end

    recipe.configure_options.flatten!

    recipe.configure_options.delete_if do |option|
      case option
      when /\A(\w+)=(.*)\z/
        env[$1] = $2
        true
      else
        false
      end
    end

    if static_p
      recipe.configure_options += [
        "--disable-shared",
        "--enable-static",
      ]
      env['CFLAGS'] = "-fPIC #{env['CFLAGS']}"
    else
      recipe.configure_options += [
        "--enable-shared",
        "--disable-static",
      ]
    end

    if cross_p
      recipe.configure_options += [
        "--target=#{recipe.host}",
        "--host=#{recipe.host}",
      ]
    end

    if RbConfig::CONFIG['target_cpu'] == 'universal'
      %w[CFLAGS LDFLAGS].each do |key|
        unless env[key].include?('-arch')
          env[key] += ' ' + RbConfig::CONFIG['ARCH_FLAG']
        end
      end
    end

    recipe.configure_options += env.map do |key, value|
      "#{key}=#{value}"
    end

    message "************************************************************************\nIMPORTANT NOTICE:\n\nBuilding Nokogiri with a packaged version of \#{name}-\#{version}\#{'.' if recipe.patch_files.empty?}\n"

    unless recipe.patch_files.empty?
      message "with the following patches applied:\n"

      recipe.patch_files.each do |patch|
        message "\t- %s\n" % File.basename(patch)
      end
    end

    message "\nTeam Nokogiri will keep on doing their best to provide security\nupdates in a timely manner, but if this is a concern for you and want\nto use the system library instead; abort this installation process and\nreinstall nokogiri as follows:\n\ngem install nokogiri -- --use-system-libraries\n[--with-xml2-config=/path/to/xml2-config]\n[--with-xslt-config=/path/to/xslt-config]\n\nIf you are using Bundler, tell it to use the option:\n\nbundle config build.nokogiri --use-system-libraries\nbundle install\n"

    message "\nNote, however, that nokogiri is not fully compatible with arbitrary\nversions of libxml2 provided by OS/package vendors.\n" if name == 'libxml2'

    message "************************************************************************\n"

    checkpoint = "#{recipe.target}/#{recipe.name}-#{recipe.version}-#{recipe.host}.installed"
    unless File.exist?(checkpoint)
      chdir_for_build do
        recipe.cook
      end
      FileUtils.touch checkpoint
    end
    recipe.activate
  end
end

#sh_export_path(path) ⇒ Object


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'ext/nokogiri/extconf.rb', line 31

def sh_export_path path
  # because libxslt 1.1.29 configure.in uses AC_PATH_TOOL which treats ":"
  # as a $PATH separator, we need to convert windows paths from
  #
  #   C:/path/to/foo
  #
  # to
  #
  #   /C/path/to/foo
  #
  # which is sh-compatible, in order to find things properly during
  # configuration
  if windows?
    match = Regexp.new("^([A-Z]):(/.*)").match(path)
    if match && match.length == 3
      return File.join("/", match[1], match[2])
    end
  end
  path
end

#solaris?Boolean

Returns:

  • (Boolean)

15
16
17
# File 'ext/nokogiri/extconf.rb', line 15

def solaris?
  RbConfig::CONFIG['target_os'] =~ /solaris/
end

#using_system_libraries?Boolean

Returns:

  • (Boolean)

380
381
382
# File 'ext/nokogiri/extconf.rb', line 380

def using_system_libraries?
  arg_config('--use-system-libraries', !!ENV['NOKOGIRI_USE_SYSTEM_LIBRARIES'])
end

#windows?Boolean

functions

Returns:

  • (Boolean)

11
12
13
# File 'ext/nokogiri/extconf.rb', line 11

def windows?
  RbConfig::CONFIG['target_os'] =~ /mingw32|mswin/
end