blob: 184d5457f1a477b4dba14af5510d9c038f140046 [file] [log] [blame]
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +02001# 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.
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +020014"""A setup module for the GRPC Python package."""
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +010015
16# setuptools need to be imported before distutils. Otherwise it might lead to
17# undesirable behaviors or errors.
18import setuptools
19
20# Monkey Patch the unix compiler to accept ASM
21# files used by boring SSL.
22from distutils.unixccompiler import UnixCCompiler
23UnixCCompiler.src_extensions.append('.S')
24del UnixCCompiler
25
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +020026from distutils import cygwinccompiler
27from distutils import extension as _extension
28from distutils import util
29import os
30import os.path
31import pkg_resources
32import platform
33import re
34import shlex
35import shutil
36import sys
37import sysconfig
38
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +020039from setuptools.command import egg_info
40
41import subprocess
42from subprocess import PIPE
43
44# Redirect the manifest template from MANIFEST.in to PYTHON-MANIFEST.in.
45egg_info.manifest_maker.template = 'PYTHON-MANIFEST.in'
46
47PY3 = sys.version_info.major == 3
48PYTHON_STEM = os.path.join('src', 'python', 'grpcio')
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +010049CORE_INCLUDE = (
50 'include',
51 '.',
52)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +020053ABSL_INCLUDE = (os.path.join('third_party', 'abseil-cpp'),)
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +010054ADDRESS_SORTING_INCLUDE = (os.path.join('third_party', 'address_sorting',
55 'include'),)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +020056CARES_INCLUDE = (
57 os.path.join('third_party', 'cares'),
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +010058 os.path.join('third_party', 'cares', 'cares'),
59)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +020060if 'darwin' in sys.platform:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +010061 CARES_INCLUDE += (os.path.join('third_party', 'cares', 'config_darwin'),)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +020062if 'freebsd' in sys.platform:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +010063 CARES_INCLUDE += (os.path.join('third_party', 'cares', 'config_freebsd'),)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +020064if 'linux' in sys.platform:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +010065 CARES_INCLUDE += (os.path.join('third_party', 'cares', 'config_linux'),)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +020066if 'openbsd' in sys.platform:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +010067 CARES_INCLUDE += (os.path.join('third_party', 'cares', 'config_openbsd'),)
68RE2_INCLUDE = (os.path.join('third_party', 're2'),)
69SSL_INCLUDE = (os.path.join('third_party', 'boringssl-with-bazel', 'src',
70 'include'),)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +020071UPB_INCLUDE = (os.path.join('third_party', 'upb'),)
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +010072UPB_GRPC_GENERATED_INCLUDE = (os.path.join('src', 'core', 'ext',
73 'upb-generated'),)
74UPBDEFS_GRPC_GENERATED_INCLUDE = (os.path.join('src', 'core', 'ext',
75 'upbdefs-generated'),)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +020076ZLIB_INCLUDE = (os.path.join('third_party', 'zlib'),)
77README = os.path.join(PYTHON_STEM, 'README.rst')
78
79# Ensure we're in the proper directory whether or not we're being used by pip.
80os.chdir(os.path.dirname(os.path.abspath(__file__)))
81sys.path.insert(0, os.path.abspath(PYTHON_STEM))
82
83# Break import-style to ensure we can actually find our in-repo dependencies.
84import _parallel_compile_patch
85import _spawn_patch
86import commands
87import grpc_core_dependencies
88import grpc_version
89
90_parallel_compile_patch.monkeypatch_compile_maybe()
91_spawn_patch.monkeypatch_spawn()
92
93LICENSE = 'Apache License 2.0'
94
95CLASSIFIERS = [
96 'Development Status :: 5 - Production/Stable',
97 'Programming Language :: Python',
98 'Programming Language :: Python :: 2',
99 'Programming Language :: Python :: 2.7',
100 'Programming Language :: Python :: 3',
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200101 'Programming Language :: Python :: 3.5',
102 'Programming Language :: Python :: 3.6',
103 'Programming Language :: Python :: 3.7',
104 'Programming Language :: Python :: 3.8',
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100105 'Programming Language :: Python :: 3.9',
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200106 'License :: OSI Approved :: Apache Software License',
107]
108
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100109BUILD_WITH_BORING_SSL_ASM = os.environ.get('GRPC_BUILD_WITH_BORING_SSL_ASM',
110 True)
111
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200112# Environment variable to determine whether or not the Cython extension should
113# *use* Cython or use the generated C files. Note that this requires the C files
114# to have been generated by building first *with* Cython support. Even if this
115# is set to false, if the script detects that the generated `.c` file isn't
116# present, then it will still attempt to use Cython.
117BUILD_WITH_CYTHON = os.environ.get('GRPC_PYTHON_BUILD_WITH_CYTHON', False)
118
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200119# Export this variable to use the system installation of openssl. You need to
120# have the header files installed (in /usr/include/openssl) and during
121# runtime, the shared library must be installed
122BUILD_WITH_SYSTEM_OPENSSL = os.environ.get('GRPC_PYTHON_BUILD_SYSTEM_OPENSSL',
123 False)
124
125# Export this variable to use the system installation of zlib. You need to
126# have the header files installed (in /usr/include/) and during
127# runtime, the shared library must be installed
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100128BUILD_WITH_SYSTEM_ZLIB = os.environ.get('GRPC_PYTHON_BUILD_SYSTEM_ZLIB', False)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200129
130# Export this variable to use the system installation of cares. You need to
131# have the header files installed (in /usr/include/) and during
132# runtime, the shared library must be installed
133BUILD_WITH_SYSTEM_CARES = os.environ.get('GRPC_PYTHON_BUILD_SYSTEM_CARES',
134 False)
135
136# For local development use only: This skips building gRPC Core and its
137# dependencies, including protobuf and boringssl. This allows "incremental"
138# compilation by first building gRPC Core using make, then building only the
139# Python/Cython layers here.
140#
141# Note that this requires libboringssl.a in the libs/{dbg,opt}/ directory, which
142# may require configuring make to not use the system openssl implementation:
143#
144# make HAS_SYSTEM_OPENSSL_ALPN=0
145#
146# TODO(ericgribkoff) Respect the BUILD_WITH_SYSTEM_* flags alongside this option
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100147USE_PREBUILT_GRPC_CORE = os.environ.get('GRPC_PYTHON_USE_PREBUILT_GRPC_CORE',
148 False)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200149
150# If this environmental variable is set, GRPC will not try to be compatible with
151# libc versions old than the one it was compiled against.
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100152DISABLE_LIBC_COMPATIBILITY = os.environ.get(
153 'GRPC_PYTHON_DISABLE_LIBC_COMPATIBILITY', False)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200154
155# Environment variable to determine whether or not to enable coverage analysis
156# in Cython modules.
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100157ENABLE_CYTHON_TRACING = os.environ.get('GRPC_PYTHON_ENABLE_CYTHON_TRACING',
158 False)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200159
160# Environment variable specifying whether or not there's interest in setting up
161# documentation building.
162ENABLE_DOCUMENTATION_BUILD = os.environ.get(
163 'GRPC_PYTHON_ENABLE_DOCUMENTATION_BUILD', False)
164
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100165
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200166def check_linker_need_libatomic():
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100167 """Test if linker on system needs libatomic."""
168 code_test = (b'#include <atomic>\n' +
169 b'int main() { return std::atomic<int64_t>{}; }')
170 cxx = os.environ.get('CXX', 'c++')
171 cpp_test = subprocess.Popen([cxx, '-x', 'c++', '-std=c++11', '-'],
172 stdin=PIPE,
173 stdout=PIPE,
174 stderr=PIPE)
175 cpp_test.communicate(input=code_test)
176 if cpp_test.returncode == 0:
177 return False
178 # Double-check to see if -latomic actually can solve the problem.
179 # https://github.com/grpc/grpc/issues/22491
180 cpp_test = subprocess.Popen(
181 [cxx, '-x', 'c++', '-std=c++11', '-latomic', '-'],
182 stdin=PIPE,
183 stdout=PIPE,
184 stderr=PIPE)
185 cpp_test.communicate(input=code_test)
186 return cpp_test.returncode == 0
187
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200188
189# There are some situations (like on Windows) where CC, CFLAGS, and LDFLAGS are
190# entirely ignored/dropped/forgotten by distutils and its Cygwin/MinGW support.
191# We use these environment variables to thus get around that without locking
192# ourselves in w.r.t. the multitude of operating systems this ought to build on.
193# We can also use these variables as a way to inject environment-specific
194# compiler/linker flags. We assume GCC-like compilers and/or MinGW as a
195# reasonable default.
196EXTRA_ENV_COMPILE_ARGS = os.environ.get('GRPC_PYTHON_CFLAGS', None)
197EXTRA_ENV_LINK_ARGS = os.environ.get('GRPC_PYTHON_LDFLAGS', None)
198if EXTRA_ENV_COMPILE_ARGS is None:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100199 EXTRA_ENV_COMPILE_ARGS = ' -std=c++11'
200 if 'win32' in sys.platform:
201 if sys.version_info < (3, 5):
202 EXTRA_ENV_COMPILE_ARGS += ' -D_hypot=hypot'
203 # We use define flags here and don't directly add to DEFINE_MACROS below to
204 # ensure that the expert user/builder has a way of turning it off (via the
205 # envvars) without adding yet more GRPC-specific envvars.
206 # See https://sourceforge.net/p/mingw-w64/bugs/363/
207 if '32' in platform.architecture()[0]:
208 EXTRA_ENV_COMPILE_ARGS += ' -D_ftime=_ftime32 -D_timeb=__timeb32 -D_ftime_s=_ftime32_s'
209 else:
210 EXTRA_ENV_COMPILE_ARGS += ' -D_ftime=_ftime64 -D_timeb=__timeb64'
211 else:
212 # We need to statically link the C++ Runtime, only the C runtime is
213 # available dynamically
214 EXTRA_ENV_COMPILE_ARGS += ' /MT'
215 elif "linux" in sys.platform:
216 EXTRA_ENV_COMPILE_ARGS += ' -std=gnu99 -fvisibility=hidden -fno-wrapv -fno-exceptions'
217 elif "darwin" in sys.platform:
218 EXTRA_ENV_COMPILE_ARGS += ' -stdlib=libc++ -fvisibility=hidden -fno-wrapv -fno-exceptions'
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200219
220if EXTRA_ENV_LINK_ARGS is None:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100221 EXTRA_ENV_LINK_ARGS = ''
222 if "linux" in sys.platform or "darwin" in sys.platform:
223 EXTRA_ENV_LINK_ARGS += ' -lpthread'
224 if check_linker_need_libatomic():
225 EXTRA_ENV_LINK_ARGS += ' -latomic'
226 elif "win32" in sys.platform and sys.version_info < (3, 5):
227 msvcr = cygwinccompiler.get_msvcr()[0]
228 EXTRA_ENV_LINK_ARGS += (
229 ' -static-libgcc -static-libstdc++ -mcrtdll={msvcr}'
230 ' -static -lshlwapi'.format(msvcr=msvcr))
231 if "linux" in sys.platform:
232 EXTRA_ENV_LINK_ARGS += ' -Wl,-wrap,memcpy -static-libgcc'
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200233
234EXTRA_COMPILE_ARGS = shlex.split(EXTRA_ENV_COMPILE_ARGS)
235EXTRA_LINK_ARGS = shlex.split(EXTRA_ENV_LINK_ARGS)
236
237CYTHON_EXTENSION_PACKAGE_NAMES = ()
238
239CYTHON_EXTENSION_MODULE_NAMES = ('grpc._cython.cygrpc',)
240
241CYTHON_HELPER_C_FILES = ()
242
243CORE_C_FILES = tuple(grpc_core_dependencies.CORE_SOURCE_FILES)
244if "win32" in sys.platform:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100245 CORE_C_FILES = filter(lambda x: 'third_party/cares' not in x, CORE_C_FILES)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200246
247if BUILD_WITH_SYSTEM_OPENSSL:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100248 CORE_C_FILES = filter(lambda x: 'third_party/boringssl' not in x,
249 CORE_C_FILES)
250 CORE_C_FILES = filter(lambda x: 'src/boringssl' not in x, CORE_C_FILES)
251 SSL_INCLUDE = (os.path.join('/usr', 'include', 'openssl'),)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200252
253if BUILD_WITH_SYSTEM_ZLIB:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100254 CORE_C_FILES = filter(lambda x: 'third_party/zlib' not in x, CORE_C_FILES)
255 ZLIB_INCLUDE = (os.path.join('/usr', 'include'),)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200256
257if BUILD_WITH_SYSTEM_CARES:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100258 CORE_C_FILES = filter(lambda x: 'third_party/cares' not in x, CORE_C_FILES)
259 CARES_INCLUDE = (os.path.join('/usr', 'include'),)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200260
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100261EXTENSION_INCLUDE_DIRECTORIES = ((PYTHON_STEM,) + CORE_INCLUDE + ABSL_INCLUDE +
262 ADDRESS_SORTING_INCLUDE + CARES_INCLUDE +
263 RE2_INCLUDE + SSL_INCLUDE + UPB_INCLUDE +
264 UPB_GRPC_GENERATED_INCLUDE +
265 UPBDEFS_GRPC_GENERATED_INCLUDE + ZLIB_INCLUDE)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200266
267EXTENSION_LIBRARIES = ()
268if "linux" in sys.platform:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100269 EXTENSION_LIBRARIES += ('rt',)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200270if not "win32" in sys.platform:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100271 EXTENSION_LIBRARIES += ('m',)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200272if "win32" in sys.platform:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100273 EXTENSION_LIBRARIES += (
274 'advapi32',
275 'ws2_32',
276 'dbghelp',
277 )
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200278if BUILD_WITH_SYSTEM_OPENSSL:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100279 EXTENSION_LIBRARIES += (
280 'ssl',
281 'crypto',
282 )
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200283if BUILD_WITH_SYSTEM_ZLIB:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100284 EXTENSION_LIBRARIES += ('z',)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200285if BUILD_WITH_SYSTEM_CARES:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100286 EXTENSION_LIBRARIES += ('cares',)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200287
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100288DEFINE_MACROS = (('_WIN32_WINNT', 0x600),)
289asm_files = []
290
291asm_key = ''
292if BUILD_WITH_BORING_SSL_ASM:
293 LINUX_X86_64 = 'linux-x86_64'
294 LINUX_ARM = 'linux-arm'
295 if LINUX_X86_64 == util.get_platform():
296 asm_key = 'crypto_linux_x86_64'
297 elif LINUX_ARM == util.get_platform():
298 asm_key = 'crypto_linux_arm'
299 elif "mac" in util.get_platform() and "x86_64" in util.get_platform():
300 asm_key = 'crypto_mac_x86_64'
301 else:
302 print("ASM Builds for BoringSSL currently not supported on:",
303 util.get_platform())
304if asm_key:
305 asm_files = grpc_core_dependencies.ASM_SOURCE_FILES[asm_key]
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200306else:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100307 DEFINE_MACROS += (('OPENSSL_NO_ASM', 1),)
308
309if not DISABLE_LIBC_COMPATIBILITY:
310 DEFINE_MACROS += (('GPR_BACKWARDS_COMPATIBILITY_MODE', 1),)
311
312if "win32" in sys.platform:
313 # TODO(zyc): Re-enable c-ares on x64 and x86 windows after fixing the
314 # ares_library_init compilation issue
315 DEFINE_MACROS += (
316 ('WIN32_LEAN_AND_MEAN', 1),
317 ('CARES_STATICLIB', 1),
318 ('GRPC_ARES', 0),
319 ('NTDDI_VERSION', 0x06000000),
320 ('NOMINMAX', 1),
321 )
322 if '64bit' in platform.architecture()[0]:
323 DEFINE_MACROS += (('MS_WIN64', 1),)
324 elif sys.version_info >= (3, 5):
325 # For some reason, this is needed to get access to inet_pton/inet_ntop
326 # on msvc, but only for 32 bits
327 DEFINE_MACROS += (('NTDDI_VERSION', 0x06000000),)
328else:
329 DEFINE_MACROS += (
330 ('HAVE_CONFIG_H', 1),
331 ('GRPC_ENABLE_FORK_SUPPORT', 1),
332 )
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200333
334LDFLAGS = tuple(EXTRA_LINK_ARGS)
335CFLAGS = tuple(EXTRA_COMPILE_ARGS)
336if "linux" in sys.platform or "darwin" in sys.platform:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100337 pymodinit_type = 'PyObject*' if PY3 else 'void'
338 pymodinit = 'extern "C" __attribute__((visibility ("default"))) {}'.format(
339 pymodinit_type)
340 DEFINE_MACROS += (('PyMODINIT_FUNC', pymodinit),)
341 DEFINE_MACROS += (('GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK', 1),)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200342
343# By default, Python3 distutils enforces compatibility of
344# c plugins (.so files) with the OSX version Python3 was built with.
345# For Python3.4, this is OSX 10.6, but we need Thread Local Support (__thread)
346if 'darwin' in sys.platform and PY3:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100347 mac_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
348 if mac_target and (pkg_resources.parse_version(mac_target) <
349 pkg_resources.parse_version('10.7.0')):
350 os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.7'
351 os.environ['_PYTHON_HOST_PLATFORM'] = re.sub(
352 r'macosx-[0-9]+\.[0-9]+-(.+)', r'macosx-10.7-\1',
353 util.get_platform())
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200354
355
356def cython_extensions_and_necessity():
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100357 cython_module_files = [
358 os.path.join(PYTHON_STEM,
359 name.replace('.', '/') + '.pyx')
360 for name in CYTHON_EXTENSION_MODULE_NAMES
361 ]
362 config = os.environ.get('CONFIG', 'opt')
363 prefix = 'libs/' + config + '/'
364 if USE_PREBUILT_GRPC_CORE:
365 extra_objects = [
366 prefix + 'libares.a', prefix + 'libboringssl.a',
367 prefix + 'libgpr.a', prefix + 'libgrpc.a'
368 ]
369 core_c_files = []
370 else:
371 core_c_files = list(CORE_C_FILES)
372 extra_objects = []
373 extensions = [
374 _extension.Extension(
375 name=module_name,
376 sources=([module_file] + list(CYTHON_HELPER_C_FILES) +
377 core_c_files + asm_files),
378 include_dirs=list(EXTENSION_INCLUDE_DIRECTORIES),
379 libraries=list(EXTENSION_LIBRARIES),
380 define_macros=list(DEFINE_MACROS),
381 extra_objects=extra_objects,
382 extra_compile_args=list(CFLAGS),
383 extra_link_args=list(LDFLAGS),
384 ) for (module_name, module_file
385 ) in zip(list(CYTHON_EXTENSION_MODULE_NAMES), cython_module_files)
386 ]
387 need_cython = BUILD_WITH_CYTHON
388 if not BUILD_WITH_CYTHON:
389 need_cython = need_cython or not commands.check_and_update_cythonization(
390 extensions)
391 # TODO: the strategy for conditional compiling and exposing the aio Cython
392 # dependencies will be revisited by https://github.com/grpc/grpc/issues/19728
393 return commands.try_cythonize(extensions,
394 linetracing=ENABLE_CYTHON_TRACING,
395 mandatory=BUILD_WITH_CYTHON), need_cython
396
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200397
398CYTHON_EXTENSION_MODULES, need_cython = cython_extensions_and_necessity()
399
400PACKAGE_DIRECTORIES = {
401 '': PYTHON_STEM,
402}
403
404INSTALL_REQUIRES = (
405 "six>=1.5.2",
406 "futures>=2.2.0; python_version<'3.2'",
407 "enum34>=1.0.4; python_version<'3.4'",
408)
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100409EXTRAS_REQUIRES = {
410 'protobuf': 'grpcio-tools>={version}'.format(version=grpc_version.VERSION),
411}
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200412
413SETUP_REQUIRES = INSTALL_REQUIRES + (
414 'Sphinx~=1.8.1',
415 'six>=1.10',
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100416) if ENABLE_DOCUMENTATION_BUILD else ()
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200417
418try:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100419 import Cython
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200420except ImportError:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100421 if BUILD_WITH_CYTHON:
422 sys.stderr.write(
423 "You requested a Cython build via GRPC_PYTHON_BUILD_WITH_CYTHON, "
424 "but do not have Cython installed. We won't stop you from using "
425 "other commands, but the extension files will fail to build.\n")
426 elif need_cython:
427 sys.stderr.write(
428 'We could not find Cython. Setup may take 10-20 minutes.\n')
429 SETUP_REQUIRES += ('cython>=0.23',)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200430
431COMMAND_CLASS = {
432 'doc': commands.SphinxDocumentation,
433 'build_project_metadata': commands.BuildProjectMetadata,
434 'build_py': commands.BuildPy,
435 'build_ext': commands.BuildExt,
436 'gather': commands.Gather,
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100437 'clean': commands.Clean,
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200438}
439
440# Ensure that package data is copied over before any commands have been run:
441credentials_dir = os.path.join(PYTHON_STEM, 'grpc', '_cython', '_credentials')
442try:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100443 os.mkdir(credentials_dir)
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200444except OSError:
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100445 pass
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200446shutil.copyfile(os.path.join('etc', 'roots.pem'),
447 os.path.join(credentials_dir, 'roots.pem'))
448
449PACKAGE_DATA = {
450 # Binaries that may or may not be present in the final installation, but are
451 # mentioned here for completeness.
452 'grpc._cython': [
453 '_credentials/roots.pem',
454 '_windows/grpc_c.32.python',
455 '_windows/grpc_c.64.python',
456 ],
457}
458PACKAGES = setuptools.find_packages(PYTHON_STEM)
459
460setuptools.setup(
Jeff Vander Stoep08902cf2020-11-19 19:03:52 +0100461 name='grpcio',
462 version=grpc_version.VERSION,
463 description='HTTP/2-based RPC framework',
464 author='The gRPC Authors',
465 author_email='grpc-io@googlegroups.com',
466 url='https://grpc.io',
467 license=LICENSE,
468 classifiers=CLASSIFIERS,
469 long_description=open(README).read(),
470 ext_modules=CYTHON_EXTENSION_MODULES,
471 packages=list(PACKAGES),
472 package_dir=PACKAGE_DIRECTORIES,
473 package_data=PACKAGE_DATA,
474 install_requires=INSTALL_REQUIRES,
475 extras_require=EXTRAS_REQUIRES,
476 setup_requires=SETUP_REQUIRES,
477 cmdclass=COMMAND_CLASS,
Jeff Vander Stoep3adfea82020-10-14 15:35:59 +0200478)