|
1 | 1 | #!/usr/bin/env python
|
2 |
| - |
3 | 2 | import os
|
| 3 | +import subprocess |
| 4 | +import sys |
4 | 5 |
|
5 | 6 | import setuptools
|
| 7 | +from configparser import ConfigParser |
| 8 | + |
| 9 | + |
| 10 | +release_info = {} |
| 11 | +with open("MySQLdb/release.py", encoding="utf-8") as f: |
| 12 | + exec(f.read(), None, release_info) |
| 13 | + |
| 14 | + |
| 15 | +def find_package_name(): |
| 16 | + """Get available pkg-config package name""" |
| 17 | + packages = ["mysqlclient", "mariadb"] |
| 18 | + for pkg in packages: |
| 19 | + try: |
| 20 | + cmd = f"pkg-config --exists {pkg}" |
| 21 | + print(f"Trying {cmd}") |
| 22 | + subprocess.check_call(cmd, shell=True) |
| 23 | + except subprocess.CalledProcessError as err: |
| 24 | + print(err) |
| 25 | + else: |
| 26 | + return pkg |
| 27 | + raise Exception( |
| 28 | + "Can not find valid pkg-config name.\n" |
| 29 | + "Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually" |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +def get_config_posix(options=None): |
| 34 | + # allow a command-line option to override the base config file to permit |
| 35 | + # a static build to be created via requirements.txt |
| 36 | + # TODO: find a better way for |
| 37 | + static = False |
| 38 | + if "--static" in sys.argv: |
| 39 | + static = True |
| 40 | + sys.argv.remove("--static") |
| 41 | + |
| 42 | + ldflags = os.environ.get("MYSQLCLIENT_LDFLAGS") |
| 43 | + cflags = os.environ.get("MYSQLCLIENT_CFLAGS") |
| 44 | + |
| 45 | + pkg_name = None |
| 46 | + static_opt = " --static" if static else "" |
| 47 | + if not (cflags and ldflags): |
| 48 | + pkg_name = find_package_name() |
| 49 | + if not cflags: |
| 50 | + cflags = subprocess.check_output( |
| 51 | + f"pkg-config{static_opt} --cflags {pkg_name}", encoding="utf-8", shell=True |
| 52 | + ) |
| 53 | + if not ldflags: |
| 54 | + ldflags = subprocess.check_output( |
| 55 | + f"pkg-config{static_opt} --libs {pkg_name}", encoding="utf-8", shell=True |
| 56 | + ) |
| 57 | + |
| 58 | + cflags = cflags.split() |
| 59 | + for f in cflags: |
| 60 | + if f.startswith("-std="): |
| 61 | + break |
| 62 | + else: |
| 63 | + cflags += ["-std=c99"] |
| 64 | + |
| 65 | + ldflags = ldflags.split() |
| 66 | + |
| 67 | + define_macros = [ |
| 68 | + ("version_info", release_info["version_info"]), |
| 69 | + ("__version__", release_info["__version__"]), |
| 70 | + ] |
| 71 | + |
| 72 | + ext_options = dict( |
| 73 | + extra_compile_args=cflags, |
| 74 | + extra_link_args=ldflags, |
| 75 | + define_macros=define_macros, |
| 76 | + ) |
| 77 | + # newer versions of gcc require libstdc++ if doing a static build |
| 78 | + if static: |
| 79 | + ext_options["language"] = "c++" |
| 80 | + |
| 81 | + print("Options for building extention module:") |
| 82 | + for k, v in ext_options.items(): |
| 83 | + print(f" {k}: {v}") |
| 84 | + |
| 85 | + return ext_options |
| 86 | + |
| 87 | + |
| 88 | +def get_config_win32(options): |
| 89 | + client = "mariadbclient" |
| 90 | + connector = os.environ.get("MYSQLCLIENT_CONNECTOR", options.get("connector")) |
| 91 | + if not connector: |
| 92 | + connector = os.path.join( |
| 93 | + os.environ["ProgramFiles"], "MariaDB", "MariaDB Connector C" |
| 94 | + ) |
| 95 | + |
| 96 | + extra_objects = [] |
| 97 | + |
| 98 | + library_dirs = [ |
| 99 | + os.path.join(connector, "lib", "mariadb"), |
| 100 | + os.path.join(connector, "lib"), |
| 101 | + ] |
| 102 | + libraries = [ |
| 103 | + "kernel32", |
| 104 | + "advapi32", |
| 105 | + "wsock32", |
| 106 | + "shlwapi", |
| 107 | + "Ws2_32", |
| 108 | + "crypt32", |
| 109 | + "secur32", |
| 110 | + "bcrypt", |
| 111 | + client, |
| 112 | + ] |
| 113 | + include_dirs = [ |
| 114 | + os.path.join(connector, "include", "mariadb"), |
| 115 | + os.path.join(connector, "include"), |
| 116 | + ] |
| 117 | + |
| 118 | + extra_link_args = ["/MANIFEST"] |
| 119 | + |
| 120 | + define_macros = [ |
| 121 | + ("version_info", release_info["version_info"]), |
| 122 | + ("__version__", release_info["__version__"]), |
| 123 | + ] |
| 124 | + |
| 125 | + ext_options = dict( |
| 126 | + library_dirs=library_dirs, |
| 127 | + libraries=libraries, |
| 128 | + extra_link_args=extra_link_args, |
| 129 | + include_dirs=include_dirs, |
| 130 | + extra_objects=extra_objects, |
| 131 | + define_macros=define_macros, |
| 132 | + ) |
| 133 | + return ext_options |
| 134 | + |
| 135 | + |
| 136 | +def enabled(options, option): |
| 137 | + value = options[option] |
| 138 | + s = value.lower() |
| 139 | + if s in ("yes", "true", "1", "y"): |
| 140 | + return True |
| 141 | + elif s in ("no", "false", "0", "n"): |
| 142 | + return False |
| 143 | + else: |
| 144 | + raise ValueError(f"Unknown value {value} for option {option}") |
| 145 | + |
| 146 | + |
| 147 | +def get_options(): |
| 148 | + config = ConfigParser() |
| 149 | + config.read(["site.cfg"]) |
| 150 | + options = dict(config.items("options")) |
| 151 | + options["static"] = enabled(options, "static") |
| 152 | + return options |
| 153 | + |
6 | 154 |
|
7 |
| -if os.name == "posix": |
8 |
| - from setup_posix import get_config |
9 |
| -else: # assume windows |
10 |
| - from setup_windows import get_config |
| 155 | +if sys.platform == "win32": |
| 156 | + ext_options = get_config_win32(get_options()) |
| 157 | +else: |
| 158 | + ext_options = get_config_posix(get_options()) |
11 | 159 |
|
12 |
| -with open("README.md", encoding="utf-8") as f: |
13 |
| - readme = f.read() |
| 160 | +print("# Extention options") |
| 161 | +for k, v in ext_options.items(): |
| 162 | + print(f" {k}: {v}") |
14 | 163 |
|
15 |
| -metadata, options = get_config() |
16 |
| -metadata["ext_modules"] = [ |
17 |
| - setuptools.Extension("MySQLdb._mysql", sources=["MySQLdb/_mysql.c"], **options) |
| 164 | +ext_modules = [ |
| 165 | + setuptools.Extension( |
| 166 | + "MySQLdb._mysql", |
| 167 | + sources=["MySQLdb/_mysql.c"], |
| 168 | + **ext_options, |
| 169 | + ) |
18 | 170 | ]
|
19 |
| -metadata["long_description"] = readme |
20 |
| -metadata["long_description_content_type"] = "text/markdown" |
21 |
| -metadata["python_requires"] = ">=3.7" |
22 |
| -setuptools.setup(**metadata) |
| 171 | +setuptools.setup(ext_modules=ext_modules) |
0 commit comments