1# Copyright 2016 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 15from distutils import cygwinccompiler 16from distutils import extension 17from distutils import util 18import errno 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 build_ext 31 32# TODO(atash) add flag to disable Cython use 33 34os.chdir(os.path.dirname(os.path.abspath(__file__))) 35sys.path.insert(0, os.path.abspath('.')) 36 37import protoc_lib_deps 38import grpc_version 39 40CLASSIFIERS = [ 41 'Development Status :: 5 - Production/Stable', 42 'Programming Language :: Python', 43 'Programming Language :: Python :: 2', 44 'Programming Language :: Python :: 2.7', 45 'Programming Language :: Python :: 3', 46 'Programming Language :: Python :: 3.4', 47 'Programming Language :: Python :: 3.5', 48 'Programming Language :: Python :: 3.6', 49 'License :: OSI Approved :: Apache Software License', 50] 51 52PY3 = sys.version_info.major == 3 53 54# Environment variable to determine whether or not the Cython extension should 55# *use* Cython or use the generated C files. Note that this requires the C files 56# to have been generated by building first *with* Cython support. 57BUILD_WITH_CYTHON = os.environ.get('GRPC_PYTHON_BUILD_WITH_CYTHON', False) 58 59# There are some situations (like on Windows) where CC, CFLAGS, and LDFLAGS are 60# entirely ignored/dropped/forgotten by distutils and its Cygwin/MinGW support. 61# We use these environment variables to thus get around that without locking 62# ourselves in w.r.t. the multitude of operating systems this ought to build on. 63# We can also use these variables as a way to inject environment-specific 64# compiler/linker flags. We assume GCC-like compilers and/or MinGW as a 65# reasonable default. 66EXTRA_ENV_COMPILE_ARGS = os.environ.get('GRPC_PYTHON_CFLAGS', None) 67EXTRA_ENV_LINK_ARGS = os.environ.get('GRPC_PYTHON_LDFLAGS', None) 68if EXTRA_ENV_COMPILE_ARGS is None: 69 EXTRA_ENV_COMPILE_ARGS = '-std=c++11' 70 if 'win32' in sys.platform: 71 if sys.version_info < (3, 5): 72 # We use define flags here and don't directly add to DEFINE_MACROS below to 73 # ensure that the expert user/builder has a way of turning it off (via the 74 # envvars) without adding yet more GRPC-specific envvars. 75 # See https://sourceforge.net/p/mingw-w64/bugs/363/ 76 if '32' in platform.architecture()[0]: 77 EXTRA_ENV_COMPILE_ARGS += ' -D_ftime=_ftime32 -D_timeb=__timeb32 -D_ftime_s=_ftime32_s -D_hypot=hypot' 78 else: 79 EXTRA_ENV_COMPILE_ARGS += ' -D_ftime=_ftime64 -D_timeb=__timeb64 -D_hypot=hypot' 80 else: 81 # We need to statically link the C++ Runtime, only the C runtime is 82 # available dynamically 83 EXTRA_ENV_COMPILE_ARGS += ' /MT' 84 elif "linux" in sys.platform or "darwin" in sys.platform: 85 EXTRA_ENV_COMPILE_ARGS += ' -fno-wrapv -frtti' 86if EXTRA_ENV_LINK_ARGS is None: 87 EXTRA_ENV_LINK_ARGS = '' 88 if "linux" in sys.platform or "darwin" in sys.platform: 89 EXTRA_ENV_LINK_ARGS += ' -lpthread' 90 elif "win32" in sys.platform and sys.version_info < (3, 5): 91 msvcr = cygwinccompiler.get_msvcr()[0] 92 # TODO(atash) sift through the GCC specs to see if libstdc++ can have any 93 # influence on the linkage outcome on MinGW for non-C++ programs. 94 EXTRA_ENV_LINK_ARGS += ( 95 ' -static-libgcc -static-libstdc++ -mcrtdll={msvcr} ' 96 '-static'.format(msvcr=msvcr)) 97 98EXTRA_COMPILE_ARGS = shlex.split(EXTRA_ENV_COMPILE_ARGS) 99EXTRA_LINK_ARGS = shlex.split(EXTRA_ENV_LINK_ARGS) 100 101CC_FILES = [os.path.normpath(cc_file) for cc_file in protoc_lib_deps.CC_FILES] 102PROTO_FILES = [ 103 os.path.normpath(proto_file) for proto_file in protoc_lib_deps.PROTO_FILES 104] 105CC_INCLUDE = os.path.normpath(protoc_lib_deps.CC_INCLUDE) 106PROTO_INCLUDE = os.path.normpath(protoc_lib_deps.PROTO_INCLUDE) 107 108GRPC_PYTHON_TOOLS_PACKAGE = 'grpc_tools' 109GRPC_PYTHON_PROTO_RESOURCES_NAME = '_proto' 110 111DEFINE_MACROS = () 112if "win32" in sys.platform: 113 DEFINE_MACROS += (('WIN32_LEAN_AND_MEAN', 1),) 114 if '64bit' in platform.architecture()[0]: 115 DEFINE_MACROS += (('MS_WIN64', 1),) 116elif "linux" in sys.platform or "darwin" in sys.platform: 117 DEFINE_MACROS += (('HAVE_PTHREAD', 1),) 118 119# By default, Python3 distutils enforces compatibility of 120# c plugins (.so files) with the OSX version Python3 was built with. 121# For Python3.4, this is OSX 10.6, but we need Thread Local Support (__thread) 122if 'darwin' in sys.platform and PY3: 123 mac_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') 124 if mac_target and (pkg_resources.parse_version(mac_target) < 125 pkg_resources.parse_version('10.9.0')): 126 os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.9' 127 os.environ['_PYTHON_HOST_PLATFORM'] = re.sub( 128 r'macosx-[0-9]+\.[0-9]+-(.+)', r'macosx-10.9-\1', 129 util.get_platform()) 130 131 132def package_data(): 133 tools_path = GRPC_PYTHON_TOOLS_PACKAGE.replace('.', os.path.sep) 134 proto_resources_path = os.path.join(tools_path, 135 GRPC_PYTHON_PROTO_RESOURCES_NAME) 136 proto_files = [] 137 for proto_file in PROTO_FILES: 138 source = os.path.join(PROTO_INCLUDE, proto_file) 139 target = os.path.join(proto_resources_path, proto_file) 140 relative_target = os.path.join(GRPC_PYTHON_PROTO_RESOURCES_NAME, 141 proto_file) 142 try: 143 os.makedirs(os.path.dirname(target)) 144 except OSError as error: 145 if error.errno == errno.EEXIST: 146 pass 147 else: 148 raise 149 shutil.copy(source, target) 150 proto_files.append(relative_target) 151 return {GRPC_PYTHON_TOOLS_PACKAGE: proto_files} 152 153 154def extension_modules(): 155 if BUILD_WITH_CYTHON: 156 plugin_sources = [os.path.join('grpc_tools', '_protoc_compiler.pyx')] 157 else: 158 plugin_sources = [os.path.join('grpc_tools', '_protoc_compiler.cpp')] 159 160 plugin_sources += [ 161 os.path.join('grpc_tools', 'main.cc'), 162 os.path.join('grpc_root', 'src', 'compiler', 'python_generator.cc') 163 ] + [os.path.join(CC_INCLUDE, cc_file) for cc_file in CC_FILES] 164 165 plugin_ext = extension.Extension( 166 name='grpc_tools._protoc_compiler', 167 sources=plugin_sources, 168 include_dirs=[ 169 '.', 170 'grpc_root', 171 os.path.join('grpc_root', 'include'), 172 CC_INCLUDE, 173 ], 174 language='c++', 175 define_macros=list(DEFINE_MACROS), 176 extra_compile_args=list(EXTRA_COMPILE_ARGS), 177 extra_link_args=list(EXTRA_LINK_ARGS), 178 ) 179 extensions = [plugin_ext] 180 if BUILD_WITH_CYTHON: 181 from Cython import Build 182 return Build.cythonize(extensions) 183 else: 184 return extensions 185 186 187setuptools.setup( 188 name='grpcio-tools', 189 version=grpc_version.VERSION, 190 description='Protobuf code generator for gRPC', 191 author='The gRPC Authors', 192 author_email='grpc-io@googlegroups.com', 193 url='https://grpc.io', 194 license='Apache License 2.0', 195 classifiers=CLASSIFIERS, 196 ext_modules=extension_modules(), 197 packages=setuptools.find_packages('.'), 198 install_requires=[ 199 'protobuf>=3.5.0.post1', 200 'grpcio>={version}'.format(version=grpc_version.VERSION), 201 ], 202 package_data=package_data(), 203) 204