pkg-config.py revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1# Copyright (c) 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import json
6import os
7import subprocess
8import sys
9import re
10from optparse import OptionParser
11
12# This script runs pkg-config, optionally filtering out some results, and
13# returns the result.
14#
15# The result will be [ <includes>, <cflags>, <libs>, <lib_dirs>, <ldflags> ]
16# where each member is itself a list of strings.
17#
18# You can filter out matches using "-v <regexp>" where all results from
19# pkgconfig matching the given regular expression will be ignored. You can
20# specify more than one regular expression my specifying "-v" more than once.
21#
22# You can specify a sysroot using "-s <sysroot>" where sysroot is the absolute
23# system path to the sysroot used for compiling. This script will attempt to
24# generate correct paths for the sysroot.
25#
26# When using a sysroot, you must also specify the architecture via
27# "-a <arch>" where arch is either "x86" or "x64".
28
29# If this is run on non-Linux platforms, just return nothing and indicate
30# success. This allows us to "kind of emulate" a Linux build from other
31# platforms.
32if sys.platform.find("linux") == -1:
33  print "[[],[],[],[]]"
34  sys.exit(0)
35
36
37def SetConfigPath(options):
38  """Set the PKG_CONFIG_PATH environment variable.
39  This takes into account any sysroot and architecture specification from the
40  options on the given command line."""
41
42  sysroot = options.sysroot
43  if not sysroot:
44    sysroot = ""
45
46  # Compute the library path name based on the architecture.
47  arch = options.arch
48  if sysroot and not arch:
49    print "You must specify an architecture via -a if using a sysroot."
50    sys.exit(1)
51  if arch == 'x64':
52    libpath = 'lib64'
53  else:
54    libpath = 'lib'
55
56  # Add the sysroot path to the environment's PKG_CONFIG_PATH
57  config_path = sysroot + '/usr/' + libpath + '/pkgconfig'
58  config_path += ':' + sysroot + '/usr/share/pkgconfig'
59  if 'PKG_CONFIG_PATH' in os.environ:
60    os.environ['PKG_CONFIG_PATH'] += ':' + config_path
61  else:
62    os.environ['PKG_CONFIG_PATH'] = config_path
63
64
65def GetPkgConfigPrefixToStrip(args):
66  """Returns the prefix from pkg-config where packages are installed.
67  This returned prefix is the one that should be stripped from the beginning of
68  directory names to take into account sysroots."""
69  # Some sysroots, like the Chromium OS ones, may generate paths that are not
70  # relative to the sysroot. For example,
71  # /path/to/chroot/build/x86-generic/usr/lib/pkgconfig/pkg.pc may have all
72  # paths relative to /path/to/chroot (i.e. prefix=/build/x86-generic/usr)
73  # instead of relative to /path/to/chroot/build/x86-generic (i.e prefix=/usr).
74  # To support this correctly, it's necessary to extract the prefix to strip
75  # from pkg-config's |prefix| variable.
76  prefix = subprocess.check_output(["pkg-config", "--variable=prefix"] + args,
77      env=os.environ)
78  if prefix[-4] == '/usr':
79    return prefix[4:]
80  return prefix
81
82
83def MatchesAnyRegexp(flag, list_of_regexps):
84  """Returns true if the first argument matches any regular expression in the
85  given list."""
86  for regexp in list_of_regexps:
87    if regexp.search(flag) != None:
88      return True
89  return False
90
91
92def RewritePath(path, strip_prefix, sysroot):
93  """Rewrites a path by stripping the prefix and prepending the sysroot."""
94  if os.path.isabs(path) and not path.startswith(sysroot):
95    if path.startswith(strip_prefix):
96      path = path[len(strip_prefix):]
97    path = path.lstrip('/')
98    return os.path.join(sysroot, path)
99  else:
100    return path
101
102
103parser = OptionParser()
104parser.add_option('-v', action='append', dest='strip_out', type='string')
105parser.add_option('-s', action='store', dest='sysroot', type='string')
106parser.add_option('-a', action='store', dest='arch', type='string')
107(options, args) = parser.parse_args()
108
109# Make a list of regular expressions to strip out.
110strip_out = []
111if options.strip_out != None:
112  for regexp in options.strip_out:
113    strip_out.append(re.compile(regexp))
114
115SetConfigPath(options)
116if options.sysroot:
117  prefix = GetPkgConfigPrefixToStrip(args)
118else:
119  prefix = ''
120
121try:
122  flag_string = subprocess.check_output(["pkg-config", "--cflags", "--libs"] +
123      args, env=os.environ)
124  # For now just split on spaces to get the args out. This will break if
125  # pkgconfig returns quoted things with spaces in them, but that doesn't seem
126  # to happen in practice.
127  all_flags = flag_string.strip().split(' ')
128except:
129  print "Could not run pkg-config."
130  sys.exit(1)
131
132
133sysroot = options.sysroot
134if not sysroot:
135  sysroot = ''
136
137includes = []
138cflags = []
139libs = []
140lib_dirs = []
141ldflags = []
142
143for flag in all_flags[:]:
144  if len(flag) == 0 or MatchesAnyRegexp(flag, strip_out):
145    continue;
146
147  if flag[:2] == '-l':
148    libs.append(RewritePath(flag[2:], prefix, sysroot))
149  elif flag[:2] == '-L':
150    lib_dirs.append(RewritePath(flag[2:], prefix, sysroot))
151  elif flag[:2] == '-I':
152    includes.append(RewritePath(flag[2:], prefix, sysroot))
153  elif flag[:3] == '-Wl':
154    ldflags.append(flag)
155  elif flag == '-pthread':
156    # Many libs specify "-pthread" which we don't need since we always include
157    # this anyway. Removing it here prevents a bunch of duplicate inclusions on
158    # the command line.
159    pass
160  else:
161    cflags.append(flag)
162
163# Output a GN array, the first one is the cflags, the second are the libs. The
164# JSON formatter prints GN compatible lists when everything is a list of
165# strings.
166print json.dumps([includes, cflags, libs, lib_dirs, ldflags])
167