Imported Upstream version 1.36.0
[platform/upstream/grpc.git] / setup.py
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 # setuptools need to be imported before distutils. Otherwise it might lead to
17 # undesirable behaviors or errors.
18 import setuptools
19
20 # Monkey Patch the unix compiler to accept ASM
21 # files used by boring SSL.
22 from distutils.unixccompiler import UnixCCompiler
23 UnixCCompiler.src_extensions.append('.S')
24 del UnixCCompiler
25
26 from distutils import cygwinccompiler
27 from distutils import extension as _extension
28 from distutils import util
29 import os
30 import os.path
31 import pkg_resources
32 import platform
33 import re
34 import shlex
35 import shutil
36 import sys
37 import sysconfig
38
39 from setuptools.command import egg_info
40
41 import subprocess
42 from subprocess import PIPE
43
44 # Redirect the manifest template from MANIFEST.in to PYTHON-MANIFEST.in.
45 egg_info.manifest_maker.template = 'PYTHON-MANIFEST.in'
46
47 PY3 = sys.version_info.major == 3
48 PYTHON_STEM = os.path.join('src', 'python', 'grpcio')
49 CORE_INCLUDE = (
50     'include',
51     '.',
52 )
53 ABSL_INCLUDE = (os.path.join('third_party', 'abseil-cpp'),)
54 ADDRESS_SORTING_INCLUDE = (os.path.join('third_party', 'address_sorting',
55                                         'include'),)
56 CARES_INCLUDE = (
57     os.path.join('third_party', 'cares'),
58     os.path.join('third_party', 'cares', 'cares'),
59 )
60 if 'darwin' in sys.platform:
61     CARES_INCLUDE += (os.path.join('third_party', 'cares', 'config_darwin'),)
62 if 'freebsd' in sys.platform:
63     CARES_INCLUDE += (os.path.join('third_party', 'cares', 'config_freebsd'),)
64 if 'linux' in sys.platform:
65     CARES_INCLUDE += (os.path.join('third_party', 'cares', 'config_linux'),)
66 if 'openbsd' in sys.platform:
67     CARES_INCLUDE += (os.path.join('third_party', 'cares', 'config_openbsd'),)
68 RE2_INCLUDE = (os.path.join('third_party', 're2'),)
69 SSL_INCLUDE = (os.path.join('third_party', 'boringssl-with-bazel', 'src',
70                             'include'),)
71 UPB_INCLUDE = (os.path.join('third_party', 'upb'),)
72 UPB_GRPC_GENERATED_INCLUDE = (os.path.join('src', 'core', 'ext',
73                                            'upb-generated'),)
74 UPBDEFS_GRPC_GENERATED_INCLUDE = (os.path.join('src', 'core', 'ext',
75                                                'upbdefs-generated'),)
76 ZLIB_INCLUDE = (os.path.join('third_party', 'zlib'),)
77 README = 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.
80 os.chdir(os.path.dirname(os.path.abspath(__file__)))
81 sys.path.insert(0, os.path.abspath(PYTHON_STEM))
82
83 # Break import-style to ensure we can actually find our in-repo dependencies.
84 import _parallel_compile_patch
85 import _spawn_patch
86 import commands
87 import grpc_core_dependencies
88 import grpc_version
89
90 _parallel_compile_patch.monkeypatch_compile_maybe()
91 _spawn_patch.monkeypatch_spawn()
92
93 LICENSE = 'Apache License 2.0'
94
95 CLASSIFIERS = [
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',
101     'Programming Language :: Python :: 3.5',
102     'Programming Language :: Python :: 3.6',
103     'Programming Language :: Python :: 3.7',
104     'Programming Language :: Python :: 3.8',
105     'Programming Language :: Python :: 3.9',
106     'License :: OSI Approved :: Apache Software License',
107 ]
108
109 BUILD_WITH_BORING_SSL_ASM = os.environ.get('GRPC_BUILD_WITH_BORING_SSL_ASM',
110                                            True)
111
112 # 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.
117 BUILD_WITH_CYTHON = os.environ.get('GRPC_PYTHON_BUILD_WITH_CYTHON', False)
118
119 # 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
122 BUILD_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
128 BUILD_WITH_SYSTEM_ZLIB = os.environ.get('GRPC_PYTHON_BUILD_SYSTEM_ZLIB', False)
129
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
133 BUILD_WITH_SYSTEM_CARES = os.environ.get('GRPC_PYTHON_BUILD_SYSTEM_CARES',
134                                          False)
135
136 # Export this variable to use the system installation of re2. You need to
137 # have the header files installed (in /usr/include/re2) and during
138 # runtime, the shared library must be installed
139 BUILD_WITH_SYSTEM_RE2 = os.environ.get('GRPC_PYTHON_BUILD_SYSTEM_RE2', False)
140
141 # For local development use only: This skips building gRPC Core and its
142 # dependencies, including protobuf and boringssl. This allows "incremental"
143 # compilation by first building gRPC Core using make, then building only the
144 # Python/Cython layers here.
145 #
146 # Note that this requires libboringssl.a in the libs/{dbg,opt}/ directory, which
147 # may require configuring make to not use the system openssl implementation:
148 #
149 #    make HAS_SYSTEM_OPENSSL_ALPN=0
150 #
151 # TODO(ericgribkoff) Respect the BUILD_WITH_SYSTEM_* flags alongside this option
152 USE_PREBUILT_GRPC_CORE = os.environ.get('GRPC_PYTHON_USE_PREBUILT_GRPC_CORE',
153                                         False)
154
155 # If this environmental variable is set, GRPC will not try to be compatible with
156 # libc versions old than the one it was compiled against.
157 DISABLE_LIBC_COMPATIBILITY = os.environ.get(
158     'GRPC_PYTHON_DISABLE_LIBC_COMPATIBILITY', False)
159
160 # Environment variable to determine whether or not to enable coverage analysis
161 # in Cython modules.
162 ENABLE_CYTHON_TRACING = os.environ.get('GRPC_PYTHON_ENABLE_CYTHON_TRACING',
163                                        False)
164
165 # Environment variable specifying whether or not there's interest in setting up
166 # documentation building.
167 ENABLE_DOCUMENTATION_BUILD = os.environ.get(
168     'GRPC_PYTHON_ENABLE_DOCUMENTATION_BUILD', False)
169
170
171 def check_linker_need_libatomic():
172     """Test if linker on system needs libatomic."""
173     code_test = (b'#include <atomic>\n' +
174                  b'int main() { return std::atomic<int64_t>{}; }')
175     cxx = os.environ.get('CXX', 'c++')
176     cpp_test = subprocess.Popen([cxx, '-x', 'c++', '-std=c++11', '-'],
177                                 stdin=PIPE,
178                                 stdout=PIPE,
179                                 stderr=PIPE)
180     cpp_test.communicate(input=code_test)
181     if cpp_test.returncode == 0:
182         return False
183     # Double-check to see if -latomic actually can solve the problem.
184     # https://github.com/grpc/grpc/issues/22491
185     cpp_test = subprocess.Popen(
186         [cxx, '-x', 'c++', '-std=c++11', '-latomic', '-'],
187         stdin=PIPE,
188         stdout=PIPE,
189         stderr=PIPE)
190     cpp_test.communicate(input=code_test)
191     return cpp_test.returncode == 0
192
193
194 # There are some situations (like on Windows) where CC, CFLAGS, and LDFLAGS are
195 # entirely ignored/dropped/forgotten by distutils and its Cygwin/MinGW support.
196 # We use these environment variables to thus get around that without locking
197 # ourselves in w.r.t. the multitude of operating systems this ought to build on.
198 # We can also use these variables as a way to inject environment-specific
199 # compiler/linker flags. We assume GCC-like compilers and/or MinGW as a
200 # reasonable default.
201 EXTRA_ENV_COMPILE_ARGS = os.environ.get('GRPC_PYTHON_CFLAGS', None)
202 EXTRA_ENV_LINK_ARGS = os.environ.get('GRPC_PYTHON_LDFLAGS', None)
203 if EXTRA_ENV_COMPILE_ARGS is None:
204     EXTRA_ENV_COMPILE_ARGS = ' -std=c++11'
205     if 'win32' in sys.platform:
206         if sys.version_info < (3, 5):
207             EXTRA_ENV_COMPILE_ARGS += ' -D_hypot=hypot'
208             # We use define flags here and don't directly add to DEFINE_MACROS below to
209             # ensure that the expert user/builder has a way of turning it off (via the
210             # envvars) without adding yet more GRPC-specific envvars.
211             # See https://sourceforge.net/p/mingw-w64/bugs/363/
212             if '32' in platform.architecture()[0]:
213                 EXTRA_ENV_COMPILE_ARGS += ' -D_ftime=_ftime32 -D_timeb=__timeb32 -D_ftime_s=_ftime32_s'
214             else:
215                 EXTRA_ENV_COMPILE_ARGS += ' -D_ftime=_ftime64 -D_timeb=__timeb64'
216         else:
217             # We need to statically link the C++ Runtime, only the C runtime is
218             # available dynamically
219             EXTRA_ENV_COMPILE_ARGS += ' /MT'
220     elif "linux" in sys.platform:
221         EXTRA_ENV_COMPILE_ARGS += ' -std=gnu99 -fvisibility=hidden -fno-wrapv -fno-exceptions'
222     elif "darwin" in sys.platform:
223         EXTRA_ENV_COMPILE_ARGS += ' -stdlib=libc++ -fvisibility=hidden -fno-wrapv -fno-exceptions'
224
225 if EXTRA_ENV_LINK_ARGS is None:
226     EXTRA_ENV_LINK_ARGS = ''
227     if "linux" in sys.platform or "darwin" in sys.platform:
228         EXTRA_ENV_LINK_ARGS += ' -lpthread'
229         if check_linker_need_libatomic():
230             EXTRA_ENV_LINK_ARGS += ' -latomic'
231     elif "win32" in sys.platform and sys.version_info < (3, 5):
232         msvcr = cygwinccompiler.get_msvcr()[0]
233         EXTRA_ENV_LINK_ARGS += (
234             ' -static-libgcc -static-libstdc++ -mcrtdll={msvcr}'
235             ' -static -lshlwapi'.format(msvcr=msvcr))
236     if "linux" in sys.platform:
237         EXTRA_ENV_LINK_ARGS += ' -Wl,-wrap,memcpy -static-libgcc'
238
239 EXTRA_COMPILE_ARGS = shlex.split(EXTRA_ENV_COMPILE_ARGS)
240 EXTRA_LINK_ARGS = shlex.split(EXTRA_ENV_LINK_ARGS)
241
242 CYTHON_EXTENSION_PACKAGE_NAMES = ()
243
244 CYTHON_EXTENSION_MODULE_NAMES = ('grpc._cython.cygrpc',)
245
246 CYTHON_HELPER_C_FILES = ()
247
248 CORE_C_FILES = tuple(grpc_core_dependencies.CORE_SOURCE_FILES)
249 if "win32" in sys.platform:
250     CORE_C_FILES = filter(lambda x: 'third_party/cares' not in x, CORE_C_FILES)
251
252 if BUILD_WITH_SYSTEM_OPENSSL:
253     CORE_C_FILES = filter(lambda x: 'third_party/boringssl' not in x,
254                           CORE_C_FILES)
255     CORE_C_FILES = filter(lambda x: 'src/boringssl' not in x, CORE_C_FILES)
256     SSL_INCLUDE = (os.path.join('/usr', 'include', 'openssl'),)
257
258 if BUILD_WITH_SYSTEM_ZLIB:
259     CORE_C_FILES = filter(lambda x: 'third_party/zlib' not in x, CORE_C_FILES)
260     ZLIB_INCLUDE = (os.path.join('/usr', 'include'),)
261
262 if BUILD_WITH_SYSTEM_CARES:
263     CORE_C_FILES = filter(lambda x: 'third_party/cares' not in x, CORE_C_FILES)
264     CARES_INCLUDE = (os.path.join('/usr', 'include'),)
265
266 if BUILD_WITH_SYSTEM_RE2:
267     CORE_C_FILES = filter(lambda x: 'third_party/re2' not in x, CORE_C_FILES)
268     RE2_INCLUDE = (os.path.join('/usr', 'include', 're2'),)
269
270 EXTENSION_INCLUDE_DIRECTORIES = ((PYTHON_STEM,) + CORE_INCLUDE + ABSL_INCLUDE +
271                                  ADDRESS_SORTING_INCLUDE + CARES_INCLUDE +
272                                  RE2_INCLUDE + SSL_INCLUDE + UPB_INCLUDE +
273                                  UPB_GRPC_GENERATED_INCLUDE +
274                                  UPBDEFS_GRPC_GENERATED_INCLUDE + ZLIB_INCLUDE)
275
276 EXTENSION_LIBRARIES = ()
277 if "linux" in sys.platform:
278     EXTENSION_LIBRARIES += ('rt',)
279 if not "win32" in sys.platform:
280     EXTENSION_LIBRARIES += ('m',)
281 if "win32" in sys.platform:
282     EXTENSION_LIBRARIES += (
283         'advapi32',
284         'ws2_32',
285         'dbghelp',
286     )
287 if BUILD_WITH_SYSTEM_OPENSSL:
288     EXTENSION_LIBRARIES += (
289         'ssl',
290         'crypto',
291     )
292 if BUILD_WITH_SYSTEM_ZLIB:
293     EXTENSION_LIBRARIES += ('z',)
294 if BUILD_WITH_SYSTEM_CARES:
295     EXTENSION_LIBRARIES += ('cares',)
296 if BUILD_WITH_SYSTEM_RE2:
297     EXTENSION_LIBRARIES += ('re2',)
298
299 DEFINE_MACROS = (('_WIN32_WINNT', 0x600),)
300 asm_files = []
301
302 asm_key = ''
303 if BUILD_WITH_BORING_SSL_ASM and not BUILD_WITH_SYSTEM_OPENSSL:
304     LINUX_X86_64 = 'linux-x86_64'
305     LINUX_ARM = 'linux-arm'
306     if LINUX_X86_64 == util.get_platform():
307         asm_key = 'crypto_linux_x86_64'
308     elif LINUX_ARM == util.get_platform():
309         asm_key = 'crypto_linux_arm'
310     elif "mac" in util.get_platform() and "x86_64" in util.get_platform():
311         asm_key = 'crypto_mac_x86_64'
312     else:
313         print("ASM Builds for BoringSSL currently not supported on:",
314               util.get_platform())
315 if asm_key:
316     asm_files = grpc_core_dependencies.ASM_SOURCE_FILES[asm_key]
317 else:
318     DEFINE_MACROS += (('OPENSSL_NO_ASM', 1),)
319
320 if not DISABLE_LIBC_COMPATIBILITY:
321     DEFINE_MACROS += (('GPR_BACKWARDS_COMPATIBILITY_MODE', 1),)
322
323 if "win32" in sys.platform:
324     # TODO(zyc): Re-enable c-ares on x64 and x86 windows after fixing the
325     # ares_library_init compilation issue
326     DEFINE_MACROS += (
327         ('WIN32_LEAN_AND_MEAN', 1),
328         ('CARES_STATICLIB', 1),
329         ('GRPC_ARES', 0),
330         ('NTDDI_VERSION', 0x06000000),
331         ('NOMINMAX', 1),
332     )
333     if '64bit' in platform.architecture()[0]:
334         DEFINE_MACROS += (('MS_WIN64', 1),)
335     elif sys.version_info >= (3, 5):
336         # For some reason, this is needed to get access to inet_pton/inet_ntop
337         # on msvc, but only for 32 bits
338         DEFINE_MACROS += (('NTDDI_VERSION', 0x06000000),)
339 else:
340     DEFINE_MACROS += (
341         ('HAVE_CONFIG_H', 1),
342         ('GRPC_ENABLE_FORK_SUPPORT', 1),
343     )
344
345 LDFLAGS = tuple(EXTRA_LINK_ARGS)
346 CFLAGS = tuple(EXTRA_COMPILE_ARGS)
347 if "linux" in sys.platform or "darwin" in sys.platform:
348     pymodinit_type = 'PyObject*' if PY3 else 'void'
349     pymodinit = 'extern "C" __attribute__((visibility ("default"))) {}'.format(
350         pymodinit_type)
351     DEFINE_MACROS += (('PyMODINIT_FUNC', pymodinit),)
352     DEFINE_MACROS += (('GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK', 1),)
353
354 # By default, Python3 distutils enforces compatibility of
355 # c plugins (.so files) with the OSX version Python was built with.
356 # We need OSX 10.10, the oldest which supports C++ thread_local.
357 # Python 3.9: Mac OS Big Sur sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') returns int (11)
358 if 'darwin' in sys.platform:
359     mac_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
360     if mac_target:
361         mac_target = pkg_resources.parse_version(str(mac_target))
362         if mac_target < pkg_resources.parse_version('10.10.0'):
363             os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.10'
364             os.environ['_PYTHON_HOST_PLATFORM'] = re.sub(
365                 r'macosx-[0-9]+\.[0-9]+-(.+)', r'macosx-10.10-\1',
366                 util.get_platform())
367
368
369 def cython_extensions_and_necessity():
370     cython_module_files = [
371         os.path.join(PYTHON_STEM,
372                      name.replace('.', '/') + '.pyx')
373         for name in CYTHON_EXTENSION_MODULE_NAMES
374     ]
375     config = os.environ.get('CONFIG', 'opt')
376     prefix = 'libs/' + config + '/'
377     if USE_PREBUILT_GRPC_CORE:
378         extra_objects = [
379             prefix + 'libares.a', prefix + 'libboringssl.a',
380             prefix + 'libgpr.a', prefix + 'libgrpc.a'
381         ]
382         core_c_files = []
383     else:
384         core_c_files = list(CORE_C_FILES)
385         extra_objects = []
386     extensions = [
387         _extension.Extension(
388             name=module_name,
389             sources=([module_file] + list(CYTHON_HELPER_C_FILES) +
390                      core_c_files + asm_files),
391             include_dirs=list(EXTENSION_INCLUDE_DIRECTORIES),
392             libraries=list(EXTENSION_LIBRARIES),
393             define_macros=list(DEFINE_MACROS),
394             extra_objects=extra_objects,
395             extra_compile_args=list(CFLAGS),
396             extra_link_args=list(LDFLAGS),
397         ) for (module_name, module_file
398               ) in zip(list(CYTHON_EXTENSION_MODULE_NAMES), cython_module_files)
399     ]
400     need_cython = BUILD_WITH_CYTHON
401     if not BUILD_WITH_CYTHON:
402         need_cython = need_cython or not commands.check_and_update_cythonization(
403             extensions)
404     # TODO: the strategy for conditional compiling and exposing the aio Cython
405     # dependencies will be revisited by https://github.com/grpc/grpc/issues/19728
406     return commands.try_cythonize(extensions,
407                                   linetracing=ENABLE_CYTHON_TRACING,
408                                   mandatory=BUILD_WITH_CYTHON), need_cython
409
410
411 CYTHON_EXTENSION_MODULES, need_cython = cython_extensions_and_necessity()
412
413 PACKAGE_DIRECTORIES = {
414     '': PYTHON_STEM,
415 }
416
417 INSTALL_REQUIRES = (
418     "six>=1.5.2",
419     "futures>=2.2.0; python_version<'3.2'",
420     "enum34>=1.0.4; python_version<'3.4'",
421 )
422 EXTRAS_REQUIRES = {
423     'protobuf': 'grpcio-tools>={version}'.format(version=grpc_version.VERSION),
424 }
425
426 SETUP_REQUIRES = INSTALL_REQUIRES + (
427     'Sphinx~=1.8.1',
428     'six>=1.10',
429 ) if ENABLE_DOCUMENTATION_BUILD else ()
430
431 try:
432     import Cython
433 except ImportError:
434     if BUILD_WITH_CYTHON:
435         sys.stderr.write(
436             "You requested a Cython build via GRPC_PYTHON_BUILD_WITH_CYTHON, "
437             "but do not have Cython installed. We won't stop you from using "
438             "other commands, but the extension files will fail to build.\n")
439     elif need_cython:
440         sys.stderr.write(
441             'We could not find Cython. Setup may take 10-20 minutes.\n')
442         SETUP_REQUIRES += ('cython>=0.23',)
443
444 COMMAND_CLASS = {
445     'doc': commands.SphinxDocumentation,
446     'build_project_metadata': commands.BuildProjectMetadata,
447     'build_py': commands.BuildPy,
448     'build_ext': commands.BuildExt,
449     'gather': commands.Gather,
450     'clean': commands.Clean,
451 }
452
453 # Ensure that package data is copied over before any commands have been run:
454 credentials_dir = os.path.join(PYTHON_STEM, 'grpc', '_cython', '_credentials')
455 try:
456     os.mkdir(credentials_dir)
457 except OSError:
458     pass
459 shutil.copyfile(os.path.join('etc', 'roots.pem'),
460                 os.path.join(credentials_dir, 'roots.pem'))
461
462 PACKAGE_DATA = {
463     # Binaries that may or may not be present in the final installation, but are
464     # mentioned here for completeness.
465     'grpc._cython': [
466         '_credentials/roots.pem',
467         '_windows/grpc_c.32.python',
468         '_windows/grpc_c.64.python',
469     ],
470 }
471 PACKAGES = setuptools.find_packages(PYTHON_STEM)
472
473 setuptools.setup(
474     name='grpcio',
475     version=grpc_version.VERSION,
476     description='HTTP/2-based RPC framework',
477     author='The gRPC Authors',
478     author_email='grpc-io@googlegroups.com',
479     url='https://grpc.io',
480     license=LICENSE,
481     classifiers=CLASSIFIERS,
482     long_description=open(README).read(),
483     ext_modules=CYTHON_EXTENSION_MODULES,
484     packages=list(PACKAGES),
485     package_dir=PACKAGE_DIRECTORIES,
486     package_data=PACKAGE_DATA,
487     install_requires=INSTALL_REQUIRES,
488     extras_require=EXTRAS_REQUIRES,
489     setup_requires=SETUP_REQUIRES,
490     cmdclass=COMMAND_CLASS,
491 )