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