-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsetup.py
177 lines (142 loc) · 6.56 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
170
171
172
173
174
175
176
177
import glob
import os
import shutil
import subprocess
import warnings
from sys import platform
from setuptools import setup
HERE = os.path.abspath(os.path.dirname(__file__))
BUILD_DIR = os.path.join(HERE, "pymongoarrow")
IS_WIN = platform == "win32"
# Find and copy the binary libbson file, unless
# MONGO_NO_COPY_LIBBSON is set (for instance in a conda build).
COPY_LIBBSON = not os.environ.get("MONGO_NO_COPY_LIBBSON", "")
# Whether to create libarrow symlinks on posix systems.
CREATE_LIBARROW_SYMLINKS = os.environ.get("MONGO_CREATE_LIBARROW_SYMLINKS", "1")
# Set a default value for MACOSX_DEPLOYMENT_TARGET.
os.environ.setdefault("MACOSX_DEPLOYMENT_TARGET", "10.15")
def query_pkgconfig(cmd):
status, output = subprocess.getstatusoutput(cmd)
if status != 0:
warnings.warn(output, UserWarning)
return None
return output
def get_min_libbson_version():
version_ns = {}
here = os.path.dirname(__file__)
version_py = os.path.join(here, "pymongoarrow", "version.py")
with open(version_py) as f:
exec(compile(f.read(), version_py, "exec"), version_ns)
return version_ns["_MIN_LIBBSON_VERSION"]
def append_libbson_flags(module):
pc_path = "libbson-1.0"
install_dir = os.environ.get("LIBBSON_INSTALL_DIR")
if install_dir:
install_dir = os.path.abspath(install_dir)
# Handle the copy-able library file if applicable.
if COPY_LIBBSON:
if platform == "darwin":
lib_file = "libbson-1.0.0.dylib"
elif platform == "linux":
lib_file = "libbson-1.0.so.0"
else: # windows
lib_file = "bson-1.0.dll"
lib_dir = "bin" if IS_WIN else "lib*"
lib_dir = glob.glob(os.path.join(install_dir, lib_dir))
if lib_dir:
lib_file = os.path.join(lib_dir[0], lib_file)
if os.path.exists(lib_file):
shutil.copy(lib_file, BUILD_DIR)
# Ensure our Cython extension can dynamically link to libraries
# - https://blog.krzyzanowskim.com/2018/12/05/rpath-what/
# - https://nehckl0.medium.com/creating-relocatable-linux-executables-by-setting-rpath-with-origin-45de573a2e98
if platform == "darwin":
module.extra_link_args += ["-rpath", "@loader_path"]
elif platform == "linux":
module.extra_link_args += ["-Wl,-rpath,$ORIGIN"]
# Find the linkable library file, and explicitly add it to the linker if on Windows.
lib_dirs = glob.glob(os.path.join(install_dir, "lib*"))
if len(lib_dirs) != 1:
warnings.warn(f"Unable to locate libbson in {install_dir}")
if IS_WIN:
raise ValueError(
"We require a LIBBSON_INSTALL_DIR with a compiled library on Windows"
)
else:
lib_dir = lib_dirs[0]
if IS_WIN:
# Note: we replace any forward slashes with backslashes so the path
# can be parsed by bash.
lib_path = os.path.join(lib_dir, "bson-1.0.lib").replace(os.sep, "/")
if os.path.exists(lib_path):
module.extra_link_args = [lib_path]
include_dir = os.path.join(install_dir, "include", "libbson-1.0").replace(
os.sep, "/"
)
module.include_dirs.append(include_dir)
else:
raise ValueError(f"Could not find the compiled libbson in {install_dir}")
pc_path = os.path.join(install_dir, lib_dir, "pkgconfig", "libbson-1.0.pc")
elif IS_WIN:
raise ValueError("We require a LIBBSON_INSTALL_DIR with a compiled library on Windows")
if IS_WIN:
# We have added the library file without raising an error, so return.
return
# Check for the existence of the library.
lnames = query_pkgconfig(f"pkg-config --libs-only-l {pc_path}")
if not lnames:
raise ValueError(f'Could not find "{pc_path}" library')
# Check against the minimum required version.
min_version = get_min_libbson_version()
mod_version = query_pkgconfig(f"pkg-config --modversion {pc_path}")
if mod_version < min_version:
raise ValueError(
f"Cannot use {pc_path} with version {mod_version}, minimum required version is {min_version}"
)
# Gather the appropriate flags.
cflags = query_pkgconfig(f"pkg-config --cflags {pc_path}")
if cflags:
orig_cflags = os.environ.get("CFLAGS", "")
os.environ["CFLAGS"] = cflags + " " + orig_cflags
ldflags = query_pkgconfig(f"pkg-config --libs {pc_path}")
if ldflags:
orig_ldflags = os.environ.get("LDFLAGS", "")
os.environ["LDFLAGS"] = ldflags + " " + orig_ldflags
# https://cython.readthedocs.io/en/latest/src/tutorial/external.html#dynamic-linking
# Strip whitespace to avoid weird linker failures on manylinux images
libnames = [lname.lstrip("-l").strip() for lname in lnames.split()]
module.libraries.extend(libnames)
def append_arrow_flags(ext):
import numpy as np
import pyarrow as pa
# From https://arrow.apache.org/docs/python/integration/extending.html#example
# The Numpy C headers are currently required
ext.include_dirs.append(np.get_include())
ext.include_dirs.append(pa.get_include())
ext.libraries.extend(pa.get_libraries())
ext.library_dirs.extend(pa.get_library_dirs())
if os.name != "nt" and CREATE_LIBARROW_SYMLINKS:
# On Linux and MacOS, we must run pyarrow.create_library_symlinks()
# as a user with write access to the directory where pyarrow is
# installed.
# See https://arrow.apache.org/docs/python/integration/extending.html#building-extensions-against-pypi-wheels.
pa.create_library_symlinks()
if os.name == "posix":
ext.extra_compile_args.append("-std=c++17")
elif os.name == "nt":
ext.extra_compile_args.append("/std:c++17")
def get_extension_modules():
# This change is needed in order to allow setuptools to import the
# library to obtain metadata information outside of a build environment.
try:
from Cython.Build import cythonize
except ImportError:
warnings.warn("Cannot compile native C code, because of a missing build dependency")
return []
modules = cythonize(["pymongoarrow/*.pyx"])
for module in modules:
append_libbson_flags(module)
append_arrow_flags(module)
return modules
ext_modules = [] if os.environ.get("NO_EXT") else get_extension_modules()
setup(ext_modules=ext_modules)