[email protected] | aa539ba4 | 2011-11-23 22:22:20 | [diff] [blame] | 1 | #!/usr/bin/env python |
[email protected] | 31aa30b | 2011-08-12 22:16:19 | [diff] [blame] | 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. |
| 5 | |
| 6 | """cups-config wrapper. |
| 7 | |
| 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. |
| 13 | |
[email protected] | fc2020f | 2014-06-04 18:09:39 | [diff] [blame] | 14 | This can be dramatically simplified or maybe removed (depending on GN |
| 15 | requirements) when this is fixed: |
[email protected] | 31aa30b | 2011-08-12 22:16:19 | [diff] [blame] | 16 | https://bugs.launchpad.net/ubuntu/+source/cupsys/+bug/163704 |
| 17 | is fixed. |
| 18 | """ |
| 19 | |
Raul Tambre | 38201dc | 2019-09-26 18:01:46 | [diff] [blame] | 20 | from __future__ import print_function |
| 21 | |
sbc | 05ac52d | 2015-05-22 23:51:26 | [diff] [blame] | 22 | import os |
[email protected] | 31aa30b | 2011-08-12 22:16:19 | [diff] [blame] | 23 | import subprocess |
| 24 | import sys |
| 25 | |
| 26 | def usage(): |
Raul Tambre | 38201dc | 2019-09-26 18:01:46 | [diff] [blame] | 27 | print('usage: %s {--api-version|--cflags|--ldflags|--libs|--libs-for-gn} ' |
| 28 | '[sysroot]' % sys.argv[0]) |
[email protected] | aa539ba4 | 2011-11-23 22:22:20 | [diff] [blame] | 29 | |
[email protected] | 31aa30b | 2011-08-12 22:16:19 | [diff] [blame] | 30 | |
sbc | 05ac52d | 2015-05-22 23:51:26 | [diff] [blame] | 31 | def run_cups_config(cups_config, mode): |
[email protected] | aa539ba4 | 2011-11-23 22:22:20 | [diff] [blame] | 32 | """Run cups-config with all --cflags etc modes, parse out the mode we want, |
| 33 | and return those flags as a list.""" |
[email protected] | 31aa30b | 2011-08-12 22:16:19 | [diff] [blame] | 34 | |
sbc | 05ac52d | 2015-05-22 23:51:26 | [diff] [blame] | 35 | cups = subprocess.Popen([cups_config, '--cflags', '--ldflags', '--libs'], |
Christian Biesinger | 0afee77 | 2019-09-25 18:12:20 | [diff] [blame] | 36 | stdout=subprocess.PIPE, universal_newlines=True) |
[email protected] | aa539ba4 | 2011-11-23 22:22:20 | [diff] [blame] | 37 | flags = cups.communicate()[0].strip() |
[email protected] | 31aa30b | 2011-08-12 22:16:19 | [diff] [blame] | 38 | |
[email protected] | aa539ba4 | 2011-11-23 22:22:20 | [diff] [blame] | 39 | flags_subset = [] |
| 40 | for flag in flags.split(): |
| 41 | flag_mode = None |
| 42 | if flag.startswith('-l'): |
| 43 | flag_mode = '--libs' |
| 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' |
[email protected] | 31aa30b | 2011-08-12 22:16:19 | [diff] [blame] | 48 | |
[email protected] | aa539ba4 | 2011-11-23 22:22:20 | [diff] [blame] | 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) |
[email protected] | 31aa30b | 2011-08-12 22:16:19 | [diff] [blame] | 53 | |
[email protected] | f7e45c1 | 2014-03-26 04:00:08 | [diff] [blame] | 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') |
| 58 | |
[email protected] | aa539ba4 | 2011-11-23 22:22:20 | [diff] [blame] | 59 | return flags_subset |
[email protected] | 31aa30b | 2011-08-12 22:16:19 | [diff] [blame] | 60 | |
[email protected] | aa539ba4 | 2011-11-23 22:22:20 | [diff] [blame] | 61 | |
| 62 | def main(): |
sbc | 05ac52d | 2015-05-22 23:51:26 | [diff] [blame] | 63 | if len(sys.argv) < 2: |
[email protected] | 31aa30b | 2011-08-12 22:16:19 | [diff] [blame] | 64 | usage() |
[email protected] | aa539ba4 | 2011-11-23 22:22:20 | [diff] [blame] | 65 | return 1 |
[email protected] | 31aa30b | 2011-08-12 22:16:19 | [diff] [blame] | 66 | |
[email protected] | aa539ba4 | 2011-11-23 22:22:20 | [diff] [blame] | 67 | mode = sys.argv[1] |
agoode | b4d790c9 | 2016-01-05 17:20:31 | [diff] [blame] | 68 | if len(sys.argv) > 2 and sys.argv[2]: |
sbc | 05ac52d | 2015-05-22 23:51:26 | [diff] [blame] | 69 | sysroot = sys.argv[2] |
| 70 | cups_config = os.path.join(sysroot, 'usr', 'bin', 'cups-config') |
| 71 | if not os.path.exists(cups_config): |
Christian Biesinger | 0afee77 | 2019-09-25 18:12:20 | [diff] [blame] | 72 | print('cups-config not found: %s' % cups_config) |
sbc | 05ac52d | 2015-05-22 23:51:26 | [diff] [blame] | 73 | return 1 |
| 74 | else: |
| 75 | cups_config = 'cups-config' |
| 76 | |
[email protected] | fc2020f | 2014-06-04 18:09:39 | [diff] [blame] | 77 | if mode == '--api-version': |
sbc | 05ac52d | 2015-05-22 23:51:26 | [diff] [blame] | 78 | subprocess.call([cups_config, '--api-version']) |
[email protected] | fc2020f | 2014-06-04 18:09:39 | [diff] [blame] | 79 | return 0 |
| 80 | |
| 81 | # All other modes get the flags. |
| 82 | if mode not in ('--cflags', '--libs', '--libs-for-gn', '--ldflags'): |
[email protected] | 31aa30b | 2011-08-12 22:16:19 | [diff] [blame] | 83 | usage() |
[email protected] | aa539ba4 | 2011-11-23 22:22:20 | [diff] [blame] | 84 | return 1 |
[email protected] | fc2020f | 2014-06-04 18:09:39 | [diff] [blame] | 85 | |
| 86 | if mode == '--libs-for-gn': |
| 87 | gn_libs_output = True |
| 88 | mode = '--libs' |
| 89 | else: |
| 90 | gn_libs_output = False |
| 91 | |
sbc | 05ac52d | 2015-05-22 23:51:26 | [diff] [blame] | 92 | flags = run_cups_config(cups_config, mode) |
[email protected] | fc2020f | 2014-06-04 18:09:39 | [diff] [blame] | 93 | |
| 94 | if gn_libs_output: |
| 95 | # Strip "-l" from beginning of libs, quote, and surround in [ ]. |
Christian Biesinger | 0afee77 | 2019-09-25 18:12:20 | [diff] [blame] | 96 | print('[') |
[email protected] | fc2020f | 2014-06-04 18:09:39 | [diff] [blame] | 97 | for lib in flags: |
| 98 | if lib[:2] == "-l": |
Christian Biesinger | 0afee77 | 2019-09-25 18:12:20 | [diff] [blame] | 99 | print('"%s", ' % lib[2:]) |
| 100 | print(']') |
[email protected] | fc2020f | 2014-06-04 18:09:39 | [diff] [blame] | 101 | else: |
Christian Biesinger | 0afee77 | 2019-09-25 18:12:20 | [diff] [blame] | 102 | print(' '.join(flags)) |
[email protected] | fc2020f | 2014-06-04 18:09:39 | [diff] [blame] | 103 | |
[email protected] | aa539ba4 | 2011-11-23 22:22:20 | [diff] [blame] | 104 | return 0 |
| 105 | |
| 106 | |
| 107 | if __name__ == '__main__': |
| 108 | sys.exit(main()) |