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.
10 from optparse import OptionParser
12 # This script runs pkg-config, optionally filtering out some results, and
15 # The result will be [ <includes>, <cflags>, <libs>, <lib_dirs>, <ldflags> ]
16 # where each member is itself a list of strings.
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.
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.
26 # When using a sysroot, you must also specify the architecture via
27 # "-a <arch>" where arch is either "x86" or "x64".
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
32 if sys.platform.find("linux") == -1:
33 print "[[],[],[],[],[]]"
37 def 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."""
42 sysroot = options.sysroot
46 # Compute the library path name based on the architecture.
48 if sysroot and not arch:
49 print "You must specify an architecture via -a if using a sysroot."
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
62 os.environ['PKG_CONFIG_PATH'] = config_path
65 def 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,
78 if prefix[-4] == '/usr':
83 def MatchesAnyRegexp(flag, list_of_regexps):
84 """Returns true if the first argument matches any regular expression in the
86 for regexp in list_of_regexps:
87 if regexp.search(flag) != None:
92 def 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)
103 parser = OptionParser()
104 parser.add_option('-p', action='store', dest='pkg_config', type='string',
105 default='pkg-config')
106 parser.add_option('-v', action='append', dest='strip_out', type='string')
107 parser.add_option('-s', action='store', dest='sysroot', type='string')
108 parser.add_option('-a', action='store', dest='arch', type='string')
109 (options, args) = parser.parse_args()
111 # Make a list of regular expressions to strip out.
113 if options.strip_out != None:
114 for regexp in options.strip_out:
115 strip_out.append(re.compile(regexp))
117 SetConfigPath(options)
119 prefix = GetPkgConfigPrefixToStrip(args)
124 flag_string = subprocess.check_output(
125 [ options.pkg_config, "--cflags", "--libs-only-l", "--libs-only-L" ] +
126 args, env=os.environ)
127 # For now just split on spaces to get the args out. This will break if
128 # pkgconfig returns quoted things with spaces in them, but that doesn't seem
129 # to happen in practice.
130 all_flags = flag_string.strip().split(' ')
132 print "Could not run pkg-config."
136 sysroot = options.sysroot
146 for flag in all_flags[:]:
147 if len(flag) == 0 or MatchesAnyRegexp(flag, strip_out):
151 libs.append(RewritePath(flag[2:], prefix, sysroot))
152 elif flag[:2] == '-L':
153 lib_dirs.append(RewritePath(flag[2:], prefix, sysroot))
154 elif flag[:2] == '-I':
155 includes.append(RewritePath(flag[2:], prefix, sysroot))
156 elif flag[:3] == '-Wl':
158 elif flag == '-pthread':
159 # Many libs specify "-pthread" which we don't need since we always include
160 # this anyway. Removing it here prevents a bunch of duplicate inclusions on
166 # Output a GN array, the first one is the cflags, the second are the libs. The
167 # JSON formatter prints GN compatible lists when everything is a list of
169 print json.dumps([includes, cflags, libs, lib_dirs, ldflags])