forked from PyMySQL/mysqlclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_posix.py
168 lines (139 loc) · 4.7 KB
/
setup_posix.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
158
159
160
161
162
163
164
165
166
167
168
import os
import sys
# This dequote() business is required for some older versions
# of mysql_config
def dequote(s):
if not s:
raise Exception(
"Wrong MySQL configuration: maybe https://bugs.mysql.com/bug.php?id=86971 ?"
)
if s[0] in "\"'" and s[0] == s[-1]:
s = s[1:-1]
return s
_mysql_config_path = "mysql_config"
def mysql_config(what):
cmd = "{} --{}".format(_mysql_config_path, what)
print(cmd)
f = os.popen(cmd)
data = f.read().strip().split()
ret = f.close()
if ret:
if ret / 256:
data = []
if ret / 256 > 1:
raise OSError("{} not found".format(_mysql_config_path))
print(data)
return data
def get_config():
from setup_common import get_metadata_and_options, enabled, create_release_file
global _mysql_config_path
metadata, options = get_metadata_and_options()
if "mysql_config" in options:
_mysql_config_path = options["mysql_config"]
else:
try:
mysql_config("version")
except OSError:
# try mariadb_config
_mysql_config_path = "mariadb_config"
try:
mysql_config("version")
except OSError:
_mysql_config_path = "mysql_config"
extra_objects = []
static = enabled(options, "static")
# allow a command-line option to override the base config file to permit
# a static build to be created via requirements.txt
#
if "--static" in sys.argv:
static = True
sys.argv.remove("--static")
libs = os.environ.get("MYSQLCLIENT_LDFLAGS")
if libs:
libs = libs.strip().split()
else:
libs = mysql_config("libs")
library_dirs = [dequote(i[2:]) for i in libs if i.startswith("-L")]
libraries = [dequote(i[2:]) for i in libs if i.startswith("-l")]
extra_link_args = [x for x in libs if not x.startswith(("-l", "-L"))]
cflags = os.environ.get("MYSQLCLIENT_CFLAGS")
if cflags:
use_mysqlconfig_cflags = False
cflags = cflags.strip().split()
else:
use_mysqlconfig_cflags = True
cflags = mysql_config("cflags")
include_dirs = []
extra_compile_args = ["-std=c99"]
for a in cflags:
if a.startswith("-I"):
include_dirs.append(dequote(a[2:]))
elif a.startswith(("-L", "-l")): # This should be LIBS.
pass
else:
extra_compile_args.append(a.replace("%", "%%"))
# Copy the arch flags for linking as well
try:
i = extra_compile_args.index("-arch")
if "-arch" not in extra_link_args:
extra_link_args += ["-arch", extra_compile_args[i + 1]]
except ValueError:
pass
if static:
# properly handle mysql client libraries that are not called libmysqlclient
client = None
CLIENT_LIST = [
"mysqlclient",
"mysqlclient_r",
"mysqld",
"mariadb",
"mariadbclient",
"perconaserverclient",
"perconaserverclient_r",
]
for c in CLIENT_LIST:
if c in libraries:
client = c
break
if client == "mariadb":
client = "mariadbclient"
if client is None:
raise ValueError("Couldn't identify mysql client library")
extra_objects.append(os.path.join(library_dirs[0], "lib%s.a" % client))
if client in libraries:
libraries.remove(client)
else:
if use_mysqlconfig_cflags:
# mysql_config may have "-lmysqlclient -lz -lssl -lcrypto", but zlib and
# ssl is not used by _mysql. They are needed only for static build.
for L in ("crypto", "ssl", "z", "zstd"):
if L in libraries:
libraries.remove(L)
name = "mysqlclient"
metadata["name"] = name
define_macros = [
("version_info", metadata["version_info"]),
("__version__", metadata["version"]),
]
create_release_file(metadata)
del metadata["version_info"]
ext_options = dict(
library_dirs=library_dirs,
libraries=libraries,
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args,
include_dirs=include_dirs,
extra_objects=extra_objects,
define_macros=define_macros,
)
# newer versions of gcc require libstdc++ if doing a static build
if static:
ext_options["language"] = "c++"
print("ext_options:")
for k, v in ext_options.items():
print(" {}: {}".format(k, v))
return metadata, ext_options
if __name__ == "__main__":
sys.stderr.write(
"""You shouldn't be running this directly; it is used by setup.py."""
)