xref: /aosp_15_r20/external/angle/build/config/linux/pkg-config.py (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*8975f5c5SAndroid Build Coastguard Worker# Copyright 2013 The Chromium Authors
3*8975f5c5SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker# found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker
6*8975f5c5SAndroid Build Coastguard Worker
7*8975f5c5SAndroid Build Coastguard Workerimport json
8*8975f5c5SAndroid Build Coastguard Workerimport os
9*8975f5c5SAndroid Build Coastguard Workerimport subprocess
10*8975f5c5SAndroid Build Coastguard Workerimport sys
11*8975f5c5SAndroid Build Coastguard Workerimport re
12*8975f5c5SAndroid Build Coastguard Workerfrom optparse import OptionParser
13*8975f5c5SAndroid Build Coastguard Worker
14*8975f5c5SAndroid Build Coastguard Worker# This script runs pkg-config, optionally filtering out some results, and
15*8975f5c5SAndroid Build Coastguard Worker# returns the result.
16*8975f5c5SAndroid Build Coastguard Worker#
17*8975f5c5SAndroid Build Coastguard Worker# The result will be [ <includes>, <cflags>, <libs>, <lib_dirs>, <ldflags> ]
18*8975f5c5SAndroid Build Coastguard Worker# where each member is itself a list of strings.
19*8975f5c5SAndroid Build Coastguard Worker#
20*8975f5c5SAndroid Build Coastguard Worker# You can filter out matches using "-v <regexp>" where all results from
21*8975f5c5SAndroid Build Coastguard Worker# pkgconfig matching the given regular expression will be ignored. You can
22*8975f5c5SAndroid Build Coastguard Worker# specify more than one regular expression my specifying "-v" more than once.
23*8975f5c5SAndroid Build Coastguard Worker#
24*8975f5c5SAndroid Build Coastguard Worker# You can specify a sysroot using "-s <sysroot>" where sysroot is the absolute
25*8975f5c5SAndroid Build Coastguard Worker# system path to the sysroot used for compiling. This script will attempt to
26*8975f5c5SAndroid Build Coastguard Worker# generate correct paths for the sysroot.
27*8975f5c5SAndroid Build Coastguard Worker#
28*8975f5c5SAndroid Build Coastguard Worker# When using a sysroot, you must also specify the architecture via
29*8975f5c5SAndroid Build Coastguard Worker# "-a <arch>" where arch is either "x86" or "x64".
30*8975f5c5SAndroid Build Coastguard Worker#
31*8975f5c5SAndroid Build Coastguard Worker# CrOS systemroots place pkgconfig files at <systemroot>/usr/share/pkgconfig
32*8975f5c5SAndroid Build Coastguard Worker# and one of <systemroot>/usr/lib/pkgconfig or <systemroot>/usr/lib64/pkgconfig
33*8975f5c5SAndroid Build Coastguard Worker# depending on whether the systemroot is for a 32 or 64 bit architecture. They
34*8975f5c5SAndroid Build Coastguard Worker# specify the 'lib' or 'lib64' of the pkgconfig path by defining the
35*8975f5c5SAndroid Build Coastguard Worker# 'system_libdir' variable in the args.gn file. pkg_config.gni communicates this
36*8975f5c5SAndroid Build Coastguard Worker# variable to this script with the "--system_libdir <system_libdir>" flag. If no
37*8975f5c5SAndroid Build Coastguard Worker# flag is provided, then pkgconfig files are assumed to come from
38*8975f5c5SAndroid Build Coastguard Worker# <systemroot>/usr/lib/pkgconfig.
39*8975f5c5SAndroid Build Coastguard Worker#
40*8975f5c5SAndroid Build Coastguard Worker# Additionally, you can specify the option --atleast-version. This will skip
41*8975f5c5SAndroid Build Coastguard Worker# the normal outputting of a dictionary and instead print true or false,
42*8975f5c5SAndroid Build Coastguard Worker# depending on the return value of pkg-config for the given package.
43*8975f5c5SAndroid Build Coastguard Worker
44*8975f5c5SAndroid Build Coastguard Worker
45*8975f5c5SAndroid Build Coastguard Workerdef SetConfigPath(options):
46*8975f5c5SAndroid Build Coastguard Worker  """Set the PKG_CONFIG_LIBDIR environment variable.
47*8975f5c5SAndroid Build Coastguard Worker
48*8975f5c5SAndroid Build Coastguard Worker  This takes into account any sysroot and architecture specification from the
49*8975f5c5SAndroid Build Coastguard Worker  options on the given command line.
50*8975f5c5SAndroid Build Coastguard Worker  """
51*8975f5c5SAndroid Build Coastguard Worker
52*8975f5c5SAndroid Build Coastguard Worker  sysroot = options.sysroot
53*8975f5c5SAndroid Build Coastguard Worker  assert sysroot
54*8975f5c5SAndroid Build Coastguard Worker
55*8975f5c5SAndroid Build Coastguard Worker  # Compute the library path name based on the architecture.
56*8975f5c5SAndroid Build Coastguard Worker  arch = options.arch
57*8975f5c5SAndroid Build Coastguard Worker  if sysroot and not arch:
58*8975f5c5SAndroid Build Coastguard Worker    print("You must specify an architecture via -a if using a sysroot.")
59*8975f5c5SAndroid Build Coastguard Worker    sys.exit(1)
60*8975f5c5SAndroid Build Coastguard Worker
61*8975f5c5SAndroid Build Coastguard Worker  libdir = sysroot + '/usr/' + options.system_libdir + '/pkgconfig'
62*8975f5c5SAndroid Build Coastguard Worker  libdir += ':' + sysroot + '/usr/share/pkgconfig'
63*8975f5c5SAndroid Build Coastguard Worker  os.environ['PKG_CONFIG_LIBDIR'] = libdir
64*8975f5c5SAndroid Build Coastguard Worker  return libdir
65*8975f5c5SAndroid Build Coastguard Worker
66*8975f5c5SAndroid Build Coastguard Worker
67*8975f5c5SAndroid Build Coastguard Workerdef GetPkgConfigPrefixToStrip(options, args):
68*8975f5c5SAndroid Build Coastguard Worker  """Returns the prefix from pkg-config where packages are installed.
69*8975f5c5SAndroid Build Coastguard Worker
70*8975f5c5SAndroid Build Coastguard Worker  This returned prefix is the one that should be stripped from the beginning of
71*8975f5c5SAndroid Build Coastguard Worker  directory names to take into account sysroots.
72*8975f5c5SAndroid Build Coastguard Worker  """
73*8975f5c5SAndroid Build Coastguard Worker  # Some sysroots, like the Chromium OS ones, may generate paths that are not
74*8975f5c5SAndroid Build Coastguard Worker  # relative to the sysroot. For example,
75*8975f5c5SAndroid Build Coastguard Worker  # /path/to/chroot/build/x86-generic/usr/lib/pkgconfig/pkg.pc may have all
76*8975f5c5SAndroid Build Coastguard Worker  # paths relative to /path/to/chroot (i.e. prefix=/build/x86-generic/usr)
77*8975f5c5SAndroid Build Coastguard Worker  # instead of relative to /path/to/chroot/build/x86-generic (i.e prefix=/usr).
78*8975f5c5SAndroid Build Coastguard Worker  # To support this correctly, it's necessary to extract the prefix to strip
79*8975f5c5SAndroid Build Coastguard Worker  # from pkg-config's |prefix| variable.
80*8975f5c5SAndroid Build Coastguard Worker  prefix = subprocess.check_output([options.pkg_config,
81*8975f5c5SAndroid Build Coastguard Worker      "--variable=prefix"] + args, env=os.environ).decode('utf-8')
82*8975f5c5SAndroid Build Coastguard Worker  if prefix[:4] == '/usr':
83*8975f5c5SAndroid Build Coastguard Worker    return prefix[4:]
84*8975f5c5SAndroid Build Coastguard Worker  return prefix
85*8975f5c5SAndroid Build Coastguard Worker
86*8975f5c5SAndroid Build Coastguard Worker
87*8975f5c5SAndroid Build Coastguard Workerdef MatchesAnyRegexp(flag, list_of_regexps):
88*8975f5c5SAndroid Build Coastguard Worker  """Returns true if the first argument matches any regular expression in the
89*8975f5c5SAndroid Build Coastguard Worker  given list."""
90*8975f5c5SAndroid Build Coastguard Worker  for regexp in list_of_regexps:
91*8975f5c5SAndroid Build Coastguard Worker    if regexp.search(flag) != None:
92*8975f5c5SAndroid Build Coastguard Worker      return True
93*8975f5c5SAndroid Build Coastguard Worker  return False
94*8975f5c5SAndroid Build Coastguard Worker
95*8975f5c5SAndroid Build Coastguard Worker
96*8975f5c5SAndroid Build Coastguard Workerdef RewritePath(path, strip_prefix, sysroot):
97*8975f5c5SAndroid Build Coastguard Worker  """Rewrites a path by stripping the prefix and prepending the sysroot."""
98*8975f5c5SAndroid Build Coastguard Worker  if os.path.isabs(path) and not path.startswith(sysroot):
99*8975f5c5SAndroid Build Coastguard Worker    if path.startswith(strip_prefix):
100*8975f5c5SAndroid Build Coastguard Worker      path = path[len(strip_prefix):]
101*8975f5c5SAndroid Build Coastguard Worker    path = path.lstrip('/')
102*8975f5c5SAndroid Build Coastguard Worker    return os.path.join(sysroot, path)
103*8975f5c5SAndroid Build Coastguard Worker  else:
104*8975f5c5SAndroid Build Coastguard Worker    return path
105*8975f5c5SAndroid Build Coastguard Worker
106*8975f5c5SAndroid Build Coastguard Worker
107*8975f5c5SAndroid Build Coastguard Workerdef main():
108*8975f5c5SAndroid Build Coastguard Worker  # If this is run on non-Linux platforms, just return nothing and indicate
109*8975f5c5SAndroid Build Coastguard Worker  # success. This allows us to "kind of emulate" a Linux build from other
110*8975f5c5SAndroid Build Coastguard Worker  # platforms.
111*8975f5c5SAndroid Build Coastguard Worker  if "linux" not in sys.platform:
112*8975f5c5SAndroid Build Coastguard Worker    print("[[],[],[],[],[]]")
113*8975f5c5SAndroid Build Coastguard Worker    return 0
114*8975f5c5SAndroid Build Coastguard Worker
115*8975f5c5SAndroid Build Coastguard Worker  parser = OptionParser()
116*8975f5c5SAndroid Build Coastguard Worker  parser.add_option('-d', '--debug', action='store_true')
117*8975f5c5SAndroid Build Coastguard Worker  parser.add_option('-p', action='store', dest='pkg_config', type='string',
118*8975f5c5SAndroid Build Coastguard Worker                    default='pkg-config')
119*8975f5c5SAndroid Build Coastguard Worker  parser.add_option('-v', action='append', dest='strip_out', type='string')
120*8975f5c5SAndroid Build Coastguard Worker  parser.add_option('-s', action='store', dest='sysroot', type='string')
121*8975f5c5SAndroid Build Coastguard Worker  parser.add_option('-a', action='store', dest='arch', type='string')
122*8975f5c5SAndroid Build Coastguard Worker  parser.add_option('--system_libdir', action='store', dest='system_libdir',
123*8975f5c5SAndroid Build Coastguard Worker                    type='string', default='lib')
124*8975f5c5SAndroid Build Coastguard Worker  parser.add_option('--atleast-version', action='store',
125*8975f5c5SAndroid Build Coastguard Worker                    dest='atleast_version', type='string')
126*8975f5c5SAndroid Build Coastguard Worker  parser.add_option('--libdir', action='store_true', dest='libdir')
127*8975f5c5SAndroid Build Coastguard Worker  parser.add_option('--dridriverdir', action='store_true', dest='dridriverdir')
128*8975f5c5SAndroid Build Coastguard Worker  parser.add_option('--version-as-components', action='store_true',
129*8975f5c5SAndroid Build Coastguard Worker                    dest='version_as_components')
130*8975f5c5SAndroid Build Coastguard Worker  (options, args) = parser.parse_args()
131*8975f5c5SAndroid Build Coastguard Worker
132*8975f5c5SAndroid Build Coastguard Worker  # Make a list of regular expressions to strip out.
133*8975f5c5SAndroid Build Coastguard Worker  strip_out = []
134*8975f5c5SAndroid Build Coastguard Worker  if options.strip_out != None:
135*8975f5c5SAndroid Build Coastguard Worker    for regexp in options.strip_out:
136*8975f5c5SAndroid Build Coastguard Worker      strip_out.append(re.compile(regexp))
137*8975f5c5SAndroid Build Coastguard Worker
138*8975f5c5SAndroid Build Coastguard Worker  if options.sysroot:
139*8975f5c5SAndroid Build Coastguard Worker    libdir = SetConfigPath(options)
140*8975f5c5SAndroid Build Coastguard Worker    if options.debug:
141*8975f5c5SAndroid Build Coastguard Worker      sys.stderr.write('PKG_CONFIG_LIBDIR=%s\n' % libdir)
142*8975f5c5SAndroid Build Coastguard Worker    prefix = GetPkgConfigPrefixToStrip(options, args)
143*8975f5c5SAndroid Build Coastguard Worker  else:
144*8975f5c5SAndroid Build Coastguard Worker    prefix = ''
145*8975f5c5SAndroid Build Coastguard Worker
146*8975f5c5SAndroid Build Coastguard Worker  if options.atleast_version:
147*8975f5c5SAndroid Build Coastguard Worker    # When asking for the return value, just run pkg-config and print the return
148*8975f5c5SAndroid Build Coastguard Worker    # value, no need to do other work.
149*8975f5c5SAndroid Build Coastguard Worker    if not subprocess.call([options.pkg_config,
150*8975f5c5SAndroid Build Coastguard Worker                            "--atleast-version=" + options.atleast_version] +
151*8975f5c5SAndroid Build Coastguard Worker                            args):
152*8975f5c5SAndroid Build Coastguard Worker      print("true")
153*8975f5c5SAndroid Build Coastguard Worker    else:
154*8975f5c5SAndroid Build Coastguard Worker      print("false")
155*8975f5c5SAndroid Build Coastguard Worker    return 0
156*8975f5c5SAndroid Build Coastguard Worker
157*8975f5c5SAndroid Build Coastguard Worker  if options.version_as_components:
158*8975f5c5SAndroid Build Coastguard Worker    cmd = [options.pkg_config, "--modversion"] + args
159*8975f5c5SAndroid Build Coastguard Worker    try:
160*8975f5c5SAndroid Build Coastguard Worker      version_string = subprocess.check_output(cmd).decode('utf-8')
161*8975f5c5SAndroid Build Coastguard Worker    except:
162*8975f5c5SAndroid Build Coastguard Worker      sys.stderr.write('Error from pkg-config.\n')
163*8975f5c5SAndroid Build Coastguard Worker      return 1
164*8975f5c5SAndroid Build Coastguard Worker    print(json.dumps(list(map(int, version_string.strip().split(".")))))
165*8975f5c5SAndroid Build Coastguard Worker    return 0
166*8975f5c5SAndroid Build Coastguard Worker
167*8975f5c5SAndroid Build Coastguard Worker
168*8975f5c5SAndroid Build Coastguard Worker  if options.libdir:
169*8975f5c5SAndroid Build Coastguard Worker    cmd = [options.pkg_config, "--variable=libdir"] + args
170*8975f5c5SAndroid Build Coastguard Worker    if options.debug:
171*8975f5c5SAndroid Build Coastguard Worker      sys.stderr.write('Running: %s\n' % cmd)
172*8975f5c5SAndroid Build Coastguard Worker    try:
173*8975f5c5SAndroid Build Coastguard Worker      libdir = subprocess.check_output(cmd).decode('utf-8')
174*8975f5c5SAndroid Build Coastguard Worker    except:
175*8975f5c5SAndroid Build Coastguard Worker      print("Error from pkg-config.")
176*8975f5c5SAndroid Build Coastguard Worker      return 1
177*8975f5c5SAndroid Build Coastguard Worker    sys.stdout.write(libdir.strip())
178*8975f5c5SAndroid Build Coastguard Worker    return 0
179*8975f5c5SAndroid Build Coastguard Worker
180*8975f5c5SAndroid Build Coastguard Worker  if options.dridriverdir:
181*8975f5c5SAndroid Build Coastguard Worker    cmd = [options.pkg_config, "--variable=dridriverdir"] + args
182*8975f5c5SAndroid Build Coastguard Worker    if options.debug:
183*8975f5c5SAndroid Build Coastguard Worker      sys.stderr.write('Running: %s\n' % cmd)
184*8975f5c5SAndroid Build Coastguard Worker    try:
185*8975f5c5SAndroid Build Coastguard Worker      dridriverdir = subprocess.check_output(cmd).decode('utf-8')
186*8975f5c5SAndroid Build Coastguard Worker    except:
187*8975f5c5SAndroid Build Coastguard Worker      print("Error from pkg-config.")
188*8975f5c5SAndroid Build Coastguard Worker      return 1
189*8975f5c5SAndroid Build Coastguard Worker    sys.stdout.write(dridriverdir.strip())
190*8975f5c5SAndroid Build Coastguard Worker    return
191*8975f5c5SAndroid Build Coastguard Worker
192*8975f5c5SAndroid Build Coastguard Worker  cmd = [options.pkg_config, "--cflags", "--libs"] + args
193*8975f5c5SAndroid Build Coastguard Worker  if options.debug:
194*8975f5c5SAndroid Build Coastguard Worker    sys.stderr.write('Running: %s\n' % ' '.join(cmd))
195*8975f5c5SAndroid Build Coastguard Worker
196*8975f5c5SAndroid Build Coastguard Worker  try:
197*8975f5c5SAndroid Build Coastguard Worker    flag_string = subprocess.check_output(cmd).decode('utf-8')
198*8975f5c5SAndroid Build Coastguard Worker  except:
199*8975f5c5SAndroid Build Coastguard Worker    sys.stderr.write('Could not run pkg-config.\n')
200*8975f5c5SAndroid Build Coastguard Worker    return 1
201*8975f5c5SAndroid Build Coastguard Worker
202*8975f5c5SAndroid Build Coastguard Worker  # For now just split on spaces to get the args out. This will break if
203*8975f5c5SAndroid Build Coastguard Worker  # pkgconfig returns quoted things with spaces in them, but that doesn't seem
204*8975f5c5SAndroid Build Coastguard Worker  # to happen in practice.
205*8975f5c5SAndroid Build Coastguard Worker  all_flags = flag_string.strip().split(' ')
206*8975f5c5SAndroid Build Coastguard Worker
207*8975f5c5SAndroid Build Coastguard Worker
208*8975f5c5SAndroid Build Coastguard Worker  sysroot = options.sysroot
209*8975f5c5SAndroid Build Coastguard Worker  if not sysroot:
210*8975f5c5SAndroid Build Coastguard Worker    sysroot = ''
211*8975f5c5SAndroid Build Coastguard Worker
212*8975f5c5SAndroid Build Coastguard Worker  includes = []
213*8975f5c5SAndroid Build Coastguard Worker  cflags = []
214*8975f5c5SAndroid Build Coastguard Worker  libs = []
215*8975f5c5SAndroid Build Coastguard Worker  lib_dirs = []
216*8975f5c5SAndroid Build Coastguard Worker
217*8975f5c5SAndroid Build Coastguard Worker  for flag in all_flags[:]:
218*8975f5c5SAndroid Build Coastguard Worker    if len(flag) == 0 or MatchesAnyRegexp(flag, strip_out):
219*8975f5c5SAndroid Build Coastguard Worker      continue;
220*8975f5c5SAndroid Build Coastguard Worker
221*8975f5c5SAndroid Build Coastguard Worker    if flag[:2] == '-l':
222*8975f5c5SAndroid Build Coastguard Worker      libs.append(RewritePath(flag[2:], prefix, sysroot))
223*8975f5c5SAndroid Build Coastguard Worker    elif flag[:2] == '-L':
224*8975f5c5SAndroid Build Coastguard Worker      lib_dirs.append(RewritePath(flag[2:], prefix, sysroot))
225*8975f5c5SAndroid Build Coastguard Worker    elif flag[:2] == '-I':
226*8975f5c5SAndroid Build Coastguard Worker      includes.append(RewritePath(flag[2:], prefix, sysroot))
227*8975f5c5SAndroid Build Coastguard Worker    elif flag[:3] == '-Wl':
228*8975f5c5SAndroid Build Coastguard Worker      # Don't allow libraries to control ld flags.  These should be specified
229*8975f5c5SAndroid Build Coastguard Worker      # only in build files.
230*8975f5c5SAndroid Build Coastguard Worker      pass
231*8975f5c5SAndroid Build Coastguard Worker    elif flag == '-pthread':
232*8975f5c5SAndroid Build Coastguard Worker      # Many libs specify "-pthread" which we don't need since we always include
233*8975f5c5SAndroid Build Coastguard Worker      # this anyway. Removing it here prevents a bunch of duplicate inclusions
234*8975f5c5SAndroid Build Coastguard Worker      # on the command line.
235*8975f5c5SAndroid Build Coastguard Worker      pass
236*8975f5c5SAndroid Build Coastguard Worker    else:
237*8975f5c5SAndroid Build Coastguard Worker      cflags.append(flag)
238*8975f5c5SAndroid Build Coastguard Worker
239*8975f5c5SAndroid Build Coastguard Worker  # Output a GN array, the first one is the cflags, the second are the libs. The
240*8975f5c5SAndroid Build Coastguard Worker  # JSON formatter prints GN compatible lists when everything is a list of
241*8975f5c5SAndroid Build Coastguard Worker  # strings.
242*8975f5c5SAndroid Build Coastguard Worker  print(json.dumps([includes, cflags, libs, lib_dirs]))
243*8975f5c5SAndroid Build Coastguard Worker  return 0
244*8975f5c5SAndroid Build Coastguard Worker
245*8975f5c5SAndroid Build Coastguard Worker
246*8975f5c5SAndroid Build Coastguard Workerif __name__ == '__main__':
247*8975f5c5SAndroid Build Coastguard Worker  sys.exit(main())
248