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