OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import json | 6 import json |
7 import os | 7 import os |
8 import subprocess | 8 import subprocess |
9 import sys | 9 import sys |
10 import re | 10 import re |
(...skipping 24 matching lines...) Expand all Loading... |
35 # variable to this script with the "--system_libdir <system_libdir>" flag. If no | 35 # variable to this script with the "--system_libdir <system_libdir>" flag. If no |
36 # flag is provided, then pkgconfig files are assumed to come from | 36 # flag is provided, then pkgconfig files are assumed to come from |
37 # <systemroot>/usr/lib/pkgconfig. | 37 # <systemroot>/usr/lib/pkgconfig. |
38 # | 38 # |
39 # Additionally, you can specify the option --atleast-version. This will skip | 39 # Additionally, you can specify the option --atleast-version. This will skip |
40 # the normal outputting of a dictionary and instead print true or false, | 40 # the normal outputting of a dictionary and instead print true or false, |
41 # depending on the return value of pkg-config for the given package. | 41 # depending on the return value of pkg-config for the given package. |
42 | 42 |
43 | 43 |
44 def SetConfigPath(options): | 44 def SetConfigPath(options): |
45 """Set the PKG_CONFIG_PATH environment variable. | 45 """Set the PKG_CONFIG_LIBDIR environment variable. |
| 46 |
46 This takes into account any sysroot and architecture specification from the | 47 This takes into account any sysroot and architecture specification from the |
47 options on the given command line.""" | 48 options on the given command line. |
| 49 """ |
48 | 50 |
49 sysroot = options.sysroot | 51 sysroot = options.sysroot |
50 assert sysroot | 52 assert sysroot |
51 | 53 |
52 # Compute the library path name based on the architecture. | 54 # Compute the library path name based on the architecture. |
53 arch = options.arch | 55 arch = options.arch |
54 if sysroot and not arch: | 56 if sysroot and not arch: |
55 print "You must specify an architecture via -a if using a sysroot." | 57 print "You must specify an architecture via -a if using a sysroot." |
56 sys.exit(1) | 58 sys.exit(1) |
57 | 59 |
58 # Add the sysroot path to the environment's PKG_CONFIG_PATH | 60 libdir = sysroot + '/usr/' + options.system_libdir + '/pkgconfig' |
59 config_path = sysroot + '/usr/' + options.system_libdir + '/pkgconfig' | 61 libdir += ':' + sysroot + '/usr/share/pkgconfig' |
60 config_path += ':' + sysroot + '/usr/share/pkgconfig' | 62 os.environ['PKG_CONFIG_LIBDIR'] = libdir |
61 if 'PKG_CONFIG_PATH' in os.environ: | 63 return libdir |
62 os.environ['PKG_CONFIG_PATH'] += ':' + config_path | |
63 else: | |
64 os.environ['PKG_CONFIG_PATH'] = config_path | |
65 | 64 |
66 | 65 |
67 def GetPkgConfigPrefixToStrip(args): | 66 def GetPkgConfigPrefixToStrip(args): |
68 """Returns the prefix from pkg-config where packages are installed. | 67 """Returns the prefix from pkg-config where packages are installed. |
| 68 |
69 This returned prefix is the one that should be stripped from the beginning of | 69 This returned prefix is the one that should be stripped from the beginning of |
70 directory names to take into account sysroots.""" | 70 directory names to take into account sysroots. |
| 71 """ |
71 # Some sysroots, like the Chromium OS ones, may generate paths that are not | 72 # Some sysroots, like the Chromium OS ones, may generate paths that are not |
72 # relative to the sysroot. For example, | 73 # relative to the sysroot. For example, |
73 # /path/to/chroot/build/x86-generic/usr/lib/pkgconfig/pkg.pc may have all | 74 # /path/to/chroot/build/x86-generic/usr/lib/pkgconfig/pkg.pc may have all |
74 # paths relative to /path/to/chroot (i.e. prefix=/build/x86-generic/usr) | 75 # paths relative to /path/to/chroot (i.e. prefix=/build/x86-generic/usr) |
75 # instead of relative to /path/to/chroot/build/x86-generic (i.e prefix=/usr). | 76 # instead of relative to /path/to/chroot/build/x86-generic (i.e prefix=/usr). |
76 # To support this correctly, it's necessary to extract the prefix to strip | 77 # To support this correctly, it's necessary to extract the prefix to strip |
77 # from pkg-config's |prefix| variable. | 78 # from pkg-config's |prefix| variable. |
78 prefix = subprocess.check_output(["pkg-config", "--variable=prefix"] + args, | 79 prefix = subprocess.check_output(["pkg-config", "--variable=prefix"] + args, |
79 env=os.environ) | 80 env=os.environ) |
80 if prefix[-4] == '/usr': | 81 if prefix[-4] == '/usr': |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 parser.add_option('--libdir', action='store_true', dest='libdir') | 125 parser.add_option('--libdir', action='store_true', dest='libdir') |
125 (options, args) = parser.parse_args() | 126 (options, args) = parser.parse_args() |
126 | 127 |
127 # Make a list of regular expressions to strip out. | 128 # Make a list of regular expressions to strip out. |
128 strip_out = [] | 129 strip_out = [] |
129 if options.strip_out != None: | 130 if options.strip_out != None: |
130 for regexp in options.strip_out: | 131 for regexp in options.strip_out: |
131 strip_out.append(re.compile(regexp)) | 132 strip_out.append(re.compile(regexp)) |
132 | 133 |
133 if options.sysroot: | 134 if options.sysroot: |
134 SetConfigPath(options) | 135 libdir = SetConfigPath(options) |
135 if options.debug: | 136 if options.debug: |
136 sys.stderr.write('PKG_CONFIG_PATH=%s\n' % os.environ['PKG_CONFIG_PATH']) | 137 sys.stderr.write('PKG_CONFIG_LIBDIR=%s\n' % libdir) |
137 prefix = GetPkgConfigPrefixToStrip(args) | 138 prefix = GetPkgConfigPrefixToStrip(args) |
138 else: | 139 else: |
139 prefix = '' | 140 prefix = '' |
140 | 141 |
141 if options.atleast_version: | 142 if options.atleast_version: |
142 # When asking for the return value, just run pkg-config and print the return | 143 # When asking for the return value, just run pkg-config and print the return |
143 # value, no need to do other work. | 144 # value, no need to do other work. |
144 if not subprocess.call([options.pkg_config, | 145 if not subprocess.call([options.pkg_config, |
145 "--atleast-version=" + options.atleast_version] + | 146 "--atleast-version=" + options.atleast_version] + |
146 args): | 147 args): |
147 print "true" | 148 print "true" |
148 else: | 149 else: |
149 print "false" | 150 print "false" |
150 return 0 | 151 return 0 |
151 | 152 |
152 if options.libdir: | 153 if options.libdir: |
| 154 cmd = [options.pkg_config, "--variable=libdir"] + args |
| 155 if options.debug: |
| 156 sys.stderr.write('Running: %s\n' % cmd) |
153 try: | 157 try: |
154 libdir = subprocess.check_output([options.pkg_config, | 158 libdir = subprocess.check_output(cmd) |
155 "--variable=libdir"] + | |
156 args) | |
157 except: | 159 except: |
158 print "Error from pkg-config." | 160 print "Error from pkg-config." |
159 return 1 | 161 return 1 |
160 sys.stdout.write(libdir.strip()) | 162 sys.stdout.write(libdir.strip()) |
161 return 0 | 163 return 0 |
162 | 164 |
| 165 cmd = [options.pkg_config, "--cflags", "--libs"] + args |
| 166 if options.debug: |
| 167 sys.stderr.write('Running: %s\n' % ' '.join(cmd)) |
| 168 |
163 try: | 169 try: |
164 flag_string = subprocess.check_output( | 170 flag_string = subprocess.check_output(cmd) |
165 [ options.pkg_config, "--cflags", "--libs" ] + | |
166 args) | |
167 # For now just split on spaces to get the args out. This will break if | |
168 # pkgconfig returns quoted things with spaces in them, but that doesn't seem | |
169 # to happen in practice. | |
170 all_flags = flag_string.strip().split(' ') | |
171 except: | 171 except: |
172 print "Could not run pkg-config." | 172 sys.stderr.write('Could not run pkg-config.\n') |
173 return 1 | 173 return 1 |
174 | 174 |
| 175 # For now just split on spaces to get the args out. This will break if |
| 176 # pkgconfig returns quoted things with spaces in them, but that doesn't seem |
| 177 # to happen in practice. |
| 178 all_flags = flag_string.strip().split(' ') |
| 179 |
175 | 180 |
176 sysroot = options.sysroot | 181 sysroot = options.sysroot |
177 if not sysroot: | 182 if not sysroot: |
178 sysroot = '' | 183 sysroot = '' |
179 | 184 |
180 includes = [] | 185 includes = [] |
181 cflags = [] | 186 cflags = [] |
182 libs = [] | 187 libs = [] |
183 lib_dirs = [] | 188 lib_dirs = [] |
184 ldflags = [] | 189 ldflags = [] |
(...skipping 20 matching lines...) Expand all Loading... |
205 | 210 |
206 # Output a GN array, the first one is the cflags, the second are the libs. The | 211 # Output a GN array, the first one is the cflags, the second are the libs. The |
207 # JSON formatter prints GN compatible lists when everything is a list of | 212 # JSON formatter prints GN compatible lists when everything is a list of |
208 # strings. | 213 # strings. |
209 print json.dumps([includes, cflags, libs, lib_dirs, ldflags]) | 214 print json.dumps([includes, cflags, libs, lib_dirs, ldflags]) |
210 return 0 | 215 return 0 |
211 | 216 |
212 | 217 |
213 if __name__ == '__main__': | 218 if __name__ == '__main__': |
214 sys.exit(main()) | 219 sys.exit(main()) |
OLD | NEW |