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