manual copyright year range of various GDB files to add 2023
[binutils-gdb.git] / gdb / python / python-config.py
blobe89e0fd40bb01731c63033664d814ba92c125e42
1 # Program to fetch python compilation parameters.
2 # Copied from python-config of the 2.7 release.
4 import sys
5 import os
6 import getopt
7 import sysconfig
9 valid_opts = ["prefix", "exec-prefix", "includes", "libs", "cflags", "ldflags", "help"]
12 def exit_with_usage(code=1):
13 sys.stderr.write(
14 "Usage: %s [%s]\n" % (sys.argv[0], "|".join("--" + opt for opt in valid_opts))
16 sys.exit(code)
19 try:
20 opts, args = getopt.getopt(sys.argv[1:], "", valid_opts)
21 except getopt.error:
22 exit_with_usage()
24 if not opts:
25 exit_with_usage()
27 pyver = sysconfig.get_config_var("VERSION")
28 getvar = sysconfig.get_config_var
29 abiflags = getattr(sys, "abiflags", "")
31 opt_flags = [flag for (flag, val) in opts]
33 if "--help" in opt_flags:
34 exit_with_usage(code=0)
37 def to_unix_path(path):
38 """On Windows, returns the given path with all backslashes
39 converted into forward slashes. This is to help prevent problems
40 when using the paths returned by this script with cygwin tools.
41 In particular, cygwin bash treats backslashes as a special character.
43 On Unix systems, returns the path unchanged.
44 """
45 if os.name == "nt":
46 path = path.replace("\\", "/")
47 return path
50 for opt in opt_flags:
51 if opt == "--prefix":
52 print(to_unix_path(os.path.normpath(sys.prefix)))
54 elif opt == "--exec-prefix":
55 print(to_unix_path(os.path.normpath(sys.exec_prefix)))
57 elif opt in ("--includes", "--cflags"):
58 flags = [
59 "-I" + sysconfig.get_path("include"),
60 "-I" + sysconfig.get_path("platinclude"),
62 if opt == "--cflags":
63 flags.extend(getvar("CFLAGS").split())
64 print(to_unix_path(" ".join(flags)))
66 elif opt in ("--libs", "--ldflags"):
67 libs = ["-lpython" + pyver + abiflags]
68 if getvar("LIBS") is not None:
69 libs.extend(getvar("LIBS").split())
70 if getvar("SYSLIBS") is not None:
71 libs.extend(getvar("SYSLIBS").split())
72 # add the prefix/lib/pythonX.Y/config dir, but only if there is no
73 # shared library in prefix/lib/.
74 if opt == "--ldflags":
75 if not getvar("Py_ENABLE_SHARED"):
76 if getvar("LIBPL") is not None:
77 libs.insert(0, "-L" + getvar("LIBPL"))
78 elif os.name == "nt":
79 libs.insert(0, "-L" + os.path.normpath(sys.prefix) + "/libs")
80 if getvar("LINKFORSHARED") is not None:
81 libs.extend(getvar("LINKFORSHARED").split())
82 print(to_unix_path(" ".join(libs)))