Commit | Line | Data |
---|---|---|
ec685c5e | 1 | # Program to fetch python compilation parameters. |
7e6e39e5 | 2 | # Copied from python-config of the 2.7 release. |
ec685c5e DE |
3 | |
4 | import sys | |
5 | import os | |
6 | import getopt | |
7 | from distutils import sysconfig | |
8 | ||
7e6e39e5 | 9 | valid_opts = ['prefix', 'exec-prefix', 'includes', 'libs', 'cflags', |
ec685c5e DE |
10 | 'ldflags', 'help'] |
11 | ||
12 | def exit_with_usage(code=1): | |
9a27f2c6 PK |
13 | sys.stderr.write ("Usage: %s [%s]\n" % (sys.argv[0], |
14 | '|'.join('--'+opt for opt in valid_opts))) | |
ec685c5e DE |
15 | sys.exit(code) |
16 | ||
17 | try: | |
18 | opts, args = getopt.getopt(sys.argv[1:], '', valid_opts) | |
19 | except getopt.error: | |
20 | exit_with_usage() | |
21 | ||
22 | if not opts: | |
23 | exit_with_usage() | |
24 | ||
ec685c5e DE |
25 | pyver = sysconfig.get_config_var('VERSION') |
26 | getvar = sysconfig.get_config_var | |
9a27f2c6 | 27 | abiflags = getattr (sys, "abiflags", "") |
ec685c5e | 28 | |
7e6e39e5 JB |
29 | opt_flags = [flag for (flag, val) in opts] |
30 | ||
31 | if '--help' in opt_flags: | |
32 | exit_with_usage(code=0) | |
33 | ||
9c4ea6c5 JB |
34 | def to_unix_path(path): |
35 | """On Windows, returns the given path with all backslashes | |
36 | converted into forward slashes. This is to help prevent problems | |
37 | when using the paths returned by this script with cygwin tools. | |
38 | In particular, cygwin bash treats backslashes as a special character. | |
39 | ||
40 | On Unix systems, returns the path unchanged. | |
41 | """ | |
42 | if os.name == 'nt': | |
43 | path = path.replace('\\', '/') | |
44 | return path | |
45 | ||
7e6e39e5 JB |
46 | for opt in opt_flags: |
47 | if opt == '--prefix': | |
9a27f2c6 | 48 | print (to_unix_path(sysconfig.PREFIX)) |
7e6e39e5 JB |
49 | |
50 | elif opt == '--exec-prefix': | |
9a27f2c6 | 51 | print (to_unix_path(sysconfig.EXEC_PREFIX)) |
7e6e39e5 JB |
52 | |
53 | elif opt in ('--includes', '--cflags'): | |
54 | flags = ['-I' + sysconfig.get_python_inc(), | |
55 | '-I' + sysconfig.get_python_inc(plat_specific=True)] | |
56 | if opt == '--cflags': | |
57 | flags.extend(getvar('CFLAGS').split()) | |
9a27f2c6 | 58 | print (to_unix_path(' '.join(flags))) |
7e6e39e5 JB |
59 | |
60 | elif opt in ('--libs', '--ldflags'): | |
bf27f0e2 | 61 | libs = ['-lpython' + pyver + abiflags] |
9c4ea6c5 JB |
62 | if getvar('LIBS') is not None: |
63 | libs.extend(getvar('LIBS').split()) | |
64 | if getvar('SYSLIBS') is not None: | |
65 | libs.extend(getvar('SYSLIBS').split()) | |
7e6e39e5 JB |
66 | # add the prefix/lib/pythonX.Y/config dir, but only if there is no |
67 | # shared library in prefix/lib/. | |
68 | if opt == '--ldflags': | |
69 | if not getvar('Py_ENABLE_SHARED'): | |
9c4ea6c5 JB |
70 | if getvar('LIBPL') is not None: |
71 | libs.insert(0, '-L' + getvar('LIBPL')) | |
72 | elif os.name == 'nt': | |
73 | libs.insert(0, '-L' + sysconfig.PREFIX + '/libs') | |
74 | if getvar('LINKFORSHARED') is not None: | |
75 | libs.extend(getvar('LINKFORSHARED').split()) | |
9a27f2c6 | 76 | print (to_unix_path(' '.join(libs))) |
ec685c5e | 77 |