From 1d177817958efb72679c55c9ef2136d15c5d9636 Mon Sep 17 00:00:00 2001 From: Masood Malekghassemi <soltanmm@users.noreply.github.com> Date: Tue, 12 Jan 2016 09:21:57 -0800 Subject: [PATCH] Check compiler versions at Python setup time --- setup.py | 11 +++-------- src/python/grpcio/commands.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/setup.py b/setup.py index 85434c92be..2eca48686c 100644 --- a/setup.py +++ b/setup.py @@ -81,15 +81,10 @@ EXTENSION_LIBRARIES = () if not "darwin" in sys.platform: EXTENSION_LIBRARIES += ('rt',) -EXTRA_COMPILE_ARGS = () -if not "win" in sys.platform: - EXTRA_COMPILE_ARGS = ('-pthread',) - DEFINE_MACROS = (('OPENSSL_NO_ASM', 1),) def cython_extensions(package_names, module_names, include_dirs, libraries, - define_macros, extra_compile_args, - build_with_cython=False): + define_macros, build_with_cython=False): if ENABLE_CYTHON_TRACING: define_macros = define_macros + [('CYTHON_TRACE_NOGIL', 1)] file_extension = 'pyx' if build_with_cython else 'c' @@ -101,7 +96,6 @@ def cython_extensions(package_names, module_names, include_dirs, libraries, name=module_name, sources=[module_file] + grpc_core_dependencies.CORE_SOURCE_FILES, include_dirs=include_dirs, libraries=libraries, - extra_compile_args=extra_compile_args, define_macros=define_macros, ) for (module_name, module_file) in zip(module_names, module_files) ] @@ -117,7 +111,7 @@ def cython_extensions(package_names, module_names, include_dirs, libraries, CYTHON_EXTENSION_MODULES = cython_extensions( list(CYTHON_EXTENSION_PACKAGE_NAMES), list(CYTHON_EXTENSION_MODULE_NAMES), list(EXTENSION_INCLUDE_DIRECTORIES), list(EXTENSION_LIBRARIES), - list(DEFINE_MACROS), list(EXTRA_COMPILE_ARGS), bool(BUILD_WITH_CYTHON)) + list(DEFINE_MACROS), bool(BUILD_WITH_CYTHON)) PACKAGE_DIRECTORIES = { '': PYTHON_STEM, @@ -137,6 +131,7 @@ COMMAND_CLASS = { 'build_proto_modules': commands.BuildProtoModules, 'build_project_metadata': commands.BuildProjectMetadata, 'build_py': commands.BuildPy, + 'build_ext': commands.BuildExt, 'gather': commands.Gather, 'run_interop': commands.RunInterop, } diff --git a/src/python/grpcio/commands.py b/src/python/grpcio/commands.py index 6a7df8e1c1..e1a3f4bed3 100644 --- a/src/python/grpcio/commands.py +++ b/src/python/grpcio/commands.py @@ -40,6 +40,17 @@ import setuptools from setuptools.command import build_py from setuptools.command import test +# Because we need to support building without Cython but simultaneously need to +# subclass its command class when we need to and because distutils requires a +# special hook to acquire a command class, we attempt to import Cython's +# build_ext, and if that fails we import setuptools'. +try: + # Due to the strange way Cython's Distutils module re-imports build_ext, we + # import the build_ext class directly. + from Cython.Distutils.build_ext import build_ext +except ImportError: + from setuptools.command.build_ext import build_ext + PYTHON_STEM = os.path.dirname(os.path.abspath(__file__)) CONF_PY_ADDENDUM = """ @@ -168,6 +179,26 @@ class BuildPy(build_py.build_py): build_py.build_py.run(self) +class BuildExt(build_ext): + """Custom build_ext command to enable compiler-specific flags.""" + + C_OPTIONS = { + 'unix': ('-pthread', '-std=gnu99'), + 'msvc': (), + } + LINK_OPTIONS = {} + + def build_extensions(self): + compiler = self.compiler.compiler_type + if compiler in BuildExt.C_OPTIONS: + for extension in self.extensions: + extension.extra_compile_args += list(BuildExt.C_OPTIONS[compiler]) + if compiler in BuildExt.LINK_OPTIONS: + for extension in self.extensions: + extension.extra_link_args += list(BuildExt.LINK_OPTIONS[compiler]) + build_ext.build_extensions(self) + + class Gather(setuptools.Command): """Command to gather project dependencies.""" -- GitLab