python-config.in 1.98 KB
Newer Older
1
#!@EXENAME@
2
# -*- python -*-
3

4 5
# Keep this script in sync with python-config.sh.in

6
import getopt
7 8 9
import os
import sys
import sysconfig
10

11
valid_opts = ['prefix', 'exec-prefix', 'includes', 'libs', 'cflags',
12
              'ldflags', 'extension-suffix', 'help', 'abiflags', 'configdir']
13 14

def exit_with_usage(code=1):
15 16
    print("Usage: {0} [{1}]".format(
        sys.argv[0], '|'.join('--'+opt for opt in valid_opts)), file=sys.stderr)
17 18 19 20 21 22 23 24 25 26 27 28 29
    sys.exit(code)

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

if not opts:
    exit_with_usage()

pyver = sysconfig.get_config_var('VERSION')
getvar = sysconfig.get_config_var

30 31 32 33 34 35 36
opt_flags = [flag for (flag, val) in opts]

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

for opt in opt_flags:
    if opt == '--prefix':
37
        print(sysconfig.get_config_var('prefix'))
38 39

    elif opt == '--exec-prefix':
40
        print(sysconfig.get_config_var('exec_prefix'))
41 42

    elif opt in ('--includes', '--cflags'):
43 44
        flags = ['-I' + sysconfig.get_path('include'),
                 '-I' + sysconfig.get_path('platinclude')]
45 46 47 48 49
        if opt == '--cflags':
            flags.extend(getvar('CFLAGS').split())
        print(' '.join(flags))

    elif opt in ('--libs', '--ldflags'):
50 51 52
        libs = ['-lpython' + pyver + sys.abiflags]
        libs += getvar('LIBS').split()
        libs += getvar('SYSLIBS').split()
53 54 55 56 57
        # add the prefix/lib/pythonX.Y/config dir, but only if there is no
        # shared library in prefix/lib/.
        if opt == '--ldflags':
            if not getvar('Py_ENABLE_SHARED'):
                libs.insert(0, '-L' + getvar('LIBPL'))
58 59
            if not getvar('PYTHONFRAMEWORK'):
                libs.extend(getvar('LINKFORSHARED').split())
60
        print(' '.join(libs))
61

62
    elif opt == '--extension-suffix':
63
        print(sysconfig.get_config_var('EXT_SUFFIX'))
64 65 66

    elif opt == '--abiflags':
        print(sys.abiflags)
67 68 69

    elif opt == '--configdir':
        print(sysconfig.get_config_var('LIBPL'))