blob: edc6def83866721dd6d5296c269d74150e652955 [file] [log] [blame]
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -08001# Copyright 2016, Google Inc.
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8# * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10# * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14# * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080030import os
31import os.path
32import shutil
33import sys
34import tempfile
35
36from distutils import errors
37
38import commands
39
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080040C_PYTHON_DEV = """
41#include <Python.h>
42int main(int argc, char **argv) { return 0; }
43"""
44C_PYTHON_DEV_ERROR_MESSAGE = """
45Could not find <Python.h>. This could mean the following:
Maciej Lasyk39cb9a92016-10-25 01:12:47 +020046 * You're on Ubuntu and haven't run `apt-get install python-dev`.
47 * You're on RHEL/Fedora and haven't run `yum install python-devel` or
48 `dnf install python-devel` (make sure you also have redhat-rpm-config
49 installed)
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080050 * You're on Mac OS X and the usual Python framework was somehow corrupted
51 (check your environment variables or try re-installing?)
52 * You're on Windows and your Python installation was somehow corrupted
53 (check your environment variables or try re-installing?)
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080054"""
55
Ken Payson2fa5f2f2017-02-06 10:27:09 -080056C_CHECKS = {
57 C_PYTHON_DEV: C_PYTHON_DEV_ERROR_MESSAGE,
58}
Masood Malekghassemicc793702017-01-13 19:20:10 -080059
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080060
61def _compile(compiler, source_string):
Masood Malekghassemicc793702017-01-13 19:20:10 -080062 tempdir = tempfile.mkdtemp()
63 cpath = os.path.join(tempdir, 'a.c')
64 with open(cpath, 'w') as cfile:
65 cfile.write(source_string)
66 try:
67 compiler.compile([cpath])
68 except errors.CompileError as error:
69 return error
70 finally:
71 shutil.rmtree(tempdir)
72
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080073
74def _expect_compile(compiler, source_string, error_message):
Masood Malekghassemicc793702017-01-13 19:20:10 -080075 if _compile(compiler, source_string) is not None:
76 sys.stderr.write(error_message)
77 raise commands.CommandError(
78 "Diagnostics found a compilation environment issue:\n{}"
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080079 .format(error_message))
80
Masood Malekghassemicc793702017-01-13 19:20:10 -080081
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080082def diagnose_compile_error(build_ext, error):
Masood Malekghassemicc793702017-01-13 19:20:10 -080083 """Attempt to diagnose an error during compilation."""
84 for c_check, message in C_CHECKS.items():
85 _expect_compile(build_ext.compiler, c_check, message)
86 python_sources = [
87 source for source in build_ext.get_source_files()
88 if source.startswith('./src/python') and source.endswith('c')
89 ]
90 for source in python_sources:
91 if not os.path.isfile(source):
92 raise commands.CommandError((
93 "Diagnostics found a missing Python extension source file:\n{}\n\n"
94 "This is usually because the Cython sources haven't been transpiled "
95 "into C yet and you're building from source.\n"
96 "Try setting the environment variable "
97 "`GRPC_PYTHON_BUILD_WITH_CYTHON=1` when invoking `setup.py` or "
98 "when using `pip`, e.g.:\n\n"
99 "pip install -rrequirements.txt\n"
100 "GRPC_PYTHON_BUILD_WITH_CYTHON=1 pip install .").format(source))
101
Masood Malekghassemi097070f2016-01-30 14:26:06 -0800102
Masood Malekghassemi4682bf32016-12-14 18:42:03 -0800103def diagnose_attribute_error(build_ext, error):
Masood Malekghassemicc793702017-01-13 19:20:10 -0800104 if any('_needs_stub' in arg for arg in error.args):
105 raise commands.CommandError(
106 "We expect a missing `_needs_stub` attribute from older versions of "
107 "setuptools. Consider upgrading setuptools.")
108
Masood Malekghassemi62cc9112016-01-28 11:00:24 -0800109
110_ERROR_DIAGNOSES = {
Masood Malekghassemi4682bf32016-12-14 18:42:03 -0800111 errors.CompileError: diagnose_compile_error,
112 AttributeError: diagnose_attribute_error
Masood Malekghassemi62cc9112016-01-28 11:00:24 -0800113}
114
Masood Malekghassemi62cc9112016-01-28 11:00:24 -0800115
Masood Malekghassemicc793702017-01-13 19:20:10 -0800116def diagnose_build_ext_error(build_ext, error, formatted):
117 diagnostic = _ERROR_DIAGNOSES.get(type(error))
118 if diagnostic is None:
119 raise commands.CommandError(
120 "\n\nWe could not diagnose your build failure. Please file an issue at "
121 "http://www.github.com/grpc/grpc with `[Python install]` in the title."
122 "\n\n{}".format(formatted))
123 else:
124 diagnostic(build_ext, error)