2 # Copyright 2011 The Chromium Authors
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
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
20 from __future__ import print_function
27 print('usage: %s {--api-version|--cflags|--ldflags|--libs|--libs-for-gn} '
28 '[sysroot]' % sys.argv[0])
31 def run_cups_config(cups_config, mode):
32 """Run cups-config with all --cflags etc modes, parse out the mode we want,
33 and return those flags as a list."""
35 cups = subprocess.Popen([cups_config, '--cflags', '--ldflags', '--libs'],
36 stdout=subprocess.PIPE, universal_newlines=True)
37 flags = cups.communicate()[0].strip()
40 for flag in flags.split():
42 if flag.startswith('-l'):
44 elif (flag.startswith('-L') or flag.startswith('-Wl,')):
45 flag_mode = '--ldflags'
46 elif (flag.startswith('-I') or flag.startswith('-D')):
47 flag_mode = '--cflags'
49 # Be conservative: for flags where we don't know which mode they
50 # belong in, always include them.
51 if flag_mode is None or flag_mode == mode:
52 flags_subset.append(flag)
54 # Note: cross build is confused by the option, and may trigger linker
55 # warning causing build error.
56 if '-lgnutls' in flags_subset:
57 flags_subset.remove('-lgnutls')
68 if len(sys.argv) > 2 and sys.argv[2]:
70 cups_config = os.path.join(sysroot, 'usr', 'bin', 'cups-config')
71 if not os.path.exists(cups_config):
72 print('cups-config not found: %s' % cups_config)
75 cups_config = 'cups-config'
77 if mode == '--api-version':
78 subprocess.call([cups_config, '--api-version'])
81 # All other modes get the flags.
82 if mode not in ('--cflags', '--libs', '--libs-for-gn', '--ldflags'):
86 if mode == '--libs-for-gn':
90 gn_libs_output = False
92 flags = run_cups_config(cups_config, mode)
95 # Strip "-l" from beginning of libs, quote, and surround in [ ].
99 print('"%s", ' % lib[2:])
102 print(' '.join(flags))
107 if __name__ == '__main__':