| OLD | NEW |
| 1 #!/usr/bin/env python |
| 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 2 # 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 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 4 | 5 |
| 5 import json | 6 import json |
| 6 import os | 7 import os |
| 7 import subprocess | 8 import subprocess |
| 8 import sys | 9 import sys |
| 9 import re | 10 import re |
| 10 from optparse import OptionParser | 11 from optparse import OptionParser |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 # specify the 'lib' or 'lib64' of the pkgconfig path by defining the | 33 # specify the 'lib' or 'lib64' of the pkgconfig path by defining the |
| 33 # 'system_libdir' variable in the args.gn file. pkg_config.gni communicates this | 34 # 'system_libdir' variable in the args.gn file. pkg_config.gni communicates this |
| 34 # 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 |
| 35 # flag is provided, then pkgconfig files are assumed to come from | 36 # flag is provided, then pkgconfig files are assumed to come from |
| 36 # <systemroot>/usr/lib/pkgconfig. | 37 # <systemroot>/usr/lib/pkgconfig. |
| 37 # | 38 # |
| 38 # Additionally, you can specify the option --atleast-version. This will skip | 39 # Additionally, you can specify the option --atleast-version. This will skip |
| 39 # 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, |
| 40 # 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. |
| 41 | 42 |
| 42 # If this is run on non-Linux platforms, just return nothing and indicate | |
| 43 # success. This allows us to "kind of emulate" a Linux build from other | |
| 44 # platforms. | |
| 45 if sys.platform.find("linux") == -1: | |
| 46 print "[[],[],[],[],[]]" | |
| 47 sys.exit(0) | |
| 48 | |
| 49 | 43 |
| 50 def SetConfigPath(options): | 44 def SetConfigPath(options): |
| 51 """Set the PKG_CONFIG_PATH environment variable. | 45 """Set the PKG_CONFIG_PATH environment variable. |
| 52 This takes into account any sysroot and architecture specification from the | 46 This takes into account any sysroot and architecture specification from the |
| 53 options on the given command line.""" | 47 options on the given command line.""" |
| 54 | 48 |
| 55 sysroot = options.sysroot | 49 sysroot = options.sysroot |
| 56 assert sysroot | 50 assert sysroot |
| 57 | 51 |
| 58 # Compute the library path name based on the architecture. | 52 # Compute the library path name based on the architecture. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 """Rewrites a path by stripping the prefix and prepending the sysroot.""" | 95 """Rewrites a path by stripping the prefix and prepending the sysroot.""" |
| 102 if os.path.isabs(path) and not path.startswith(sysroot): | 96 if os.path.isabs(path) and not path.startswith(sysroot): |
| 103 if path.startswith(strip_prefix): | 97 if path.startswith(strip_prefix): |
| 104 path = path[len(strip_prefix):] | 98 path = path[len(strip_prefix):] |
| 105 path = path.lstrip('/') | 99 path = path.lstrip('/') |
| 106 return os.path.join(sysroot, path) | 100 return os.path.join(sysroot, path) |
| 107 else: | 101 else: |
| 108 return path | 102 return path |
| 109 | 103 |
| 110 | 104 |
| 111 parser = OptionParser() | 105 def main(): |
| 112 parser.add_option('-p', action='store', dest='pkg_config', type='string', | 106 # If this is run on non-Linux platforms, just return nothing and indicate |
| 113 default='pkg-config') | 107 # success. This allows us to "kind of emulate" a Linux build from other |
| 114 parser.add_option('-v', action='append', dest='strip_out', type='string') | 108 # platforms. |
| 115 parser.add_option('-s', action='store', dest='sysroot', type='string') | 109 if "linux" not in sys.platform: |
| 116 parser.add_option('-a', action='store', dest='arch', type='string') | 110 print "[[],[],[],[],[]]" |
| 117 parser.add_option('--system_libdir', action='store', dest='system_libdir', | 111 return 0 |
| 118 type='string', default='lib') | |
| 119 parser.add_option('--atleast-version', action='store', | |
| 120 dest='atleast_version', type='string') | |
| 121 parser.add_option('--libdir', action='store_true', dest='libdir') | |
| 122 (options, args) = parser.parse_args() | |
| 123 | 112 |
| 124 # Make a list of regular expressions to strip out. | 113 parser = OptionParser() |
| 125 strip_out = [] | 114 parser.add_option('-d', '--debug', action='store_true') |
| 126 if options.strip_out != None: | 115 parser.add_option('-p', action='store', dest='pkg_config', type='string', |
| 127 for regexp in options.strip_out: | 116 default='pkg-config') |
| 128 strip_out.append(re.compile(regexp)) | 117 parser.add_option('-v', action='append', dest='strip_out', type='string') |
| 118 parser.add_option('-s', action='store', dest='sysroot', type='string') |
| 119 parser.add_option('-a', action='store', dest='arch', type='string') |
| 120 parser.add_option('--system_libdir', action='store', dest='system_libdir', |
| 121 type='string', default='lib') |
| 122 parser.add_option('--atleast-version', action='store', |
| 123 dest='atleast_version', type='string') |
| 124 parser.add_option('--libdir', action='store_true', dest='libdir') |
| 125 (options, args) = parser.parse_args() |
| 129 | 126 |
| 130 if options.sysroot: | 127 # Make a list of regular expressions to strip out. |
| 131 SetConfigPath(options) | 128 strip_out = [] |
| 132 prefix = GetPkgConfigPrefixToStrip(args) | 129 if options.strip_out != None: |
| 133 else: | 130 for regexp in options.strip_out: |
| 134 prefix = '' | 131 strip_out.append(re.compile(regexp)) |
| 135 | 132 |
| 136 if options.atleast_version: | 133 if options.sysroot: |
| 137 # When asking for the return value, just run pkg-config and print the return | 134 SetConfigPath(options) |
| 138 # value, no need to do other work. | 135 if options.debug: |
| 139 if not subprocess.call([options.pkg_config, | 136 sys.stderr.write('PKG_CONFIG_PATH=%s\n' % os.environ['PKG_CONFIG_PATH']) |
| 140 "--atleast-version=" + options.atleast_version] + | 137 prefix = GetPkgConfigPrefixToStrip(args) |
| 141 args, | |
| 142 env=os.environ): | |
| 143 print "true" | |
| 144 else: | 138 else: |
| 145 print "false" | 139 prefix = '' |
| 146 sys.exit(0) | |
| 147 | 140 |
| 148 if options.libdir: | 141 if options.atleast_version: |
| 142 # When asking for the return value, just run pkg-config and print the return |
| 143 # value, no need to do other work. |
| 144 if not subprocess.call([options.pkg_config, |
| 145 "--atleast-version=" + options.atleast_version] + |
| 146 args): |
| 147 print "true" |
| 148 else: |
| 149 print "false" |
| 150 return 0 |
| 151 |
| 152 if options.libdir: |
| 153 try: |
| 154 libdir = subprocess.check_output([options.pkg_config, |
| 155 "--variable=libdir"] + |
| 156 args) |
| 157 except: |
| 158 print "Error from pkg-config." |
| 159 return 1 |
| 160 sys.stdout.write(libdir.strip()) |
| 161 return 0 |
| 162 |
| 149 try: | 163 try: |
| 150 libdir = subprocess.check_output([options.pkg_config, | 164 flag_string = subprocess.check_output( |
| 151 "--variable=libdir"] + | 165 [ options.pkg_config, "--cflags", "--libs" ] + |
| 152 args, | 166 args) |
| 153 env=os.environ) | 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(' ') |
| 154 except: | 171 except: |
| 155 print "Error from pkg-config." | 172 print "Could not run pkg-config." |
| 156 sys.exit(1) | 173 return 1 |
| 157 sys.stdout.write(libdir.strip()) | |
| 158 sys.exit(0) | |
| 159 | |
| 160 try: | |
| 161 flag_string = subprocess.check_output( | |
| 162 [ options.pkg_config, "--cflags", "--libs" ] + | |
| 163 args, env=os.environ) | |
| 164 # For now just split on spaces to get the args out. This will break if | |
| 165 # pkgconfig returns quoted things with spaces in them, but that doesn't seem | |
| 166 # to happen in practice. | |
| 167 all_flags = flag_string.strip().split(' ') | |
| 168 except: | |
| 169 print "Could not run pkg-config." | |
| 170 sys.exit(1) | |
| 171 | 174 |
| 172 | 175 |
| 173 sysroot = options.sysroot | 176 sysroot = options.sysroot |
| 174 if not sysroot: | 177 if not sysroot: |
| 175 sysroot = '' | 178 sysroot = '' |
| 176 | 179 |
| 177 includes = [] | 180 includes = [] |
| 178 cflags = [] | 181 cflags = [] |
| 179 libs = [] | 182 libs = [] |
| 180 lib_dirs = [] | 183 lib_dirs = [] |
| 181 ldflags = [] | 184 ldflags = [] |
| 182 | 185 |
| 183 for flag in all_flags[:]: | 186 for flag in all_flags[:]: |
| 184 if len(flag) == 0 or MatchesAnyRegexp(flag, strip_out): | 187 if len(flag) == 0 or MatchesAnyRegexp(flag, strip_out): |
| 185 continue; | 188 continue; |
| 186 | 189 |
| 187 if flag[:2] == '-l': | 190 if flag[:2] == '-l': |
| 188 libs.append(RewritePath(flag[2:], prefix, sysroot)) | 191 libs.append(RewritePath(flag[2:], prefix, sysroot)) |
| 189 elif flag[:2] == '-L': | 192 elif flag[:2] == '-L': |
| 190 lib_dirs.append(RewritePath(flag[2:], prefix, sysroot)) | 193 lib_dirs.append(RewritePath(flag[2:], prefix, sysroot)) |
| 191 elif flag[:2] == '-I': | 194 elif flag[:2] == '-I': |
| 192 includes.append(RewritePath(flag[2:], prefix, sysroot)) | 195 includes.append(RewritePath(flag[2:], prefix, sysroot)) |
| 193 elif flag[:3] == '-Wl': | 196 elif flag[:3] == '-Wl': |
| 194 ldflags.append(flag) | 197 ldflags.append(flag) |
| 195 elif flag == '-pthread': | 198 elif flag == '-pthread': |
| 196 # Many libs specify "-pthread" which we don't need since we always include | 199 # Many libs specify "-pthread" which we don't need since we always include |
| 197 # this anyway. Removing it here prevents a bunch of duplicate inclusions on | 200 # this anyway. Removing it here prevents a bunch of duplicate inclusions |
| 198 # the command line. | 201 # on the command line. |
| 199 pass | 202 pass |
| 200 else: | 203 else: |
| 201 cflags.append(flag) | 204 cflags.append(flag) |
| 202 | 205 |
| 203 # Output a GN array, the first one is the cflags, the second are the libs. The | 206 # Output a GN array, the first one is the cflags, the second are the libs. The |
| 204 # JSON formatter prints GN compatible lists when everything is a list of | 207 # JSON formatter prints GN compatible lists when everything is a list of |
| 205 # strings. | 208 # strings. |
| 206 print json.dumps([includes, cflags, libs, lib_dirs, ldflags]) | 209 print json.dumps([includes, cflags, libs, lib_dirs, ldflags]) |
| 210 return 0 |
| 211 |
| 212 |
| 213 if __name__ == '__main__': |
| 214 sys.exit(main()) |
| OLD | NEW |