python-config 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python3
  2. from __future__ import print_function
  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. print("Usage: %s [%s]" % (sys.argv[0], '|'.join('--'+opt for opt in valid_opts)), file=sys.stderr)
  11. sys.exit(code)
  12. try:
  13. opts, args = getopt.getopt(sys.argv[1:], '', valid_opts)
  14. except getopt.error:
  15. exit_with_usage()
  16. if not opts:
  17. exit_with_usage()
  18. opt = opts[0][0]
  19. pyver = sysconfig.get_config_var('LDVERSION')
  20. if pyver == None:
  21. pyver = sysconfig.get_config_var('VERSION')
  22. getvar = sysconfig.get_config_var
  23. if opt == '--help':
  24. exit_with_usage(0)
  25. elif opt == '--prefix':
  26. print(sysconfig.PREFIX)
  27. elif opt == '--exec-prefix':
  28. print(sysconfig.EXEC_PREFIX)
  29. elif opt in ('--includes', '--cflags'):
  30. flags = ['-I' + sysconfig.get_python_inc(),
  31. '-I' + sysconfig.get_python_inc(plat_specific=True)]
  32. if opt == '--cflags':
  33. flags.extend(getvar('CFLAGS').split())
  34. print(' '.join(flags))
  35. elif opt in ('--libs', '--ldflags'):
  36. libs = getvar('LIBS').split() + getvar('SYSLIBS').split()
  37. libs.append('-lpython'+pyver)
  38. # add the prefix/lib/pythonX.Y/config dir, but only if there is no
  39. # shared library in prefix/lib/.
  40. if opt == '--ldflags' and not getvar('Py_ENABLE_SHARED'):
  41. libs.insert(0, '-L' + getvar('LIBPL'))
  42. print(' '.join(libs))