Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
pkg-config.py
Go to the documentation of this file.
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# Additionally, you can specify the option --atleast-version. This will skip
30# the normal outputting of a dictionary and instead print true or false,
31# depending on the return value of pkg-config for the given package.
32
33# If this is run on non-Linux platforms, just return nothing and indicate
34# success. This allows us to "kind of emulate" a Linux build from other
35# platforms.
36if sys.platform.find("linux") == -1:
37 print("[[],[],[],[],[]]")
38 sys.exit(0)
39
40
41def SetConfigPath(options):
42 """Set the PKG_CONFIG_PATH environment variable.
43 This takes into account any sysroot and architecture specification from the
44 options on the given command line."""
45
46 sysroot = options.sysroot
47 if not sysroot:
48 sysroot = ""
49
50 # Compute the library path name based on the architecture.
51 arch = options.arch
52 if sysroot and not arch:
53 print("You must specify an architecture via -a if using a sysroot.")
54 sys.exit(1)
55 if arch == 'x64':
56 libpath = 'lib64'
57 else:
58 libpath = 'lib'
59
60 # Add the sysroot path to the environment's PKG_CONFIG_PATH
61 config_path = sysroot + '/usr/' + libpath + '/pkgconfig'
62 config_path += ':' + sysroot + '/usr/share/pkgconfig'
63 if 'PKG_CONFIG_PATH' in os.environ:
64 os.environ['PKG_CONFIG_PATH'] += ':' + config_path
65 else:
66 os.environ['PKG_CONFIG_PATH'] = config_path
67
68
70 """Returns the prefix from pkg-config where packages are installed.
71 This returned prefix is the one that should be stripped from the beginning of
72 directory names to take into account sysroots."""
73 # Some sysroots, like the Chromium OS ones, may generate paths that are not
74 # relative to the sysroot. For example,
75 # /path/to/chroot/build/x86-generic/usr/lib/pkgconfig/pkg.pc may have all
76 # paths relative to /path/to/chroot (i.e. prefix=/build/x86-generic/usr)
77 # instead of relative to /path/to/chroot/build/x86-generic (i.e prefix=/usr).
78 # To support this correctly, it's necessary to extract the prefix to strip
79 # from pkg-config's |prefix| variable.
80 prefix = subprocess.check_output(
81 ["pkg-config", "--variable=prefix"] + args, env=os.environ)
82 if prefix[-4] == '/usr':
83 return prefix[4:]
84 return prefix
85
86
87def MatchesAnyRegexp(flag, list_of_regexps):
88 """Returns true if the first argument matches any regular expression in the
89 given list."""
90 for regexp in list_of_regexps:
91 if regexp.search(flag) != None:
92 return True
93 return False
94
95
96def RewritePath(path, strip_prefix, sysroot):
97 """Rewrites a path by stripping the prefix and prepending the sysroot."""
98 if os.path.isabs(path) and not path.startswith(sysroot):
99 if path.startswith(strip_prefix):
100 path = path[len(strip_prefix):]
101 path = path.lstrip('/')
102 return os.path.join(sysroot, path)
103 else:
104 return path
105
106
107parser = OptionParser()
108parser.add_option(
109 '-p',
110 action='store',
111 dest='pkg_config',
112 type='string',
113 default='pkg-config')
114parser.add_option('-v', action='append', dest='strip_out', type='string')
115parser.add_option('-s', action='store', dest='sysroot', type='string')
116parser.add_option('-a', action='store', dest='arch', type='string')
117parser.add_option(
118 '--atleast-version', action='store', dest='atleast_version', type='string')
119parser.add_option('--libdir', action='store_true', dest='libdir')
120(options, args) = parser.parse_args()
121
122# Make a list of regular expressions to strip out.
123strip_out = []
124if options.strip_out != None:
125 for regexp in options.strip_out:
126 strip_out.append(re.compile(regexp))
127
128SetConfigPath(options)
129if options.sysroot:
131else:
132 prefix = ''
133
134if options.atleast_version:
135 # When asking for the return value, just run pkg-config and print the return
136 # value, no need to do other work.
137 if not subprocess.call(
138 [options.pkg_config, "--atleast-version=" + options.atleast_version] +
139 args,
140 env=os.environ):
141 print("true")
142 else:
143 print("false")
144 sys.exit(0)
145
146if options.libdir:
147 try:
148 libdir = subprocess.check_output(
149 [options.pkg_config, "--variable=libdir"] + args, env=os.environ)
150 except:
151 print("Error from pkg-config.")
152 sys.exit(1)
153 sys.stdout.write(libdir.strip())
154 sys.exit(0)
155
156try:
157 flag_string = subprocess.check_output(
158 [options.pkg_config, "--cflags", "--libs-only-l", "--libs-only-L"] +
159 args,
160 env=os.environ)
161 # For now just split on spaces to get the args out. This will break if
162 # pkgconfig returns quoted things with spaces in them, but that doesn't seem
163 # to happen in practice.
164 all_flags = flag_string.decode('utf-8').strip().split(' ')
165except:
166 print("Could not run pkg-config.")
167 sys.exit(1)
168
169sysroot = options.sysroot
170if not sysroot:
171 sysroot = ''
172
173includes = []
174cflags = []
175libs = []
176lib_dirs = []
177ldflags = []
178
179for flag in all_flags[:]:
180 if len(flag) == 0 or MatchesAnyRegexp(flag, strip_out):
181 continue
182
183 if flag[:2] == '-l':
184 libs.append(RewritePath(flag[2:], prefix, sysroot))
185 elif flag[:2] == '-L':
186 lib_dirs.append(RewritePath(flag[2:], prefix, sysroot))
187 elif flag[:2] == '-I':
188 includes.append(RewritePath(flag[2:], prefix, sysroot))
189 elif flag[:3] == '-Wl':
190 ldflags.append(flag)
191 elif flag == '-pthread':
192 # Many libs specify "-pthread" which we don't need since we always include
193 # this anyway. Removing it here prevents a bunch of duplicate inclusions on
194 # the command line.
195 pass
196 else:
197 cflags.append(flag)
198
199# Output a GN array, the first one is the cflags, the second are the libs. The
200# JSON formatter prints GN compatible lists when everything is a list of
201# strings.
202print(json.dumps([includes, cflags, libs, lib_dirs, ldflags]))
void print(void *str)
Definition bridge.cpp:126
RewritePath(path, strip_prefix, sysroot)
Definition pkg-config.py:96
SetConfigPath(options)
Definition pkg-config.py:41
MatchesAnyRegexp(flag, list_of_regexps)
Definition pkg-config.py:87
GetPkgConfigPrefixToStrip(args)
Definition pkg-config.py:69