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