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 15"""A setup module for the GRPC Python package.""" 16from distutils import cygwinccompiler 17from distutils import extension as _extension 18from distutils import util 19import os 20import os.path 21import pkg_resources 22import platform 23import re 24import shlex 25import shutil 26import sys 27import sysconfig 28 29import setuptools 30from setuptools.command import egg_info 31 32# Redirect the manifest template from MANIFEST.in to PYTHON-MANIFEST.in. 33egg_info.manifest_maker.template = 'PYTHON-MANIFEST.in' 34 35PY3 = sys.version_info.major == 3 36PYTHON_STEM = os.path.join('src', 'python', 'grpcio') 37CORE_INCLUDE = ('include', '.',) 38SSL_INCLUDE = (os.path.join('third_party', 'boringssl', 'include'),) 39ZLIB_INCLUDE = (os.path.join('third_party', 'zlib'),) 40NANOPB_INCLUDE = (os.path.join('third_party', 'nanopb'),) 41CARES_INCLUDE = ( 42 os.path.join('third_party', 'cares'), 43 os.path.join('third_party', 'cares', 'cares'),) 44if 'darwin' in sys.platform: 45 CARES_INCLUDE += (os.path.join('third_party', 'cares', 'config_darwin'),) 46if 'freebsd' in sys.platform: 47 CARES_INCLUDE += (os.path.join('third_party', 'cares', 'config_freebsd'),) 48if 'linux' in sys.platform: 49 CARES_INCLUDE += (os.path.join('third_party', 'cares', 'config_linux'),) 50if 'openbsd' in sys.platform: 51 CARES_INCLUDE += (os.path.join('third_party', 'cares', 'config_openbsd'),) 52ADDRESS_SORTING_INCLUDE = (os.path.join('third_party', 'address_sorting', 'include'),) 53README = os.path.join(PYTHON_STEM, 'README.rst') 54 55# Ensure we're in the proper directory whether or not we're being used by pip. 56os.chdir(os.path.dirname(os.path.abspath(__file__))) 57sys.path.insert(0, os.path.abspath(PYTHON_STEM)) 58 59# Break import-style to ensure we can actually find our in-repo dependencies. 60import _spawn_patch 61import commands 62import grpc_core_dependencies 63import grpc_version 64 65_spawn_patch.monkeypatch_spawn() 66 67LICENSE = 'Apache License 2.0' 68 69CLASSIFIERS = [ 70 'Development Status :: 5 - Production/Stable', 71 'Programming Language :: Python', 72 'Programming Language :: Python :: 2', 73 'Programming Language :: Python :: 2.7', 74 'Programming Language :: Python :: 3', 75 'Programming Language :: Python :: 3.4', 76 'Programming Language :: Python :: 3.5', 77 'Programming Language :: Python :: 3.6', 78 'License :: OSI Approved :: Apache Software License', 79] 80 81# Environment variable to determine whether or not the Cython extension should 82# *use* Cython or use the generated C files. Note that this requires the C files 83# to have been generated by building first *with* Cython support. Even if this 84# is set to false, if the script detects that the generated `.c` file isn't 85# present, then it will still attempt to use Cython. 86BUILD_WITH_CYTHON = os.environ.get('GRPC_PYTHON_BUILD_WITH_CYTHON', False) 87 88# Export this variable to use the system installation of openssl. You need to 89# have the header files installed (in /usr/include/openssl) and during 90# runtime, the shared libary must be installed 91BUILD_WITH_SYSTEM_OPENSSL = os.environ.get('GRPC_PYTHON_BUILD_SYSTEM_OPENSSL', 92 False) 93 94# Export this variable to use the system installation of zlib. You need to 95# have the header files installed (in /usr/include/) and during 96# runtime, the shared libary must be installed 97BUILD_WITH_SYSTEM_ZLIB = os.environ.get('GRPC_PYTHON_BUILD_SYSTEM_ZLIB', 98 False) 99 100# Export this variable to use the system installation of cares. You need to 101# have the header files installed (in /usr/include/) and during 102# runtime, the shared libary must be installed 103BUILD_WITH_SYSTEM_CARES = os.environ.get('GRPC_PYTHON_BUILD_SYSTEM_CARES', 104 False) 105 106# Environment variable to determine whether or not to enable coverage analysis 107# in Cython modules. 108ENABLE_CYTHON_TRACING = os.environ.get( 109 'GRPC_PYTHON_ENABLE_CYTHON_TRACING', False) 110 111# Environment variable specifying whether or not there's interest in setting up 112# documentation building. 113ENABLE_DOCUMENTATION_BUILD = os.environ.get( 114 'GRPC_PYTHON_ENABLE_DOCUMENTATION_BUILD', False) 115 116# There are some situations (like on Windows) where CC, CFLAGS, and LDFLAGS are 117# entirely ignored/dropped/forgotten by distutils and its Cygwin/MinGW support. 118# We use these environment variables to thus get around that without locking 119# ourselves in w.r.t. the multitude of operating systems this ought to build on. 120# We can also use these variables as a way to inject environment-specific 121# compiler/linker flags. We assume GCC-like compilers and/or MinGW as a 122# reasonable default. 123EXTRA_ENV_COMPILE_ARGS = os.environ.get('GRPC_PYTHON_CFLAGS', None) 124EXTRA_ENV_LINK_ARGS = os.environ.get('GRPC_PYTHON_LDFLAGS', None) 125if EXTRA_ENV_COMPILE_ARGS is None: 126 EXTRA_ENV_COMPILE_ARGS = ' -std=c++11' 127 if 'win32' in sys.platform and sys.version_info < (3, 5): 128 EXTRA_ENV_COMPILE_ARGS += ' -D_hypot=hypot' 129 # We use define flags here and don't directly add to DEFINE_MACROS below to 130 # ensure that the expert user/builder has a way of turning it off (via the 131 # envvars) without adding yet more GRPC-specific envvars. 132 # See https://sourceforge.net/p/mingw-w64/bugs/363/ 133 if '32' in platform.architecture()[0]: 134 EXTRA_ENV_COMPILE_ARGS += ' -D_ftime=_ftime32 -D_timeb=__timeb32 -D_ftime_s=_ftime32_s' 135 else: 136 EXTRA_ENV_COMPILE_ARGS += ' -D_ftime=_ftime64 -D_timeb=__timeb64' 137 elif "linux" in sys.platform: 138 EXTRA_ENV_COMPILE_ARGS += ' -std=gnu99 -fvisibility=hidden -fno-wrapv -fno-exceptions' 139 elif "darwin" in sys.platform: 140 EXTRA_ENV_COMPILE_ARGS += ' -fvisibility=hidden -fno-wrapv -fno-exceptions' 141EXTRA_ENV_COMPILE_ARGS += ' -DPB_FIELD_16BIT' 142 143if EXTRA_ENV_LINK_ARGS is None: 144 EXTRA_ENV_LINK_ARGS = '' 145 if "linux" in sys.platform or "darwin" in sys.platform: 146 EXTRA_ENV_LINK_ARGS += ' -lpthread' 147 elif "win32" in sys.platform and sys.version_info < (3, 5): 148 msvcr = cygwinccompiler.get_msvcr()[0] 149 # TODO(atash) sift through the GCC specs to see if libstdc++ can have any 150 # influence on the linkage outcome on MinGW for non-C++ programs. 151 EXTRA_ENV_LINK_ARGS += ( 152 ' -static-libgcc -static-libstdc++ -mcrtdll={msvcr} ' 153 '-static'.format(msvcr=msvcr)) 154 if "linux" in sys.platform: 155 EXTRA_ENV_LINK_ARGS += ' -Wl,-wrap,memcpy -static-libgcc' 156 157EXTRA_COMPILE_ARGS = shlex.split(EXTRA_ENV_COMPILE_ARGS) 158EXTRA_LINK_ARGS = shlex.split(EXTRA_ENV_LINK_ARGS) 159 160CYTHON_EXTENSION_PACKAGE_NAMES = () 161 162CYTHON_EXTENSION_MODULE_NAMES = ('grpc._cython.cygrpc',) 163 164CYTHON_HELPER_C_FILES = () 165 166CORE_C_FILES = tuple(grpc_core_dependencies.CORE_SOURCE_FILES) 167if "win32" in sys.platform: 168 CORE_C_FILES = filter(lambda x: 'third_party/cares' not in x, CORE_C_FILES) 169 170if BUILD_WITH_SYSTEM_OPENSSL: 171 CORE_C_FILES = filter(lambda x: 'third_party/boringssl' not in x, CORE_C_FILES) 172 CORE_C_FILES = filter(lambda x: 'src/boringssl' not in x, CORE_C_FILES) 173 SSL_INCLUDE = (os.path.join('/usr', 'include', 'openssl'),) 174 175if BUILD_WITH_SYSTEM_ZLIB: 176 CORE_C_FILES = filter(lambda x: 'third_party/zlib' not in x, CORE_C_FILES) 177 ZLIB_INCLUDE = (os.path.join('/usr', 'include'),) 178 179if BUILD_WITH_SYSTEM_CARES: 180 CORE_C_FILES = filter(lambda x: 'third_party/cares' not in x, CORE_C_FILES) 181 CARES_INCLUDE = (os.path.join('/usr', 'include'),) 182 183EXTENSION_INCLUDE_DIRECTORIES = ( 184 (PYTHON_STEM,) + CORE_INCLUDE + SSL_INCLUDE + ZLIB_INCLUDE + 185 NANOPB_INCLUDE + CARES_INCLUDE + ADDRESS_SORTING_INCLUDE) 186 187EXTENSION_LIBRARIES = () 188if "linux" in sys.platform: 189 EXTENSION_LIBRARIES += ('rt',) 190if not "win32" in sys.platform: 191 EXTENSION_LIBRARIES += ('m',) 192if "win32" in sys.platform: 193 EXTENSION_LIBRARIES += ('advapi32', 'ws2_32',) 194if BUILD_WITH_SYSTEM_OPENSSL: 195 EXTENSION_LIBRARIES += ('ssl', 'crypto',) 196if BUILD_WITH_SYSTEM_ZLIB: 197 EXTENSION_LIBRARIES += ('z',) 198if BUILD_WITH_SYSTEM_CARES: 199 EXTENSION_LIBRARIES += ('cares',) 200 201DEFINE_MACROS = ( 202 ('OPENSSL_NO_ASM', 1), ('_WIN32_WINNT', 0x600), 203 ('GPR_BACKWARDS_COMPATIBILITY_MODE', 1)) 204if "win32" in sys.platform: 205 # TODO(zyc): Re-enble c-ares on x64 and x86 windows after fixing the 206 # ares_library_init compilation issue 207 DEFINE_MACROS += (('WIN32_LEAN_AND_MEAN', 1), ('CARES_STATICLIB', 1), 208 ('GRPC_ARES', 0), ('NTDDI_VERSION', 0x06000000), 209 ('NOMINMAX', 1),) 210 if '64bit' in platform.architecture()[0]: 211 DEFINE_MACROS += (('MS_WIN64', 1),) 212 elif sys.version_info >= (3, 5): 213 # For some reason, this is needed to get access to inet_pton/inet_ntop 214 # on msvc, but only for 32 bits 215 DEFINE_MACROS += (('NTDDI_VERSION', 0x06000000),) 216else: 217 DEFINE_MACROS += (('HAVE_CONFIG_H', 1), ('GRPC_ENABLE_FORK_SUPPORT', 1),) 218 219LDFLAGS = tuple(EXTRA_LINK_ARGS) 220CFLAGS = tuple(EXTRA_COMPILE_ARGS) 221if "linux" in sys.platform or "darwin" in sys.platform: 222 pymodinit_type = 'PyObject*' if PY3 else 'void' 223 pymodinit = 'extern "C" __attribute__((visibility ("default"))) {}'.format(pymodinit_type) 224 DEFINE_MACROS += (('PyMODINIT_FUNC', pymodinit),) 225 DEFINE_MACROS += (('GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK', 1),) 226 227# By default, Python3 distutils enforces compatibility of 228# c plugins (.so files) with the OSX version Python3 was built with. 229# For Python3.4, this is OSX 10.6, but we need Thread Local Support (__thread) 230if 'darwin' in sys.platform and PY3: 231 mac_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') 232 if mac_target and (pkg_resources.parse_version(mac_target) < 233 pkg_resources.parse_version('10.7.0')): 234 os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.7' 235 os.environ['_PYTHON_HOST_PLATFORM'] = re.sub( 236 r'macosx-[0-9]+\.[0-9]+-(.+)', 237 r'macosx-10.7-\1', 238 util.get_platform()) 239 240def cython_extensions_and_necessity(): 241 cython_module_files = [os.path.join(PYTHON_STEM, 242 name.replace('.', '/') + '.pyx') 243 for name in CYTHON_EXTENSION_MODULE_NAMES] 244 config = os.environ.get('CONFIG', 'opt') 245 prefix = 'libs/' + config + '/' 246 if "darwin" in sys.platform: 247 extra_objects = [prefix + 'libares.a', 248 prefix + 'libboringssl.a', 249 prefix + 'libgpr.a', 250 prefix + 'libgrpc.a'] 251 core_c_files = [] 252 else: 253 core_c_files = list(CORE_C_FILES) 254 extra_objects = [] 255 extensions = [ 256 _extension.Extension( 257 name=module_name, 258 sources=[module_file] + list(CYTHON_HELPER_C_FILES) + core_c_files, 259 include_dirs=list(EXTENSION_INCLUDE_DIRECTORIES), 260 libraries=list(EXTENSION_LIBRARIES), 261 define_macros=list(DEFINE_MACROS), 262 extra_objects=extra_objects, 263 extra_compile_args=list(CFLAGS), 264 extra_link_args=list(LDFLAGS), 265 ) for (module_name, module_file) in zip(list(CYTHON_EXTENSION_MODULE_NAMES), cython_module_files) 266 ] 267 need_cython = BUILD_WITH_CYTHON 268 if not BUILD_WITH_CYTHON: 269 need_cython = need_cython or not commands.check_and_update_cythonization(extensions) 270 return commands.try_cythonize(extensions, linetracing=ENABLE_CYTHON_TRACING, mandatory=BUILD_WITH_CYTHON), need_cython 271 272CYTHON_EXTENSION_MODULES, need_cython = cython_extensions_and_necessity() 273 274PACKAGE_DIRECTORIES = { 275 '': PYTHON_STEM, 276} 277 278INSTALL_REQUIRES = ( 279 'six>=1.5.2', 280) 281 282if not PY3: 283 INSTALL_REQUIRES += ('futures>=2.2.0', 'enum34>=1.0.4') 284 285SETUP_REQUIRES = INSTALL_REQUIRES + ( 286 'sphinx>=1.3', 287 'sphinx_rtd_theme>=0.1.8', 288 'six>=1.10', 289 ) if ENABLE_DOCUMENTATION_BUILD else () 290 291try: 292 import Cython 293except ImportError: 294 if BUILD_WITH_CYTHON: 295 sys.stderr.write( 296 "You requested a Cython build via GRPC_PYTHON_BUILD_WITH_CYTHON, " 297 "but do not have Cython installed. We won't stop you from using " 298 "other commands, but the extension files will fail to build.\n") 299 elif need_cython: 300 sys.stderr.write( 301 'We could not find Cython. Setup may take 10-20 minutes.\n') 302 SETUP_REQUIRES += ('cython>=0.23',) 303 304COMMAND_CLASS = { 305 'doc': commands.SphinxDocumentation, 306 'build_project_metadata': commands.BuildProjectMetadata, 307 'build_py': commands.BuildPy, 308 'build_ext': commands.BuildExt, 309 'gather': commands.Gather, 310} 311 312# Ensure that package data is copied over before any commands have been run: 313credentials_dir = os.path.join(PYTHON_STEM, 'grpc', '_cython', '_credentials') 314try: 315 os.mkdir(credentials_dir) 316except OSError: 317 pass 318shutil.copyfile(os.path.join('etc', 'roots.pem'), 319 os.path.join(credentials_dir, 'roots.pem')) 320 321PACKAGE_DATA = { 322 # Binaries that may or may not be present in the final installation, but are 323 # mentioned here for completeness. 324 'grpc._cython': [ 325 '_credentials/roots.pem', 326 '_windows/grpc_c.32.python', 327 '_windows/grpc_c.64.python', 328 ], 329} 330PACKAGES = setuptools.find_packages(PYTHON_STEM) 331 332setuptools.setup( 333 name='grpcio', 334 version=grpc_version.VERSION, 335 description='HTTP/2-based RPC framework', 336 author='The gRPC Authors', 337 author_email='grpc-io@googlegroups.com', 338 url='https://grpc.io', 339 license=LICENSE, 340 classifiers=CLASSIFIERS, 341 long_description=open(README).read(), 342 ext_modules=CYTHON_EXTENSION_MODULES, 343 packages=list(PACKAGES), 344 package_dir=PACKAGE_DIRECTORIES, 345 package_data=PACKAGE_DATA, 346 install_requires=INSTALL_REQUIRES, 347 setup_requires=SETUP_REQUIRES, 348 cmdclass=COMMAND_CLASS, 349) 350