Chromium Code Reviews| 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 This takes into account any sysroot and architecture specification from the | 46 This takes into account any sysroot and architecture specification from the |
| 47 options on the given command line.""" | 47 options on the given command line.""" |
| 48 | 48 |
| 49 sysroot = options.sysroot | 49 sysroot = options.sysroot |
| 50 assert sysroot | 50 assert sysroot |
| 51 | 51 |
| 52 # Compute the library path name based on the architecture. | 52 # Compute the library path name based on the architecture. |
| 53 arch = options.arch | 53 arch = options.arch |
| 54 if sysroot and not arch: | 54 if sysroot and not arch: |
| 55 print "You must specify an architecture via -a if using a sysroot." | 55 print "You must specify an architecture via -a if using a sysroot." |
| 56 sys.exit(1) | 56 sys.exit(1) |
| 57 | 57 |
| 58 # Add the sysroot path to the environment's PKG_CONFIG_PATH | 58 # Add the sysroot path to the environment's PKG_CONFIG_LIBDIR |
|
Lei Zhang
2016/01/12 23:01:50
More like overwrite than add.
Sam Clegg
2016/01/12 23:25:12
Done
| |
| 59 config_path = sysroot + '/usr/' + options.system_libdir + '/pkgconfig' | 59 config_path = sysroot + '/usr/' + options.system_libdir + '/pkgconfig' |
| 60 config_path += ':' + sysroot + '/usr/share/pkgconfig' | 60 config_path += ':' + sysroot + '/usr/share/pkgconfig' |
| 61 if 'PKG_CONFIG_PATH' in os.environ: | 61 os.environ['PKG_CONFIG_LIBDIR'] = config_path |
| 62 os.environ['PKG_CONFIG_PATH'] += ':' + config_path | 62 return config_path |
| 63 else: | |
| 64 os.environ['PKG_CONFIG_PATH'] = config_path | |
| 65 | 63 |
| 66 | 64 |
| 67 def GetPkgConfigPrefixToStrip(args): | 65 def GetPkgConfigPrefixToStrip(args): |
| 68 """Returns the prefix from pkg-config where packages are installed. | 66 """Returns the prefix from pkg-config where packages are installed. |
| 69 This returned prefix is the one that should be stripped from the beginning of | 67 This returned prefix is the one that should be stripped from the beginning of |
| 70 directory names to take into account sysroots.""" | 68 directory names to take into account sysroots.""" |
| 71 # Some sysroots, like the Chromium OS ones, may generate paths that are not | 69 # Some sysroots, like the Chromium OS ones, may generate paths that are not |
| 72 # relative to the sysroot. For example, | 70 # relative to the sysroot. For example, |
| 73 # /path/to/chroot/build/x86-generic/usr/lib/pkgconfig/pkg.pc may have all | 71 # /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) | 72 # paths relative to /path/to/chroot (i.e. prefix=/build/x86-generic/usr) |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 parser.add_option('--libdir', action='store_true', dest='libdir') | 122 parser.add_option('--libdir', action='store_true', dest='libdir') |
| 125 (options, args) = parser.parse_args() | 123 (options, args) = parser.parse_args() |
| 126 | 124 |
| 127 # Make a list of regular expressions to strip out. | 125 # Make a list of regular expressions to strip out. |
| 128 strip_out = [] | 126 strip_out = [] |
| 129 if options.strip_out != None: | 127 if options.strip_out != None: |
| 130 for regexp in options.strip_out: | 128 for regexp in options.strip_out: |
| 131 strip_out.append(re.compile(regexp)) | 129 strip_out.append(re.compile(regexp)) |
| 132 | 130 |
| 133 if options.sysroot: | 131 if options.sysroot: |
| 134 SetConfigPath(options) | 132 libdir = SetConfigPath(options) |
| 135 if options.debug: | 133 if options.debug: |
| 136 sys.stderr.write('PKG_CONFIG_PATH=%s\n' % os.environ['PKG_CONFIG_PATH']) | 134 sys.stderr.write('PKG_CONFIG_LIBDIR=%s\n' % libdir) |
| 137 prefix = GetPkgConfigPrefixToStrip(args) | 135 prefix = GetPkgConfigPrefixToStrip(args) |
| 138 else: | 136 else: |
| 139 prefix = '' | 137 prefix = '' |
| 140 | 138 |
| 141 if options.atleast_version: | 139 if options.atleast_version: |
| 142 # When asking for the return value, just run pkg-config and print the return | 140 # When asking for the return value, just run pkg-config and print the return |
| 143 # value, no need to do other work. | 141 # value, no need to do other work. |
| 144 if not subprocess.call([options.pkg_config, | 142 if not subprocess.call([options.pkg_config, |
| 145 "--atleast-version=" + options.atleast_version] + | 143 "--atleast-version=" + options.atleast_version] + |
| 146 args): | 144 args): |
| 147 print "true" | 145 print "true" |
| 148 else: | 146 else: |
| 149 print "false" | 147 print "false" |
| 150 return 0 | 148 return 0 |
| 151 | 149 |
| 152 if options.libdir: | 150 if options.libdir: |
| 151 cmd = [options.pkg_config, "--variable=libdir"] + args | |
| 152 if options.debug: | |
| 153 sys.stderr.write('Running: %s\n' % cmd) | |
| 153 try: | 154 try: |
| 154 libdir = subprocess.check_output([options.pkg_config, | 155 libdir = subprocess.check_output(cmd) |
| 155 "--variable=libdir"] + | |
| 156 args) | |
| 157 except: | 156 except: |
| 158 print "Error from pkg-config." | 157 print "Error from pkg-config." |
| 159 return 1 | 158 return 1 |
| 160 sys.stdout.write(libdir.strip()) | 159 sys.stdout.write(libdir.strip()) |
| 161 return 0 | 160 return 0 |
| 162 | 161 |
| 162 cmd = [options.pkg_config, "--cflags", "--libs"] + args | |
| 163 if options.debug: | |
| 164 sys.stderr.write('Running: %s\n' % ' '.join(cmd)) | |
| 165 | |
| 163 try: | 166 try: |
| 164 flag_string = subprocess.check_output( | 167 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: | 168 except: |
| 172 print "Could not run pkg-config." | 169 sys.stderr.write('Could not run pkg-config.\n') |
| 173 return 1 | 170 return 1 |
| 174 | 171 |
| 172 # For now just split on spaces to get the args out. This will break if | |
| 173 # pkgconfig returns quoted things with spaces in them, but that doesn't seem | |
| 174 # to happen in practice. | |
| 175 all_flags = flag_string.strip().split(' ') | |
| 176 | |
| 175 | 177 |
| 176 sysroot = options.sysroot | 178 sysroot = options.sysroot |
| 177 if not sysroot: | 179 if not sysroot: |
| 178 sysroot = '' | 180 sysroot = '' |
| 179 | 181 |
| 180 includes = [] | 182 includes = [] |
| 181 cflags = [] | 183 cflags = [] |
| 182 libs = [] | 184 libs = [] |
| 183 lib_dirs = [] | 185 lib_dirs = [] |
| 184 ldflags = [] | 186 ldflags = [] |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 205 | 207 |
| 206 # Output a GN array, the first one is the cflags, the second are the libs. The | 208 # 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 | 209 # JSON formatter prints GN compatible lists when everything is a list of |
| 208 # strings. | 210 # strings. |
| 209 print json.dumps([includes, cflags, libs, lib_dirs, ldflags]) | 211 print json.dumps([includes, cflags, libs, lib_dirs, ldflags]) |
| 210 return 0 | 212 return 0 |
| 211 | 213 |
| 212 | 214 |
| 213 if __name__ == '__main__': | 215 if __name__ == '__main__': |
| 214 sys.exit(main()) | 216 sys.exit(main()) |
| OLD | NEW |