Home | History | Annotate | Download | only in linux
      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 subprocess
      7 import sys
      8 import re
      9 from optparse import OptionParser
     10 
     11 # This script runs pkg-config, optionally filtering out some results, and
     12 # returns the result.
     13 #
     14 # The result will be [ <includes>, <cflags>, <libs>, <lib_dirs> ] where each
     15 # member is itself a list of strings.
     16 #
     17 # 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 # specify more than one regular expression my specifying "-v" more than once.
     20 
     21 # 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
     23 # platforms.
     24 if sys.platform.find("linux") == -1:
     25   print "[[],[],[]]"
     26   sys.exit(0)
     27 
     28 parser = OptionParser()
     29 parser.add_option('-v', action='append', dest='strip_out', type='string')
     30 (options, args) = parser.parse_args()
     31 
     32 # Make a list of regular expressions to strip out.
     33 strip_out = []
     34 if options.strip_out != None:
     35   for regexp in options.strip_out:
     36     strip_out.append(re.compile(regexp))
     37 
     38 try:
     39   flag_string = subprocess.check_output(["pkg-config", "--cflags", "--libs"] +
     40       args)
     41   # 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
     43   # to happen in practice.
     44   all_flags = flag_string.strip().split(' ')
     45 except:
     46   print "Could not run pkg-config."
     47   sys.exit(1)
     48 
     49 includes = []
     50 cflags = []
     51 libs = []
     52 lib_dirs = []
     53 
     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[:]:
     61   if len(flag) == 0 or MatchesAnyRegexp(flag, strip_out):
     62     continue;
     63 
     64   if flag[:2] == '-l':
     65     libs.append(flag[2:])
     66   if flag[:2] == '-L':
     67     lib_dirs.append(flag[2:])
     68   elif flag[:2] == '-I':
     69     includes.append(flag[2:])
     70   else:
     71     cflags.append(flag)
     72 
     73 # 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
     75 # strings.
     76 print json.dumps([includes, cflags, libs, lib_dirs])
     77