rust: better treatment of cargo/rustc errors
authorGeorges Racinet <gracinet@anybox.fr>
Thu, 06 Dec 2018 16:23:20 +0100
changeset 40966 1eaf62a67c1a
parent 40965 5532823e8c18
child 40967 462a26756f70
rust: better treatment of cargo/rustc errors Differential Revision: https://phab.mercurial-scm.org/D5435
setup.py
--- a/setup.py	Mon Dec 03 06:52:17 2018 +0100
+++ b/setup.py	Thu Dec 06 16:23:20 2018 +0100
@@ -135,6 +135,7 @@
 iswithrustextensions = 'HGWITHRUSTEXT' in os.environ
 
 import ctypes
+import errno
 import stat, subprocess, time
 import re
 import shutil
@@ -898,6 +899,9 @@
     'mercurial/thirdparty/xdiff/xutils.h',
 ]
 
+class RustCompilationError(CCompilerError):
+    """Exception class for Rust compilation errors."""
+
 class RustExtension(Extension):
     """A C Extension, conditionnally enhanced with Rust code.
 
@@ -942,9 +946,21 @@
             import pwd
             env['HOME'] = pwd.getpwuid(os.getuid()).pw_dir
 
-        subprocess.check_call(['cargo', 'build', '-vv', '--release'],
-                              env=env, cwd=self.rustsrcdir)
-        self.library_dirs.append(self.rusttargetdir)
+        cargocmd = ['cargo', 'build', '-vv', '--release']
+        try:
+            subprocess.check_call(cargocmd, env=env, cwd=self.rustsrcdir)
+        except OSError as exc:
+            if exc.errno == errno.ENOENT:
+                raise RustCompilationError("Cargo not found")
+            elif exc.errno == errno.EACCES:
+                raise RustCompilationError(
+                    "Cargo found, but permisssion to execute it is denied")
+            else:
+                raise
+        except subprocess.CalledProcessError:
+            raise RustCompilationError(
+                "Cargo failed. Working directory: %r, "
+                "command: %r, environment: %r" % (self.rustsrcdir, cmd, env))
 
 extmodules = [
     Extension('mercurial.cext.base85', ['mercurial/cext/base85.c'],