]>
Commit | Line | Data |
---|---|---|
1 | # Program to fetch python compilation parameters. | |
2 | # Copied from python-config of the 2.7 release. | |
3 | ||
4 | import getopt | |
5 | import os | |
6 | import sys | |
7 | import sysconfig | |
8 | ||
9 | valid_opts = ["prefix", "exec-prefix", "includes", "libs", "cflags", "ldflags", "help"] | |
10 | ||
11 | ||
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)) | |
15 | ) | |
16 | sys.exit(code) | |
17 | ||
18 | ||
19 | try: | |
20 | opts, args = getopt.getopt(sys.argv[1:], "", valid_opts) | |
21 | except getopt.error: | |
22 | exit_with_usage() | |
23 | ||
24 | if not opts: | |
25 | exit_with_usage() | |
26 | ||
27 | pyver = sysconfig.get_config_var("VERSION") | |
28 | getvar = sysconfig.get_config_var | |
29 | abiflags = getattr(sys, "abiflags", "") | |
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 | ||
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. | |
42 | ||
43 | On Unix systems, returns the path unchanged. | |
44 | """ | |
45 | if os.name == "nt": | |
46 | path = path.replace("\\", "/") | |
47 | return path | |
48 | ||
49 | ||
50 | for opt in opt_flags: | |
51 | if opt == "--prefix": | |
52 | print(to_unix_path(os.path.normpath(sys.prefix))) | |
53 | ||
54 | elif opt == "--exec-prefix": | |
55 | print(to_unix_path(os.path.normpath(sys.exec_prefix))) | |
56 | ||
57 | elif opt in ("--includes", "--cflags"): | |
58 | flags = [ | |
59 | "-I" + sysconfig.get_path("include"), | |
60 | "-I" + sysconfig.get_path("platinclude"), | |
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("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))) |