Patchwork D6392: rust-cpython: build for py3, Windows and MacOSX

login
register
mail settings
Submitter phabricator
Date May 16, 2019, 7:15 p.m.
Message ID <differential-rev-PHID-DREV-rqbjaovcsuhwveixrvgi-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/40103/
State Superseded
Headers show

Comments

phabricator - May 16, 2019, 7:15 p.m.
gracinet created this revision.
Herald added subscribers: mercurial-devel, kevincox, durin42.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  Before this changeset, the shared library objects suffixes
  were both (rustc output and Python input) hardcoded to '.so',
  which is wrong for Python3 and non Linux targets.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D6392

AFFECTED FILES
  rust/hg-cpython/src/lib.rs
  setup.py

CHANGE DETAILS




To: gracinet, #hg-reviewers
Cc: durin42, kevincox, mercurial-devel

Patch

diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -32,6 +32,7 @@ 
     ])
 
 import sys, platform
+import sysconfig
 if sys.version_info[0] >= 3:
     printf = eval('print')
     libdir_escape = 'unicode_escape'
@@ -104,6 +105,12 @@ 
         printf(error, file=sys.stderr)
         sys.exit(1)
 
+if sys.version_info[0] >= 3:
+    DYLIB_SUFFIX = sysconfig.get_config_vars()['EXT_SUFFIX']
+else:
+    # deprecated in Python 3
+    DYLIB_SUFFIX = sysconfig.get_config_vars()['SO']
+
 # Solaris Python packaging brain damage
 try:
     import hashlib
@@ -1147,6 +1154,19 @@ 
                                 for fname in fnames
                                 if os.path.splitext(fname)[1] == '.rs')
 
+    @staticmethod
+    def rust_dylib_suffix():
+        """Return the suffix for shared libraries produced by rustc.
+
+        See also: https://doc.rust-lang.org/reference/linkage.html
+        """
+        if sys.platform == 'darwin':
+            return '.dylib'
+        elif os.name == 'nt':
+            return '.dll'
+        else:
+            return '.so'
+
     def rustbuild(self):
         if hgrustext is None:
             return
@@ -1212,9 +1232,9 @@ 
         self.rustbuild()
         target = [target_dir]
         target.extend(self.name.split('.'))
-        ext = '.so'  # TODO Unix only
-        target[-1] += ext
-        shutil.copy2(os.path.join(self.rusttargetdir, self.dylibname + ext),
+        target[-1] += DYLIB_SUFFIX
+        shutil.copy2(os.path.join(self.rusttargetdir,
+                                  self.dylibname + self.rust_dylib_suffix()),
                      os.path.join(*target))
 
 
diff --git a/rust/hg-cpython/src/lib.rs b/rust/hg-cpython/src/lib.rs
--- a/rust/hg-cpython/src/lib.rs
+++ b/rust/hg-cpython/src/lib.rs
@@ -23,7 +23,6 @@ 
 extern crate cpython;
 extern crate hg;
 extern crate libc;
-extern crate python27_sys;
 
 pub mod ancestors;
 mod cindex;