Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: build/config/linux/pkg-config.py

Issue 116803003: Add support for sysroots to GN's pkg-config. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « build/config/compiler/BUILD.gn ('k') | build/config/linux/pkg_config.gni » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import json 5 import json
6 import os
6 import subprocess 7 import subprocess
7 import sys 8 import sys
8 import re 9 import re
9 from optparse import OptionParser 10 from optparse import OptionParser
10 11
11 # This script runs pkg-config, optionally filtering out some results, and 12 # This script runs pkg-config, optionally filtering out some results, and
12 # returns the result. 13 # returns the result.
13 # 14 #
14 # The result will be [ <includes>, <cflags>, <libs>, <lib_dirs> ] where each 15 # The result will be [ <includes>, <cflags>, <libs>, <lib_dirs> ] where each
15 # member is itself a list of strings. 16 # member is itself a list of strings.
16 # 17 #
17 # You can filter out matches using "-v <regexp>" where all results from 18 # You can filter out matches using "-v <regexp>" where all results from
18 # pkgconfig matching the given regular expression will be ignored. You can 19 # pkgconfig matching the given regular expression will be ignored. You can
19 # specify more than one regular expression my specifying "-v" more than once. 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".
20 28
21 # If this is run on non-Linux platforms, just return nothing and indicate 29 # If this is run on non-Linux platforms, just return nothing and indicate
22 # success. This allows us to "kind of emulate" a Linux build from other 30 # success. This allows us to "kind of emulate" a Linux build from other
23 # platforms. 31 # platforms.
24 if sys.platform.find("linux") == -1: 32 if sys.platform.find("linux") == -1:
25 print "[[],[],[]]" 33 print "[[],[],[]]"
26 sys.exit(0) 34 sys.exit(0)
27 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
28 parser = OptionParser() 103 parser = OptionParser()
29 parser.add_option('-v', action='append', dest='strip_out', type='string') 104 parser.add_option('-v', action='append', dest='strip_out', type='string')
105 parser.add_option('-s', action='store', dest='sysroot', type='string')
106 parser.add_option('-a', action='store', dest='arch', type='string')
30 (options, args) = parser.parse_args() 107 (options, args) = parser.parse_args()
31 108
32 # Make a list of regular expressions to strip out. 109 # Make a list of regular expressions to strip out.
33 strip_out = [] 110 strip_out = []
34 if options.strip_out != None: 111 if options.strip_out != None:
35 for regexp in options.strip_out: 112 for regexp in options.strip_out:
36 strip_out.append(re.compile(regexp)) 113 strip_out.append(re.compile(regexp))
37 114
115 SetConfigPath(options)
116 if options.sysroot:
117 prefix = GetPkgConfigPrefixToStrip(args)
118 else:
119 prefix = ''
120
38 try: 121 try:
39 flag_string = subprocess.check_output(["pkg-config", "--cflags", "--libs"] + 122 flag_string = subprocess.check_output(["pkg-config", "--cflags", "--libs"] +
40 args) 123 args, env=os.environ)
41 # For now just split on spaces to get the args out. This will break if 124 # For now just split on spaces to get the args out. This will break if
42 # pkgconfig returns quoted things with spaces in them, but that doesn't seem 125 # pkgconfig returns quoted things with spaces in them, but that doesn't seem
43 # to happen in practice. 126 # to happen in practice.
44 all_flags = flag_string.strip().split(' ') 127 all_flags = flag_string.strip().split(' ')
45 except: 128 except:
46 print "Could not run pkg-config." 129 print "Could not run pkg-config."
47 sys.exit(1) 130 sys.exit(1)
48 131
132
133 sysroot = options.sysroot
134 if not sysroot:
135 sysroot = ''
136
49 includes = [] 137 includes = []
50 cflags = [] 138 cflags = []
51 libs = [] 139 libs = []
52 lib_dirs = [] 140 lib_dirs = []
53 141
54 def MatchesAnyRegexp(flag, list_of_regexps):
55 for regexp in list_of_regexps:
56 if regexp.search(flag) != None:
57 return True
58 return False
59
60 for flag in all_flags[:]: 142 for flag in all_flags[:]:
61 if len(flag) == 0 or MatchesAnyRegexp(flag, strip_out): 143 if len(flag) == 0 or MatchesAnyRegexp(flag, strip_out):
62 continue; 144 continue;
63 145
64 if flag[:2] == '-l': 146 if flag[:2] == '-l':
65 libs.append(flag[2:]) 147 libs.append(RewritePath(flag[2:], prefix, sysroot))
66 if flag[:2] == '-L': 148 if flag[:2] == '-L':
67 lib_dirs.append(flag[2:]) 149 lib_dirs.append(RewritePath(flag[2:], prefix, sysroot))
68 elif flag[:2] == '-I': 150 elif flag[:2] == '-I':
69 includes.append(flag[2:]) 151 includes.append(RewritePath(flag[2:], prefix, sysroot))
70 else: 152 else:
71 cflags.append(flag) 153 cflags.append(flag)
72 154
73 # Output a GN array, the first one is the cflags, the second are the libs. The 155 # Output a GN array, the first one is the cflags, the second are the libs. The
74 # JSON formatter prints GN compatible lists when everything is a list of 156 # JSON formatter prints GN compatible lists when everything is a list of
75 # strings. 157 # strings.
76 print json.dumps([includes, cflags, libs, lib_dirs]) 158 print json.dumps([includes, cflags, libs, lib_dirs])
OLDNEW
« no previous file with comments | « build/config/compiler/BUILD.gn ('k') | build/config/linux/pkg_config.gni » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698