Top Level Namespace

Defined Under Namespace

Modules: Nokogiri, XSD

Constant Summary collapse

PACKAGE_ROOT_DIR =

helpful constants

File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
REQUIRED_LIBXML_VERSION =
"2.6.21"
"2.9.3"
REQUIRED_MINI_PORTILE_VERSION =

The gem version constraint in the Rakefile is not respected at install time. Keep this version in sync with the one in the Rakefile !

"~> 2.5.0"
REQUIRED_PKG_CONFIG_VERSION =
"~> 1.1"
OTHER_LIBRARY_VERSIONS =

Keep track of what versions of what libraries we build against

{}
NOKOGIRI_HELP_MESSAGE =
<<~HELP
  USAGE: ruby #{$0} [options]

    Flags that are always valid:

      --use-system-libraries
          Use system libraries instead of building and using the packaged libraries

      --disable-clean
          Do not clean out intermediate files after successful build

      --prevent-strip
          Take steps to prevent stripping the symbol table and debugging info from the shared
          library, potentially overriding RbConfig's CFLAGS/LDFLAGS/DLDFLAGS.


    Flags only used when using system libraries:

      --with-opt-dir=DIRECTORY
          Look for headers and libraries in DIRECTORY

      --with-zlib-dir=DIR
          Look for zlib header and library in DIRECTORY

      --with-iconv-dir=DIRECTORY
          Look for iconv header and library in DIRECTORY

      --with-xml2-dir=DIRECTORY
          Look for xml2 headers and library in DIRECTORY

      --with-xml2-lib=DIRECTORY
          Look for xml2 library in DIRECTORY

      --with-xslt-include=DIRECTORY
          Look for xslt headers in DIRECTORY

      --with-xslt-dir=DIRECTORY
          Look for xslt headers and library in DIRECTORY

      --with-xslt-lib=DIRECTORY
          Look for xslt library in DIRECTORY

      --with-xslt-include=DIRECTORY
          Look for xslt headers in DIRECTORY

      --with-exslt-dir=DIRECTORY
          Look for exslt headers and library in DIRECTORY

      --with-exslt-lib=DIRECTORY
          Look for exslt library in DIRECTORY

      --with-exslt-include=DIRECTORY
          Look for exslt headers in DIRECTORY


    Flags only used when building and using the packaged libraries:

      --disable-static
          Do not statically link packaged libraries, instead use shared libraries

      --enable-cross-build
          Enable cross-build mode. (You probably do not want to set this manually.)


    Environment variables used:

      NOKOGIRI_USE_SYSTEM_LIBRARIES
          When set, even if nil or blank, use system libraries instead of building and using the
          packaged libraries. Equivalent to `--use-system-libraries`.

      CC
          Use this path to invoke the compiler instead of `RbConfig::CONFIG['CC']`

      CPPFLAGS
          If this string is accepted by the C preprocessor, add it to the flags passed to the C preprocessor

      CFLAGS
          If this string is accepted by the compiler, add it to the flags passed to the compiler

      LDFLAGS
          If this string is accepted by the linker, add it to the flags passed to the linker

      LIBS
          Add this string to the flags passed to the linker
HELP
LOCAL_PACKAGE_RESPONSE =
Object.new

Instance Method Summary collapse

Instance Method Details

#abort_could_not_find_library(lib) ⇒ Object



219
220
221
# File 'ext/nokogiri/extconf.rb', line 219

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

#aix?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'ext/nokogiri/extconf.rb', line 130

def aix?
  RbConfig::CONFIG["target_os"] =~ /aix/
end

#chdir_for_buildObject



223
224
225
226
227
228
229
230
231
# File 'ext/nokogiri/extconf.rb', line 223

def chdir_for_build
  # 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.
  build_dir = ENV['RCD_HOST_RUBY_PLATFORM'].to_s =~ /mingw|mswin|cygwin/ ? '/tmp' : '.'
  Dir.chdir(build_dir) do
    yield
  end
end

#concat_flags(*args) ⇒ Object



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

def concat_flags *args
  args.compact.join(" ")
end

#copy_packaged_libraries_headers(to_path:, from_recipes:) ⇒ Object



460
461
462
463
464
465
466
# File 'ext/nokogiri/extconf.rb', line 460

def copy_packaged_libraries_headers(to_path:, from_recipes:)
  FileUtils.rm_rf(to_path, secure: true)
  FileUtils.mkdir(to_path)
  from_recipes.each do |recipe|
    FileUtils.cp_r(Dir[File.join(recipe.path, 'include/*')], to_path)
  end
end

#darwin?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'ext/nokogiri/extconf.rb', line 122

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

#do_cleanObject



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'ext/nokogiri/extconf.rb', line 473

def do_clean
  root = Pathname(PACKAGE_ROOT_DIR)
  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



468
469
470
471
# File 'ext/nokogiri/extconf.rb', line 468

def do_help
  print NOKOGIRI_HELP_MESSAGE
  exit! 0
end

#ensure_func(func, headers = nil) ⇒ Object



208
209
210
# File 'ext/nokogiri/extconf.rb', line 208

def ensure_func(func, headers=nil)
  have_func(func, headers) or abort_could_not_find_library(lib)
end

#ensure_package_configuration(opt: nil, pc: nil, lib:, func:, headers:) ⇒ Object



203
204
205
206
# File 'ext/nokogiri/extconf.rb', line 203

def ensure_package_configuration(opt: nil, pc: nil, lib:, func:, headers:)
  have_package_configuration(opt: opt, pc: pc, lib: lib, func: func, headers: headers) or
    abort_could_not_find_library(lib)
end

#have_libxml_headers?(version = nil) ⇒ Boolean

Returns:

  • (Boolean)


267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'ext/nokogiri/extconf.rb', line 267

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

  try_cpp source
end

#have_package_configuration(opt: nil, pc: nil, lib:, func:, headers:) ⇒ Object

set up mkmf to link against the library if we can find it



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'ext/nokogiri/extconf.rb', line 186

def have_package_configuration(opt: nil, pc: nil, lib:, func:, headers:)
  if opt
    dir_config(opt)
    dir_config("opt")
  end

  # see if we have enough path info to do this without trying any harder
  if !ENV.key?("NOKOGIRI_TEST_PKG_CONFIG")
    return true if local_have_library(lib, func, headers)
  end

  try_package_configuration(pc) if pc

  # verify that we can compile and link against the library
  local_have_library(lib, func, headers)
end

#iconv_configure_flagsObject



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

def iconv_configure_flags
  # give --with-iconv-dir and --with-opt-dir first priority
  ["iconv", "opt"].each do |target|
    config = preserving_globals { dir_config(target) }
    if config.any? && try_link_iconv("--with-#{target}-* flags") { dir_config(target) }
      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 try_link_iconv
    return ['--with-iconv=yes']
  end

  config = preserving_globals { have_package_configuration('libiconv') }
  if config && try_link_iconv('pkg-config libiconv') { have_package_configuration('libiconv') }
    cflags, ldflags, libs = config

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

  abort_could_not_find_library "libiconv"
end

#libflag_to_filename(ldflag) ⇒ Object



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

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

#local_have_library(lib, func = nil, headers = nil) ⇒ Object



142
143
144
# File 'ext/nokogiri/extconf.rb', line 142

def local_have_library(lib, func=nil, headers=nil)
  have_library(lib, func, headers) or have_library("lib#{lib}", func, headers)
end

#nix?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'ext/nokogiri/extconf.rb', line 134

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

#Nokogiri(*args, &block) ⇒ Object

Parse 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

#openbsd?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'ext/nokogiri/extconf.rb', line 126

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

#preserving_globalsObject



212
213
214
215
216
217
# File 'ext/nokogiri/extconf.rb', line 212

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

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



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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'ext/nokogiri/extconf.rb', line 344

def process_recipe(name, version, static_p, cross_p)
  require 'rubygems'
  gem 'mini_portile2', REQUIRED_MINI_PORTILE_VERSION
  require 'mini_portile2'
  message "Using mini_portile version #{MiniPortile::VERSION}\n"

  if name != "libxml2" && name != "libxslt"
    OTHER_LIBRARY_VERSIONS[name] = version
  end

  MiniPortile.new(name, version).tap do |recipe|
    recipe.target = File.join(PACKAGE_ROOT_DIR, "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(PACKAGE_ROOT_DIR, "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/
        if env.key?($1)
          env[$1] = concat_flags(env[$1], $2)
        else
          env[$1] = $2
        end
        true
      else
        false
      end
    end

    if static_p
      recipe.configure_options += [
        "--disable-shared",
        "--enable-static",
      ]
      env["CFLAGS"] = concat_flags(env["CFLAGS"], "-fPIC")
    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] = concat_flags(env[key], RbConfig::CONFIG['ARCH_FLAG'])
        end
      end
    end

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

    checkpoint = "#{recipe.target}/#{recipe.name}-#{recipe.version}-#{recipe.host}.installed"
    if File.exist?(checkpoint)
      message "Building Nokogiri with a packaged version of #{name}-#{version}.\n"
    else
      message <<~EOM
        ---------- IMPORTANT NOTICE ----------
        Building Nokogiri with a packaged version of #{name}-#{version}.
        Configuration options: #{recipe.configure_options.shelljoin}
      EOM

      unless recipe.patch_files.empty?
        message "The following patches are being applied:\n"

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

      message <<~EOM

        The Nokogiri maintainers intend to provide timely security updates, but if
        this is a concern for you and want to use your OS/distro system library
        instead, then abort this installation process and install nokogiri as
        instructed at:

          https://nokogiri.org/tutorials/installing_nokogiri.html#install-with-system-libraries

      EOM

      message <<~EOM if name == 'libxml2'
        Note, however, that nokogiri cannot guarantee compatiblity with every
        version of libxml2 that may be provided by OS/package vendors.

      EOM

      chdir_for_build do
        recipe.cook
      end
      FileUtils.touch checkpoint
    end
    recipe.activate
  end
end

#sh_export_path(path) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'ext/nokogiri/extconf.rb', line 233

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
  return path if !windows?

  match = Regexp.new("^([A-Z]):(/.*)").match(path)
  if match && match.length == 3
    return File.join("/", match[1], match[2])
  end

  path
end

#solaris?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'ext/nokogiri/extconf.rb', line 118

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


285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'ext/nokogiri/extconf.rb', line 285

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

        try_link(<<~'SRC', opt)
          #include <stdlib.h>
          #include <iconv.h>
          int main(void)
          {
              iconv_t cd = iconv_open("", "");
              iconv(cd, NULL, NULL, NULL, NULL);
              return EXIT_SUCCESS;
          }
        SRC
      end
    end
  end
end

#try_package_configuration(pc) ⇒ Object

wrapper around MakeMakefil#pkg_config and the PKGConfig gem



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

def try_package_configuration(pc)
  if !ENV.key?("NOKOGIRI_TEST_PKG_CONFIG_GEM")
    # try MakeMakefile#pkg_config, which uses the system utility `pkg-config`.
    return if checking_for("#{pc} using `pkg_config`", LOCAL_PACKAGE_RESPONSE) do
      pkg_config(pc)
    end
  end

  # `pkg-config` probably isn't installed, which appears to be the case for lots of freebsd systems.
  # let's fall back to the pkg-config gem, which knows how to parse .pc files, and wrap it with the
  # same logic as MakeMakefile#pkg_config
  begin
    require 'rubygems'
    gem 'pkg-config', REQUIRED_PKG_CONFIG_VERSION
    require 'pkg-config'

    checking_for("#{pc} using pkg-config gem version #{PKGConfig::VERSION}", LOCAL_PACKAGE_RESPONSE) do
      if PKGConfig.have_package(pc)
        cflags  = PKGConfig.cflags(pc)
        ldflags = PKGConfig.libs_only_L(pc)
        libs    = PKGConfig.libs_only_l(pc)

        Logging::message "pkg-config gem found package configuration for %s\n", pc
        Logging::message "cflags: %s\nldflags: %s\nlibs: %s\n\n", cflags, ldflags, libs

        [cflags, ldflags, libs]
      end
    end
  rescue LoadError
    message "Please install either the `pkg-config` utility or the `pkg-config` rubygem.\n"
  end
end

#using_system_libraries?Boolean

Returns:

  • (Boolean)


262
263
264
265
# File 'ext/nokogiri/extconf.rb', line 262

def using_system_libraries?
  # NOTE: TruffleRuby uses this env var as it does not support using static libraries yet.
  arg_config('--use-system-libraries', ENV.key?("NOKOGIRI_USE_SYSTEM_LIBRARIES"))
end

#windows?Boolean

utility functions

Returns:

  • (Boolean)


114
115
116
# File 'ext/nokogiri/extconf.rb', line 114

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