Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / printing / cups_config_helper.py
blob138a688735a1e0c35919357d3cefd6a44fd2e753
1 #!/usr/bin/env python
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """cups-config wrapper.
8 cups-config, at least on Ubuntu Lucid and Natty, dumps all
9 cflags/ldflags/libs when passed the --libs argument. gyp would like
10 to keep these separate: cflags are only needed when compiling files
11 that use cups directly, while libs are only needed on the final link
12 line.
14 This can be dramatically simplified or maybe removed (depending on GN
15 requirements) when this is fixed:
16 https://bugs.launchpad.net/ubuntu/+source/cupsys/+bug/163704
17 is fixed.
18 """
20 import os
21 import subprocess
22 import sys
24 def usage():
25 print 'usage: %s {--api-version|--cflags|--ldflags|--libs|--libs-for-gn}' % \
26 sys.argv[0]
29 def run_cups_config(cups_config, mode):
30 """Run cups-config with all --cflags etc modes, parse out the mode we want,
31 and return those flags as a list."""
33 cups = subprocess.Popen([cups_config, '--cflags', '--ldflags', '--libs'],
34 stdout=subprocess.PIPE)
35 flags = cups.communicate()[0].strip()
37 flags_subset = []
38 for flag in flags.split():
39 flag_mode = None
40 if flag.startswith('-l'):
41 flag_mode = '--libs'
42 elif (flag.startswith('-L') or flag.startswith('-Wl,')):
43 flag_mode = '--ldflags'
44 elif (flag.startswith('-I') or flag.startswith('-D')):
45 flag_mode = '--cflags'
47 # Be conservative: for flags where we don't know which mode they
48 # belong in, always include them.
49 if flag_mode is None or flag_mode == mode:
50 flags_subset.append(flag)
52 # Note: cross build is confused by the option, and may trigger linker
53 # warning causing build error.
54 if '-lgnutls' in flags_subset:
55 flags_subset.remove('-lgnutls')
57 return flags_subset
60 def main():
61 if len(sys.argv) < 2:
62 usage()
63 return 1
65 mode = sys.argv[1]
66 if len(sys.argv) > 2:
67 sysroot = sys.argv[2]
68 cups_config = os.path.join(sysroot, 'usr', 'bin', 'cups-config')
69 if not os.path.exists(cups_config):
70 print 'cups-config not found: %s' % cups_config
71 return 1
72 else:
73 cups_config = 'cups-config'
75 if mode == '--api-version':
76 subprocess.call([cups_config, '--api-version'])
77 return 0
79 # All other modes get the flags.
80 if mode not in ('--cflags', '--libs', '--libs-for-gn', '--ldflags'):
81 usage()
82 return 1
84 if mode == '--libs-for-gn':
85 gn_libs_output = True
86 mode = '--libs'
87 else:
88 gn_libs_output = False
90 flags = run_cups_config(cups_config, mode)
92 if gn_libs_output:
93 # Strip "-l" from beginning of libs, quote, and surround in [ ].
94 print '['
95 for lib in flags:
96 if lib[:2] == "-l":
97 print '"%s", ' % lib[2:]
98 print ']'
99 else:
100 print ' '.join(flags)
102 return 0
105 if __name__ == '__main__':
106 sys.exit(main())