blob: f2395eb26c2f0a34ac6da911625df2f78afa59ea [file] [log] [blame]
Jan Tattermusch7897ae92017-06-07 22:57:36 +02001# Copyright 2016 gRPC authors.
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -08002#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -08006#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02007# http://www.apache.org/licenses/LICENSE-2.0
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -08008#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080014
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080015import os
16import os.path
17import shutil
18import sys
19import tempfile
20
21from distutils import errors
22
23import commands
24
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080025C_PYTHON_DEV = """
26#include <Python.h>
27int main(int argc, char **argv) { return 0; }
28"""
29C_PYTHON_DEV_ERROR_MESSAGE = """
30Could not find <Python.h>. This could mean the following:
Maciej Lasyk39cb9a92016-10-25 01:12:47 +020031 * You're on Ubuntu and haven't run `apt-get install python-dev`.
32 * You're on RHEL/Fedora and haven't run `yum install python-devel` or
33 `dnf install python-devel` (make sure you also have redhat-rpm-config
34 installed)
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080035 * You're on Mac OS X and the usual Python framework was somehow corrupted
36 (check your environment variables or try re-installing?)
37 * You're on Windows and your Python installation was somehow corrupted
38 (check your environment variables or try re-installing?)
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080039"""
40
Ken Payson2fa5f2f2017-02-06 10:27:09 -080041C_CHECKS = {
42 C_PYTHON_DEV: C_PYTHON_DEV_ERROR_MESSAGE,
43}
Masood Malekghassemicc793702017-01-13 19:20:10 -080044
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080045
46def _compile(compiler, source_string):
Masood Malekghassemicc793702017-01-13 19:20:10 -080047 tempdir = tempfile.mkdtemp()
48 cpath = os.path.join(tempdir, 'a.c')
49 with open(cpath, 'w') as cfile:
50 cfile.write(source_string)
51 try:
52 compiler.compile([cpath])
53 except errors.CompileError as error:
54 return error
55 finally:
56 shutil.rmtree(tempdir)
57
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080058
59def _expect_compile(compiler, source_string, error_message):
Masood Malekghassemicc793702017-01-13 19:20:10 -080060 if _compile(compiler, source_string) is not None:
61 sys.stderr.write(error_message)
62 raise commands.CommandError(
63 "Diagnostics found a compilation environment issue:\n{}"
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080064 .format(error_message))
65
Masood Malekghassemicc793702017-01-13 19:20:10 -080066
Masood Malekghassemi58a1dc22016-01-21 14:23:55 -080067def diagnose_compile_error(build_ext, error):
Masood Malekghassemicc793702017-01-13 19:20:10 -080068 """Attempt to diagnose an error during compilation."""
69 for c_check, message in C_CHECKS.items():
70 _expect_compile(build_ext.compiler, c_check, message)
71 python_sources = [
72 source for source in build_ext.get_source_files()
73 if source.startswith('./src/python') and source.endswith('c')
74 ]
75 for source in python_sources:
76 if not os.path.isfile(source):
77 raise commands.CommandError((
78 "Diagnostics found a missing Python extension source file:\n{}\n\n"
79 "This is usually because the Cython sources haven't been transpiled "
80 "into C yet and you're building from source.\n"
81 "Try setting the environment variable "
82 "`GRPC_PYTHON_BUILD_WITH_CYTHON=1` when invoking `setup.py` or "
83 "when using `pip`, e.g.:\n\n"
84 "pip install -rrequirements.txt\n"
85 "GRPC_PYTHON_BUILD_WITH_CYTHON=1 pip install .").format(source))
86
Masood Malekghassemi097070f2016-01-30 14:26:06 -080087
Masood Malekghassemi4682bf32016-12-14 18:42:03 -080088def diagnose_attribute_error(build_ext, error):
Masood Malekghassemicc793702017-01-13 19:20:10 -080089 if any('_needs_stub' in arg for arg in error.args):
90 raise commands.CommandError(
91 "We expect a missing `_needs_stub` attribute from older versions of "
92 "setuptools. Consider upgrading setuptools.")
93
Masood Malekghassemi62cc9112016-01-28 11:00:24 -080094
95_ERROR_DIAGNOSES = {
Masood Malekghassemi4682bf32016-12-14 18:42:03 -080096 errors.CompileError: diagnose_compile_error,
Mehrdad Afsharie2490792017-09-26 11:25:45 -070097 AttributeError: diagnose_attribute_error,
Masood Malekghassemi62cc9112016-01-28 11:00:24 -080098}
99
Masood Malekghassemi62cc9112016-01-28 11:00:24 -0800100
Masood Malekghassemicc793702017-01-13 19:20:10 -0800101def diagnose_build_ext_error(build_ext, error, formatted):
102 diagnostic = _ERROR_DIAGNOSES.get(type(error))
103 if diagnostic is None:
104 raise commands.CommandError(
Mehrdad Afsharie2490792017-09-26 11:25:45 -0700105 "\n\nWe could not diagnose your build failure. If you are unable to "
106 "proceed, please file an issue at http://www.github.com/grpc/grpc "
107 "with `[Python install]` in the title; please attach the whole log "
108 "(including everything that may have appeared above the Python "
109 "backtrace).\n\n{}".format(formatted))
Masood Malekghassemicc793702017-01-13 19:20:10 -0800110 else:
111 diagnostic(build_ext, error)