blob: 48cb33376e359190b940c8ef4bb4731d1ca6260c [file] [log] [blame]
[email protected]aa539ba42011-11-23 22:22:201#!/usr/bin/env python
[email protected]31aa30b2011-08-12 22:16:192# 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.
5
6"""cups-config wrapper.
7
8cups-config, at least on Ubuntu Lucid and Natty, dumps all
9cflags/ldflags/libs when passed the --libs argument. gyp would like
10to keep these separate: cflags are only needed when compiling files
11that use cups directly, while libs are only needed on the final link
12line.
13
[email protected]fc2020f2014-06-04 18:09:3914This can be dramatically simplified or maybe removed (depending on GN
15requirements) when this is fixed:
[email protected]31aa30b2011-08-12 22:16:1916 https://bugs.launchpad.net/ubuntu/+source/cupsys/+bug/163704
17is fixed.
18"""
19
sbc05ac52d2015-05-22 23:51:2620import os
[email protected]31aa30b2011-08-12 22:16:1921import subprocess
22import sys
23
24def usage():
agoodeb4d790c92016-01-05 17:20:3125 print ('usage: %s {--api-version|--cflags|--ldflags|--libs|--libs-for-gn} '
26 '[sysroot]' % sys.argv[0])
[email protected]aa539ba42011-11-23 22:22:2027
[email protected]31aa30b2011-08-12 22:16:1928
sbc05ac52d2015-05-22 23:51:2629def run_cups_config(cups_config, mode):
[email protected]aa539ba42011-11-23 22:22:2030 """Run cups-config with all --cflags etc modes, parse out the mode we want,
31 and return those flags as a list."""
[email protected]31aa30b2011-08-12 22:16:1932
sbc05ac52d2015-05-22 23:51:2633 cups = subprocess.Popen([cups_config, '--cflags', '--ldflags', '--libs'],
[email protected]aa539ba42011-11-23 22:22:2034 stdout=subprocess.PIPE)
35 flags = cups.communicate()[0].strip()
[email protected]31aa30b2011-08-12 22:16:1936
[email protected]aa539ba42011-11-23 22:22:2037 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'
[email protected]31aa30b2011-08-12 22:16:1946
[email protected]aa539ba42011-11-23 22:22:2047 # 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)
[email protected]31aa30b2011-08-12 22:16:1951
[email protected]f7e45c12014-03-26 04:00:0852 # 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')
56
[email protected]aa539ba42011-11-23 22:22:2057 return flags_subset
[email protected]31aa30b2011-08-12 22:16:1958
[email protected]aa539ba42011-11-23 22:22:2059
60def main():
sbc05ac52d2015-05-22 23:51:2661 if len(sys.argv) < 2:
[email protected]31aa30b2011-08-12 22:16:1962 usage()
[email protected]aa539ba42011-11-23 22:22:2063 return 1
[email protected]31aa30b2011-08-12 22:16:1964
[email protected]aa539ba42011-11-23 22:22:2065 mode = sys.argv[1]
agoodeb4d790c92016-01-05 17:20:3166 if len(sys.argv) > 2 and sys.argv[2]:
sbc05ac52d2015-05-22 23:51:2667 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'
74
[email protected]fc2020f2014-06-04 18:09:3975 if mode == '--api-version':
sbc05ac52d2015-05-22 23:51:2676 subprocess.call([cups_config, '--api-version'])
[email protected]fc2020f2014-06-04 18:09:3977 return 0
78
79 # All other modes get the flags.
80 if mode not in ('--cflags', '--libs', '--libs-for-gn', '--ldflags'):
[email protected]31aa30b2011-08-12 22:16:1981 usage()
[email protected]aa539ba42011-11-23 22:22:2082 return 1
[email protected]fc2020f2014-06-04 18:09:3983
84 if mode == '--libs-for-gn':
85 gn_libs_output = True
86 mode = '--libs'
87 else:
88 gn_libs_output = False
89
sbc05ac52d2015-05-22 23:51:2690 flags = run_cups_config(cups_config, mode)
[email protected]fc2020f2014-06-04 18:09:3991
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)
101
[email protected]aa539ba42011-11-23 22:22:20102 return 0
103
104
105if __name__ == '__main__':
106 sys.exit(main())