blob: 7730374df08204c923660ed44c604f5feb507d4b [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:
48 * You're on Ubuntu and haven't `apt-get install`ed `python-dev`.
49 * You're on Mac OS X and the usual Python framework was somehow corrupted
50 (check your environment variables or try re-installing?)
51 * You're on Windows and your Python installation was somehow corrupted
52 (check your environment variables or try re-installing?)
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080053"""
54
55C_CHECKS = {
56 C_PYTHON_DEV: C_PYTHON_DEV_ERROR_MESSAGE,
57}
58
59def _compile(compiler, source_string):
60 tempdir = tempfile.mkdtemp()
61 cpath = os.path.join(tempdir, 'a.c')
62 with open(cpath, 'w') as cfile:
63 cfile.write(source_string)
64 try:
65 compiler.compile([cpath])
66 except errors.CompileError as error:
67 return error
68 finally:
69 shutil.rmtree(tempdir)
70
71def _expect_compile(compiler, source_string, error_message):
72 if _compile(compiler, source_string) is not None:
73 sys.stderr.write(error_message)
74 raise commands.CommandError(
75 "Diagnostics found a compilation environment issue:\n{}"
76 .format(error_message))
77
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080078def diagnose_compile_error(build_ext, error):
Masood Malekghassemi097070f2016-01-30 14:26:06 -080079 """Attempt to diagnose an error during compilation."""
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080080 for c_check, message in C_CHECKS.items():
81 _expect_compile(build_ext.compiler, c_check, message)
Masood Malekghassemi097070f2016-01-30 14:26:06 -080082 python_sources = [
83 source for source in build_ext.get_source_files()
84 if source.startswith('./src/python') and source.endswith('c')
85 ]
86 for source in python_sources:
87 if not os.path.isfile(source):
88 raise commands.CommandError(
89 ("Diagnostics found a missing Python extension source file:\n{}\n\n"
90 "This is usually because the Cython sources haven't been transpiled "
91 "into C yet and you're building from source.\n"
92 "Try setting the environment variable "
93 "`GRPC_PYTHON_BUILD_WITH_CYTHON=1` when invoking `setup.py` or "
94 "when using `pip`, e.g.:\n\n"
95 "pip install -rrequirements.txt\n"
96 "GRPC_PYTHON_BUILD_WITH_CYTHON=1 pip install .")
97 .format(source)
98 )
99
Masood Malekghassemi62cc9112016-01-28 11:00:24 -0800100
101_ERROR_DIAGNOSES = {
102 errors.CompileError: diagnose_compile_error
103}
104
105def diagnose_build_ext_error(build_ext, error, formatted):
106 diagnostic = _ERROR_DIAGNOSES.get(type(error))
107 if diagnostic is None:
108 raise commands.CommandError(
109 "\n\nWe could not diagnose your build failure. Please file an issue at "
110 "http://www.github.com/grpc/grpc with `[Python install]` in the title."
111 "\n\n{}".format(formatted))
112 else:
113 diagnostic(build_ext, error)
114