- Notifications
You must be signed in to change notification settings - Fork 442
/
Copy pathsetup.py
169 lines (140 loc) · 4.4 KB
/
setup.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
169
#!/usr/bin/env python
importos
importsubprocess
importsys
importsetuptools
fromconfigparserimportConfigParser
release_info= {}
withopen("src/MySQLdb/release.py", encoding="utf-8") asf:
exec(f.read(), None, release_info)
deffind_package_name():
"""Get available pkg-config package name"""
# Ubuntu uses mariadb.pc, but CentOS uses libmariadb.pc
packages= ["mysqlclient", "mariadb", "libmariadb", "perconaserverclient"]
forpkginpackages:
try:
cmd=f"pkg-config --exists {pkg}"
print(f"Trying {cmd}")
subprocess.check_call(cmd, shell=True)
exceptsubprocess.CalledProcessErroraserr:
print(err)
else:
returnpkg
raiseException(
"Can not find valid pkg-config name.\n"
"Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually"
)
defget_config_posix(options=None):
# allow a command-line option to override the base config file to permit
# a static build to be created via requirements.txt
# TODO: find a better way for
static=False
if"--static"insys.argv:
static=True
sys.argv.remove("--static")
ldflags=os.environ.get("MYSQLCLIENT_LDFLAGS")
cflags=os.environ.get("MYSQLCLIENT_CFLAGS")
pkg_name=None
static_opt=" --static"ifstaticelse""
ifnot (cflagsandldflags):
pkg_name=find_package_name()
ifnotcflags:
cflags=subprocess.check_output(
f"pkg-config{static_opt} --cflags {pkg_name}", encoding="utf-8", shell=True
)
ifnotldflags:
ldflags=subprocess.check_output(
f"pkg-config{static_opt} --libs {pkg_name}", encoding="utf-8", shell=True
)
cflags=cflags.split()
forfincflags:
iff.startswith("-std="):
break
else:
cflags+= ["-std=c99"]
ldflags=ldflags.split()
define_macros= [
("version_info", release_info["version_info"]),
("__version__", release_info["__version__"]),
]
ext_options=dict(
extra_compile_args=cflags,
extra_link_args=ldflags,
define_macros=define_macros,
)
# newer versions of gcc require libstdc++ if doing a static build
ifstatic:
ext_options["language"] ="c++"
returnext_options
defget_config_win32(options):
client="mariadbclient"
connector=os.environ.get("MYSQLCLIENT_CONNECTOR", options.get("connector"))
ifnotconnector:
connector=os.path.join(
os.environ["ProgramFiles"], "MariaDB", "MariaDB Connector C"
)
extra_objects= []
library_dirs= [
os.path.join(connector, "lib", "mariadb"),
os.path.join(connector, "lib"),
]
libraries= [
"kernel32",
"advapi32",
"wsock32",
"shlwapi",
"Ws2_32",
"crypt32",
"secur32",
"bcrypt",
client,
]
include_dirs= [
os.path.join(connector, "include", "mariadb"),
os.path.join(connector, "include", "mysql"),
os.path.join(connector, "include"),
]
extra_link_args= ["/MANIFEST"]
define_macros= [
("version_info", release_info["version_info"]),
("__version__", release_info["__version__"]),
]
ext_options=dict(
library_dirs=library_dirs,
libraries=libraries,
extra_link_args=extra_link_args,
include_dirs=include_dirs,
extra_objects=extra_objects,
define_macros=define_macros,
)
returnext_options
defenabled(options, option):
value=options[option]
s=value.lower()
ifsin ("yes", "true", "1", "y"):
returnTrue
elifsin ("no", "false", "0", "n"):
returnFalse
else:
raiseValueError(f"Unknown value {value} for option {option}")
defget_options():
config=ConfigParser()
config.read(["site.cfg"])
options=dict(config.items("options"))
options["static"] =enabled(options, "static")
returnoptions
ifsys.platform=="win32":
ext_options=get_config_win32(get_options())
else:
ext_options=get_config_posix(get_options())
print("# Options for building extension module:")
fork, vinext_options.items():
print(f" {k}: {v}")
ext_modules= [
setuptools.Extension(
"MySQLdb._mysql",
sources=["src/MySQLdb/_mysql.c"],
**ext_options,
)
]
setuptools.setup(ext_modules=ext_modules)