10from optparse
import OptionParser
36if sys.platform.find(
"linux") == -1:
37 print(
"[[],[],[],[],[]]")
42 """Set the PKG_CONFIG_PATH environment variable.
43 This takes into account any sysroot and architecture specification from the
44 options on the given command line."""
46 sysroot = options.sysroot
52 if sysroot
and not arch:
53 print(
"You must specify an architecture via -a if using a sysroot.")
61 config_path = sysroot +
'/usr/' + libpath +
'/pkgconfig'
62 config_path +=
':' + sysroot +
'/usr/share/pkgconfig'
63 if 'PKG_CONFIG_PATH' in os.environ:
64 os.environ[
'PKG_CONFIG_PATH'] +=
':' + config_path
66 os.environ[
'PKG_CONFIG_PATH'] = config_path
70 """Returns the prefix from pkg-config where packages are installed.
71 This returned prefix is the one that should be stripped from the beginning of
72 directory names to take into account sysroots."""
80 prefix = subprocess.check_output(
81 [
"pkg-config",
"--variable=prefix"] + args, env=os.environ)
82 if prefix[-4] ==
'/usr':
88 """Returns true if the first argument matches any regular expression in the
90 for regexp
in list_of_regexps:
91 if regexp.search(flag) !=
None:
97 """Rewrites a path by stripping the prefix and prepending the sysroot."""
98 if os.path.isabs(path)
and not path.startswith(sysroot):
99 if path.startswith(strip_prefix):
100 path = path[len(strip_prefix):]
101 path = path.lstrip(
'/')
102 return os.path.join(sysroot, path)
107parser = OptionParser()
113 default=
'pkg-config')
114parser.add_option(
'-v', action=
'append', dest=
'strip_out', type=
'string')
115parser.add_option(
'-s', action=
'store', dest=
'sysroot', type=
'string')
116parser.add_option(
'-a', action=
'store', dest=
'arch', type=
'string')
118 '--atleast-version', action=
'store', dest=
'atleast_version', type=
'string')
119parser.add_option(
'--libdir', action=
'store_true', dest=
'libdir')
120(options, args) = parser.parse_args()
124if options.strip_out !=
None:
125 for regexp
in options.strip_out:
126 strip_out.append(re.compile(regexp))
134if options.atleast_version:
137 if not subprocess.call(
138 [options.pkg_config,
"--atleast-version=" + options.atleast_version] +
148 libdir = subprocess.check_output(
149 [options.pkg_config,
"--variable=libdir"] + args, env=os.environ)
151 print(
"Error from pkg-config.")
153 sys.stdout.write(libdir.strip())
157 flag_string = subprocess.check_output(
158 [options.pkg_config,
"--cflags",
"--libs-only-l",
"--libs-only-L"] +
164 all_flags = flag_string.decode(
'utf-8').strip().split(
' ')
166 print(
"Could not run pkg-config.")
169sysroot = options.sysroot
179for flag
in all_flags[:]:
184 libs.append(
RewritePath(flag[2:], prefix, sysroot))
185 elif flag[:2] ==
'-L':
186 lib_dirs.append(
RewritePath(flag[2:], prefix, sysroot))
187 elif flag[:2] ==
'-I':
188 includes.append(
RewritePath(flag[2:], prefix, sysroot))
189 elif flag[:3] ==
'-Wl':
191 elif flag ==
'-pthread':
202print(json.dumps([includes, cflags, libs, lib_dirs, ldflags]))
RewritePath(path, strip_prefix, sysroot)
MatchesAnyRegexp(flag, list_of_regexps)
GetPkgConfigPrefixToStrip(args)