| 1 |
# Copyright 2015 gRPC authors. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
"""A setup module for the GRPC Python package.""" |
| 15 |
|
| 16 |
# NOTE(https://github.com/grpc/grpc/issues/24028): allow setuptools to monkey |
| 17 |
# patch distutils |
| 18 |
import setuptools # isort:skip |
| 19 |
|
| 20 |
# Monkey Patch the unix compiler to accept ASM |
| 21 |
# files used by boring SSL. |
| 22 |
from distutils.unixccompiler import UnixCCompiler |
| 23 |
|
| 24 |
UnixCCompiler.src_extensions.append('.S') |
| 25 |
del UnixCCompiler |
| 26 |
|
| 27 |
from distutils import cygwinccompiler |
| 28 |
from distutils import extension as _extension |
| 29 |
from distutils import util |
| 30 |
import os |
| 31 |
import os.path |
| 32 |
import pathlib |
| 33 |
import platform |
| 34 |
import re |
| 35 |
import shlex |
| 36 |
import shutil |
| 37 |
import subprocess |
| 38 |
from subprocess import PIPE |
| 39 |
import sys |
| 40 |
import sysconfig |
| 41 |
|
| 42 |
import _metadata |
| 43 |
import pkg_resources |
| 44 |
from setuptools.command import egg_info |
| 45 |
|
| 46 |
# Redirect the manifest template from MANIFEST.in to PYTHON-MANIFEST.in. |
| 47 |
egg_info.manifest_maker.template = 'PYTHON-MANIFEST.in' |
| 48 |
|
| 49 |
PY3 = sys.version_info.major == 3 |
| 50 |
PYTHON_STEM = os.path.join('src', 'python', 'grpcio') |
| 51 |
CORE_INCLUDE = ( |
| 52 |
'include', |
| 53 |
'.', |
| 54 |
) |
| 55 |
ABSL_INCLUDE = (os.path.join('third_party', 'abseil-cpp'),) |
| 56 |
ADDRESS_SORTING_INCLUDE = (os.path.join('third_party', 'address_sorting', |
| 57 |
'include'),) |
| 58 |
CARES_INCLUDE = ( |
| 59 |
os.path.join('third_party', 'cares', 'cares', 'include'), |
| 60 |
os.path.join('third_party', 'cares'), |
| 61 |
os.path.join('third_party', 'cares', 'cares'), |
| 62 |
) |
| 63 |
if 'darwin' in sys.platform: |
| 64 |
CARES_INCLUDE += (os.path.join('third_party', 'cares', 'config_darwin'),) |
| 65 |
if 'freebsd' in sys.platform: |
| 66 |
CARES_INCLUDE += (os.path.join('third_party', 'cares', 'config_freebsd'),) |
| 67 |
if 'linux' in sys.platform: |
| 68 |
CARES_INCLUDE += (os.path.join('third_party', 'cares', 'config_linux'),) |
| 69 |
if 'openbsd' in sys.platform: |
| 70 |
CARES_INCLUDE += (os.path.join('third_party', 'cares', 'config_openbsd'),) |
| 71 |
RE2_INCLUDE = (os.path.join('third_party', 're2'),) |
| 72 |
SSL_INCLUDE = (os.path.join('third_party', 'boringssl-with-bazel', 'src', |
| 73 |
'include'),) |
| 74 |
UPB_INCLUDE = (os.path.join('third_party', 'upb'),) |
| 75 |
UPB_GRPC_GENERATED_INCLUDE = (os.path.join('src', 'core', 'ext', |
| 76 |
'upb-generated'),) |
| 77 |
UPBDEFS_GRPC_GENERATED_INCLUDE = (os.path.join('src', 'core', 'ext', |
| 78 |
'upbdefs-generated'),) |
| 79 |
XXHASH_INCLUDE = (os.path.join('third_party', 'xxhash'),) |
| 80 |
ZLIB_INCLUDE = (os.path.join('third_party', 'zlib'),) |
| 81 |
README = os.path.join(PYTHON_STEM, 'README.rst') |
| 82 |
|
| 83 |
# Ensure we're in the proper directory whether or not we're being used by pip. |
| 84 |
os.chdir(os.path.dirname(os.path.abspath(__file__))) |
| 85 |
sys.path.insert(0, os.path.abspath(PYTHON_STEM)) |
| 86 |
|
| 87 |
# Break import-style to ensure we can actually find our in-repo dependencies. |
| 88 |
import _parallel_compile_patch |
| 89 |
import _spawn_patch |
| 90 |
import grpc_core_dependencies |
| 91 |
|
| 92 |
import commands |
| 93 |
import grpc_version |
| 94 |
|
| 95 |
_parallel_compile_patch.monkeypatch_compile_maybe() |
| 96 |
_spawn_patch.monkeypatch_spawn() |
| 97 |
|
| 98 |
LICENSE = 'Apache License 2.0' |
| 99 |
|
| 100 |
CLASSIFIERS = [ |
| 101 |
'Development Status :: 5 - Production/Stable', |
| 102 |
'Programming Language :: Python', |
| 103 |
'Programming Language :: Python :: 3', |
| 104 |
'Programming Language :: Python :: 3.7', |
| 105 |
'Programming Language :: Python :: 3.8', |
| 106 |
'Programming Language :: Python :: 3.9', |
| 107 |
'Programming Language :: Python :: 3.10', |
| 108 |
'Programming Language :: Python :: 3.11', |
| 109 |
'License :: OSI Approved :: Apache Software License', |
| 110 |
] |
| 111 |
|
| 112 |
|
| 113 |
def _env_bool_value(env_name, default): |
| 114 |
"""Parses a bool option from an environment variable""" |
| 115 |
return os.environ.get(env_name, default).upper() not in ['FALSE', '0', ''] |
| 116 |
|
| 117 |
|
| 118 |
BUILD_WITH_BORING_SSL_ASM = _env_bool_value('GRPC_BUILD_WITH_BORING_SSL_ASM', |
| 119 |
'True') |
| 120 |
|
| 121 |
# Export this environment variable to override the platform variant that will |
| 122 |
# be chosen for boringssl assembly optimizations. This option is useful when |
| 123 |
# crosscompiling and the host platform as obtained by distutils.utils.get_platform() |
| 124 |
# doesn't match the platform we are targetting. |
| 125 |
# Example value: "linux-aarch64" |
| 126 |
BUILD_OVERRIDE_BORING_SSL_ASM_PLATFORM = os.environ.get( |
| 127 |
'GRPC_BUILD_OVERRIDE_BORING_SSL_ASM_PLATFORM', '') |
| 128 |
|
| 129 |
# Environment variable to determine whether or not the Cython extension should |
| 130 |
# *use* Cython or use the generated C files. Note that this requires the C files |
| 131 |
# to have been generated by building first *with* Cython support. Even if this |
| 132 |
# is set to false, if the script detects that the generated `.c` file isn't |
| 133 |
# present, then it will still attempt to use Cython. |
| 134 |
BUILD_WITH_CYTHON = _env_bool_value('GRPC_PYTHON_BUILD_WITH_CYTHON', 'False') |
| 135 |
|
| 136 |
# Export this variable to use the system installation of openssl. You need to |
| 137 |
# have the header files installed (in /usr/include/openssl) and during |
| 138 |
# runtime, the shared library must be installed |
| 139 |
BUILD_WITH_SYSTEM_OPENSSL = _env_bool_value('GRPC_PYTHON_BUILD_SYSTEM_OPENSSL', |
| 140 |
'False') |
| 141 |
|
| 142 |
# Export this variable to use the system installation of zlib. You need to |
| 143 |
# have the header files installed (in /usr/include/) and during |
| 144 |
# runtime, the shared library must be installed |
| 145 |
BUILD_WITH_SYSTEM_ZLIB = _env_bool_value('GRPC_PYTHON_BUILD_SYSTEM_ZLIB', |
| 146 |
'False') |
| 147 |
|
| 148 |
# Export this variable to use the system installation of cares. You need to |
| 149 |
# have the header files installed (in /usr/include/) and during |
| 150 |
# runtime, the shared library must be installed |
| 151 |
BUILD_WITH_SYSTEM_CARES = _env_bool_value('GRPC_PYTHON_BUILD_SYSTEM_CARES', |
| 152 |
'False') |
| 153 |
|
| 154 |
# Export this variable to use the system installation of re2. You need to |
| 155 |
# have the header files installed (in /usr/include/re2) and during |
| 156 |
# runtime, the shared library must be installed |
| 157 |
BUILD_WITH_SYSTEM_RE2 = _env_bool_value('GRPC_PYTHON_BUILD_SYSTEM_RE2', 'False') |
| 158 |
|
| 159 |
# Export this variable to use the system installation of abseil. You need to |
| 160 |
# have the header files installed (in /usr/include/absl) and during |
| 161 |
# runtime, the shared library must be installed |
| 162 |
BUILD_WITH_SYSTEM_ABSL = os.environ.get('GRPC_PYTHON_BUILD_SYSTEM_ABSL', False) |
| 163 |
|
| 164 |
# Export this variable to force building the python extension with a statically linked libstdc++. |
| 165 |
# At least on linux, this is normally not needed as we can build manylinux-compatible wheels on linux just fine |
| 166 |
# without statically linking libstdc++ (which leads to a slight increase in the wheel size). |
| 167 |
# This option is useful when crosscompiling wheels for aarch64 where |
| 168 |
# it's difficult to ensure that the crosscompilation toolchain has a high-enough version |
| 169 |
# of GCC (we require >=5.1) but still uses old-enough libstdc++ symbols. |
| 170 |
# TODO(jtattermusch): remove this workaround once issues with crosscompiler version are resolved. |
| 171 |
BUILD_WITH_STATIC_LIBSTDCXX = _env_bool_value( |
| 172 |
'GRPC_PYTHON_BUILD_WITH_STATIC_LIBSTDCXX', 'False') |
| 173 |
|
| 174 |
# For local development use only: This skips building gRPC Core and its |
| 175 |
# dependencies, including protobuf and boringssl. This allows "incremental" |
| 176 |
# compilation by first building gRPC Core using make, then building only the |
| 177 |
# Python/Cython layers here. |
| 178 |
# |
| 179 |
# Note that this requires libboringssl.a in the libs/{dbg,opt}/ directory, which |
| 180 |
# may require configuring make to not use the system openssl implementation: |
| 181 |
# |
| 182 |
# make HAS_SYSTEM_OPENSSL_ALPN=0 |
| 183 |
# |
| 184 |
# TODO(ericgribkoff) Respect the BUILD_WITH_SYSTEM_* flags alongside this option |
| 185 |
USE_PREBUILT_GRPC_CORE = _env_bool_value('GRPC_PYTHON_USE_PREBUILT_GRPC_CORE', |
| 186 |
'False') |
| 187 |
|
| 188 |
# If this environmental variable is set, GRPC will not try to be compatible with |
| 189 |
# libc versions old than the one it was compiled against. |
| 190 |
DISABLE_LIBC_COMPATIBILITY = _env_bool_value( |
| 191 |
'GRPC_PYTHON_DISABLE_LIBC_COMPATIBILITY', 'False') |
| 192 |
|
| 193 |
# Environment variable to determine whether or not to enable coverage analysis |
| 194 |
# in Cython modules. |
| 195 |
ENABLE_CYTHON_TRACING = _env_bool_value('GRPC_PYTHON_ENABLE_CYTHON_TRACING', |
| 196 |
'False') |
| 197 |
|
| 198 |
# Environment variable specifying whether or not there's interest in setting up |
| 199 |
# documentation building. |
| 200 |
ENABLE_DOCUMENTATION_BUILD = _env_bool_value( |
| 201 |
'GRPC_PYTHON_ENABLE_DOCUMENTATION_BUILD', 'False') |
| 202 |
|
| 203 |
|
| 204 |
def check_linker_need_libatomic(): |
| 205 |
"""Test if linker on system needs libatomic.""" |
| 206 |
code_test = (b'#include <atomic>\n' + |
| 207 |
b'int main() { return std::atomic<int64_t>{}; }') |
| 208 |
cxx = shlex.split(os.environ.get('CXX', 'c++')) |
| 209 |
cpp_test = subprocess.Popen(cxx + ['-x', 'c++', '-std=c++17', '-'], |
| 210 |
stdin=PIPE, |
| 211 |
stdout=PIPE, |
| 212 |
stderr=PIPE) |
| 213 |
cpp_test.communicate(input=code_test) |
| 214 |
if cpp_test.returncode == 0: |
| 215 |
return False |
| 216 |
# Double-check to see if -latomic actually can solve the problem. |
| 217 |
# https://github.com/grpc/grpc/issues/22491 |
| 218 |
cpp_test = subprocess.Popen(cxx + |
| 219 |
['-x', 'c++', '-std=c++17', '-', '-latomic'], |
| 220 |
stdin=PIPE, |
| 221 |
stdout=PIPE, |
| 222 |
stderr=PIPE) |
| 223 |
cpp_test.communicate(input=code_test) |
| 224 |
return cpp_test.returncode == 0 |
| 225 |
|
| 226 |
|
| 227 |
# There are some situations (like on Windows) where CC, CFLAGS, and LDFLAGS are |
| 228 |
# entirely ignored/dropped/forgotten by distutils and its Cygwin/MinGW support. |
| 229 |
# We use these environment variables to thus get around that without locking |
| 230 |
# ourselves in w.r.t. the multitude of operating systems this ought to build on. |
| 231 |
# We can also use these variables as a way to inject environment-specific |
| 232 |
# compiler/linker flags. We assume GCC-like compilers and/or MinGW as a |
| 233 |
# reasonable default. |
| 234 |
EXTRA_ENV_COMPILE_ARGS = os.environ.get('GRPC_PYTHON_CFLAGS', None) |
| 235 |
EXTRA_ENV_LINK_ARGS = os.environ.get('GRPC_PYTHON_LDFLAGS', None) |
| 236 |
if EXTRA_ENV_COMPILE_ARGS is None: |
| 237 |
EXTRA_ENV_COMPILE_ARGS = ' -std=c++17' |
| 238 |
if 'win32' in sys.platform: |
| 239 |
if sys.version_info < (3, 5): |
| 240 |
EXTRA_ENV_COMPILE_ARGS += ' -D_hypot=hypot' |
| 241 |
# We use define flags here and don't directly add to DEFINE_MACROS below to |
| 242 |
# ensure that the expert user/builder has a way of turning it off (via the |
| 243 |
# envvars) without adding yet more GRPC-specific envvars. |
| 244 |
# See https://sourceforge.net/p/mingw-w64/bugs/363/ |
| 245 |
if '32' in platform.architecture()[0]: |
| 246 |
EXTRA_ENV_COMPILE_ARGS += ' -D_ftime=_ftime32 -D_timeb=__timeb32 -D_ftime_s=_ftime32_s' |
| 247 |
else: |
| 248 |
EXTRA_ENV_COMPILE_ARGS += ' -D_ftime=_ftime64 -D_timeb=__timeb64' |
| 249 |
else: |
| 250 |
# We need to statically link the C++ Runtime, only the C runtime is |
| 251 |
# available dynamically |
| 252 |
EXTRA_ENV_COMPILE_ARGS += ' /MT' |
| 253 |
elif "linux" in sys.platform: |
| 254 |
EXTRA_ENV_COMPILE_ARGS += ' -fvisibility=hidden -fno-wrapv -fno-exceptions' |
| 255 |
elif "darwin" in sys.platform: |
| 256 |
EXTRA_ENV_COMPILE_ARGS += ' -stdlib=libc++ -fvisibility=hidden -fno-wrapv -fno-exceptions -DHAVE_UNISTD_H' |
| 257 |
|
| 258 |
if EXTRA_ENV_LINK_ARGS is None: |
| 259 |
EXTRA_ENV_LINK_ARGS = '' |
| 260 |
if "linux" in sys.platform or "darwin" in sys.platform: |
| 261 |
EXTRA_ENV_LINK_ARGS += ' -lpthread' |
| 262 |
if check_linker_need_libatomic(): |
| 263 |
EXTRA_ENV_LINK_ARGS += ' -latomic' |
| 264 |
elif "win32" in sys.platform and sys.version_info < (3, 5): |
| 265 |
msvcr = cygwinccompiler.get_msvcr()[0] |
| 266 |
EXTRA_ENV_LINK_ARGS += ( |
| 267 |
' -static-libgcc -static-libstdc++ -mcrtdll={msvcr}' |
| 268 |
' -static -lshlwapi'.format(msvcr=msvcr)) |
| 269 |
if "linux" in sys.platform: |
| 270 |
EXTRA_ENV_LINK_ARGS += ' -static-libgcc' |
| 271 |
EXTRA_ENV_LINK_ARGS += ' -labsl_str_format_internal -labsl_time -labsl_hash -labsl_raw_hash_set -labsl_status -labsl_statusor -labsl_random_internal_randen_slow -labsl_random_internal_randen_hwaes_impl -labsl_random_internal_randen -labsl_random_internal_entropy_pool' |
| 272 |
|
| 273 |
EXTRA_COMPILE_ARGS = shlex.split(EXTRA_ENV_COMPILE_ARGS) |
| 274 |
EXTRA_LINK_ARGS = shlex.split(EXTRA_ENV_LINK_ARGS) |
| 275 |
|
| 276 |
if BUILD_WITH_STATIC_LIBSTDCXX: |
| 277 |
EXTRA_LINK_ARGS.append('-static-libstdc++') |
| 278 |
|
| 279 |
CYTHON_EXTENSION_PACKAGE_NAMES = () |
| 280 |
|
| 281 |
CYTHON_EXTENSION_MODULE_NAMES = ('grpc._cython.cygrpc',) |
| 282 |
|
| 283 |
CYTHON_HELPER_C_FILES = () |
| 284 |
|
| 285 |
CORE_C_FILES = tuple(grpc_core_dependencies.CORE_SOURCE_FILES) |
| 286 |
if "win32" in sys.platform: |
| 287 |
CORE_C_FILES = filter(lambda x: 'third_party/cares' not in x, CORE_C_FILES) |
| 288 |
|
| 289 |
if BUILD_WITH_SYSTEM_OPENSSL: |
| 290 |
CORE_C_FILES = filter(lambda x: 'third_party/boringssl' not in x, |
| 291 |
CORE_C_FILES) |
| 292 |
CORE_C_FILES = filter(lambda x: 'src/boringssl' not in x, CORE_C_FILES) |
| 293 |
SSL_INCLUDE = (os.path.join('/usr', 'include', 'openssl'),) |
| 294 |
|
| 295 |
if BUILD_WITH_SYSTEM_ZLIB: |
| 296 |
CORE_C_FILES = filter(lambda x: 'third_party/zlib' not in x, CORE_C_FILES) |
| 297 |
ZLIB_INCLUDE = (os.path.join('/usr', 'include'),) |
| 298 |
|
| 299 |
if BUILD_WITH_SYSTEM_CARES: |
| 300 |
CORE_C_FILES = filter(lambda x: 'third_party/cares' not in x, CORE_C_FILES) |
| 301 |
CARES_INCLUDE = (os.path.join('/usr', 'include'),) |
| 302 |
|
| 303 |
if BUILD_WITH_SYSTEM_RE2: |
| 304 |
CORE_C_FILES = filter(lambda x: 'third_party/re2' not in x, CORE_C_FILES) |
| 305 |
RE2_INCLUDE = (os.path.join('/usr', 'include', 're2'),) |
| 306 |
|
| 307 |
if BUILD_WITH_SYSTEM_ABSL: |
| 308 |
CORE_C_FILES = filter(lambda x: 'third_party/abseil-cpp' not in x, |
| 309 |
CORE_C_FILES) |
| 310 |
ABSL_INCLUDE = (os.path.join('/usr', 'include'),) |
| 311 |
|
| 312 |
EXTENSION_INCLUDE_DIRECTORIES = ((PYTHON_STEM,) + CORE_INCLUDE + ABSL_INCLUDE + |
| 313 |
ADDRESS_SORTING_INCLUDE + CARES_INCLUDE + |
| 314 |
RE2_INCLUDE + SSL_INCLUDE + UPB_INCLUDE + |
| 315 |
UPB_GRPC_GENERATED_INCLUDE + |
| 316 |
UPBDEFS_GRPC_GENERATED_INCLUDE + |
| 317 |
XXHASH_INCLUDE + ZLIB_INCLUDE) |
| 318 |
|
| 319 |
EXTENSION_LIBRARIES = () |
| 320 |
if "linux" in sys.platform: |
| 321 |
EXTENSION_LIBRARIES += ('rt',) |
| 322 |
if not "win32" in sys.platform: |
| 323 |
EXTENSION_LIBRARIES += ('m',) |
| 324 |
if "win32" in sys.platform: |
| 325 |
EXTENSION_LIBRARIES += ( |
| 326 |
'advapi32', |
| 327 |
'bcrypt', |
| 328 |
'dbghelp', |
| 329 |
'ws2_32', |
| 330 |
) |
| 331 |
if BUILD_WITH_SYSTEM_OPENSSL: |
| 332 |
EXTENSION_LIBRARIES += ( |
| 333 |
'ssl', |
| 334 |
'crypto', |
| 335 |
) |
| 336 |
if BUILD_WITH_SYSTEM_ZLIB: |
| 337 |
EXTENSION_LIBRARIES += ('z',) |
| 338 |
if BUILD_WITH_SYSTEM_CARES: |
| 339 |
EXTENSION_LIBRARIES += ('cares',) |
| 340 |
if BUILD_WITH_SYSTEM_RE2: |
| 341 |
EXTENSION_LIBRARIES += ('re2',) |
| 342 |
if BUILD_WITH_SYSTEM_ABSL: |
| 343 |
EXTENSION_LIBRARIES += tuple( |
| 344 |
lib.stem[3:] for lib in pathlib.Path('/usr').glob('lib*/libabsl_*.so')) |
| 345 |
|
| 346 |
DEFINE_MACROS = (('_WIN32_WINNT', 0x600),) |
| 347 |
asm_files = [] |
| 348 |
|
| 349 |
|
| 350 |
# Quotes on Windows build macros are evaluated differently from other platforms, |
| 351 |
# so we must apply quotes asymmetrically in order to yield the proper result in |
| 352 |
# the binary. |
| 353 |
def _quote_build_define(argument): |
| 354 |
if "win32" in sys.platform: |
| 355 |
return '"\\\"{}\\\""'.format(argument) |
| 356 |
return '"{}"'.format(argument) |
| 357 |
|
| 358 |
|
| 359 |
DEFINE_MACROS += ( |
| 360 |
("GRPC_XDS_USER_AGENT_NAME_SUFFIX", _quote_build_define("Python")), |
| 361 |
("GRPC_XDS_USER_AGENT_VERSION_SUFFIX", |
| 362 |
_quote_build_define(_metadata.__version__)), |
| 363 |
) |
| 364 |
|
| 365 |
asm_key = '' |
| 366 |
if BUILD_WITH_BORING_SSL_ASM and not BUILD_WITH_SYSTEM_OPENSSL: |
| 367 |
boringssl_asm_platform = BUILD_OVERRIDE_BORING_SSL_ASM_PLATFORM if BUILD_OVERRIDE_BORING_SSL_ASM_PLATFORM else util.get_platform( |
| 368 |
) |
| 369 |
LINUX_X86_64 = 'linux-x86_64' |
| 370 |
LINUX_ARM = 'linux-arm' |
| 371 |
LINUX_AARCH64 = 'linux-aarch64' |
| 372 |
if LINUX_X86_64 == boringssl_asm_platform: |
| 373 |
asm_key = 'crypto_linux_x86_64' |
| 374 |
elif LINUX_ARM == boringssl_asm_platform: |
| 375 |
asm_key = 'crypto_linux_arm' |
| 376 |
elif LINUX_AARCH64 == boringssl_asm_platform: |
| 377 |
asm_key = 'crypto_linux_aarch64' |
| 378 |
elif "mac" in boringssl_asm_platform and "x86_64" in boringssl_asm_platform: |
| 379 |
asm_key = 'crypto_mac_x86_64' |
| 380 |
else: |
| 381 |
print("ASM Builds for BoringSSL currently not supported on:", |
| 382 |
boringssl_asm_platform) |
| 383 |
if asm_key: |
| 384 |
asm_files = grpc_core_dependencies.ASM_SOURCE_FILES[asm_key] |
| 385 |
else: |
| 386 |
DEFINE_MACROS += (('OPENSSL_NO_ASM', 1),) |
| 387 |
|
| 388 |
if not DISABLE_LIBC_COMPATIBILITY: |
| 389 |
DEFINE_MACROS += (('GPR_BACKWARDS_COMPATIBILITY_MODE', 1),) |
| 390 |
|
| 391 |
if "win32" in sys.platform: |
| 392 |
# TODO(zyc): Re-enable c-ares on x64 and x86 windows after fixing the |
| 393 |
# ares_library_init compilation issue |
| 394 |
DEFINE_MACROS += ( |
| 395 |
('WIN32_LEAN_AND_MEAN', 1), |
| 396 |
('CARES_STATICLIB', 1), |
| 397 |
('GRPC_ARES', 0), |
| 398 |
('NTDDI_VERSION', 0x06000000), |
| 399 |
('NOMINMAX', 1), |
| 400 |
) |
| 401 |
if '64bit' in platform.architecture()[0]: |
| 402 |
DEFINE_MACROS += (('MS_WIN64', 1),) |
| 403 |
elif sys.version_info >= (3, 5): |
| 404 |
# For some reason, this is needed to get access to inet_pton/inet_ntop |
| 405 |
# on msvc, but only for 32 bits |
| 406 |
DEFINE_MACROS += (('NTDDI_VERSION', 0x06000000),) |
| 407 |
else: |
| 408 |
DEFINE_MACROS += ( |
| 409 |
('HAVE_CONFIG_H', 1), |
| 410 |
('GRPC_ENABLE_FORK_SUPPORT', 1), |
| 411 |
) |
| 412 |
|
| 413 |
LDFLAGS = tuple(EXTRA_LINK_ARGS) |
| 414 |
CFLAGS = tuple(EXTRA_COMPILE_ARGS) |
| 415 |
if "linux" in sys.platform or "darwin" in sys.platform: |
| 416 |
pymodinit_type = 'PyObject*' if PY3 else 'void' |
| 417 |
pymodinit = 'extern "C" __attribute__((visibility ("default"))) {}'.format( |
| 418 |
pymodinit_type) |
| 419 |
DEFINE_MACROS += (('PyMODINIT_FUNC', pymodinit),) |
| 420 |
DEFINE_MACROS += (('GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK', 1),) |
| 421 |
|
| 422 |
# By default, Python3 distutils enforces compatibility of |
| 423 |
# c plugins (.so files) with the OSX version Python was built with. |
| 424 |
# We need OSX 10.10, the oldest which supports C++ thread_local. |
| 425 |
# Python 3.9: Mac OS Big Sur sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') returns int (11) |
| 426 |
if 'darwin' in sys.platform: |
| 427 |
mac_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') |
| 428 |
if mac_target: |
| 429 |
mac_target = pkg_resources.parse_version(str(mac_target)) |
| 430 |
if mac_target < pkg_resources.parse_version('10.10.0'): |
| 431 |
os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.10' |
| 432 |
os.environ['_PYTHON_HOST_PLATFORM'] = re.sub( |
| 433 |
r'macosx-[0-9]+\.[0-9]+-(.+)', r'macosx-10.10-\1', |
| 434 |
util.get_platform()) |
| 435 |
|
| 436 |
|
| 437 |
def cython_extensions_and_necessity(): |
| 438 |
cython_module_files = [ |
| 439 |
os.path.join(PYTHON_STEM, |
| 440 |
name.replace('.', '/') + '.pyx') |
| 441 |
for name in CYTHON_EXTENSION_MODULE_NAMES |
| 442 |
] |
| 443 |
config = os.environ.get('CONFIG', 'opt') |
| 444 |
prefix = 'libs/' + config + '/' |
| 445 |
if USE_PREBUILT_GRPC_CORE: |
| 446 |
extra_objects = [ |
| 447 |
prefix + 'libares.a', prefix + 'libboringssl.a', |
| 448 |
prefix + 'libgpr.a', prefix + 'libgrpc.a' |
| 449 |
] |
| 450 |
core_c_files = [] |
| 451 |
else: |
| 452 |
core_c_files = list(CORE_C_FILES) |
| 453 |
extra_objects = [] |
| 454 |
extensions = [ |
| 455 |
_extension.Extension( |
| 456 |
name=module_name, |
| 457 |
sources=([module_file] + list(CYTHON_HELPER_C_FILES) + |
| 458 |
core_c_files + asm_files), |
| 459 |
include_dirs=list(EXTENSION_INCLUDE_DIRECTORIES), |
| 460 |
libraries=list(EXTENSION_LIBRARIES), |
| 461 |
define_macros=list(DEFINE_MACROS), |
| 462 |
extra_objects=extra_objects, |
| 463 |
extra_compile_args=list(CFLAGS), |
| 464 |
extra_link_args=list(LDFLAGS), |
| 465 |
) for (module_name, module_file |
| 466 |
) in zip(list(CYTHON_EXTENSION_MODULE_NAMES), cython_module_files) |
| 467 |
] |
| 468 |
need_cython = BUILD_WITH_CYTHON |
| 469 |
if not BUILD_WITH_CYTHON: |
| 470 |
need_cython = need_cython or not commands.check_and_update_cythonization( |
| 471 |
extensions) |
| 472 |
# TODO: the strategy for conditional compiling and exposing the aio Cython |
| 473 |
# dependencies will be revisited by https://github.com/grpc/grpc/issues/19728 |
| 474 |
return commands.try_cythonize(extensions, |
| 475 |
linetracing=ENABLE_CYTHON_TRACING, |
| 476 |
mandatory=BUILD_WITH_CYTHON), need_cython |
| 477 |
|
| 478 |
|
| 479 |
CYTHON_EXTENSION_MODULES, need_cython = cython_extensions_and_necessity() |
| 480 |
|
| 481 |
PACKAGE_DIRECTORIES = { |
| 482 |
'': PYTHON_STEM, |
| 483 |
} |
| 484 |
|
| 485 |
INSTALL_REQUIRES = () |
| 486 |
|
| 487 |
EXTRAS_REQUIRES = { |
| 488 |
'protobuf': 'grpcio-tools>={version}'.format(version=grpc_version.VERSION), |
| 489 |
} |
| 490 |
|
| 491 |
SETUP_REQUIRES = INSTALL_REQUIRES + ( |
| 492 |
'Sphinx',) if ENABLE_DOCUMENTATION_BUILD else () |
| 493 |
|
| 494 |
try: |
| 495 |
import Cython |
| 496 |
except ImportError: |
| 497 |
if BUILD_WITH_CYTHON: |
| 498 |
sys.stderr.write( |
| 499 |
"You requested a Cython build via GRPC_PYTHON_BUILD_WITH_CYTHON, " |
| 500 |
"but do not have Cython installed. We won't stop you from using " |
| 501 |
"other commands, but the extension files will fail to build.\n") |
| 502 |
elif need_cython: |
| 503 |
sys.stderr.write( |
| 504 |
'We could not find Cython. Setup may take 10-20 minutes.\n') |
| 505 |
SETUP_REQUIRES += ('cython>=3.0.0',) |
| 506 |
|
| 507 |
COMMAND_CLASS = { |
| 508 |
'doc': commands.SphinxDocumentation, |
| 509 |
'build_project_metadata': commands.BuildProjectMetadata, |
| 510 |
'build_py': commands.BuildPy, |
| 511 |
'build_ext': commands.BuildExt, |
| 512 |
'gather': commands.Gather, |
| 513 |
'clean': commands.Clean, |
| 514 |
} |
| 515 |
|
| 516 |
# Ensure that package data is copied over before any commands have been run: |
| 517 |
credentials_dir = os.path.join(PYTHON_STEM, 'grpc', '_cython', '_credentials') |
| 518 |
try: |
| 519 |
os.mkdir(credentials_dir) |
| 520 |
except OSError: |
| 521 |
pass |
| 522 |
shutil.copyfile(os.path.join('etc', 'roots.pem'), |
| 523 |
os.path.join(credentials_dir, 'roots.pem')) |
| 524 |
|
| 525 |
PACKAGE_DATA = { |
| 526 |
# Binaries that may or may not be present in the final installation, but are |
| 527 |
# mentioned here for completeness. |
| 528 |
'grpc._cython': [ |
| 529 |
'_credentials/roots.pem', |
| 530 |
'_windows/grpc_c.32.python', |
| 531 |
'_windows/grpc_c.64.python', |
| 532 |
], |
| 533 |
} |
| 534 |
PACKAGES = setuptools.find_packages(PYTHON_STEM) |
| 535 |
|
| 536 |
setuptools.setup( |
| 537 |
name='grpcio', |
| 538 |
version=grpc_version.VERSION, |
| 539 |
description='HTTP/2-based RPC framework', |
| 540 |
author='The gRPC Authors', |
| 541 |
author_email='[email protected]', |
| 542 |
url='https://grpc.io', |
| 543 |
license=LICENSE, |
| 544 |
classifiers=CLASSIFIERS, |
| 545 |
long_description=open(README).read(), |
| 546 |
ext_modules=CYTHON_EXTENSION_MODULES, |
| 547 |
packages=list(PACKAGES), |
| 548 |
package_dir=PACKAGE_DIRECTORIES, |
| 549 |
package_data=PACKAGE_DATA, |
| 550 |
python_requires='>=3.7', |
| 551 |
install_requires=INSTALL_REQUIRES, |
| 552 |
extras_require=EXTRAS_REQUIRES, |
| 553 |
setup_requires=SETUP_REQUIRES, |
| 554 |
cmdclass=COMMAND_CLASS, |
| 555 |
) |