python-config.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/python3.5m
  2. # -*- python -*-
  3. # Keep this script in sync with python-config.sh.in
  4. import getopt
  5. import os
  6. import sys
  7. from distutils import sysconfig
  8. valid_opts = ['prefix', 'exec-prefix', 'includes', 'libs', 'cflags',
  9. 'ldflags', 'extension-suffix', 'help', 'abiflags', 'configdir']
  10. def exit_with_usage(code=1):
  11. print("Usage: {0} [{1}]".format(
  12. sys.argv[0], '|'.join('--'+opt for opt in valid_opts)), file=sys.stderr)
  13. sys.exit(code)
  14. try:
  15. opts, args = getopt.getopt(sys.argv[1:], '', valid_opts)
  16. except getopt.error:
  17. exit_with_usage()
  18. if not opts:
  19. exit_with_usage()
  20. pyver = sysconfig.get_config_var('VERSION')
  21. getvar = sysconfig.get_config_var
  22. opt_flags = [flag for (flag, val) in opts]
  23. if '--help' in opt_flags:
  24. exit_with_usage(code=0)
  25. for opt in opt_flags:
  26. if opt == '--prefix':
  27. print(sysconfig.PREFIX)
  28. elif opt == '--exec-prefix':
  29. print(sysconfig.EXEC_PREFIX)
  30. elif opt in ('--includes', '--cflags'):
  31. flags = ['-I' + sysconfig.get_python_inc(),
  32. '-I' + sysconfig.get_python_inc(plat_specific=True)]
  33. if opt == '--cflags':
  34. flags.extend(getvar('CFLAGS').split())
  35. print(' '.join(flags))
  36. elif opt in ('--libs', '--ldflags'):
  37. libs = ['-lpython' + pyver + sys.abiflags]
  38. libs += getvar('LIBS').split()
  39. libs += getvar('SYSLIBS').split()
  40. # add the prefix/lib/pythonX.Y/config dir, but only if there is no
  41. # shared library in prefix/lib/.
  42. if opt == '--ldflags':
  43. if not getvar('Py_ENABLE_SHARED'):
  44. libs.insert(0, '-L' + getvar('LIBPL'))
  45. if not getvar('PYTHONFRAMEWORK'):
  46. libs.extend(getvar('LINKFORSHARED').split())
  47. print(' '.join(libs))
  48. elif opt == '--extension-suffix':
  49. print(sysconfig.get_config_var('EXT_SUFFIX'))
  50. elif opt == '--abiflags':
  51. print(sys.abiflags)
  52. elif opt == '--configdir':
  53. print(sysconfig.get_config_var('LIBPL'))