aboutsummaryrefslogtreecommitdiffhomepage
path: root/setup.py
diff options
context:
space:
mode:
Diffstat (limited to 'setup.py')
-rw-r--r--setup.py102
1 files changed, 61 insertions, 41 deletions
diff --git a/setup.py b/setup.py
index 1dddf55e5e..fb9537fccd 100644
--- a/setup.py
+++ b/setup.py
@@ -71,7 +71,9 @@ LICENSE = '3-clause BSD'
# Environment variable to determine whether or not the Cython extension should
# *use* Cython or use the generated C files. Note that this requires the C files
-# to have been generated by building first *with* Cython support.
+# to have been generated by building first *with* Cython support. Even if this
+# is set to false, if the script detects that the generated `.c` file isn't
+# present, then it will still attempt to use Cython.
BUILD_WITH_CYTHON = os.environ.get('GRPC_PYTHON_BUILD_WITH_CYTHON', False)
# Environment variable to determine whether or not to enable coverage analysis
@@ -83,9 +85,40 @@ ENABLE_CYTHON_TRACING = os.environ.get(
# entirely ignored/dropped/forgotten by distutils and its Cygwin/MinGW support.
# We use these environment variables to thus get around that without locking
# ourselves in w.r.t. the multitude of operating systems this ought to build on.
-# By default we assume a GCC-like compiler.
-EXTRA_COMPILE_ARGS = shlex.split(os.environ.get('GRPC_PYTHON_CFLAGS', ''))
-EXTRA_LINK_ARGS = shlex.split(os.environ.get('GRPC_PYTHON_LDFLAGS', ''))
+# We can also use these variables as a way to inject environment-specific
+# compiler/linker flags. We assume GCC-like compilers and/or MinGW as a
+# reasonable default.
+EXTRA_ENV_COMPILE_ARGS = os.environ.get('GRPC_PYTHON_CFLAGS', None)
+EXTRA_ENV_LINK_ARGS = os.environ.get('GRPC_PYTHON_LDFLAGS', None)
+if EXTRA_ENV_COMPILE_ARGS is None:
+ EXTRA_ENV_COMPILE_ARGS = '-fno-wrapv'
+ if 'win32' in sys.platform:
+ # We use define flags here and don't directly add to DEFINE_MACROS below to
+ # ensure that the expert user/builder has a way of turning it off (via the
+ # envvars) without adding yet more GRPC-specific envvars.
+ # See https://sourceforge.net/p/mingw-w64/bugs/363/
+ if '32' in platform.architecture()[0]:
+ EXTRA_ENV_COMPILE_ARGS += ' -D_ftime=_ftime32 -D_timeb=__timeb32 -D_ftime_s=_ftime32_s'
+ else:
+ EXTRA_ENV_COMPILE_ARGS += ' -D_ftime=_ftime64 -D_timeb=__timeb64'
+ elif "linux" in sys.platform or "darwin" in sys.platform:
+ EXTRA_ENV_COMPILE_ARGS += ' -fvisibility=hidden'
+if EXTRA_ENV_LINK_ARGS is None:
+ EXTRA_ENV_LINK_ARGS = '-lpthread'
+ if 'win32' in sys.platform:
+ # TODO(atash) check if this is actually safe to just import and call on
+ # non-Windows (to avoid breaking import style)
+ from distutils.cygwinccompiler import get_msvcr
+ msvcr = get_msvcr()[0]
+ # TODO(atash) sift through the GCC specs to see if libstdc++ can have any
+ # influence on the linkage outcome on MinGW for non-C++ programs.
+ EXTRA_ENV_LINK_ARGS += (
+ ' -static-libgcc -static-libstdc++ -mcrtdll={msvcr} '
+ '-static'.format(msvcr=msvcr))
+ elif "linux" in sys.platform:
+ EXTRA_ENV_LINK_ARGS += ' -Wl,-wrap,memcpy'
+EXTRA_COMPILE_ARGS = shlex.split(EXTRA_ENV_COMPILE_ARGS)
+EXTRA_LINK_ARGS = shlex.split(EXTRA_ENV_LINK_ARGS)
CYTHON_EXTENSION_PACKAGE_NAMES = ()
@@ -116,13 +149,8 @@ if "win32" in sys.platform:
LDFLAGS = tuple(EXTRA_LINK_ARGS)
CFLAGS = tuple(EXTRA_COMPILE_ARGS)
-if "linux" in sys.platform:
- LDFLAGS += ('-Wl,-wrap,memcpy',)
if "linux" in sys.platform or "darwin" in sys.platform:
- CFLAGS += ('-fvisibility=hidden',)
-
pymodinit_type = 'PyObject*' if PY3 else 'void'
-
pymodinit = '__attribute__((visibility ("default"))) {}'.format(pymodinit_type)
DEFINE_MACROS += (('PyMODINIT_FUNC', pymodinit),)
@@ -140,44 +168,27 @@ if 'darwin' in sys.platform and PY3:
r'macosx-10.7-\1',
util.get_platform())
-
-def cython_extensions(module_names, extra_sources, include_dirs,
- libraries, define_macros, build_with_cython=False):
- # Set compiler directives linetrace argument only if we care about tracing;
- # this is due to Cython having different behavior between linetrace being
- # False and linetrace being unset. See issue #5689.
- cython_compiler_directives = {}
- if ENABLE_CYTHON_TRACING:
- define_macros = define_macros + [('CYTHON_TRACE_NOGIL', 1)]
- cython_compiler_directives['linetrace'] = True
- file_extension = 'pyx' if build_with_cython else 'c'
- module_files = [os.path.join(PYTHON_STEM,
- name.replace('.', '/') + '.' + file_extension)
- for name in module_names]
+def cython_extensions_and_necessity():
+ cython_module_files = [os.path.join(PYTHON_STEM,
+ name.replace('.', '/') + '.pyx')
+ for name in CYTHON_EXTENSION_MODULE_NAMES]
extensions = [
_extension.Extension(
name=module_name,
- sources=[module_file] + extra_sources,
- include_dirs=include_dirs, libraries=libraries,
- define_macros=define_macros,
+ sources=[module_file] + list(CYTHON_HELPER_C_FILES) + list(CORE_C_FILES),
+ include_dirs=list(EXTENSION_INCLUDE_DIRECTORIES),
+ libraries=list(EXTENSION_LIBRARIES),
+ define_macros=list(DEFINE_MACROS),
extra_compile_args=list(CFLAGS),
extra_link_args=list(LDFLAGS),
- ) for (module_name, module_file) in zip(module_names, module_files)
+ ) for (module_name, module_file) in zip(list(CYTHON_EXTENSION_MODULE_NAMES), cython_module_files)
]
- if build_with_cython:
- import Cython.Build
- return Cython.Build.cythonize(
- extensions,
- include_path=include_dirs,
- compiler_directives=cython_compiler_directives)
- else:
- return extensions
-
-CYTHON_EXTENSION_MODULES = cython_extensions(
- list(CYTHON_EXTENSION_MODULE_NAMES),
- list(CYTHON_HELPER_C_FILES) + list(CORE_C_FILES),
- list(EXTENSION_INCLUDE_DIRECTORIES), list(EXTENSION_LIBRARIES),
- list(DEFINE_MACROS), bool(BUILD_WITH_CYTHON))
+ need_cython = BUILD_WITH_CYTHON
+ if not BUILD_WITH_CYTHON:
+ need_cython = need_cython or not commands.check_and_update_cythonization(extensions)
+ return commands.try_cythonize(extensions, linetracing=ENABLE_CYTHON_TRACING, mandatory=BUILD_WITH_CYTHON), need_cython
+
+CYTHON_EXTENSION_MODULES, need_cython = cython_extensions_and_necessity()
PACKAGE_DIRECTORIES = {
'': PYTHON_STEM,
@@ -197,6 +208,15 @@ SETUP_REQUIRES = INSTALL_REQUIRES + (
'sphinx_rtd_theme>=0.1.8',
'six>=1.10',
)
+if BUILD_WITH_CYTHON:
+ sys.stderr.write(
+ "You requested a Cython build via GRPC_PYTHON_BUILD_WITH_CYTHON, "
+ "but do not have Cython installed. We won't stop you from using "
+ "other commands, but the extension files will fail to build.\n")
+elif need_cython:
+ sys.stderr.write(
+ 'We could not find Cython. Setup may take 10-20 minutes.\n')
+ SETUP_REQUIRES += ('cython>=0.23',)
COMMAND_CLASS = {
'doc': commands.SphinxDocumentation,