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 5 import json 6 import os 7 import subprocess 8 import sys 9 import re 10 from 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 # 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 31 # platforms. 32 if sys.platform.find("linux") == -1: 33 print "[[],[],[],[],[]]" 34 sys.exit(0) 35 36 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.""" 41 42 sysroot = options.sysroot 43 if not sysroot: 44 sysroot = "" 45 46 # Compute the library path name based on the architecture. 47 arch = options.arch 48 if sysroot and not arch: 49 print "You must specify an architecture via -a if using a sysroot." 50 sys.exit(1) 51 if arch == 'x64': 52 libpath = 'lib64' 53 else: 54 libpath = 'lib' 55 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 61 else: 62 os.environ['PKG_CONFIG_PATH'] = config_path 63 64 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, 77 env=os.environ) 78 if prefix[-4] == '/usr': 79 return prefix[4:] 80 return prefix 81 82 83 def MatchesAnyRegexp(flag, list_of_regexps): 84 """Returns true if the first argument matches any regular expression in the 85 given list.""" 86 for regexp in list_of_regexps: 87 if regexp.search(flag) != None: 88 return True 89 return False 90 91 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) 99 else: 100 return path 101 102 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() 110 111 # Make a list of regular expressions to strip out. 112 strip_out = [] 113 if options.strip_out != None: 114 for regexp in options.strip_out: 115 strip_out.append(re.compile(regexp)) 116 117 SetConfigPath(options) 118 if options.sysroot: 119 prefix = GetPkgConfigPrefixToStrip(args) 120 else: 121 prefix = '' 122 123 try: 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(' ') 131 except: 132 print "Could not run pkg-config." 133 sys.exit(1) 134 135 136 sysroot = options.sysroot 137 if not sysroot: 138 sysroot = '' 139 140 includes = [] 141 cflags = [] 142 libs = [] 143 lib_dirs = [] 144 ldflags = [] 145 146 for flag in all_flags[:]: 147 if len(flag) == 0 or MatchesAnyRegexp(flag, strip_out): 148 continue; 149 150 if flag[:2] == '-l': 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': 157 ldflags.append(flag) 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 161 # the command line. 162 pass 163 else: 164 cflags.append(flag) 165 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 168 # strings. 169 print json.dumps([includes, cflags, libs, lib_dirs, ldflags]) 170