changeset 42473:f4a65077e949

rust-cpython: management of shared libray suffix 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.
author Georges Racinet <georges.racinet@octobus.net>
date Fri, 14 Jun 2019 10:57:07 +0100
parents 87a34c767384
children 8ee0fdf3b087
files setup.py
diffstat 1 files changed, 23 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/setup.py	Mon May 27 16:55:46 2019 -0400
+++ b/setup.py	Fri Jun 14 10:57:07 2019 +0100
@@ -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
@@ -1160,6 +1167,19 @@
                                 for fname in fnames
                                 if os.path.splitext(fname)[1] == '.rs')
 
+    @staticmethod
+    def rustdylibsuffix():
+        """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):
         env = os.environ.copy()
         if 'HGTEST_RESTOREENV' in env:
@@ -1227,9 +1247,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.rustdylibsuffix()),
                      os.path.join(*target))