Masood Malekghassemi | 58a1dc2 | 2016-01-21 14:23:55 -0800 | [diff] [blame] | 1 | # 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 Malekghassemi | 58a1dc2 | 2016-01-21 14:23:55 -0800 | [diff] [blame] | 30 | import os |
| 31 | import os.path |
| 32 | import shutil |
| 33 | import sys |
| 34 | import tempfile |
| 35 | |
| 36 | from distutils import errors |
| 37 | |
| 38 | import commands |
| 39 | |
Masood Malekghassemi | 58a1dc2 | 2016-01-21 14:23:55 -0800 | [diff] [blame] | 40 | C_PYTHON_DEV = """ |
| 41 | #include <Python.h> |
| 42 | int main(int argc, char **argv) { return 0; } |
| 43 | """ |
| 44 | C_PYTHON_DEV_ERROR_MESSAGE = """ |
| 45 | Could not find <Python.h>. This could mean the following: |
Maciej Lasyk | 39cb9a9 | 2016-10-25 01:12:47 +0200 | [diff] [blame] | 46 | * 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 Malekghassemi | 58a1dc2 | 2016-01-21 14:23:55 -0800 | [diff] [blame] | 50 | * 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 Malekghassemi | 58a1dc2 | 2016-01-21 14:23:55 -0800 | [diff] [blame] | 54 | """ |
| 55 | |
Ken Payson | 2fa5f2f | 2017-02-06 10:27:09 -0800 | [diff] [blame] | 56 | C_CHECKS = { |
| 57 | C_PYTHON_DEV: C_PYTHON_DEV_ERROR_MESSAGE, |
| 58 | } |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 59 | |
Masood Malekghassemi | 58a1dc2 | 2016-01-21 14:23:55 -0800 | [diff] [blame] | 60 | |
| 61 | def _compile(compiler, source_string): |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 62 | 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 Malekghassemi | 58a1dc2 | 2016-01-21 14:23:55 -0800 | [diff] [blame] | 73 | |
| 74 | def _expect_compile(compiler, source_string, error_message): |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 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{}" |
Masood Malekghassemi | 58a1dc2 | 2016-01-21 14:23:55 -0800 | [diff] [blame] | 79 | .format(error_message)) |
| 80 | |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 81 | |
Masood Malekghassemi | 58a1dc2 | 2016-01-21 14:23:55 -0800 | [diff] [blame] | 82 | def diagnose_compile_error(build_ext, error): |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 83 | """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 Malekghassemi | 097070f | 2016-01-30 14:26:06 -0800 | [diff] [blame] | 102 | |
Masood Malekghassemi | 4682bf3 | 2016-12-14 18:42:03 -0800 | [diff] [blame] | 103 | def diagnose_attribute_error(build_ext, error): |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 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.") |
| 108 | |
Masood Malekghassemi | 62cc911 | 2016-01-28 11:00:24 -0800 | [diff] [blame] | 109 | |
| 110 | _ERROR_DIAGNOSES = { |
Masood Malekghassemi | 4682bf3 | 2016-12-14 18:42:03 -0800 | [diff] [blame] | 111 | errors.CompileError: diagnose_compile_error, |
| 112 | AttributeError: diagnose_attribute_error |
Masood Malekghassemi | 62cc911 | 2016-01-28 11:00:24 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Masood Malekghassemi | 62cc911 | 2016-01-28 11:00:24 -0800 | [diff] [blame] | 115 | |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 116 | def 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) |