blob: b226e690fdd3b18a838ae69ca2df51bed5bd10c2 [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
30
31import os
32import os.path
33import shutil
34import sys
35import tempfile
36
37from distutils import errors
38
39import commands
40
41
42C_PYTHON_DEV = """
43#include <Python.h>
44int main(int argc, char **argv) { return 0; }
45"""
46C_PYTHON_DEV_ERROR_MESSAGE = """
47Could not find <Python.h>. This could mean the following:
Maciej Lasyk39cb9a92016-10-25 01:12:47 +020048 * You're on Ubuntu and haven't run `apt-get install python-dev`.
49 * You're on RHEL/Fedora and haven't run `yum install python-devel` or
50 `dnf install python-devel` (make sure you also have redhat-rpm-config
51 installed)
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080052 * You're on Mac OS X and the usual Python framework was somehow corrupted
53 (check your environment variables or try re-installing?)
54 * You're on Windows and your Python installation was somehow corrupted
55 (check your environment variables or try re-installing?)
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080056"""
57
58C_CHECKS = {
59 C_PYTHON_DEV: C_PYTHON_DEV_ERROR_MESSAGE,
60}
61
62def _compile(compiler, source_string):
63 tempdir = tempfile.mkdtemp()
64 cpath = os.path.join(tempdir, 'a.c')
65 with open(cpath, 'w') as cfile:
66 cfile.write(source_string)
67 try:
68 compiler.compile([cpath])
69 except errors.CompileError as error:
70 return error
71 finally:
72 shutil.rmtree(tempdir)
73
74def _expect_compile(compiler, source_string, error_message):
75 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{}"
79 .format(error_message))
80
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080081def diagnose_compile_error(build_ext, error):
Masood Malekghassemi097070f2016-01-30 14:26:06 -080082 """Attempt to diagnose an error during compilation."""
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080083 for c_check, message in C_CHECKS.items():
84 _expect_compile(build_ext.compiler, c_check, message)
Masood Malekghassemi097070f2016-01-30 14:26:06 -080085 python_sources = [
86 source for source in build_ext.get_source_files()
87 if source.startswith('./src/python') and source.endswith('c')
88 ]
89 for source in python_sources:
90 if not os.path.isfile(source):
91 raise commands.CommandError(
92 ("Diagnostics found a missing Python extension source file:\n{}\n\n"
93 "This is usually because the Cython sources haven't been transpiled "
94 "into C yet and you're building from source.\n"
95 "Try setting the environment variable "
96 "`GRPC_PYTHON_BUILD_WITH_CYTHON=1` when invoking `setup.py` or "
97 "when using `pip`, e.g.:\n\n"
98 "pip install -rrequirements.txt\n"
99 "GRPC_PYTHON_BUILD_WITH_CYTHON=1 pip install .")
100 .format(source)
101 )
102
Masood Malekghassemi4682bf32016-12-14 18:42:03 -0800103def diagnose_attribute_error(build_ext, error):
104 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.")
Masood Malekghassemi62cc9112016-01-28 11:00:24 -0800108
109_ERROR_DIAGNOSES = {
Masood Malekghassemi4682bf32016-12-14 18:42:03 -0800110 errors.CompileError: diagnose_compile_error,
111 AttributeError: diagnose_attribute_error
Masood Malekghassemi62cc9112016-01-28 11:00:24 -0800112}
113
114def diagnose_build_ext_error(build_ext, error, formatted):
115 diagnostic = _ERROR_DIAGNOSES.get(type(error))
116 if diagnostic is None:
117 raise commands.CommandError(
118 "\n\nWe could not diagnose your build failure. Please file an issue at "
119 "http://www.github.com/grpc/grpc with `[Python install]` in the title."
120 "\n\n{}".format(formatted))
121 else:
122 diagnostic(build_ext, error)
123