1# Program to fetch python compilation parameters. 2# Copied from python-config of the 2.7 release. 3 4import sys 5import os 6import getopt 7from distutils import sysconfig 8 9valid_opts = ["prefix", "exec-prefix", "includes", "libs", "cflags", "ldflags", "help"] 10 11 12def exit_with_usage(code=1): 13 sys.stderr.write( 14 "Usage: %s [%s]\n" % (sys.argv[0], "|".join("--" + opt for opt in valid_opts)) 15 ) 16 sys.exit(code) 17 18 19try: 20 opts, args = getopt.getopt(sys.argv[1:], "", valid_opts) 21except getopt.error: 22 exit_with_usage() 23 24if not opts: 25 exit_with_usage() 26 27pyver = sysconfig.get_config_var("VERSION") 28getvar = sysconfig.get_config_var 29abiflags = getattr(sys, "abiflags", "") 30 31opt_flags = [flag for (flag, val) in opts] 32 33if "--help" in opt_flags: 34 exit_with_usage(code=0) 35 36 37def 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. 42 43 On Unix systems, returns the path unchanged. 44 """ 45 if os.name == "nt": 46 path = path.replace("\\", "/") 47 return path 48 49 50for opt in opt_flags: 51 if opt == "--prefix": 52 print(to_unix_path(sysconfig.PREFIX)) 53 54 elif opt == "--exec-prefix": 55 print(to_unix_path(sysconfig.EXEC_PREFIX)) 56 57 elif opt in ("--includes", "--cflags"): 58 flags = [ 59 "-I" + sysconfig.get_python_inc(), 60 "-I" + sysconfig.get_python_inc(plat_specific=True), 61 ] 62 if opt == "--cflags": 63 flags.extend(getvar("CFLAGS").split()) 64 print(to_unix_path(" ".join(flags))) 65 66 elif opt in ("--libs", "--ldflags"): 67 libs = ["-lpython" + pyver + abiflags] 68 if getvar('LDFLAGS') is not None: 69 libs.extend(getvar('LDFLAGS').split()) 70 if getvar("LIBS") is not None: 71 libs.extend(getvar("LIBS").split()) 72 if getvar("SYSLIBS") is not None: 73 libs.extend(getvar("SYSLIBS").split()) 74 # add the prefix/lib/pythonX.Y/config dir, but only if there is no 75 # shared library in prefix/lib/. 76 if opt == "--ldflags": 77 if not getvar("Py_ENABLE_SHARED"): 78 if getvar("LIBPL") is not None: 79 libs.insert(0, "-L" + getvar("LIBPL")) 80 elif os.name == "nt": 81 libs.insert(0, "-L" + sysconfig.PREFIX + "/libs") 82 if getvar("LINKFORSHARED") is not None: 83 libs.extend(getvar("LINKFORSHARED").split()) 84 print(to_unix_path(" ".join(libs))) 85