cups_config_helper.py 2.9 KB

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