-
Notifications
You must be signed in to change notification settings - Fork 1
/
python-config.py
157 lines (112 loc) · 3.87 KB
/
python-config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# python-config.py
#
# This script tries to find the correct ldflags and cflags through python3
# introspection. See CPython's Misc/python-config.in and configure.ac for
# details.
import sys
import sysconfig
import platform
import pathlib
class MSVC:
def __init__(self):
self.name = "cl"
self.cflags = []
self.ldflags = ["-link"]
def add_library_path(self, path):
self.ldflags.append('-LIBPATH:"' + path + '"')
def add_library(self, lib):
self.ldflags.append(lib + ".lib")
def add_libraries(self, libs):
for lib in libs:
self.add_library(lib)
def add_include_path(self, path):
self.cflags.append('-I"' + path + '"')
class GnuLikeCompiler:
def __init__(self, name):
self.name = name
self.cflags = []
self.ldflags = []
def add_library_path(self, path):
self.ldflags.append('-L"' + path + '"')
def add_library(self, lib):
self.ldflags.append('-l' + lib)
def add_libraries(self, libs):
for lib in libs:
self.add_library(lib)
def add_include_path(self, path):
self.cflags.append('-I"' + path + '"')
getvar = sysconfig.get_config_var
def extend_with_config_var(array, name):
var = getvar(name)
if var is not None:
array.extend(var.split())
def find_compiler():
try:
CC = getvar("CC").lower()
if "clang" in CC:
compiler = GnuLikeCompiler("clang")
elif "gcc" in CC:
compiler = GnuLikeCompiler("gcc")
else:
raise RuntimeError("Unknown compiler")
except Exception:
pycompiler = platform.python_compiler().lower()
if "msc" in pycompiler:
compiler = MSVC()
elif "clang" in pycompiler:
compiler = GnuLikeCompiler("clang")
elif "gcc" in pycompiler:
compiler = GnuLikeCompiler("gcc")
else:
raise RuntimeError("Unknown compiler")
return compiler
# Detect the platform/system
system = platform.system().lower()
if system not in ["linux", "darwin", "windows"]:
raise RuntimeError("Unsupported system")
# Detect the compiler
compiler = find_compiler()
# Get common Python configuration variables
VERSION = getvar("VERSION")
LIBDIR = getvar("LIBDIR")
CONFINCLUDEDIR = getvar("CONFINCLUDEDIR")
try:
abiflags = sys.abiflags
except Exception:
abiflags = getvar("abiflags") or ""
# Configure system specific variables
if system == "windows" and LIBDIR is None:
# Assume libpath is %PYTHONPREFIX%\\libs on Windows
prefix = pathlib.Path(sysconfig.get_config_var("prefix"))
libs = prefix / "libs"
if not libs.exists():
raise RuntimeError("Unable to find python C libraries")
LIBDIR = str(libs)
elif system == "darwin":
# Set @rpath when using clang
PYTHONFRAMEWORKPREFIX = getvar("PYTHONFRAMEWORKPREFIX")
if PYTHONFRAMEWORKPREFIX != "":
compiler.cflags.append("-Wl,-rpath -Wl," + PYTHONFRAMEWORKPREFIX)
# Configure ldflags
compiler.add_library_path(LIBDIR)
compiler.add_library("python" + VERSION + abiflags)
extend_with_config_var(compiler.ldflags, "LIBS")
extend_with_config_var(compiler.ldflags, "SYSLIBS")
if not getvar("Py_ENABLE_SHARED"):
LIBPL = getvar("LIBPL")
if LIBPL is not None:
compiler.add_library_path(LIBPL)
if not getvar("PYTHONFRAMEWORK"):
extend_with_config_var(compiler.ldflags, "LINKFORSHARED")
# Configure cflags
compiler.add_include_path(sysconfig.get_path("include"))
compiler.add_include_path(sysconfig.get_path("platinclude"))
if CONFINCLUDEDIR is not None: # Can be None on Windows
compiler.add_include_path(CONFINCLUDEDIR + "/python" + VERSION + abiflags)
extend_with_config_var(compiler.cflags, "CFLAGS")
# The output is parsed by gsc, one line at a time:
print(VERSION)
print(compiler.name)
print(" ".join(compiler.ldflags))
print(" ".join(compiler.cflags))
print(LIBDIR)