python-config 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python
  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('VERSION')
  20. getvar = sysconfig.get_config_var
  21. if opt == '--help':
  22. exit_with_usage(0)
  23. elif opt == '--prefix':
  24. print(sysconfig.PREFIX)
  25. elif opt == '--exec-prefix':
  26. print(sysconfig.EXEC_PREFIX)
  27. elif opt in ('--includes', '--cflags'):
  28. flags = ['-I' + sysconfig.get_python_inc(),
  29. '-I' + sysconfig.get_python_inc(plat_specific=True)]
  30. if opt == '--cflags':
  31. flags.extend(getvar('CFLAGS').split())
  32. print(' '.join(flags))
  33. elif opt in ('--libs', '--ldflags'):
  34. libs = getvar('LIBS').split() + getvar('SYSLIBS').split()
  35. libs.append('-lpython'+pyver)
  36. # add the prefix/lib/pythonX.Y/config dir, but only if there is no
  37. # shared library in prefix/lib/.
  38. if opt == '--ldflags' and not getvar('Py_ENABLE_SHARED'):
  39. libs.insert(0, '-L' + getvar('LIBPL'))
  40. print(' '.join(libs))