• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
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
26from 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
39from 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')
49CORE_INCLUDE = (
50    'include',
51    '.',
52)
53ABSL_INCLUDE = (os.path.join('third_party', 'abseil-cpp'),)
54ADDRESS_SORTING_INCLUDE = (os.path.join('third_party', 'address_sorting',
55                                        'include'),)
56CARES_INCLUDE = (
57    os.path.join('third_party', 'cares'),
58    os.path.join('third_party', 'cares', 'cares'),
59)
60if 'darwin' in sys.platform:
61    CARES_INCLUDE += (os.path.join('third_party', 'cares', 'config_darwin'),)
62if 'freebsd' in sys.platform:
63    CARES_INCLUDE += (os.path.join('third_party', 'cares', 'config_freebsd'),)
64if 'linux' in sys.platform:
65    CARES_INCLUDE += (os.path.join('third_party', 'cares', 'config_linux'),)
66if 'openbsd' in sys.platform:
67    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'),)
71UPB_INCLUDE = (os.path.join('third_party', 'upb'),)
72UPB_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'),)
76ZLIB_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',
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
109BUILD_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.
117BUILD_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
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
128BUILD_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
133BUILD_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
139BUILD_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
152USE_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.
157DISABLE_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.
162ENABLE_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.
167ENABLE_DOCUMENTATION_BUILD = os.environ.get(
168    'GRPC_PYTHON_ENABLE_DOCUMENTATION_BUILD', False)
169
170
171def 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.
201EXTRA_ENV_COMPILE_ARGS = os.environ.get('GRPC_PYTHON_CFLAGS', None)
202EXTRA_ENV_LINK_ARGS = os.environ.get('GRPC_PYTHON_LDFLAGS', None)
203if 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
225if 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
239EXTRA_COMPILE_ARGS = shlex.split(EXTRA_ENV_COMPILE_ARGS)
240EXTRA_LINK_ARGS = shlex.split(EXTRA_ENV_LINK_ARGS)
241
242CYTHON_EXTENSION_PACKAGE_NAMES = ()
243
244CYTHON_EXTENSION_MODULE_NAMES = ('grpc._cython.cygrpc',)
245
246CYTHON_HELPER_C_FILES = ()
247
248CORE_C_FILES = tuple(grpc_core_dependencies.CORE_SOURCE_FILES)
249if "win32" in sys.platform:
250    CORE_C_FILES = filter(lambda x: 'third_party/cares' not in x, CORE_C_FILES)
251
252if 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
258if 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
262if 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
266if 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
270EXTENSION_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
276EXTENSION_LIBRARIES = ()
277if "linux" in sys.platform:
278    EXTENSION_LIBRARIES += ('rt',)
279if not "win32" in sys.platform:
280    EXTENSION_LIBRARIES += ('m',)
281if "win32" in sys.platform:
282    EXTENSION_LIBRARIES += (
283        'advapi32',
284        'ws2_32',
285        'dbghelp',
286    )
287if BUILD_WITH_SYSTEM_OPENSSL:
288    EXTENSION_LIBRARIES += (
289        'ssl',
290        'crypto',
291    )
292if BUILD_WITH_SYSTEM_ZLIB:
293    EXTENSION_LIBRARIES += ('z',)
294if BUILD_WITH_SYSTEM_CARES:
295    EXTENSION_LIBRARIES += ('cares',)
296if BUILD_WITH_SYSTEM_RE2:
297    EXTENSION_LIBRARIES += ('re2',)
298
299DEFINE_MACROS = (('_WIN32_WINNT', 0x600),)
300asm_files = []
301
302asm_key = ''
303if 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())
315if asm_key:
316    asm_files = grpc_core_dependencies.ASM_SOURCE_FILES[asm_key]
317else:
318    DEFINE_MACROS += (('OPENSSL_NO_ASM', 1),)
319
320if not DISABLE_LIBC_COMPATIBILITY:
321    DEFINE_MACROS += (('GPR_BACKWARDS_COMPATIBILITY_MODE', 1),)
322
323if "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),)
339else:
340    DEFINE_MACROS += (
341        ('HAVE_CONFIG_H', 1),
342        ('GRPC_ENABLE_FORK_SUPPORT', 1),
343    )
344
345LDFLAGS = tuple(EXTRA_LINK_ARGS)
346CFLAGS = tuple(EXTRA_COMPILE_ARGS)
347if "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.
357if 'darwin' in sys.platform:
358    mac_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
359    if mac_target and (pkg_resources.parse_version(mac_target) <
360                       pkg_resources.parse_version('10.10.0')):
361        os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.10'
362        os.environ['_PYTHON_HOST_PLATFORM'] = re.sub(
363            r'macosx-[0-9]+\.[0-9]+-(.+)', r'macosx-10.10-\1',
364            util.get_platform())
365
366
367def cython_extensions_and_necessity():
368    cython_module_files = [
369        os.path.join(PYTHON_STEM,
370                     name.replace('.', '/') + '.pyx')
371        for name in CYTHON_EXTENSION_MODULE_NAMES
372    ]
373    config = os.environ.get('CONFIG', 'opt')
374    prefix = 'libs/' + config + '/'
375    if USE_PREBUILT_GRPC_CORE:
376        extra_objects = [
377            prefix + 'libares.a', prefix + 'libboringssl.a',
378            prefix + 'libgpr.a', prefix + 'libgrpc.a'
379        ]
380        core_c_files = []
381    else:
382        core_c_files = list(CORE_C_FILES)
383        extra_objects = []
384    extensions = [
385        _extension.Extension(
386            name=module_name,
387            sources=([module_file] + list(CYTHON_HELPER_C_FILES) +
388                     core_c_files + asm_files),
389            include_dirs=list(EXTENSION_INCLUDE_DIRECTORIES),
390            libraries=list(EXTENSION_LIBRARIES),
391            define_macros=list(DEFINE_MACROS),
392            extra_objects=extra_objects,
393            extra_compile_args=list(CFLAGS),
394            extra_link_args=list(LDFLAGS),
395        ) for (module_name, module_file
396              ) in zip(list(CYTHON_EXTENSION_MODULE_NAMES), cython_module_files)
397    ]
398    need_cython = BUILD_WITH_CYTHON
399    if not BUILD_WITH_CYTHON:
400        need_cython = need_cython or not commands.check_and_update_cythonization(
401            extensions)
402    # TODO: the strategy for conditional compiling and exposing the aio Cython
403    # dependencies will be revisited by https://github.com/grpc/grpc/issues/19728
404    return commands.try_cythonize(extensions,
405                                  linetracing=ENABLE_CYTHON_TRACING,
406                                  mandatory=BUILD_WITH_CYTHON), need_cython
407
408
409CYTHON_EXTENSION_MODULES, need_cython = cython_extensions_and_necessity()
410
411PACKAGE_DIRECTORIES = {
412    '': PYTHON_STEM,
413}
414
415INSTALL_REQUIRES = (
416    "six>=1.5.2",
417    "futures>=2.2.0; python_version<'3.2'",
418    "enum34>=1.0.4; python_version<'3.4'",
419)
420EXTRAS_REQUIRES = {
421    'protobuf': 'grpcio-tools>={version}'.format(version=grpc_version.VERSION),
422}
423
424SETUP_REQUIRES = INSTALL_REQUIRES + (
425    'Sphinx~=1.8.1',
426    'six>=1.10',
427) if ENABLE_DOCUMENTATION_BUILD else ()
428
429try:
430    import Cython
431except ImportError:
432    if BUILD_WITH_CYTHON:
433        sys.stderr.write(
434            "You requested a Cython build via GRPC_PYTHON_BUILD_WITH_CYTHON, "
435            "but do not have Cython installed. We won't stop you from using "
436            "other commands, but the extension files will fail to build.\n")
437    elif need_cython:
438        sys.stderr.write(
439            'We could not find Cython. Setup may take 10-20 minutes.\n')
440        SETUP_REQUIRES += ('cython>=0.23',)
441
442COMMAND_CLASS = {
443    'doc': commands.SphinxDocumentation,
444    'build_project_metadata': commands.BuildProjectMetadata,
445    'build_py': commands.BuildPy,
446    'build_ext': commands.BuildExt,
447    'gather': commands.Gather,
448    'clean': commands.Clean,
449}
450
451# Ensure that package data is copied over before any commands have been run:
452credentials_dir = os.path.join(PYTHON_STEM, 'grpc', '_cython', '_credentials')
453try:
454    os.mkdir(credentials_dir)
455except OSError:
456    pass
457shutil.copyfile(os.path.join('etc', 'roots.pem'),
458                os.path.join(credentials_dir, 'roots.pem'))
459
460PACKAGE_DATA = {
461    # Binaries that may or may not be present in the final installation, but are
462    # mentioned here for completeness.
463    'grpc._cython': [
464        '_credentials/roots.pem',
465        '_windows/grpc_c.32.python',
466        '_windows/grpc_c.64.python',
467    ],
468}
469PACKAGES = setuptools.find_packages(PYTHON_STEM)
470
471setuptools.setup(
472    name='grpcio',
473    version=grpc_version.VERSION,
474    description='HTTP/2-based RPC framework',
475    author='The gRPC Authors',
476    author_email='grpc-io@googlegroups.com',
477    url='https://grpc.io',
478    license=LICENSE,
479    classifiers=CLASSIFIERS,
480    long_description=open(README).read(),
481    ext_modules=CYTHON_EXTENSION_MODULES,
482    packages=list(PACKAGES),
483    package_dir=PACKAGE_DIRECTORIES,
484    package_data=PACKAGE_DATA,
485    install_requires=INSTALL_REQUIRES,
486    extras_require=EXTRAS_REQUIRES,
487    setup_requires=SETUP_REQUIRES,
488    cmdclass=COMMAND_CLASS,
489)
490