def main()

in build/config/linux/pkg-config.py [0:0]


def main():
  # If this is run on non-Linux platforms, just return nothing and indicate
  # success. This allows us to "kind of emulate" a Linux build from other
  # platforms.
  if "linux" not in sys.platform:
    print("[[],[],[],[],[]]")
    return 0

  parser = OptionParser()
  parser.add_option('-d', '--debug', action='store_true')
  parser.add_option('-p', action='store', dest='pkg_config', type='string',
                    default='pkg-config')
  parser.add_option('-v', action='append', dest='strip_out', type='string')
  parser.add_option('-s', action='store', dest='sysroot', type='string')
  parser.add_option('-a', action='store', dest='arch', type='string')
  parser.add_option('--system_libdir', action='store', dest='system_libdir',
                    type='string', default='lib')
  parser.add_option('--atleast-version', action='store',
                    dest='atleast_version', type='string')
  parser.add_option('--libdir', action='store_true', dest='libdir')
  parser.add_option('--dridriverdir', action='store_true', dest='dridriverdir')
  parser.add_option('--version-as-components', action='store_true',
                    dest='version_as_components')
  (options, args) = parser.parse_args()

  # Make a list of regular expressions to strip out.
  strip_out = []
  if options.strip_out != None:
    for regexp in options.strip_out:
      strip_out.append(re.compile(regexp))

  if options.sysroot:
    libdir = SetConfigPath(options)
    if options.debug:
      sys.stderr.write('PKG_CONFIG_LIBDIR=%s\n' % libdir)
    prefix = GetPkgConfigPrefixToStrip(options, args)
  else:
    prefix = ''

  if options.atleast_version:
    # When asking for the return value, just run pkg-config and print the return
    # value, no need to do other work.
    if not subprocess.call([options.pkg_config,
                            "--atleast-version=" + options.atleast_version] +
                            args):
      print("true")
    else:
      print("false")
    return 0

  if options.version_as_components:
    cmd = [options.pkg_config, "--modversion"] + args
    try:
      version_string = subprocess.check_output(cmd).decode('utf-8')
    except:
      sys.stderr.write('Error from pkg-config.\n')
      return 1
    print(json.dumps(list(map(int, version_string.strip().split(".")))))
    return 0


  if options.libdir:
    cmd = [options.pkg_config, "--variable=libdir"] + args
    if options.debug:
      sys.stderr.write('Running: %s\n' % cmd)
    try:
      libdir = subprocess.check_output(cmd).decode('utf-8')
    except:
      print("Error from pkg-config.")
      return 1
    sys.stdout.write(libdir.strip())
    return 0

  if options.dridriverdir:
    cmd = [options.pkg_config, "--variable=dridriverdir"] + args
    if options.debug:
      sys.stderr.write('Running: %s\n' % cmd)
    try:
      dridriverdir = subprocess.check_output(cmd).decode('utf-8')
    except:
      print("Error from pkg-config.")
      return 1
    sys.stdout.write(dridriverdir.strip())
    return

  cmd = [options.pkg_config, "--cflags", "--libs"] + args
  if options.debug:
    sys.stderr.write('Running: %s\n' % ' '.join(cmd))

  try:
    flag_string = subprocess.check_output(cmd).decode('utf-8')
  except:
    sys.stderr.write('Could not run pkg-config.\n')
    return 1

  # For now just split on spaces to get the args out. This will break if
  # pkgconfig returns quoted things with spaces in them, but that doesn't seem
  # to happen in practice.
  all_flags = flag_string.strip().split(' ')


  sysroot = options.sysroot
  if not sysroot:
    sysroot = ''

  includes = []
  cflags = []
  libs = []
  lib_dirs = []

  for flag in all_flags[:]:
    if len(flag) == 0 or MatchesAnyRegexp(flag, strip_out):
      continue;

    if flag[:2] == '-l':
      libs.append(RewritePath(flag[2:], prefix, sysroot))
    elif flag[:2] == '-L':
      lib_dirs.append(RewritePath(flag[2:], prefix, sysroot))
    elif flag[:2] == '-I':
      includes.append(RewritePath(flag[2:], prefix, sysroot))
    elif flag[:3] == '-Wl':
      # Don't allow libraries to control ld flags.  These should be specified
      # only in build files.
      pass
    elif flag == '-pthread':
      # Many libs specify "-pthread" which we don't need since we always include
      # this anyway. Removing it here prevents a bunch of duplicate inclusions
      # on the command line.
      pass
    else:
      cflags.append(flag)

  # Output a GN array, the first one is the cflags, the second are the libs. The
  # JSON formatter prints GN compatible lists when everything is a list of
  # strings.
  print(json.dumps([includes, cflags, libs, lib_dirs]))
  return 0