gdb/python/python-config.py - gdb

Global variables defined

Functions defined

Source code

  1. # Program to fetch python compilation parameters.
  2. # Copied from python-config of the 2.7 release.

  3. import sys
  4. import os
  5. import getopt
  6. from distutils import sysconfig

  7. valid_opts = ['prefix', 'exec-prefix', 'includes', 'libs', 'cflags',
  8.               'ldflags', 'help']

  9. def exit_with_usage(code=1):
  10.     sys.stderr.write ("Usage: %s [%s]\n" % (sys.argv[0],
  11.                                           '|'.join('--'+opt for opt in valid_opts)))
  12.     sys.exit(code)

  13. try:
  14.     opts, args = getopt.getopt(sys.argv[1:], '', valid_opts)
  15. except getopt.error:
  16.     exit_with_usage()

  17. if not opts:
  18.     exit_with_usage()

  19. pyver = sysconfig.get_config_var('VERSION')
  20. getvar = sysconfig.get_config_var
  21. abiflags = getattr (sys, "abiflags", "")

  22. opt_flags = [flag for (flag, val) in opts]

  23. if '--help' in opt_flags:
  24.     exit_with_usage(code=0)

  25. def to_unix_path(path):
  26.     """On Windows, returns the given path with all backslashes
  27.     converted into forward slashes.  This is to help prevent problems
  28.     when using the paths returned by this script with cygwin tools.
  29.     In particular, cygwin bash treats backslashes as a special character.

  30.     On Unix systems, returns the path unchanged.
  31.     """
  32.     if os.name == 'nt':
  33.         path = path.replace('\\', '/')
  34.     return path

  35. for opt in opt_flags:
  36.     if opt == '--prefix':
  37.         print (to_unix_path(sysconfig.PREFIX))

  38.     elif opt == '--exec-prefix':
  39.         print (to_unix_path(sysconfig.EXEC_PREFIX))

  40.     elif opt in ('--includes', '--cflags'):
  41.         flags = ['-I' + sysconfig.get_python_inc(),
  42.                  '-I' + sysconfig.get_python_inc(plat_specific=True)]
  43.         if opt == '--cflags':
  44.             flags.extend(getvar('CFLAGS').split())
  45.         print (to_unix_path(' '.join(flags)))

  46.     elif opt in ('--libs', '--ldflags'):
  47.         libs = []
  48.         if getvar('LIBS') is not None:
  49.             libs.extend(getvar('LIBS').split())
  50.         if getvar('SYSLIBS') is not None:
  51.             libs.extend(getvar('SYSLIBS').split())
  52.         libs.append('-lpython'+pyver + abiflags)
  53.         # add the prefix/lib/pythonX.Y/config dir, but only if there is no
  54.         # shared library in prefix/lib/.
  55.         if opt == '--ldflags':
  56.             if not getvar('Py_ENABLE_SHARED'):
  57.                 if getvar('LIBPL') is not None:
  58.                     libs.insert(0, '-L' + getvar('LIBPL'))
  59.                 elif os.name == 'nt':
  60.                     libs.insert(0, '-L' + sysconfig.PREFIX + '/libs')
  61.             if getvar('LINKFORSHARED') is not None:
  62.                 libs.extend(getvar('LINKFORSHARED').split())
  63.         print (to_unix_path(' '.join(libs)))