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 $INCFLAGS, $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.



1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
# File 'lib/mkmf.rb', line 1822

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
      xsystem("#{$PKGCONFIG} --exists #{pkg}")
    # default to pkg-config command
    pkgconfig = $PKGCONFIG
    get = proc {|opt|
      opt = xpopen("#{$PKGCONFIG} --#{opt} #{pkg}", err:[:child, :out], &:read)
      Logging.open {puts opt.each_line.map{|s|"=> #{s.inspect}"}}
      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 = xpopen("#{pkgconfig} --#{opt}", err:[:child, :out], &:read)
      Logging.open {puts opt.each_line.map{|s|"=> #{s.inspect}"}}
      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']
    if cflags
      $CFLAGS += " " << cflags
      $CXXFLAGS += " " << cflags
    end
    if libs
      ldflags = (Shellwords.shellwords(ldflags) - Shellwords.shellwords(libs)).quote.join(" ")
    else
      libs, ldflags = Shellwords.shellwords(ldflags).partition {|s| s =~ /-l([^ ]+)/ }.map {|l|l.quote.join(" ")}
    end
    $libs += " " << libs

    $LDFLAGS = [orig_ldflags, ldflags].join(' ')
    Logging::message "package configuration for %s\n", pkg
    Logging::message "incflags: %s\ncflags: %s\nldflags: %s\nlibs: %s\n\n",
                     incflags, cflags, ldflags, libs
    [[incflags, cflags].join(' '), ldflags, libs]
  else
    Logging::message "package configuration for %s is not found\n", pkg
    nil
  end
end