Method: MakeMakefile#pkg_config

Defined in:
lib/mkmf.rb

#pkg_config(pkg, option = nil) ⇒ Object

Returns compile/link information about an installed library in a tuple of [cflags, ldflags, libs], by using the command found first in the following commands:

  1. If --with-{pkg}-config={command} is given via command line option: {command} {option}

  2. {pkg}-config {option}

  3. pkg-config {option} {pkg}

Where option is, for instance, --cflags.

The values obtained are appended to $CFLAGS, $LDFLAGS and $libs.

If an option argument is given, the config command is invoked with the option and a stripped output string is returned without modifying any of the global values mentioned above.



1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
# File 'lib/mkmf.rb', line 1753

def pkg_config(pkg, option=nil)
  if pkgconfig = with_config("#{pkg}-config") and find_executable0(pkgconfig)
    # iff package specific config command is given
  elsif ($PKGCONFIG ||=
         (pkgconfig = with_config("pkg-config", ("pkg-config" unless CROSS_COMPILING))) &&
         find_executable0(pkgconfig) && pkgconfig) and
      system("#{$PKGCONFIG} --exists #{pkg}")
    # default to pkg-config command
    pkgconfig = $PKGCONFIG
    get = proc {|opt|
      opt = IO.popen("#{$PKGCONFIG} --#{opt} #{pkg}", err:[:child, :out], &:read)
      opt.strip if $?.success?
    }
  elsif find_executable0(pkgconfig = "#{pkg}-config")
    # default to package specific config command, as a last resort.
  else
    pkgconfig = nil
  end
  if pkgconfig
    get ||= proc {|opt|
      opt = IO.popen("#{pkgconfig} --#{opt}", err:[:child, :out], &:read)
      opt.strip if $?.success?
    }
  end
  orig_ldflags = $LDFLAGS
  if get and option
    get[option]
  elsif get and try_ldflags(ldflags = get['libs'])
    if incflags = get['cflags-only-I']
      $INCFLAGS << " " << incflags
      cflags = get['cflags-only-other']
    else
      cflags = get['cflags']
    end
    libs = get['libs-only-l']
    ldflags = (Shellwords.shellwords(ldflags) - Shellwords.shellwords(libs)).quote.join(" ")
    $CFLAGS += " " << cflags
    $CXXFLAGS += " " << cflags
    $LDFLAGS = [orig_ldflags, ldflags].join(' ')
    $libs += " " << libs
    Logging::message "package configuration for %s\n", pkg
    Logging::message "cflags: %s\nldflags: %s\nlibs: %s\n\n",
                     cflags, ldflags, libs
    [cflags, ldflags, libs]
  else
    Logging::message "package configuration for %s is not found\n", pkg
    nil
  end
end